Explain vc-BACKEND-root in Commentary; nfc.
[emacs.git] / src / xdisp.c
blobcb4967c705afd56c07297214900779ed10303c7b
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, 2007, 2008 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 3, 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 "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192 #include "fontset.h"
193 #include "blockinput.h"
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef MAC_OS
202 #include "macterm.h"
203 #endif
205 #ifdef HAVE_WINDOW_SYSTEM
206 #ifdef USE_FONT_BACKEND
207 #include "font.h"
208 #endif /* USE_FONT_BACKEND */
209 #endif /* HAVE_WINDOW_SYSTEM */
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
215 #define INFINITY 10000000
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
218 || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
223 extern int interrupt_input;
224 extern int command_loop_level;
226 extern Lisp_Object do_mouse_tracking;
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
243 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
244 Lisp_Object Qinhibit_point_motion_hooks;
245 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
246 Lisp_Object Qfontified;
247 Lisp_Object Qgrow_only;
248 Lisp_Object Qinhibit_eval_during_redisplay;
249 Lisp_Object Qbuffer_position, Qposition, Qobject;
251 /* Cursor shapes */
252 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
254 /* Pointer shapes */
255 Lisp_Object Qarrow, Qhand, Qtext;
257 Lisp_Object Qrisky_local_variable;
259 /* Holds the list (error). */
260 Lisp_Object list_of_error;
262 /* Functions called to fontify regions of text. */
264 Lisp_Object Vfontification_functions;
265 Lisp_Object Qfontification_functions;
267 /* Non-nil means automatically select any window when the mouse
268 cursor moves into it. */
269 Lisp_Object Vmouse_autoselect_window;
271 /* Non-zero means draw tool bar buttons raised when the mouse moves
272 over them. */
274 int auto_raise_tool_bar_buttons_p;
276 /* Non-zero means to reposition window if cursor line is only partially visible. */
278 int make_cursor_line_fully_visible_p;
280 /* Margin below tool bar in pixels. 0 or nil means no margin.
281 If value is `internal-border-width' or `border-width',
282 the corresponding frame parameter is used. */
284 Lisp_Object Vtool_bar_border;
286 /* Margin around tool bar buttons in pixels. */
288 Lisp_Object Vtool_bar_button_margin;
290 /* Thickness of shadow to draw around tool bar buttons. */
292 EMACS_INT tool_bar_button_relief;
294 /* Non-nil means automatically resize tool-bars so that all tool-bar
295 items are visible, and no blank lines remain.
297 If value is `grow-only', only make tool-bar bigger. */
299 Lisp_Object Vauto_resize_tool_bars;
301 /* Non-zero means draw block and hollow cursor as wide as the glyph
302 under it. For example, if a block cursor is over a tab, it will be
303 drawn as wide as that tab on the display. */
305 int x_stretch_cursor_p;
307 /* Non-nil means don't actually do any redisplay. */
309 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
311 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
313 int inhibit_eval_during_redisplay;
315 /* Names of text properties relevant for redisplay. */
317 Lisp_Object Qdisplay;
318 extern Lisp_Object Qface, Qinvisible, Qwidth;
320 /* Symbols used in text property values. */
322 Lisp_Object Vdisplay_pixels_per_inch;
323 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
324 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
325 Lisp_Object Qslice;
326 Lisp_Object Qcenter;
327 Lisp_Object Qmargin, Qpointer;
328 Lisp_Object Qline_height;
329 extern Lisp_Object Qheight;
330 extern Lisp_Object QCwidth, QCheight, QCascent;
331 extern Lisp_Object Qscroll_bar;
332 extern Lisp_Object Qcursor;
334 /* Non-nil means highlight trailing whitespace. */
336 Lisp_Object Vshow_trailing_whitespace;
338 /* Non-nil means escape non-break space and hyphens. */
340 Lisp_Object Vnobreak_char_display;
342 #ifdef HAVE_WINDOW_SYSTEM
343 extern Lisp_Object Voverflow_newline_into_fringe;
345 /* Test if overflow newline into fringe. Called with iterator IT
346 at or past right window margin, and with IT->current_x set. */
348 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
349 (!NILP (Voverflow_newline_into_fringe) \
350 && FRAME_WINDOW_P (it->f) \
351 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
352 && it->current_x == it->last_visible_x)
354 #endif /* HAVE_WINDOW_SYSTEM */
356 /* Non-nil means show the text cursor in void text areas
357 i.e. in blank areas after eol and eob. This used to be
358 the default in 21.3. */
360 Lisp_Object Vvoid_text_area_pointer;
362 /* Name of the face used to highlight trailing whitespace. */
364 Lisp_Object Qtrailing_whitespace;
366 /* Name and number of the face used to highlight escape glyphs. */
368 Lisp_Object Qescape_glyph;
370 /* Name and number of the face used to highlight non-breaking spaces. */
372 Lisp_Object Qnobreak_space;
374 /* The symbol `image' which is the car of the lists used to represent
375 images in Lisp. */
377 Lisp_Object Qimage;
379 /* The image map types. */
380 Lisp_Object QCmap, QCpointer;
381 Lisp_Object Qrect, Qcircle, Qpoly;
383 /* Non-zero means print newline to stdout before next mini-buffer
384 message. */
386 int noninteractive_need_newline;
388 /* Non-zero means print newline to message log before next message. */
390 static int message_log_need_newline;
392 /* Three markers that message_dolog uses.
393 It could allocate them itself, but that causes trouble
394 in handling memory-full errors. */
395 static Lisp_Object message_dolog_marker1;
396 static Lisp_Object message_dolog_marker2;
397 static Lisp_Object message_dolog_marker3;
399 /* The buffer position of the first character appearing entirely or
400 partially on the line of the selected window which contains the
401 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
402 redisplay optimization in redisplay_internal. */
404 static struct text_pos this_line_start_pos;
406 /* Number of characters past the end of the line above, including the
407 terminating newline. */
409 static struct text_pos this_line_end_pos;
411 /* The vertical positions and the height of this line. */
413 static int this_line_vpos;
414 static int this_line_y;
415 static int this_line_pixel_height;
417 /* X position at which this display line starts. Usually zero;
418 negative if first character is partially visible. */
420 static int this_line_start_x;
422 /* Buffer that this_line_.* variables are referring to. */
424 static struct buffer *this_line_buffer;
426 /* Nonzero means truncate lines in all windows less wide than the
427 frame. */
429 int truncate_partial_width_windows;
431 /* A flag to control how to display unibyte 8-bit character. */
433 int unibyte_display_via_language_environment;
435 /* Nonzero means we have more than one non-mini-buffer-only frame.
436 Not guaranteed to be accurate except while parsing
437 frame-title-format. */
439 int multiple_frames;
441 Lisp_Object Vglobal_mode_string;
444 /* List of variables (symbols) which hold markers for overlay arrows.
445 The symbols on this list are examined during redisplay to determine
446 where to display overlay arrows. */
448 Lisp_Object Voverlay_arrow_variable_list;
450 /* Marker for where to display an arrow on top of the buffer text. */
452 Lisp_Object Voverlay_arrow_position;
454 /* String to display for the arrow. Only used on terminal frames. */
456 Lisp_Object Voverlay_arrow_string;
458 /* Values of those variables at last redisplay are stored as
459 properties on `overlay-arrow-position' symbol. However, if
460 Voverlay_arrow_position is a marker, last-arrow-position is its
461 numerical position. */
463 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
465 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
466 properties on a symbol in overlay-arrow-variable-list. */
468 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
470 /* Like mode-line-format, but for the title bar on a visible frame. */
472 Lisp_Object Vframe_title_format;
474 /* Like mode-line-format, but for the title bar on an iconified frame. */
476 Lisp_Object Vicon_title_format;
478 /* List of functions to call when a window's size changes. These
479 functions get one arg, a frame on which one or more windows' sizes
480 have changed. */
482 static Lisp_Object Vwindow_size_change_functions;
484 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
486 /* Nonzero if an overlay arrow has been displayed in this window. */
488 static int overlay_arrow_seen;
490 /* Nonzero means highlight the region even in nonselected windows. */
492 int highlight_nonselected_windows;
494 /* If cursor motion alone moves point off frame, try scrolling this
495 many lines up or down if that will bring it back. */
497 static EMACS_INT scroll_step;
499 /* Nonzero means scroll just far enough to bring point back on the
500 screen, when appropriate. */
502 static EMACS_INT scroll_conservatively;
504 /* Recenter the window whenever point gets within this many lines of
505 the top or bottom of the window. This value is translated into a
506 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
507 that there is really a fixed pixel height scroll margin. */
509 EMACS_INT scroll_margin;
511 /* Number of windows showing the buffer of the selected window (or
512 another buffer with the same base buffer). keyboard.c refers to
513 this. */
515 int buffer_shared;
517 /* Vector containing glyphs for an ellipsis `...'. */
519 static Lisp_Object default_invis_vector[3];
521 /* Zero means display the mode-line/header-line/menu-bar in the default face
522 (this slightly odd definition is for compatibility with previous versions
523 of emacs), non-zero means display them using their respective faces.
525 This variable is deprecated. */
527 int mode_line_inverse_video;
529 /* Prompt to display in front of the mini-buffer contents. */
531 Lisp_Object minibuf_prompt;
533 /* Width of current mini-buffer prompt. Only set after display_line
534 of the line that contains the prompt. */
536 int minibuf_prompt_width;
538 /* This is the window where the echo area message was displayed. It
539 is always a mini-buffer window, but it may not be the same window
540 currently active as a mini-buffer. */
542 Lisp_Object echo_area_window;
544 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
545 pushes the current message and the value of
546 message_enable_multibyte on the stack, the function restore_message
547 pops the stack and displays MESSAGE again. */
549 Lisp_Object Vmessage_stack;
551 /* Nonzero means multibyte characters were enabled when the echo area
552 message was specified. */
554 int message_enable_multibyte;
556 /* Nonzero if we should redraw the mode lines on the next redisplay. */
558 int update_mode_lines;
560 /* Nonzero if window sizes or contents have changed since last
561 redisplay that finished. */
563 int windows_or_buffers_changed;
565 /* Nonzero means a frame's cursor type has been changed. */
567 int cursor_type_changed;
569 /* Nonzero after display_mode_line if %l was used and it displayed a
570 line number. */
572 int line_number_displayed;
574 /* Maximum buffer size for which to display line numbers. */
576 Lisp_Object Vline_number_display_limit;
578 /* Line width to consider when repositioning for line number display. */
580 static EMACS_INT line_number_display_limit_width;
582 /* Number of lines to keep in the message log buffer. t means
583 infinite. nil means don't log at all. */
585 Lisp_Object Vmessage_log_max;
587 /* The name of the *Messages* buffer, a string. */
589 static Lisp_Object Vmessages_buffer_name;
591 /* Current, index 0, and last displayed echo area message. Either
592 buffers from echo_buffers, or nil to indicate no message. */
594 Lisp_Object echo_area_buffer[2];
596 /* The buffers referenced from echo_area_buffer. */
598 static Lisp_Object echo_buffer[2];
600 /* A vector saved used in with_area_buffer to reduce consing. */
602 static Lisp_Object Vwith_echo_area_save_vector;
604 /* Non-zero means display_echo_area should display the last echo area
605 message again. Set by redisplay_preserve_echo_area. */
607 static int display_last_displayed_message_p;
609 /* Nonzero if echo area is being used by print; zero if being used by
610 message. */
612 int message_buf_print;
614 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
616 Lisp_Object Qinhibit_menubar_update;
617 int inhibit_menubar_update;
619 /* When evaluating expressions from menu bar items (enable conditions,
620 for instance), this is the frame they are being processed for. */
622 Lisp_Object Vmenu_updating_frame;
624 /* Maximum height for resizing mini-windows. Either a float
625 specifying a fraction of the available height, or an integer
626 specifying a number of lines. */
628 Lisp_Object Vmax_mini_window_height;
630 /* Non-zero means messages should be displayed with truncated
631 lines instead of being continued. */
633 int message_truncate_lines;
634 Lisp_Object Qmessage_truncate_lines;
636 /* Set to 1 in clear_message to make redisplay_internal aware
637 of an emptied echo area. */
639 static int message_cleared_p;
641 /* How to blink the default frame cursor off. */
642 Lisp_Object Vblink_cursor_alist;
644 /* A scratch glyph row with contents used for generating truncation
645 glyphs. Also used in direct_output_for_insert. */
647 #define MAX_SCRATCH_GLYPHS 100
648 struct glyph_row scratch_glyph_row;
649 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
651 /* Ascent and height of the last line processed by move_it_to. */
653 static int last_max_ascent, last_height;
655 /* Non-zero if there's a help-echo in the echo area. */
657 int help_echo_showing_p;
659 /* If >= 0, computed, exact values of mode-line and header-line height
660 to use in the macros CURRENT_MODE_LINE_HEIGHT and
661 CURRENT_HEADER_LINE_HEIGHT. */
663 int current_mode_line_height, current_header_line_height;
665 /* The maximum distance to look ahead for text properties. Values
666 that are too small let us call compute_char_face and similar
667 functions too often which is expensive. Values that are too large
668 let us call compute_char_face and alike too often because we
669 might not be interested in text properties that far away. */
671 #define TEXT_PROP_DISTANCE_LIMIT 100
673 #if GLYPH_DEBUG
675 /* Variables to turn off display optimizations from Lisp. */
677 int inhibit_try_window_id, inhibit_try_window_reusing;
678 int inhibit_try_cursor_movement;
680 /* Non-zero means print traces of redisplay if compiled with
681 GLYPH_DEBUG != 0. */
683 int trace_redisplay_p;
685 #endif /* GLYPH_DEBUG */
687 #ifdef DEBUG_TRACE_MOVE
688 /* Non-zero means trace with TRACE_MOVE to stderr. */
689 int trace_move;
691 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
692 #else
693 #define TRACE_MOVE(x) (void) 0
694 #endif
696 /* Non-zero means automatically scroll windows horizontally to make
697 point visible. */
699 int automatic_hscrolling_p;
700 Lisp_Object Qauto_hscroll_mode;
702 /* How close to the margin can point get before the window is scrolled
703 horizontally. */
704 EMACS_INT hscroll_margin;
706 /* How much to scroll horizontally when point is inside the above margin. */
707 Lisp_Object Vhscroll_step;
709 /* The variable `resize-mini-windows'. If nil, don't resize
710 mini-windows. If t, always resize them to fit the text they
711 display. If `grow-only', let mini-windows grow only until they
712 become empty. */
714 Lisp_Object Vresize_mini_windows;
716 /* Buffer being redisplayed -- for redisplay_window_error. */
718 struct buffer *displayed_buffer;
720 /* Space between overline and text. */
722 EMACS_INT overline_margin;
724 /* Value returned from text property handlers (see below). */
726 enum prop_handled
728 HANDLED_NORMALLY,
729 HANDLED_RECOMPUTE_PROPS,
730 HANDLED_OVERLAY_STRING_CONSUMED,
731 HANDLED_RETURN
734 /* A description of text properties that redisplay is interested
735 in. */
737 struct props
739 /* The name of the property. */
740 Lisp_Object *name;
742 /* A unique index for the property. */
743 enum prop_idx idx;
745 /* A handler function called to set up iterator IT from the property
746 at IT's current position. Value is used to steer handle_stop. */
747 enum prop_handled (*handler) P_ ((struct it *it));
750 static enum prop_handled handle_face_prop P_ ((struct it *));
751 static enum prop_handled handle_invisible_prop P_ ((struct it *));
752 static enum prop_handled handle_display_prop P_ ((struct it *));
753 static enum prop_handled handle_composition_prop P_ ((struct it *));
754 static enum prop_handled handle_overlay_change P_ ((struct it *));
755 static enum prop_handled handle_fontified_prop P_ ((struct it *));
756 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
758 /* Properties handled by iterators. */
760 static struct props it_props[] =
762 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
763 /* Handle `face' before `display' because some sub-properties of
764 `display' need to know the face. */
765 {&Qface, FACE_PROP_IDX, handle_face_prop},
766 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
767 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
768 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
769 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
770 {NULL, 0, NULL}
773 /* Value is the position described by X. If X is a marker, value is
774 the marker_position of X. Otherwise, value is X. */
776 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
778 /* Enumeration returned by some move_it_.* functions internally. */
780 enum move_it_result
782 /* Not used. Undefined value. */
783 MOVE_UNDEFINED,
785 /* Move ended at the requested buffer position or ZV. */
786 MOVE_POS_MATCH_OR_ZV,
788 /* Move ended at the requested X pixel position. */
789 MOVE_X_REACHED,
791 /* Move within a line ended at the end of a line that must be
792 continued. */
793 MOVE_LINE_CONTINUED,
795 /* Move within a line ended at the end of a line that would
796 be displayed truncated. */
797 MOVE_LINE_TRUNCATED,
799 /* Move within a line ended at a line end. */
800 MOVE_NEWLINE_OR_CR
803 /* This counter is used to clear the face cache every once in a while
804 in redisplay_internal. It is incremented for each redisplay.
805 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
806 cleared. */
808 #define CLEAR_FACE_CACHE_COUNT 500
809 static int clear_face_cache_count;
811 /* Similarly for the image cache. */
813 #ifdef HAVE_WINDOW_SYSTEM
814 #define CLEAR_IMAGE_CACHE_COUNT 101
815 static int clear_image_cache_count;
816 #endif
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, Lisp_Object,
969 struct text_pos *, 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 Lisp_Object window, prop;
1335 XSETWINDOW (window, w);
1336 prop = Fget_char_property (make_number (it.position.charpos),
1337 Qinvisible, window);
1339 /* If charpos coincides with invisible text covered with an
1340 ellipsis, use the first glyph of the ellipsis to compute
1341 the pixel positions. */
1342 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1344 struct glyph_row *row = it.glyph_row;
1345 struct glyph *glyph = row->glyphs[TEXT_AREA];
1346 struct glyph *end = glyph + row->used[TEXT_AREA];
1347 int x = row->x;
1349 for (; glyph < end && glyph->charpos < charpos; glyph++)
1350 x += glyph->pixel_width;
1352 top_x = x;
1355 *x = top_x;
1356 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1357 *rtop = max (0, window_top_y - top_y);
1358 *rbot = max (0, bottom_y - it.last_visible_y);
1359 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1360 - max (top_y, window_top_y)));
1361 *vpos = it.vpos;
1364 else
1366 struct it it2;
1368 it2 = it;
1369 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1370 move_it_by_lines (&it, 1, 0);
1371 if (charpos < IT_CHARPOS (it)
1372 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1374 visible_p = 1;
1375 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1376 *x = it2.current_x;
1377 *y = it2.current_y + it2.max_ascent - it2.ascent;
1378 *rtop = max (0, -it2.current_y);
1379 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1380 - it.last_visible_y));
1381 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1382 it.last_visible_y)
1383 - max (it2.current_y,
1384 WINDOW_HEADER_LINE_HEIGHT (w))));
1385 *vpos = it2.vpos;
1389 if (old_buffer)
1390 set_buffer_internal_1 (old_buffer);
1392 current_header_line_height = current_mode_line_height = -1;
1394 if (visible_p && XFASTINT (w->hscroll) > 0)
1395 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1397 #if 0
1398 /* Debugging code. */
1399 if (visible_p)
1400 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1401 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1402 else
1403 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1404 #endif
1406 return visible_p;
1410 /* Return the next character from STR which is MAXLEN bytes long.
1411 Return in *LEN the length of the character. This is like
1412 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1413 we find one, we return a `?', but with the length of the invalid
1414 character. */
1416 static INLINE int
1417 string_char_and_length (str, maxlen, len)
1418 const unsigned char *str;
1419 int maxlen, *len;
1421 int c;
1423 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1424 if (!CHAR_VALID_P (c, 1))
1425 /* We may not change the length here because other places in Emacs
1426 don't use this function, i.e. they silently accept invalid
1427 characters. */
1428 c = '?';
1430 return c;
1435 /* Given a position POS containing a valid character and byte position
1436 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1438 static struct text_pos
1439 string_pos_nchars_ahead (pos, string, nchars)
1440 struct text_pos pos;
1441 Lisp_Object string;
1442 int nchars;
1444 xassert (STRINGP (string) && nchars >= 0);
1446 if (STRING_MULTIBYTE (string))
1448 int rest = SBYTES (string) - BYTEPOS (pos);
1449 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1450 int len;
1452 while (nchars--)
1454 string_char_and_length (p, rest, &len);
1455 p += len, rest -= len;
1456 xassert (rest >= 0);
1457 CHARPOS (pos) += 1;
1458 BYTEPOS (pos) += len;
1461 else
1462 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1464 return pos;
1468 /* Value is the text position, i.e. character and byte position,
1469 for character position CHARPOS in STRING. */
1471 static INLINE struct text_pos
1472 string_pos (charpos, string)
1473 int charpos;
1474 Lisp_Object string;
1476 struct text_pos pos;
1477 xassert (STRINGP (string));
1478 xassert (charpos >= 0);
1479 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1480 return pos;
1484 /* Value is a text position, i.e. character and byte position, for
1485 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1486 means recognize multibyte characters. */
1488 static struct text_pos
1489 c_string_pos (charpos, s, multibyte_p)
1490 int charpos;
1491 unsigned char *s;
1492 int multibyte_p;
1494 struct text_pos pos;
1496 xassert (s != NULL);
1497 xassert (charpos >= 0);
1499 if (multibyte_p)
1501 int rest = strlen (s), len;
1503 SET_TEXT_POS (pos, 0, 0);
1504 while (charpos--)
1506 string_char_and_length (s, rest, &len);
1507 s += len, rest -= len;
1508 xassert (rest >= 0);
1509 CHARPOS (pos) += 1;
1510 BYTEPOS (pos) += len;
1513 else
1514 SET_TEXT_POS (pos, charpos, charpos);
1516 return pos;
1520 /* Value is the number of characters in C string S. MULTIBYTE_P
1521 non-zero means recognize multibyte characters. */
1523 static int
1524 number_of_chars (s, multibyte_p)
1525 unsigned char *s;
1526 int multibyte_p;
1528 int nchars;
1530 if (multibyte_p)
1532 int rest = strlen (s), len;
1533 unsigned char *p = (unsigned char *) s;
1535 for (nchars = 0; rest > 0; ++nchars)
1537 string_char_and_length (p, rest, &len);
1538 rest -= len, p += len;
1541 else
1542 nchars = strlen (s);
1544 return nchars;
1548 /* Compute byte position NEWPOS->bytepos corresponding to
1549 NEWPOS->charpos. POS is a known position in string STRING.
1550 NEWPOS->charpos must be >= POS.charpos. */
1552 static void
1553 compute_string_pos (newpos, pos, string)
1554 struct text_pos *newpos, pos;
1555 Lisp_Object string;
1557 xassert (STRINGP (string));
1558 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1560 if (STRING_MULTIBYTE (string))
1561 *newpos = string_pos_nchars_ahead (pos, string,
1562 CHARPOS (*newpos) - CHARPOS (pos));
1563 else
1564 BYTEPOS (*newpos) = CHARPOS (*newpos);
1567 /* EXPORT:
1568 Return an estimation of the pixel height of mode or top lines on
1569 frame F. FACE_ID specifies what line's height to estimate. */
1572 estimate_mode_line_height (f, face_id)
1573 struct frame *f;
1574 enum face_id face_id;
1576 #ifdef HAVE_WINDOW_SYSTEM
1577 if (FRAME_WINDOW_P (f))
1579 int height = FONT_HEIGHT (FRAME_FONT (f));
1581 /* This function is called so early when Emacs starts that the face
1582 cache and mode line face are not yet initialized. */
1583 if (FRAME_FACE_CACHE (f))
1585 struct face *face = FACE_FROM_ID (f, face_id);
1586 if (face)
1588 if (face->font)
1589 height = FONT_HEIGHT (face->font);
1590 if (face->box_line_width > 0)
1591 height += 2 * face->box_line_width;
1595 return height;
1597 #endif
1599 return 1;
1602 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1603 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1604 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1605 not force the value into range. */
1607 void
1608 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1609 FRAME_PTR f;
1610 register int pix_x, pix_y;
1611 int *x, *y;
1612 NativeRectangle *bounds;
1613 int noclip;
1616 #ifdef HAVE_WINDOW_SYSTEM
1617 if (FRAME_WINDOW_P (f))
1619 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1620 even for negative values. */
1621 if (pix_x < 0)
1622 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1623 if (pix_y < 0)
1624 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1626 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1627 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1629 if (bounds)
1630 STORE_NATIVE_RECT (*bounds,
1631 FRAME_COL_TO_PIXEL_X (f, pix_x),
1632 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1633 FRAME_COLUMN_WIDTH (f) - 1,
1634 FRAME_LINE_HEIGHT (f) - 1);
1636 if (!noclip)
1638 if (pix_x < 0)
1639 pix_x = 0;
1640 else if (pix_x > FRAME_TOTAL_COLS (f))
1641 pix_x = FRAME_TOTAL_COLS (f);
1643 if (pix_y < 0)
1644 pix_y = 0;
1645 else if (pix_y > FRAME_LINES (f))
1646 pix_y = FRAME_LINES (f);
1649 #endif
1651 *x = pix_x;
1652 *y = pix_y;
1656 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1657 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1658 can't tell the positions because W's display is not up to date,
1659 return 0. */
1662 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1663 struct window *w;
1664 int hpos, vpos;
1665 int *frame_x, *frame_y;
1667 #ifdef HAVE_WINDOW_SYSTEM
1668 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1670 int success_p;
1672 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1673 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1675 if (display_completed)
1677 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1678 struct glyph *glyph = row->glyphs[TEXT_AREA];
1679 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1681 hpos = row->x;
1682 vpos = row->y;
1683 while (glyph < end)
1685 hpos += glyph->pixel_width;
1686 ++glyph;
1689 /* If first glyph is partially visible, its first visible position is still 0. */
1690 if (hpos < 0)
1691 hpos = 0;
1693 success_p = 1;
1695 else
1697 hpos = vpos = 0;
1698 success_p = 0;
1701 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1702 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1703 return success_p;
1705 #endif
1707 *frame_x = hpos;
1708 *frame_y = vpos;
1709 return 1;
1713 #ifdef HAVE_WINDOW_SYSTEM
1715 /* Find the glyph under window-relative coordinates X/Y in window W.
1716 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1717 strings. Return in *HPOS and *VPOS the row and column number of
1718 the glyph found. Return in *AREA the glyph area containing X.
1719 Value is a pointer to the glyph found or null if X/Y is not on
1720 text, or we can't tell because W's current matrix is not up to
1721 date. */
1723 static struct glyph *
1724 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1725 struct window *w;
1726 int x, y;
1727 int *hpos, *vpos, *dx, *dy, *area;
1729 struct glyph *glyph, *end;
1730 struct glyph_row *row = NULL;
1731 int x0, i;
1733 /* Find row containing Y. Give up if some row is not enabled. */
1734 for (i = 0; i < w->current_matrix->nrows; ++i)
1736 row = MATRIX_ROW (w->current_matrix, i);
1737 if (!row->enabled_p)
1738 return NULL;
1739 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1740 break;
1743 *vpos = i;
1744 *hpos = 0;
1746 /* Give up if Y is not in the window. */
1747 if (i == w->current_matrix->nrows)
1748 return NULL;
1750 /* Get the glyph area containing X. */
1751 if (w->pseudo_window_p)
1753 *area = TEXT_AREA;
1754 x0 = 0;
1756 else
1758 if (x < window_box_left_offset (w, TEXT_AREA))
1760 *area = LEFT_MARGIN_AREA;
1761 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1763 else if (x < window_box_right_offset (w, TEXT_AREA))
1765 *area = TEXT_AREA;
1766 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1768 else
1770 *area = RIGHT_MARGIN_AREA;
1771 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1775 /* Find glyph containing X. */
1776 glyph = row->glyphs[*area];
1777 end = glyph + row->used[*area];
1778 x -= x0;
1779 while (glyph < end && x >= glyph->pixel_width)
1781 x -= glyph->pixel_width;
1782 ++glyph;
1785 if (glyph == end)
1786 return NULL;
1788 if (dx)
1790 *dx = x;
1791 *dy = y - (row->y + row->ascent - glyph->ascent);
1794 *hpos = glyph - row->glyphs[*area];
1795 return glyph;
1799 /* EXPORT:
1800 Convert frame-relative x/y to coordinates relative to window W.
1801 Takes pseudo-windows into account. */
1803 void
1804 frame_to_window_pixel_xy (w, x, y)
1805 struct window *w;
1806 int *x, *y;
1808 if (w->pseudo_window_p)
1810 /* A pseudo-window is always full-width, and starts at the
1811 left edge of the frame, plus a frame border. */
1812 struct frame *f = XFRAME (w->frame);
1813 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1814 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1816 else
1818 *x -= WINDOW_LEFT_EDGE_X (w);
1819 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1823 /* EXPORT:
1824 Return in RECTS[] at most N clipping rectangles for glyph string S.
1825 Return the number of stored rectangles. */
1828 get_glyph_string_clip_rects (s, rects, n)
1829 struct glyph_string *s;
1830 NativeRectangle *rects;
1831 int n;
1833 XRectangle r;
1835 if (n <= 0)
1836 return 0;
1838 if (s->row->full_width_p)
1840 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1841 r.x = WINDOW_LEFT_EDGE_X (s->w);
1842 r.width = WINDOW_TOTAL_WIDTH (s->w);
1844 /* Unless displaying a mode or menu bar line, which are always
1845 fully visible, clip to the visible part of the row. */
1846 if (s->w->pseudo_window_p)
1847 r.height = s->row->visible_height;
1848 else
1849 r.height = s->height;
1851 else
1853 /* This is a text line that may be partially visible. */
1854 r.x = window_box_left (s->w, s->area);
1855 r.width = window_box_width (s->w, s->area);
1856 r.height = s->row->visible_height;
1859 if (s->clip_head)
1860 if (r.x < s->clip_head->x)
1862 if (r.width >= s->clip_head->x - r.x)
1863 r.width -= s->clip_head->x - r.x;
1864 else
1865 r.width = 0;
1866 r.x = s->clip_head->x;
1868 if (s->clip_tail)
1869 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1871 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1872 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1873 else
1874 r.width = 0;
1877 /* If S draws overlapping rows, it's sufficient to use the top and
1878 bottom of the window for clipping because this glyph string
1879 intentionally draws over other lines. */
1880 if (s->for_overlaps)
1882 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1883 r.height = window_text_bottom_y (s->w) - r.y;
1885 /* Alas, the above simple strategy does not work for the
1886 environments with anti-aliased text: if the same text is
1887 drawn onto the same place multiple times, it gets thicker.
1888 If the overlap we are processing is for the erased cursor, we
1889 take the intersection with the rectagle of the cursor. */
1890 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1892 XRectangle rc, r_save = r;
1894 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1895 rc.y = s->w->phys_cursor.y;
1896 rc.width = s->w->phys_cursor_width;
1897 rc.height = s->w->phys_cursor_height;
1899 x_intersect_rectangles (&r_save, &rc, &r);
1902 else
1904 /* Don't use S->y for clipping because it doesn't take partially
1905 visible lines into account. For example, it can be negative for
1906 partially visible lines at the top of a window. */
1907 if (!s->row->full_width_p
1908 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1909 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1910 else
1911 r.y = max (0, s->row->y);
1913 /* If drawing a tool-bar window, draw it over the internal border
1914 at the top of the window. */
1915 if (WINDOWP (s->f->tool_bar_window)
1916 && s->w == XWINDOW (s->f->tool_bar_window))
1917 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1920 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1922 /* If drawing the cursor, don't let glyph draw outside its
1923 advertised boundaries. Cleartype does this under some circumstances. */
1924 if (s->hl == DRAW_CURSOR)
1926 struct glyph *glyph = s->first_glyph;
1927 int height, max_y;
1929 if (s->x > r.x)
1931 r.width -= s->x - r.x;
1932 r.x = s->x;
1934 r.width = min (r.width, glyph->pixel_width);
1936 /* If r.y is below window bottom, ensure that we still see a cursor. */
1937 height = min (glyph->ascent + glyph->descent,
1938 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1939 max_y = window_text_bottom_y (s->w) - height;
1940 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1941 if (s->ybase - glyph->ascent > max_y)
1943 r.y = max_y;
1944 r.height = height;
1946 else
1948 /* Don't draw cursor glyph taller than our actual glyph. */
1949 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1950 if (height < r.height)
1952 max_y = r.y + r.height;
1953 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1954 r.height = min (max_y - r.y, height);
1959 if (s->row->clip)
1961 XRectangle r_save = r;
1963 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1964 r.width = 0;
1967 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1968 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1970 #ifdef CONVERT_FROM_XRECT
1971 CONVERT_FROM_XRECT (r, *rects);
1972 #else
1973 *rects = r;
1974 #endif
1975 return 1;
1977 else
1979 /* If we are processing overlapping and allowed to return
1980 multiple clipping rectangles, we exclude the row of the glyph
1981 string from the clipping rectangle. This is to avoid drawing
1982 the same text on the environment with anti-aliasing. */
1983 #ifdef CONVERT_FROM_XRECT
1984 XRectangle rs[2];
1985 #else
1986 XRectangle *rs = rects;
1987 #endif
1988 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1990 if (s->for_overlaps & OVERLAPS_PRED)
1992 rs[i] = r;
1993 if (r.y + r.height > row_y)
1995 if (r.y < row_y)
1996 rs[i].height = row_y - r.y;
1997 else
1998 rs[i].height = 0;
2000 i++;
2002 if (s->for_overlaps & OVERLAPS_SUCC)
2004 rs[i] = r;
2005 if (r.y < row_y + s->row->visible_height)
2007 if (r.y + r.height > row_y + s->row->visible_height)
2009 rs[i].y = row_y + s->row->visible_height;
2010 rs[i].height = r.y + r.height - rs[i].y;
2012 else
2013 rs[i].height = 0;
2015 i++;
2018 n = i;
2019 #ifdef CONVERT_FROM_XRECT
2020 for (i = 0; i < n; i++)
2021 CONVERT_FROM_XRECT (rs[i], rects[i]);
2022 #endif
2023 return n;
2027 /* EXPORT:
2028 Return in *NR the clipping rectangle for glyph string S. */
2030 void
2031 get_glyph_string_clip_rect (s, nr)
2032 struct glyph_string *s;
2033 NativeRectangle *nr;
2035 get_glyph_string_clip_rects (s, nr, 1);
2039 /* EXPORT:
2040 Return the position and height of the phys cursor in window W.
2041 Set w->phys_cursor_width to width of phys cursor.
2044 void
2045 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2046 struct window *w;
2047 struct glyph_row *row;
2048 struct glyph *glyph;
2049 int *xp, *yp, *heightp;
2051 struct frame *f = XFRAME (WINDOW_FRAME (w));
2052 int x, y, wd, h, h0, y0;
2054 /* Compute the width of the rectangle to draw. If on a stretch
2055 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2056 rectangle as wide as the glyph, but use a canonical character
2057 width instead. */
2058 wd = glyph->pixel_width - 1;
2059 #ifdef HAVE_NTGUI
2060 wd++; /* Why? */
2061 #endif
2063 x = w->phys_cursor.x;
2064 if (x < 0)
2066 wd += x;
2067 x = 0;
2070 if (glyph->type == STRETCH_GLYPH
2071 && !x_stretch_cursor_p)
2072 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2073 w->phys_cursor_width = wd;
2075 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2077 /* If y is below window bottom, ensure that we still see a cursor. */
2078 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2080 h = max (h0, glyph->ascent + glyph->descent);
2081 h0 = min (h0, glyph->ascent + glyph->descent);
2083 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2084 if (y < y0)
2086 h = max (h - (y0 - y) + 1, h0);
2087 y = y0 - 1;
2089 else
2091 y0 = window_text_bottom_y (w) - h0;
2092 if (y > y0)
2094 h += y - y0;
2095 y = y0;
2099 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2100 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2101 *heightp = h;
2105 * Remember which glyph the mouse is over.
2108 void
2109 remember_mouse_glyph (f, gx, gy, rect)
2110 struct frame *f;
2111 int gx, gy;
2112 NativeRectangle *rect;
2114 Lisp_Object window;
2115 struct window *w;
2116 struct glyph_row *r, *gr, *end_row;
2117 enum window_part part;
2118 enum glyph_row_area area;
2119 int x, y, width, height;
2121 /* Try to determine frame pixel position and size of the glyph under
2122 frame pixel coordinates X/Y on frame F. */
2124 if (!f->glyphs_initialized_p
2125 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2126 NILP (window)))
2128 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2129 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2130 goto virtual_glyph;
2133 w = XWINDOW (window);
2134 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2135 height = WINDOW_FRAME_LINE_HEIGHT (w);
2137 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2138 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2140 if (w->pseudo_window_p)
2142 area = TEXT_AREA;
2143 part = ON_MODE_LINE; /* Don't adjust margin. */
2144 goto text_glyph;
2147 switch (part)
2149 case ON_LEFT_MARGIN:
2150 area = LEFT_MARGIN_AREA;
2151 goto text_glyph;
2153 case ON_RIGHT_MARGIN:
2154 area = RIGHT_MARGIN_AREA;
2155 goto text_glyph;
2157 case ON_HEADER_LINE:
2158 case ON_MODE_LINE:
2159 gr = (part == ON_HEADER_LINE
2160 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2161 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2162 gy = gr->y;
2163 area = TEXT_AREA;
2164 goto text_glyph_row_found;
2166 case ON_TEXT:
2167 area = TEXT_AREA;
2169 text_glyph:
2170 gr = 0; gy = 0;
2171 for (; r <= end_row && r->enabled_p; ++r)
2172 if (r->y + r->height > y)
2174 gr = r; gy = r->y;
2175 break;
2178 text_glyph_row_found:
2179 if (gr && gy <= y)
2181 struct glyph *g = gr->glyphs[area];
2182 struct glyph *end = g + gr->used[area];
2184 height = gr->height;
2185 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2186 if (gx + g->pixel_width > x)
2187 break;
2189 if (g < end)
2191 if (g->type == IMAGE_GLYPH)
2193 /* Don't remember when mouse is over image, as
2194 image may have hot-spots. */
2195 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2196 return;
2198 width = g->pixel_width;
2200 else
2202 /* Use nominal char spacing at end of line. */
2203 x -= gx;
2204 gx += (x / width) * width;
2207 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2208 gx += window_box_left_offset (w, area);
2210 else
2212 /* Use nominal line height at end of window. */
2213 gx = (x / width) * width;
2214 y -= gy;
2215 gy += (y / height) * height;
2217 break;
2219 case ON_LEFT_FRINGE:
2220 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2221 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2222 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2223 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2224 goto row_glyph;
2226 case ON_RIGHT_FRINGE:
2227 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2228 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2229 : window_box_right_offset (w, TEXT_AREA));
2230 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2231 goto row_glyph;
2233 case ON_SCROLL_BAR:
2234 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2236 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2237 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2238 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2239 : 0)));
2240 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2242 row_glyph:
2243 gr = 0, gy = 0;
2244 for (; r <= end_row && r->enabled_p; ++r)
2245 if (r->y + r->height > y)
2247 gr = r; gy = r->y;
2248 break;
2251 if (gr && gy <= y)
2252 height = gr->height;
2253 else
2255 /* Use nominal line height at end of window. */
2256 y -= gy;
2257 gy += (y / height) * height;
2259 break;
2261 default:
2263 virtual_glyph:
2264 /* If there is no glyph under the mouse, then we divide the screen
2265 into a grid of the smallest glyph in the frame, and use that
2266 as our "glyph". */
2268 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2269 round down even for negative values. */
2270 if (gx < 0)
2271 gx -= width - 1;
2272 if (gy < 0)
2273 gy -= height - 1;
2275 gx = (gx / width) * width;
2276 gy = (gy / height) * height;
2278 goto store_rect;
2281 gx += WINDOW_LEFT_EDGE_X (w);
2282 gy += WINDOW_TOP_EDGE_Y (w);
2284 store_rect:
2285 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2287 /* Visible feedback for debugging. */
2288 #if 0
2289 #if HAVE_X_WINDOWS
2290 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2291 f->output_data.x->normal_gc,
2292 gx, gy, width, height);
2293 #endif
2294 #endif
2298 #endif /* HAVE_WINDOW_SYSTEM */
2301 /***********************************************************************
2302 Lisp form evaluation
2303 ***********************************************************************/
2305 /* Error handler for safe_eval and safe_call. */
2307 static Lisp_Object
2308 safe_eval_handler (arg)
2309 Lisp_Object arg;
2311 add_to_log ("Error during redisplay: %s", arg, Qnil);
2312 return Qnil;
2316 /* Evaluate SEXPR and return the result, or nil if something went
2317 wrong. Prevent redisplay during the evaluation. */
2319 Lisp_Object
2320 safe_eval (sexpr)
2321 Lisp_Object sexpr;
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 (sexpr);
2333 specbind (Qinhibit_redisplay, Qt);
2334 /* Use Qt to ensure debugger does not run,
2335 so there is no possibility of wanting to redisplay. */
2336 val = internal_condition_case_1 (Feval, sexpr, Qt,
2337 safe_eval_handler);
2338 UNGCPRO;
2339 val = unbind_to (count, val);
2342 return val;
2346 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2347 Return the result, or nil if something went wrong. Prevent
2348 redisplay during the evaluation. */
2350 Lisp_Object
2351 safe_call (nargs, args)
2352 int nargs;
2353 Lisp_Object *args;
2355 Lisp_Object val;
2357 if (inhibit_eval_during_redisplay)
2358 val = Qnil;
2359 else
2361 int count = SPECPDL_INDEX ();
2362 struct gcpro gcpro1;
2364 GCPRO1 (args[0]);
2365 gcpro1.nvars = nargs;
2366 specbind (Qinhibit_redisplay, Qt);
2367 /* Use Qt to ensure debugger does not run,
2368 so there is no possibility of wanting to redisplay. */
2369 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2370 safe_eval_handler);
2371 UNGCPRO;
2372 val = unbind_to (count, val);
2375 return val;
2379 /* Call function FN with one argument ARG.
2380 Return the result, or nil if something went wrong. */
2382 Lisp_Object
2383 safe_call1 (fn, arg)
2384 Lisp_Object fn, arg;
2386 Lisp_Object args[2];
2387 args[0] = fn;
2388 args[1] = arg;
2389 return safe_call (2, args);
2394 /***********************************************************************
2395 Debugging
2396 ***********************************************************************/
2398 #if 0
2400 /* Define CHECK_IT to perform sanity checks on iterators.
2401 This is for debugging. It is too slow to do unconditionally. */
2403 static void
2404 check_it (it)
2405 struct it *it;
2407 if (it->method == GET_FROM_STRING)
2409 xassert (STRINGP (it->string));
2410 xassert (IT_STRING_CHARPOS (*it) >= 0);
2412 else
2414 xassert (IT_STRING_CHARPOS (*it) < 0);
2415 if (it->method == GET_FROM_BUFFER)
2417 /* Check that character and byte positions agree. */
2418 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2422 if (it->dpvec)
2423 xassert (it->current.dpvec_index >= 0);
2424 else
2425 xassert (it->current.dpvec_index < 0);
2428 #define CHECK_IT(IT) check_it ((IT))
2430 #else /* not 0 */
2432 #define CHECK_IT(IT) (void) 0
2434 #endif /* not 0 */
2437 #if GLYPH_DEBUG
2439 /* Check that the window end of window W is what we expect it
2440 to be---the last row in the current matrix displaying text. */
2442 static void
2443 check_window_end (w)
2444 struct window *w;
2446 if (!MINI_WINDOW_P (w)
2447 && !NILP (w->window_end_valid))
2449 struct glyph_row *row;
2450 xassert ((row = MATRIX_ROW (w->current_matrix,
2451 XFASTINT (w->window_end_vpos)),
2452 !row->enabled_p
2453 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2454 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2458 #define CHECK_WINDOW_END(W) check_window_end ((W))
2460 #else /* not GLYPH_DEBUG */
2462 #define CHECK_WINDOW_END(W) (void) 0
2464 #endif /* not GLYPH_DEBUG */
2468 /***********************************************************************
2469 Iterator initialization
2470 ***********************************************************************/
2472 /* Initialize IT for displaying current_buffer in window W, starting
2473 at character position CHARPOS. CHARPOS < 0 means that no buffer
2474 position is specified which is useful when the iterator is assigned
2475 a position later. BYTEPOS is the byte position corresponding to
2476 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2478 If ROW is not null, calls to produce_glyphs with IT as parameter
2479 will produce glyphs in that row.
2481 BASE_FACE_ID is the id of a base face to use. It must be one of
2482 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2483 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2484 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2486 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2487 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2488 will be initialized to use the corresponding mode line glyph row of
2489 the desired matrix of W. */
2491 void
2492 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2493 struct it *it;
2494 struct window *w;
2495 int charpos, bytepos;
2496 struct glyph_row *row;
2497 enum face_id base_face_id;
2499 int highlight_region_p;
2501 /* Some precondition checks. */
2502 xassert (w != NULL && it != NULL);
2503 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2504 && charpos <= ZV));
2506 /* If face attributes have been changed since the last redisplay,
2507 free realized faces now because they depend on face definitions
2508 that might have changed. Don't free faces while there might be
2509 desired matrices pending which reference these faces. */
2510 if (face_change_count && !inhibit_free_realized_faces)
2512 face_change_count = 0;
2513 free_all_realized_faces (Qnil);
2516 /* Use one of the mode line rows of W's desired matrix if
2517 appropriate. */
2518 if (row == NULL)
2520 if (base_face_id == MODE_LINE_FACE_ID
2521 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2522 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2523 else if (base_face_id == HEADER_LINE_FACE_ID)
2524 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2527 /* Clear IT. */
2528 bzero (it, sizeof *it);
2529 it->current.overlay_string_index = -1;
2530 it->current.dpvec_index = -1;
2531 it->base_face_id = base_face_id;
2532 it->string = Qnil;
2533 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2535 /* The window in which we iterate over current_buffer: */
2536 XSETWINDOW (it->window, w);
2537 it->w = w;
2538 it->f = XFRAME (w->frame);
2540 /* Extra space between lines (on window systems only). */
2541 if (base_face_id == DEFAULT_FACE_ID
2542 && FRAME_WINDOW_P (it->f))
2544 if (NATNUMP (current_buffer->extra_line_spacing))
2545 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2546 else if (FLOATP (current_buffer->extra_line_spacing))
2547 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2548 * FRAME_LINE_HEIGHT (it->f));
2549 else if (it->f->extra_line_spacing > 0)
2550 it->extra_line_spacing = it->f->extra_line_spacing;
2551 it->max_extra_line_spacing = 0;
2554 /* If realized faces have been removed, e.g. because of face
2555 attribute changes of named faces, recompute them. When running
2556 in batch mode, the face cache of the initial frame is null. If
2557 we happen to get called, make a dummy face cache. */
2558 if (FRAME_FACE_CACHE (it->f) == NULL)
2559 init_frame_faces (it->f);
2560 if (FRAME_FACE_CACHE (it->f)->used == 0)
2561 recompute_basic_faces (it->f);
2563 /* Current value of the `slice', `space-width', and 'height' properties. */
2564 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2565 it->space_width = Qnil;
2566 it->font_height = Qnil;
2567 it->override_ascent = -1;
2569 /* Are control characters displayed as `^C'? */
2570 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2572 /* -1 means everything between a CR and the following line end
2573 is invisible. >0 means lines indented more than this value are
2574 invisible. */
2575 it->selective = (INTEGERP (current_buffer->selective_display)
2576 ? XFASTINT (current_buffer->selective_display)
2577 : (!NILP (current_buffer->selective_display)
2578 ? -1 : 0));
2579 it->selective_display_ellipsis_p
2580 = !NILP (current_buffer->selective_display_ellipses);
2582 /* Display table to use. */
2583 it->dp = window_display_table (w);
2585 /* Are multibyte characters enabled in current_buffer? */
2586 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2588 /* Non-zero if we should highlight the region. */
2589 highlight_region_p
2590 = (!NILP (Vtransient_mark_mode)
2591 && !NILP (current_buffer->mark_active)
2592 && XMARKER (current_buffer->mark)->buffer != 0);
2594 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2595 start and end of a visible region in window IT->w. Set both to
2596 -1 to indicate no region. */
2597 if (highlight_region_p
2598 /* Maybe highlight only in selected window. */
2599 && (/* Either show region everywhere. */
2600 highlight_nonselected_windows
2601 /* Or show region in the selected window. */
2602 || w == XWINDOW (selected_window)
2603 /* Or show the region if we are in the mini-buffer and W is
2604 the window the mini-buffer refers to. */
2605 || (MINI_WINDOW_P (XWINDOW (selected_window))
2606 && WINDOWP (minibuf_selected_window)
2607 && w == XWINDOW (minibuf_selected_window))))
2609 int charpos = marker_position (current_buffer->mark);
2610 it->region_beg_charpos = min (PT, charpos);
2611 it->region_end_charpos = max (PT, charpos);
2613 else
2614 it->region_beg_charpos = it->region_end_charpos = -1;
2616 /* Get the position at which the redisplay_end_trigger hook should
2617 be run, if it is to be run at all. */
2618 if (MARKERP (w->redisplay_end_trigger)
2619 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2620 it->redisplay_end_trigger_charpos
2621 = marker_position (w->redisplay_end_trigger);
2622 else if (INTEGERP (w->redisplay_end_trigger))
2623 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2625 /* Correct bogus values of tab_width. */
2626 it->tab_width = XINT (current_buffer->tab_width);
2627 if (it->tab_width <= 0 || it->tab_width > 1000)
2628 it->tab_width = 8;
2630 /* Are lines in the display truncated? */
2631 it->truncate_lines_p
2632 = (base_face_id != DEFAULT_FACE_ID
2633 || XINT (it->w->hscroll)
2634 || (truncate_partial_width_windows
2635 && !WINDOW_FULL_WIDTH_P (it->w))
2636 || !NILP (current_buffer->truncate_lines));
2638 /* Get dimensions of truncation and continuation glyphs. These are
2639 displayed as fringe bitmaps under X, so we don't need them for such
2640 frames. */
2641 if (!FRAME_WINDOW_P (it->f))
2643 if (it->truncate_lines_p)
2645 /* We will need the truncation glyph. */
2646 xassert (it->glyph_row == NULL);
2647 produce_special_glyphs (it, IT_TRUNCATION);
2648 it->truncation_pixel_width = it->pixel_width;
2650 else
2652 /* We will need the continuation glyph. */
2653 xassert (it->glyph_row == NULL);
2654 produce_special_glyphs (it, IT_CONTINUATION);
2655 it->continuation_pixel_width = it->pixel_width;
2658 /* Reset these values to zero because the produce_special_glyphs
2659 above has changed them. */
2660 it->pixel_width = it->ascent = it->descent = 0;
2661 it->phys_ascent = it->phys_descent = 0;
2664 /* Set this after getting the dimensions of truncation and
2665 continuation glyphs, so that we don't produce glyphs when calling
2666 produce_special_glyphs, above. */
2667 it->glyph_row = row;
2668 it->area = TEXT_AREA;
2670 /* Get the dimensions of the display area. The display area
2671 consists of the visible window area plus a horizontally scrolled
2672 part to the left of the window. All x-values are relative to the
2673 start of this total display area. */
2674 if (base_face_id != DEFAULT_FACE_ID)
2676 /* Mode lines, menu bar in terminal frames. */
2677 it->first_visible_x = 0;
2678 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2680 else
2682 it->first_visible_x
2683 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2684 it->last_visible_x = (it->first_visible_x
2685 + window_box_width (w, TEXT_AREA));
2687 /* If we truncate lines, leave room for the truncator glyph(s) at
2688 the right margin. Otherwise, leave room for the continuation
2689 glyph(s). Truncation and continuation glyphs are not inserted
2690 for window-based redisplay. */
2691 if (!FRAME_WINDOW_P (it->f))
2693 if (it->truncate_lines_p)
2694 it->last_visible_x -= it->truncation_pixel_width;
2695 else
2696 it->last_visible_x -= it->continuation_pixel_width;
2699 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2700 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2703 /* Leave room for a border glyph. */
2704 if (!FRAME_WINDOW_P (it->f)
2705 && !WINDOW_RIGHTMOST_P (it->w))
2706 it->last_visible_x -= 1;
2708 it->last_visible_y = window_text_bottom_y (w);
2710 /* For mode lines and alike, arrange for the first glyph having a
2711 left box line if the face specifies a box. */
2712 if (base_face_id != DEFAULT_FACE_ID)
2714 struct face *face;
2716 it->face_id = base_face_id;
2718 /* If we have a boxed mode line, make the first character appear
2719 with a left box line. */
2720 face = FACE_FROM_ID (it->f, base_face_id);
2721 if (face->box != FACE_NO_BOX)
2722 it->start_of_box_run_p = 1;
2725 /* If a buffer position was specified, set the iterator there,
2726 getting overlays and face properties from that position. */
2727 if (charpos >= BUF_BEG (current_buffer))
2729 it->end_charpos = ZV;
2730 it->face_id = -1;
2731 IT_CHARPOS (*it) = charpos;
2733 /* Compute byte position if not specified. */
2734 if (bytepos < charpos)
2735 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2736 else
2737 IT_BYTEPOS (*it) = bytepos;
2739 it->start = it->current;
2741 /* Compute faces etc. */
2742 reseat (it, it->current.pos, 1);
2745 CHECK_IT (it);
2749 /* Initialize IT for the display of window W with window start POS. */
2751 void
2752 start_display (it, w, pos)
2753 struct it *it;
2754 struct window *w;
2755 struct text_pos pos;
2757 struct glyph_row *row;
2758 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2760 row = w->desired_matrix->rows + first_vpos;
2761 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2762 it->first_vpos = first_vpos;
2764 /* Don't reseat to previous visible line start if current start
2765 position is in a string or image. */
2766 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2768 int start_at_line_beg_p;
2769 int first_y = it->current_y;
2771 /* If window start is not at a line start, skip forward to POS to
2772 get the correct continuation lines width. */
2773 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2774 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2775 if (!start_at_line_beg_p)
2777 int new_x;
2779 reseat_at_previous_visible_line_start (it);
2780 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2782 new_x = it->current_x + it->pixel_width;
2784 /* If lines are continued, this line may end in the middle
2785 of a multi-glyph character (e.g. a control character
2786 displayed as \003, or in the middle of an overlay
2787 string). In this case move_it_to above will not have
2788 taken us to the start of the continuation line but to the
2789 end of the continued line. */
2790 if (it->current_x > 0
2791 && !it->truncate_lines_p /* Lines are continued. */
2792 && (/* And glyph doesn't fit on the line. */
2793 new_x > it->last_visible_x
2794 /* Or it fits exactly and we're on a window
2795 system frame. */
2796 || (new_x == it->last_visible_x
2797 && FRAME_WINDOW_P (it->f))))
2799 if (it->current.dpvec_index >= 0
2800 || it->current.overlay_string_index >= 0)
2802 set_iterator_to_next (it, 1);
2803 move_it_in_display_line_to (it, -1, -1, 0);
2806 it->continuation_lines_width += it->current_x;
2809 /* We're starting a new display line, not affected by the
2810 height of the continued line, so clear the appropriate
2811 fields in the iterator structure. */
2812 it->max_ascent = it->max_descent = 0;
2813 it->max_phys_ascent = it->max_phys_descent = 0;
2815 it->current_y = first_y;
2816 it->vpos = 0;
2817 it->current_x = it->hpos = 0;
2821 #if 0 /* Don't assert the following because start_display is sometimes
2822 called intentionally with a window start that is not at a
2823 line start. Please leave this code in as a comment. */
2825 /* Window start should be on a line start, now. */
2826 xassert (it->continuation_lines_width
2827 || IT_CHARPOS (it) == BEGV
2828 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2829 #endif /* 0 */
2833 /* Return 1 if POS is a position in ellipses displayed for invisible
2834 text. W is the window we display, for text property lookup. */
2836 static int
2837 in_ellipses_for_invisible_text_p (pos, w)
2838 struct display_pos *pos;
2839 struct window *w;
2841 Lisp_Object prop, window;
2842 int ellipses_p = 0;
2843 int charpos = CHARPOS (pos->pos);
2845 /* If POS specifies a position in a display vector, this might
2846 be for an ellipsis displayed for invisible text. We won't
2847 get the iterator set up for delivering that ellipsis unless
2848 we make sure that it gets aware of the invisible text. */
2849 if (pos->dpvec_index >= 0
2850 && pos->overlay_string_index < 0
2851 && CHARPOS (pos->string_pos) < 0
2852 && charpos > BEGV
2853 && (XSETWINDOW (window, w),
2854 prop = Fget_char_property (make_number (charpos),
2855 Qinvisible, window),
2856 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2858 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2859 window);
2860 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2863 return ellipses_p;
2867 /* Initialize IT for stepping through current_buffer in window W,
2868 starting at position POS that includes overlay string and display
2869 vector/ control character translation position information. Value
2870 is zero if there are overlay strings with newlines at POS. */
2872 static int
2873 init_from_display_pos (it, w, pos)
2874 struct it *it;
2875 struct window *w;
2876 struct display_pos *pos;
2878 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2879 int i, overlay_strings_with_newlines = 0;
2881 /* If POS specifies a position in a display vector, this might
2882 be for an ellipsis displayed for invisible text. We won't
2883 get the iterator set up for delivering that ellipsis unless
2884 we make sure that it gets aware of the invisible text. */
2885 if (in_ellipses_for_invisible_text_p (pos, w))
2887 --charpos;
2888 bytepos = 0;
2891 /* Keep in mind: the call to reseat in init_iterator skips invisible
2892 text, so we might end up at a position different from POS. This
2893 is only a problem when POS is a row start after a newline and an
2894 overlay starts there with an after-string, and the overlay has an
2895 invisible property. Since we don't skip invisible text in
2896 display_line and elsewhere immediately after consuming the
2897 newline before the row start, such a POS will not be in a string,
2898 but the call to init_iterator below will move us to the
2899 after-string. */
2900 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2902 /* This only scans the current chunk -- it should scan all chunks.
2903 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2904 to 16 in 22.1 to make this a lesser problem. */
2905 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2907 const char *s = SDATA (it->overlay_strings[i]);
2908 const char *e = s + SBYTES (it->overlay_strings[i]);
2910 while (s < e && *s != '\n')
2911 ++s;
2913 if (s < e)
2915 overlay_strings_with_newlines = 1;
2916 break;
2920 /* If position is within an overlay string, set up IT to the right
2921 overlay string. */
2922 if (pos->overlay_string_index >= 0)
2924 int relative_index;
2926 /* If the first overlay string happens to have a `display'
2927 property for an image, the iterator will be set up for that
2928 image, and we have to undo that setup first before we can
2929 correct the overlay string index. */
2930 if (it->method == GET_FROM_IMAGE)
2931 pop_it (it);
2933 /* We already have the first chunk of overlay strings in
2934 IT->overlay_strings. Load more until the one for
2935 pos->overlay_string_index is in IT->overlay_strings. */
2936 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2938 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2939 it->current.overlay_string_index = 0;
2940 while (n--)
2942 load_overlay_strings (it, 0);
2943 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2947 it->current.overlay_string_index = pos->overlay_string_index;
2948 relative_index = (it->current.overlay_string_index
2949 % OVERLAY_STRING_CHUNK_SIZE);
2950 it->string = it->overlay_strings[relative_index];
2951 xassert (STRINGP (it->string));
2952 it->current.string_pos = pos->string_pos;
2953 it->method = GET_FROM_STRING;
2956 #if 0 /* This is bogus because POS not having an overlay string
2957 position does not mean it's after the string. Example: A
2958 line starting with a before-string and initialization of IT
2959 to the previous row's end position. */
2960 else if (it->current.overlay_string_index >= 0)
2962 /* If POS says we're already after an overlay string ending at
2963 POS, make sure to pop the iterator because it will be in
2964 front of that overlay string. When POS is ZV, we've thereby
2965 also ``processed'' overlay strings at ZV. */
2966 while (it->sp)
2967 pop_it (it);
2968 xassert (it->current.overlay_string_index == -1);
2969 xassert (it->method == GET_FROM_BUFFER);
2970 if (CHARPOS (pos->pos) == ZV)
2971 it->overlay_strings_at_end_processed_p = 1;
2973 #endif /* 0 */
2975 if (CHARPOS (pos->string_pos) >= 0)
2977 /* Recorded position is not in an overlay string, but in another
2978 string. This can only be a string from a `display' property.
2979 IT should already be filled with that string. */
2980 it->current.string_pos = pos->string_pos;
2981 xassert (STRINGP (it->string));
2984 /* Restore position in display vector translations, control
2985 character translations or ellipses. */
2986 if (pos->dpvec_index >= 0)
2988 if (it->dpvec == NULL)
2989 get_next_display_element (it);
2990 xassert (it->dpvec && it->current.dpvec_index == 0);
2991 it->current.dpvec_index = pos->dpvec_index;
2994 CHECK_IT (it);
2995 return !overlay_strings_with_newlines;
2999 /* Initialize IT for stepping through current_buffer in window W
3000 starting at ROW->start. */
3002 static void
3003 init_to_row_start (it, w, row)
3004 struct it *it;
3005 struct window *w;
3006 struct glyph_row *row;
3008 init_from_display_pos (it, w, &row->start);
3009 it->start = row->start;
3010 it->continuation_lines_width = row->continuation_lines_width;
3011 CHECK_IT (it);
3015 /* Initialize IT for stepping through current_buffer in window W
3016 starting in the line following ROW, i.e. starting at ROW->end.
3017 Value is zero if there are overlay strings with newlines at ROW's
3018 end position. */
3020 static int
3021 init_to_row_end (it, w, row)
3022 struct it *it;
3023 struct window *w;
3024 struct glyph_row *row;
3026 int success = 0;
3028 if (init_from_display_pos (it, w, &row->end))
3030 if (row->continued_p)
3031 it->continuation_lines_width
3032 = row->continuation_lines_width + row->pixel_width;
3033 CHECK_IT (it);
3034 success = 1;
3037 return success;
3043 /***********************************************************************
3044 Text properties
3045 ***********************************************************************/
3047 /* Called when IT reaches IT->stop_charpos. Handle text property and
3048 overlay changes. Set IT->stop_charpos to the next position where
3049 to stop. */
3051 static void
3052 handle_stop (it)
3053 struct it *it;
3055 enum prop_handled handled;
3056 int handle_overlay_change_p;
3057 struct props *p;
3059 it->dpvec = NULL;
3060 it->current.dpvec_index = -1;
3061 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3062 it->ignore_overlay_strings_at_pos_p = 0;
3064 /* Use face of preceding text for ellipsis (if invisible) */
3065 if (it->selective_display_ellipsis_p)
3066 it->saved_face_id = it->face_id;
3070 handled = HANDLED_NORMALLY;
3072 /* Call text property handlers. */
3073 for (p = it_props; p->handler; ++p)
3075 handled = p->handler (it);
3077 if (handled == HANDLED_RECOMPUTE_PROPS)
3078 break;
3079 else if (handled == HANDLED_RETURN)
3081 /* We still want to show before and after strings from
3082 overlays even if the actual buffer text is replaced. */
3083 if (!handle_overlay_change_p || it->sp > 1)
3084 return;
3085 if (!get_overlay_strings_1 (it, 0, 0))
3086 return;
3087 it->ignore_overlay_strings_at_pos_p = 1;
3088 it->string_from_display_prop_p = 0;
3089 handle_overlay_change_p = 0;
3090 handled = HANDLED_RECOMPUTE_PROPS;
3091 break;
3093 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3094 handle_overlay_change_p = 0;
3097 if (handled != HANDLED_RECOMPUTE_PROPS)
3099 /* Don't check for overlay strings below when set to deliver
3100 characters from a display vector. */
3101 if (it->method == GET_FROM_DISPLAY_VECTOR)
3102 handle_overlay_change_p = 0;
3104 /* Handle overlay changes.
3105 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3106 if it finds overlays. */
3107 if (handle_overlay_change_p)
3108 handled = handle_overlay_change (it);
3111 while (handled == HANDLED_RECOMPUTE_PROPS);
3113 /* Determine where to stop next. */
3114 if (handled == HANDLED_NORMALLY)
3115 compute_stop_pos (it);
3119 /* Compute IT->stop_charpos from text property and overlay change
3120 information for IT's current position. */
3122 static void
3123 compute_stop_pos (it)
3124 struct it *it;
3126 register INTERVAL iv, next_iv;
3127 Lisp_Object object, limit, position;
3129 /* If nowhere else, stop at the end. */
3130 it->stop_charpos = it->end_charpos;
3132 if (STRINGP (it->string))
3134 /* Strings are usually short, so don't limit the search for
3135 properties. */
3136 object = it->string;
3137 limit = Qnil;
3138 position = make_number (IT_STRING_CHARPOS (*it));
3140 else
3142 int charpos;
3144 /* If next overlay change is in front of the current stop pos
3145 (which is IT->end_charpos), stop there. Note: value of
3146 next_overlay_change is point-max if no overlay change
3147 follows. */
3148 charpos = next_overlay_change (IT_CHARPOS (*it));
3149 if (charpos < it->stop_charpos)
3150 it->stop_charpos = charpos;
3152 /* If showing the region, we have to stop at the region
3153 start or end because the face might change there. */
3154 if (it->region_beg_charpos > 0)
3156 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3157 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3158 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3159 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3162 /* Set up variables for computing the stop position from text
3163 property changes. */
3164 XSETBUFFER (object, current_buffer);
3165 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3166 position = make_number (IT_CHARPOS (*it));
3170 /* Get the interval containing IT's position. Value is a null
3171 interval if there isn't such an interval. */
3172 iv = validate_interval_range (object, &position, &position, 0);
3173 if (!NULL_INTERVAL_P (iv))
3175 Lisp_Object values_here[LAST_PROP_IDX];
3176 struct props *p;
3178 /* Get properties here. */
3179 for (p = it_props; p->handler; ++p)
3180 values_here[p->idx] = textget (iv->plist, *p->name);
3182 /* Look for an interval following iv that has different
3183 properties. */
3184 for (next_iv = next_interval (iv);
3185 (!NULL_INTERVAL_P (next_iv)
3186 && (NILP (limit)
3187 || XFASTINT (limit) > next_iv->position));
3188 next_iv = next_interval (next_iv))
3190 for (p = it_props; p->handler; ++p)
3192 Lisp_Object new_value;
3194 new_value = textget (next_iv->plist, *p->name);
3195 if (!EQ (values_here[p->idx], new_value))
3196 break;
3199 if (p->handler)
3200 break;
3203 if (!NULL_INTERVAL_P (next_iv))
3205 if (INTEGERP (limit)
3206 && next_iv->position >= XFASTINT (limit))
3207 /* No text property change up to limit. */
3208 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3209 else
3210 /* Text properties change in next_iv. */
3211 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3215 xassert (STRINGP (it->string)
3216 || (it->stop_charpos >= BEGV
3217 && it->stop_charpos >= IT_CHARPOS (*it)));
3221 /* Return the position of the next overlay change after POS in
3222 current_buffer. Value is point-max if no overlay change
3223 follows. This is like `next-overlay-change' but doesn't use
3224 xmalloc. */
3226 static int
3227 next_overlay_change (pos)
3228 int pos;
3230 int noverlays;
3231 int endpos;
3232 Lisp_Object *overlays;
3233 int i;
3235 /* Get all overlays at the given position. */
3236 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3238 /* If any of these overlays ends before endpos,
3239 use its ending point instead. */
3240 for (i = 0; i < noverlays; ++i)
3242 Lisp_Object oend;
3243 int oendpos;
3245 oend = OVERLAY_END (overlays[i]);
3246 oendpos = OVERLAY_POSITION (oend);
3247 endpos = min (endpos, oendpos);
3250 return endpos;
3255 /***********************************************************************
3256 Fontification
3257 ***********************************************************************/
3259 /* Handle changes in the `fontified' property of the current buffer by
3260 calling hook functions from Qfontification_functions to fontify
3261 regions of text. */
3263 static enum prop_handled
3264 handle_fontified_prop (it)
3265 struct it *it;
3267 Lisp_Object prop, pos;
3268 enum prop_handled handled = HANDLED_NORMALLY;
3270 if (!NILP (Vmemory_full))
3271 return handled;
3273 /* Get the value of the `fontified' property at IT's current buffer
3274 position. (The `fontified' property doesn't have a special
3275 meaning in strings.) If the value is nil, call functions from
3276 Qfontification_functions. */
3277 if (!STRINGP (it->string)
3278 && it->s == NULL
3279 && !NILP (Vfontification_functions)
3280 && !NILP (Vrun_hooks)
3281 && (pos = make_number (IT_CHARPOS (*it)),
3282 prop = Fget_char_property (pos, Qfontified, Qnil),
3283 /* Ignore the special cased nil value always present at EOB since
3284 no amount of fontifying will be able to change it. */
3285 NILP (prop) && IT_CHARPOS (*it) < Z))
3287 int count = SPECPDL_INDEX ();
3288 Lisp_Object val;
3290 val = Vfontification_functions;
3291 specbind (Qfontification_functions, Qnil);
3293 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3294 safe_call1 (val, pos);
3295 else
3297 Lisp_Object globals, fn;
3298 struct gcpro gcpro1, gcpro2;
3300 globals = Qnil;
3301 GCPRO2 (val, globals);
3303 for (; CONSP (val); val = XCDR (val))
3305 fn = XCAR (val);
3307 if (EQ (fn, Qt))
3309 /* A value of t indicates this hook has a local
3310 binding; it means to run the global binding too.
3311 In a global value, t should not occur. If it
3312 does, we must ignore it to avoid an endless
3313 loop. */
3314 for (globals = Fdefault_value (Qfontification_functions);
3315 CONSP (globals);
3316 globals = XCDR (globals))
3318 fn = XCAR (globals);
3319 if (!EQ (fn, Qt))
3320 safe_call1 (fn, pos);
3323 else
3324 safe_call1 (fn, pos);
3327 UNGCPRO;
3330 unbind_to (count, Qnil);
3332 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3333 something. This avoids an endless loop if they failed to
3334 fontify the text for which reason ever. */
3335 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3336 handled = HANDLED_RECOMPUTE_PROPS;
3339 return handled;
3344 /***********************************************************************
3345 Faces
3346 ***********************************************************************/
3348 /* Set up iterator IT from face properties at its current position.
3349 Called from handle_stop. */
3351 static enum prop_handled
3352 handle_face_prop (it)
3353 struct it *it;
3355 int new_face_id, next_stop;
3357 if (!STRINGP (it->string))
3359 new_face_id
3360 = face_at_buffer_position (it->w,
3361 IT_CHARPOS (*it),
3362 it->region_beg_charpos,
3363 it->region_end_charpos,
3364 &next_stop,
3365 (IT_CHARPOS (*it)
3366 + TEXT_PROP_DISTANCE_LIMIT),
3369 /* Is this a start of a run of characters with box face?
3370 Caveat: this can be called for a freshly initialized
3371 iterator; face_id is -1 in this case. We know that the new
3372 face will not change until limit, i.e. if the new face has a
3373 box, all characters up to limit will have one. But, as
3374 usual, we don't know whether limit is really the end. */
3375 if (new_face_id != it->face_id)
3377 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3379 /* If new face has a box but old face has not, this is
3380 the start of a run of characters with box, i.e. it has
3381 a shadow on the left side. The value of face_id of the
3382 iterator will be -1 if this is the initial call that gets
3383 the face. In this case, we have to look in front of IT's
3384 position and see whether there is a face != new_face_id. */
3385 it->start_of_box_run_p
3386 = (new_face->box != FACE_NO_BOX
3387 && (it->face_id >= 0
3388 || IT_CHARPOS (*it) == BEG
3389 || new_face_id != face_before_it_pos (it)));
3390 it->face_box_p = new_face->box != FACE_NO_BOX;
3393 else
3395 int base_face_id, bufpos;
3396 int i;
3397 Lisp_Object from_overlay
3398 = (it->current.overlay_string_index >= 0
3399 ? it->string_overlays[it->current.overlay_string_index]
3400 : Qnil);
3402 /* See if we got to this string directly or indirectly from
3403 an overlay property. That includes the before-string or
3404 after-string of an overlay, strings in display properties
3405 provided by an overlay, their text properties, etc.
3407 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3408 if (! NILP (from_overlay))
3409 for (i = it->sp - 1; i >= 0; i--)
3411 if (it->stack[i].current.overlay_string_index >= 0)
3412 from_overlay
3413 = it->string_overlays[it->stack[i].current.overlay_string_index];
3414 else if (! NILP (it->stack[i].from_overlay))
3415 from_overlay = it->stack[i].from_overlay;
3417 if (!NILP (from_overlay))
3418 break;
3421 if (! NILP (from_overlay))
3423 bufpos = IT_CHARPOS (*it);
3424 /* For a string from an overlay, the base face depends
3425 only on text properties and ignores overlays. */
3426 base_face_id
3427 = face_for_overlay_string (it->w,
3428 IT_CHARPOS (*it),
3429 it->region_beg_charpos,
3430 it->region_end_charpos,
3431 &next_stop,
3432 (IT_CHARPOS (*it)
3433 + TEXT_PROP_DISTANCE_LIMIT),
3435 from_overlay);
3437 else
3439 bufpos = 0;
3441 /* For strings from a `display' property, use the face at
3442 IT's current buffer position as the base face to merge
3443 with, so that overlay strings appear in the same face as
3444 surrounding text, unless they specify their own
3445 faces. */
3446 base_face_id = underlying_face_id (it);
3449 new_face_id = face_at_string_position (it->w,
3450 it->string,
3451 IT_STRING_CHARPOS (*it),
3452 bufpos,
3453 it->region_beg_charpos,
3454 it->region_end_charpos,
3455 &next_stop,
3456 base_face_id, 0);
3458 #if 0 /* This shouldn't be neccessary. Let's check it. */
3459 /* If IT is used to display a mode line we would really like to
3460 use the mode line face instead of the frame's default face. */
3461 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3462 && new_face_id == DEFAULT_FACE_ID)
3463 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3464 #endif
3466 /* Is this a start of a run of characters with box? Caveat:
3467 this can be called for a freshly allocated iterator; face_id
3468 is -1 is this case. We know that the new face will not
3469 change until the next check pos, i.e. if the new face has a
3470 box, all characters up to that position will have a
3471 box. But, as usual, we don't know whether that position
3472 is really the end. */
3473 if (new_face_id != it->face_id)
3475 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3476 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3478 /* If new face has a box but old face hasn't, this is the
3479 start of a run of characters with box, i.e. it has a
3480 shadow on the left side. */
3481 it->start_of_box_run_p
3482 = new_face->box && (old_face == NULL || !old_face->box);
3483 it->face_box_p = new_face->box != FACE_NO_BOX;
3487 it->face_id = new_face_id;
3488 return HANDLED_NORMALLY;
3492 /* Return the ID of the face ``underlying'' IT's current position,
3493 which is in a string. If the iterator is associated with a
3494 buffer, return the face at IT's current buffer position.
3495 Otherwise, use the iterator's base_face_id. */
3497 static int
3498 underlying_face_id (it)
3499 struct it *it;
3501 int face_id = it->base_face_id, i;
3503 xassert (STRINGP (it->string));
3505 for (i = it->sp - 1; i >= 0; --i)
3506 if (NILP (it->stack[i].string))
3507 face_id = it->stack[i].face_id;
3509 return face_id;
3513 /* Compute the face one character before or after the current position
3514 of IT. BEFORE_P non-zero means get the face in front of IT's
3515 position. Value is the id of the face. */
3517 static int
3518 face_before_or_after_it_pos (it, before_p)
3519 struct it *it;
3520 int before_p;
3522 int face_id, limit;
3523 int next_check_charpos;
3524 struct text_pos pos;
3526 xassert (it->s == NULL);
3528 if (STRINGP (it->string))
3530 int bufpos, base_face_id;
3532 /* No face change past the end of the string (for the case
3533 we are padding with spaces). No face change before the
3534 string start. */
3535 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3536 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3537 return it->face_id;
3539 /* Set pos to the position before or after IT's current position. */
3540 if (before_p)
3541 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3542 else
3543 /* For composition, we must check the character after the
3544 composition. */
3545 pos = (it->what == IT_COMPOSITION
3546 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3547 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3549 if (it->current.overlay_string_index >= 0)
3550 bufpos = IT_CHARPOS (*it);
3551 else
3552 bufpos = 0;
3554 base_face_id = underlying_face_id (it);
3556 /* Get the face for ASCII, or unibyte. */
3557 face_id = face_at_string_position (it->w,
3558 it->string,
3559 CHARPOS (pos),
3560 bufpos,
3561 it->region_beg_charpos,
3562 it->region_end_charpos,
3563 &next_check_charpos,
3564 base_face_id, 0);
3566 /* Correct the face for charsets different from ASCII. Do it
3567 for the multibyte case only. The face returned above is
3568 suitable for unibyte text if IT->string is unibyte. */
3569 if (STRING_MULTIBYTE (it->string))
3571 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3572 int rest = SBYTES (it->string) - BYTEPOS (pos);
3573 int c, len;
3574 struct face *face = FACE_FROM_ID (it->f, face_id);
3576 c = string_char_and_length (p, rest, &len);
3577 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3580 else
3582 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3583 || (IT_CHARPOS (*it) <= BEGV && before_p))
3584 return it->face_id;
3586 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3587 pos = it->current.pos;
3589 if (before_p)
3590 DEC_TEXT_POS (pos, it->multibyte_p);
3591 else
3593 if (it->what == IT_COMPOSITION)
3594 /* For composition, we must check the position after the
3595 composition. */
3596 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3597 else
3598 INC_TEXT_POS (pos, it->multibyte_p);
3601 /* Determine face for CHARSET_ASCII, or unibyte. */
3602 face_id = face_at_buffer_position (it->w,
3603 CHARPOS (pos),
3604 it->region_beg_charpos,
3605 it->region_end_charpos,
3606 &next_check_charpos,
3607 limit, 0);
3609 /* Correct the face for charsets different from ASCII. Do it
3610 for the multibyte case only. The face returned above is
3611 suitable for unibyte text if current_buffer is unibyte. */
3612 if (it->multibyte_p)
3614 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3615 struct face *face = FACE_FROM_ID (it->f, face_id);
3616 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3620 return face_id;
3625 /***********************************************************************
3626 Invisible text
3627 ***********************************************************************/
3629 /* Set up iterator IT from invisible properties at its current
3630 position. Called from handle_stop. */
3632 static enum prop_handled
3633 handle_invisible_prop (it)
3634 struct it *it;
3636 enum prop_handled handled = HANDLED_NORMALLY;
3638 if (STRINGP (it->string))
3640 extern Lisp_Object Qinvisible;
3641 Lisp_Object prop, end_charpos, limit, charpos;
3643 /* Get the value of the invisible text property at the
3644 current position. Value will be nil if there is no such
3645 property. */
3646 charpos = make_number (IT_STRING_CHARPOS (*it));
3647 prop = Fget_text_property (charpos, Qinvisible, it->string);
3649 if (!NILP (prop)
3650 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3652 handled = HANDLED_RECOMPUTE_PROPS;
3654 /* Get the position at which the next change of the
3655 invisible text property can be found in IT->string.
3656 Value will be nil if the property value is the same for
3657 all the rest of IT->string. */
3658 XSETINT (limit, SCHARS (it->string));
3659 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3660 it->string, limit);
3662 /* Text at current position is invisible. The next
3663 change in the property is at position end_charpos.
3664 Move IT's current position to that position. */
3665 if (INTEGERP (end_charpos)
3666 && XFASTINT (end_charpos) < XFASTINT (limit))
3668 struct text_pos old;
3669 old = it->current.string_pos;
3670 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3671 compute_string_pos (&it->current.string_pos, old, it->string);
3673 else
3675 /* The rest of the string is invisible. If this is an
3676 overlay string, proceed with the next overlay string
3677 or whatever comes and return a character from there. */
3678 if (it->current.overlay_string_index >= 0)
3680 next_overlay_string (it);
3681 /* Don't check for overlay strings when we just
3682 finished processing them. */
3683 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3685 else
3687 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3688 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3693 else
3695 int invis_p;
3696 EMACS_INT newpos, next_stop, start_charpos;
3697 Lisp_Object pos, prop, overlay;
3699 /* First of all, is there invisible text at this position? */
3700 start_charpos = IT_CHARPOS (*it);
3701 pos = make_number (IT_CHARPOS (*it));
3702 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3703 &overlay);
3704 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3706 /* If we are on invisible text, skip over it. */
3707 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3709 /* Record whether we have to display an ellipsis for the
3710 invisible text. */
3711 int display_ellipsis_p = invis_p == 2;
3713 handled = HANDLED_RECOMPUTE_PROPS;
3715 /* Loop skipping over invisible text. The loop is left at
3716 ZV or with IT on the first char being visible again. */
3719 /* Try to skip some invisible text. Return value is the
3720 position reached which can be equal to IT's position
3721 if there is nothing invisible here. This skips both
3722 over invisible text properties and overlays with
3723 invisible property. */
3724 newpos = skip_invisible (IT_CHARPOS (*it),
3725 &next_stop, ZV, it->window);
3727 /* If we skipped nothing at all we weren't at invisible
3728 text in the first place. If everything to the end of
3729 the buffer was skipped, end the loop. */
3730 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3731 invis_p = 0;
3732 else
3734 /* We skipped some characters but not necessarily
3735 all there are. Check if we ended up on visible
3736 text. Fget_char_property returns the property of
3737 the char before the given position, i.e. if we
3738 get invis_p = 0, this means that the char at
3739 newpos is visible. */
3740 pos = make_number (newpos);
3741 prop = Fget_char_property (pos, Qinvisible, it->window);
3742 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3745 /* If we ended up on invisible text, proceed to
3746 skip starting with next_stop. */
3747 if (invis_p)
3748 IT_CHARPOS (*it) = next_stop;
3750 /* If there are adjacent invisible texts, don't lose the
3751 second one's ellipsis. */
3752 if (invis_p == 2)
3753 display_ellipsis_p = 1;
3755 while (invis_p);
3757 /* The position newpos is now either ZV or on visible text. */
3758 IT_CHARPOS (*it) = newpos;
3759 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3761 /* If there are before-strings at the start of invisible
3762 text, and the text is invisible because of a text
3763 property, arrange to show before-strings because 20.x did
3764 it that way. (If the text is invisible because of an
3765 overlay property instead of a text property, this is
3766 already handled in the overlay code.) */
3767 if (NILP (overlay)
3768 && get_overlay_strings (it, start_charpos))
3770 handled = HANDLED_RECOMPUTE_PROPS;
3771 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3773 else if (display_ellipsis_p)
3775 /* Make sure that the glyphs of the ellipsis will get
3776 correct `charpos' values. If we would not update
3777 it->position here, the glyphs would belong to the
3778 last visible character _before_ the invisible
3779 text, which confuses `set_cursor_from_row'.
3781 We use the last invisible position instead of the
3782 first because this way the cursor is always drawn on
3783 the first "." of the ellipsis, whenever PT is inside
3784 the invisible text. Otherwise the cursor would be
3785 placed _after_ the ellipsis when the point is after the
3786 first invisible character. */
3787 if (!STRINGP (it->object))
3789 it->position.charpos = IT_CHARPOS (*it) - 1;
3790 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3792 setup_for_ellipsis (it, 0);
3793 /* Let the ellipsis display before
3794 considering any properties of the following char.
3795 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3796 handled = HANDLED_RETURN;
3801 return handled;
3805 /* Make iterator IT return `...' next.
3806 Replaces LEN characters from buffer. */
3808 static void
3809 setup_for_ellipsis (it, len)
3810 struct it *it;
3811 int len;
3813 /* Use the display table definition for `...'. Invalid glyphs
3814 will be handled by the method returning elements from dpvec. */
3815 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3817 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3818 it->dpvec = v->contents;
3819 it->dpend = v->contents + v->size;
3821 else
3823 /* Default `...'. */
3824 it->dpvec = default_invis_vector;
3825 it->dpend = default_invis_vector + 3;
3828 it->dpvec_char_len = len;
3829 it->current.dpvec_index = 0;
3830 it->dpvec_face_id = -1;
3832 /* Remember the current face id in case glyphs specify faces.
3833 IT's face is restored in set_iterator_to_next.
3834 saved_face_id was set to preceding char's face in handle_stop. */
3835 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3836 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3838 it->method = GET_FROM_DISPLAY_VECTOR;
3839 it->ellipsis_p = 1;
3844 /***********************************************************************
3845 'display' property
3846 ***********************************************************************/
3848 /* Set up iterator IT from `display' property at its current position.
3849 Called from handle_stop.
3850 We return HANDLED_RETURN if some part of the display property
3851 overrides the display of the buffer text itself.
3852 Otherwise we return HANDLED_NORMALLY. */
3854 static enum prop_handled
3855 handle_display_prop (it)
3856 struct it *it;
3858 Lisp_Object prop, object, overlay;
3859 struct text_pos *position;
3860 /* Nonzero if some property replaces the display of the text itself. */
3861 int display_replaced_p = 0;
3863 if (STRINGP (it->string))
3865 object = it->string;
3866 position = &it->current.string_pos;
3868 else
3870 XSETWINDOW (object, it->w);
3871 position = &it->current.pos;
3874 /* Reset those iterator values set from display property values. */
3875 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3876 it->space_width = Qnil;
3877 it->font_height = Qnil;
3878 it->voffset = 0;
3880 /* We don't support recursive `display' properties, i.e. string
3881 values that have a string `display' property, that have a string
3882 `display' property etc. */
3883 if (!it->string_from_display_prop_p)
3884 it->area = TEXT_AREA;
3886 prop = get_char_property_and_overlay (make_number (position->charpos),
3887 Qdisplay, object, &overlay);
3888 if (NILP (prop))
3889 return HANDLED_NORMALLY;
3890 /* Now OVERLAY is the overlay that gave us this property, or nil
3891 if it was a text property. */
3893 if (!STRINGP (it->string))
3894 object = it->w->buffer;
3896 if (CONSP (prop)
3897 /* Simple properties. */
3898 && !EQ (XCAR (prop), Qimage)
3899 && !EQ (XCAR (prop), Qspace)
3900 && !EQ (XCAR (prop), Qwhen)
3901 && !EQ (XCAR (prop), Qslice)
3902 && !EQ (XCAR (prop), Qspace_width)
3903 && !EQ (XCAR (prop), Qheight)
3904 && !EQ (XCAR (prop), Qraise)
3905 /* Marginal area specifications. */
3906 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3907 && !EQ (XCAR (prop), Qleft_fringe)
3908 && !EQ (XCAR (prop), Qright_fringe)
3909 && !NILP (XCAR (prop)))
3911 for (; CONSP (prop); prop = XCDR (prop))
3913 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3914 position, display_replaced_p))
3916 display_replaced_p = 1;
3917 /* If some text in a string is replaced, `position' no
3918 longer points to the position of `object'. */
3919 if (STRINGP (object))
3920 break;
3924 else if (VECTORP (prop))
3926 int i;
3927 for (i = 0; i < ASIZE (prop); ++i)
3928 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3929 position, display_replaced_p))
3931 display_replaced_p = 1;
3932 /* If some text in a string is replaced, `position' no
3933 longer points to the position of `object'. */
3934 if (STRINGP (object))
3935 break;
3938 else
3940 int ret = handle_single_display_spec (it, prop, object, overlay,
3941 position, 0);
3942 if (ret < 0) /* Replaced by "", i.e. nothing. */
3943 return HANDLED_RECOMPUTE_PROPS;
3944 if (ret)
3945 display_replaced_p = 1;
3948 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3952 /* Value is the position of the end of the `display' property starting
3953 at START_POS in OBJECT. */
3955 static struct text_pos
3956 display_prop_end (it, object, start_pos)
3957 struct it *it;
3958 Lisp_Object object;
3959 struct text_pos start_pos;
3961 Lisp_Object end;
3962 struct text_pos end_pos;
3964 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3965 Qdisplay, object, Qnil);
3966 CHARPOS (end_pos) = XFASTINT (end);
3967 if (STRINGP (object))
3968 compute_string_pos (&end_pos, start_pos, it->string);
3969 else
3970 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3972 return end_pos;
3976 /* Set up IT from a single `display' specification PROP. OBJECT
3977 is the object in which the `display' property was found. *POSITION
3978 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3979 means that we previously saw a display specification which already
3980 replaced text display with something else, for example an image;
3981 we ignore such properties after the first one has been processed.
3983 OVERLAY is the overlay this `display' property came from,
3984 or nil if it was a text property.
3986 If PROP is a `space' or `image' specification, and in some other
3987 cases too, set *POSITION to the position where the `display'
3988 property ends.
3990 Value is non-zero if something was found which replaces the display
3991 of buffer or string text. Specifically, the value is -1 if that
3992 "something" is "nothing". */
3994 static int
3995 handle_single_display_spec (it, spec, object, overlay, position,
3996 display_replaced_before_p)
3997 struct it *it;
3998 Lisp_Object spec;
3999 Lisp_Object object;
4000 Lisp_Object overlay;
4001 struct text_pos *position;
4002 int display_replaced_before_p;
4004 Lisp_Object form;
4005 Lisp_Object location, value;
4006 struct text_pos start_pos, save_pos;
4007 int valid_p;
4009 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4010 If the result is non-nil, use VALUE instead of SPEC. */
4011 form = Qt;
4012 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4014 spec = XCDR (spec);
4015 if (!CONSP (spec))
4016 return 0;
4017 form = XCAR (spec);
4018 spec = XCDR (spec);
4021 if (!NILP (form) && !EQ (form, Qt))
4023 int count = SPECPDL_INDEX ();
4024 struct gcpro gcpro1;
4026 /* Bind `object' to the object having the `display' property, a
4027 buffer or string. Bind `position' to the position in the
4028 object where the property was found, and `buffer-position'
4029 to the current position in the buffer. */
4030 specbind (Qobject, object);
4031 specbind (Qposition, make_number (CHARPOS (*position)));
4032 specbind (Qbuffer_position,
4033 make_number (STRINGP (object)
4034 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4035 GCPRO1 (form);
4036 form = safe_eval (form);
4037 UNGCPRO;
4038 unbind_to (count, Qnil);
4041 if (NILP (form))
4042 return 0;
4044 /* Handle `(height HEIGHT)' specifications. */
4045 if (CONSP (spec)
4046 && EQ (XCAR (spec), Qheight)
4047 && CONSP (XCDR (spec)))
4049 if (!FRAME_WINDOW_P (it->f))
4050 return 0;
4052 it->font_height = XCAR (XCDR (spec));
4053 if (!NILP (it->font_height))
4055 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4056 int new_height = -1;
4058 if (CONSP (it->font_height)
4059 && (EQ (XCAR (it->font_height), Qplus)
4060 || EQ (XCAR (it->font_height), Qminus))
4061 && CONSP (XCDR (it->font_height))
4062 && INTEGERP (XCAR (XCDR (it->font_height))))
4064 /* `(+ N)' or `(- N)' where N is an integer. */
4065 int steps = XINT (XCAR (XCDR (it->font_height)));
4066 if (EQ (XCAR (it->font_height), Qplus))
4067 steps = - steps;
4068 it->face_id = smaller_face (it->f, it->face_id, steps);
4070 else if (FUNCTIONP (it->font_height))
4072 /* Call function with current height as argument.
4073 Value is the new height. */
4074 Lisp_Object height;
4075 height = safe_call1 (it->font_height,
4076 face->lface[LFACE_HEIGHT_INDEX]);
4077 if (NUMBERP (height))
4078 new_height = XFLOATINT (height);
4080 else if (NUMBERP (it->font_height))
4082 /* Value is a multiple of the canonical char height. */
4083 struct face *face;
4085 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4086 new_height = (XFLOATINT (it->font_height)
4087 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4089 else
4091 /* Evaluate IT->font_height with `height' bound to the
4092 current specified height to get the new height. */
4093 int count = SPECPDL_INDEX ();
4095 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4096 value = safe_eval (it->font_height);
4097 unbind_to (count, Qnil);
4099 if (NUMBERP (value))
4100 new_height = XFLOATINT (value);
4103 if (new_height > 0)
4104 it->face_id = face_with_height (it->f, it->face_id, new_height);
4107 return 0;
4110 /* Handle `(space-width WIDTH)'. */
4111 if (CONSP (spec)
4112 && EQ (XCAR (spec), Qspace_width)
4113 && CONSP (XCDR (spec)))
4115 if (!FRAME_WINDOW_P (it->f))
4116 return 0;
4118 value = XCAR (XCDR (spec));
4119 if (NUMBERP (value) && XFLOATINT (value) > 0)
4120 it->space_width = value;
4122 return 0;
4125 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4126 if (CONSP (spec)
4127 && EQ (XCAR (spec), Qslice))
4129 Lisp_Object tem;
4131 if (!FRAME_WINDOW_P (it->f))
4132 return 0;
4134 if (tem = XCDR (spec), CONSP (tem))
4136 it->slice.x = XCAR (tem);
4137 if (tem = XCDR (tem), CONSP (tem))
4139 it->slice.y = XCAR (tem);
4140 if (tem = XCDR (tem), CONSP (tem))
4142 it->slice.width = XCAR (tem);
4143 if (tem = XCDR (tem), CONSP (tem))
4144 it->slice.height = XCAR (tem);
4149 return 0;
4152 /* Handle `(raise FACTOR)'. */
4153 if (CONSP (spec)
4154 && EQ (XCAR (spec), Qraise)
4155 && CONSP (XCDR (spec)))
4157 if (!FRAME_WINDOW_P (it->f))
4158 return 0;
4160 #ifdef HAVE_WINDOW_SYSTEM
4161 value = XCAR (XCDR (spec));
4162 if (NUMBERP (value))
4164 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4165 it->voffset = - (XFLOATINT (value)
4166 * (FONT_HEIGHT (face->font)));
4168 #endif /* HAVE_WINDOW_SYSTEM */
4170 return 0;
4173 /* Don't handle the other kinds of display specifications
4174 inside a string that we got from a `display' property. */
4175 if (it->string_from_display_prop_p)
4176 return 0;
4178 /* Characters having this form of property are not displayed, so
4179 we have to find the end of the property. */
4180 start_pos = *position;
4181 *position = display_prop_end (it, object, start_pos);
4182 value = Qnil;
4184 /* Stop the scan at that end position--we assume that all
4185 text properties change there. */
4186 it->stop_charpos = position->charpos;
4188 /* Handle `(left-fringe BITMAP [FACE])'
4189 and `(right-fringe BITMAP [FACE])'. */
4190 if (CONSP (spec)
4191 && (EQ (XCAR (spec), Qleft_fringe)
4192 || EQ (XCAR (spec), Qright_fringe))
4193 && CONSP (XCDR (spec)))
4195 int face_id = DEFAULT_FACE_ID;
4196 int fringe_bitmap;
4198 if (!FRAME_WINDOW_P (it->f))
4199 /* If we return here, POSITION has been advanced
4200 across the text with this property. */
4201 return 0;
4203 #ifdef HAVE_WINDOW_SYSTEM
4204 value = XCAR (XCDR (spec));
4205 if (!SYMBOLP (value)
4206 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4207 /* If we return here, POSITION has been advanced
4208 across the text with this property. */
4209 return 0;
4211 if (CONSP (XCDR (XCDR (spec))))
4213 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4214 int face_id2 = lookup_derived_face (it->f, face_name,
4215 FRINGE_FACE_ID, 0);
4216 if (face_id2 >= 0)
4217 face_id = face_id2;
4220 /* Save current settings of IT so that we can restore them
4221 when we are finished with the glyph property value. */
4223 save_pos = it->position;
4224 it->position = *position;
4225 push_it (it);
4226 it->position = save_pos;
4228 it->area = TEXT_AREA;
4229 it->what = IT_IMAGE;
4230 it->image_id = -1; /* no image */
4231 it->position = start_pos;
4232 it->object = NILP (object) ? it->w->buffer : object;
4233 it->method = GET_FROM_IMAGE;
4234 it->from_overlay = Qnil;
4235 it->face_id = face_id;
4237 /* Say that we haven't consumed the characters with
4238 `display' property yet. The call to pop_it in
4239 set_iterator_to_next will clean this up. */
4240 *position = start_pos;
4242 if (EQ (XCAR (spec), Qleft_fringe))
4244 it->left_user_fringe_bitmap = fringe_bitmap;
4245 it->left_user_fringe_face_id = face_id;
4247 else
4249 it->right_user_fringe_bitmap = fringe_bitmap;
4250 it->right_user_fringe_face_id = face_id;
4252 #endif /* HAVE_WINDOW_SYSTEM */
4253 return 1;
4256 /* Prepare to handle `((margin left-margin) ...)',
4257 `((margin right-margin) ...)' and `((margin nil) ...)'
4258 prefixes for display specifications. */
4259 location = Qunbound;
4260 if (CONSP (spec) && CONSP (XCAR (spec)))
4262 Lisp_Object tem;
4264 value = XCDR (spec);
4265 if (CONSP (value))
4266 value = XCAR (value);
4268 tem = XCAR (spec);
4269 if (EQ (XCAR (tem), Qmargin)
4270 && (tem = XCDR (tem),
4271 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4272 (NILP (tem)
4273 || EQ (tem, Qleft_margin)
4274 || EQ (tem, Qright_margin))))
4275 location = tem;
4278 if (EQ (location, Qunbound))
4280 location = Qnil;
4281 value = spec;
4284 /* After this point, VALUE is the property after any
4285 margin prefix has been stripped. It must be a string,
4286 an image specification, or `(space ...)'.
4288 LOCATION specifies where to display: `left-margin',
4289 `right-margin' or nil. */
4291 valid_p = (STRINGP (value)
4292 #ifdef HAVE_WINDOW_SYSTEM
4293 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4294 #endif /* not HAVE_WINDOW_SYSTEM */
4295 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4297 if (valid_p && !display_replaced_before_p)
4299 /* Save current settings of IT so that we can restore them
4300 when we are finished with the glyph property value. */
4301 save_pos = it->position;
4302 it->position = *position;
4303 push_it (it);
4304 it->position = save_pos;
4305 it->from_overlay = overlay;
4307 if (NILP (location))
4308 it->area = TEXT_AREA;
4309 else if (EQ (location, Qleft_margin))
4310 it->area = LEFT_MARGIN_AREA;
4311 else
4312 it->area = RIGHT_MARGIN_AREA;
4314 if (STRINGP (value))
4316 if (SCHARS (value) == 0)
4318 pop_it (it);
4319 return -1; /* Replaced by "", i.e. nothing. */
4321 it->string = value;
4322 it->multibyte_p = STRING_MULTIBYTE (it->string);
4323 it->current.overlay_string_index = -1;
4324 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4325 it->end_charpos = it->string_nchars = SCHARS (it->string);
4326 it->method = GET_FROM_STRING;
4327 it->stop_charpos = 0;
4328 it->string_from_display_prop_p = 1;
4329 /* Say that we haven't consumed the characters with
4330 `display' property yet. The call to pop_it in
4331 set_iterator_to_next will clean this up. */
4332 if (BUFFERP (object))
4333 it->current.pos = start_pos;
4335 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4337 it->method = GET_FROM_STRETCH;
4338 it->object = value;
4339 it->position = start_pos;
4340 if (BUFFERP (object))
4341 it->current.pos = start_pos;
4343 #ifdef HAVE_WINDOW_SYSTEM
4344 else
4346 it->what = IT_IMAGE;
4347 it->image_id = lookup_image (it->f, value);
4348 it->position = start_pos;
4349 it->object = NILP (object) ? it->w->buffer : object;
4350 it->method = GET_FROM_IMAGE;
4352 /* Say that we haven't consumed the characters with
4353 `display' property yet. The call to pop_it in
4354 set_iterator_to_next will clean this up. */
4355 if (BUFFERP (object))
4356 it->current.pos = start_pos;
4358 #endif /* HAVE_WINDOW_SYSTEM */
4360 return 1;
4363 /* Invalid property or property not supported. Restore
4364 POSITION to what it was before. */
4365 *position = start_pos;
4366 return 0;
4370 /* Check if SPEC is a display sub-property value whose text should be
4371 treated as intangible. */
4373 static int
4374 single_display_spec_intangible_p (prop)
4375 Lisp_Object prop;
4377 /* Skip over `when FORM'. */
4378 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4380 prop = XCDR (prop);
4381 if (!CONSP (prop))
4382 return 0;
4383 prop = XCDR (prop);
4386 if (STRINGP (prop))
4387 return 1;
4389 if (!CONSP (prop))
4390 return 0;
4392 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4393 we don't need to treat text as intangible. */
4394 if (EQ (XCAR (prop), Qmargin))
4396 prop = XCDR (prop);
4397 if (!CONSP (prop))
4398 return 0;
4400 prop = XCDR (prop);
4401 if (!CONSP (prop)
4402 || EQ (XCAR (prop), Qleft_margin)
4403 || EQ (XCAR (prop), Qright_margin))
4404 return 0;
4407 return (CONSP (prop)
4408 && (EQ (XCAR (prop), Qimage)
4409 || EQ (XCAR (prop), Qspace)));
4413 /* Check if PROP is a display property value whose text should be
4414 treated as intangible. */
4417 display_prop_intangible_p (prop)
4418 Lisp_Object prop;
4420 if (CONSP (prop)
4421 && CONSP (XCAR (prop))
4422 && !EQ (Qmargin, XCAR (XCAR (prop))))
4424 /* A list of sub-properties. */
4425 while (CONSP (prop))
4427 if (single_display_spec_intangible_p (XCAR (prop)))
4428 return 1;
4429 prop = XCDR (prop);
4432 else if (VECTORP (prop))
4434 /* A vector of sub-properties. */
4435 int i;
4436 for (i = 0; i < ASIZE (prop); ++i)
4437 if (single_display_spec_intangible_p (AREF (prop, i)))
4438 return 1;
4440 else
4441 return single_display_spec_intangible_p (prop);
4443 return 0;
4447 /* Return 1 if PROP is a display sub-property value containing STRING. */
4449 static int
4450 single_display_spec_string_p (prop, string)
4451 Lisp_Object prop, string;
4453 if (EQ (string, prop))
4454 return 1;
4456 /* Skip over `when FORM'. */
4457 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4459 prop = XCDR (prop);
4460 if (!CONSP (prop))
4461 return 0;
4462 prop = XCDR (prop);
4465 if (CONSP (prop))
4466 /* Skip over `margin LOCATION'. */
4467 if (EQ (XCAR (prop), Qmargin))
4469 prop = XCDR (prop);
4470 if (!CONSP (prop))
4471 return 0;
4473 prop = XCDR (prop);
4474 if (!CONSP (prop))
4475 return 0;
4478 return CONSP (prop) && EQ (XCAR (prop), string);
4482 /* Return 1 if STRING appears in the `display' property PROP. */
4484 static int
4485 display_prop_string_p (prop, string)
4486 Lisp_Object prop, string;
4488 if (CONSP (prop)
4489 && CONSP (XCAR (prop))
4490 && !EQ (Qmargin, XCAR (XCAR (prop))))
4492 /* A list of sub-properties. */
4493 while (CONSP (prop))
4495 if (single_display_spec_string_p (XCAR (prop), string))
4496 return 1;
4497 prop = XCDR (prop);
4500 else if (VECTORP (prop))
4502 /* A vector of sub-properties. */
4503 int i;
4504 for (i = 0; i < ASIZE (prop); ++i)
4505 if (single_display_spec_string_p (AREF (prop, i), string))
4506 return 1;
4508 else
4509 return single_display_spec_string_p (prop, string);
4511 return 0;
4515 /* Determine from which buffer position in W's buffer STRING comes
4516 from. AROUND_CHARPOS is an approximate position where it could
4517 be from. Value is the buffer position or 0 if it couldn't be
4518 determined.
4520 W's buffer must be current.
4522 This function is necessary because we don't record buffer positions
4523 in glyphs generated from strings (to keep struct glyph small).
4524 This function may only use code that doesn't eval because it is
4525 called asynchronously from note_mouse_highlight. */
4528 string_buffer_position (w, string, around_charpos)
4529 struct window *w;
4530 Lisp_Object string;
4531 int around_charpos;
4533 Lisp_Object limit, prop, pos;
4534 const int MAX_DISTANCE = 1000;
4535 int found = 0;
4537 pos = make_number (around_charpos);
4538 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4539 while (!found && !EQ (pos, limit))
4541 prop = Fget_char_property (pos, Qdisplay, Qnil);
4542 if (!NILP (prop) && display_prop_string_p (prop, string))
4543 found = 1;
4544 else
4545 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4548 if (!found)
4550 pos = make_number (around_charpos);
4551 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4552 while (!found && !EQ (pos, limit))
4554 prop = Fget_char_property (pos, Qdisplay, Qnil);
4555 if (!NILP (prop) && display_prop_string_p (prop, string))
4556 found = 1;
4557 else
4558 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4559 limit);
4563 return found ? XINT (pos) : 0;
4568 /***********************************************************************
4569 `composition' property
4570 ***********************************************************************/
4572 static enum prop_handled
4573 handle_auto_composed_prop (it)
4574 struct it *it;
4576 enum prop_handled handled = HANDLED_NORMALLY;
4578 if (FUNCTIONP (Vauto_composition_function))
4580 Lisp_Object val = Qnil;
4581 EMACS_INT pos, limit = -1;
4583 if (STRINGP (it->string))
4584 pos = IT_STRING_CHARPOS (*it);
4585 else
4586 pos = IT_CHARPOS (*it);
4588 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4589 if (! NILP (val))
4591 Lisp_Object cmp_prop;
4592 EMACS_INT cmp_start, cmp_end;
4594 #ifdef USE_FONT_BACKEND
4595 if (enable_font_backend
4596 && get_property_and_range (pos, Qcomposition, &cmp_prop,
4597 &cmp_start, &cmp_end, it->string)
4598 && cmp_start == pos
4599 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4601 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4602 Lisp_Object font_object = LGSTRING_FONT (gstring);
4604 if (! EQ (font_object,
4605 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4606 it->w, it->string)))
4607 /* We must re-compute the composition for the
4608 different font. */
4609 val = Qnil;
4611 #endif
4612 if (! NILP (val))
4614 Lisp_Object end;
4616 /* As Fnext_single_char_property_change is very slow, we
4617 limit the search to the current line. */
4618 if (STRINGP (it->string))
4619 limit = SCHARS (it->string);
4620 else
4621 limit = find_next_newline_no_quit (pos, 1);
4622 end = Fnext_single_char_property_change (make_number (pos),
4623 Qauto_composed,
4624 it->string,
4625 make_number (limit));
4627 if (XINT (end) < limit)
4628 /* The current point is auto-composed, but there exist
4629 characters not yet composed beyond the
4630 auto-composed region. There's a possiblity that
4631 the last characters in the region may be newly
4632 composed. */
4633 val = Qnil;
4636 if (NILP (val))
4638 if (limit < 0)
4639 limit = (STRINGP (it->string) ? SCHARS (it->string)
4640 : find_next_newline_no_quit (pos, 1));
4641 if (pos < limit)
4643 int count = SPECPDL_INDEX ();
4644 Lisp_Object args[5];
4646 args[0] = Vauto_composition_function;
4647 specbind (Qauto_composition_function, Qnil);
4648 args[1] = make_number (pos);
4649 args[2] = make_number (limit);
4650 #ifdef USE_FONT_BACKEND
4651 if (enable_font_backend)
4652 args[3] = it->window;
4653 else
4654 #endif /* USE_FONT_BACKEND */
4655 args[3] = Qnil;
4656 args[4] = it->string;
4657 safe_call (5, args);
4658 unbind_to (count, Qnil);
4663 return handled;
4666 /* Set up iterator IT from `composition' property at its current
4667 position. Called from handle_stop. */
4669 static enum prop_handled
4670 handle_composition_prop (it)
4671 struct it *it;
4673 Lisp_Object prop, string;
4674 EMACS_INT pos, pos_byte, start, end;
4675 enum prop_handled handled = HANDLED_NORMALLY;
4677 if (STRINGP (it->string))
4679 unsigned char *s;
4681 pos = IT_STRING_CHARPOS (*it);
4682 pos_byte = IT_STRING_BYTEPOS (*it);
4683 string = it->string;
4684 s = SDATA (string) + pos_byte;
4685 it->c = STRING_CHAR (s, 0);
4687 else
4689 pos = IT_CHARPOS (*it);
4690 pos_byte = IT_BYTEPOS (*it);
4691 string = Qnil;
4692 it->c = FETCH_CHAR (pos_byte);
4695 /* If there's a valid composition and point is not inside of the
4696 composition (in the case that the composition is from the current
4697 buffer), draw a glyph composed from the composition components. */
4698 if (find_composition (pos, -1, &start, &end, &prop, string)
4699 && COMPOSITION_VALID_P (start, end, prop)
4700 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4702 int id;
4704 if (start != pos)
4706 if (STRINGP (it->string))
4707 pos_byte = string_char_to_byte (it->string, start);
4708 else
4709 pos_byte = CHAR_TO_BYTE (start);
4711 id = get_composition_id (start, pos_byte, end - start, prop, string);
4713 if (id >= 0)
4715 struct composition *cmp = composition_table[id];
4717 if (cmp->glyph_len == 0)
4719 /* No glyph. */
4720 if (STRINGP (it->string))
4722 IT_STRING_CHARPOS (*it) = end;
4723 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4724 end);
4726 else
4728 IT_CHARPOS (*it) = end;
4729 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4731 return HANDLED_RECOMPUTE_PROPS;
4734 it->stop_charpos = end;
4735 push_it (it);
4737 it->method = GET_FROM_COMPOSITION;
4738 it->cmp_id = id;
4739 it->cmp_len = COMPOSITION_LENGTH (prop);
4740 /* For a terminal, draw only the first (non-TAB) character
4741 of the components. */
4742 #ifdef USE_FONT_BACKEND
4743 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4745 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4746 ->key_and_value,
4747 cmp->hash_index * 2);
4749 else
4750 #endif /* USE_FONT_BACKEND */
4752 int i;
4754 for (i = 0; i < cmp->glyph_len; i++)
4755 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4756 != '\t')
4757 break;
4759 if (it->c == '\t')
4760 it->c = ' ';
4761 it->len = (STRINGP (it->string)
4762 ? string_char_to_byte (it->string, end)
4763 : CHAR_TO_BYTE (end)) - pos_byte;
4764 handled = HANDLED_RETURN;
4768 return handled;
4773 /***********************************************************************
4774 Overlay strings
4775 ***********************************************************************/
4777 /* The following structure is used to record overlay strings for
4778 later sorting in load_overlay_strings. */
4780 struct overlay_entry
4782 Lisp_Object overlay;
4783 Lisp_Object string;
4784 int priority;
4785 int after_string_p;
4789 /* Set up iterator IT from overlay strings at its current position.
4790 Called from handle_stop. */
4792 static enum prop_handled
4793 handle_overlay_change (it)
4794 struct it *it;
4796 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4797 return HANDLED_RECOMPUTE_PROPS;
4798 else
4799 return HANDLED_NORMALLY;
4803 /* Set up the next overlay string for delivery by IT, if there is an
4804 overlay string to deliver. Called by set_iterator_to_next when the
4805 end of the current overlay string is reached. If there are more
4806 overlay strings to display, IT->string and
4807 IT->current.overlay_string_index are set appropriately here.
4808 Otherwise IT->string is set to nil. */
4810 static void
4811 next_overlay_string (it)
4812 struct it *it;
4814 ++it->current.overlay_string_index;
4815 if (it->current.overlay_string_index == it->n_overlay_strings)
4817 /* No more overlay strings. Restore IT's settings to what
4818 they were before overlay strings were processed, and
4819 continue to deliver from current_buffer. */
4820 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4822 pop_it (it);
4823 xassert (it->sp > 0
4824 || it->method == GET_FROM_COMPOSITION
4825 || (NILP (it->string)
4826 && it->method == GET_FROM_BUFFER
4827 && it->stop_charpos >= BEGV
4828 && it->stop_charpos <= it->end_charpos));
4829 it->current.overlay_string_index = -1;
4830 it->n_overlay_strings = 0;
4832 /* If we're at the end of the buffer, record that we have
4833 processed the overlay strings there already, so that
4834 next_element_from_buffer doesn't try it again. */
4835 if (IT_CHARPOS (*it) >= it->end_charpos)
4836 it->overlay_strings_at_end_processed_p = 1;
4838 /* If we have to display `...' for invisible text, set
4839 the iterator up for that. */
4840 if (display_ellipsis_p)
4841 setup_for_ellipsis (it, 0);
4843 else
4845 /* There are more overlay strings to process. If
4846 IT->current.overlay_string_index has advanced to a position
4847 where we must load IT->overlay_strings with more strings, do
4848 it. */
4849 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4851 if (it->current.overlay_string_index && i == 0)
4852 load_overlay_strings (it, 0);
4854 /* Initialize IT to deliver display elements from the overlay
4855 string. */
4856 it->string = it->overlay_strings[i];
4857 it->multibyte_p = STRING_MULTIBYTE (it->string);
4858 SET_TEXT_POS (it->current.string_pos, 0, 0);
4859 it->method = GET_FROM_STRING;
4860 it->stop_charpos = 0;
4863 CHECK_IT (it);
4867 /* Compare two overlay_entry structures E1 and E2. Used as a
4868 comparison function for qsort in load_overlay_strings. Overlay
4869 strings for the same position are sorted so that
4871 1. All after-strings come in front of before-strings, except
4872 when they come from the same overlay.
4874 2. Within after-strings, strings are sorted so that overlay strings
4875 from overlays with higher priorities come first.
4877 2. Within before-strings, strings are sorted so that overlay
4878 strings from overlays with higher priorities come last.
4880 Value is analogous to strcmp. */
4883 static int
4884 compare_overlay_entries (e1, e2)
4885 void *e1, *e2;
4887 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4888 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4889 int result;
4891 if (entry1->after_string_p != entry2->after_string_p)
4893 /* Let after-strings appear in front of before-strings if
4894 they come from different overlays. */
4895 if (EQ (entry1->overlay, entry2->overlay))
4896 result = entry1->after_string_p ? 1 : -1;
4897 else
4898 result = entry1->after_string_p ? -1 : 1;
4900 else if (entry1->after_string_p)
4901 /* After-strings sorted in order of decreasing priority. */
4902 result = entry2->priority - entry1->priority;
4903 else
4904 /* Before-strings sorted in order of increasing priority. */
4905 result = entry1->priority - entry2->priority;
4907 return result;
4911 /* Load the vector IT->overlay_strings with overlay strings from IT's
4912 current buffer position, or from CHARPOS if that is > 0. Set
4913 IT->n_overlays to the total number of overlay strings found.
4915 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4916 a time. On entry into load_overlay_strings,
4917 IT->current.overlay_string_index gives the number of overlay
4918 strings that have already been loaded by previous calls to this
4919 function.
4921 IT->add_overlay_start contains an additional overlay start
4922 position to consider for taking overlay strings from, if non-zero.
4923 This position comes into play when the overlay has an `invisible'
4924 property, and both before and after-strings. When we've skipped to
4925 the end of the overlay, because of its `invisible' property, we
4926 nevertheless want its before-string to appear.
4927 IT->add_overlay_start will contain the overlay start position
4928 in this case.
4930 Overlay strings are sorted so that after-string strings come in
4931 front of before-string strings. Within before and after-strings,
4932 strings are sorted by overlay priority. See also function
4933 compare_overlay_entries. */
4935 static void
4936 load_overlay_strings (it, charpos)
4937 struct it *it;
4938 int charpos;
4940 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4941 Lisp_Object overlay, window, str, invisible;
4942 struct Lisp_Overlay *ov;
4943 int start, end;
4944 int size = 20;
4945 int n = 0, i, j, invis_p;
4946 struct overlay_entry *entries
4947 = (struct overlay_entry *) alloca (size * sizeof *entries);
4949 if (charpos <= 0)
4950 charpos = IT_CHARPOS (*it);
4952 /* Append the overlay string STRING of overlay OVERLAY to vector
4953 `entries' which has size `size' and currently contains `n'
4954 elements. AFTER_P non-zero means STRING is an after-string of
4955 OVERLAY. */
4956 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4957 do \
4959 Lisp_Object priority; \
4961 if (n == size) \
4963 int new_size = 2 * size; \
4964 struct overlay_entry *old = entries; \
4965 entries = \
4966 (struct overlay_entry *) alloca (new_size \
4967 * sizeof *entries); \
4968 bcopy (old, entries, size * sizeof *entries); \
4969 size = new_size; \
4972 entries[n].string = (STRING); \
4973 entries[n].overlay = (OVERLAY); \
4974 priority = Foverlay_get ((OVERLAY), Qpriority); \
4975 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4976 entries[n].after_string_p = (AFTER_P); \
4977 ++n; \
4979 while (0)
4981 /* Process overlay before the overlay center. */
4982 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4984 XSETMISC (overlay, ov);
4985 xassert (OVERLAYP (overlay));
4986 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4987 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4989 if (end < charpos)
4990 break;
4992 /* Skip this overlay if it doesn't start or end at IT's current
4993 position. */
4994 if (end != charpos && start != charpos)
4995 continue;
4997 /* Skip this overlay if it doesn't apply to IT->w. */
4998 window = Foverlay_get (overlay, Qwindow);
4999 if (WINDOWP (window) && XWINDOW (window) != it->w)
5000 continue;
5002 /* If the text ``under'' the overlay is invisible, both before-
5003 and after-strings from this overlay are visible; start and
5004 end position are indistinguishable. */
5005 invisible = Foverlay_get (overlay, Qinvisible);
5006 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5008 /* If overlay has a non-empty before-string, record it. */
5009 if ((start == charpos || (end == charpos && invis_p))
5010 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5011 && SCHARS (str))
5012 RECORD_OVERLAY_STRING (overlay, str, 0);
5014 /* If overlay has a non-empty after-string, record it. */
5015 if ((end == charpos || (start == charpos && invis_p))
5016 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5017 && SCHARS (str))
5018 RECORD_OVERLAY_STRING (overlay, str, 1);
5021 /* Process overlays after the overlay center. */
5022 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5024 XSETMISC (overlay, ov);
5025 xassert (OVERLAYP (overlay));
5026 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5027 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5029 if (start > charpos)
5030 break;
5032 /* Skip this overlay if it doesn't start or end at IT's current
5033 position. */
5034 if (end != charpos && start != charpos)
5035 continue;
5037 /* Skip this overlay if it doesn't apply to IT->w. */
5038 window = Foverlay_get (overlay, Qwindow);
5039 if (WINDOWP (window) && XWINDOW (window) != it->w)
5040 continue;
5042 /* If the text ``under'' the overlay is invisible, it has a zero
5043 dimension, and both before- and after-strings apply. */
5044 invisible = Foverlay_get (overlay, Qinvisible);
5045 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5047 /* If overlay has a non-empty before-string, record it. */
5048 if ((start == charpos || (end == charpos && invis_p))
5049 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5050 && SCHARS (str))
5051 RECORD_OVERLAY_STRING (overlay, str, 0);
5053 /* If overlay has a non-empty after-string, record it. */
5054 if ((end == charpos || (start == charpos && invis_p))
5055 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5056 && SCHARS (str))
5057 RECORD_OVERLAY_STRING (overlay, str, 1);
5060 #undef RECORD_OVERLAY_STRING
5062 /* Sort entries. */
5063 if (n > 1)
5064 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5066 /* Record the total number of strings to process. */
5067 it->n_overlay_strings = n;
5069 /* IT->current.overlay_string_index is the number of overlay strings
5070 that have already been consumed by IT. Copy some of the
5071 remaining overlay strings to IT->overlay_strings. */
5072 i = 0;
5073 j = it->current.overlay_string_index;
5074 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5076 it->overlay_strings[i] = entries[j].string;
5077 it->string_overlays[i++] = entries[j++].overlay;
5080 CHECK_IT (it);
5084 /* Get the first chunk of overlay strings at IT's current buffer
5085 position, or at CHARPOS if that is > 0. Value is non-zero if at
5086 least one overlay string was found. */
5088 static int
5089 get_overlay_strings_1 (it, charpos, compute_stop_p)
5090 struct it *it;
5091 int charpos;
5093 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5094 process. This fills IT->overlay_strings with strings, and sets
5095 IT->n_overlay_strings to the total number of strings to process.
5096 IT->pos.overlay_string_index has to be set temporarily to zero
5097 because load_overlay_strings needs this; it must be set to -1
5098 when no overlay strings are found because a zero value would
5099 indicate a position in the first overlay string. */
5100 it->current.overlay_string_index = 0;
5101 load_overlay_strings (it, charpos);
5103 /* If we found overlay strings, set up IT to deliver display
5104 elements from the first one. Otherwise set up IT to deliver
5105 from current_buffer. */
5106 if (it->n_overlay_strings)
5108 /* Make sure we know settings in current_buffer, so that we can
5109 restore meaningful values when we're done with the overlay
5110 strings. */
5111 if (compute_stop_p)
5112 compute_stop_pos (it);
5113 xassert (it->face_id >= 0);
5115 /* Save IT's settings. They are restored after all overlay
5116 strings have been processed. */
5117 xassert (!compute_stop_p || it->sp == 0);
5118 push_it (it);
5120 /* Set up IT to deliver display elements from the first overlay
5121 string. */
5122 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5123 it->string = it->overlay_strings[0];
5124 it->from_overlay = Qnil;
5125 it->stop_charpos = 0;
5126 xassert (STRINGP (it->string));
5127 it->end_charpos = SCHARS (it->string);
5128 it->multibyte_p = STRING_MULTIBYTE (it->string);
5129 it->method = GET_FROM_STRING;
5130 return 1;
5133 it->current.overlay_string_index = -1;
5134 return 0;
5137 static int
5138 get_overlay_strings (it, charpos)
5139 struct it *it;
5140 int charpos;
5142 it->string = Qnil;
5143 it->method = GET_FROM_BUFFER;
5145 (void) get_overlay_strings_1 (it, charpos, 1);
5147 CHECK_IT (it);
5149 /* Value is non-zero if we found at least one overlay string. */
5150 return STRINGP (it->string);
5155 /***********************************************************************
5156 Saving and restoring state
5157 ***********************************************************************/
5159 /* Save current settings of IT on IT->stack. Called, for example,
5160 before setting up IT for an overlay string, to be able to restore
5161 IT's settings to what they were after the overlay string has been
5162 processed. */
5164 static void
5165 push_it (it)
5166 struct it *it;
5168 struct iterator_stack_entry *p;
5170 xassert (it->sp < IT_STACK_SIZE);
5171 p = it->stack + it->sp;
5173 p->stop_charpos = it->stop_charpos;
5174 xassert (it->face_id >= 0);
5175 p->face_id = it->face_id;
5176 p->string = it->string;
5177 p->method = it->method;
5178 p->from_overlay = it->from_overlay;
5179 switch (p->method)
5181 case GET_FROM_IMAGE:
5182 p->u.image.object = it->object;
5183 p->u.image.image_id = it->image_id;
5184 p->u.image.slice = it->slice;
5185 break;
5186 case GET_FROM_COMPOSITION:
5187 p->u.comp.object = it->object;
5188 p->u.comp.c = it->c;
5189 p->u.comp.len = it->len;
5190 p->u.comp.cmp_id = it->cmp_id;
5191 p->u.comp.cmp_len = it->cmp_len;
5192 break;
5193 case GET_FROM_STRETCH:
5194 p->u.stretch.object = it->object;
5195 break;
5197 p->position = it->position;
5198 p->current = it->current;
5199 p->end_charpos = it->end_charpos;
5200 p->string_nchars = it->string_nchars;
5201 p->area = it->area;
5202 p->multibyte_p = it->multibyte_p;
5203 p->space_width = it->space_width;
5204 p->font_height = it->font_height;
5205 p->voffset = it->voffset;
5206 p->string_from_display_prop_p = it->string_from_display_prop_p;
5207 p->display_ellipsis_p = 0;
5208 ++it->sp;
5212 /* Restore IT's settings from IT->stack. Called, for example, when no
5213 more overlay strings must be processed, and we return to delivering
5214 display elements from a buffer, or when the end of a string from a
5215 `display' property is reached and we return to delivering display
5216 elements from an overlay string, or from a buffer. */
5218 static void
5219 pop_it (it)
5220 struct it *it;
5222 struct iterator_stack_entry *p;
5224 xassert (it->sp > 0);
5225 --it->sp;
5226 p = it->stack + it->sp;
5227 it->stop_charpos = p->stop_charpos;
5228 it->face_id = p->face_id;
5229 it->current = p->current;
5230 it->position = p->position;
5231 it->string = p->string;
5232 it->from_overlay = p->from_overlay;
5233 if (NILP (it->string))
5234 SET_TEXT_POS (it->current.string_pos, -1, -1);
5235 it->method = p->method;
5236 switch (it->method)
5238 case GET_FROM_IMAGE:
5239 it->image_id = p->u.image.image_id;
5240 it->object = p->u.image.object;
5241 it->slice = p->u.image.slice;
5242 break;
5243 case GET_FROM_COMPOSITION:
5244 it->object = p->u.comp.object;
5245 it->c = p->u.comp.c;
5246 it->len = p->u.comp.len;
5247 it->cmp_id = p->u.comp.cmp_id;
5248 it->cmp_len = p->u.comp.cmp_len;
5249 break;
5250 case GET_FROM_STRETCH:
5251 it->object = p->u.comp.object;
5252 break;
5253 case GET_FROM_BUFFER:
5254 it->object = it->w->buffer;
5255 break;
5256 case GET_FROM_STRING:
5257 it->object = it->string;
5258 break;
5260 it->end_charpos = p->end_charpos;
5261 it->string_nchars = p->string_nchars;
5262 it->area = p->area;
5263 it->multibyte_p = p->multibyte_p;
5264 it->space_width = p->space_width;
5265 it->font_height = p->font_height;
5266 it->voffset = p->voffset;
5267 it->string_from_display_prop_p = p->string_from_display_prop_p;
5272 /***********************************************************************
5273 Moving over lines
5274 ***********************************************************************/
5276 /* Set IT's current position to the previous line start. */
5278 static void
5279 back_to_previous_line_start (it)
5280 struct it *it;
5282 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5283 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5287 /* Move IT to the next line start.
5289 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5290 we skipped over part of the text (as opposed to moving the iterator
5291 continuously over the text). Otherwise, don't change the value
5292 of *SKIPPED_P.
5294 Newlines may come from buffer text, overlay strings, or strings
5295 displayed via the `display' property. That's the reason we can't
5296 simply use find_next_newline_no_quit.
5298 Note that this function may not skip over invisible text that is so
5299 because of text properties and immediately follows a newline. If
5300 it would, function reseat_at_next_visible_line_start, when called
5301 from set_iterator_to_next, would effectively make invisible
5302 characters following a newline part of the wrong glyph row, which
5303 leads to wrong cursor motion. */
5305 static int
5306 forward_to_next_line_start (it, skipped_p)
5307 struct it *it;
5308 int *skipped_p;
5310 int old_selective, newline_found_p, n;
5311 const int MAX_NEWLINE_DISTANCE = 500;
5313 /* If already on a newline, just consume it to avoid unintended
5314 skipping over invisible text below. */
5315 if (it->what == IT_CHARACTER
5316 && it->c == '\n'
5317 && CHARPOS (it->position) == IT_CHARPOS (*it))
5319 set_iterator_to_next (it, 0);
5320 it->c = 0;
5321 return 1;
5324 /* Don't handle selective display in the following. It's (a)
5325 unnecessary because it's done by the caller, and (b) leads to an
5326 infinite recursion because next_element_from_ellipsis indirectly
5327 calls this function. */
5328 old_selective = it->selective;
5329 it->selective = 0;
5331 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5332 from buffer text. */
5333 for (n = newline_found_p = 0;
5334 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5335 n += STRINGP (it->string) ? 0 : 1)
5337 if (!get_next_display_element (it))
5338 return 0;
5339 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5340 set_iterator_to_next (it, 0);
5343 /* If we didn't find a newline near enough, see if we can use a
5344 short-cut. */
5345 if (!newline_found_p)
5347 int start = IT_CHARPOS (*it);
5348 int limit = find_next_newline_no_quit (start, 1);
5349 Lisp_Object pos;
5351 xassert (!STRINGP (it->string));
5353 /* If there isn't any `display' property in sight, and no
5354 overlays, we can just use the position of the newline in
5355 buffer text. */
5356 if (it->stop_charpos >= limit
5357 || ((pos = Fnext_single_property_change (make_number (start),
5358 Qdisplay,
5359 Qnil, make_number (limit)),
5360 NILP (pos))
5361 && next_overlay_change (start) == ZV))
5363 IT_CHARPOS (*it) = limit;
5364 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5365 *skipped_p = newline_found_p = 1;
5367 else
5369 while (get_next_display_element (it)
5370 && !newline_found_p)
5372 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5373 set_iterator_to_next (it, 0);
5378 it->selective = old_selective;
5379 return newline_found_p;
5383 /* Set IT's current position to the previous visible line start. Skip
5384 invisible text that is so either due to text properties or due to
5385 selective display. Caution: this does not change IT->current_x and
5386 IT->hpos. */
5388 static void
5389 back_to_previous_visible_line_start (it)
5390 struct it *it;
5392 while (IT_CHARPOS (*it) > BEGV)
5394 back_to_previous_line_start (it);
5396 if (IT_CHARPOS (*it) <= BEGV)
5397 break;
5399 /* If selective > 0, then lines indented more than that values
5400 are invisible. */
5401 if (it->selective > 0
5402 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5403 (double) it->selective)) /* iftc */
5404 continue;
5406 /* Check the newline before point for invisibility. */
5408 Lisp_Object prop;
5409 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5410 Qinvisible, it->window);
5411 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5412 continue;
5415 if (IT_CHARPOS (*it) <= BEGV)
5416 break;
5419 struct it it2;
5420 int pos;
5421 EMACS_INT beg, end;
5422 Lisp_Object val, overlay;
5424 /* If newline is part of a composition, continue from start of composition */
5425 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5426 && beg < IT_CHARPOS (*it))
5427 goto replaced;
5429 /* If newline is replaced by a display property, find start of overlay
5430 or interval and continue search from that point. */
5431 it2 = *it;
5432 pos = --IT_CHARPOS (it2);
5433 --IT_BYTEPOS (it2);
5434 it2.sp = 0;
5435 if (handle_display_prop (&it2) == HANDLED_RETURN
5436 && !NILP (val = get_char_property_and_overlay
5437 (make_number (pos), Qdisplay, Qnil, &overlay))
5438 && (OVERLAYP (overlay)
5439 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5440 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5441 goto replaced;
5443 /* Newline is not replaced by anything -- so we are done. */
5444 break;
5446 replaced:
5447 if (beg < BEGV)
5448 beg = BEGV;
5449 IT_CHARPOS (*it) = beg;
5450 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5454 it->continuation_lines_width = 0;
5456 xassert (IT_CHARPOS (*it) >= BEGV);
5457 xassert (IT_CHARPOS (*it) == BEGV
5458 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5459 CHECK_IT (it);
5463 /* Reseat iterator IT at the previous visible line start. Skip
5464 invisible text that is so either due to text properties or due to
5465 selective display. At the end, update IT's overlay information,
5466 face information etc. */
5468 void
5469 reseat_at_previous_visible_line_start (it)
5470 struct it *it;
5472 back_to_previous_visible_line_start (it);
5473 reseat (it, it->current.pos, 1);
5474 CHECK_IT (it);
5478 /* Reseat iterator IT on the next visible line start in the current
5479 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5480 preceding the line start. Skip over invisible text that is so
5481 because of selective display. Compute faces, overlays etc at the
5482 new position. Note that this function does not skip over text that
5483 is invisible because of text properties. */
5485 static void
5486 reseat_at_next_visible_line_start (it, on_newline_p)
5487 struct it *it;
5488 int on_newline_p;
5490 int newline_found_p, skipped_p = 0;
5492 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5494 /* Skip over lines that are invisible because they are indented
5495 more than the value of IT->selective. */
5496 if (it->selective > 0)
5497 while (IT_CHARPOS (*it) < ZV
5498 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5499 (double) it->selective)) /* iftc */
5501 xassert (IT_BYTEPOS (*it) == BEGV
5502 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5503 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5506 /* Position on the newline if that's what's requested. */
5507 if (on_newline_p && newline_found_p)
5509 if (STRINGP (it->string))
5511 if (IT_STRING_CHARPOS (*it) > 0)
5513 --IT_STRING_CHARPOS (*it);
5514 --IT_STRING_BYTEPOS (*it);
5517 else if (IT_CHARPOS (*it) > BEGV)
5519 --IT_CHARPOS (*it);
5520 --IT_BYTEPOS (*it);
5521 reseat (it, it->current.pos, 0);
5524 else if (skipped_p)
5525 reseat (it, it->current.pos, 0);
5527 CHECK_IT (it);
5532 /***********************************************************************
5533 Changing an iterator's position
5534 ***********************************************************************/
5536 /* Change IT's current position to POS in current_buffer. If FORCE_P
5537 is non-zero, always check for text properties at the new position.
5538 Otherwise, text properties are only looked up if POS >=
5539 IT->check_charpos of a property. */
5541 static void
5542 reseat (it, pos, force_p)
5543 struct it *it;
5544 struct text_pos pos;
5545 int force_p;
5547 int original_pos = IT_CHARPOS (*it);
5549 reseat_1 (it, pos, 0);
5551 /* Determine where to check text properties. Avoid doing it
5552 where possible because text property lookup is very expensive. */
5553 if (force_p
5554 || CHARPOS (pos) > it->stop_charpos
5555 || CHARPOS (pos) < original_pos)
5556 handle_stop (it);
5558 CHECK_IT (it);
5562 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5563 IT->stop_pos to POS, also. */
5565 static void
5566 reseat_1 (it, pos, set_stop_p)
5567 struct it *it;
5568 struct text_pos pos;
5569 int set_stop_p;
5571 /* Don't call this function when scanning a C string. */
5572 xassert (it->s == NULL);
5574 /* POS must be a reasonable value. */
5575 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5577 it->current.pos = it->position = pos;
5578 it->end_charpos = ZV;
5579 it->dpvec = NULL;
5580 it->current.dpvec_index = -1;
5581 it->current.overlay_string_index = -1;
5582 IT_STRING_CHARPOS (*it) = -1;
5583 IT_STRING_BYTEPOS (*it) = -1;
5584 it->string = Qnil;
5585 it->method = GET_FROM_BUFFER;
5586 it->object = it->w->buffer;
5587 it->area = TEXT_AREA;
5588 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5589 it->sp = 0;
5590 it->string_from_display_prop_p = 0;
5591 it->face_before_selective_p = 0;
5593 if (set_stop_p)
5594 it->stop_charpos = CHARPOS (pos);
5598 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5599 If S is non-null, it is a C string to iterate over. Otherwise,
5600 STRING gives a Lisp string to iterate over.
5602 If PRECISION > 0, don't return more then PRECISION number of
5603 characters from the string.
5605 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5606 characters have been returned. FIELD_WIDTH < 0 means an infinite
5607 field width.
5609 MULTIBYTE = 0 means disable processing of multibyte characters,
5610 MULTIBYTE > 0 means enable it,
5611 MULTIBYTE < 0 means use IT->multibyte_p.
5613 IT must be initialized via a prior call to init_iterator before
5614 calling this function. */
5616 static void
5617 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5618 struct it *it;
5619 unsigned char *s;
5620 Lisp_Object string;
5621 int charpos;
5622 int precision, field_width, multibyte;
5624 /* No region in strings. */
5625 it->region_beg_charpos = it->region_end_charpos = -1;
5627 /* No text property checks performed by default, but see below. */
5628 it->stop_charpos = -1;
5630 /* Set iterator position and end position. */
5631 bzero (&it->current, sizeof it->current);
5632 it->current.overlay_string_index = -1;
5633 it->current.dpvec_index = -1;
5634 xassert (charpos >= 0);
5636 /* If STRING is specified, use its multibyteness, otherwise use the
5637 setting of MULTIBYTE, if specified. */
5638 if (multibyte >= 0)
5639 it->multibyte_p = multibyte > 0;
5641 if (s == NULL)
5643 xassert (STRINGP (string));
5644 it->string = string;
5645 it->s = NULL;
5646 it->end_charpos = it->string_nchars = SCHARS (string);
5647 it->method = GET_FROM_STRING;
5648 it->current.string_pos = string_pos (charpos, string);
5650 else
5652 it->s = s;
5653 it->string = Qnil;
5655 /* Note that we use IT->current.pos, not it->current.string_pos,
5656 for displaying C strings. */
5657 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5658 if (it->multibyte_p)
5660 it->current.pos = c_string_pos (charpos, s, 1);
5661 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5663 else
5665 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5666 it->end_charpos = it->string_nchars = strlen (s);
5669 it->method = GET_FROM_C_STRING;
5672 /* PRECISION > 0 means don't return more than PRECISION characters
5673 from the string. */
5674 if (precision > 0 && it->end_charpos - charpos > precision)
5675 it->end_charpos = it->string_nchars = charpos + precision;
5677 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5678 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5679 FIELD_WIDTH < 0 means infinite field width. This is useful for
5680 padding with `-' at the end of a mode line. */
5681 if (field_width < 0)
5682 field_width = INFINITY;
5683 if (field_width > it->end_charpos - charpos)
5684 it->end_charpos = charpos + field_width;
5686 /* Use the standard display table for displaying strings. */
5687 if (DISP_TABLE_P (Vstandard_display_table))
5688 it->dp = XCHAR_TABLE (Vstandard_display_table);
5690 it->stop_charpos = charpos;
5691 CHECK_IT (it);
5696 /***********************************************************************
5697 Iteration
5698 ***********************************************************************/
5700 /* Map enum it_method value to corresponding next_element_from_* function. */
5702 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5704 next_element_from_buffer,
5705 next_element_from_display_vector,
5706 next_element_from_composition,
5707 next_element_from_string,
5708 next_element_from_c_string,
5709 next_element_from_image,
5710 next_element_from_stretch
5714 /* Load IT's display element fields with information about the next
5715 display element from the current position of IT. Value is zero if
5716 end of buffer (or C string) is reached. */
5718 static struct frame *last_escape_glyph_frame = NULL;
5719 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5720 static int last_escape_glyph_merged_face_id = 0;
5723 get_next_display_element (it)
5724 struct it *it;
5726 /* Non-zero means that we found a display element. Zero means that
5727 we hit the end of what we iterate over. Performance note: the
5728 function pointer `method' used here turns out to be faster than
5729 using a sequence of if-statements. */
5730 int success_p;
5732 get_next:
5733 success_p = (*get_next_element[it->method]) (it);
5735 if (it->what == IT_CHARACTER)
5737 /* Map via display table or translate control characters.
5738 IT->c, IT->len etc. have been set to the next character by
5739 the function call above. If we have a display table, and it
5740 contains an entry for IT->c, translate it. Don't do this if
5741 IT->c itself comes from a display table, otherwise we could
5742 end up in an infinite recursion. (An alternative could be to
5743 count the recursion depth of this function and signal an
5744 error when a certain maximum depth is reached.) Is it worth
5745 it? */
5746 if (success_p && it->dpvec == NULL)
5748 Lisp_Object dv;
5750 if (it->dp
5751 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5752 VECTORP (dv)))
5754 struct Lisp_Vector *v = XVECTOR (dv);
5756 /* Return the first character from the display table
5757 entry, if not empty. If empty, don't display the
5758 current character. */
5759 if (v->size)
5761 it->dpvec_char_len = it->len;
5762 it->dpvec = v->contents;
5763 it->dpend = v->contents + v->size;
5764 it->current.dpvec_index = 0;
5765 it->dpvec_face_id = -1;
5766 it->saved_face_id = it->face_id;
5767 it->method = GET_FROM_DISPLAY_VECTOR;
5768 it->ellipsis_p = 0;
5770 else
5772 set_iterator_to_next (it, 0);
5774 goto get_next;
5777 /* Translate control characters into `\003' or `^C' form.
5778 Control characters coming from a display table entry are
5779 currently not translated because we use IT->dpvec to hold
5780 the translation. This could easily be changed but I
5781 don't believe that it is worth doing.
5783 If it->multibyte_p is nonzero, non-printable non-ASCII
5784 characters are also translated to octal form.
5786 If it->multibyte_p is zero, eight-bit characters that
5787 don't have corresponding multibyte char code are also
5788 translated to octal form. */
5789 else if ((it->c < ' '
5790 ? (it->area != TEXT_AREA
5791 /* In mode line, treat \n, \t like other crl chars. */
5792 || (it->c != '\t'
5793 && it->glyph_row && it->glyph_row->mode_line_p)
5794 || (it->c != '\n' && it->c != '\t'))
5795 : (it->multibyte_p
5796 ? (!CHAR_PRINTABLE_P (it->c)
5797 || (!NILP (Vnobreak_char_display)
5798 && (it->c == 0xA0 /* NO-BREAK SPACE */
5799 || it->c == 0xAD /* SOFT HYPHEN */)))
5800 : (it->c >= 127
5801 && (! unibyte_display_via_language_environment
5802 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5804 /* IT->c is a control character which must be displayed
5805 either as '\003' or as `^C' where the '\\' and '^'
5806 can be defined in the display table. Fill
5807 IT->ctl_chars with glyphs for what we have to
5808 display. Then, set IT->dpvec to these glyphs. */
5809 GLYPH g;
5810 int ctl_len;
5811 int face_id, lface_id = 0 ;
5812 GLYPH escape_glyph;
5814 /* Handle control characters with ^. */
5816 if (it->c < 128 && it->ctl_arrow_p)
5818 g = '^'; /* default glyph for Control */
5819 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5820 if (it->dp
5821 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5822 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5824 g = XINT (DISP_CTRL_GLYPH (it->dp));
5825 lface_id = FAST_GLYPH_FACE (g);
5827 if (lface_id)
5829 g = FAST_GLYPH_CHAR (g);
5830 face_id = merge_faces (it->f, Qt, lface_id,
5831 it->face_id);
5833 else if (it->f == last_escape_glyph_frame
5834 && it->face_id == last_escape_glyph_face_id)
5836 face_id = last_escape_glyph_merged_face_id;
5838 else
5840 /* Merge the escape-glyph face into the current face. */
5841 face_id = merge_faces (it->f, Qescape_glyph, 0,
5842 it->face_id);
5843 last_escape_glyph_frame = it->f;
5844 last_escape_glyph_face_id = it->face_id;
5845 last_escape_glyph_merged_face_id = face_id;
5848 XSETINT (it->ctl_chars[0], g);
5849 g = it->c ^ 0100;
5850 XSETINT (it->ctl_chars[1], g);
5851 ctl_len = 2;
5852 goto display_control;
5855 /* Handle non-break space in the mode where it only gets
5856 highlighting. */
5858 if (EQ (Vnobreak_char_display, Qt)
5859 && it->c == 0xA0)
5861 /* Merge the no-break-space face into the current face. */
5862 face_id = merge_faces (it->f, Qnobreak_space, 0,
5863 it->face_id);
5865 g = it->c = ' ';
5866 XSETINT (it->ctl_chars[0], g);
5867 ctl_len = 1;
5868 goto display_control;
5871 /* Handle sequences that start with the "escape glyph". */
5873 /* the default escape glyph is \. */
5874 escape_glyph = '\\';
5876 if (it->dp
5877 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5878 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5880 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5881 lface_id = FAST_GLYPH_FACE (escape_glyph);
5883 if (lface_id)
5885 /* The display table specified a face.
5886 Merge it into face_id and also into escape_glyph. */
5887 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5888 face_id = merge_faces (it->f, Qt, lface_id,
5889 it->face_id);
5891 else if (it->f == last_escape_glyph_frame
5892 && it->face_id == last_escape_glyph_face_id)
5894 face_id = last_escape_glyph_merged_face_id;
5896 else
5898 /* Merge the escape-glyph face into the current face. */
5899 face_id = merge_faces (it->f, Qescape_glyph, 0,
5900 it->face_id);
5901 last_escape_glyph_frame = it->f;
5902 last_escape_glyph_face_id = it->face_id;
5903 last_escape_glyph_merged_face_id = face_id;
5906 /* Handle soft hyphens in the mode where they only get
5907 highlighting. */
5909 if (EQ (Vnobreak_char_display, Qt)
5910 && it->c == 0xAD)
5912 g = it->c = '-';
5913 XSETINT (it->ctl_chars[0], g);
5914 ctl_len = 1;
5915 goto display_control;
5918 /* Handle non-break space and soft hyphen
5919 with the escape glyph. */
5921 if (it->c == 0xA0 || it->c == 0xAD)
5923 XSETINT (it->ctl_chars[0], escape_glyph);
5924 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5925 XSETINT (it->ctl_chars[1], g);
5926 ctl_len = 2;
5927 goto display_control;
5931 unsigned char str[MAX_MULTIBYTE_LENGTH];
5932 int len;
5933 int i;
5935 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5936 if (CHAR_BYTE8_P (it->c))
5938 str[0] = CHAR_TO_BYTE8 (it->c);
5939 len = 1;
5941 else if (it->c < 256)
5943 str[0] = it->c;
5944 len = 1;
5946 else
5948 /* It's an invalid character, which shouldn't
5949 happen actually, but due to bugs it may
5950 happen. Let's print the char as is, there's
5951 not much meaningful we can do with it. */
5952 str[0] = it->c;
5953 str[1] = it->c >> 8;
5954 str[2] = it->c >> 16;
5955 str[3] = it->c >> 24;
5956 len = 4;
5959 for (i = 0; i < len; i++)
5961 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5962 /* Insert three more glyphs into IT->ctl_chars for
5963 the octal display of the character. */
5964 g = ((str[i] >> 6) & 7) + '0';
5965 XSETINT (it->ctl_chars[i * 4 + 1], g);
5966 g = ((str[i] >> 3) & 7) + '0';
5967 XSETINT (it->ctl_chars[i * 4 + 2], g);
5968 g = (str[i] & 7) + '0';
5969 XSETINT (it->ctl_chars[i * 4 + 3], g);
5971 ctl_len = len * 4;
5974 display_control:
5975 /* Set up IT->dpvec and return first character from it. */
5976 it->dpvec_char_len = it->len;
5977 it->dpvec = it->ctl_chars;
5978 it->dpend = it->dpvec + ctl_len;
5979 it->current.dpvec_index = 0;
5980 it->dpvec_face_id = face_id;
5981 it->saved_face_id = it->face_id;
5982 it->method = GET_FROM_DISPLAY_VECTOR;
5983 it->ellipsis_p = 0;
5984 goto get_next;
5989 /* Adjust face id for a multibyte character. There are no multibyte
5990 character in unibyte text. */
5991 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5992 && it->multibyte_p
5993 && success_p
5994 && FRAME_WINDOW_P (it->f))
5996 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5997 int pos = (it->s ? -1
5998 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5999 : IT_CHARPOS (*it));
6001 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6004 /* Is this character the last one of a run of characters with
6005 box? If yes, set IT->end_of_box_run_p to 1. */
6006 if (it->face_box_p
6007 && it->s == NULL)
6009 int face_id;
6010 struct face *face;
6012 it->end_of_box_run_p
6013 = ((face_id = face_after_it_pos (it),
6014 face_id != it->face_id)
6015 && (face = FACE_FROM_ID (it->f, face_id),
6016 face->box == FACE_NO_BOX));
6019 /* Value is 0 if end of buffer or string reached. */
6020 return success_p;
6024 /* Move IT to the next display element.
6026 RESEAT_P non-zero means if called on a newline in buffer text,
6027 skip to the next visible line start.
6029 Functions get_next_display_element and set_iterator_to_next are
6030 separate because I find this arrangement easier to handle than a
6031 get_next_display_element function that also increments IT's
6032 position. The way it is we can first look at an iterator's current
6033 display element, decide whether it fits on a line, and if it does,
6034 increment the iterator position. The other way around we probably
6035 would either need a flag indicating whether the iterator has to be
6036 incremented the next time, or we would have to implement a
6037 decrement position function which would not be easy to write. */
6039 void
6040 set_iterator_to_next (it, reseat_p)
6041 struct it *it;
6042 int reseat_p;
6044 /* Reset flags indicating start and end of a sequence of characters
6045 with box. Reset them at the start of this function because
6046 moving the iterator to a new position might set them. */
6047 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6049 switch (it->method)
6051 case GET_FROM_BUFFER:
6052 /* The current display element of IT is a character from
6053 current_buffer. Advance in the buffer, and maybe skip over
6054 invisible lines that are so because of selective display. */
6055 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6056 reseat_at_next_visible_line_start (it, 0);
6057 else
6059 xassert (it->len != 0);
6060 IT_BYTEPOS (*it) += it->len;
6061 IT_CHARPOS (*it) += 1;
6062 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6064 break;
6066 case GET_FROM_COMPOSITION:
6067 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6068 xassert (it->sp > 0);
6069 pop_it (it);
6070 if (it->method == GET_FROM_STRING)
6072 IT_STRING_BYTEPOS (*it) += it->len;
6073 IT_STRING_CHARPOS (*it) += it->cmp_len;
6074 goto consider_string_end;
6076 else if (it->method == GET_FROM_BUFFER)
6078 IT_BYTEPOS (*it) += it->len;
6079 IT_CHARPOS (*it) += it->cmp_len;
6081 break;
6083 case GET_FROM_C_STRING:
6084 /* Current display element of IT is from a C string. */
6085 IT_BYTEPOS (*it) += it->len;
6086 IT_CHARPOS (*it) += 1;
6087 break;
6089 case GET_FROM_DISPLAY_VECTOR:
6090 /* Current display element of IT is from a display table entry.
6091 Advance in the display table definition. Reset it to null if
6092 end reached, and continue with characters from buffers/
6093 strings. */
6094 ++it->current.dpvec_index;
6096 /* Restore face of the iterator to what they were before the
6097 display vector entry (these entries may contain faces). */
6098 it->face_id = it->saved_face_id;
6100 if (it->dpvec + it->current.dpvec_index == it->dpend)
6102 int recheck_faces = it->ellipsis_p;
6104 if (it->s)
6105 it->method = GET_FROM_C_STRING;
6106 else if (STRINGP (it->string))
6107 it->method = GET_FROM_STRING;
6108 else
6110 it->method = GET_FROM_BUFFER;
6111 it->object = it->w->buffer;
6114 it->dpvec = NULL;
6115 it->current.dpvec_index = -1;
6117 /* Skip over characters which were displayed via IT->dpvec. */
6118 if (it->dpvec_char_len < 0)
6119 reseat_at_next_visible_line_start (it, 1);
6120 else if (it->dpvec_char_len > 0)
6122 if (it->method == GET_FROM_STRING
6123 && it->n_overlay_strings > 0)
6124 it->ignore_overlay_strings_at_pos_p = 1;
6125 it->len = it->dpvec_char_len;
6126 set_iterator_to_next (it, reseat_p);
6129 /* Maybe recheck faces after display vector */
6130 if (recheck_faces)
6131 it->stop_charpos = IT_CHARPOS (*it);
6133 break;
6135 case GET_FROM_STRING:
6136 /* Current display element is a character from a Lisp string. */
6137 xassert (it->s == NULL && STRINGP (it->string));
6138 IT_STRING_BYTEPOS (*it) += it->len;
6139 IT_STRING_CHARPOS (*it) += 1;
6141 consider_string_end:
6143 if (it->current.overlay_string_index >= 0)
6145 /* IT->string is an overlay string. Advance to the
6146 next, if there is one. */
6147 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6148 next_overlay_string (it);
6150 else
6152 /* IT->string is not an overlay string. If we reached
6153 its end, and there is something on IT->stack, proceed
6154 with what is on the stack. This can be either another
6155 string, this time an overlay string, or a buffer. */
6156 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6157 && it->sp > 0)
6159 pop_it (it);
6160 if (it->method == GET_FROM_STRING)
6161 goto consider_string_end;
6164 break;
6166 case GET_FROM_IMAGE:
6167 case GET_FROM_STRETCH:
6168 /* The position etc with which we have to proceed are on
6169 the stack. The position may be at the end of a string,
6170 if the `display' property takes up the whole string. */
6171 xassert (it->sp > 0);
6172 pop_it (it);
6173 if (it->method == GET_FROM_STRING)
6174 goto consider_string_end;
6175 break;
6177 default:
6178 /* There are no other methods defined, so this should be a bug. */
6179 abort ();
6182 xassert (it->method != GET_FROM_STRING
6183 || (STRINGP (it->string)
6184 && IT_STRING_CHARPOS (*it) >= 0));
6187 /* Load IT's display element fields with information about the next
6188 display element which comes from a display table entry or from the
6189 result of translating a control character to one of the forms `^C'
6190 or `\003'.
6192 IT->dpvec holds the glyphs to return as characters.
6193 IT->saved_face_id holds the face id before the display vector--
6194 it is restored into IT->face_idin set_iterator_to_next. */
6196 static int
6197 next_element_from_display_vector (it)
6198 struct it *it;
6200 /* Precondition. */
6201 xassert (it->dpvec && it->current.dpvec_index >= 0);
6203 it->face_id = it->saved_face_id;
6205 if (INTEGERP (*it->dpvec)
6206 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6208 GLYPH g;
6210 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6211 it->c = FAST_GLYPH_CHAR (g);
6212 it->len = CHAR_BYTES (it->c);
6214 /* The entry may contain a face id to use. Such a face id is
6215 the id of a Lisp face, not a realized face. A face id of
6216 zero means no face is specified. */
6217 if (it->dpvec_face_id >= 0)
6218 it->face_id = it->dpvec_face_id;
6219 else
6221 int lface_id = FAST_GLYPH_FACE (g);
6222 if (lface_id > 0)
6223 it->face_id = merge_faces (it->f, Qt, lface_id,
6224 it->saved_face_id);
6227 else
6228 /* Display table entry is invalid. Return a space. */
6229 it->c = ' ', it->len = 1;
6231 /* Don't change position and object of the iterator here. They are
6232 still the values of the character that had this display table
6233 entry or was translated, and that's what we want. */
6234 it->what = IT_CHARACTER;
6235 return 1;
6239 /* Load IT with the next display element from Lisp string IT->string.
6240 IT->current.string_pos is the current position within the string.
6241 If IT->current.overlay_string_index >= 0, the Lisp string is an
6242 overlay string. */
6244 static int
6245 next_element_from_string (it)
6246 struct it *it;
6248 struct text_pos position;
6250 xassert (STRINGP (it->string));
6251 xassert (IT_STRING_CHARPOS (*it) >= 0);
6252 position = it->current.string_pos;
6254 /* Time to check for invisible text? */
6255 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6256 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6258 handle_stop (it);
6260 /* Since a handler may have changed IT->method, we must
6261 recurse here. */
6262 return get_next_display_element (it);
6265 if (it->current.overlay_string_index >= 0)
6267 /* Get the next character from an overlay string. In overlay
6268 strings, There is no field width or padding with spaces to
6269 do. */
6270 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6272 it->what = IT_EOB;
6273 return 0;
6275 else if (STRING_MULTIBYTE (it->string))
6277 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6278 const unsigned char *s = (SDATA (it->string)
6279 + IT_STRING_BYTEPOS (*it));
6280 it->c = string_char_and_length (s, remaining, &it->len);
6282 else
6284 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6285 it->len = 1;
6288 else
6290 /* Get the next character from a Lisp string that is not an
6291 overlay string. Such strings come from the mode line, for
6292 example. We may have to pad with spaces, or truncate the
6293 string. See also next_element_from_c_string. */
6294 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6296 it->what = IT_EOB;
6297 return 0;
6299 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6301 /* Pad with spaces. */
6302 it->c = ' ', it->len = 1;
6303 CHARPOS (position) = BYTEPOS (position) = -1;
6305 else if (STRING_MULTIBYTE (it->string))
6307 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6308 const unsigned char *s = (SDATA (it->string)
6309 + IT_STRING_BYTEPOS (*it));
6310 it->c = string_char_and_length (s, maxlen, &it->len);
6312 else
6314 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6315 it->len = 1;
6319 /* Record what we have and where it came from. */
6320 it->what = IT_CHARACTER;
6321 it->object = it->string;
6322 it->position = position;
6323 return 1;
6327 /* Load IT with next display element from C string IT->s.
6328 IT->string_nchars is the maximum number of characters to return
6329 from the string. IT->end_charpos may be greater than
6330 IT->string_nchars when this function is called, in which case we
6331 may have to return padding spaces. Value is zero if end of string
6332 reached, including padding spaces. */
6334 static int
6335 next_element_from_c_string (it)
6336 struct it *it;
6338 int success_p = 1;
6340 xassert (it->s);
6341 it->what = IT_CHARACTER;
6342 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6343 it->object = Qnil;
6345 /* IT's position can be greater IT->string_nchars in case a field
6346 width or precision has been specified when the iterator was
6347 initialized. */
6348 if (IT_CHARPOS (*it) >= it->end_charpos)
6350 /* End of the game. */
6351 it->what = IT_EOB;
6352 success_p = 0;
6354 else if (IT_CHARPOS (*it) >= it->string_nchars)
6356 /* Pad with spaces. */
6357 it->c = ' ', it->len = 1;
6358 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6360 else if (it->multibyte_p)
6362 /* Implementation note: The calls to strlen apparently aren't a
6363 performance problem because there is no noticeable performance
6364 difference between Emacs running in unibyte or multibyte mode. */
6365 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6366 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6367 maxlen, &it->len);
6369 else
6370 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6372 return success_p;
6376 /* Set up IT to return characters from an ellipsis, if appropriate.
6377 The definition of the ellipsis glyphs may come from a display table
6378 entry. This function Fills IT with the first glyph from the
6379 ellipsis if an ellipsis is to be displayed. */
6381 static int
6382 next_element_from_ellipsis (it)
6383 struct it *it;
6385 if (it->selective_display_ellipsis_p)
6386 setup_for_ellipsis (it, it->len);
6387 else
6389 /* The face at the current position may be different from the
6390 face we find after the invisible text. Remember what it
6391 was in IT->saved_face_id, and signal that it's there by
6392 setting face_before_selective_p. */
6393 it->saved_face_id = it->face_id;
6394 it->method = GET_FROM_BUFFER;
6395 it->object = it->w->buffer;
6396 reseat_at_next_visible_line_start (it, 1);
6397 it->face_before_selective_p = 1;
6400 return get_next_display_element (it);
6404 /* Deliver an image display element. The iterator IT is already
6405 filled with image information (done in handle_display_prop). Value
6406 is always 1. */
6409 static int
6410 next_element_from_image (it)
6411 struct it *it;
6413 it->what = IT_IMAGE;
6414 return 1;
6418 /* Fill iterator IT with next display element from a stretch glyph
6419 property. IT->object is the value of the text property. Value is
6420 always 1. */
6422 static int
6423 next_element_from_stretch (it)
6424 struct it *it;
6426 it->what = IT_STRETCH;
6427 return 1;
6431 /* Load IT with the next display element from current_buffer. Value
6432 is zero if end of buffer reached. IT->stop_charpos is the next
6433 position at which to stop and check for text properties or buffer
6434 end. */
6436 static int
6437 next_element_from_buffer (it)
6438 struct it *it;
6440 int success_p = 1;
6442 /* Check this assumption, otherwise, we would never enter the
6443 if-statement, below. */
6444 xassert (IT_CHARPOS (*it) >= BEGV
6445 && IT_CHARPOS (*it) <= it->stop_charpos);
6447 if (IT_CHARPOS (*it) >= it->stop_charpos)
6449 if (IT_CHARPOS (*it) >= it->end_charpos)
6451 int overlay_strings_follow_p;
6453 /* End of the game, except when overlay strings follow that
6454 haven't been returned yet. */
6455 if (it->overlay_strings_at_end_processed_p)
6456 overlay_strings_follow_p = 0;
6457 else
6459 it->overlay_strings_at_end_processed_p = 1;
6460 overlay_strings_follow_p = get_overlay_strings (it, 0);
6463 if (overlay_strings_follow_p)
6464 success_p = get_next_display_element (it);
6465 else
6467 it->what = IT_EOB;
6468 it->position = it->current.pos;
6469 success_p = 0;
6472 else
6474 handle_stop (it);
6475 return get_next_display_element (it);
6478 else
6480 /* No face changes, overlays etc. in sight, so just return a
6481 character from current_buffer. */
6482 unsigned char *p;
6484 /* Maybe run the redisplay end trigger hook. Performance note:
6485 This doesn't seem to cost measurable time. */
6486 if (it->redisplay_end_trigger_charpos
6487 && it->glyph_row
6488 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6489 run_redisplay_end_trigger_hook (it);
6491 /* Get the next character, maybe multibyte. */
6492 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6493 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6495 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6496 - IT_BYTEPOS (*it));
6497 it->c = string_char_and_length (p, maxlen, &it->len);
6499 else
6500 it->c = *p, it->len = 1;
6502 /* Record what we have and where it came from. */
6503 it->what = IT_CHARACTER;
6504 it->object = it->w->buffer;
6505 it->position = it->current.pos;
6507 /* Normally we return the character found above, except when we
6508 really want to return an ellipsis for selective display. */
6509 if (it->selective)
6511 if (it->c == '\n')
6513 /* A value of selective > 0 means hide lines indented more
6514 than that number of columns. */
6515 if (it->selective > 0
6516 && IT_CHARPOS (*it) + 1 < ZV
6517 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6518 IT_BYTEPOS (*it) + 1,
6519 (double) it->selective)) /* iftc */
6521 success_p = next_element_from_ellipsis (it);
6522 it->dpvec_char_len = -1;
6525 else if (it->c == '\r' && it->selective == -1)
6527 /* A value of selective == -1 means that everything from the
6528 CR to the end of the line is invisible, with maybe an
6529 ellipsis displayed for it. */
6530 success_p = next_element_from_ellipsis (it);
6531 it->dpvec_char_len = -1;
6536 /* Value is zero if end of buffer reached. */
6537 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6538 return success_p;
6542 /* Run the redisplay end trigger hook for IT. */
6544 static void
6545 run_redisplay_end_trigger_hook (it)
6546 struct it *it;
6548 Lisp_Object args[3];
6550 /* IT->glyph_row should be non-null, i.e. we should be actually
6551 displaying something, or otherwise we should not run the hook. */
6552 xassert (it->glyph_row);
6554 /* Set up hook arguments. */
6555 args[0] = Qredisplay_end_trigger_functions;
6556 args[1] = it->window;
6557 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6558 it->redisplay_end_trigger_charpos = 0;
6560 /* Since we are *trying* to run these functions, don't try to run
6561 them again, even if they get an error. */
6562 it->w->redisplay_end_trigger = Qnil;
6563 Frun_hook_with_args (3, args);
6565 /* Notice if it changed the face of the character we are on. */
6566 handle_face_prop (it);
6570 /* Deliver a composition display element. The iterator IT is already
6571 filled with composition information (done in
6572 handle_composition_prop). Value is always 1. */
6574 static int
6575 next_element_from_composition (it)
6576 struct it *it;
6578 it->what = IT_COMPOSITION;
6579 it->position = (STRINGP (it->string)
6580 ? it->current.string_pos
6581 : it->current.pos);
6582 if (STRINGP (it->string))
6583 it->object = it->string;
6584 else
6585 it->object = it->w->buffer;
6586 return 1;
6591 /***********************************************************************
6592 Moving an iterator without producing glyphs
6593 ***********************************************************************/
6595 /* Check if iterator is at a position corresponding to a valid buffer
6596 position after some move_it_ call. */
6598 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6599 ((it)->method == GET_FROM_STRING \
6600 ? IT_STRING_CHARPOS (*it) == 0 \
6601 : 1)
6604 /* Move iterator IT to a specified buffer or X position within one
6605 line on the display without producing glyphs.
6607 OP should be a bit mask including some or all of these bits:
6608 MOVE_TO_X: Stop on reaching x-position TO_X.
6609 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6610 Regardless of OP's value, stop in reaching the end of the display line.
6612 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6613 This means, in particular, that TO_X includes window's horizontal
6614 scroll amount.
6616 The return value has several possible values that
6617 say what condition caused the scan to stop:
6619 MOVE_POS_MATCH_OR_ZV
6620 - when TO_POS or ZV was reached.
6622 MOVE_X_REACHED
6623 -when TO_X was reached before TO_POS or ZV were reached.
6625 MOVE_LINE_CONTINUED
6626 - when we reached the end of the display area and the line must
6627 be continued.
6629 MOVE_LINE_TRUNCATED
6630 - when we reached the end of the display area and the line is
6631 truncated.
6633 MOVE_NEWLINE_OR_CR
6634 - when we stopped at a line end, i.e. a newline or a CR and selective
6635 display is on. */
6637 static enum move_it_result
6638 move_it_in_display_line_to (it, to_charpos, to_x, op)
6639 struct it *it;
6640 int to_charpos, to_x, op;
6642 enum move_it_result result = MOVE_UNDEFINED;
6643 struct glyph_row *saved_glyph_row;
6645 /* Don't produce glyphs in produce_glyphs. */
6646 saved_glyph_row = it->glyph_row;
6647 it->glyph_row = NULL;
6649 #define BUFFER_POS_REACHED_P() \
6650 ((op & MOVE_TO_POS) != 0 \
6651 && BUFFERP (it->object) \
6652 && IT_CHARPOS (*it) >= to_charpos \
6653 && (it->method == GET_FROM_BUFFER \
6654 || (it->method == GET_FROM_DISPLAY_VECTOR \
6655 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6658 while (1)
6660 int x, i, ascent = 0, descent = 0;
6662 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6663 if ((op & MOVE_TO_POS) != 0
6664 && BUFFERP (it->object)
6665 && it->method == GET_FROM_BUFFER
6666 && IT_CHARPOS (*it) > to_charpos)
6668 result = MOVE_POS_MATCH_OR_ZV;
6669 break;
6672 /* Stop when ZV reached.
6673 We used to stop here when TO_CHARPOS reached as well, but that is
6674 too soon if this glyph does not fit on this line. So we handle it
6675 explicitly below. */
6676 if (!get_next_display_element (it)
6677 || (it->truncate_lines_p
6678 && BUFFER_POS_REACHED_P ()))
6680 result = MOVE_POS_MATCH_OR_ZV;
6681 break;
6684 /* The call to produce_glyphs will get the metrics of the
6685 display element IT is loaded with. We record in x the
6686 x-position before this display element in case it does not
6687 fit on the line. */
6688 x = it->current_x;
6690 /* Remember the line height so far in case the next element doesn't
6691 fit on the line. */
6692 if (!it->truncate_lines_p)
6694 ascent = it->max_ascent;
6695 descent = it->max_descent;
6698 PRODUCE_GLYPHS (it);
6700 if (it->area != TEXT_AREA)
6702 set_iterator_to_next (it, 1);
6703 continue;
6706 /* The number of glyphs we get back in IT->nglyphs will normally
6707 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6708 character on a terminal frame, or (iii) a line end. For the
6709 second case, IT->nglyphs - 1 padding glyphs will be present
6710 (on X frames, there is only one glyph produced for a
6711 composite character.
6713 The behavior implemented below means, for continuation lines,
6714 that as many spaces of a TAB as fit on the current line are
6715 displayed there. For terminal frames, as many glyphs of a
6716 multi-glyph character are displayed in the current line, too.
6717 This is what the old redisplay code did, and we keep it that
6718 way. Under X, the whole shape of a complex character must
6719 fit on the line or it will be completely displayed in the
6720 next line.
6722 Note that both for tabs and padding glyphs, all glyphs have
6723 the same width. */
6724 if (it->nglyphs)
6726 /* More than one glyph or glyph doesn't fit on line. All
6727 glyphs have the same width. */
6728 int single_glyph_width = it->pixel_width / it->nglyphs;
6729 int new_x;
6730 int x_before_this_char = x;
6731 int hpos_before_this_char = it->hpos;
6733 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6735 new_x = x + single_glyph_width;
6737 /* We want to leave anything reaching TO_X to the caller. */
6738 if ((op & MOVE_TO_X) && new_x > to_x)
6740 if (BUFFER_POS_REACHED_P ())
6741 goto buffer_pos_reached;
6742 it->current_x = x;
6743 result = MOVE_X_REACHED;
6744 break;
6746 else if (/* Lines are continued. */
6747 !it->truncate_lines_p
6748 && (/* And glyph doesn't fit on the line. */
6749 new_x > it->last_visible_x
6750 /* Or it fits exactly and we're on a window
6751 system frame. */
6752 || (new_x == it->last_visible_x
6753 && FRAME_WINDOW_P (it->f))))
6755 if (/* IT->hpos == 0 means the very first glyph
6756 doesn't fit on the line, e.g. a wide image. */
6757 it->hpos == 0
6758 || (new_x == it->last_visible_x
6759 && FRAME_WINDOW_P (it->f)))
6761 ++it->hpos;
6762 it->current_x = new_x;
6764 /* The character's last glyph just barely fits
6765 in this row. */
6766 if (i == it->nglyphs - 1)
6768 /* If this is the destination position,
6769 return a position *before* it in this row,
6770 now that we know it fits in this row. */
6771 if (BUFFER_POS_REACHED_P ())
6773 it->hpos = hpos_before_this_char;
6774 it->current_x = x_before_this_char;
6775 result = MOVE_POS_MATCH_OR_ZV;
6776 break;
6779 set_iterator_to_next (it, 1);
6780 #ifdef HAVE_WINDOW_SYSTEM
6781 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6783 if (!get_next_display_element (it))
6785 result = MOVE_POS_MATCH_OR_ZV;
6786 break;
6788 if (BUFFER_POS_REACHED_P ())
6790 if (ITERATOR_AT_END_OF_LINE_P (it))
6791 result = MOVE_POS_MATCH_OR_ZV;
6792 else
6793 result = MOVE_LINE_CONTINUED;
6794 break;
6796 if (ITERATOR_AT_END_OF_LINE_P (it))
6798 result = MOVE_NEWLINE_OR_CR;
6799 break;
6802 #endif /* HAVE_WINDOW_SYSTEM */
6805 else
6807 it->current_x = x;
6808 it->max_ascent = ascent;
6809 it->max_descent = descent;
6812 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6813 IT_CHARPOS (*it)));
6814 result = MOVE_LINE_CONTINUED;
6815 break;
6817 else if (BUFFER_POS_REACHED_P ())
6818 goto buffer_pos_reached;
6819 else if (new_x > it->first_visible_x)
6821 /* Glyph is visible. Increment number of glyphs that
6822 would be displayed. */
6823 ++it->hpos;
6825 else
6827 /* Glyph is completely off the left margin of the display
6828 area. Nothing to do. */
6832 if (result != MOVE_UNDEFINED)
6833 break;
6835 else if (BUFFER_POS_REACHED_P ())
6837 buffer_pos_reached:
6838 it->current_x = x;
6839 it->max_ascent = ascent;
6840 it->max_descent = descent;
6841 result = MOVE_POS_MATCH_OR_ZV;
6842 break;
6844 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6846 /* Stop when TO_X specified and reached. This check is
6847 necessary here because of lines consisting of a line end,
6848 only. The line end will not produce any glyphs and we
6849 would never get MOVE_X_REACHED. */
6850 xassert (it->nglyphs == 0);
6851 result = MOVE_X_REACHED;
6852 break;
6855 /* Is this a line end? If yes, we're done. */
6856 if (ITERATOR_AT_END_OF_LINE_P (it))
6858 result = MOVE_NEWLINE_OR_CR;
6859 break;
6862 /* The current display element has been consumed. Advance
6863 to the next. */
6864 set_iterator_to_next (it, 1);
6866 /* Stop if lines are truncated and IT's current x-position is
6867 past the right edge of the window now. */
6868 if (it->truncate_lines_p
6869 && it->current_x >= it->last_visible_x)
6871 #ifdef HAVE_WINDOW_SYSTEM
6872 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6874 if (!get_next_display_element (it)
6875 || BUFFER_POS_REACHED_P ())
6877 result = MOVE_POS_MATCH_OR_ZV;
6878 break;
6880 if (ITERATOR_AT_END_OF_LINE_P (it))
6882 result = MOVE_NEWLINE_OR_CR;
6883 break;
6886 #endif /* HAVE_WINDOW_SYSTEM */
6887 result = MOVE_LINE_TRUNCATED;
6888 break;
6892 #undef BUFFER_POS_REACHED_P
6894 /* Restore the iterator settings altered at the beginning of this
6895 function. */
6896 it->glyph_row = saved_glyph_row;
6897 return result;
6901 /* Move IT forward until it satisfies one or more of the criteria in
6902 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6904 OP is a bit-mask that specifies where to stop, and in particular,
6905 which of those four position arguments makes a difference. See the
6906 description of enum move_operation_enum.
6908 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6909 screen line, this function will set IT to the next position >
6910 TO_CHARPOS. */
6912 void
6913 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6914 struct it *it;
6915 int to_charpos, to_x, to_y, to_vpos;
6916 int op;
6918 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6919 int line_height;
6920 int reached = 0;
6922 for (;;)
6924 if (op & MOVE_TO_VPOS)
6926 /* If no TO_CHARPOS and no TO_X specified, stop at the
6927 start of the line TO_VPOS. */
6928 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6930 if (it->vpos == to_vpos)
6932 reached = 1;
6933 break;
6935 else
6936 skip = move_it_in_display_line_to (it, -1, -1, 0);
6938 else
6940 /* TO_VPOS >= 0 means stop at TO_X in the line at
6941 TO_VPOS, or at TO_POS, whichever comes first. */
6942 if (it->vpos == to_vpos)
6944 reached = 2;
6945 break;
6948 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6950 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6952 reached = 3;
6953 break;
6955 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6957 /* We have reached TO_X but not in the line we want. */
6958 skip = move_it_in_display_line_to (it, to_charpos,
6959 -1, MOVE_TO_POS);
6960 if (skip == MOVE_POS_MATCH_OR_ZV)
6962 reached = 4;
6963 break;
6968 else if (op & MOVE_TO_Y)
6970 struct it it_backup;
6972 /* TO_Y specified means stop at TO_X in the line containing
6973 TO_Y---or at TO_CHARPOS if this is reached first. The
6974 problem is that we can't really tell whether the line
6975 contains TO_Y before we have completely scanned it, and
6976 this may skip past TO_X. What we do is to first scan to
6977 TO_X.
6979 If TO_X is not specified, use a TO_X of zero. The reason
6980 is to make the outcome of this function more predictable.
6981 If we didn't use TO_X == 0, we would stop at the end of
6982 the line which is probably not what a caller would expect
6983 to happen. */
6984 skip = move_it_in_display_line_to (it, to_charpos,
6985 ((op & MOVE_TO_X)
6986 ? to_x : 0),
6987 (MOVE_TO_X
6988 | (op & MOVE_TO_POS)));
6990 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6991 if (skip == MOVE_POS_MATCH_OR_ZV)
6993 reached = 5;
6994 break;
6997 /* If TO_X was reached, we would like to know whether TO_Y
6998 is in the line. This can only be said if we know the
6999 total line height which requires us to scan the rest of
7000 the line. */
7001 if (skip == MOVE_X_REACHED)
7003 /* Wait! We can conclude that TO_Y is in the line if
7004 the already scanned glyphs make the line tall enough
7005 because further scanning doesn't make it shorter. */
7006 line_height = it->max_ascent + it->max_descent;
7007 if (to_y >= it->current_y
7008 && to_y < it->current_y + line_height)
7010 reached = 6;
7011 break;
7013 it_backup = *it;
7014 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7015 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7016 op & MOVE_TO_POS);
7017 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7020 /* Now, decide whether TO_Y is in this line. */
7021 line_height = it->max_ascent + it->max_descent;
7022 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7024 if (to_y >= it->current_y
7025 && to_y < it->current_y + line_height)
7027 if (skip == MOVE_X_REACHED)
7028 /* If TO_Y is in this line and TO_X was reached above,
7029 we scanned too far. We have to restore IT's settings
7030 to the ones before skipping. */
7031 *it = it_backup;
7032 reached = 6;
7034 else if (skip == MOVE_X_REACHED)
7036 skip = skip2;
7037 if (skip == MOVE_POS_MATCH_OR_ZV)
7038 reached = 7;
7041 if (reached)
7042 break;
7044 else if (BUFFERP (it->object)
7045 && it->method == GET_FROM_BUFFER
7046 && IT_CHARPOS (*it) >= to_charpos)
7047 skip = MOVE_POS_MATCH_OR_ZV;
7048 else
7049 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7051 switch (skip)
7053 case MOVE_POS_MATCH_OR_ZV:
7054 reached = 8;
7055 goto out;
7057 case MOVE_NEWLINE_OR_CR:
7058 set_iterator_to_next (it, 1);
7059 it->continuation_lines_width = 0;
7060 break;
7062 case MOVE_LINE_TRUNCATED:
7063 it->continuation_lines_width = 0;
7064 reseat_at_next_visible_line_start (it, 0);
7065 if ((op & MOVE_TO_POS) != 0
7066 && IT_CHARPOS (*it) > to_charpos)
7068 reached = 9;
7069 goto out;
7071 break;
7073 case MOVE_LINE_CONTINUED:
7074 /* For continued lines ending in a tab, some of the glyphs
7075 associated with the tab are displayed on the current
7076 line. Since it->current_x does not include these glyphs,
7077 we use it->last_visible_x instead. */
7078 it->continuation_lines_width +=
7079 (it->c == '\t') ? it->last_visible_x : it->current_x;
7080 break;
7082 default:
7083 abort ();
7086 /* Reset/increment for the next run. */
7087 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7088 it->current_x = it->hpos = 0;
7089 it->current_y += it->max_ascent + it->max_descent;
7090 ++it->vpos;
7091 last_height = it->max_ascent + it->max_descent;
7092 last_max_ascent = it->max_ascent;
7093 it->max_ascent = it->max_descent = 0;
7096 out:
7098 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7102 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7104 If DY > 0, move IT backward at least that many pixels. DY = 0
7105 means move IT backward to the preceding line start or BEGV. This
7106 function may move over more than DY pixels if IT->current_y - DY
7107 ends up in the middle of a line; in this case IT->current_y will be
7108 set to the top of the line moved to. */
7110 void
7111 move_it_vertically_backward (it, dy)
7112 struct it *it;
7113 int dy;
7115 int nlines, h;
7116 struct it it2, it3;
7117 int start_pos;
7119 move_further_back:
7120 xassert (dy >= 0);
7122 start_pos = IT_CHARPOS (*it);
7124 /* Estimate how many newlines we must move back. */
7125 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7127 /* Set the iterator's position that many lines back. */
7128 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7129 back_to_previous_visible_line_start (it);
7131 /* Reseat the iterator here. When moving backward, we don't want
7132 reseat to skip forward over invisible text, set up the iterator
7133 to deliver from overlay strings at the new position etc. So,
7134 use reseat_1 here. */
7135 reseat_1 (it, it->current.pos, 1);
7137 /* We are now surely at a line start. */
7138 it->current_x = it->hpos = 0;
7139 it->continuation_lines_width = 0;
7141 /* Move forward and see what y-distance we moved. First move to the
7142 start of the next line so that we get its height. We need this
7143 height to be able to tell whether we reached the specified
7144 y-distance. */
7145 it2 = *it;
7146 it2.max_ascent = it2.max_descent = 0;
7149 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7150 MOVE_TO_POS | MOVE_TO_VPOS);
7152 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7153 xassert (IT_CHARPOS (*it) >= BEGV);
7154 it3 = it2;
7156 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7157 xassert (IT_CHARPOS (*it) >= BEGV);
7158 /* H is the actual vertical distance from the position in *IT
7159 and the starting position. */
7160 h = it2.current_y - it->current_y;
7161 /* NLINES is the distance in number of lines. */
7162 nlines = it2.vpos - it->vpos;
7164 /* Correct IT's y and vpos position
7165 so that they are relative to the starting point. */
7166 it->vpos -= nlines;
7167 it->current_y -= h;
7169 if (dy == 0)
7171 /* DY == 0 means move to the start of the screen line. The
7172 value of nlines is > 0 if continuation lines were involved. */
7173 if (nlines > 0)
7174 move_it_by_lines (it, nlines, 1);
7175 #if 0
7176 /* I think this assert is bogus if buffer contains
7177 invisible text or images. KFS. */
7178 xassert (IT_CHARPOS (*it) <= start_pos);
7179 #endif
7181 else
7183 /* The y-position we try to reach, relative to *IT.
7184 Note that H has been subtracted in front of the if-statement. */
7185 int target_y = it->current_y + h - dy;
7186 int y0 = it3.current_y;
7187 int y1 = line_bottom_y (&it3);
7188 int line_height = y1 - y0;
7190 /* If we did not reach target_y, try to move further backward if
7191 we can. If we moved too far backward, try to move forward. */
7192 if (target_y < it->current_y
7193 /* This is heuristic. In a window that's 3 lines high, with
7194 a line height of 13 pixels each, recentering with point
7195 on the bottom line will try to move -39/2 = 19 pixels
7196 backward. Try to avoid moving into the first line. */
7197 && (it->current_y - target_y
7198 > min (window_box_height (it->w), line_height * 2 / 3))
7199 && IT_CHARPOS (*it) > BEGV)
7201 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7202 target_y - it->current_y));
7203 dy = it->current_y - target_y;
7204 goto move_further_back;
7206 else if (target_y >= it->current_y + line_height
7207 && IT_CHARPOS (*it) < ZV)
7209 /* Should move forward by at least one line, maybe more.
7211 Note: Calling move_it_by_lines can be expensive on
7212 terminal frames, where compute_motion is used (via
7213 vmotion) to do the job, when there are very long lines
7214 and truncate-lines is nil. That's the reason for
7215 treating terminal frames specially here. */
7217 if (!FRAME_WINDOW_P (it->f))
7218 move_it_vertically (it, target_y - (it->current_y + line_height));
7219 else
7223 move_it_by_lines (it, 1, 1);
7225 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7228 #if 0
7229 /* I think this assert is bogus if buffer contains
7230 invisible text or images. KFS. */
7231 xassert (IT_CHARPOS (*it) >= BEGV);
7232 #endif
7238 /* Move IT by a specified amount of pixel lines DY. DY negative means
7239 move backwards. DY = 0 means move to start of screen line. At the
7240 end, IT will be on the start of a screen line. */
7242 void
7243 move_it_vertically (it, dy)
7244 struct it *it;
7245 int dy;
7247 if (dy <= 0)
7248 move_it_vertically_backward (it, -dy);
7249 else
7251 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7252 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7253 MOVE_TO_POS | MOVE_TO_Y);
7254 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7256 /* If buffer ends in ZV without a newline, move to the start of
7257 the line to satisfy the post-condition. */
7258 if (IT_CHARPOS (*it) == ZV
7259 && ZV > BEGV
7260 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7261 move_it_by_lines (it, 0, 0);
7266 /* Move iterator IT past the end of the text line it is in. */
7268 void
7269 move_it_past_eol (it)
7270 struct it *it;
7272 enum move_it_result rc;
7274 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7275 if (rc == MOVE_NEWLINE_OR_CR)
7276 set_iterator_to_next (it, 0);
7280 #if 0 /* Currently not used. */
7282 /* Return non-zero if some text between buffer positions START_CHARPOS
7283 and END_CHARPOS is invisible. IT->window is the window for text
7284 property lookup. */
7286 static int
7287 invisible_text_between_p (it, start_charpos, end_charpos)
7288 struct it *it;
7289 int start_charpos, end_charpos;
7291 Lisp_Object prop, limit;
7292 int invisible_found_p;
7294 xassert (it != NULL && start_charpos <= end_charpos);
7296 /* Is text at START invisible? */
7297 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7298 it->window);
7299 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7300 invisible_found_p = 1;
7301 else
7303 limit = Fnext_single_char_property_change (make_number (start_charpos),
7304 Qinvisible, Qnil,
7305 make_number (end_charpos));
7306 invisible_found_p = XFASTINT (limit) < end_charpos;
7309 return invisible_found_p;
7312 #endif /* 0 */
7315 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7316 negative means move up. DVPOS == 0 means move to the start of the
7317 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7318 NEED_Y_P is zero, IT->current_y will be left unchanged.
7320 Further optimization ideas: If we would know that IT->f doesn't use
7321 a face with proportional font, we could be faster for
7322 truncate-lines nil. */
7324 void
7325 move_it_by_lines (it, dvpos, need_y_p)
7326 struct it *it;
7327 int dvpos, need_y_p;
7329 struct position pos;
7331 /* The commented-out optimization uses vmotion on terminals. This
7332 gives bad results, because elements like it->what, on which
7333 callers such as pos_visible_p rely, aren't updated. */
7334 /* if (!FRAME_WINDOW_P (it->f))
7336 struct text_pos textpos;
7338 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7339 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7340 reseat (it, textpos, 1);
7341 it->vpos += pos.vpos;
7342 it->current_y += pos.vpos;
7344 else */
7346 if (dvpos == 0)
7348 /* DVPOS == 0 means move to the start of the screen line. */
7349 move_it_vertically_backward (it, 0);
7350 xassert (it->current_x == 0 && it->hpos == 0);
7351 /* Let next call to line_bottom_y calculate real line height */
7352 last_height = 0;
7354 else if (dvpos > 0)
7356 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7357 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7358 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7360 else
7362 struct it it2;
7363 int start_charpos, i;
7365 /* Start at the beginning of the screen line containing IT's
7366 position. This may actually move vertically backwards,
7367 in case of overlays, so adjust dvpos accordingly. */
7368 dvpos += it->vpos;
7369 move_it_vertically_backward (it, 0);
7370 dvpos -= it->vpos;
7372 /* Go back -DVPOS visible lines and reseat the iterator there. */
7373 start_charpos = IT_CHARPOS (*it);
7374 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7375 back_to_previous_visible_line_start (it);
7376 reseat (it, it->current.pos, 1);
7378 /* Move further back if we end up in a string or an image. */
7379 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7381 /* First try to move to start of display line. */
7382 dvpos += it->vpos;
7383 move_it_vertically_backward (it, 0);
7384 dvpos -= it->vpos;
7385 if (IT_POS_VALID_AFTER_MOVE_P (it))
7386 break;
7387 /* If start of line is still in string or image,
7388 move further back. */
7389 back_to_previous_visible_line_start (it);
7390 reseat (it, it->current.pos, 1);
7391 dvpos--;
7394 it->current_x = it->hpos = 0;
7396 /* Above call may have moved too far if continuation lines
7397 are involved. Scan forward and see if it did. */
7398 it2 = *it;
7399 it2.vpos = it2.current_y = 0;
7400 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7401 it->vpos -= it2.vpos;
7402 it->current_y -= it2.current_y;
7403 it->current_x = it->hpos = 0;
7405 /* If we moved too far back, move IT some lines forward. */
7406 if (it2.vpos > -dvpos)
7408 int delta = it2.vpos + dvpos;
7409 it2 = *it;
7410 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7411 /* Move back again if we got too far ahead. */
7412 if (IT_CHARPOS (*it) >= start_charpos)
7413 *it = it2;
7418 /* Return 1 if IT points into the middle of a display vector. */
7421 in_display_vector_p (it)
7422 struct it *it;
7424 return (it->method == GET_FROM_DISPLAY_VECTOR
7425 && it->current.dpvec_index > 0
7426 && it->dpvec + it->current.dpvec_index != it->dpend);
7430 /***********************************************************************
7431 Messages
7432 ***********************************************************************/
7435 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7436 to *Messages*. */
7438 void
7439 add_to_log (format, arg1, arg2)
7440 char *format;
7441 Lisp_Object arg1, arg2;
7443 Lisp_Object args[3];
7444 Lisp_Object msg, fmt;
7445 char *buffer;
7446 int len;
7447 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7448 USE_SAFE_ALLOCA;
7450 /* Do nothing if called asynchronously. Inserting text into
7451 a buffer may call after-change-functions and alike and
7452 that would means running Lisp asynchronously. */
7453 if (handling_signal)
7454 return;
7456 fmt = msg = Qnil;
7457 GCPRO4 (fmt, msg, arg1, arg2);
7459 args[0] = fmt = build_string (format);
7460 args[1] = arg1;
7461 args[2] = arg2;
7462 msg = Fformat (3, args);
7464 len = SBYTES (msg) + 1;
7465 SAFE_ALLOCA (buffer, char *, len);
7466 bcopy (SDATA (msg), buffer, len);
7468 message_dolog (buffer, len - 1, 1, 0);
7469 SAFE_FREE ();
7471 UNGCPRO;
7475 /* Output a newline in the *Messages* buffer if "needs" one. */
7477 void
7478 message_log_maybe_newline ()
7480 if (message_log_need_newline)
7481 message_dolog ("", 0, 1, 0);
7485 /* Add a string M of length NBYTES to the message log, optionally
7486 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7487 nonzero, means interpret the contents of M as multibyte. This
7488 function calls low-level routines in order to bypass text property
7489 hooks, etc. which might not be safe to run.
7491 This may GC (insert may run before/after change hooks),
7492 so the buffer M must NOT point to a Lisp string. */
7494 void
7495 message_dolog (m, nbytes, nlflag, multibyte)
7496 const char *m;
7497 int nbytes, nlflag, multibyte;
7499 if (!NILP (Vmemory_full))
7500 return;
7502 if (!NILP (Vmessage_log_max))
7504 struct buffer *oldbuf;
7505 Lisp_Object oldpoint, oldbegv, oldzv;
7506 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7507 int point_at_end = 0;
7508 int zv_at_end = 0;
7509 Lisp_Object old_deactivate_mark, tem;
7510 struct gcpro gcpro1;
7512 old_deactivate_mark = Vdeactivate_mark;
7513 oldbuf = current_buffer;
7514 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7515 current_buffer->undo_list = Qt;
7517 oldpoint = message_dolog_marker1;
7518 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7519 oldbegv = message_dolog_marker2;
7520 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7521 oldzv = message_dolog_marker3;
7522 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7523 GCPRO1 (old_deactivate_mark);
7525 if (PT == Z)
7526 point_at_end = 1;
7527 if (ZV == Z)
7528 zv_at_end = 1;
7530 BEGV = BEG;
7531 BEGV_BYTE = BEG_BYTE;
7532 ZV = Z;
7533 ZV_BYTE = Z_BYTE;
7534 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7536 /* Insert the string--maybe converting multibyte to single byte
7537 or vice versa, so that all the text fits the buffer. */
7538 if (multibyte
7539 && NILP (current_buffer->enable_multibyte_characters))
7541 int i, c, char_bytes;
7542 unsigned char work[1];
7544 /* Convert a multibyte string to single-byte
7545 for the *Message* buffer. */
7546 for (i = 0; i < nbytes; i += char_bytes)
7548 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7549 work[0] = (ASCII_CHAR_P (c)
7551 : multibyte_char_to_unibyte (c, Qnil));
7552 insert_1_both (work, 1, 1, 1, 0, 0);
7555 else if (! multibyte
7556 && ! NILP (current_buffer->enable_multibyte_characters))
7558 int i, c, char_bytes;
7559 unsigned char *msg = (unsigned char *) m;
7560 unsigned char str[MAX_MULTIBYTE_LENGTH];
7561 /* Convert a single-byte string to multibyte
7562 for the *Message* buffer. */
7563 for (i = 0; i < nbytes; i++)
7565 c = msg[i];
7566 c = unibyte_char_to_multibyte (c);
7567 char_bytes = CHAR_STRING (c, str);
7568 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7571 else if (nbytes)
7572 insert_1 (m, nbytes, 1, 0, 0);
7574 if (nlflag)
7576 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7577 insert_1 ("\n", 1, 1, 0, 0);
7579 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7580 this_bol = PT;
7581 this_bol_byte = PT_BYTE;
7583 /* See if this line duplicates the previous one.
7584 If so, combine duplicates. */
7585 if (this_bol > BEG)
7587 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7588 prev_bol = PT;
7589 prev_bol_byte = PT_BYTE;
7591 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7592 this_bol, this_bol_byte);
7593 if (dup)
7595 del_range_both (prev_bol, prev_bol_byte,
7596 this_bol, this_bol_byte, 0);
7597 if (dup > 1)
7599 char dupstr[40];
7600 int duplen;
7602 /* If you change this format, don't forget to also
7603 change message_log_check_duplicate. */
7604 sprintf (dupstr, " [%d times]", dup);
7605 duplen = strlen (dupstr);
7606 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7607 insert_1 (dupstr, duplen, 1, 0, 1);
7612 /* If we have more than the desired maximum number of lines
7613 in the *Messages* buffer now, delete the oldest ones.
7614 This is safe because we don't have undo in this buffer. */
7616 if (NATNUMP (Vmessage_log_max))
7618 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7619 -XFASTINT (Vmessage_log_max) - 1, 0);
7620 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7623 BEGV = XMARKER (oldbegv)->charpos;
7624 BEGV_BYTE = marker_byte_position (oldbegv);
7626 if (zv_at_end)
7628 ZV = Z;
7629 ZV_BYTE = Z_BYTE;
7631 else
7633 ZV = XMARKER (oldzv)->charpos;
7634 ZV_BYTE = marker_byte_position (oldzv);
7637 if (point_at_end)
7638 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7639 else
7640 /* We can't do Fgoto_char (oldpoint) because it will run some
7641 Lisp code. */
7642 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7643 XMARKER (oldpoint)->bytepos);
7645 UNGCPRO;
7646 unchain_marker (XMARKER (oldpoint));
7647 unchain_marker (XMARKER (oldbegv));
7648 unchain_marker (XMARKER (oldzv));
7650 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7651 set_buffer_internal (oldbuf);
7652 if (NILP (tem))
7653 windows_or_buffers_changed = old_windows_or_buffers_changed;
7654 message_log_need_newline = !nlflag;
7655 Vdeactivate_mark = old_deactivate_mark;
7660 /* We are at the end of the buffer after just having inserted a newline.
7661 (Note: We depend on the fact we won't be crossing the gap.)
7662 Check to see if the most recent message looks a lot like the previous one.
7663 Return 0 if different, 1 if the new one should just replace it, or a
7664 value N > 1 if we should also append " [N times]". */
7666 static int
7667 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7668 int prev_bol, this_bol;
7669 int prev_bol_byte, this_bol_byte;
7671 int i;
7672 int len = Z_BYTE - 1 - this_bol_byte;
7673 int seen_dots = 0;
7674 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7675 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7677 for (i = 0; i < len; i++)
7679 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7680 seen_dots = 1;
7681 if (p1[i] != p2[i])
7682 return seen_dots;
7684 p1 += len;
7685 if (*p1 == '\n')
7686 return 2;
7687 if (*p1++ == ' ' && *p1++ == '[')
7689 int n = 0;
7690 while (*p1 >= '0' && *p1 <= '9')
7691 n = n * 10 + *p1++ - '0';
7692 if (strncmp (p1, " times]\n", 8) == 0)
7693 return n+1;
7695 return 0;
7699 /* Display an echo area message M with a specified length of NBYTES
7700 bytes. The string may include null characters. If M is 0, clear
7701 out any existing message, and let the mini-buffer text show
7702 through.
7704 This may GC, so the buffer M must NOT point to a Lisp string. */
7706 void
7707 message2 (m, nbytes, multibyte)
7708 const char *m;
7709 int nbytes;
7710 int multibyte;
7712 /* First flush out any partial line written with print. */
7713 message_log_maybe_newline ();
7714 if (m)
7715 message_dolog (m, nbytes, 1, multibyte);
7716 message2_nolog (m, nbytes, multibyte);
7720 /* The non-logging counterpart of message2. */
7722 void
7723 message2_nolog (m, nbytes, multibyte)
7724 const char *m;
7725 int nbytes, multibyte;
7727 struct frame *sf = SELECTED_FRAME ();
7728 message_enable_multibyte = multibyte;
7730 if (noninteractive)
7732 if (noninteractive_need_newline)
7733 putc ('\n', stderr);
7734 noninteractive_need_newline = 0;
7735 if (m)
7736 fwrite (m, nbytes, 1, stderr);
7737 if (cursor_in_echo_area == 0)
7738 fprintf (stderr, "\n");
7739 fflush (stderr);
7741 /* A null message buffer means that the frame hasn't really been
7742 initialized yet. Error messages get reported properly by
7743 cmd_error, so this must be just an informative message; toss it. */
7744 else if (INTERACTIVE
7745 && sf->glyphs_initialized_p
7746 && FRAME_MESSAGE_BUF (sf))
7748 Lisp_Object mini_window;
7749 struct frame *f;
7751 /* Get the frame containing the mini-buffer
7752 that the selected frame is using. */
7753 mini_window = FRAME_MINIBUF_WINDOW (sf);
7754 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7756 FRAME_SAMPLE_VISIBILITY (f);
7757 if (FRAME_VISIBLE_P (sf)
7758 && ! FRAME_VISIBLE_P (f))
7759 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7761 if (m)
7763 set_message (m, Qnil, nbytes, multibyte);
7764 if (minibuffer_auto_raise)
7765 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7767 else
7768 clear_message (1, 1);
7770 do_pending_window_change (0);
7771 echo_area_display (1);
7772 do_pending_window_change (0);
7773 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7774 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7779 /* Display an echo area message M with a specified length of NBYTES
7780 bytes. The string may include null characters. If M is not a
7781 string, clear out any existing message, and let the mini-buffer
7782 text show through.
7784 This function cancels echoing. */
7786 void
7787 message3 (m, nbytes, multibyte)
7788 Lisp_Object m;
7789 int nbytes;
7790 int multibyte;
7792 struct gcpro gcpro1;
7794 GCPRO1 (m);
7795 clear_message (1,1);
7796 cancel_echoing ();
7798 /* First flush out any partial line written with print. */
7799 message_log_maybe_newline ();
7800 if (STRINGP (m))
7802 char *buffer;
7803 USE_SAFE_ALLOCA;
7805 SAFE_ALLOCA (buffer, char *, nbytes);
7806 bcopy (SDATA (m), buffer, nbytes);
7807 message_dolog (buffer, nbytes, 1, multibyte);
7808 SAFE_FREE ();
7810 message3_nolog (m, nbytes, multibyte);
7812 UNGCPRO;
7816 /* The non-logging version of message3.
7817 This does not cancel echoing, because it is used for echoing.
7818 Perhaps we need to make a separate function for echoing
7819 and make this cancel echoing. */
7821 void
7822 message3_nolog (m, nbytes, multibyte)
7823 Lisp_Object m;
7824 int nbytes, multibyte;
7826 struct frame *sf = SELECTED_FRAME ();
7827 message_enable_multibyte = multibyte;
7829 if (noninteractive)
7831 if (noninteractive_need_newline)
7832 putc ('\n', stderr);
7833 noninteractive_need_newline = 0;
7834 if (STRINGP (m))
7835 fwrite (SDATA (m), nbytes, 1, stderr);
7836 if (cursor_in_echo_area == 0)
7837 fprintf (stderr, "\n");
7838 fflush (stderr);
7840 /* A null message buffer means that the frame hasn't really been
7841 initialized yet. Error messages get reported properly by
7842 cmd_error, so this must be just an informative message; toss it. */
7843 else if (INTERACTIVE
7844 && sf->glyphs_initialized_p
7845 && FRAME_MESSAGE_BUF (sf))
7847 Lisp_Object mini_window;
7848 Lisp_Object frame;
7849 struct frame *f;
7851 /* Get the frame containing the mini-buffer
7852 that the selected frame is using. */
7853 mini_window = FRAME_MINIBUF_WINDOW (sf);
7854 frame = XWINDOW (mini_window)->frame;
7855 f = XFRAME (frame);
7857 FRAME_SAMPLE_VISIBILITY (f);
7858 if (FRAME_VISIBLE_P (sf)
7859 && !FRAME_VISIBLE_P (f))
7860 Fmake_frame_visible (frame);
7862 if (STRINGP (m) && SCHARS (m) > 0)
7864 set_message (NULL, m, nbytes, multibyte);
7865 if (minibuffer_auto_raise)
7866 Fraise_frame (frame);
7867 /* Assume we are not echoing.
7868 (If we are, echo_now will override this.) */
7869 echo_message_buffer = Qnil;
7871 else
7872 clear_message (1, 1);
7874 do_pending_window_change (0);
7875 echo_area_display (1);
7876 do_pending_window_change (0);
7877 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7878 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7883 /* Display a null-terminated echo area message M. If M is 0, clear
7884 out any existing message, and let the mini-buffer text show through.
7886 The buffer M must continue to exist until after the echo area gets
7887 cleared or some other message gets displayed there. Do not pass
7888 text that is stored in a Lisp string. Do not pass text in a buffer
7889 that was alloca'd. */
7891 void
7892 message1 (m)
7893 char *m;
7895 message2 (m, (m ? strlen (m) : 0), 0);
7899 /* The non-logging counterpart of message1. */
7901 void
7902 message1_nolog (m)
7903 char *m;
7905 message2_nolog (m, (m ? strlen (m) : 0), 0);
7908 /* Display a message M which contains a single %s
7909 which gets replaced with STRING. */
7911 void
7912 message_with_string (m, string, log)
7913 char *m;
7914 Lisp_Object string;
7915 int log;
7917 CHECK_STRING (string);
7919 if (noninteractive)
7921 if (m)
7923 if (noninteractive_need_newline)
7924 putc ('\n', stderr);
7925 noninteractive_need_newline = 0;
7926 fprintf (stderr, m, SDATA (string));
7927 if (cursor_in_echo_area == 0)
7928 fprintf (stderr, "\n");
7929 fflush (stderr);
7932 else if (INTERACTIVE)
7934 /* The frame whose minibuffer we're going to display the message on.
7935 It may be larger than the selected frame, so we need
7936 to use its buffer, not the selected frame's buffer. */
7937 Lisp_Object mini_window;
7938 struct frame *f, *sf = SELECTED_FRAME ();
7940 /* Get the frame containing the minibuffer
7941 that the selected frame is using. */
7942 mini_window = FRAME_MINIBUF_WINDOW (sf);
7943 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7945 /* A null message buffer means that the frame hasn't really been
7946 initialized yet. Error messages get reported properly by
7947 cmd_error, so this must be just an informative message; toss it. */
7948 if (FRAME_MESSAGE_BUF (f))
7950 Lisp_Object args[2], message;
7951 struct gcpro gcpro1, gcpro2;
7953 args[0] = build_string (m);
7954 args[1] = message = string;
7955 GCPRO2 (args[0], message);
7956 gcpro1.nvars = 2;
7958 message = Fformat (2, args);
7960 if (log)
7961 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7962 else
7963 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7965 UNGCPRO;
7967 /* Print should start at the beginning of the message
7968 buffer next time. */
7969 message_buf_print = 0;
7975 /* Dump an informative message to the minibuf. If M is 0, clear out
7976 any existing message, and let the mini-buffer text show through. */
7978 /* VARARGS 1 */
7979 void
7980 message (m, a1, a2, a3)
7981 char *m;
7982 EMACS_INT a1, a2, a3;
7984 if (noninteractive)
7986 if (m)
7988 if (noninteractive_need_newline)
7989 putc ('\n', stderr);
7990 noninteractive_need_newline = 0;
7991 fprintf (stderr, m, a1, a2, a3);
7992 if (cursor_in_echo_area == 0)
7993 fprintf (stderr, "\n");
7994 fflush (stderr);
7997 else if (INTERACTIVE)
7999 /* The frame whose mini-buffer we're going to display the message
8000 on. It may be larger than the selected frame, so we need to
8001 use its buffer, not the selected frame's buffer. */
8002 Lisp_Object mini_window;
8003 struct frame *f, *sf = SELECTED_FRAME ();
8005 /* Get the frame containing the mini-buffer
8006 that the selected frame is using. */
8007 mini_window = FRAME_MINIBUF_WINDOW (sf);
8008 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8010 /* A null message buffer means that the frame hasn't really been
8011 initialized yet. Error messages get reported properly by
8012 cmd_error, so this must be just an informative message; toss
8013 it. */
8014 if (FRAME_MESSAGE_BUF (f))
8016 if (m)
8018 int len;
8019 #ifdef NO_ARG_ARRAY
8020 char *a[3];
8021 a[0] = (char *) a1;
8022 a[1] = (char *) a2;
8023 a[2] = (char *) a3;
8025 len = doprnt (FRAME_MESSAGE_BUF (f),
8026 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8027 #else
8028 len = doprnt (FRAME_MESSAGE_BUF (f),
8029 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8030 (char **) &a1);
8031 #endif /* NO_ARG_ARRAY */
8033 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8035 else
8036 message1 (0);
8038 /* Print should start at the beginning of the message
8039 buffer next time. */
8040 message_buf_print = 0;
8046 /* The non-logging version of message. */
8048 void
8049 message_nolog (m, a1, a2, a3)
8050 char *m;
8051 EMACS_INT a1, a2, a3;
8053 Lisp_Object old_log_max;
8054 old_log_max = Vmessage_log_max;
8055 Vmessage_log_max = Qnil;
8056 message (m, a1, a2, a3);
8057 Vmessage_log_max = old_log_max;
8061 /* Display the current message in the current mini-buffer. This is
8062 only called from error handlers in process.c, and is not time
8063 critical. */
8065 void
8066 update_echo_area ()
8068 if (!NILP (echo_area_buffer[0]))
8070 Lisp_Object string;
8071 string = Fcurrent_message ();
8072 message3 (string, SBYTES (string),
8073 !NILP (current_buffer->enable_multibyte_characters));
8078 /* Make sure echo area buffers in `echo_buffers' are live.
8079 If they aren't, make new ones. */
8081 static void
8082 ensure_echo_area_buffers ()
8084 int i;
8086 for (i = 0; i < 2; ++i)
8087 if (!BUFFERP (echo_buffer[i])
8088 || NILP (XBUFFER (echo_buffer[i])->name))
8090 char name[30];
8091 Lisp_Object old_buffer;
8092 int j;
8094 old_buffer = echo_buffer[i];
8095 sprintf (name, " *Echo Area %d*", i);
8096 echo_buffer[i] = Fget_buffer_create (build_string (name));
8097 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8099 for (j = 0; j < 2; ++j)
8100 if (EQ (old_buffer, echo_area_buffer[j]))
8101 echo_area_buffer[j] = echo_buffer[i];
8106 /* Call FN with args A1..A4 with either the current or last displayed
8107 echo_area_buffer as current buffer.
8109 WHICH zero means use the current message buffer
8110 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8111 from echo_buffer[] and clear it.
8113 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8114 suitable buffer from echo_buffer[] and clear it.
8116 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8117 that the current message becomes the last displayed one, make
8118 choose a suitable buffer for echo_area_buffer[0], and clear it.
8120 Value is what FN returns. */
8122 static int
8123 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8124 struct window *w;
8125 int which;
8126 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8127 EMACS_INT a1;
8128 Lisp_Object a2;
8129 EMACS_INT a3, a4;
8131 Lisp_Object buffer;
8132 int this_one, the_other, clear_buffer_p, rc;
8133 int count = SPECPDL_INDEX ();
8135 /* If buffers aren't live, make new ones. */
8136 ensure_echo_area_buffers ();
8138 clear_buffer_p = 0;
8140 if (which == 0)
8141 this_one = 0, the_other = 1;
8142 else if (which > 0)
8143 this_one = 1, the_other = 0;
8144 else
8146 this_one = 0, the_other = 1;
8147 clear_buffer_p = 1;
8149 /* We need a fresh one in case the current echo buffer equals
8150 the one containing the last displayed echo area message. */
8151 if (!NILP (echo_area_buffer[this_one])
8152 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8153 echo_area_buffer[this_one] = Qnil;
8156 /* Choose a suitable buffer from echo_buffer[] is we don't
8157 have one. */
8158 if (NILP (echo_area_buffer[this_one]))
8160 echo_area_buffer[this_one]
8161 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8162 ? echo_buffer[the_other]
8163 : echo_buffer[this_one]);
8164 clear_buffer_p = 1;
8167 buffer = echo_area_buffer[this_one];
8169 /* Don't get confused by reusing the buffer used for echoing
8170 for a different purpose. */
8171 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8172 cancel_echoing ();
8174 record_unwind_protect (unwind_with_echo_area_buffer,
8175 with_echo_area_buffer_unwind_data (w));
8177 /* Make the echo area buffer current. Note that for display
8178 purposes, it is not necessary that the displayed window's buffer
8179 == current_buffer, except for text property lookup. So, let's
8180 only set that buffer temporarily here without doing a full
8181 Fset_window_buffer. We must also change w->pointm, though,
8182 because otherwise an assertions in unshow_buffer fails, and Emacs
8183 aborts. */
8184 set_buffer_internal_1 (XBUFFER (buffer));
8185 if (w)
8187 w->buffer = buffer;
8188 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8191 current_buffer->undo_list = Qt;
8192 current_buffer->read_only = Qnil;
8193 specbind (Qinhibit_read_only, Qt);
8194 specbind (Qinhibit_modification_hooks, Qt);
8196 if (clear_buffer_p && Z > BEG)
8197 del_range (BEG, Z);
8199 xassert (BEGV >= BEG);
8200 xassert (ZV <= Z && ZV >= BEGV);
8202 rc = fn (a1, a2, a3, a4);
8204 xassert (BEGV >= BEG);
8205 xassert (ZV <= Z && ZV >= BEGV);
8207 unbind_to (count, Qnil);
8208 return rc;
8212 /* Save state that should be preserved around the call to the function
8213 FN called in with_echo_area_buffer. */
8215 static Lisp_Object
8216 with_echo_area_buffer_unwind_data (w)
8217 struct window *w;
8219 int i = 0;
8220 Lisp_Object vector, tmp;
8222 /* Reduce consing by keeping one vector in
8223 Vwith_echo_area_save_vector. */
8224 vector = Vwith_echo_area_save_vector;
8225 Vwith_echo_area_save_vector = Qnil;
8227 if (NILP (vector))
8228 vector = Fmake_vector (make_number (7), Qnil);
8230 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8231 ASET (vector, i, Vdeactivate_mark); ++i;
8232 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8234 if (w)
8236 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8237 ASET (vector, i, w->buffer); ++i;
8238 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8239 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8241 else
8243 int end = i + 4;
8244 for (; i < end; ++i)
8245 ASET (vector, i, Qnil);
8248 xassert (i == ASIZE (vector));
8249 return vector;
8253 /* Restore global state from VECTOR which was created by
8254 with_echo_area_buffer_unwind_data. */
8256 static Lisp_Object
8257 unwind_with_echo_area_buffer (vector)
8258 Lisp_Object vector;
8260 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8261 Vdeactivate_mark = AREF (vector, 1);
8262 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8264 if (WINDOWP (AREF (vector, 3)))
8266 struct window *w;
8267 Lisp_Object buffer, charpos, bytepos;
8269 w = XWINDOW (AREF (vector, 3));
8270 buffer = AREF (vector, 4);
8271 charpos = AREF (vector, 5);
8272 bytepos = AREF (vector, 6);
8274 w->buffer = buffer;
8275 set_marker_both (w->pointm, buffer,
8276 XFASTINT (charpos), XFASTINT (bytepos));
8279 Vwith_echo_area_save_vector = vector;
8280 return Qnil;
8284 /* Set up the echo area for use by print functions. MULTIBYTE_P
8285 non-zero means we will print multibyte. */
8287 void
8288 setup_echo_area_for_printing (multibyte_p)
8289 int multibyte_p;
8291 /* If we can't find an echo area any more, exit. */
8292 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8293 Fkill_emacs (Qnil);
8295 ensure_echo_area_buffers ();
8297 if (!message_buf_print)
8299 /* A message has been output since the last time we printed.
8300 Choose a fresh echo area buffer. */
8301 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8302 echo_area_buffer[0] = echo_buffer[1];
8303 else
8304 echo_area_buffer[0] = echo_buffer[0];
8306 /* Switch to that buffer and clear it. */
8307 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8308 current_buffer->truncate_lines = Qnil;
8310 if (Z > BEG)
8312 int count = SPECPDL_INDEX ();
8313 specbind (Qinhibit_read_only, Qt);
8314 /* Note that undo recording is always disabled. */
8315 del_range (BEG, Z);
8316 unbind_to (count, Qnil);
8318 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8320 /* Set up the buffer for the multibyteness we need. */
8321 if (multibyte_p
8322 != !NILP (current_buffer->enable_multibyte_characters))
8323 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8325 /* Raise the frame containing the echo area. */
8326 if (minibuffer_auto_raise)
8328 struct frame *sf = SELECTED_FRAME ();
8329 Lisp_Object mini_window;
8330 mini_window = FRAME_MINIBUF_WINDOW (sf);
8331 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8334 message_log_maybe_newline ();
8335 message_buf_print = 1;
8337 else
8339 if (NILP (echo_area_buffer[0]))
8341 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8342 echo_area_buffer[0] = echo_buffer[1];
8343 else
8344 echo_area_buffer[0] = echo_buffer[0];
8347 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8349 /* Someone switched buffers between print requests. */
8350 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8351 current_buffer->truncate_lines = Qnil;
8357 /* Display an echo area message in window W. Value is non-zero if W's
8358 height is changed. If display_last_displayed_message_p is
8359 non-zero, display the message that was last displayed, otherwise
8360 display the current message. */
8362 static int
8363 display_echo_area (w)
8364 struct window *w;
8366 int i, no_message_p, window_height_changed_p, count;
8368 /* Temporarily disable garbage collections while displaying the echo
8369 area. This is done because a GC can print a message itself.
8370 That message would modify the echo area buffer's contents while a
8371 redisplay of the buffer is going on, and seriously confuse
8372 redisplay. */
8373 count = inhibit_garbage_collection ();
8375 /* If there is no message, we must call display_echo_area_1
8376 nevertheless because it resizes the window. But we will have to
8377 reset the echo_area_buffer in question to nil at the end because
8378 with_echo_area_buffer will sets it to an empty buffer. */
8379 i = display_last_displayed_message_p ? 1 : 0;
8380 no_message_p = NILP (echo_area_buffer[i]);
8382 window_height_changed_p
8383 = with_echo_area_buffer (w, display_last_displayed_message_p,
8384 display_echo_area_1,
8385 (EMACS_INT) w, Qnil, 0, 0);
8387 if (no_message_p)
8388 echo_area_buffer[i] = Qnil;
8390 unbind_to (count, Qnil);
8391 return window_height_changed_p;
8395 /* Helper for display_echo_area. Display the current buffer which
8396 contains the current echo area message in window W, a mini-window,
8397 a pointer to which is passed in A1. A2..A4 are currently not used.
8398 Change the height of W so that all of the message is displayed.
8399 Value is non-zero if height of W was changed. */
8401 static int
8402 display_echo_area_1 (a1, a2, a3, a4)
8403 EMACS_INT a1;
8404 Lisp_Object a2;
8405 EMACS_INT a3, a4;
8407 struct window *w = (struct window *) a1;
8408 Lisp_Object window;
8409 struct text_pos start;
8410 int window_height_changed_p = 0;
8412 /* Do this before displaying, so that we have a large enough glyph
8413 matrix for the display. If we can't get enough space for the
8414 whole text, display the last N lines. That works by setting w->start. */
8415 window_height_changed_p = resize_mini_window (w, 0);
8417 /* Use the starting position chosen by resize_mini_window. */
8418 SET_TEXT_POS_FROM_MARKER (start, w->start);
8420 /* Display. */
8421 clear_glyph_matrix (w->desired_matrix);
8422 XSETWINDOW (window, w);
8423 try_window (window, start, 0);
8425 return window_height_changed_p;
8429 /* Resize the echo area window to exactly the size needed for the
8430 currently displayed message, if there is one. If a mini-buffer
8431 is active, don't shrink it. */
8433 void
8434 resize_echo_area_exactly ()
8436 if (BUFFERP (echo_area_buffer[0])
8437 && WINDOWP (echo_area_window))
8439 struct window *w = XWINDOW (echo_area_window);
8440 int resized_p;
8441 Lisp_Object resize_exactly;
8443 if (minibuf_level == 0)
8444 resize_exactly = Qt;
8445 else
8446 resize_exactly = Qnil;
8448 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8449 (EMACS_INT) w, resize_exactly, 0, 0);
8450 if (resized_p)
8452 ++windows_or_buffers_changed;
8453 ++update_mode_lines;
8454 redisplay_internal (0);
8460 /* Callback function for with_echo_area_buffer, when used from
8461 resize_echo_area_exactly. A1 contains a pointer to the window to
8462 resize, EXACTLY non-nil means resize the mini-window exactly to the
8463 size of the text displayed. A3 and A4 are not used. Value is what
8464 resize_mini_window returns. */
8466 static int
8467 resize_mini_window_1 (a1, exactly, a3, a4)
8468 EMACS_INT a1;
8469 Lisp_Object exactly;
8470 EMACS_INT a3, a4;
8472 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8476 /* Resize mini-window W to fit the size of its contents. EXACT_P
8477 means size the window exactly to the size needed. Otherwise, it's
8478 only enlarged until W's buffer is empty.
8480 Set W->start to the right place to begin display. If the whole
8481 contents fit, start at the beginning. Otherwise, start so as
8482 to make the end of the contents appear. This is particularly
8483 important for y-or-n-p, but seems desirable generally.
8485 Value is non-zero if the window height has been changed. */
8488 resize_mini_window (w, exact_p)
8489 struct window *w;
8490 int exact_p;
8492 struct frame *f = XFRAME (w->frame);
8493 int window_height_changed_p = 0;
8495 xassert (MINI_WINDOW_P (w));
8497 /* By default, start display at the beginning. */
8498 set_marker_both (w->start, w->buffer,
8499 BUF_BEGV (XBUFFER (w->buffer)),
8500 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8502 /* Don't resize windows while redisplaying a window; it would
8503 confuse redisplay functions when the size of the window they are
8504 displaying changes from under them. Such a resizing can happen,
8505 for instance, when which-func prints a long message while
8506 we are running fontification-functions. We're running these
8507 functions with safe_call which binds inhibit-redisplay to t. */
8508 if (!NILP (Vinhibit_redisplay))
8509 return 0;
8511 /* Nil means don't try to resize. */
8512 if (NILP (Vresize_mini_windows)
8513 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8514 return 0;
8516 if (!FRAME_MINIBUF_ONLY_P (f))
8518 struct it it;
8519 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8520 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8521 int height, max_height;
8522 int unit = FRAME_LINE_HEIGHT (f);
8523 struct text_pos start;
8524 struct buffer *old_current_buffer = NULL;
8526 if (current_buffer != XBUFFER (w->buffer))
8528 old_current_buffer = current_buffer;
8529 set_buffer_internal (XBUFFER (w->buffer));
8532 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8534 /* Compute the max. number of lines specified by the user. */
8535 if (FLOATP (Vmax_mini_window_height))
8536 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8537 else if (INTEGERP (Vmax_mini_window_height))
8538 max_height = XINT (Vmax_mini_window_height);
8539 else
8540 max_height = total_height / 4;
8542 /* Correct that max. height if it's bogus. */
8543 max_height = max (1, max_height);
8544 max_height = min (total_height, max_height);
8546 /* Find out the height of the text in the window. */
8547 if (it.truncate_lines_p)
8548 height = 1;
8549 else
8551 last_height = 0;
8552 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8553 if (it.max_ascent == 0 && it.max_descent == 0)
8554 height = it.current_y + last_height;
8555 else
8556 height = it.current_y + it.max_ascent + it.max_descent;
8557 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8558 height = (height + unit - 1) / unit;
8561 /* Compute a suitable window start. */
8562 if (height > max_height)
8564 height = max_height;
8565 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8566 move_it_vertically_backward (&it, (height - 1) * unit);
8567 start = it.current.pos;
8569 else
8570 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8571 SET_MARKER_FROM_TEXT_POS (w->start, start);
8573 if (EQ (Vresize_mini_windows, Qgrow_only))
8575 /* Let it grow only, until we display an empty message, in which
8576 case the window shrinks again. */
8577 if (height > WINDOW_TOTAL_LINES (w))
8579 int old_height = WINDOW_TOTAL_LINES (w);
8580 freeze_window_starts (f, 1);
8581 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8582 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8584 else if (height < WINDOW_TOTAL_LINES (w)
8585 && (exact_p || BEGV == ZV))
8587 int old_height = WINDOW_TOTAL_LINES (w);
8588 freeze_window_starts (f, 0);
8589 shrink_mini_window (w);
8590 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8593 else
8595 /* Always resize to exact size needed. */
8596 if (height > WINDOW_TOTAL_LINES (w))
8598 int old_height = WINDOW_TOTAL_LINES (w);
8599 freeze_window_starts (f, 1);
8600 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8601 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8603 else if (height < WINDOW_TOTAL_LINES (w))
8605 int old_height = WINDOW_TOTAL_LINES (w);
8606 freeze_window_starts (f, 0);
8607 shrink_mini_window (w);
8609 if (height)
8611 freeze_window_starts (f, 1);
8612 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8615 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8619 if (old_current_buffer)
8620 set_buffer_internal (old_current_buffer);
8623 return window_height_changed_p;
8627 /* Value is the current message, a string, or nil if there is no
8628 current message. */
8630 Lisp_Object
8631 current_message ()
8633 Lisp_Object msg;
8635 if (NILP (echo_area_buffer[0]))
8636 msg = Qnil;
8637 else
8639 with_echo_area_buffer (0, 0, current_message_1,
8640 (EMACS_INT) &msg, Qnil, 0, 0);
8641 if (NILP (msg))
8642 echo_area_buffer[0] = Qnil;
8645 return msg;
8649 static int
8650 current_message_1 (a1, a2, a3, a4)
8651 EMACS_INT a1;
8652 Lisp_Object a2;
8653 EMACS_INT a3, a4;
8655 Lisp_Object *msg = (Lisp_Object *) a1;
8657 if (Z > BEG)
8658 *msg = make_buffer_string (BEG, Z, 1);
8659 else
8660 *msg = Qnil;
8661 return 0;
8665 /* Push the current message on Vmessage_stack for later restauration
8666 by restore_message. Value is non-zero if the current message isn't
8667 empty. This is a relatively infrequent operation, so it's not
8668 worth optimizing. */
8671 push_message ()
8673 Lisp_Object msg;
8674 msg = current_message ();
8675 Vmessage_stack = Fcons (msg, Vmessage_stack);
8676 return STRINGP (msg);
8680 /* Restore message display from the top of Vmessage_stack. */
8682 void
8683 restore_message ()
8685 Lisp_Object msg;
8687 xassert (CONSP (Vmessage_stack));
8688 msg = XCAR (Vmessage_stack);
8689 if (STRINGP (msg))
8690 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8691 else
8692 message3_nolog (msg, 0, 0);
8696 /* Handler for record_unwind_protect calling pop_message. */
8698 Lisp_Object
8699 pop_message_unwind (dummy)
8700 Lisp_Object dummy;
8702 pop_message ();
8703 return Qnil;
8706 /* Pop the top-most entry off Vmessage_stack. */
8708 void
8709 pop_message ()
8711 xassert (CONSP (Vmessage_stack));
8712 Vmessage_stack = XCDR (Vmessage_stack);
8716 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8717 exits. If the stack is not empty, we have a missing pop_message
8718 somewhere. */
8720 void
8721 check_message_stack ()
8723 if (!NILP (Vmessage_stack))
8724 abort ();
8728 /* Truncate to NCHARS what will be displayed in the echo area the next
8729 time we display it---but don't redisplay it now. */
8731 void
8732 truncate_echo_area (nchars)
8733 int nchars;
8735 if (nchars == 0)
8736 echo_area_buffer[0] = Qnil;
8737 /* A null message buffer means that the frame hasn't really been
8738 initialized yet. Error messages get reported properly by
8739 cmd_error, so this must be just an informative message; toss it. */
8740 else if (!noninteractive
8741 && INTERACTIVE
8742 && !NILP (echo_area_buffer[0]))
8744 struct frame *sf = SELECTED_FRAME ();
8745 if (FRAME_MESSAGE_BUF (sf))
8746 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8751 /* Helper function for truncate_echo_area. Truncate the current
8752 message to at most NCHARS characters. */
8754 static int
8755 truncate_message_1 (nchars, a2, a3, a4)
8756 EMACS_INT nchars;
8757 Lisp_Object a2;
8758 EMACS_INT a3, a4;
8760 if (BEG + nchars < Z)
8761 del_range (BEG + nchars, Z);
8762 if (Z == BEG)
8763 echo_area_buffer[0] = Qnil;
8764 return 0;
8768 /* Set the current message to a substring of S or STRING.
8770 If STRING is a Lisp string, set the message to the first NBYTES
8771 bytes from STRING. NBYTES zero means use the whole string. If
8772 STRING is multibyte, the message will be displayed multibyte.
8774 If S is not null, set the message to the first LEN bytes of S. LEN
8775 zero means use the whole string. MULTIBYTE_P non-zero means S is
8776 multibyte. Display the message multibyte in that case.
8778 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8779 to t before calling set_message_1 (which calls insert).
8782 void
8783 set_message (s, string, nbytes, multibyte_p)
8784 const char *s;
8785 Lisp_Object string;
8786 int nbytes, multibyte_p;
8788 message_enable_multibyte
8789 = ((s && multibyte_p)
8790 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8792 with_echo_area_buffer (0, -1, set_message_1,
8793 (EMACS_INT) s, string, nbytes, multibyte_p);
8794 message_buf_print = 0;
8795 help_echo_showing_p = 0;
8799 /* Helper function for set_message. Arguments have the same meaning
8800 as there, with A1 corresponding to S and A2 corresponding to STRING
8801 This function is called with the echo area buffer being
8802 current. */
8804 static int
8805 set_message_1 (a1, a2, nbytes, multibyte_p)
8806 EMACS_INT a1;
8807 Lisp_Object a2;
8808 EMACS_INT nbytes, multibyte_p;
8810 const char *s = (const char *) a1;
8811 Lisp_Object string = a2;
8813 /* Change multibyteness of the echo buffer appropriately. */
8814 if (message_enable_multibyte
8815 != !NILP (current_buffer->enable_multibyte_characters))
8816 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8818 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8820 /* Insert new message at BEG. */
8821 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8823 if (STRINGP (string))
8825 int nchars;
8827 if (nbytes == 0)
8828 nbytes = SBYTES (string);
8829 nchars = string_byte_to_char (string, nbytes);
8831 /* This function takes care of single/multibyte conversion. We
8832 just have to ensure that the echo area buffer has the right
8833 setting of enable_multibyte_characters. */
8834 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8836 else if (s)
8838 if (nbytes == 0)
8839 nbytes = strlen (s);
8841 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8843 /* Convert from multi-byte to single-byte. */
8844 int i, c, n;
8845 unsigned char work[1];
8847 /* Convert a multibyte string to single-byte. */
8848 for (i = 0; i < nbytes; i += n)
8850 c = string_char_and_length (s + i, nbytes - i, &n);
8851 work[0] = (ASCII_CHAR_P (c)
8853 : multibyte_char_to_unibyte (c, Qnil));
8854 insert_1_both (work, 1, 1, 1, 0, 0);
8857 else if (!multibyte_p
8858 && !NILP (current_buffer->enable_multibyte_characters))
8860 /* Convert from single-byte to multi-byte. */
8861 int i, c, n;
8862 const unsigned char *msg = (const unsigned char *) s;
8863 unsigned char str[MAX_MULTIBYTE_LENGTH];
8865 /* Convert a single-byte string to multibyte. */
8866 for (i = 0; i < nbytes; i++)
8868 c = msg[i];
8869 c = unibyte_char_to_multibyte (c);
8870 n = CHAR_STRING (c, str);
8871 insert_1_both (str, 1, n, 1, 0, 0);
8874 else
8875 insert_1 (s, nbytes, 1, 0, 0);
8878 return 0;
8882 /* Clear messages. CURRENT_P non-zero means clear the current
8883 message. LAST_DISPLAYED_P non-zero means clear the message
8884 last displayed. */
8886 void
8887 clear_message (current_p, last_displayed_p)
8888 int current_p, last_displayed_p;
8890 if (current_p)
8892 echo_area_buffer[0] = Qnil;
8893 message_cleared_p = 1;
8896 if (last_displayed_p)
8897 echo_area_buffer[1] = Qnil;
8899 message_buf_print = 0;
8902 /* Clear garbaged frames.
8904 This function is used where the old redisplay called
8905 redraw_garbaged_frames which in turn called redraw_frame which in
8906 turn called clear_frame. The call to clear_frame was a source of
8907 flickering. I believe a clear_frame is not necessary. It should
8908 suffice in the new redisplay to invalidate all current matrices,
8909 and ensure a complete redisplay of all windows. */
8911 static void
8912 clear_garbaged_frames ()
8914 if (frame_garbaged)
8916 Lisp_Object tail, frame;
8917 int changed_count = 0;
8919 FOR_EACH_FRAME (tail, frame)
8921 struct frame *f = XFRAME (frame);
8923 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8925 if (f->resized_p)
8927 Fredraw_frame (frame);
8928 f->force_flush_display_p = 1;
8930 clear_current_matrices (f);
8931 changed_count++;
8932 f->garbaged = 0;
8933 f->resized_p = 0;
8937 frame_garbaged = 0;
8938 if (changed_count)
8939 ++windows_or_buffers_changed;
8944 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8945 is non-zero update selected_frame. Value is non-zero if the
8946 mini-windows height has been changed. */
8948 static int
8949 echo_area_display (update_frame_p)
8950 int update_frame_p;
8952 Lisp_Object mini_window;
8953 struct window *w;
8954 struct frame *f;
8955 int window_height_changed_p = 0;
8956 struct frame *sf = SELECTED_FRAME ();
8958 mini_window = FRAME_MINIBUF_WINDOW (sf);
8959 w = XWINDOW (mini_window);
8960 f = XFRAME (WINDOW_FRAME (w));
8962 /* Don't display if frame is invisible or not yet initialized. */
8963 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8964 return 0;
8966 #ifdef HAVE_WINDOW_SYSTEM
8967 /* When Emacs starts, selected_frame may be the initial terminal
8968 frame. If we let this through, a message would be displayed on
8969 the terminal. */
8970 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8971 return 0;
8972 #endif /* HAVE_WINDOW_SYSTEM */
8974 /* Redraw garbaged frames. */
8975 if (frame_garbaged)
8976 clear_garbaged_frames ();
8978 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8980 echo_area_window = mini_window;
8981 window_height_changed_p = display_echo_area (w);
8982 w->must_be_updated_p = 1;
8984 /* Update the display, unless called from redisplay_internal.
8985 Also don't update the screen during redisplay itself. The
8986 update will happen at the end of redisplay, and an update
8987 here could cause confusion. */
8988 if (update_frame_p && !redisplaying_p)
8990 int n = 0;
8992 /* If the display update has been interrupted by pending
8993 input, update mode lines in the frame. Due to the
8994 pending input, it might have been that redisplay hasn't
8995 been called, so that mode lines above the echo area are
8996 garbaged. This looks odd, so we prevent it here. */
8997 if (!display_completed)
8998 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9000 if (window_height_changed_p
9001 /* Don't do this if Emacs is shutting down. Redisplay
9002 needs to run hooks. */
9003 && !NILP (Vrun_hooks))
9005 /* Must update other windows. Likewise as in other
9006 cases, don't let this update be interrupted by
9007 pending input. */
9008 int count = SPECPDL_INDEX ();
9009 specbind (Qredisplay_dont_pause, Qt);
9010 windows_or_buffers_changed = 1;
9011 redisplay_internal (0);
9012 unbind_to (count, Qnil);
9014 else if (FRAME_WINDOW_P (f) && n == 0)
9016 /* Window configuration is the same as before.
9017 Can do with a display update of the echo area,
9018 unless we displayed some mode lines. */
9019 update_single_window (w, 1);
9020 FRAME_RIF (f)->flush_display (f);
9022 else
9023 update_frame (f, 1, 1);
9025 /* If cursor is in the echo area, make sure that the next
9026 redisplay displays the minibuffer, so that the cursor will
9027 be replaced with what the minibuffer wants. */
9028 if (cursor_in_echo_area)
9029 ++windows_or_buffers_changed;
9032 else if (!EQ (mini_window, selected_window))
9033 windows_or_buffers_changed++;
9035 /* Last displayed message is now the current message. */
9036 echo_area_buffer[1] = echo_area_buffer[0];
9037 /* Inform read_char that we're not echoing. */
9038 echo_message_buffer = Qnil;
9040 /* Prevent redisplay optimization in redisplay_internal by resetting
9041 this_line_start_pos. This is done because the mini-buffer now
9042 displays the message instead of its buffer text. */
9043 if (EQ (mini_window, selected_window))
9044 CHARPOS (this_line_start_pos) = 0;
9046 return window_height_changed_p;
9051 /***********************************************************************
9052 Mode Lines and Frame Titles
9053 ***********************************************************************/
9055 /* A buffer for constructing non-propertized mode-line strings and
9056 frame titles in it; allocated from the heap in init_xdisp and
9057 resized as needed in store_mode_line_noprop_char. */
9059 static char *mode_line_noprop_buf;
9061 /* The buffer's end, and a current output position in it. */
9063 static char *mode_line_noprop_buf_end;
9064 static char *mode_line_noprop_ptr;
9066 #define MODE_LINE_NOPROP_LEN(start) \
9067 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9069 static enum {
9070 MODE_LINE_DISPLAY = 0,
9071 MODE_LINE_TITLE,
9072 MODE_LINE_NOPROP,
9073 MODE_LINE_STRING
9074 } mode_line_target;
9076 /* Alist that caches the results of :propertize.
9077 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9078 static Lisp_Object mode_line_proptrans_alist;
9080 /* List of strings making up the mode-line. */
9081 static Lisp_Object mode_line_string_list;
9083 /* Base face property when building propertized mode line string. */
9084 static Lisp_Object mode_line_string_face;
9085 static Lisp_Object mode_line_string_face_prop;
9088 /* Unwind data for mode line strings */
9090 static Lisp_Object Vmode_line_unwind_vector;
9092 static Lisp_Object
9093 format_mode_line_unwind_data (obuf, save_proptrans)
9094 struct buffer *obuf;
9096 Lisp_Object vector, tmp;
9098 /* Reduce consing by keeping one vector in
9099 Vwith_echo_area_save_vector. */
9100 vector = Vmode_line_unwind_vector;
9101 Vmode_line_unwind_vector = Qnil;
9103 if (NILP (vector))
9104 vector = Fmake_vector (make_number (7), Qnil);
9106 ASET (vector, 0, make_number (mode_line_target));
9107 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9108 ASET (vector, 2, mode_line_string_list);
9109 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9110 ASET (vector, 4, mode_line_string_face);
9111 ASET (vector, 5, mode_line_string_face_prop);
9113 if (obuf)
9114 XSETBUFFER (tmp, obuf);
9115 else
9116 tmp = Qnil;
9117 ASET (vector, 6, tmp);
9119 return vector;
9122 static Lisp_Object
9123 unwind_format_mode_line (vector)
9124 Lisp_Object vector;
9126 mode_line_target = XINT (AREF (vector, 0));
9127 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9128 mode_line_string_list = AREF (vector, 2);
9129 if (! EQ (AREF (vector, 3), Qt))
9130 mode_line_proptrans_alist = AREF (vector, 3);
9131 mode_line_string_face = AREF (vector, 4);
9132 mode_line_string_face_prop = AREF (vector, 5);
9134 if (!NILP (AREF (vector, 6)))
9136 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9137 ASET (vector, 6, Qnil);
9140 Vmode_line_unwind_vector = vector;
9141 return Qnil;
9145 /* Store a single character C for the frame title in mode_line_noprop_buf.
9146 Re-allocate mode_line_noprop_buf if necessary. */
9148 static void
9149 #ifdef PROTOTYPES
9150 store_mode_line_noprop_char (char c)
9151 #else
9152 store_mode_line_noprop_char (c)
9153 char c;
9154 #endif
9156 /* If output position has reached the end of the allocated buffer,
9157 double the buffer's size. */
9158 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9160 int len = MODE_LINE_NOPROP_LEN (0);
9161 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9162 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9163 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9164 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9167 *mode_line_noprop_ptr++ = c;
9171 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9172 mode_line_noprop_ptr. STR is the string to store. Do not copy
9173 characters that yield more columns than PRECISION; PRECISION <= 0
9174 means copy the whole string. Pad with spaces until FIELD_WIDTH
9175 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9176 pad. Called from display_mode_element when it is used to build a
9177 frame title. */
9179 static int
9180 store_mode_line_noprop (str, field_width, precision)
9181 const unsigned char *str;
9182 int field_width, precision;
9184 int n = 0;
9185 int dummy, nbytes;
9187 /* Copy at most PRECISION chars from STR. */
9188 nbytes = strlen (str);
9189 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9190 while (nbytes--)
9191 store_mode_line_noprop_char (*str++);
9193 /* Fill up with spaces until FIELD_WIDTH reached. */
9194 while (field_width > 0
9195 && n < field_width)
9197 store_mode_line_noprop_char (' ');
9198 ++n;
9201 return n;
9204 /***********************************************************************
9205 Frame Titles
9206 ***********************************************************************/
9208 #ifdef HAVE_WINDOW_SYSTEM
9210 /* Set the title of FRAME, if it has changed. The title format is
9211 Vicon_title_format if FRAME is iconified, otherwise it is
9212 frame_title_format. */
9214 static void
9215 x_consider_frame_title (frame)
9216 Lisp_Object frame;
9218 struct frame *f = XFRAME (frame);
9220 if (FRAME_WINDOW_P (f)
9221 || FRAME_MINIBUF_ONLY_P (f)
9222 || f->explicit_name)
9224 /* Do we have more than one visible frame on this X display? */
9225 Lisp_Object tail;
9226 Lisp_Object fmt;
9227 int title_start;
9228 char *title;
9229 int len;
9230 struct it it;
9231 int count = SPECPDL_INDEX ();
9233 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9235 Lisp_Object other_frame = XCAR (tail);
9236 struct frame *tf = XFRAME (other_frame);
9238 if (tf != f
9239 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9240 && !FRAME_MINIBUF_ONLY_P (tf)
9241 && !EQ (other_frame, tip_frame)
9242 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9243 break;
9246 /* Set global variable indicating that multiple frames exist. */
9247 multiple_frames = CONSP (tail);
9249 /* Switch to the buffer of selected window of the frame. Set up
9250 mode_line_target so that display_mode_element will output into
9251 mode_line_noprop_buf; then display the title. */
9252 record_unwind_protect (unwind_format_mode_line,
9253 format_mode_line_unwind_data (current_buffer, 0));
9255 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9256 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9258 mode_line_target = MODE_LINE_TITLE;
9259 title_start = MODE_LINE_NOPROP_LEN (0);
9260 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9261 NULL, DEFAULT_FACE_ID);
9262 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9263 len = MODE_LINE_NOPROP_LEN (title_start);
9264 title = mode_line_noprop_buf + title_start;
9265 unbind_to (count, Qnil);
9267 /* Set the title only if it's changed. This avoids consing in
9268 the common case where it hasn't. (If it turns out that we've
9269 already wasted too much time by walking through the list with
9270 display_mode_element, then we might need to optimize at a
9271 higher level than this.) */
9272 if (! STRINGP (f->name)
9273 || SBYTES (f->name) != len
9274 || bcmp (title, SDATA (f->name), len) != 0)
9275 x_implicitly_set_name (f, make_string (title, len), Qnil);
9279 #endif /* not HAVE_WINDOW_SYSTEM */
9284 /***********************************************************************
9285 Menu Bars
9286 ***********************************************************************/
9289 /* Prepare for redisplay by updating menu-bar item lists when
9290 appropriate. This can call eval. */
9292 void
9293 prepare_menu_bars ()
9295 int all_windows;
9296 struct gcpro gcpro1, gcpro2;
9297 struct frame *f;
9298 Lisp_Object tooltip_frame;
9300 #ifdef HAVE_WINDOW_SYSTEM
9301 tooltip_frame = tip_frame;
9302 #else
9303 tooltip_frame = Qnil;
9304 #endif
9306 /* Update all frame titles based on their buffer names, etc. We do
9307 this before the menu bars so that the buffer-menu will show the
9308 up-to-date frame titles. */
9309 #ifdef HAVE_WINDOW_SYSTEM
9310 if (windows_or_buffers_changed || update_mode_lines)
9312 Lisp_Object tail, frame;
9314 FOR_EACH_FRAME (tail, frame)
9316 f = XFRAME (frame);
9317 if (!EQ (frame, tooltip_frame)
9318 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9319 x_consider_frame_title (frame);
9322 #endif /* HAVE_WINDOW_SYSTEM */
9324 /* Update the menu bar item lists, if appropriate. This has to be
9325 done before any actual redisplay or generation of display lines. */
9326 all_windows = (update_mode_lines
9327 || buffer_shared > 1
9328 || windows_or_buffers_changed);
9329 if (all_windows)
9331 Lisp_Object tail, frame;
9332 int count = SPECPDL_INDEX ();
9333 /* 1 means that update_menu_bar has run its hooks
9334 so any further calls to update_menu_bar shouldn't do so again. */
9335 int menu_bar_hooks_run = 0;
9337 record_unwind_save_match_data ();
9339 FOR_EACH_FRAME (tail, frame)
9341 f = XFRAME (frame);
9343 /* Ignore tooltip frame. */
9344 if (EQ (frame, tooltip_frame))
9345 continue;
9347 /* If a window on this frame changed size, report that to
9348 the user and clear the size-change flag. */
9349 if (FRAME_WINDOW_SIZES_CHANGED (f))
9351 Lisp_Object functions;
9353 /* Clear flag first in case we get an error below. */
9354 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9355 functions = Vwindow_size_change_functions;
9356 GCPRO2 (tail, functions);
9358 while (CONSP (functions))
9360 call1 (XCAR (functions), frame);
9361 functions = XCDR (functions);
9363 UNGCPRO;
9366 GCPRO1 (tail);
9367 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9368 #ifdef HAVE_WINDOW_SYSTEM
9369 update_tool_bar (f, 0);
9370 #ifdef MAC_OS
9371 mac_update_title_bar (f, 0);
9372 #endif
9373 #endif
9374 UNGCPRO;
9377 unbind_to (count, Qnil);
9379 else
9381 struct frame *sf = SELECTED_FRAME ();
9382 update_menu_bar (sf, 1, 0);
9383 #ifdef HAVE_WINDOW_SYSTEM
9384 update_tool_bar (sf, 1);
9385 #ifdef MAC_OS
9386 mac_update_title_bar (sf, 1);
9387 #endif
9388 #endif
9391 /* Motif needs this. See comment in xmenu.c. Turn it off when
9392 pending_menu_activation is not defined. */
9393 #ifdef USE_X_TOOLKIT
9394 pending_menu_activation = 0;
9395 #endif
9399 /* Update the menu bar item list for frame F. This has to be done
9400 before we start to fill in any display lines, because it can call
9401 eval.
9403 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9405 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9406 already ran the menu bar hooks for this redisplay, so there
9407 is no need to run them again. The return value is the
9408 updated value of this flag, to pass to the next call. */
9410 static int
9411 update_menu_bar (f, save_match_data, hooks_run)
9412 struct frame *f;
9413 int save_match_data;
9414 int hooks_run;
9416 Lisp_Object window;
9417 register struct window *w;
9419 /* If called recursively during a menu update, do nothing. This can
9420 happen when, for instance, an activate-menubar-hook causes a
9421 redisplay. */
9422 if (inhibit_menubar_update)
9423 return hooks_run;
9425 window = FRAME_SELECTED_WINDOW (f);
9426 w = XWINDOW (window);
9428 #if 0 /* The if statement below this if statement used to include the
9429 condition !NILP (w->update_mode_line), rather than using
9430 update_mode_lines directly, and this if statement may have
9431 been added to make that condition work. Now the if
9432 statement below matches its comment, this isn't needed. */
9433 if (update_mode_lines)
9434 w->update_mode_line = Qt;
9435 #endif
9437 if (FRAME_WINDOW_P (f)
9439 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9440 || defined (USE_GTK)
9441 FRAME_EXTERNAL_MENU_BAR (f)
9442 #else
9443 FRAME_MENU_BAR_LINES (f) > 0
9444 #endif
9445 : FRAME_MENU_BAR_LINES (f) > 0)
9447 /* If the user has switched buffers or windows, we need to
9448 recompute to reflect the new bindings. But we'll
9449 recompute when update_mode_lines is set too; that means
9450 that people can use force-mode-line-update to request
9451 that the menu bar be recomputed. The adverse effect on
9452 the rest of the redisplay algorithm is about the same as
9453 windows_or_buffers_changed anyway. */
9454 if (windows_or_buffers_changed
9455 /* This used to test w->update_mode_line, but we believe
9456 there is no need to recompute the menu in that case. */
9457 || update_mode_lines
9458 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9459 < BUF_MODIFF (XBUFFER (w->buffer)))
9460 != !NILP (w->last_had_star))
9461 || ((!NILP (Vtransient_mark_mode)
9462 && !NILP (XBUFFER (w->buffer)->mark_active))
9463 != !NILP (w->region_showing)))
9465 struct buffer *prev = current_buffer;
9466 int count = SPECPDL_INDEX ();
9468 specbind (Qinhibit_menubar_update, Qt);
9470 set_buffer_internal_1 (XBUFFER (w->buffer));
9471 if (save_match_data)
9472 record_unwind_save_match_data ();
9473 if (NILP (Voverriding_local_map_menu_flag))
9475 specbind (Qoverriding_terminal_local_map, Qnil);
9476 specbind (Qoverriding_local_map, Qnil);
9479 if (!hooks_run)
9481 /* Run the Lucid hook. */
9482 safe_run_hooks (Qactivate_menubar_hook);
9484 /* If it has changed current-menubar from previous value,
9485 really recompute the menu-bar from the value. */
9486 if (! NILP (Vlucid_menu_bar_dirty_flag))
9487 call0 (Qrecompute_lucid_menubar);
9489 safe_run_hooks (Qmenu_bar_update_hook);
9491 hooks_run = 1;
9494 XSETFRAME (Vmenu_updating_frame, f);
9495 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9497 /* Redisplay the menu bar in case we changed it. */
9498 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9499 || defined (USE_GTK)
9500 if (FRAME_WINDOW_P (f))
9502 #ifdef MAC_OS
9503 /* All frames on Mac OS share the same menubar. So only
9504 the selected frame should be allowed to set it. */
9505 if (f == SELECTED_FRAME ())
9506 #endif
9507 set_frame_menubar (f, 0, 0);
9509 else
9510 /* On a terminal screen, the menu bar is an ordinary screen
9511 line, and this makes it get updated. */
9512 w->update_mode_line = Qt;
9513 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9514 /* In the non-toolkit version, the menu bar is an ordinary screen
9515 line, and this makes it get updated. */
9516 w->update_mode_line = Qt;
9517 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9519 unbind_to (count, Qnil);
9520 set_buffer_internal_1 (prev);
9524 return hooks_run;
9529 /***********************************************************************
9530 Output Cursor
9531 ***********************************************************************/
9533 #ifdef HAVE_WINDOW_SYSTEM
9535 /* EXPORT:
9536 Nominal cursor position -- where to draw output.
9537 HPOS and VPOS are window relative glyph matrix coordinates.
9538 X and Y are window relative pixel coordinates. */
9540 struct cursor_pos output_cursor;
9543 /* EXPORT:
9544 Set the global variable output_cursor to CURSOR. All cursor
9545 positions are relative to updated_window. */
9547 void
9548 set_output_cursor (cursor)
9549 struct cursor_pos *cursor;
9551 output_cursor.hpos = cursor->hpos;
9552 output_cursor.vpos = cursor->vpos;
9553 output_cursor.x = cursor->x;
9554 output_cursor.y = cursor->y;
9558 /* EXPORT for RIF:
9559 Set a nominal cursor position.
9561 HPOS and VPOS are column/row positions in a window glyph matrix. X
9562 and Y are window text area relative pixel positions.
9564 If this is done during an update, updated_window will contain the
9565 window that is being updated and the position is the future output
9566 cursor position for that window. If updated_window is null, use
9567 selected_window and display the cursor at the given position. */
9569 void
9570 x_cursor_to (vpos, hpos, y, x)
9571 int vpos, hpos, y, x;
9573 struct window *w;
9575 /* If updated_window is not set, work on selected_window. */
9576 if (updated_window)
9577 w = updated_window;
9578 else
9579 w = XWINDOW (selected_window);
9581 /* Set the output cursor. */
9582 output_cursor.hpos = hpos;
9583 output_cursor.vpos = vpos;
9584 output_cursor.x = x;
9585 output_cursor.y = y;
9587 /* If not called as part of an update, really display the cursor.
9588 This will also set the cursor position of W. */
9589 if (updated_window == NULL)
9591 BLOCK_INPUT;
9592 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9593 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9594 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9595 UNBLOCK_INPUT;
9599 #endif /* HAVE_WINDOW_SYSTEM */
9602 /***********************************************************************
9603 Tool-bars
9604 ***********************************************************************/
9606 #ifdef HAVE_WINDOW_SYSTEM
9608 /* Where the mouse was last time we reported a mouse event. */
9610 FRAME_PTR last_mouse_frame;
9612 /* Tool-bar item index of the item on which a mouse button was pressed
9613 or -1. */
9615 int last_tool_bar_item;
9618 /* Update the tool-bar item list for frame F. This has to be done
9619 before we start to fill in any display lines. Called from
9620 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9621 and restore it here. */
9623 static void
9624 update_tool_bar (f, save_match_data)
9625 struct frame *f;
9626 int save_match_data;
9628 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9629 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9630 #else
9631 int do_update = WINDOWP (f->tool_bar_window)
9632 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9633 #endif
9635 if (do_update)
9637 Lisp_Object window;
9638 struct window *w;
9640 window = FRAME_SELECTED_WINDOW (f);
9641 w = XWINDOW (window);
9643 /* If the user has switched buffers or windows, we need to
9644 recompute to reflect the new bindings. But we'll
9645 recompute when update_mode_lines is set too; that means
9646 that people can use force-mode-line-update to request
9647 that the menu bar be recomputed. The adverse effect on
9648 the rest of the redisplay algorithm is about the same as
9649 windows_or_buffers_changed anyway. */
9650 if (windows_or_buffers_changed
9651 || !NILP (w->update_mode_line)
9652 || update_mode_lines
9653 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9654 < BUF_MODIFF (XBUFFER (w->buffer)))
9655 != !NILP (w->last_had_star))
9656 || ((!NILP (Vtransient_mark_mode)
9657 && !NILP (XBUFFER (w->buffer)->mark_active))
9658 != !NILP (w->region_showing)))
9660 struct buffer *prev = current_buffer;
9661 int count = SPECPDL_INDEX ();
9662 Lisp_Object new_tool_bar;
9663 int new_n_tool_bar;
9664 struct gcpro gcpro1;
9666 /* Set current_buffer to the buffer of the selected
9667 window of the frame, so that we get the right local
9668 keymaps. */
9669 set_buffer_internal_1 (XBUFFER (w->buffer));
9671 /* Save match data, if we must. */
9672 if (save_match_data)
9673 record_unwind_save_match_data ();
9675 /* Make sure that we don't accidentally use bogus keymaps. */
9676 if (NILP (Voverriding_local_map_menu_flag))
9678 specbind (Qoverriding_terminal_local_map, Qnil);
9679 specbind (Qoverriding_local_map, Qnil);
9682 GCPRO1 (new_tool_bar);
9684 /* Build desired tool-bar items from keymaps. */
9685 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9686 &new_n_tool_bar);
9688 /* Redisplay the tool-bar if we changed it. */
9689 if (new_n_tool_bar != f->n_tool_bar_items
9690 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9692 /* Redisplay that happens asynchronously due to an expose event
9693 may access f->tool_bar_items. Make sure we update both
9694 variables within BLOCK_INPUT so no such event interrupts. */
9695 BLOCK_INPUT;
9696 f->tool_bar_items = new_tool_bar;
9697 f->n_tool_bar_items = new_n_tool_bar;
9698 w->update_mode_line = Qt;
9699 UNBLOCK_INPUT;
9702 UNGCPRO;
9704 unbind_to (count, Qnil);
9705 set_buffer_internal_1 (prev);
9711 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9712 F's desired tool-bar contents. F->tool_bar_items must have
9713 been set up previously by calling prepare_menu_bars. */
9715 static void
9716 build_desired_tool_bar_string (f)
9717 struct frame *f;
9719 int i, size, size_needed;
9720 struct gcpro gcpro1, gcpro2, gcpro3;
9721 Lisp_Object image, plist, props;
9723 image = plist = props = Qnil;
9724 GCPRO3 (image, plist, props);
9726 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9727 Otherwise, make a new string. */
9729 /* The size of the string we might be able to reuse. */
9730 size = (STRINGP (f->desired_tool_bar_string)
9731 ? SCHARS (f->desired_tool_bar_string)
9732 : 0);
9734 /* We need one space in the string for each image. */
9735 size_needed = f->n_tool_bar_items;
9737 /* Reuse f->desired_tool_bar_string, if possible. */
9738 if (size < size_needed || NILP (f->desired_tool_bar_string))
9739 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9740 make_number (' '));
9741 else
9743 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9744 Fremove_text_properties (make_number (0), make_number (size),
9745 props, f->desired_tool_bar_string);
9748 /* Put a `display' property on the string for the images to display,
9749 put a `menu_item' property on tool-bar items with a value that
9750 is the index of the item in F's tool-bar item vector. */
9751 for (i = 0; i < f->n_tool_bar_items; ++i)
9753 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9755 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9756 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9757 int hmargin, vmargin, relief, idx, end;
9758 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9760 /* If image is a vector, choose the image according to the
9761 button state. */
9762 image = PROP (TOOL_BAR_ITEM_IMAGES);
9763 if (VECTORP (image))
9765 if (enabled_p)
9766 idx = (selected_p
9767 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9768 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9769 else
9770 idx = (selected_p
9771 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9772 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9774 xassert (ASIZE (image) >= idx);
9775 image = AREF (image, idx);
9777 else
9778 idx = -1;
9780 /* Ignore invalid image specifications. */
9781 if (!valid_image_p (image))
9782 continue;
9784 /* Display the tool-bar button pressed, or depressed. */
9785 plist = Fcopy_sequence (XCDR (image));
9787 /* Compute margin and relief to draw. */
9788 relief = (tool_bar_button_relief >= 0
9789 ? tool_bar_button_relief
9790 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9791 hmargin = vmargin = relief;
9793 if (INTEGERP (Vtool_bar_button_margin)
9794 && XINT (Vtool_bar_button_margin) > 0)
9796 hmargin += XFASTINT (Vtool_bar_button_margin);
9797 vmargin += XFASTINT (Vtool_bar_button_margin);
9799 else if (CONSP (Vtool_bar_button_margin))
9801 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9802 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9803 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9805 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9806 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9807 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9810 if (auto_raise_tool_bar_buttons_p)
9812 /* Add a `:relief' property to the image spec if the item is
9813 selected. */
9814 if (selected_p)
9816 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9817 hmargin -= relief;
9818 vmargin -= relief;
9821 else
9823 /* If image is selected, display it pressed, i.e. with a
9824 negative relief. If it's not selected, display it with a
9825 raised relief. */
9826 plist = Fplist_put (plist, QCrelief,
9827 (selected_p
9828 ? make_number (-relief)
9829 : make_number (relief)));
9830 hmargin -= relief;
9831 vmargin -= relief;
9834 /* Put a margin around the image. */
9835 if (hmargin || vmargin)
9837 if (hmargin == vmargin)
9838 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9839 else
9840 plist = Fplist_put (plist, QCmargin,
9841 Fcons (make_number (hmargin),
9842 make_number (vmargin)));
9845 /* If button is not enabled, and we don't have special images
9846 for the disabled state, make the image appear disabled by
9847 applying an appropriate algorithm to it. */
9848 if (!enabled_p && idx < 0)
9849 plist = Fplist_put (plist, QCconversion, Qdisabled);
9851 /* Put a `display' text property on the string for the image to
9852 display. Put a `menu-item' property on the string that gives
9853 the start of this item's properties in the tool-bar items
9854 vector. */
9855 image = Fcons (Qimage, plist);
9856 props = list4 (Qdisplay, image,
9857 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9859 /* Let the last image hide all remaining spaces in the tool bar
9860 string. The string can be longer than needed when we reuse a
9861 previous string. */
9862 if (i + 1 == f->n_tool_bar_items)
9863 end = SCHARS (f->desired_tool_bar_string);
9864 else
9865 end = i + 1;
9866 Fadd_text_properties (make_number (i), make_number (end),
9867 props, f->desired_tool_bar_string);
9868 #undef PROP
9871 UNGCPRO;
9875 /* Display one line of the tool-bar of frame IT->f.
9877 HEIGHT specifies the desired height of the tool-bar line.
9878 If the actual height of the glyph row is less than HEIGHT, the
9879 row's height is increased to HEIGHT, and the icons are centered
9880 vertically in the new height.
9882 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9883 count a final empty row in case the tool-bar width exactly matches
9884 the window width.
9887 static void
9888 display_tool_bar_line (it, height)
9889 struct it *it;
9890 int height;
9892 struct glyph_row *row = it->glyph_row;
9893 int max_x = it->last_visible_x;
9894 struct glyph *last;
9896 prepare_desired_row (row);
9897 row->y = it->current_y;
9899 /* Note that this isn't made use of if the face hasn't a box,
9900 so there's no need to check the face here. */
9901 it->start_of_box_run_p = 1;
9903 while (it->current_x < max_x)
9905 int x, n_glyphs_before, i, nglyphs;
9906 struct it it_before;
9908 /* Get the next display element. */
9909 if (!get_next_display_element (it))
9911 /* Don't count empty row if we are counting needed tool-bar lines. */
9912 if (height < 0 && !it->hpos)
9913 return;
9914 break;
9917 /* Produce glyphs. */
9918 n_glyphs_before = row->used[TEXT_AREA];
9919 it_before = *it;
9921 PRODUCE_GLYPHS (it);
9923 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9924 i = 0;
9925 x = it_before.current_x;
9926 while (i < nglyphs)
9928 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9930 if (x + glyph->pixel_width > max_x)
9932 /* Glyph doesn't fit on line. Backtrack. */
9933 row->used[TEXT_AREA] = n_glyphs_before;
9934 *it = it_before;
9935 /* If this is the only glyph on this line, it will never fit on the
9936 toolbar, so skip it. But ensure there is at least one glyph,
9937 so we don't accidentally disable the tool-bar. */
9938 if (n_glyphs_before == 0
9939 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9940 break;
9941 goto out;
9944 ++it->hpos;
9945 x += glyph->pixel_width;
9946 ++i;
9949 /* Stop at line ends. */
9950 if (ITERATOR_AT_END_OF_LINE_P (it))
9951 break;
9953 set_iterator_to_next (it, 1);
9956 out:;
9958 row->displays_text_p = row->used[TEXT_AREA] != 0;
9960 /* Use default face for the border below the tool bar.
9962 FIXME: When auto-resize-tool-bars is grow-only, there is
9963 no additional border below the possibly empty tool-bar lines.
9964 So to make the extra empty lines look "normal", we have to
9965 use the tool-bar face for the border too. */
9966 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9967 it->face_id = DEFAULT_FACE_ID;
9969 extend_face_to_end_of_line (it);
9970 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9971 last->right_box_line_p = 1;
9972 if (last == row->glyphs[TEXT_AREA])
9973 last->left_box_line_p = 1;
9975 /* Make line the desired height and center it vertically. */
9976 if ((height -= it->max_ascent + it->max_descent) > 0)
9978 /* Don't add more than one line height. */
9979 height %= FRAME_LINE_HEIGHT (it->f);
9980 it->max_ascent += height / 2;
9981 it->max_descent += (height + 1) / 2;
9984 compute_line_metrics (it);
9986 /* If line is empty, make it occupy the rest of the tool-bar. */
9987 if (!row->displays_text_p)
9989 row->height = row->phys_height = it->last_visible_y - row->y;
9990 row->visible_height = row->height;
9991 row->ascent = row->phys_ascent = 0;
9992 row->extra_line_spacing = 0;
9995 row->full_width_p = 1;
9996 row->continued_p = 0;
9997 row->truncated_on_left_p = 0;
9998 row->truncated_on_right_p = 0;
10000 it->current_x = it->hpos = 0;
10001 it->current_y += row->height;
10002 ++it->vpos;
10003 ++it->glyph_row;
10007 /* Max tool-bar height. */
10009 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10010 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10012 /* Value is the number of screen lines needed to make all tool-bar
10013 items of frame F visible. The number of actual rows needed is
10014 returned in *N_ROWS if non-NULL. */
10016 static int
10017 tool_bar_lines_needed (f, n_rows)
10018 struct frame *f;
10019 int *n_rows;
10021 struct window *w = XWINDOW (f->tool_bar_window);
10022 struct it it;
10023 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10024 the desired matrix, so use (unused) mode-line row as temporary row to
10025 avoid destroying the first tool-bar row. */
10026 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10028 /* Initialize an iterator for iteration over
10029 F->desired_tool_bar_string in the tool-bar window of frame F. */
10030 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10031 it.first_visible_x = 0;
10032 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10033 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10035 while (!ITERATOR_AT_END_P (&it))
10037 clear_glyph_row (temp_row);
10038 it.glyph_row = temp_row;
10039 display_tool_bar_line (&it, -1);
10041 clear_glyph_row (temp_row);
10043 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10044 if (n_rows)
10045 *n_rows = it.vpos > 0 ? it.vpos : -1;
10047 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10051 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10052 0, 1, 0,
10053 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10054 (frame)
10055 Lisp_Object frame;
10057 struct frame *f;
10058 struct window *w;
10059 int nlines = 0;
10061 if (NILP (frame))
10062 frame = selected_frame;
10063 else
10064 CHECK_FRAME (frame);
10065 f = XFRAME (frame);
10067 if (WINDOWP (f->tool_bar_window)
10068 || (w = XWINDOW (f->tool_bar_window),
10069 WINDOW_TOTAL_LINES (w) > 0))
10071 update_tool_bar (f, 1);
10072 if (f->n_tool_bar_items)
10074 build_desired_tool_bar_string (f);
10075 nlines = tool_bar_lines_needed (f, NULL);
10079 return make_number (nlines);
10083 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10084 height should be changed. */
10086 static int
10087 redisplay_tool_bar (f)
10088 struct frame *f;
10090 struct window *w;
10091 struct it it;
10092 struct glyph_row *row;
10094 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10095 if (FRAME_EXTERNAL_TOOL_BAR (f))
10096 update_frame_tool_bar (f);
10097 return 0;
10098 #endif
10100 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10101 do anything. This means you must start with tool-bar-lines
10102 non-zero to get the auto-sizing effect. Or in other words, you
10103 can turn off tool-bars by specifying tool-bar-lines zero. */
10104 if (!WINDOWP (f->tool_bar_window)
10105 || (w = XWINDOW (f->tool_bar_window),
10106 WINDOW_TOTAL_LINES (w) == 0))
10107 return 0;
10109 /* Set up an iterator for the tool-bar window. */
10110 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10111 it.first_visible_x = 0;
10112 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10113 row = it.glyph_row;
10115 /* Build a string that represents the contents of the tool-bar. */
10116 build_desired_tool_bar_string (f);
10117 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10119 if (f->n_tool_bar_rows == 0)
10121 int nlines;
10123 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10124 nlines != WINDOW_TOTAL_LINES (w)))
10126 extern Lisp_Object Qtool_bar_lines;
10127 Lisp_Object frame;
10128 int old_height = WINDOW_TOTAL_LINES (w);
10130 XSETFRAME (frame, f);
10131 Fmodify_frame_parameters (frame,
10132 Fcons (Fcons (Qtool_bar_lines,
10133 make_number (nlines)),
10134 Qnil));
10135 if (WINDOW_TOTAL_LINES (w) != old_height)
10137 clear_glyph_matrix (w->desired_matrix);
10138 fonts_changed_p = 1;
10139 return 1;
10144 /* Display as many lines as needed to display all tool-bar items. */
10146 if (f->n_tool_bar_rows > 0)
10148 int border, rows, height, extra;
10150 if (INTEGERP (Vtool_bar_border))
10151 border = XINT (Vtool_bar_border);
10152 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10153 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10154 else if (EQ (Vtool_bar_border, Qborder_width))
10155 border = f->border_width;
10156 else
10157 border = 0;
10158 if (border < 0)
10159 border = 0;
10161 rows = f->n_tool_bar_rows;
10162 height = max (1, (it.last_visible_y - border) / rows);
10163 extra = it.last_visible_y - border - height * rows;
10165 while (it.current_y < it.last_visible_y)
10167 int h = 0;
10168 if (extra > 0 && rows-- > 0)
10170 h = (extra + rows - 1) / rows;
10171 extra -= h;
10173 display_tool_bar_line (&it, height + h);
10176 else
10178 while (it.current_y < it.last_visible_y)
10179 display_tool_bar_line (&it, 0);
10182 /* It doesn't make much sense to try scrolling in the tool-bar
10183 window, so don't do it. */
10184 w->desired_matrix->no_scrolling_p = 1;
10185 w->must_be_updated_p = 1;
10187 if (!NILP (Vauto_resize_tool_bars))
10189 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10190 int change_height_p = 0;
10192 /* If we couldn't display everything, change the tool-bar's
10193 height if there is room for more. */
10194 if (IT_STRING_CHARPOS (it) < it.end_charpos
10195 && it.current_y < max_tool_bar_height)
10196 change_height_p = 1;
10198 row = it.glyph_row - 1;
10200 /* If there are blank lines at the end, except for a partially
10201 visible blank line at the end that is smaller than
10202 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10203 if (!row->displays_text_p
10204 && row->height >= FRAME_LINE_HEIGHT (f))
10205 change_height_p = 1;
10207 /* If row displays tool-bar items, but is partially visible,
10208 change the tool-bar's height. */
10209 if (row->displays_text_p
10210 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10211 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10212 change_height_p = 1;
10214 /* Resize windows as needed by changing the `tool-bar-lines'
10215 frame parameter. */
10216 if (change_height_p)
10218 extern Lisp_Object Qtool_bar_lines;
10219 Lisp_Object frame;
10220 int old_height = WINDOW_TOTAL_LINES (w);
10221 int nrows;
10222 int nlines = tool_bar_lines_needed (f, &nrows);
10224 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10225 && !f->minimize_tool_bar_window_p)
10226 ? (nlines > old_height)
10227 : (nlines != old_height));
10228 f->minimize_tool_bar_window_p = 0;
10230 if (change_height_p)
10232 XSETFRAME (frame, f);
10233 Fmodify_frame_parameters (frame,
10234 Fcons (Fcons (Qtool_bar_lines,
10235 make_number (nlines)),
10236 Qnil));
10237 if (WINDOW_TOTAL_LINES (w) != old_height)
10239 clear_glyph_matrix (w->desired_matrix);
10240 f->n_tool_bar_rows = nrows;
10241 fonts_changed_p = 1;
10242 return 1;
10248 f->minimize_tool_bar_window_p = 0;
10249 return 0;
10253 /* Get information about the tool-bar item which is displayed in GLYPH
10254 on frame F. Return in *PROP_IDX the index where tool-bar item
10255 properties start in F->tool_bar_items. Value is zero if
10256 GLYPH doesn't display a tool-bar item. */
10258 static int
10259 tool_bar_item_info (f, glyph, prop_idx)
10260 struct frame *f;
10261 struct glyph *glyph;
10262 int *prop_idx;
10264 Lisp_Object prop;
10265 int success_p;
10266 int charpos;
10268 /* This function can be called asynchronously, which means we must
10269 exclude any possibility that Fget_text_property signals an
10270 error. */
10271 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10272 charpos = max (0, charpos);
10274 /* Get the text property `menu-item' at pos. The value of that
10275 property is the start index of this item's properties in
10276 F->tool_bar_items. */
10277 prop = Fget_text_property (make_number (charpos),
10278 Qmenu_item, f->current_tool_bar_string);
10279 if (INTEGERP (prop))
10281 *prop_idx = XINT (prop);
10282 success_p = 1;
10284 else
10285 success_p = 0;
10287 return success_p;
10291 /* Get information about the tool-bar item at position X/Y on frame F.
10292 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10293 the current matrix of the tool-bar window of F, or NULL if not
10294 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10295 item in F->tool_bar_items. Value is
10297 -1 if X/Y is not on a tool-bar item
10298 0 if X/Y is on the same item that was highlighted before.
10299 1 otherwise. */
10301 static int
10302 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10303 struct frame *f;
10304 int x, y;
10305 struct glyph **glyph;
10306 int *hpos, *vpos, *prop_idx;
10308 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10309 struct window *w = XWINDOW (f->tool_bar_window);
10310 int area;
10312 /* Find the glyph under X/Y. */
10313 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10314 if (*glyph == NULL)
10315 return -1;
10317 /* Get the start of this tool-bar item's properties in
10318 f->tool_bar_items. */
10319 if (!tool_bar_item_info (f, *glyph, prop_idx))
10320 return -1;
10322 /* Is mouse on the highlighted item? */
10323 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10324 && *vpos >= dpyinfo->mouse_face_beg_row
10325 && *vpos <= dpyinfo->mouse_face_end_row
10326 && (*vpos > dpyinfo->mouse_face_beg_row
10327 || *hpos >= dpyinfo->mouse_face_beg_col)
10328 && (*vpos < dpyinfo->mouse_face_end_row
10329 || *hpos < dpyinfo->mouse_face_end_col
10330 || dpyinfo->mouse_face_past_end))
10331 return 0;
10333 return 1;
10337 /* EXPORT:
10338 Handle mouse button event on the tool-bar of frame F, at
10339 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10340 0 for button release. MODIFIERS is event modifiers for button
10341 release. */
10343 void
10344 handle_tool_bar_click (f, x, y, down_p, modifiers)
10345 struct frame *f;
10346 int x, y, down_p;
10347 unsigned int modifiers;
10349 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10350 struct window *w = XWINDOW (f->tool_bar_window);
10351 int hpos, vpos, prop_idx;
10352 struct glyph *glyph;
10353 Lisp_Object enabled_p;
10355 /* If not on the highlighted tool-bar item, return. */
10356 frame_to_window_pixel_xy (w, &x, &y);
10357 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10358 return;
10360 /* If item is disabled, do nothing. */
10361 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10362 if (NILP (enabled_p))
10363 return;
10365 if (down_p)
10367 /* Show item in pressed state. */
10368 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10369 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10370 last_tool_bar_item = prop_idx;
10372 else
10374 Lisp_Object key, frame;
10375 struct input_event event;
10376 EVENT_INIT (event);
10378 /* Show item in released state. */
10379 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10380 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10382 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10384 XSETFRAME (frame, f);
10385 event.kind = TOOL_BAR_EVENT;
10386 event.frame_or_window = frame;
10387 event.arg = frame;
10388 kbd_buffer_store_event (&event);
10390 event.kind = TOOL_BAR_EVENT;
10391 event.frame_or_window = frame;
10392 event.arg = key;
10393 event.modifiers = modifiers;
10394 kbd_buffer_store_event (&event);
10395 last_tool_bar_item = -1;
10400 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10401 tool-bar window-relative coordinates X/Y. Called from
10402 note_mouse_highlight. */
10404 static void
10405 note_tool_bar_highlight (f, x, y)
10406 struct frame *f;
10407 int x, y;
10409 Lisp_Object window = f->tool_bar_window;
10410 struct window *w = XWINDOW (window);
10411 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10412 int hpos, vpos;
10413 struct glyph *glyph;
10414 struct glyph_row *row;
10415 int i;
10416 Lisp_Object enabled_p;
10417 int prop_idx;
10418 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10419 int mouse_down_p, rc;
10421 /* Function note_mouse_highlight is called with negative x(y
10422 values when mouse moves outside of the frame. */
10423 if (x <= 0 || y <= 0)
10425 clear_mouse_face (dpyinfo);
10426 return;
10429 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10430 if (rc < 0)
10432 /* Not on tool-bar item. */
10433 clear_mouse_face (dpyinfo);
10434 return;
10436 else if (rc == 0)
10437 /* On same tool-bar item as before. */
10438 goto set_help_echo;
10440 clear_mouse_face (dpyinfo);
10442 /* Mouse is down, but on different tool-bar item? */
10443 mouse_down_p = (dpyinfo->grabbed
10444 && f == last_mouse_frame
10445 && FRAME_LIVE_P (f));
10446 if (mouse_down_p
10447 && last_tool_bar_item != prop_idx)
10448 return;
10450 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10451 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10453 /* If tool-bar item is not enabled, don't highlight it. */
10454 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10455 if (!NILP (enabled_p))
10457 /* Compute the x-position of the glyph. In front and past the
10458 image is a space. We include this in the highlighted area. */
10459 row = MATRIX_ROW (w->current_matrix, vpos);
10460 for (i = x = 0; i < hpos; ++i)
10461 x += row->glyphs[TEXT_AREA][i].pixel_width;
10463 /* Record this as the current active region. */
10464 dpyinfo->mouse_face_beg_col = hpos;
10465 dpyinfo->mouse_face_beg_row = vpos;
10466 dpyinfo->mouse_face_beg_x = x;
10467 dpyinfo->mouse_face_beg_y = row->y;
10468 dpyinfo->mouse_face_past_end = 0;
10470 dpyinfo->mouse_face_end_col = hpos + 1;
10471 dpyinfo->mouse_face_end_row = vpos;
10472 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10473 dpyinfo->mouse_face_end_y = row->y;
10474 dpyinfo->mouse_face_window = window;
10475 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10477 /* Display it as active. */
10478 show_mouse_face (dpyinfo, draw);
10479 dpyinfo->mouse_face_image_state = draw;
10482 set_help_echo:
10484 /* Set help_echo_string to a help string to display for this tool-bar item.
10485 XTread_socket does the rest. */
10486 help_echo_object = help_echo_window = Qnil;
10487 help_echo_pos = -1;
10488 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10489 if (NILP (help_echo_string))
10490 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10493 #endif /* HAVE_WINDOW_SYSTEM */
10497 /************************************************************************
10498 Horizontal scrolling
10499 ************************************************************************/
10501 static int hscroll_window_tree P_ ((Lisp_Object));
10502 static int hscroll_windows P_ ((Lisp_Object));
10504 /* For all leaf windows in the window tree rooted at WINDOW, set their
10505 hscroll value so that PT is (i) visible in the window, and (ii) so
10506 that it is not within a certain margin at the window's left and
10507 right border. Value is non-zero if any window's hscroll has been
10508 changed. */
10510 static int
10511 hscroll_window_tree (window)
10512 Lisp_Object window;
10514 int hscrolled_p = 0;
10515 int hscroll_relative_p = FLOATP (Vhscroll_step);
10516 int hscroll_step_abs = 0;
10517 double hscroll_step_rel = 0;
10519 if (hscroll_relative_p)
10521 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10522 if (hscroll_step_rel < 0)
10524 hscroll_relative_p = 0;
10525 hscroll_step_abs = 0;
10528 else if (INTEGERP (Vhscroll_step))
10530 hscroll_step_abs = XINT (Vhscroll_step);
10531 if (hscroll_step_abs < 0)
10532 hscroll_step_abs = 0;
10534 else
10535 hscroll_step_abs = 0;
10537 while (WINDOWP (window))
10539 struct window *w = XWINDOW (window);
10541 if (WINDOWP (w->hchild))
10542 hscrolled_p |= hscroll_window_tree (w->hchild);
10543 else if (WINDOWP (w->vchild))
10544 hscrolled_p |= hscroll_window_tree (w->vchild);
10545 else if (w->cursor.vpos >= 0)
10547 int h_margin;
10548 int text_area_width;
10549 struct glyph_row *current_cursor_row
10550 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10551 struct glyph_row *desired_cursor_row
10552 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10553 struct glyph_row *cursor_row
10554 = (desired_cursor_row->enabled_p
10555 ? desired_cursor_row
10556 : current_cursor_row);
10558 text_area_width = window_box_width (w, TEXT_AREA);
10560 /* Scroll when cursor is inside this scroll margin. */
10561 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10563 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10564 && ((XFASTINT (w->hscroll)
10565 && w->cursor.x <= h_margin)
10566 || (cursor_row->enabled_p
10567 && cursor_row->truncated_on_right_p
10568 && (w->cursor.x >= text_area_width - h_margin))))
10570 struct it it;
10571 int hscroll;
10572 struct buffer *saved_current_buffer;
10573 int pt;
10574 int wanted_x;
10576 /* Find point in a display of infinite width. */
10577 saved_current_buffer = current_buffer;
10578 current_buffer = XBUFFER (w->buffer);
10580 if (w == XWINDOW (selected_window))
10581 pt = BUF_PT (current_buffer);
10582 else
10584 pt = marker_position (w->pointm);
10585 pt = max (BEGV, pt);
10586 pt = min (ZV, pt);
10589 /* Move iterator to pt starting at cursor_row->start in
10590 a line with infinite width. */
10591 init_to_row_start (&it, w, cursor_row);
10592 it.last_visible_x = INFINITY;
10593 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10594 current_buffer = saved_current_buffer;
10596 /* Position cursor in window. */
10597 if (!hscroll_relative_p && hscroll_step_abs == 0)
10598 hscroll = max (0, (it.current_x
10599 - (ITERATOR_AT_END_OF_LINE_P (&it)
10600 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10601 : (text_area_width / 2))))
10602 / FRAME_COLUMN_WIDTH (it.f);
10603 else if (w->cursor.x >= text_area_width - h_margin)
10605 if (hscroll_relative_p)
10606 wanted_x = text_area_width * (1 - hscroll_step_rel)
10607 - h_margin;
10608 else
10609 wanted_x = text_area_width
10610 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10611 - h_margin;
10612 hscroll
10613 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10615 else
10617 if (hscroll_relative_p)
10618 wanted_x = text_area_width * hscroll_step_rel
10619 + h_margin;
10620 else
10621 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10622 + h_margin;
10623 hscroll
10624 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10626 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10628 /* Don't call Fset_window_hscroll if value hasn't
10629 changed because it will prevent redisplay
10630 optimizations. */
10631 if (XFASTINT (w->hscroll) != hscroll)
10633 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10634 w->hscroll = make_number (hscroll);
10635 hscrolled_p = 1;
10640 window = w->next;
10643 /* Value is non-zero if hscroll of any leaf window has been changed. */
10644 return hscrolled_p;
10648 /* Set hscroll so that cursor is visible and not inside horizontal
10649 scroll margins for all windows in the tree rooted at WINDOW. See
10650 also hscroll_window_tree above. Value is non-zero if any window's
10651 hscroll has been changed. If it has, desired matrices on the frame
10652 of WINDOW are cleared. */
10654 static int
10655 hscroll_windows (window)
10656 Lisp_Object window;
10658 int hscrolled_p = hscroll_window_tree (window);
10659 if (hscrolled_p)
10660 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10661 return hscrolled_p;
10666 /************************************************************************
10667 Redisplay
10668 ************************************************************************/
10670 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10671 to a non-zero value. This is sometimes handy to have in a debugger
10672 session. */
10674 #if GLYPH_DEBUG
10676 /* First and last unchanged row for try_window_id. */
10678 int debug_first_unchanged_at_end_vpos;
10679 int debug_last_unchanged_at_beg_vpos;
10681 /* Delta vpos and y. */
10683 int debug_dvpos, debug_dy;
10685 /* Delta in characters and bytes for try_window_id. */
10687 int debug_delta, debug_delta_bytes;
10689 /* Values of window_end_pos and window_end_vpos at the end of
10690 try_window_id. */
10692 EMACS_INT debug_end_pos, debug_end_vpos;
10694 /* Append a string to W->desired_matrix->method. FMT is a printf
10695 format string. A1...A9 are a supplement for a variable-length
10696 argument list. If trace_redisplay_p is non-zero also printf the
10697 resulting string to stderr. */
10699 static void
10700 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10701 struct window *w;
10702 char *fmt;
10703 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10705 char buffer[512];
10706 char *method = w->desired_matrix->method;
10707 int len = strlen (method);
10708 int size = sizeof w->desired_matrix->method;
10709 int remaining = size - len - 1;
10711 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10712 if (len && remaining)
10714 method[len] = '|';
10715 --remaining, ++len;
10718 strncpy (method + len, buffer, remaining);
10720 if (trace_redisplay_p)
10721 fprintf (stderr, "%p (%s): %s\n",
10723 ((BUFFERP (w->buffer)
10724 && STRINGP (XBUFFER (w->buffer)->name))
10725 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10726 : "no buffer"),
10727 buffer);
10730 #endif /* GLYPH_DEBUG */
10733 /* Value is non-zero if all changes in window W, which displays
10734 current_buffer, are in the text between START and END. START is a
10735 buffer position, END is given as a distance from Z. Used in
10736 redisplay_internal for display optimization. */
10738 static INLINE int
10739 text_outside_line_unchanged_p (w, start, end)
10740 struct window *w;
10741 int start, end;
10743 int unchanged_p = 1;
10745 /* If text or overlays have changed, see where. */
10746 if (XFASTINT (w->last_modified) < MODIFF
10747 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10749 /* Gap in the line? */
10750 if (GPT < start || Z - GPT < end)
10751 unchanged_p = 0;
10753 /* Changes start in front of the line, or end after it? */
10754 if (unchanged_p
10755 && (BEG_UNCHANGED < start - 1
10756 || END_UNCHANGED < end))
10757 unchanged_p = 0;
10759 /* If selective display, can't optimize if changes start at the
10760 beginning of the line. */
10761 if (unchanged_p
10762 && INTEGERP (current_buffer->selective_display)
10763 && XINT (current_buffer->selective_display) > 0
10764 && (BEG_UNCHANGED < start || GPT <= start))
10765 unchanged_p = 0;
10767 /* If there are overlays at the start or end of the line, these
10768 may have overlay strings with newlines in them. A change at
10769 START, for instance, may actually concern the display of such
10770 overlay strings as well, and they are displayed on different
10771 lines. So, quickly rule out this case. (For the future, it
10772 might be desirable to implement something more telling than
10773 just BEG/END_UNCHANGED.) */
10774 if (unchanged_p)
10776 if (BEG + BEG_UNCHANGED == start
10777 && overlay_touches_p (start))
10778 unchanged_p = 0;
10779 if (END_UNCHANGED == end
10780 && overlay_touches_p (Z - end))
10781 unchanged_p = 0;
10785 return unchanged_p;
10789 /* Do a frame update, taking possible shortcuts into account. This is
10790 the main external entry point for redisplay.
10792 If the last redisplay displayed an echo area message and that message
10793 is no longer requested, we clear the echo area or bring back the
10794 mini-buffer if that is in use. */
10796 void
10797 redisplay ()
10799 redisplay_internal (0);
10803 static Lisp_Object
10804 overlay_arrow_string_or_property (var)
10805 Lisp_Object var;
10807 Lisp_Object val;
10809 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10810 return val;
10812 return Voverlay_arrow_string;
10815 /* Return 1 if there are any overlay-arrows in current_buffer. */
10816 static int
10817 overlay_arrow_in_current_buffer_p ()
10819 Lisp_Object vlist;
10821 for (vlist = Voverlay_arrow_variable_list;
10822 CONSP (vlist);
10823 vlist = XCDR (vlist))
10825 Lisp_Object var = XCAR (vlist);
10826 Lisp_Object val;
10828 if (!SYMBOLP (var))
10829 continue;
10830 val = find_symbol_value (var);
10831 if (MARKERP (val)
10832 && current_buffer == XMARKER (val)->buffer)
10833 return 1;
10835 return 0;
10839 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10840 has changed. */
10842 static int
10843 overlay_arrows_changed_p ()
10845 Lisp_Object vlist;
10847 for (vlist = Voverlay_arrow_variable_list;
10848 CONSP (vlist);
10849 vlist = XCDR (vlist))
10851 Lisp_Object var = XCAR (vlist);
10852 Lisp_Object val, pstr;
10854 if (!SYMBOLP (var))
10855 continue;
10856 val = find_symbol_value (var);
10857 if (!MARKERP (val))
10858 continue;
10859 if (! EQ (COERCE_MARKER (val),
10860 Fget (var, Qlast_arrow_position))
10861 || ! (pstr = overlay_arrow_string_or_property (var),
10862 EQ (pstr, Fget (var, Qlast_arrow_string))))
10863 return 1;
10865 return 0;
10868 /* Mark overlay arrows to be updated on next redisplay. */
10870 static void
10871 update_overlay_arrows (up_to_date)
10872 int up_to_date;
10874 Lisp_Object vlist;
10876 for (vlist = Voverlay_arrow_variable_list;
10877 CONSP (vlist);
10878 vlist = XCDR (vlist))
10880 Lisp_Object var = XCAR (vlist);
10882 if (!SYMBOLP (var))
10883 continue;
10885 if (up_to_date > 0)
10887 Lisp_Object val = find_symbol_value (var);
10888 Fput (var, Qlast_arrow_position,
10889 COERCE_MARKER (val));
10890 Fput (var, Qlast_arrow_string,
10891 overlay_arrow_string_or_property (var));
10893 else if (up_to_date < 0
10894 || !NILP (Fget (var, Qlast_arrow_position)))
10896 Fput (var, Qlast_arrow_position, Qt);
10897 Fput (var, Qlast_arrow_string, Qt);
10903 /* Return overlay arrow string to display at row.
10904 Return integer (bitmap number) for arrow bitmap in left fringe.
10905 Return nil if no overlay arrow. */
10907 static Lisp_Object
10908 overlay_arrow_at_row (it, row)
10909 struct it *it;
10910 struct glyph_row *row;
10912 Lisp_Object vlist;
10914 for (vlist = Voverlay_arrow_variable_list;
10915 CONSP (vlist);
10916 vlist = XCDR (vlist))
10918 Lisp_Object var = XCAR (vlist);
10919 Lisp_Object val;
10921 if (!SYMBOLP (var))
10922 continue;
10924 val = find_symbol_value (var);
10926 if (MARKERP (val)
10927 && current_buffer == XMARKER (val)->buffer
10928 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10930 if (FRAME_WINDOW_P (it->f)
10931 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10933 #ifdef HAVE_WINDOW_SYSTEM
10934 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10936 int fringe_bitmap;
10937 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10938 return make_number (fringe_bitmap);
10940 #endif
10941 return make_number (-1); /* Use default arrow bitmap */
10943 return overlay_arrow_string_or_property (var);
10947 return Qnil;
10950 /* Return 1 if point moved out of or into a composition. Otherwise
10951 return 0. PREV_BUF and PREV_PT are the last point buffer and
10952 position. BUF and PT are the current point buffer and position. */
10955 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10956 struct buffer *prev_buf, *buf;
10957 int prev_pt, pt;
10959 EMACS_INT start, end;
10960 Lisp_Object prop;
10961 Lisp_Object buffer;
10963 XSETBUFFER (buffer, buf);
10964 /* Check a composition at the last point if point moved within the
10965 same buffer. */
10966 if (prev_buf == buf)
10968 if (prev_pt == pt)
10969 /* Point didn't move. */
10970 return 0;
10972 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10973 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10974 && COMPOSITION_VALID_P (start, end, prop)
10975 && start < prev_pt && end > prev_pt)
10976 /* The last point was within the composition. Return 1 iff
10977 point moved out of the composition. */
10978 return (pt <= start || pt >= end);
10981 /* Check a composition at the current point. */
10982 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10983 && find_composition (pt, -1, &start, &end, &prop, buffer)
10984 && COMPOSITION_VALID_P (start, end, prop)
10985 && start < pt && end > pt);
10989 /* Reconsider the setting of B->clip_changed which is displayed
10990 in window W. */
10992 static INLINE void
10993 reconsider_clip_changes (w, b)
10994 struct window *w;
10995 struct buffer *b;
10997 if (b->clip_changed
10998 && !NILP (w->window_end_valid)
10999 && w->current_matrix->buffer == b
11000 && w->current_matrix->zv == BUF_ZV (b)
11001 && w->current_matrix->begv == BUF_BEGV (b))
11002 b->clip_changed = 0;
11004 /* If display wasn't paused, and W is not a tool bar window, see if
11005 point has been moved into or out of a composition. In that case,
11006 we set b->clip_changed to 1 to force updating the screen. If
11007 b->clip_changed has already been set to 1, we can skip this
11008 check. */
11009 if (!b->clip_changed
11010 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11012 int pt;
11014 if (w == XWINDOW (selected_window))
11015 pt = BUF_PT (current_buffer);
11016 else
11017 pt = marker_position (w->pointm);
11019 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11020 || pt != XINT (w->last_point))
11021 && check_point_in_composition (w->current_matrix->buffer,
11022 XINT (w->last_point),
11023 XBUFFER (w->buffer), pt))
11024 b->clip_changed = 1;
11029 /* Select FRAME to forward the values of frame-local variables into C
11030 variables so that the redisplay routines can access those values
11031 directly. */
11033 static void
11034 select_frame_for_redisplay (frame)
11035 Lisp_Object frame;
11037 Lisp_Object tail, sym, val;
11038 Lisp_Object old = selected_frame;
11040 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11042 selected_frame = frame;
11044 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11045 if (CONSP (XCAR (tail))
11046 && (sym = XCAR (XCAR (tail)),
11047 SYMBOLP (sym))
11048 && (sym = indirect_variable (sym),
11049 val = SYMBOL_VALUE (sym),
11050 (BUFFER_LOCAL_VALUEP (val)))
11051 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11052 /* Use find_symbol_value rather than Fsymbol_value
11053 to avoid an error if it is void. */
11054 find_symbol_value (sym);
11056 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
11057 if (CONSP (XCAR (tail))
11058 && (sym = XCAR (XCAR (tail)),
11059 SYMBOLP (sym))
11060 && (sym = indirect_variable (sym),
11061 val = SYMBOL_VALUE (sym),
11062 (BUFFER_LOCAL_VALUEP (val)))
11063 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11064 find_symbol_value (sym);
11068 #define STOP_POLLING \
11069 do { if (! polling_stopped_here) stop_polling (); \
11070 polling_stopped_here = 1; } while (0)
11072 #define RESUME_POLLING \
11073 do { if (polling_stopped_here) start_polling (); \
11074 polling_stopped_here = 0; } while (0)
11077 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11078 response to any user action; therefore, we should preserve the echo
11079 area. (Actually, our caller does that job.) Perhaps in the future
11080 avoid recentering windows if it is not necessary; currently that
11081 causes some problems. */
11083 static void
11084 redisplay_internal (preserve_echo_area)
11085 int preserve_echo_area;
11087 struct window *w = XWINDOW (selected_window);
11088 struct frame *f;
11089 int pause;
11090 int must_finish = 0;
11091 struct text_pos tlbufpos, tlendpos;
11092 int number_of_visible_frames;
11093 int count, count1;
11094 struct frame *sf;
11095 int polling_stopped_here = 0;
11096 Lisp_Object old_frame = selected_frame;
11098 /* Non-zero means redisplay has to consider all windows on all
11099 frames. Zero means, only selected_window is considered. */
11100 int consider_all_windows_p;
11102 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11104 /* No redisplay if running in batch mode or frame is not yet fully
11105 initialized, or redisplay is explicitly turned off by setting
11106 Vinhibit_redisplay. */
11107 if (noninteractive
11108 || !NILP (Vinhibit_redisplay))
11109 return;
11111 /* Don't examine these until after testing Vinhibit_redisplay.
11112 When Emacs is shutting down, perhaps because its connection to
11113 X has dropped, we should not look at them at all. */
11114 f = XFRAME (w->frame);
11115 sf = SELECTED_FRAME ();
11117 if (!f->glyphs_initialized_p)
11118 return;
11120 /* The flag redisplay_performed_directly_p is set by
11121 direct_output_for_insert when it already did the whole screen
11122 update necessary. */
11123 if (redisplay_performed_directly_p)
11125 redisplay_performed_directly_p = 0;
11126 if (!hscroll_windows (selected_window))
11127 return;
11130 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11131 if (popup_activated ())
11132 return;
11133 #endif
11135 /* I don't think this happens but let's be paranoid. */
11136 if (redisplaying_p)
11137 return;
11139 /* Record a function that resets redisplaying_p to its old value
11140 when we leave this function. */
11141 count = SPECPDL_INDEX ();
11142 record_unwind_protect (unwind_redisplay,
11143 Fcons (make_number (redisplaying_p), selected_frame));
11144 ++redisplaying_p;
11145 specbind (Qinhibit_free_realized_faces, Qnil);
11148 Lisp_Object tail, frame;
11150 FOR_EACH_FRAME (tail, frame)
11152 struct frame *f = XFRAME (frame);
11153 f->already_hscrolled_p = 0;
11157 retry:
11158 if (!EQ (old_frame, selected_frame)
11159 && FRAME_LIVE_P (XFRAME (old_frame)))
11160 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11161 selected_frame and selected_window to be temporarily out-of-sync so
11162 when we come back here via `goto retry', we need to resync because we
11163 may need to run Elisp code (via prepare_menu_bars). */
11164 select_frame_for_redisplay (old_frame);
11166 pause = 0;
11167 reconsider_clip_changes (w, current_buffer);
11168 last_escape_glyph_frame = NULL;
11169 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11171 /* If new fonts have been loaded that make a glyph matrix adjustment
11172 necessary, do it. */
11173 if (fonts_changed_p)
11175 adjust_glyphs (NULL);
11176 ++windows_or_buffers_changed;
11177 fonts_changed_p = 0;
11180 /* If face_change_count is non-zero, init_iterator will free all
11181 realized faces, which includes the faces referenced from current
11182 matrices. So, we can't reuse current matrices in this case. */
11183 if (face_change_count)
11184 ++windows_or_buffers_changed;
11186 if (FRAME_TERMCAP_P (sf)
11187 && FRAME_TTY (sf)->previous_frame != sf)
11189 /* Since frames on a single ASCII terminal share the same
11190 display area, displaying a different frame means redisplay
11191 the whole thing. */
11192 windows_or_buffers_changed++;
11193 SET_FRAME_GARBAGED (sf);
11194 FRAME_TTY (sf)->previous_frame = sf;
11197 /* Set the visible flags for all frames. Do this before checking
11198 for resized or garbaged frames; they want to know if their frames
11199 are visible. See the comment in frame.h for
11200 FRAME_SAMPLE_VISIBILITY. */
11202 Lisp_Object tail, frame;
11204 number_of_visible_frames = 0;
11206 FOR_EACH_FRAME (tail, frame)
11208 struct frame *f = XFRAME (frame);
11210 FRAME_SAMPLE_VISIBILITY (f);
11211 if (FRAME_VISIBLE_P (f))
11212 ++number_of_visible_frames;
11213 clear_desired_matrices (f);
11218 /* Notice any pending interrupt request to change frame size. */
11219 do_pending_window_change (1);
11221 /* Clear frames marked as garbaged. */
11222 if (frame_garbaged)
11223 clear_garbaged_frames ();
11225 /* Build menubar and tool-bar items. */
11226 if (NILP (Vmemory_full))
11227 prepare_menu_bars ();
11229 if (windows_or_buffers_changed)
11230 update_mode_lines++;
11232 /* Detect case that we need to write or remove a star in the mode line. */
11233 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11235 w->update_mode_line = Qt;
11236 if (buffer_shared > 1)
11237 update_mode_lines++;
11240 /* Avoid invocation of point motion hooks by `current_column' below. */
11241 count1 = SPECPDL_INDEX ();
11242 specbind (Qinhibit_point_motion_hooks, Qt);
11244 /* If %c is in the mode line, update it if needed. */
11245 if (!NILP (w->column_number_displayed)
11246 /* This alternative quickly identifies a common case
11247 where no change is needed. */
11248 && !(PT == XFASTINT (w->last_point)
11249 && XFASTINT (w->last_modified) >= MODIFF
11250 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11251 && (XFASTINT (w->column_number_displayed)
11252 != (int) current_column ())) /* iftc */
11253 w->update_mode_line = Qt;
11255 unbind_to (count1, Qnil);
11257 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11259 /* The variable buffer_shared is set in redisplay_window and
11260 indicates that we redisplay a buffer in different windows. See
11261 there. */
11262 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11263 || cursor_type_changed);
11265 /* If specs for an arrow have changed, do thorough redisplay
11266 to ensure we remove any arrow that should no longer exist. */
11267 if (overlay_arrows_changed_p ())
11268 consider_all_windows_p = windows_or_buffers_changed = 1;
11270 /* Normally the message* functions will have already displayed and
11271 updated the echo area, but the frame may have been trashed, or
11272 the update may have been preempted, so display the echo area
11273 again here. Checking message_cleared_p captures the case that
11274 the echo area should be cleared. */
11275 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11276 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11277 || (message_cleared_p
11278 && minibuf_level == 0
11279 /* If the mini-window is currently selected, this means the
11280 echo-area doesn't show through. */
11281 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11283 int window_height_changed_p = echo_area_display (0);
11284 must_finish = 1;
11286 /* If we don't display the current message, don't clear the
11287 message_cleared_p flag, because, if we did, we wouldn't clear
11288 the echo area in the next redisplay which doesn't preserve
11289 the echo area. */
11290 if (!display_last_displayed_message_p)
11291 message_cleared_p = 0;
11293 if (fonts_changed_p)
11294 goto retry;
11295 else if (window_height_changed_p)
11297 consider_all_windows_p = 1;
11298 ++update_mode_lines;
11299 ++windows_or_buffers_changed;
11301 /* If window configuration was changed, frames may have been
11302 marked garbaged. Clear them or we will experience
11303 surprises wrt scrolling. */
11304 if (frame_garbaged)
11305 clear_garbaged_frames ();
11308 else if (EQ (selected_window, minibuf_window)
11309 && (current_buffer->clip_changed
11310 || XFASTINT (w->last_modified) < MODIFF
11311 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11312 && resize_mini_window (w, 0))
11314 /* Resized active mini-window to fit the size of what it is
11315 showing if its contents might have changed. */
11316 must_finish = 1;
11317 consider_all_windows_p = 1;
11318 ++windows_or_buffers_changed;
11319 ++update_mode_lines;
11321 /* If window configuration was changed, frames may have been
11322 marked garbaged. Clear them or we will experience
11323 surprises wrt scrolling. */
11324 if (frame_garbaged)
11325 clear_garbaged_frames ();
11329 /* If showing the region, and mark has changed, we must redisplay
11330 the whole window. The assignment to this_line_start_pos prevents
11331 the optimization directly below this if-statement. */
11332 if (((!NILP (Vtransient_mark_mode)
11333 && !NILP (XBUFFER (w->buffer)->mark_active))
11334 != !NILP (w->region_showing))
11335 || (!NILP (w->region_showing)
11336 && !EQ (w->region_showing,
11337 Fmarker_position (XBUFFER (w->buffer)->mark))))
11338 CHARPOS (this_line_start_pos) = 0;
11340 /* Optimize the case that only the line containing the cursor in the
11341 selected window has changed. Variables starting with this_ are
11342 set in display_line and record information about the line
11343 containing the cursor. */
11344 tlbufpos = this_line_start_pos;
11345 tlendpos = this_line_end_pos;
11346 if (!consider_all_windows_p
11347 && CHARPOS (tlbufpos) > 0
11348 && NILP (w->update_mode_line)
11349 && !current_buffer->clip_changed
11350 && !current_buffer->prevent_redisplay_optimizations_p
11351 && FRAME_VISIBLE_P (XFRAME (w->frame))
11352 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11353 /* Make sure recorded data applies to current buffer, etc. */
11354 && this_line_buffer == current_buffer
11355 && current_buffer == XBUFFER (w->buffer)
11356 && NILP (w->force_start)
11357 && NILP (w->optional_new_start)
11358 /* Point must be on the line that we have info recorded about. */
11359 && PT >= CHARPOS (tlbufpos)
11360 && PT <= Z - CHARPOS (tlendpos)
11361 /* All text outside that line, including its final newline,
11362 must be unchanged */
11363 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11364 CHARPOS (tlendpos)))
11366 if (CHARPOS (tlbufpos) > BEGV
11367 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11368 && (CHARPOS (tlbufpos) == ZV
11369 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11370 /* Former continuation line has disappeared by becoming empty */
11371 goto cancel;
11372 else if (XFASTINT (w->last_modified) < MODIFF
11373 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11374 || MINI_WINDOW_P (w))
11376 /* We have to handle the case of continuation around a
11377 wide-column character (See the comment in indent.c around
11378 line 885).
11380 For instance, in the following case:
11382 -------- Insert --------
11383 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11384 J_I_ ==> J_I_ `^^' are cursors.
11385 ^^ ^^
11386 -------- --------
11388 As we have to redraw the line above, we should goto cancel. */
11390 struct it it;
11391 int line_height_before = this_line_pixel_height;
11393 /* Note that start_display will handle the case that the
11394 line starting at tlbufpos is a continuation lines. */
11395 start_display (&it, w, tlbufpos);
11397 /* Implementation note: It this still necessary? */
11398 if (it.current_x != this_line_start_x)
11399 goto cancel;
11401 TRACE ((stderr, "trying display optimization 1\n"));
11402 w->cursor.vpos = -1;
11403 overlay_arrow_seen = 0;
11404 it.vpos = this_line_vpos;
11405 it.current_y = this_line_y;
11406 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11407 display_line (&it);
11409 /* If line contains point, is not continued,
11410 and ends at same distance from eob as before, we win */
11411 if (w->cursor.vpos >= 0
11412 /* Line is not continued, otherwise this_line_start_pos
11413 would have been set to 0 in display_line. */
11414 && CHARPOS (this_line_start_pos)
11415 /* Line ends as before. */
11416 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11417 /* Line has same height as before. Otherwise other lines
11418 would have to be shifted up or down. */
11419 && this_line_pixel_height == line_height_before)
11421 /* If this is not the window's last line, we must adjust
11422 the charstarts of the lines below. */
11423 if (it.current_y < it.last_visible_y)
11425 struct glyph_row *row
11426 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11427 int delta, delta_bytes;
11429 if (Z - CHARPOS (tlendpos) == ZV)
11431 /* This line ends at end of (accessible part of)
11432 buffer. There is no newline to count. */
11433 delta = (Z
11434 - CHARPOS (tlendpos)
11435 - MATRIX_ROW_START_CHARPOS (row));
11436 delta_bytes = (Z_BYTE
11437 - BYTEPOS (tlendpos)
11438 - MATRIX_ROW_START_BYTEPOS (row));
11440 else
11442 /* This line ends in a newline. Must take
11443 account of the newline and the rest of the
11444 text that follows. */
11445 delta = (Z
11446 - CHARPOS (tlendpos)
11447 - MATRIX_ROW_START_CHARPOS (row));
11448 delta_bytes = (Z_BYTE
11449 - BYTEPOS (tlendpos)
11450 - MATRIX_ROW_START_BYTEPOS (row));
11453 increment_matrix_positions (w->current_matrix,
11454 this_line_vpos + 1,
11455 w->current_matrix->nrows,
11456 delta, delta_bytes);
11459 /* If this row displays text now but previously didn't,
11460 or vice versa, w->window_end_vpos may have to be
11461 adjusted. */
11462 if ((it.glyph_row - 1)->displays_text_p)
11464 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11465 XSETINT (w->window_end_vpos, this_line_vpos);
11467 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11468 && this_line_vpos > 0)
11469 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11470 w->window_end_valid = Qnil;
11472 /* Update hint: No need to try to scroll in update_window. */
11473 w->desired_matrix->no_scrolling_p = 1;
11475 #if GLYPH_DEBUG
11476 *w->desired_matrix->method = 0;
11477 debug_method_add (w, "optimization 1");
11478 #endif
11479 #ifdef HAVE_WINDOW_SYSTEM
11480 update_window_fringes (w, 0);
11481 #endif
11482 goto update;
11484 else
11485 goto cancel;
11487 else if (/* Cursor position hasn't changed. */
11488 PT == XFASTINT (w->last_point)
11489 /* Make sure the cursor was last displayed
11490 in this window. Otherwise we have to reposition it. */
11491 && 0 <= w->cursor.vpos
11492 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11494 if (!must_finish)
11496 do_pending_window_change (1);
11498 /* We used to always goto end_of_redisplay here, but this
11499 isn't enough if we have a blinking cursor. */
11500 if (w->cursor_off_p == w->last_cursor_off_p)
11501 goto end_of_redisplay;
11503 goto update;
11505 /* If highlighting the region, or if the cursor is in the echo area,
11506 then we can't just move the cursor. */
11507 else if (! (!NILP (Vtransient_mark_mode)
11508 && !NILP (current_buffer->mark_active))
11509 && (EQ (selected_window, current_buffer->last_selected_window)
11510 || highlight_nonselected_windows)
11511 && NILP (w->region_showing)
11512 && NILP (Vshow_trailing_whitespace)
11513 && !cursor_in_echo_area)
11515 struct it it;
11516 struct glyph_row *row;
11518 /* Skip from tlbufpos to PT and see where it is. Note that
11519 PT may be in invisible text. If so, we will end at the
11520 next visible position. */
11521 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11522 NULL, DEFAULT_FACE_ID);
11523 it.current_x = this_line_start_x;
11524 it.current_y = this_line_y;
11525 it.vpos = this_line_vpos;
11527 /* The call to move_it_to stops in front of PT, but
11528 moves over before-strings. */
11529 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11531 if (it.vpos == this_line_vpos
11532 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11533 row->enabled_p))
11535 xassert (this_line_vpos == it.vpos);
11536 xassert (this_line_y == it.current_y);
11537 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11538 #if GLYPH_DEBUG
11539 *w->desired_matrix->method = 0;
11540 debug_method_add (w, "optimization 3");
11541 #endif
11542 goto update;
11544 else
11545 goto cancel;
11548 cancel:
11549 /* Text changed drastically or point moved off of line. */
11550 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11553 CHARPOS (this_line_start_pos) = 0;
11554 consider_all_windows_p |= buffer_shared > 1;
11555 ++clear_face_cache_count;
11556 #ifdef HAVE_WINDOW_SYSTEM
11557 ++clear_image_cache_count;
11558 #endif
11560 /* Build desired matrices, and update the display. If
11561 consider_all_windows_p is non-zero, do it for all windows on all
11562 frames. Otherwise do it for selected_window, only. */
11564 if (consider_all_windows_p)
11566 Lisp_Object tail, frame;
11568 FOR_EACH_FRAME (tail, frame)
11569 XFRAME (frame)->updated_p = 0;
11571 /* Recompute # windows showing selected buffer. This will be
11572 incremented each time such a window is displayed. */
11573 buffer_shared = 0;
11575 FOR_EACH_FRAME (tail, frame)
11577 struct frame *f = XFRAME (frame);
11579 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11581 if (! EQ (frame, selected_frame))
11582 /* Select the frame, for the sake of frame-local
11583 variables. */
11584 select_frame_for_redisplay (frame);
11586 /* Mark all the scroll bars to be removed; we'll redeem
11587 the ones we want when we redisplay their windows. */
11588 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11589 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11591 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11592 redisplay_windows (FRAME_ROOT_WINDOW (f));
11594 /* Any scroll bars which redisplay_windows should have
11595 nuked should now go away. */
11596 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11597 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11599 /* If fonts changed, display again. */
11600 /* ??? rms: I suspect it is a mistake to jump all the way
11601 back to retry here. It should just retry this frame. */
11602 if (fonts_changed_p)
11603 goto retry;
11605 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11607 /* See if we have to hscroll. */
11608 if (!f->already_hscrolled_p)
11610 f->already_hscrolled_p = 1;
11611 if (hscroll_windows (f->root_window))
11612 goto retry;
11615 /* Prevent various kinds of signals during display
11616 update. stdio is not robust about handling
11617 signals, which can cause an apparent I/O
11618 error. */
11619 if (interrupt_input)
11620 unrequest_sigio ();
11621 STOP_POLLING;
11623 /* Update the display. */
11624 set_window_update_flags (XWINDOW (f->root_window), 1);
11625 pause |= update_frame (f, 0, 0);
11626 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11627 if (pause)
11628 break;
11629 #endif
11631 f->updated_p = 1;
11636 if (!pause)
11638 /* Do the mark_window_display_accurate after all windows have
11639 been redisplayed because this call resets flags in buffers
11640 which are needed for proper redisplay. */
11641 FOR_EACH_FRAME (tail, frame)
11643 struct frame *f = XFRAME (frame);
11644 if (f->updated_p)
11646 mark_window_display_accurate (f->root_window, 1);
11647 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11648 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11653 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11655 Lisp_Object mini_window;
11656 struct frame *mini_frame;
11658 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11659 /* Use list_of_error, not Qerror, so that
11660 we catch only errors and don't run the debugger. */
11661 internal_condition_case_1 (redisplay_window_1, selected_window,
11662 list_of_error,
11663 redisplay_window_error);
11665 /* Compare desired and current matrices, perform output. */
11667 update:
11668 /* If fonts changed, display again. */
11669 if (fonts_changed_p)
11670 goto retry;
11672 /* Prevent various kinds of signals during display update.
11673 stdio is not robust about handling signals,
11674 which can cause an apparent I/O error. */
11675 if (interrupt_input)
11676 unrequest_sigio ();
11677 STOP_POLLING;
11679 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11681 if (hscroll_windows (selected_window))
11682 goto retry;
11684 XWINDOW (selected_window)->must_be_updated_p = 1;
11685 pause = update_frame (sf, 0, 0);
11688 /* We may have called echo_area_display at the top of this
11689 function. If the echo area is on another frame, that may
11690 have put text on a frame other than the selected one, so the
11691 above call to update_frame would not have caught it. Catch
11692 it here. */
11693 mini_window = FRAME_MINIBUF_WINDOW (sf);
11694 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11696 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11698 XWINDOW (mini_window)->must_be_updated_p = 1;
11699 pause |= update_frame (mini_frame, 0, 0);
11700 if (!pause && hscroll_windows (mini_window))
11701 goto retry;
11705 /* If display was paused because of pending input, make sure we do a
11706 thorough update the next time. */
11707 if (pause)
11709 /* Prevent the optimization at the beginning of
11710 redisplay_internal that tries a single-line update of the
11711 line containing the cursor in the selected window. */
11712 CHARPOS (this_line_start_pos) = 0;
11714 /* Let the overlay arrow be updated the next time. */
11715 update_overlay_arrows (0);
11717 /* If we pause after scrolling, some rows in the current
11718 matrices of some windows are not valid. */
11719 if (!WINDOW_FULL_WIDTH_P (w)
11720 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11721 update_mode_lines = 1;
11723 else
11725 if (!consider_all_windows_p)
11727 /* This has already been done above if
11728 consider_all_windows_p is set. */
11729 mark_window_display_accurate_1 (w, 1);
11731 /* Say overlay arrows are up to date. */
11732 update_overlay_arrows (1);
11734 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11735 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11738 update_mode_lines = 0;
11739 windows_or_buffers_changed = 0;
11740 cursor_type_changed = 0;
11743 /* Start SIGIO interrupts coming again. Having them off during the
11744 code above makes it less likely one will discard output, but not
11745 impossible, since there might be stuff in the system buffer here.
11746 But it is much hairier to try to do anything about that. */
11747 if (interrupt_input)
11748 request_sigio ();
11749 RESUME_POLLING;
11751 /* If a frame has become visible which was not before, redisplay
11752 again, so that we display it. Expose events for such a frame
11753 (which it gets when becoming visible) don't call the parts of
11754 redisplay constructing glyphs, so simply exposing a frame won't
11755 display anything in this case. So, we have to display these
11756 frames here explicitly. */
11757 if (!pause)
11759 Lisp_Object tail, frame;
11760 int new_count = 0;
11762 FOR_EACH_FRAME (tail, frame)
11764 int this_is_visible = 0;
11766 if (XFRAME (frame)->visible)
11767 this_is_visible = 1;
11768 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11769 if (XFRAME (frame)->visible)
11770 this_is_visible = 1;
11772 if (this_is_visible)
11773 new_count++;
11776 if (new_count != number_of_visible_frames)
11777 windows_or_buffers_changed++;
11780 /* Change frame size now if a change is pending. */
11781 do_pending_window_change (1);
11783 /* If we just did a pending size change, or have additional
11784 visible frames, redisplay again. */
11785 if (windows_or_buffers_changed && !pause)
11786 goto retry;
11788 /* Clear the face cache eventually. */
11789 if (consider_all_windows_p)
11791 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11793 clear_face_cache (0);
11794 clear_face_cache_count = 0;
11796 #ifdef HAVE_WINDOW_SYSTEM
11797 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11799 Lisp_Object tail, frame;
11800 FOR_EACH_FRAME (tail, frame)
11802 struct frame *f = XFRAME (frame);
11803 if (FRAME_WINDOW_P (f))
11804 clear_image_cache (f, 0);
11806 clear_image_cache_count = 0;
11808 #endif /* HAVE_WINDOW_SYSTEM */
11811 end_of_redisplay:
11812 unbind_to (count, Qnil);
11813 RESUME_POLLING;
11817 /* Redisplay, but leave alone any recent echo area message unless
11818 another message has been requested in its place.
11820 This is useful in situations where you need to redisplay but no
11821 user action has occurred, making it inappropriate for the message
11822 area to be cleared. See tracking_off and
11823 wait_reading_process_output for examples of these situations.
11825 FROM_WHERE is an integer saying from where this function was
11826 called. This is useful for debugging. */
11828 void
11829 redisplay_preserve_echo_area (from_where)
11830 int from_where;
11832 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11834 if (!NILP (echo_area_buffer[1]))
11836 /* We have a previously displayed message, but no current
11837 message. Redisplay the previous message. */
11838 display_last_displayed_message_p = 1;
11839 redisplay_internal (1);
11840 display_last_displayed_message_p = 0;
11842 else
11843 redisplay_internal (1);
11845 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11846 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11847 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11851 /* Function registered with record_unwind_protect in
11852 redisplay_internal. Reset redisplaying_p to the value it had
11853 before redisplay_internal was called, and clear
11854 prevent_freeing_realized_faces_p. It also selects the previously
11855 selected frame, unless it has been deleted (by an X connection
11856 failure during redisplay, for example). */
11858 static Lisp_Object
11859 unwind_redisplay (val)
11860 Lisp_Object val;
11862 Lisp_Object old_redisplaying_p, old_frame;
11864 old_redisplaying_p = XCAR (val);
11865 redisplaying_p = XFASTINT (old_redisplaying_p);
11866 old_frame = XCDR (val);
11867 if (! EQ (old_frame, selected_frame)
11868 && FRAME_LIVE_P (XFRAME (old_frame)))
11869 select_frame_for_redisplay (old_frame);
11870 return Qnil;
11874 /* Mark the display of window W as accurate or inaccurate. If
11875 ACCURATE_P is non-zero mark display of W as accurate. If
11876 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11877 redisplay_internal is called. */
11879 static void
11880 mark_window_display_accurate_1 (w, accurate_p)
11881 struct window *w;
11882 int accurate_p;
11884 if (BUFFERP (w->buffer))
11886 struct buffer *b = XBUFFER (w->buffer);
11888 w->last_modified
11889 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11890 w->last_overlay_modified
11891 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11892 w->last_had_star
11893 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11895 if (accurate_p)
11897 b->clip_changed = 0;
11898 b->prevent_redisplay_optimizations_p = 0;
11900 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11901 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11902 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11903 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11905 w->current_matrix->buffer = b;
11906 w->current_matrix->begv = BUF_BEGV (b);
11907 w->current_matrix->zv = BUF_ZV (b);
11909 w->last_cursor = w->cursor;
11910 w->last_cursor_off_p = w->cursor_off_p;
11912 if (w == XWINDOW (selected_window))
11913 w->last_point = make_number (BUF_PT (b));
11914 else
11915 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11919 if (accurate_p)
11921 w->window_end_valid = w->buffer;
11922 #if 0 /* This is incorrect with variable-height lines. */
11923 xassert (XINT (w->window_end_vpos)
11924 < (WINDOW_TOTAL_LINES (w)
11925 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11926 #endif
11927 w->update_mode_line = Qnil;
11932 /* Mark the display of windows in the window tree rooted at WINDOW as
11933 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11934 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11935 be redisplayed the next time redisplay_internal is called. */
11937 void
11938 mark_window_display_accurate (window, accurate_p)
11939 Lisp_Object window;
11940 int accurate_p;
11942 struct window *w;
11944 for (; !NILP (window); window = w->next)
11946 w = XWINDOW (window);
11947 mark_window_display_accurate_1 (w, accurate_p);
11949 if (!NILP (w->vchild))
11950 mark_window_display_accurate (w->vchild, accurate_p);
11951 if (!NILP (w->hchild))
11952 mark_window_display_accurate (w->hchild, accurate_p);
11955 if (accurate_p)
11957 update_overlay_arrows (1);
11959 else
11961 /* Force a thorough redisplay the next time by setting
11962 last_arrow_position and last_arrow_string to t, which is
11963 unequal to any useful value of Voverlay_arrow_... */
11964 update_overlay_arrows (-1);
11969 /* Return value in display table DP (Lisp_Char_Table *) for character
11970 C. Since a display table doesn't have any parent, we don't have to
11971 follow parent. Do not call this function directly but use the
11972 macro DISP_CHAR_VECTOR. */
11974 Lisp_Object
11975 disp_char_vector (dp, c)
11976 struct Lisp_Char_Table *dp;
11977 int c;
11979 Lisp_Object val;
11981 if (ASCII_CHAR_P (c))
11983 val = dp->ascii;
11984 if (SUB_CHAR_TABLE_P (val))
11985 val = XSUB_CHAR_TABLE (val)->contents[c];
11987 else
11989 Lisp_Object table;
11991 XSETCHAR_TABLE (table, dp);
11992 val = char_table_ref (table, c);
11994 if (NILP (val))
11995 val = dp->defalt;
11996 return val;
12001 /***********************************************************************
12002 Window Redisplay
12003 ***********************************************************************/
12005 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12007 static void
12008 redisplay_windows (window)
12009 Lisp_Object window;
12011 while (!NILP (window))
12013 struct window *w = XWINDOW (window);
12015 if (!NILP (w->hchild))
12016 redisplay_windows (w->hchild);
12017 else if (!NILP (w->vchild))
12018 redisplay_windows (w->vchild);
12019 else
12021 displayed_buffer = XBUFFER (w->buffer);
12022 /* Use list_of_error, not Qerror, so that
12023 we catch only errors and don't run the debugger. */
12024 internal_condition_case_1 (redisplay_window_0, window,
12025 list_of_error,
12026 redisplay_window_error);
12029 window = w->next;
12033 static Lisp_Object
12034 redisplay_window_error ()
12036 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12037 return Qnil;
12040 static Lisp_Object
12041 redisplay_window_0 (window)
12042 Lisp_Object window;
12044 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12045 redisplay_window (window, 0);
12046 return Qnil;
12049 static Lisp_Object
12050 redisplay_window_1 (window)
12051 Lisp_Object window;
12053 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12054 redisplay_window (window, 1);
12055 return Qnil;
12059 /* Increment GLYPH until it reaches END or CONDITION fails while
12060 adding (GLYPH)->pixel_width to X. */
12062 #define SKIP_GLYPHS(glyph, end, x, condition) \
12063 do \
12065 (x) += (glyph)->pixel_width; \
12066 ++(glyph); \
12068 while ((glyph) < (end) && (condition))
12071 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12072 DELTA is the number of bytes by which positions recorded in ROW
12073 differ from current buffer positions.
12075 Return 0 if cursor is not on this row. 1 otherwise. */
12078 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12079 struct window *w;
12080 struct glyph_row *row;
12081 struct glyph_matrix *matrix;
12082 int delta, delta_bytes, dy, dvpos;
12084 struct glyph *glyph = row->glyphs[TEXT_AREA];
12085 struct glyph *end = glyph + row->used[TEXT_AREA];
12086 struct glyph *cursor = NULL;
12087 /* The first glyph that starts a sequence of glyphs from string. */
12088 struct glyph *string_start;
12089 /* The X coordinate of string_start. */
12090 int string_start_x;
12091 /* The last known character position. */
12092 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12093 /* The last known character position before string_start. */
12094 int string_before_pos;
12095 int x = row->x;
12096 int cursor_x = x;
12097 int cursor_from_overlay_pos = 0;
12098 int pt_old = PT - delta;
12100 /* Skip over glyphs not having an object at the start of the row.
12101 These are special glyphs like truncation marks on terminal
12102 frames. */
12103 if (row->displays_text_p)
12104 while (glyph < end
12105 && INTEGERP (glyph->object)
12106 && glyph->charpos < 0)
12108 x += glyph->pixel_width;
12109 ++glyph;
12112 string_start = NULL;
12113 while (glyph < end
12114 && !INTEGERP (glyph->object)
12115 && (!BUFFERP (glyph->object)
12116 || (last_pos = glyph->charpos) < pt_old))
12118 if (! STRINGP (glyph->object))
12120 string_start = NULL;
12121 x += glyph->pixel_width;
12122 ++glyph;
12123 if (cursor_from_overlay_pos
12124 && last_pos >= cursor_from_overlay_pos)
12126 cursor_from_overlay_pos = 0;
12127 cursor = 0;
12130 else
12132 if (string_start == NULL)
12134 string_before_pos = last_pos;
12135 string_start = glyph;
12136 string_start_x = x;
12138 /* Skip all glyphs from string. */
12141 Lisp_Object cprop;
12142 int pos;
12143 if ((cursor == NULL || glyph > cursor)
12144 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12145 Qcursor, (glyph)->object),
12146 !NILP (cprop))
12147 && (pos = string_buffer_position (w, glyph->object,
12148 string_before_pos),
12149 (pos == 0 /* From overlay */
12150 || pos == pt_old)))
12152 /* Estimate overlay buffer position from the buffer
12153 positions of the glyphs before and after the overlay.
12154 Add 1 to last_pos so that if point corresponds to the
12155 glyph right after the overlay, we still use a 'cursor'
12156 property found in that overlay. */
12157 cursor_from_overlay_pos = (pos ? 0 : last_pos
12158 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12159 cursor = glyph;
12160 cursor_x = x;
12162 x += glyph->pixel_width;
12163 ++glyph;
12165 while (glyph < end && EQ (glyph->object, string_start->object));
12169 if (cursor != NULL)
12171 glyph = cursor;
12172 x = cursor_x;
12174 else if (row->ends_in_ellipsis_p && glyph == end)
12176 /* Scan back over the ellipsis glyphs, decrementing positions. */
12177 while (glyph > row->glyphs[TEXT_AREA]
12178 && (glyph - 1)->charpos == last_pos)
12179 glyph--, x -= glyph->pixel_width;
12180 /* That loop always goes one position too far,
12181 including the glyph before the ellipsis.
12182 So scan forward over that one. */
12183 x += glyph->pixel_width;
12184 glyph++;
12186 else if (string_start
12187 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12189 /* We may have skipped over point because the previous glyphs
12190 are from string. As there's no easy way to know the
12191 character position of the current glyph, find the correct
12192 glyph on point by scanning from string_start again. */
12193 Lisp_Object limit;
12194 Lisp_Object string;
12195 struct glyph *stop = glyph;
12196 int pos;
12198 limit = make_number (pt_old + 1);
12199 glyph = string_start;
12200 x = string_start_x;
12201 string = glyph->object;
12202 pos = string_buffer_position (w, string, string_before_pos);
12203 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12204 because we always put cursor after overlay strings. */
12205 while (pos == 0 && glyph < stop)
12207 string = glyph->object;
12208 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12209 if (glyph < stop)
12210 pos = string_buffer_position (w, glyph->object, string_before_pos);
12213 while (glyph < stop)
12215 pos = XINT (Fnext_single_char_property_change
12216 (make_number (pos), Qdisplay, Qnil, limit));
12217 if (pos > pt_old)
12218 break;
12219 /* Skip glyphs from the same string. */
12220 string = glyph->object;
12221 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12222 /* Skip glyphs from an overlay. */
12223 while (glyph < stop
12224 && ! string_buffer_position (w, glyph->object, pos))
12226 string = glyph->object;
12227 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12231 /* If we reached the end of the line, and end was from a string,
12232 cursor is not on this line. */
12233 if (glyph == end && row->continued_p)
12234 return 0;
12237 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12238 w->cursor.x = x;
12239 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12240 w->cursor.y = row->y + dy;
12242 if (w == XWINDOW (selected_window))
12244 if (!row->continued_p
12245 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12246 && row->x == 0)
12248 this_line_buffer = XBUFFER (w->buffer);
12250 CHARPOS (this_line_start_pos)
12251 = MATRIX_ROW_START_CHARPOS (row) + delta;
12252 BYTEPOS (this_line_start_pos)
12253 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12255 CHARPOS (this_line_end_pos)
12256 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12257 BYTEPOS (this_line_end_pos)
12258 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12260 this_line_y = w->cursor.y;
12261 this_line_pixel_height = row->height;
12262 this_line_vpos = w->cursor.vpos;
12263 this_line_start_x = row->x;
12265 else
12266 CHARPOS (this_line_start_pos) = 0;
12269 return 1;
12273 /* Run window scroll functions, if any, for WINDOW with new window
12274 start STARTP. Sets the window start of WINDOW to that position.
12276 We assume that the window's buffer is really current. */
12278 static INLINE struct text_pos
12279 run_window_scroll_functions (window, startp)
12280 Lisp_Object window;
12281 struct text_pos startp;
12283 struct window *w = XWINDOW (window);
12284 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12286 if (current_buffer != XBUFFER (w->buffer))
12287 abort ();
12289 if (!NILP (Vwindow_scroll_functions))
12291 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12292 make_number (CHARPOS (startp)));
12293 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12294 /* In case the hook functions switch buffers. */
12295 if (current_buffer != XBUFFER (w->buffer))
12296 set_buffer_internal_1 (XBUFFER (w->buffer));
12299 return startp;
12303 /* Make sure the line containing the cursor is fully visible.
12304 A value of 1 means there is nothing to be done.
12305 (Either the line is fully visible, or it cannot be made so,
12306 or we cannot tell.)
12308 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12309 is higher than window.
12311 A value of 0 means the caller should do scrolling
12312 as if point had gone off the screen. */
12314 static int
12315 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12316 struct window *w;
12317 int force_p;
12318 int current_matrix_p;
12320 struct glyph_matrix *matrix;
12321 struct glyph_row *row;
12322 int window_height;
12324 if (!make_cursor_line_fully_visible_p)
12325 return 1;
12327 /* It's not always possible to find the cursor, e.g, when a window
12328 is full of overlay strings. Don't do anything in that case. */
12329 if (w->cursor.vpos < 0)
12330 return 1;
12332 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12333 row = MATRIX_ROW (matrix, w->cursor.vpos);
12335 /* If the cursor row is not partially visible, there's nothing to do. */
12336 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12337 return 1;
12339 /* If the row the cursor is in is taller than the window's height,
12340 it's not clear what to do, so do nothing. */
12341 window_height = window_box_height (w);
12342 if (row->height >= window_height)
12344 if (!force_p || MINI_WINDOW_P (w)
12345 || w->vscroll || w->cursor.vpos == 0)
12346 return 1;
12348 return 0;
12350 #if 0
12351 /* This code used to try to scroll the window just enough to make
12352 the line visible. It returned 0 to say that the caller should
12353 allocate larger glyph matrices. */
12355 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12357 int dy = row->height - row->visible_height;
12358 w->vscroll = 0;
12359 w->cursor.y += dy;
12360 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12362 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12364 int dy = - (row->height - row->visible_height);
12365 w->vscroll = dy;
12366 w->cursor.y += dy;
12367 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12370 /* When we change the cursor y-position of the selected window,
12371 change this_line_y as well so that the display optimization for
12372 the cursor line of the selected window in redisplay_internal uses
12373 the correct y-position. */
12374 if (w == XWINDOW (selected_window))
12375 this_line_y = w->cursor.y;
12377 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12378 redisplay with larger matrices. */
12379 if (matrix->nrows < required_matrix_height (w))
12381 fonts_changed_p = 1;
12382 return 0;
12385 return 1;
12386 #endif /* 0 */
12390 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12391 non-zero means only WINDOW is redisplayed in redisplay_internal.
12392 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12393 in redisplay_window to bring a partially visible line into view in
12394 the case that only the cursor has moved.
12396 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12397 last screen line's vertical height extends past the end of the screen.
12399 Value is
12401 1 if scrolling succeeded
12403 0 if scrolling didn't find point.
12405 -1 if new fonts have been loaded so that we must interrupt
12406 redisplay, adjust glyph matrices, and try again. */
12408 enum
12410 SCROLLING_SUCCESS,
12411 SCROLLING_FAILED,
12412 SCROLLING_NEED_LARGER_MATRICES
12415 static int
12416 try_scrolling (window, just_this_one_p, scroll_conservatively,
12417 scroll_step, temp_scroll_step, last_line_misfit)
12418 Lisp_Object window;
12419 int just_this_one_p;
12420 EMACS_INT scroll_conservatively, scroll_step;
12421 int temp_scroll_step;
12422 int last_line_misfit;
12424 struct window *w = XWINDOW (window);
12425 struct frame *f = XFRAME (w->frame);
12426 struct text_pos scroll_margin_pos;
12427 struct text_pos pos;
12428 struct text_pos startp;
12429 struct it it;
12430 Lisp_Object window_end;
12431 int this_scroll_margin;
12432 int dy = 0;
12433 int scroll_max;
12434 int rc;
12435 int amount_to_scroll = 0;
12436 Lisp_Object aggressive;
12437 int height;
12438 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12440 #if GLYPH_DEBUG
12441 debug_method_add (w, "try_scrolling");
12442 #endif
12444 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12446 /* Compute scroll margin height in pixels. We scroll when point is
12447 within this distance from the top or bottom of the window. */
12448 if (scroll_margin > 0)
12450 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12451 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12453 else
12454 this_scroll_margin = 0;
12456 /* Force scroll_conservatively to have a reasonable value so it doesn't
12457 cause an overflow while computing how much to scroll. */
12458 if (scroll_conservatively)
12459 scroll_conservatively = min (scroll_conservatively,
12460 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12462 /* Compute how much we should try to scroll maximally to bring point
12463 into view. */
12464 if (scroll_step || scroll_conservatively || temp_scroll_step)
12465 scroll_max = max (scroll_step,
12466 max (scroll_conservatively, temp_scroll_step));
12467 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12468 || NUMBERP (current_buffer->scroll_up_aggressively))
12469 /* We're trying to scroll because of aggressive scrolling
12470 but no scroll_step is set. Choose an arbitrary one. Maybe
12471 there should be a variable for this. */
12472 scroll_max = 10;
12473 else
12474 scroll_max = 0;
12475 scroll_max *= FRAME_LINE_HEIGHT (f);
12477 /* Decide whether we have to scroll down. Start at the window end
12478 and move this_scroll_margin up to find the position of the scroll
12479 margin. */
12480 window_end = Fwindow_end (window, Qt);
12482 too_near_end:
12484 CHARPOS (scroll_margin_pos) = XINT (window_end);
12485 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12487 if (this_scroll_margin || extra_scroll_margin_lines)
12489 start_display (&it, w, scroll_margin_pos);
12490 if (this_scroll_margin)
12491 move_it_vertically_backward (&it, this_scroll_margin);
12492 if (extra_scroll_margin_lines)
12493 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12494 scroll_margin_pos = it.current.pos;
12497 if (PT >= CHARPOS (scroll_margin_pos))
12499 int y0;
12501 /* Point is in the scroll margin at the bottom of the window, or
12502 below. Compute a new window start that makes point visible. */
12504 /* Compute the distance from the scroll margin to PT.
12505 Give up if the distance is greater than scroll_max. */
12506 start_display (&it, w, scroll_margin_pos);
12507 y0 = it.current_y;
12508 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12509 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12511 /* To make point visible, we have to move the window start
12512 down so that the line the cursor is in is visible, which
12513 means we have to add in the height of the cursor line. */
12514 dy = line_bottom_y (&it) - y0;
12516 if (dy > scroll_max)
12517 return SCROLLING_FAILED;
12519 /* Move the window start down. If scrolling conservatively,
12520 move it just enough down to make point visible. If
12521 scroll_step is set, move it down by scroll_step. */
12522 start_display (&it, w, startp);
12524 if (scroll_conservatively)
12525 /* Set AMOUNT_TO_SCROLL to at least one line,
12526 and at most scroll_conservatively lines. */
12527 amount_to_scroll
12528 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12529 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12530 else if (scroll_step || temp_scroll_step)
12531 amount_to_scroll = scroll_max;
12532 else
12534 aggressive = current_buffer->scroll_up_aggressively;
12535 height = WINDOW_BOX_TEXT_HEIGHT (w);
12536 if (NUMBERP (aggressive))
12538 double float_amount = XFLOATINT (aggressive) * height;
12539 amount_to_scroll = float_amount;
12540 if (amount_to_scroll == 0 && float_amount > 0)
12541 amount_to_scroll = 1;
12545 if (amount_to_scroll <= 0)
12546 return SCROLLING_FAILED;
12548 /* If moving by amount_to_scroll leaves STARTP unchanged,
12549 move it down one screen line. */
12551 move_it_vertically (&it, amount_to_scroll);
12552 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12553 move_it_by_lines (&it, 1, 1);
12554 startp = it.current.pos;
12556 else
12558 /* See if point is inside the scroll margin at the top of the
12559 window. */
12560 scroll_margin_pos = startp;
12561 if (this_scroll_margin)
12563 start_display (&it, w, startp);
12564 move_it_vertically (&it, this_scroll_margin);
12565 scroll_margin_pos = it.current.pos;
12568 if (PT < CHARPOS (scroll_margin_pos))
12570 /* Point is in the scroll margin at the top of the window or
12571 above what is displayed in the window. */
12572 int y0;
12574 /* Compute the vertical distance from PT to the scroll
12575 margin position. Give up if distance is greater than
12576 scroll_max. */
12577 SET_TEXT_POS (pos, PT, PT_BYTE);
12578 start_display (&it, w, pos);
12579 y0 = it.current_y;
12580 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12581 it.last_visible_y, -1,
12582 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12583 dy = it.current_y - y0;
12584 if (dy > scroll_max)
12585 return SCROLLING_FAILED;
12587 /* Compute new window start. */
12588 start_display (&it, w, startp);
12590 if (scroll_conservatively)
12591 amount_to_scroll
12592 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12593 else if (scroll_step || temp_scroll_step)
12594 amount_to_scroll = scroll_max;
12595 else
12597 aggressive = current_buffer->scroll_down_aggressively;
12598 height = WINDOW_BOX_TEXT_HEIGHT (w);
12599 if (NUMBERP (aggressive))
12601 double float_amount = XFLOATINT (aggressive) * height;
12602 amount_to_scroll = float_amount;
12603 if (amount_to_scroll == 0 && float_amount > 0)
12604 amount_to_scroll = 1;
12608 if (amount_to_scroll <= 0)
12609 return SCROLLING_FAILED;
12611 move_it_vertically_backward (&it, amount_to_scroll);
12612 startp = it.current.pos;
12616 /* Run window scroll functions. */
12617 startp = run_window_scroll_functions (window, startp);
12619 /* Display the window. Give up if new fonts are loaded, or if point
12620 doesn't appear. */
12621 if (!try_window (window, startp, 0))
12622 rc = SCROLLING_NEED_LARGER_MATRICES;
12623 else if (w->cursor.vpos < 0)
12625 clear_glyph_matrix (w->desired_matrix);
12626 rc = SCROLLING_FAILED;
12628 else
12630 /* Maybe forget recorded base line for line number display. */
12631 if (!just_this_one_p
12632 || current_buffer->clip_changed
12633 || BEG_UNCHANGED < CHARPOS (startp))
12634 w->base_line_number = Qnil;
12636 /* If cursor ends up on a partially visible line,
12637 treat that as being off the bottom of the screen. */
12638 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12640 clear_glyph_matrix (w->desired_matrix);
12641 ++extra_scroll_margin_lines;
12642 goto too_near_end;
12644 rc = SCROLLING_SUCCESS;
12647 return rc;
12651 /* Compute a suitable window start for window W if display of W starts
12652 on a continuation line. Value is non-zero if a new window start
12653 was computed.
12655 The new window start will be computed, based on W's width, starting
12656 from the start of the continued line. It is the start of the
12657 screen line with the minimum distance from the old start W->start. */
12659 static int
12660 compute_window_start_on_continuation_line (w)
12661 struct window *w;
12663 struct text_pos pos, start_pos;
12664 int window_start_changed_p = 0;
12666 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12668 /* If window start is on a continuation line... Window start may be
12669 < BEGV in case there's invisible text at the start of the
12670 buffer (M-x rmail, for example). */
12671 if (CHARPOS (start_pos) > BEGV
12672 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12674 struct it it;
12675 struct glyph_row *row;
12677 /* Handle the case that the window start is out of range. */
12678 if (CHARPOS (start_pos) < BEGV)
12679 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12680 else if (CHARPOS (start_pos) > ZV)
12681 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12683 /* Find the start of the continued line. This should be fast
12684 because scan_buffer is fast (newline cache). */
12685 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12686 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12687 row, DEFAULT_FACE_ID);
12688 reseat_at_previous_visible_line_start (&it);
12690 /* If the line start is "too far" away from the window start,
12691 say it takes too much time to compute a new window start. */
12692 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12693 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12695 int min_distance, distance;
12697 /* Move forward by display lines to find the new window
12698 start. If window width was enlarged, the new start can
12699 be expected to be > the old start. If window width was
12700 decreased, the new window start will be < the old start.
12701 So, we're looking for the display line start with the
12702 minimum distance from the old window start. */
12703 pos = it.current.pos;
12704 min_distance = INFINITY;
12705 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12706 distance < min_distance)
12708 min_distance = distance;
12709 pos = it.current.pos;
12710 move_it_by_lines (&it, 1, 0);
12713 /* Set the window start there. */
12714 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12715 window_start_changed_p = 1;
12719 return window_start_changed_p;
12723 /* Try cursor movement in case text has not changed in window WINDOW,
12724 with window start STARTP. Value is
12726 CURSOR_MOVEMENT_SUCCESS if successful
12728 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12730 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12731 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12732 we want to scroll as if scroll-step were set to 1. See the code.
12734 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12735 which case we have to abort this redisplay, and adjust matrices
12736 first. */
12738 enum
12740 CURSOR_MOVEMENT_SUCCESS,
12741 CURSOR_MOVEMENT_CANNOT_BE_USED,
12742 CURSOR_MOVEMENT_MUST_SCROLL,
12743 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12746 static int
12747 try_cursor_movement (window, startp, scroll_step)
12748 Lisp_Object window;
12749 struct text_pos startp;
12750 int *scroll_step;
12752 struct window *w = XWINDOW (window);
12753 struct frame *f = XFRAME (w->frame);
12754 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12756 #if GLYPH_DEBUG
12757 if (inhibit_try_cursor_movement)
12758 return rc;
12759 #endif
12761 /* Handle case where text has not changed, only point, and it has
12762 not moved off the frame. */
12763 if (/* Point may be in this window. */
12764 PT >= CHARPOS (startp)
12765 /* Selective display hasn't changed. */
12766 && !current_buffer->clip_changed
12767 /* Function force-mode-line-update is used to force a thorough
12768 redisplay. It sets either windows_or_buffers_changed or
12769 update_mode_lines. So don't take a shortcut here for these
12770 cases. */
12771 && !update_mode_lines
12772 && !windows_or_buffers_changed
12773 && !cursor_type_changed
12774 /* Can't use this case if highlighting a region. When a
12775 region exists, cursor movement has to do more than just
12776 set the cursor. */
12777 && !(!NILP (Vtransient_mark_mode)
12778 && !NILP (current_buffer->mark_active))
12779 && NILP (w->region_showing)
12780 && NILP (Vshow_trailing_whitespace)
12781 /* Right after splitting windows, last_point may be nil. */
12782 && INTEGERP (w->last_point)
12783 /* This code is not used for mini-buffer for the sake of the case
12784 of redisplaying to replace an echo area message; since in
12785 that case the mini-buffer contents per se are usually
12786 unchanged. This code is of no real use in the mini-buffer
12787 since the handling of this_line_start_pos, etc., in redisplay
12788 handles the same cases. */
12789 && !EQ (window, minibuf_window)
12790 /* When splitting windows or for new windows, it happens that
12791 redisplay is called with a nil window_end_vpos or one being
12792 larger than the window. This should really be fixed in
12793 window.c. I don't have this on my list, now, so we do
12794 approximately the same as the old redisplay code. --gerd. */
12795 && INTEGERP (w->window_end_vpos)
12796 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12797 && (FRAME_WINDOW_P (f)
12798 || !overlay_arrow_in_current_buffer_p ()))
12800 int this_scroll_margin, top_scroll_margin;
12801 struct glyph_row *row = NULL;
12803 #if GLYPH_DEBUG
12804 debug_method_add (w, "cursor movement");
12805 #endif
12807 /* Scroll if point within this distance from the top or bottom
12808 of the window. This is a pixel value. */
12809 this_scroll_margin = max (0, scroll_margin);
12810 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12811 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12813 top_scroll_margin = this_scroll_margin;
12814 if (WINDOW_WANTS_HEADER_LINE_P (w))
12815 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12817 /* Start with the row the cursor was displayed during the last
12818 not paused redisplay. Give up if that row is not valid. */
12819 if (w->last_cursor.vpos < 0
12820 || w->last_cursor.vpos >= w->current_matrix->nrows)
12821 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12822 else
12824 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12825 if (row->mode_line_p)
12826 ++row;
12827 if (!row->enabled_p)
12828 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12831 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12833 int scroll_p = 0;
12834 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12836 if (PT > XFASTINT (w->last_point))
12838 /* Point has moved forward. */
12839 while (MATRIX_ROW_END_CHARPOS (row) < PT
12840 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12842 xassert (row->enabled_p);
12843 ++row;
12846 /* The end position of a row equals the start position
12847 of the next row. If PT is there, we would rather
12848 display it in the next line. */
12849 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12850 && MATRIX_ROW_END_CHARPOS (row) == PT
12851 && !cursor_row_p (w, row))
12852 ++row;
12854 /* If within the scroll margin, scroll. Note that
12855 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12856 the next line would be drawn, and that
12857 this_scroll_margin can be zero. */
12858 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12859 || PT > MATRIX_ROW_END_CHARPOS (row)
12860 /* Line is completely visible last line in window
12861 and PT is to be set in the next line. */
12862 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12863 && PT == MATRIX_ROW_END_CHARPOS (row)
12864 && !row->ends_at_zv_p
12865 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12866 scroll_p = 1;
12868 else if (PT < XFASTINT (w->last_point))
12870 /* Cursor has to be moved backward. Note that PT >=
12871 CHARPOS (startp) because of the outer if-statement. */
12872 while (!row->mode_line_p
12873 && (MATRIX_ROW_START_CHARPOS (row) > PT
12874 || (MATRIX_ROW_START_CHARPOS (row) == PT
12875 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12876 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12877 row > w->current_matrix->rows
12878 && (row-1)->ends_in_newline_from_string_p))))
12879 && (row->y > top_scroll_margin
12880 || CHARPOS (startp) == BEGV))
12882 xassert (row->enabled_p);
12883 --row;
12886 /* Consider the following case: Window starts at BEGV,
12887 there is invisible, intangible text at BEGV, so that
12888 display starts at some point START > BEGV. It can
12889 happen that we are called with PT somewhere between
12890 BEGV and START. Try to handle that case. */
12891 if (row < w->current_matrix->rows
12892 || row->mode_line_p)
12894 row = w->current_matrix->rows;
12895 if (row->mode_line_p)
12896 ++row;
12899 /* Due to newlines in overlay strings, we may have to
12900 skip forward over overlay strings. */
12901 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12902 && MATRIX_ROW_END_CHARPOS (row) == PT
12903 && !cursor_row_p (w, row))
12904 ++row;
12906 /* If within the scroll margin, scroll. */
12907 if (row->y < top_scroll_margin
12908 && CHARPOS (startp) != BEGV)
12909 scroll_p = 1;
12911 else
12913 /* Cursor did not move. So don't scroll even if cursor line
12914 is partially visible, as it was so before. */
12915 rc = CURSOR_MOVEMENT_SUCCESS;
12918 if (PT < MATRIX_ROW_START_CHARPOS (row)
12919 || PT > MATRIX_ROW_END_CHARPOS (row))
12921 /* if PT is not in the glyph row, give up. */
12922 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12924 else if (rc != CURSOR_MOVEMENT_SUCCESS
12925 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12926 && make_cursor_line_fully_visible_p)
12928 if (PT == MATRIX_ROW_END_CHARPOS (row)
12929 && !row->ends_at_zv_p
12930 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12931 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12932 else if (row->height > window_box_height (w))
12934 /* If we end up in a partially visible line, let's
12935 make it fully visible, except when it's taller
12936 than the window, in which case we can't do much
12937 about it. */
12938 *scroll_step = 1;
12939 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12941 else
12943 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12944 if (!cursor_row_fully_visible_p (w, 0, 1))
12945 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12946 else
12947 rc = CURSOR_MOVEMENT_SUCCESS;
12950 else if (scroll_p)
12951 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12952 else
12956 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12958 rc = CURSOR_MOVEMENT_SUCCESS;
12959 break;
12961 ++row;
12963 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12964 && MATRIX_ROW_START_CHARPOS (row) == PT
12965 && cursor_row_p (w, row));
12970 return rc;
12973 void
12974 set_vertical_scroll_bar (w)
12975 struct window *w;
12977 int start, end, whole;
12979 /* Calculate the start and end positions for the current window.
12980 At some point, it would be nice to choose between scrollbars
12981 which reflect the whole buffer size, with special markers
12982 indicating narrowing, and scrollbars which reflect only the
12983 visible region.
12985 Note that mini-buffers sometimes aren't displaying any text. */
12986 if (!MINI_WINDOW_P (w)
12987 || (w == XWINDOW (minibuf_window)
12988 && NILP (echo_area_buffer[0])))
12990 struct buffer *buf = XBUFFER (w->buffer);
12991 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12992 start = marker_position (w->start) - BUF_BEGV (buf);
12993 /* I don't think this is guaranteed to be right. For the
12994 moment, we'll pretend it is. */
12995 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12997 if (end < start)
12998 end = start;
12999 if (whole < (end - start))
13000 whole = end - start;
13002 else
13003 start = end = whole = 0;
13005 /* Indicate what this scroll bar ought to be displaying now. */
13006 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13007 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13008 (w, end - start, whole, start);
13012 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13013 selected_window is redisplayed.
13015 We can return without actually redisplaying the window if
13016 fonts_changed_p is nonzero. In that case, redisplay_internal will
13017 retry. */
13019 static void
13020 redisplay_window (window, just_this_one_p)
13021 Lisp_Object window;
13022 int just_this_one_p;
13024 struct window *w = XWINDOW (window);
13025 struct frame *f = XFRAME (w->frame);
13026 struct buffer *buffer = XBUFFER (w->buffer);
13027 struct buffer *old = current_buffer;
13028 struct text_pos lpoint, opoint, startp;
13029 int update_mode_line;
13030 int tem;
13031 struct it it;
13032 /* Record it now because it's overwritten. */
13033 int current_matrix_up_to_date_p = 0;
13034 int used_current_matrix_p = 0;
13035 /* This is less strict than current_matrix_up_to_date_p.
13036 It indictes that the buffer contents and narrowing are unchanged. */
13037 int buffer_unchanged_p = 0;
13038 int temp_scroll_step = 0;
13039 int count = SPECPDL_INDEX ();
13040 int rc;
13041 int centering_position = -1;
13042 int last_line_misfit = 0;
13043 int beg_unchanged, end_unchanged;
13045 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13046 opoint = lpoint;
13048 /* W must be a leaf window here. */
13049 xassert (!NILP (w->buffer));
13050 #if GLYPH_DEBUG
13051 *w->desired_matrix->method = 0;
13052 #endif
13054 restart:
13055 reconsider_clip_changes (w, buffer);
13057 /* Has the mode line to be updated? */
13058 update_mode_line = (!NILP (w->update_mode_line)
13059 || update_mode_lines
13060 || buffer->clip_changed
13061 || buffer->prevent_redisplay_optimizations_p);
13063 if (MINI_WINDOW_P (w))
13065 if (w == XWINDOW (echo_area_window)
13066 && !NILP (echo_area_buffer[0]))
13068 if (update_mode_line)
13069 /* We may have to update a tty frame's menu bar or a
13070 tool-bar. Example `M-x C-h C-h C-g'. */
13071 goto finish_menu_bars;
13072 else
13073 /* We've already displayed the echo area glyphs in this window. */
13074 goto finish_scroll_bars;
13076 else if ((w != XWINDOW (minibuf_window)
13077 || minibuf_level == 0)
13078 /* When buffer is nonempty, redisplay window normally. */
13079 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13080 /* Quail displays non-mini buffers in minibuffer window.
13081 In that case, redisplay the window normally. */
13082 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13084 /* W is a mini-buffer window, but it's not active, so clear
13085 it. */
13086 int yb = window_text_bottom_y (w);
13087 struct glyph_row *row;
13088 int y;
13090 for (y = 0, row = w->desired_matrix->rows;
13091 y < yb;
13092 y += row->height, ++row)
13093 blank_row (w, row, y);
13094 goto finish_scroll_bars;
13097 clear_glyph_matrix (w->desired_matrix);
13100 /* Otherwise set up data on this window; select its buffer and point
13101 value. */
13102 /* Really select the buffer, for the sake of buffer-local
13103 variables. */
13104 set_buffer_internal_1 (XBUFFER (w->buffer));
13106 current_matrix_up_to_date_p
13107 = (!NILP (w->window_end_valid)
13108 && !current_buffer->clip_changed
13109 && !current_buffer->prevent_redisplay_optimizations_p
13110 && XFASTINT (w->last_modified) >= MODIFF
13111 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13113 /* Run the window-bottom-change-functions
13114 if it is possible that the text on the screen has changed
13115 (either due to modification of the text, or any other reason). */
13116 if (!current_matrix_up_to_date_p
13117 && !NILP (Vwindow_text_change_functions))
13119 safe_run_hooks (Qwindow_text_change_functions);
13120 goto restart;
13123 beg_unchanged = BEG_UNCHANGED;
13124 end_unchanged = END_UNCHANGED;
13126 SET_TEXT_POS (opoint, PT, PT_BYTE);
13128 specbind (Qinhibit_point_motion_hooks, Qt);
13130 buffer_unchanged_p
13131 = (!NILP (w->window_end_valid)
13132 && !current_buffer->clip_changed
13133 && XFASTINT (w->last_modified) >= MODIFF
13134 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13136 /* When windows_or_buffers_changed is non-zero, we can't rely on
13137 the window end being valid, so set it to nil there. */
13138 if (windows_or_buffers_changed)
13140 /* If window starts on a continuation line, maybe adjust the
13141 window start in case the window's width changed. */
13142 if (XMARKER (w->start)->buffer == current_buffer)
13143 compute_window_start_on_continuation_line (w);
13145 w->window_end_valid = Qnil;
13148 /* Some sanity checks. */
13149 CHECK_WINDOW_END (w);
13150 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13151 abort ();
13152 if (BYTEPOS (opoint) < CHARPOS (opoint))
13153 abort ();
13155 /* If %c is in mode line, update it if needed. */
13156 if (!NILP (w->column_number_displayed)
13157 /* This alternative quickly identifies a common case
13158 where no change is needed. */
13159 && !(PT == XFASTINT (w->last_point)
13160 && XFASTINT (w->last_modified) >= MODIFF
13161 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13162 && (XFASTINT (w->column_number_displayed)
13163 != (int) current_column ())) /* iftc */
13164 update_mode_line = 1;
13166 /* Count number of windows showing the selected buffer. An indirect
13167 buffer counts as its base buffer. */
13168 if (!just_this_one_p)
13170 struct buffer *current_base, *window_base;
13171 current_base = current_buffer;
13172 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13173 if (current_base->base_buffer)
13174 current_base = current_base->base_buffer;
13175 if (window_base->base_buffer)
13176 window_base = window_base->base_buffer;
13177 if (current_base == window_base)
13178 buffer_shared++;
13181 /* Point refers normally to the selected window. For any other
13182 window, set up appropriate value. */
13183 if (!EQ (window, selected_window))
13185 int new_pt = XMARKER (w->pointm)->charpos;
13186 int new_pt_byte = marker_byte_position (w->pointm);
13187 if (new_pt < BEGV)
13189 new_pt = BEGV;
13190 new_pt_byte = BEGV_BYTE;
13191 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13193 else if (new_pt > (ZV - 1))
13195 new_pt = ZV;
13196 new_pt_byte = ZV_BYTE;
13197 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13200 /* We don't use SET_PT so that the point-motion hooks don't run. */
13201 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13204 /* If any of the character widths specified in the display table
13205 have changed, invalidate the width run cache. It's true that
13206 this may be a bit late to catch such changes, but the rest of
13207 redisplay goes (non-fatally) haywire when the display table is
13208 changed, so why should we worry about doing any better? */
13209 if (current_buffer->width_run_cache)
13211 struct Lisp_Char_Table *disptab = buffer_display_table ();
13213 if (! disptab_matches_widthtab (disptab,
13214 XVECTOR (current_buffer->width_table)))
13216 invalidate_region_cache (current_buffer,
13217 current_buffer->width_run_cache,
13218 BEG, Z);
13219 recompute_width_table (current_buffer, disptab);
13223 /* If window-start is screwed up, choose a new one. */
13224 if (XMARKER (w->start)->buffer != current_buffer)
13225 goto recenter;
13227 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13229 /* If someone specified a new starting point but did not insist,
13230 check whether it can be used. */
13231 if (!NILP (w->optional_new_start)
13232 && CHARPOS (startp) >= BEGV
13233 && CHARPOS (startp) <= ZV)
13235 w->optional_new_start = Qnil;
13236 start_display (&it, w, startp);
13237 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13238 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13239 if (IT_CHARPOS (it) == PT)
13240 w->force_start = Qt;
13241 /* IT may overshoot PT if text at PT is invisible. */
13242 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13243 w->force_start = Qt;
13246 force_start:
13248 /* Handle case where place to start displaying has been specified,
13249 unless the specified location is outside the accessible range. */
13250 if (!NILP (w->force_start)
13251 || w->frozen_window_start_p)
13253 /* We set this later on if we have to adjust point. */
13254 int new_vpos = -1;
13255 int val;
13257 w->force_start = Qnil;
13258 w->vscroll = 0;
13259 w->window_end_valid = Qnil;
13261 /* Forget any recorded base line for line number display. */
13262 if (!buffer_unchanged_p)
13263 w->base_line_number = Qnil;
13265 /* Redisplay the mode line. Select the buffer properly for that.
13266 Also, run the hook window-scroll-functions
13267 because we have scrolled. */
13268 /* Note, we do this after clearing force_start because
13269 if there's an error, it is better to forget about force_start
13270 than to get into an infinite loop calling the hook functions
13271 and having them get more errors. */
13272 if (!update_mode_line
13273 || ! NILP (Vwindow_scroll_functions))
13275 update_mode_line = 1;
13276 w->update_mode_line = Qt;
13277 startp = run_window_scroll_functions (window, startp);
13280 w->last_modified = make_number (0);
13281 w->last_overlay_modified = make_number (0);
13282 if (CHARPOS (startp) < BEGV)
13283 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13284 else if (CHARPOS (startp) > ZV)
13285 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13287 /* Redisplay, then check if cursor has been set during the
13288 redisplay. Give up if new fonts were loaded. */
13289 val = try_window (window, startp, 1);
13290 if (!val)
13292 w->force_start = Qt;
13293 clear_glyph_matrix (w->desired_matrix);
13294 goto need_larger_matrices;
13296 /* Point was outside the scroll margins. */
13297 if (val < 0)
13298 new_vpos = window_box_height (w) / 2;
13300 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13302 /* If point does not appear, try to move point so it does
13303 appear. The desired matrix has been built above, so we
13304 can use it here. */
13305 new_vpos = window_box_height (w) / 2;
13308 if (!cursor_row_fully_visible_p (w, 0, 0))
13310 /* Point does appear, but on a line partly visible at end of window.
13311 Move it back to a fully-visible line. */
13312 new_vpos = window_box_height (w);
13315 /* If we need to move point for either of the above reasons,
13316 now actually do it. */
13317 if (new_vpos >= 0)
13319 struct glyph_row *row;
13321 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13322 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13323 ++row;
13325 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13326 MATRIX_ROW_START_BYTEPOS (row));
13328 if (w != XWINDOW (selected_window))
13329 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13330 else if (current_buffer == old)
13331 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13333 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13335 /* If we are highlighting the region, then we just changed
13336 the region, so redisplay to show it. */
13337 if (!NILP (Vtransient_mark_mode)
13338 && !NILP (current_buffer->mark_active))
13340 clear_glyph_matrix (w->desired_matrix);
13341 if (!try_window (window, startp, 0))
13342 goto need_larger_matrices;
13346 #if GLYPH_DEBUG
13347 debug_method_add (w, "forced window start");
13348 #endif
13349 goto done;
13352 /* Handle case where text has not changed, only point, and it has
13353 not moved off the frame, and we are not retrying after hscroll.
13354 (current_matrix_up_to_date_p is nonzero when retrying.) */
13355 if (current_matrix_up_to_date_p
13356 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13357 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13359 switch (rc)
13361 case CURSOR_MOVEMENT_SUCCESS:
13362 used_current_matrix_p = 1;
13363 goto done;
13365 #if 0 /* try_cursor_movement never returns this value. */
13366 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13367 goto need_larger_matrices;
13368 #endif
13370 case CURSOR_MOVEMENT_MUST_SCROLL:
13371 goto try_to_scroll;
13373 default:
13374 abort ();
13377 /* If current starting point was originally the beginning of a line
13378 but no longer is, find a new starting point. */
13379 else if (!NILP (w->start_at_line_beg)
13380 && !(CHARPOS (startp) <= BEGV
13381 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13383 #if GLYPH_DEBUG
13384 debug_method_add (w, "recenter 1");
13385 #endif
13386 goto recenter;
13389 /* Try scrolling with try_window_id. Value is > 0 if update has
13390 been done, it is -1 if we know that the same window start will
13391 not work. It is 0 if unsuccessful for some other reason. */
13392 else if ((tem = try_window_id (w)) != 0)
13394 #if GLYPH_DEBUG
13395 debug_method_add (w, "try_window_id %d", tem);
13396 #endif
13398 if (fonts_changed_p)
13399 goto need_larger_matrices;
13400 if (tem > 0)
13401 goto done;
13403 /* Otherwise try_window_id has returned -1 which means that we
13404 don't want the alternative below this comment to execute. */
13406 else if (CHARPOS (startp) >= BEGV
13407 && CHARPOS (startp) <= ZV
13408 && PT >= CHARPOS (startp)
13409 && (CHARPOS (startp) < ZV
13410 /* Avoid starting at end of buffer. */
13411 || CHARPOS (startp) == BEGV
13412 || (XFASTINT (w->last_modified) >= MODIFF
13413 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13416 /* If first window line is a continuation line, and window start
13417 is inside the modified region, but the first change is before
13418 current window start, we must select a new window start.
13420 However, if this is the result of a down-mouse event (e.g. by
13421 extending the mouse-drag-overlay), we don't want to select a
13422 new window start, since that would change the position under
13423 the mouse, resulting in an unwanted mouse-movement rather
13424 than a simple mouse-click. */
13425 if (NILP (w->start_at_line_beg)
13426 && NILP (do_mouse_tracking)
13427 && CHARPOS (startp) > BEGV
13428 && CHARPOS (startp) > BEG + beg_unchanged
13429 && CHARPOS (startp) <= Z - end_unchanged)
13431 w->force_start = Qt;
13432 if (XMARKER (w->start)->buffer == current_buffer)
13433 compute_window_start_on_continuation_line (w);
13434 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13435 goto force_start;
13438 #if GLYPH_DEBUG
13439 debug_method_add (w, "same window start");
13440 #endif
13442 /* Try to redisplay starting at same place as before.
13443 If point has not moved off frame, accept the results. */
13444 if (!current_matrix_up_to_date_p
13445 /* Don't use try_window_reusing_current_matrix in this case
13446 because a window scroll function can have changed the
13447 buffer. */
13448 || !NILP (Vwindow_scroll_functions)
13449 || MINI_WINDOW_P (w)
13450 || !(used_current_matrix_p
13451 = try_window_reusing_current_matrix (w)))
13453 IF_DEBUG (debug_method_add (w, "1"));
13454 if (try_window (window, startp, 1) < 0)
13455 /* -1 means we need to scroll.
13456 0 means we need new matrices, but fonts_changed_p
13457 is set in that case, so we will detect it below. */
13458 goto try_to_scroll;
13461 if (fonts_changed_p)
13462 goto need_larger_matrices;
13464 if (w->cursor.vpos >= 0)
13466 if (!just_this_one_p
13467 || current_buffer->clip_changed
13468 || BEG_UNCHANGED < CHARPOS (startp))
13469 /* Forget any recorded base line for line number display. */
13470 w->base_line_number = Qnil;
13472 if (!cursor_row_fully_visible_p (w, 1, 0))
13474 clear_glyph_matrix (w->desired_matrix);
13475 last_line_misfit = 1;
13477 /* Drop through and scroll. */
13478 else
13479 goto done;
13481 else
13482 clear_glyph_matrix (w->desired_matrix);
13485 try_to_scroll:
13487 w->last_modified = make_number (0);
13488 w->last_overlay_modified = make_number (0);
13490 /* Redisplay the mode line. Select the buffer properly for that. */
13491 if (!update_mode_line)
13493 update_mode_line = 1;
13494 w->update_mode_line = Qt;
13497 /* Try to scroll by specified few lines. */
13498 if ((scroll_conservatively
13499 || scroll_step
13500 || temp_scroll_step
13501 || NUMBERP (current_buffer->scroll_up_aggressively)
13502 || NUMBERP (current_buffer->scroll_down_aggressively))
13503 && !current_buffer->clip_changed
13504 && CHARPOS (startp) >= BEGV
13505 && CHARPOS (startp) <= ZV)
13507 /* The function returns -1 if new fonts were loaded, 1 if
13508 successful, 0 if not successful. */
13509 int rc = try_scrolling (window, just_this_one_p,
13510 scroll_conservatively,
13511 scroll_step,
13512 temp_scroll_step, last_line_misfit);
13513 switch (rc)
13515 case SCROLLING_SUCCESS:
13516 goto done;
13518 case SCROLLING_NEED_LARGER_MATRICES:
13519 goto need_larger_matrices;
13521 case SCROLLING_FAILED:
13522 break;
13524 default:
13525 abort ();
13529 /* Finally, just choose place to start which centers point */
13531 recenter:
13532 if (centering_position < 0)
13533 centering_position = window_box_height (w) / 2;
13535 #if GLYPH_DEBUG
13536 debug_method_add (w, "recenter");
13537 #endif
13539 /* w->vscroll = 0; */
13541 /* Forget any previously recorded base line for line number display. */
13542 if (!buffer_unchanged_p)
13543 w->base_line_number = Qnil;
13545 /* Move backward half the height of the window. */
13546 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13547 it.current_y = it.last_visible_y;
13548 move_it_vertically_backward (&it, centering_position);
13549 xassert (IT_CHARPOS (it) >= BEGV);
13551 /* The function move_it_vertically_backward may move over more
13552 than the specified y-distance. If it->w is small, e.g. a
13553 mini-buffer window, we may end up in front of the window's
13554 display area. Start displaying at the start of the line
13555 containing PT in this case. */
13556 if (it.current_y <= 0)
13558 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13559 move_it_vertically_backward (&it, 0);
13560 #if 0
13561 /* I think this assert is bogus if buffer contains
13562 invisible text or images. KFS. */
13563 xassert (IT_CHARPOS (it) <= PT);
13564 #endif
13565 it.current_y = 0;
13568 it.current_x = it.hpos = 0;
13570 /* Set startp here explicitly in case that helps avoid an infinite loop
13571 in case the window-scroll-functions functions get errors. */
13572 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13574 /* Run scroll hooks. */
13575 startp = run_window_scroll_functions (window, it.current.pos);
13577 /* Redisplay the window. */
13578 if (!current_matrix_up_to_date_p
13579 || windows_or_buffers_changed
13580 || cursor_type_changed
13581 /* Don't use try_window_reusing_current_matrix in this case
13582 because it can have changed the buffer. */
13583 || !NILP (Vwindow_scroll_functions)
13584 || !just_this_one_p
13585 || MINI_WINDOW_P (w)
13586 || !(used_current_matrix_p
13587 = try_window_reusing_current_matrix (w)))
13588 try_window (window, startp, 0);
13590 /* If new fonts have been loaded (due to fontsets), give up. We
13591 have to start a new redisplay since we need to re-adjust glyph
13592 matrices. */
13593 if (fonts_changed_p)
13594 goto need_larger_matrices;
13596 /* If cursor did not appear assume that the middle of the window is
13597 in the first line of the window. Do it again with the next line.
13598 (Imagine a window of height 100, displaying two lines of height
13599 60. Moving back 50 from it->last_visible_y will end in the first
13600 line.) */
13601 if (w->cursor.vpos < 0)
13603 if (!NILP (w->window_end_valid)
13604 && PT >= Z - XFASTINT (w->window_end_pos))
13606 clear_glyph_matrix (w->desired_matrix);
13607 move_it_by_lines (&it, 1, 0);
13608 try_window (window, it.current.pos, 0);
13610 else if (PT < IT_CHARPOS (it))
13612 clear_glyph_matrix (w->desired_matrix);
13613 move_it_by_lines (&it, -1, 0);
13614 try_window (window, it.current.pos, 0);
13616 else
13618 /* Not much we can do about it. */
13622 /* Consider the following case: Window starts at BEGV, there is
13623 invisible, intangible text at BEGV, so that display starts at
13624 some point START > BEGV. It can happen that we are called with
13625 PT somewhere between BEGV and START. Try to handle that case. */
13626 if (w->cursor.vpos < 0)
13628 struct glyph_row *row = w->current_matrix->rows;
13629 if (row->mode_line_p)
13630 ++row;
13631 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13634 if (!cursor_row_fully_visible_p (w, 0, 0))
13636 /* If vscroll is enabled, disable it and try again. */
13637 if (w->vscroll)
13639 w->vscroll = 0;
13640 clear_glyph_matrix (w->desired_matrix);
13641 goto recenter;
13644 /* If centering point failed to make the whole line visible,
13645 put point at the top instead. That has to make the whole line
13646 visible, if it can be done. */
13647 if (centering_position == 0)
13648 goto done;
13650 clear_glyph_matrix (w->desired_matrix);
13651 centering_position = 0;
13652 goto recenter;
13655 done:
13657 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13658 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13659 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13660 ? Qt : Qnil);
13662 /* Display the mode line, if we must. */
13663 if ((update_mode_line
13664 /* If window not full width, must redo its mode line
13665 if (a) the window to its side is being redone and
13666 (b) we do a frame-based redisplay. This is a consequence
13667 of how inverted lines are drawn in frame-based redisplay. */
13668 || (!just_this_one_p
13669 && !FRAME_WINDOW_P (f)
13670 && !WINDOW_FULL_WIDTH_P (w))
13671 /* Line number to display. */
13672 || INTEGERP (w->base_line_pos)
13673 /* Column number is displayed and different from the one displayed. */
13674 || (!NILP (w->column_number_displayed)
13675 && (XFASTINT (w->column_number_displayed)
13676 != (int) current_column ()))) /* iftc */
13677 /* This means that the window has a mode line. */
13678 && (WINDOW_WANTS_MODELINE_P (w)
13679 || WINDOW_WANTS_HEADER_LINE_P (w)))
13681 display_mode_lines (w);
13683 /* If mode line height has changed, arrange for a thorough
13684 immediate redisplay using the correct mode line height. */
13685 if (WINDOW_WANTS_MODELINE_P (w)
13686 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13688 fonts_changed_p = 1;
13689 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13690 = DESIRED_MODE_LINE_HEIGHT (w);
13693 /* If top line height has changed, arrange for a thorough
13694 immediate redisplay using the correct mode line height. */
13695 if (WINDOW_WANTS_HEADER_LINE_P (w)
13696 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13698 fonts_changed_p = 1;
13699 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13700 = DESIRED_HEADER_LINE_HEIGHT (w);
13703 if (fonts_changed_p)
13704 goto need_larger_matrices;
13707 if (!line_number_displayed
13708 && !BUFFERP (w->base_line_pos))
13710 w->base_line_pos = Qnil;
13711 w->base_line_number = Qnil;
13714 finish_menu_bars:
13716 /* When we reach a frame's selected window, redo the frame's menu bar. */
13717 if (update_mode_line
13718 && EQ (FRAME_SELECTED_WINDOW (f), window))
13720 int redisplay_menu_p = 0;
13721 int redisplay_tool_bar_p = 0;
13723 if (FRAME_WINDOW_P (f))
13725 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13726 || defined (USE_GTK)
13727 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13728 #else
13729 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13730 #endif
13732 else
13733 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13735 if (redisplay_menu_p)
13736 display_menu_bar (w);
13738 #ifdef HAVE_WINDOW_SYSTEM
13739 if (FRAME_WINDOW_P (f))
13741 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13742 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13743 #else
13744 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13745 && (FRAME_TOOL_BAR_LINES (f) > 0
13746 || !NILP (Vauto_resize_tool_bars));
13747 #endif
13749 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13751 extern int ignore_mouse_drag_p;
13752 ignore_mouse_drag_p = 1;
13755 #endif
13758 #ifdef HAVE_WINDOW_SYSTEM
13759 if (FRAME_WINDOW_P (f)
13760 && update_window_fringes (w, (just_this_one_p
13761 || (!used_current_matrix_p && !overlay_arrow_seen)
13762 || w->pseudo_window_p)))
13764 update_begin (f);
13765 BLOCK_INPUT;
13766 if (draw_window_fringes (w, 1))
13767 x_draw_vertical_border (w);
13768 UNBLOCK_INPUT;
13769 update_end (f);
13771 #endif /* HAVE_WINDOW_SYSTEM */
13773 /* We go to this label, with fonts_changed_p nonzero,
13774 if it is necessary to try again using larger glyph matrices.
13775 We have to redeem the scroll bar even in this case,
13776 because the loop in redisplay_internal expects that. */
13777 need_larger_matrices:
13779 finish_scroll_bars:
13781 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13783 /* Set the thumb's position and size. */
13784 set_vertical_scroll_bar (w);
13786 /* Note that we actually used the scroll bar attached to this
13787 window, so it shouldn't be deleted at the end of redisplay. */
13788 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13789 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13792 /* Restore current_buffer and value of point in it. */
13793 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13794 set_buffer_internal_1 (old);
13795 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13796 shorter. This can be caused by log truncation in *Messages*. */
13797 if (CHARPOS (lpoint) <= ZV)
13798 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13800 unbind_to (count, Qnil);
13804 /* Build the complete desired matrix of WINDOW with a window start
13805 buffer position POS.
13807 Value is 1 if successful. It is zero if fonts were loaded during
13808 redisplay which makes re-adjusting glyph matrices necessary, and -1
13809 if point would appear in the scroll margins.
13810 (We check that only if CHECK_MARGINS is nonzero. */
13813 try_window (window, pos, check_margins)
13814 Lisp_Object window;
13815 struct text_pos pos;
13816 int check_margins;
13818 struct window *w = XWINDOW (window);
13819 struct it it;
13820 struct glyph_row *last_text_row = NULL;
13821 struct frame *f = XFRAME (w->frame);
13823 /* Make POS the new window start. */
13824 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13826 /* Mark cursor position as unknown. No overlay arrow seen. */
13827 w->cursor.vpos = -1;
13828 overlay_arrow_seen = 0;
13830 /* Initialize iterator and info to start at POS. */
13831 start_display (&it, w, pos);
13833 /* Display all lines of W. */
13834 while (it.current_y < it.last_visible_y)
13836 if (display_line (&it))
13837 last_text_row = it.glyph_row - 1;
13838 if (fonts_changed_p)
13839 return 0;
13842 /* Don't let the cursor end in the scroll margins. */
13843 if (check_margins
13844 && !MINI_WINDOW_P (w))
13846 int this_scroll_margin;
13848 this_scroll_margin = max (0, scroll_margin);
13849 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13850 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13852 if ((w->cursor.y >= 0 /* not vscrolled */
13853 && w->cursor.y < this_scroll_margin
13854 && CHARPOS (pos) > BEGV
13855 && IT_CHARPOS (it) < ZV)
13856 /* rms: considering make_cursor_line_fully_visible_p here
13857 seems to give wrong results. We don't want to recenter
13858 when the last line is partly visible, we want to allow
13859 that case to be handled in the usual way. */
13860 || (w->cursor.y + 1) > it.last_visible_y)
13862 w->cursor.vpos = -1;
13863 clear_glyph_matrix (w->desired_matrix);
13864 return -1;
13868 /* If bottom moved off end of frame, change mode line percentage. */
13869 if (XFASTINT (w->window_end_pos) <= 0
13870 && Z != IT_CHARPOS (it))
13871 w->update_mode_line = Qt;
13873 /* Set window_end_pos to the offset of the last character displayed
13874 on the window from the end of current_buffer. Set
13875 window_end_vpos to its row number. */
13876 if (last_text_row)
13878 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13879 w->window_end_bytepos
13880 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13881 w->window_end_pos
13882 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13883 w->window_end_vpos
13884 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13885 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13886 ->displays_text_p);
13888 else
13890 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13891 w->window_end_pos = make_number (Z - ZV);
13892 w->window_end_vpos = make_number (0);
13895 /* But that is not valid info until redisplay finishes. */
13896 w->window_end_valid = Qnil;
13897 return 1;
13902 /************************************************************************
13903 Window redisplay reusing current matrix when buffer has not changed
13904 ************************************************************************/
13906 /* Try redisplay of window W showing an unchanged buffer with a
13907 different window start than the last time it was displayed by
13908 reusing its current matrix. Value is non-zero if successful.
13909 W->start is the new window start. */
13911 static int
13912 try_window_reusing_current_matrix (w)
13913 struct window *w;
13915 struct frame *f = XFRAME (w->frame);
13916 struct glyph_row *row, *bottom_row;
13917 struct it it;
13918 struct run run;
13919 struct text_pos start, new_start;
13920 int nrows_scrolled, i;
13921 struct glyph_row *last_text_row;
13922 struct glyph_row *last_reused_text_row;
13923 struct glyph_row *start_row;
13924 int start_vpos, min_y, max_y;
13926 #if GLYPH_DEBUG
13927 if (inhibit_try_window_reusing)
13928 return 0;
13929 #endif
13931 if (/* This function doesn't handle terminal frames. */
13932 !FRAME_WINDOW_P (f)
13933 /* Don't try to reuse the display if windows have been split
13934 or such. */
13935 || windows_or_buffers_changed
13936 || cursor_type_changed)
13937 return 0;
13939 /* Can't do this if region may have changed. */
13940 if ((!NILP (Vtransient_mark_mode)
13941 && !NILP (current_buffer->mark_active))
13942 || !NILP (w->region_showing)
13943 || !NILP (Vshow_trailing_whitespace))
13944 return 0;
13946 /* If top-line visibility has changed, give up. */
13947 if (WINDOW_WANTS_HEADER_LINE_P (w)
13948 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13949 return 0;
13951 /* Give up if old or new display is scrolled vertically. We could
13952 make this function handle this, but right now it doesn't. */
13953 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13954 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13955 return 0;
13957 /* The variable new_start now holds the new window start. The old
13958 start `start' can be determined from the current matrix. */
13959 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13960 start = start_row->start.pos;
13961 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13963 /* Clear the desired matrix for the display below. */
13964 clear_glyph_matrix (w->desired_matrix);
13966 if (CHARPOS (new_start) <= CHARPOS (start))
13968 int first_row_y;
13970 /* Don't use this method if the display starts with an ellipsis
13971 displayed for invisible text. It's not easy to handle that case
13972 below, and it's certainly not worth the effort since this is
13973 not a frequent case. */
13974 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13975 return 0;
13977 IF_DEBUG (debug_method_add (w, "twu1"));
13979 /* Display up to a row that can be reused. The variable
13980 last_text_row is set to the last row displayed that displays
13981 text. Note that it.vpos == 0 if or if not there is a
13982 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13983 start_display (&it, w, new_start);
13984 first_row_y = it.current_y;
13985 w->cursor.vpos = -1;
13986 last_text_row = last_reused_text_row = NULL;
13988 while (it.current_y < it.last_visible_y
13989 && !fonts_changed_p)
13991 /* If we have reached into the characters in the START row,
13992 that means the line boundaries have changed. So we
13993 can't start copying with the row START. Maybe it will
13994 work to start copying with the following row. */
13995 while (IT_CHARPOS (it) > CHARPOS (start))
13997 /* Advance to the next row as the "start". */
13998 start_row++;
13999 start = start_row->start.pos;
14000 /* If there are no more rows to try, or just one, give up. */
14001 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14002 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14003 || CHARPOS (start) == ZV)
14005 clear_glyph_matrix (w->desired_matrix);
14006 return 0;
14009 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14011 /* If we have reached alignment,
14012 we can copy the rest of the rows. */
14013 if (IT_CHARPOS (it) == CHARPOS (start))
14014 break;
14016 if (display_line (&it))
14017 last_text_row = it.glyph_row - 1;
14020 /* A value of current_y < last_visible_y means that we stopped
14021 at the previous window start, which in turn means that we
14022 have at least one reusable row. */
14023 if (it.current_y < it.last_visible_y)
14025 /* IT.vpos always starts from 0; it counts text lines. */
14026 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14028 /* Find PT if not already found in the lines displayed. */
14029 if (w->cursor.vpos < 0)
14031 int dy = it.current_y - start_row->y;
14033 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14034 row = row_containing_pos (w, PT, row, NULL, dy);
14035 if (row)
14036 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14037 dy, nrows_scrolled);
14038 else
14040 clear_glyph_matrix (w->desired_matrix);
14041 return 0;
14045 /* Scroll the display. Do it before the current matrix is
14046 changed. The problem here is that update has not yet
14047 run, i.e. part of the current matrix is not up to date.
14048 scroll_run_hook will clear the cursor, and use the
14049 current matrix to get the height of the row the cursor is
14050 in. */
14051 run.current_y = start_row->y;
14052 run.desired_y = it.current_y;
14053 run.height = it.last_visible_y - it.current_y;
14055 if (run.height > 0 && run.current_y != run.desired_y)
14057 update_begin (f);
14058 FRAME_RIF (f)->update_window_begin_hook (w);
14059 FRAME_RIF (f)->clear_window_mouse_face (w);
14060 FRAME_RIF (f)->scroll_run_hook (w, &run);
14061 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14062 update_end (f);
14065 /* Shift current matrix down by nrows_scrolled lines. */
14066 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14067 rotate_matrix (w->current_matrix,
14068 start_vpos,
14069 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14070 nrows_scrolled);
14072 /* Disable lines that must be updated. */
14073 for (i = 0; i < nrows_scrolled; ++i)
14074 (start_row + i)->enabled_p = 0;
14076 /* Re-compute Y positions. */
14077 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14078 max_y = it.last_visible_y;
14079 for (row = start_row + nrows_scrolled;
14080 row < bottom_row;
14081 ++row)
14083 row->y = it.current_y;
14084 row->visible_height = row->height;
14086 if (row->y < min_y)
14087 row->visible_height -= min_y - row->y;
14088 if (row->y + row->height > max_y)
14089 row->visible_height -= row->y + row->height - max_y;
14090 row->redraw_fringe_bitmaps_p = 1;
14092 it.current_y += row->height;
14094 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14095 last_reused_text_row = row;
14096 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14097 break;
14100 /* Disable lines in the current matrix which are now
14101 below the window. */
14102 for (++row; row < bottom_row; ++row)
14103 row->enabled_p = row->mode_line_p = 0;
14106 /* Update window_end_pos etc.; last_reused_text_row is the last
14107 reused row from the current matrix containing text, if any.
14108 The value of last_text_row is the last displayed line
14109 containing text. */
14110 if (last_reused_text_row)
14112 w->window_end_bytepos
14113 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14114 w->window_end_pos
14115 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14116 w->window_end_vpos
14117 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14118 w->current_matrix));
14120 else if (last_text_row)
14122 w->window_end_bytepos
14123 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14124 w->window_end_pos
14125 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14126 w->window_end_vpos
14127 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14129 else
14131 /* This window must be completely empty. */
14132 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14133 w->window_end_pos = make_number (Z - ZV);
14134 w->window_end_vpos = make_number (0);
14136 w->window_end_valid = Qnil;
14138 /* Update hint: don't try scrolling again in update_window. */
14139 w->desired_matrix->no_scrolling_p = 1;
14141 #if GLYPH_DEBUG
14142 debug_method_add (w, "try_window_reusing_current_matrix 1");
14143 #endif
14144 return 1;
14146 else if (CHARPOS (new_start) > CHARPOS (start))
14148 struct glyph_row *pt_row, *row;
14149 struct glyph_row *first_reusable_row;
14150 struct glyph_row *first_row_to_display;
14151 int dy;
14152 int yb = window_text_bottom_y (w);
14154 /* Find the row starting at new_start, if there is one. Don't
14155 reuse a partially visible line at the end. */
14156 first_reusable_row = start_row;
14157 while (first_reusable_row->enabled_p
14158 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14159 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14160 < CHARPOS (new_start)))
14161 ++first_reusable_row;
14163 /* Give up if there is no row to reuse. */
14164 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14165 || !first_reusable_row->enabled_p
14166 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14167 != CHARPOS (new_start)))
14168 return 0;
14170 /* We can reuse fully visible rows beginning with
14171 first_reusable_row to the end of the window. Set
14172 first_row_to_display to the first row that cannot be reused.
14173 Set pt_row to the row containing point, if there is any. */
14174 pt_row = NULL;
14175 for (first_row_to_display = first_reusable_row;
14176 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14177 ++first_row_to_display)
14179 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14180 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14181 pt_row = first_row_to_display;
14184 /* Start displaying at the start of first_row_to_display. */
14185 xassert (first_row_to_display->y < yb);
14186 init_to_row_start (&it, w, first_row_to_display);
14188 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14189 - start_vpos);
14190 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14191 - nrows_scrolled);
14192 it.current_y = (first_row_to_display->y - first_reusable_row->y
14193 + WINDOW_HEADER_LINE_HEIGHT (w));
14195 /* Display lines beginning with first_row_to_display in the
14196 desired matrix. Set last_text_row to the last row displayed
14197 that displays text. */
14198 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14199 if (pt_row == NULL)
14200 w->cursor.vpos = -1;
14201 last_text_row = NULL;
14202 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14203 if (display_line (&it))
14204 last_text_row = it.glyph_row - 1;
14206 /* Give up If point isn't in a row displayed or reused. */
14207 if (w->cursor.vpos < 0)
14209 clear_glyph_matrix (w->desired_matrix);
14210 return 0;
14213 /* If point is in a reused row, adjust y and vpos of the cursor
14214 position. */
14215 if (pt_row)
14217 w->cursor.vpos -= nrows_scrolled;
14218 w->cursor.y -= first_reusable_row->y - start_row->y;
14221 /* Scroll the display. */
14222 run.current_y = first_reusable_row->y;
14223 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14224 run.height = it.last_visible_y - run.current_y;
14225 dy = run.current_y - run.desired_y;
14227 if (run.height)
14229 update_begin (f);
14230 FRAME_RIF (f)->update_window_begin_hook (w);
14231 FRAME_RIF (f)->clear_window_mouse_face (w);
14232 FRAME_RIF (f)->scroll_run_hook (w, &run);
14233 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14234 update_end (f);
14237 /* Adjust Y positions of reused rows. */
14238 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14239 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14240 max_y = it.last_visible_y;
14241 for (row = first_reusable_row; row < first_row_to_display; ++row)
14243 row->y -= dy;
14244 row->visible_height = row->height;
14245 if (row->y < min_y)
14246 row->visible_height -= min_y - row->y;
14247 if (row->y + row->height > max_y)
14248 row->visible_height -= row->y + row->height - max_y;
14249 row->redraw_fringe_bitmaps_p = 1;
14252 /* Scroll the current matrix. */
14253 xassert (nrows_scrolled > 0);
14254 rotate_matrix (w->current_matrix,
14255 start_vpos,
14256 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14257 -nrows_scrolled);
14259 /* Disable rows not reused. */
14260 for (row -= nrows_scrolled; row < bottom_row; ++row)
14261 row->enabled_p = 0;
14263 /* Point may have moved to a different line, so we cannot assume that
14264 the previous cursor position is valid; locate the correct row. */
14265 if (pt_row)
14267 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14268 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14269 row++)
14271 w->cursor.vpos++;
14272 w->cursor.y = row->y;
14274 if (row < bottom_row)
14276 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14277 while (glyph->charpos < PT)
14279 w->cursor.hpos++;
14280 w->cursor.x += glyph->pixel_width;
14281 glyph++;
14286 /* Adjust window end. A null value of last_text_row means that
14287 the window end is in reused rows which in turn means that
14288 only its vpos can have changed. */
14289 if (last_text_row)
14291 w->window_end_bytepos
14292 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14293 w->window_end_pos
14294 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14295 w->window_end_vpos
14296 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14298 else
14300 w->window_end_vpos
14301 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14304 w->window_end_valid = Qnil;
14305 w->desired_matrix->no_scrolling_p = 1;
14307 #if GLYPH_DEBUG
14308 debug_method_add (w, "try_window_reusing_current_matrix 2");
14309 #endif
14310 return 1;
14313 return 0;
14318 /************************************************************************
14319 Window redisplay reusing current matrix when buffer has changed
14320 ************************************************************************/
14322 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14323 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14324 int *, int *));
14325 static struct glyph_row *
14326 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14327 struct glyph_row *));
14330 /* Return the last row in MATRIX displaying text. If row START is
14331 non-null, start searching with that row. IT gives the dimensions
14332 of the display. Value is null if matrix is empty; otherwise it is
14333 a pointer to the row found. */
14335 static struct glyph_row *
14336 find_last_row_displaying_text (matrix, it, start)
14337 struct glyph_matrix *matrix;
14338 struct it *it;
14339 struct glyph_row *start;
14341 struct glyph_row *row, *row_found;
14343 /* Set row_found to the last row in IT->w's current matrix
14344 displaying text. The loop looks funny but think of partially
14345 visible lines. */
14346 row_found = NULL;
14347 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14348 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14350 xassert (row->enabled_p);
14351 row_found = row;
14352 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14353 break;
14354 ++row;
14357 return row_found;
14361 /* Return the last row in the current matrix of W that is not affected
14362 by changes at the start of current_buffer that occurred since W's
14363 current matrix was built. Value is null if no such row exists.
14365 BEG_UNCHANGED us the number of characters unchanged at the start of
14366 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14367 first changed character in current_buffer. Characters at positions <
14368 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14369 when the current matrix was built. */
14371 static struct glyph_row *
14372 find_last_unchanged_at_beg_row (w)
14373 struct window *w;
14375 int first_changed_pos = BEG + BEG_UNCHANGED;
14376 struct glyph_row *row;
14377 struct glyph_row *row_found = NULL;
14378 int yb = window_text_bottom_y (w);
14380 /* Find the last row displaying unchanged text. */
14381 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14382 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14383 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14385 if (/* If row ends before first_changed_pos, it is unchanged,
14386 except in some case. */
14387 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14388 /* When row ends in ZV and we write at ZV it is not
14389 unchanged. */
14390 && !row->ends_at_zv_p
14391 /* When first_changed_pos is the end of a continued line,
14392 row is not unchanged because it may be no longer
14393 continued. */
14394 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14395 && (row->continued_p
14396 || row->exact_window_width_line_p)))
14397 row_found = row;
14399 /* Stop if last visible row. */
14400 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14401 break;
14403 ++row;
14406 return row_found;
14410 /* Find the first glyph row in the current matrix of W that is not
14411 affected by changes at the end of current_buffer since the
14412 time W's current matrix was built.
14414 Return in *DELTA the number of chars by which buffer positions in
14415 unchanged text at the end of current_buffer must be adjusted.
14417 Return in *DELTA_BYTES the corresponding number of bytes.
14419 Value is null if no such row exists, i.e. all rows are affected by
14420 changes. */
14422 static struct glyph_row *
14423 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14424 struct window *w;
14425 int *delta, *delta_bytes;
14427 struct glyph_row *row;
14428 struct glyph_row *row_found = NULL;
14430 *delta = *delta_bytes = 0;
14432 /* Display must not have been paused, otherwise the current matrix
14433 is not up to date. */
14434 if (NILP (w->window_end_valid))
14435 abort ();
14437 /* A value of window_end_pos >= END_UNCHANGED means that the window
14438 end is in the range of changed text. If so, there is no
14439 unchanged row at the end of W's current matrix. */
14440 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14441 return NULL;
14443 /* Set row to the last row in W's current matrix displaying text. */
14444 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14446 /* If matrix is entirely empty, no unchanged row exists. */
14447 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14449 /* The value of row is the last glyph row in the matrix having a
14450 meaningful buffer position in it. The end position of row
14451 corresponds to window_end_pos. This allows us to translate
14452 buffer positions in the current matrix to current buffer
14453 positions for characters not in changed text. */
14454 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14455 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14456 int last_unchanged_pos, last_unchanged_pos_old;
14457 struct glyph_row *first_text_row
14458 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14460 *delta = Z - Z_old;
14461 *delta_bytes = Z_BYTE - Z_BYTE_old;
14463 /* Set last_unchanged_pos to the buffer position of the last
14464 character in the buffer that has not been changed. Z is the
14465 index + 1 of the last character in current_buffer, i.e. by
14466 subtracting END_UNCHANGED we get the index of the last
14467 unchanged character, and we have to add BEG to get its buffer
14468 position. */
14469 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14470 last_unchanged_pos_old = last_unchanged_pos - *delta;
14472 /* Search backward from ROW for a row displaying a line that
14473 starts at a minimum position >= last_unchanged_pos_old. */
14474 for (; row > first_text_row; --row)
14476 /* This used to abort, but it can happen.
14477 It is ok to just stop the search instead here. KFS. */
14478 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14479 break;
14481 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14482 row_found = row;
14486 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14487 abort ();
14489 return row_found;
14493 /* Make sure that glyph rows in the current matrix of window W
14494 reference the same glyph memory as corresponding rows in the
14495 frame's frame matrix. This function is called after scrolling W's
14496 current matrix on a terminal frame in try_window_id and
14497 try_window_reusing_current_matrix. */
14499 static void
14500 sync_frame_with_window_matrix_rows (w)
14501 struct window *w;
14503 struct frame *f = XFRAME (w->frame);
14504 struct glyph_row *window_row, *window_row_end, *frame_row;
14506 /* Preconditions: W must be a leaf window and full-width. Its frame
14507 must have a frame matrix. */
14508 xassert (NILP (w->hchild) && NILP (w->vchild));
14509 xassert (WINDOW_FULL_WIDTH_P (w));
14510 xassert (!FRAME_WINDOW_P (f));
14512 /* If W is a full-width window, glyph pointers in W's current matrix
14513 have, by definition, to be the same as glyph pointers in the
14514 corresponding frame matrix. Note that frame matrices have no
14515 marginal areas (see build_frame_matrix). */
14516 window_row = w->current_matrix->rows;
14517 window_row_end = window_row + w->current_matrix->nrows;
14518 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14519 while (window_row < window_row_end)
14521 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14522 struct glyph *end = window_row->glyphs[LAST_AREA];
14524 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14525 frame_row->glyphs[TEXT_AREA] = start;
14526 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14527 frame_row->glyphs[LAST_AREA] = end;
14529 /* Disable frame rows whose corresponding window rows have
14530 been disabled in try_window_id. */
14531 if (!window_row->enabled_p)
14532 frame_row->enabled_p = 0;
14534 ++window_row, ++frame_row;
14539 /* Find the glyph row in window W containing CHARPOS. Consider all
14540 rows between START and END (not inclusive). END null means search
14541 all rows to the end of the display area of W. Value is the row
14542 containing CHARPOS or null. */
14544 struct glyph_row *
14545 row_containing_pos (w, charpos, start, end, dy)
14546 struct window *w;
14547 int charpos;
14548 struct glyph_row *start, *end;
14549 int dy;
14551 struct glyph_row *row = start;
14552 int last_y;
14554 /* If we happen to start on a header-line, skip that. */
14555 if (row->mode_line_p)
14556 ++row;
14558 if ((end && row >= end) || !row->enabled_p)
14559 return NULL;
14561 last_y = window_text_bottom_y (w) - dy;
14563 while (1)
14565 /* Give up if we have gone too far. */
14566 if (end && row >= end)
14567 return NULL;
14568 /* This formerly returned if they were equal.
14569 I think that both quantities are of a "last plus one" type;
14570 if so, when they are equal, the row is within the screen. -- rms. */
14571 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14572 return NULL;
14574 /* If it is in this row, return this row. */
14575 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14576 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14577 /* The end position of a row equals the start
14578 position of the next row. If CHARPOS is there, we
14579 would rather display it in the next line, except
14580 when this line ends in ZV. */
14581 && !row->ends_at_zv_p
14582 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14583 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14584 return row;
14585 ++row;
14590 /* Try to redisplay window W by reusing its existing display. W's
14591 current matrix must be up to date when this function is called,
14592 i.e. window_end_valid must not be nil.
14594 Value is
14596 1 if display has been updated
14597 0 if otherwise unsuccessful
14598 -1 if redisplay with same window start is known not to succeed
14600 The following steps are performed:
14602 1. Find the last row in the current matrix of W that is not
14603 affected by changes at the start of current_buffer. If no such row
14604 is found, give up.
14606 2. Find the first row in W's current matrix that is not affected by
14607 changes at the end of current_buffer. Maybe there is no such row.
14609 3. Display lines beginning with the row + 1 found in step 1 to the
14610 row found in step 2 or, if step 2 didn't find a row, to the end of
14611 the window.
14613 4. If cursor is not known to appear on the window, give up.
14615 5. If display stopped at the row found in step 2, scroll the
14616 display and current matrix as needed.
14618 6. Maybe display some lines at the end of W, if we must. This can
14619 happen under various circumstances, like a partially visible line
14620 becoming fully visible, or because newly displayed lines are displayed
14621 in smaller font sizes.
14623 7. Update W's window end information. */
14625 static int
14626 try_window_id (w)
14627 struct window *w;
14629 struct frame *f = XFRAME (w->frame);
14630 struct glyph_matrix *current_matrix = w->current_matrix;
14631 struct glyph_matrix *desired_matrix = w->desired_matrix;
14632 struct glyph_row *last_unchanged_at_beg_row;
14633 struct glyph_row *first_unchanged_at_end_row;
14634 struct glyph_row *row;
14635 struct glyph_row *bottom_row;
14636 int bottom_vpos;
14637 struct it it;
14638 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14639 struct text_pos start_pos;
14640 struct run run;
14641 int first_unchanged_at_end_vpos = 0;
14642 struct glyph_row *last_text_row, *last_text_row_at_end;
14643 struct text_pos start;
14644 int first_changed_charpos, last_changed_charpos;
14646 #if GLYPH_DEBUG
14647 if (inhibit_try_window_id)
14648 return 0;
14649 #endif
14651 /* This is handy for debugging. */
14652 #if 0
14653 #define GIVE_UP(X) \
14654 do { \
14655 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14656 return 0; \
14657 } while (0)
14658 #else
14659 #define GIVE_UP(X) return 0
14660 #endif
14662 SET_TEXT_POS_FROM_MARKER (start, w->start);
14664 /* Don't use this for mini-windows because these can show
14665 messages and mini-buffers, and we don't handle that here. */
14666 if (MINI_WINDOW_P (w))
14667 GIVE_UP (1);
14669 /* This flag is used to prevent redisplay optimizations. */
14670 if (windows_or_buffers_changed || cursor_type_changed)
14671 GIVE_UP (2);
14673 /* Verify that narrowing has not changed.
14674 Also verify that we were not told to prevent redisplay optimizations.
14675 It would be nice to further
14676 reduce the number of cases where this prevents try_window_id. */
14677 if (current_buffer->clip_changed
14678 || current_buffer->prevent_redisplay_optimizations_p)
14679 GIVE_UP (3);
14681 /* Window must either use window-based redisplay or be full width. */
14682 if (!FRAME_WINDOW_P (f)
14683 && (!FRAME_LINE_INS_DEL_OK (f)
14684 || !WINDOW_FULL_WIDTH_P (w)))
14685 GIVE_UP (4);
14687 /* Give up if point is not known NOT to appear in W. */
14688 if (PT < CHARPOS (start))
14689 GIVE_UP (5);
14691 /* Another way to prevent redisplay optimizations. */
14692 if (XFASTINT (w->last_modified) == 0)
14693 GIVE_UP (6);
14695 /* Verify that window is not hscrolled. */
14696 if (XFASTINT (w->hscroll) != 0)
14697 GIVE_UP (7);
14699 /* Verify that display wasn't paused. */
14700 if (NILP (w->window_end_valid))
14701 GIVE_UP (8);
14703 /* Can't use this if highlighting a region because a cursor movement
14704 will do more than just set the cursor. */
14705 if (!NILP (Vtransient_mark_mode)
14706 && !NILP (current_buffer->mark_active))
14707 GIVE_UP (9);
14709 /* Likewise if highlighting trailing whitespace. */
14710 if (!NILP (Vshow_trailing_whitespace))
14711 GIVE_UP (11);
14713 /* Likewise if showing a region. */
14714 if (!NILP (w->region_showing))
14715 GIVE_UP (10);
14717 /* Can use this if overlay arrow position and or string have changed. */
14718 if (overlay_arrows_changed_p ())
14719 GIVE_UP (12);
14722 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14723 only if buffer has really changed. The reason is that the gap is
14724 initially at Z for freshly visited files. The code below would
14725 set end_unchanged to 0 in that case. */
14726 if (MODIFF > SAVE_MODIFF
14727 /* This seems to happen sometimes after saving a buffer. */
14728 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14730 if (GPT - BEG < BEG_UNCHANGED)
14731 BEG_UNCHANGED = GPT - BEG;
14732 if (Z - GPT < END_UNCHANGED)
14733 END_UNCHANGED = Z - GPT;
14736 /* The position of the first and last character that has been changed. */
14737 first_changed_charpos = BEG + BEG_UNCHANGED;
14738 last_changed_charpos = Z - END_UNCHANGED;
14740 /* If window starts after a line end, and the last change is in
14741 front of that newline, then changes don't affect the display.
14742 This case happens with stealth-fontification. Note that although
14743 the display is unchanged, glyph positions in the matrix have to
14744 be adjusted, of course. */
14745 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14746 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14747 && ((last_changed_charpos < CHARPOS (start)
14748 && CHARPOS (start) == BEGV)
14749 || (last_changed_charpos < CHARPOS (start) - 1
14750 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14752 int Z_old, delta, Z_BYTE_old, delta_bytes;
14753 struct glyph_row *r0;
14755 /* Compute how many chars/bytes have been added to or removed
14756 from the buffer. */
14757 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14758 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14759 delta = Z - Z_old;
14760 delta_bytes = Z_BYTE - Z_BYTE_old;
14762 /* Give up if PT is not in the window. Note that it already has
14763 been checked at the start of try_window_id that PT is not in
14764 front of the window start. */
14765 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14766 GIVE_UP (13);
14768 /* If window start is unchanged, we can reuse the whole matrix
14769 as is, after adjusting glyph positions. No need to compute
14770 the window end again, since its offset from Z hasn't changed. */
14771 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14772 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14773 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14774 /* PT must not be in a partially visible line. */
14775 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14776 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14778 /* Adjust positions in the glyph matrix. */
14779 if (delta || delta_bytes)
14781 struct glyph_row *r1
14782 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14783 increment_matrix_positions (w->current_matrix,
14784 MATRIX_ROW_VPOS (r0, current_matrix),
14785 MATRIX_ROW_VPOS (r1, current_matrix),
14786 delta, delta_bytes);
14789 /* Set the cursor. */
14790 row = row_containing_pos (w, PT, r0, NULL, 0);
14791 if (row)
14792 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14793 else
14794 abort ();
14795 return 1;
14799 /* Handle the case that changes are all below what is displayed in
14800 the window, and that PT is in the window. This shortcut cannot
14801 be taken if ZV is visible in the window, and text has been added
14802 there that is visible in the window. */
14803 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14804 /* ZV is not visible in the window, or there are no
14805 changes at ZV, actually. */
14806 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14807 || first_changed_charpos == last_changed_charpos))
14809 struct glyph_row *r0;
14811 /* Give up if PT is not in the window. Note that it already has
14812 been checked at the start of try_window_id that PT is not in
14813 front of the window start. */
14814 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14815 GIVE_UP (14);
14817 /* If window start is unchanged, we can reuse the whole matrix
14818 as is, without changing glyph positions since no text has
14819 been added/removed in front of the window end. */
14820 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14821 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14822 /* PT must not be in a partially visible line. */
14823 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14824 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14826 /* We have to compute the window end anew since text
14827 can have been added/removed after it. */
14828 w->window_end_pos
14829 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14830 w->window_end_bytepos
14831 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14833 /* Set the cursor. */
14834 row = row_containing_pos (w, PT, r0, NULL, 0);
14835 if (row)
14836 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14837 else
14838 abort ();
14839 return 2;
14843 /* Give up if window start is in the changed area.
14845 The condition used to read
14847 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14849 but why that was tested escapes me at the moment. */
14850 if (CHARPOS (start) >= first_changed_charpos
14851 && CHARPOS (start) <= last_changed_charpos)
14852 GIVE_UP (15);
14854 /* Check that window start agrees with the start of the first glyph
14855 row in its current matrix. Check this after we know the window
14856 start is not in changed text, otherwise positions would not be
14857 comparable. */
14858 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14859 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14860 GIVE_UP (16);
14862 /* Give up if the window ends in strings. Overlay strings
14863 at the end are difficult to handle, so don't try. */
14864 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14865 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14866 GIVE_UP (20);
14868 /* Compute the position at which we have to start displaying new
14869 lines. Some of the lines at the top of the window might be
14870 reusable because they are not displaying changed text. Find the
14871 last row in W's current matrix not affected by changes at the
14872 start of current_buffer. Value is null if changes start in the
14873 first line of window. */
14874 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14875 if (last_unchanged_at_beg_row)
14877 /* Avoid starting to display in the moddle of a character, a TAB
14878 for instance. This is easier than to set up the iterator
14879 exactly, and it's not a frequent case, so the additional
14880 effort wouldn't really pay off. */
14881 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14882 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14883 && last_unchanged_at_beg_row > w->current_matrix->rows)
14884 --last_unchanged_at_beg_row;
14886 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14887 GIVE_UP (17);
14889 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14890 GIVE_UP (18);
14891 start_pos = it.current.pos;
14893 /* Start displaying new lines in the desired matrix at the same
14894 vpos we would use in the current matrix, i.e. below
14895 last_unchanged_at_beg_row. */
14896 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14897 current_matrix);
14898 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14899 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14901 xassert (it.hpos == 0 && it.current_x == 0);
14903 else
14905 /* There are no reusable lines at the start of the window.
14906 Start displaying in the first text line. */
14907 start_display (&it, w, start);
14908 it.vpos = it.first_vpos;
14909 start_pos = it.current.pos;
14912 /* Find the first row that is not affected by changes at the end of
14913 the buffer. Value will be null if there is no unchanged row, in
14914 which case we must redisplay to the end of the window. delta
14915 will be set to the value by which buffer positions beginning with
14916 first_unchanged_at_end_row have to be adjusted due to text
14917 changes. */
14918 first_unchanged_at_end_row
14919 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14920 IF_DEBUG (debug_delta = delta);
14921 IF_DEBUG (debug_delta_bytes = delta_bytes);
14923 /* Set stop_pos to the buffer position up to which we will have to
14924 display new lines. If first_unchanged_at_end_row != NULL, this
14925 is the buffer position of the start of the line displayed in that
14926 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14927 that we don't stop at a buffer position. */
14928 stop_pos = 0;
14929 if (first_unchanged_at_end_row)
14931 xassert (last_unchanged_at_beg_row == NULL
14932 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14934 /* If this is a continuation line, move forward to the next one
14935 that isn't. Changes in lines above affect this line.
14936 Caution: this may move first_unchanged_at_end_row to a row
14937 not displaying text. */
14938 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14939 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14940 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14941 < it.last_visible_y))
14942 ++first_unchanged_at_end_row;
14944 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14945 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14946 >= it.last_visible_y))
14947 first_unchanged_at_end_row = NULL;
14948 else
14950 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14951 + delta);
14952 first_unchanged_at_end_vpos
14953 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14954 xassert (stop_pos >= Z - END_UNCHANGED);
14957 else if (last_unchanged_at_beg_row == NULL)
14958 GIVE_UP (19);
14961 #if GLYPH_DEBUG
14963 /* Either there is no unchanged row at the end, or the one we have
14964 now displays text. This is a necessary condition for the window
14965 end pos calculation at the end of this function. */
14966 xassert (first_unchanged_at_end_row == NULL
14967 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14969 debug_last_unchanged_at_beg_vpos
14970 = (last_unchanged_at_beg_row
14971 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14972 : -1);
14973 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14975 #endif /* GLYPH_DEBUG != 0 */
14978 /* Display new lines. Set last_text_row to the last new line
14979 displayed which has text on it, i.e. might end up as being the
14980 line where the window_end_vpos is. */
14981 w->cursor.vpos = -1;
14982 last_text_row = NULL;
14983 overlay_arrow_seen = 0;
14984 while (it.current_y < it.last_visible_y
14985 && !fonts_changed_p
14986 && (first_unchanged_at_end_row == NULL
14987 || IT_CHARPOS (it) < stop_pos))
14989 if (display_line (&it))
14990 last_text_row = it.glyph_row - 1;
14993 if (fonts_changed_p)
14994 return -1;
14997 /* Compute differences in buffer positions, y-positions etc. for
14998 lines reused at the bottom of the window. Compute what we can
14999 scroll. */
15000 if (first_unchanged_at_end_row
15001 /* No lines reused because we displayed everything up to the
15002 bottom of the window. */
15003 && it.current_y < it.last_visible_y)
15005 dvpos = (it.vpos
15006 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15007 current_matrix));
15008 dy = it.current_y - first_unchanged_at_end_row->y;
15009 run.current_y = first_unchanged_at_end_row->y;
15010 run.desired_y = run.current_y + dy;
15011 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15013 else
15015 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
15016 first_unchanged_at_end_row = NULL;
15018 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15021 /* Find the cursor if not already found. We have to decide whether
15022 PT will appear on this window (it sometimes doesn't, but this is
15023 not a very frequent case.) This decision has to be made before
15024 the current matrix is altered. A value of cursor.vpos < 0 means
15025 that PT is either in one of the lines beginning at
15026 first_unchanged_at_end_row or below the window. Don't care for
15027 lines that might be displayed later at the window end; as
15028 mentioned, this is not a frequent case. */
15029 if (w->cursor.vpos < 0)
15031 /* Cursor in unchanged rows at the top? */
15032 if (PT < CHARPOS (start_pos)
15033 && last_unchanged_at_beg_row)
15035 row = row_containing_pos (w, PT,
15036 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15037 last_unchanged_at_beg_row + 1, 0);
15038 if (row)
15039 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15042 /* Start from first_unchanged_at_end_row looking for PT. */
15043 else if (first_unchanged_at_end_row)
15045 row = row_containing_pos (w, PT - delta,
15046 first_unchanged_at_end_row, NULL, 0);
15047 if (row)
15048 set_cursor_from_row (w, row, w->current_matrix, delta,
15049 delta_bytes, dy, dvpos);
15052 /* Give up if cursor was not found. */
15053 if (w->cursor.vpos < 0)
15055 clear_glyph_matrix (w->desired_matrix);
15056 return -1;
15060 /* Don't let the cursor end in the scroll margins. */
15062 int this_scroll_margin, cursor_height;
15064 this_scroll_margin = max (0, scroll_margin);
15065 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15066 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15067 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15069 if ((w->cursor.y < this_scroll_margin
15070 && CHARPOS (start) > BEGV)
15071 /* Old redisplay didn't take scroll margin into account at the bottom,
15072 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15073 || (w->cursor.y + (make_cursor_line_fully_visible_p
15074 ? cursor_height + this_scroll_margin
15075 : 1)) > it.last_visible_y)
15077 w->cursor.vpos = -1;
15078 clear_glyph_matrix (w->desired_matrix);
15079 return -1;
15083 /* Scroll the display. Do it before changing the current matrix so
15084 that xterm.c doesn't get confused about where the cursor glyph is
15085 found. */
15086 if (dy && run.height)
15088 update_begin (f);
15090 if (FRAME_WINDOW_P (f))
15092 FRAME_RIF (f)->update_window_begin_hook (w);
15093 FRAME_RIF (f)->clear_window_mouse_face (w);
15094 FRAME_RIF (f)->scroll_run_hook (w, &run);
15095 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15097 else
15099 /* Terminal frame. In this case, dvpos gives the number of
15100 lines to scroll by; dvpos < 0 means scroll up. */
15101 int first_unchanged_at_end_vpos
15102 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15103 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15104 int end = (WINDOW_TOP_EDGE_LINE (w)
15105 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15106 + window_internal_height (w));
15108 /* Perform the operation on the screen. */
15109 if (dvpos > 0)
15111 /* Scroll last_unchanged_at_beg_row to the end of the
15112 window down dvpos lines. */
15113 set_terminal_window (f, end);
15115 /* On dumb terminals delete dvpos lines at the end
15116 before inserting dvpos empty lines. */
15117 if (!FRAME_SCROLL_REGION_OK (f))
15118 ins_del_lines (f, end - dvpos, -dvpos);
15120 /* Insert dvpos empty lines in front of
15121 last_unchanged_at_beg_row. */
15122 ins_del_lines (f, from, dvpos);
15124 else if (dvpos < 0)
15126 /* Scroll up last_unchanged_at_beg_vpos to the end of
15127 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15128 set_terminal_window (f, end);
15130 /* Delete dvpos lines in front of
15131 last_unchanged_at_beg_vpos. ins_del_lines will set
15132 the cursor to the given vpos and emit |dvpos| delete
15133 line sequences. */
15134 ins_del_lines (f, from + dvpos, dvpos);
15136 /* On a dumb terminal insert dvpos empty lines at the
15137 end. */
15138 if (!FRAME_SCROLL_REGION_OK (f))
15139 ins_del_lines (f, end + dvpos, -dvpos);
15142 set_terminal_window (f, 0);
15145 update_end (f);
15148 /* Shift reused rows of the current matrix to the right position.
15149 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15150 text. */
15151 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15152 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15153 if (dvpos < 0)
15155 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15156 bottom_vpos, dvpos);
15157 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15158 bottom_vpos, 0);
15160 else if (dvpos > 0)
15162 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15163 bottom_vpos, dvpos);
15164 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15165 first_unchanged_at_end_vpos + dvpos, 0);
15168 /* For frame-based redisplay, make sure that current frame and window
15169 matrix are in sync with respect to glyph memory. */
15170 if (!FRAME_WINDOW_P (f))
15171 sync_frame_with_window_matrix_rows (w);
15173 /* Adjust buffer positions in reused rows. */
15174 if (delta || delta_bytes)
15175 increment_matrix_positions (current_matrix,
15176 first_unchanged_at_end_vpos + dvpos,
15177 bottom_vpos, delta, delta_bytes);
15179 /* Adjust Y positions. */
15180 if (dy)
15181 shift_glyph_matrix (w, current_matrix,
15182 first_unchanged_at_end_vpos + dvpos,
15183 bottom_vpos, dy);
15185 if (first_unchanged_at_end_row)
15187 first_unchanged_at_end_row += dvpos;
15188 if (first_unchanged_at_end_row->y >= it.last_visible_y
15189 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15190 first_unchanged_at_end_row = NULL;
15193 /* If scrolling up, there may be some lines to display at the end of
15194 the window. */
15195 last_text_row_at_end = NULL;
15196 if (dy < 0)
15198 /* Scrolling up can leave for example a partially visible line
15199 at the end of the window to be redisplayed. */
15200 /* Set last_row to the glyph row in the current matrix where the
15201 window end line is found. It has been moved up or down in
15202 the matrix by dvpos. */
15203 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15204 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15206 /* If last_row is the window end line, it should display text. */
15207 xassert (last_row->displays_text_p);
15209 /* If window end line was partially visible before, begin
15210 displaying at that line. Otherwise begin displaying with the
15211 line following it. */
15212 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15214 init_to_row_start (&it, w, last_row);
15215 it.vpos = last_vpos;
15216 it.current_y = last_row->y;
15218 else
15220 init_to_row_end (&it, w, last_row);
15221 it.vpos = 1 + last_vpos;
15222 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15223 ++last_row;
15226 /* We may start in a continuation line. If so, we have to
15227 get the right continuation_lines_width and current_x. */
15228 it.continuation_lines_width = last_row->continuation_lines_width;
15229 it.hpos = it.current_x = 0;
15231 /* Display the rest of the lines at the window end. */
15232 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15233 while (it.current_y < it.last_visible_y
15234 && !fonts_changed_p)
15236 /* Is it always sure that the display agrees with lines in
15237 the current matrix? I don't think so, so we mark rows
15238 displayed invalid in the current matrix by setting their
15239 enabled_p flag to zero. */
15240 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15241 if (display_line (&it))
15242 last_text_row_at_end = it.glyph_row - 1;
15246 /* Update window_end_pos and window_end_vpos. */
15247 if (first_unchanged_at_end_row
15248 && !last_text_row_at_end)
15250 /* Window end line if one of the preserved rows from the current
15251 matrix. Set row to the last row displaying text in current
15252 matrix starting at first_unchanged_at_end_row, after
15253 scrolling. */
15254 xassert (first_unchanged_at_end_row->displays_text_p);
15255 row = find_last_row_displaying_text (w->current_matrix, &it,
15256 first_unchanged_at_end_row);
15257 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15259 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15260 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15261 w->window_end_vpos
15262 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15263 xassert (w->window_end_bytepos >= 0);
15264 IF_DEBUG (debug_method_add (w, "A"));
15266 else if (last_text_row_at_end)
15268 w->window_end_pos
15269 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15270 w->window_end_bytepos
15271 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15272 w->window_end_vpos
15273 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15274 xassert (w->window_end_bytepos >= 0);
15275 IF_DEBUG (debug_method_add (w, "B"));
15277 else if (last_text_row)
15279 /* We have displayed either to the end of the window or at the
15280 end of the window, i.e. the last row with text is to be found
15281 in the desired matrix. */
15282 w->window_end_pos
15283 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15284 w->window_end_bytepos
15285 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15286 w->window_end_vpos
15287 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15288 xassert (w->window_end_bytepos >= 0);
15290 else if (first_unchanged_at_end_row == NULL
15291 && last_text_row == NULL
15292 && last_text_row_at_end == NULL)
15294 /* Displayed to end of window, but no line containing text was
15295 displayed. Lines were deleted at the end of the window. */
15296 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15297 int vpos = XFASTINT (w->window_end_vpos);
15298 struct glyph_row *current_row = current_matrix->rows + vpos;
15299 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15301 for (row = NULL;
15302 row == NULL && vpos >= first_vpos;
15303 --vpos, --current_row, --desired_row)
15305 if (desired_row->enabled_p)
15307 if (desired_row->displays_text_p)
15308 row = desired_row;
15310 else if (current_row->displays_text_p)
15311 row = current_row;
15314 xassert (row != NULL);
15315 w->window_end_vpos = make_number (vpos + 1);
15316 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15317 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15318 xassert (w->window_end_bytepos >= 0);
15319 IF_DEBUG (debug_method_add (w, "C"));
15321 else
15322 abort ();
15324 #if 0 /* This leads to problems, for instance when the cursor is
15325 at ZV, and the cursor line displays no text. */
15326 /* Disable rows below what's displayed in the window. This makes
15327 debugging easier. */
15328 enable_glyph_matrix_rows (current_matrix,
15329 XFASTINT (w->window_end_vpos) + 1,
15330 bottom_vpos, 0);
15331 #endif
15333 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15334 debug_end_vpos = XFASTINT (w->window_end_vpos));
15336 /* Record that display has not been completed. */
15337 w->window_end_valid = Qnil;
15338 w->desired_matrix->no_scrolling_p = 1;
15339 return 3;
15341 #undef GIVE_UP
15346 /***********************************************************************
15347 More debugging support
15348 ***********************************************************************/
15350 #if GLYPH_DEBUG
15352 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15353 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15354 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15357 /* Dump the contents of glyph matrix MATRIX on stderr.
15359 GLYPHS 0 means don't show glyph contents.
15360 GLYPHS 1 means show glyphs in short form
15361 GLYPHS > 1 means show glyphs in long form. */
15363 void
15364 dump_glyph_matrix (matrix, glyphs)
15365 struct glyph_matrix *matrix;
15366 int glyphs;
15368 int i;
15369 for (i = 0; i < matrix->nrows; ++i)
15370 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15374 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15375 the glyph row and area where the glyph comes from. */
15377 void
15378 dump_glyph (row, glyph, area)
15379 struct glyph_row *row;
15380 struct glyph *glyph;
15381 int area;
15383 if (glyph->type == CHAR_GLYPH)
15385 fprintf (stderr,
15386 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15387 glyph - row->glyphs[TEXT_AREA],
15388 'C',
15389 glyph->charpos,
15390 (BUFFERP (glyph->object)
15391 ? 'B'
15392 : (STRINGP (glyph->object)
15393 ? 'S'
15394 : '-')),
15395 glyph->pixel_width,
15396 glyph->u.ch,
15397 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15398 ? glyph->u.ch
15399 : '.'),
15400 glyph->face_id,
15401 glyph->left_box_line_p,
15402 glyph->right_box_line_p);
15404 else if (glyph->type == STRETCH_GLYPH)
15406 fprintf (stderr,
15407 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15408 glyph - row->glyphs[TEXT_AREA],
15409 'S',
15410 glyph->charpos,
15411 (BUFFERP (glyph->object)
15412 ? 'B'
15413 : (STRINGP (glyph->object)
15414 ? 'S'
15415 : '-')),
15416 glyph->pixel_width,
15418 '.',
15419 glyph->face_id,
15420 glyph->left_box_line_p,
15421 glyph->right_box_line_p);
15423 else if (glyph->type == IMAGE_GLYPH)
15425 fprintf (stderr,
15426 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15427 glyph - row->glyphs[TEXT_AREA],
15428 'I',
15429 glyph->charpos,
15430 (BUFFERP (glyph->object)
15431 ? 'B'
15432 : (STRINGP (glyph->object)
15433 ? 'S'
15434 : '-')),
15435 glyph->pixel_width,
15436 glyph->u.img_id,
15437 '.',
15438 glyph->face_id,
15439 glyph->left_box_line_p,
15440 glyph->right_box_line_p);
15442 else if (glyph->type == COMPOSITE_GLYPH)
15444 fprintf (stderr,
15445 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15446 glyph - row->glyphs[TEXT_AREA],
15447 '+',
15448 glyph->charpos,
15449 (BUFFERP (glyph->object)
15450 ? 'B'
15451 : (STRINGP (glyph->object)
15452 ? 'S'
15453 : '-')),
15454 glyph->pixel_width,
15455 glyph->u.cmp_id,
15456 '.',
15457 glyph->face_id,
15458 glyph->left_box_line_p,
15459 glyph->right_box_line_p);
15464 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15465 GLYPHS 0 means don't show glyph contents.
15466 GLYPHS 1 means show glyphs in short form
15467 GLYPHS > 1 means show glyphs in long form. */
15469 void
15470 dump_glyph_row (row, vpos, glyphs)
15471 struct glyph_row *row;
15472 int vpos, glyphs;
15474 if (glyphs != 1)
15476 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15477 fprintf (stderr, "======================================================================\n");
15479 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15480 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15481 vpos,
15482 MATRIX_ROW_START_CHARPOS (row),
15483 MATRIX_ROW_END_CHARPOS (row),
15484 row->used[TEXT_AREA],
15485 row->contains_overlapping_glyphs_p,
15486 row->enabled_p,
15487 row->truncated_on_left_p,
15488 row->truncated_on_right_p,
15489 row->continued_p,
15490 MATRIX_ROW_CONTINUATION_LINE_P (row),
15491 row->displays_text_p,
15492 row->ends_at_zv_p,
15493 row->fill_line_p,
15494 row->ends_in_middle_of_char_p,
15495 row->starts_in_middle_of_char_p,
15496 row->mouse_face_p,
15497 row->x,
15498 row->y,
15499 row->pixel_width,
15500 row->height,
15501 row->visible_height,
15502 row->ascent,
15503 row->phys_ascent);
15504 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15505 row->end.overlay_string_index,
15506 row->continuation_lines_width);
15507 fprintf (stderr, "%9d %5d\n",
15508 CHARPOS (row->start.string_pos),
15509 CHARPOS (row->end.string_pos));
15510 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15511 row->end.dpvec_index);
15514 if (glyphs > 1)
15516 int area;
15518 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15520 struct glyph *glyph = row->glyphs[area];
15521 struct glyph *glyph_end = glyph + row->used[area];
15523 /* Glyph for a line end in text. */
15524 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15525 ++glyph_end;
15527 if (glyph < glyph_end)
15528 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15530 for (; glyph < glyph_end; ++glyph)
15531 dump_glyph (row, glyph, area);
15534 else if (glyphs == 1)
15536 int area;
15538 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15540 char *s = (char *) alloca (row->used[area] + 1);
15541 int i;
15543 for (i = 0; i < row->used[area]; ++i)
15545 struct glyph *glyph = row->glyphs[area] + i;
15546 if (glyph->type == CHAR_GLYPH
15547 && glyph->u.ch < 0x80
15548 && glyph->u.ch >= ' ')
15549 s[i] = glyph->u.ch;
15550 else
15551 s[i] = '.';
15554 s[i] = '\0';
15555 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15561 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15562 Sdump_glyph_matrix, 0, 1, "p",
15563 doc: /* Dump the current matrix of the selected window to stderr.
15564 Shows contents of glyph row structures. With non-nil
15565 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15566 glyphs in short form, otherwise show glyphs in long form. */)
15567 (glyphs)
15568 Lisp_Object glyphs;
15570 struct window *w = XWINDOW (selected_window);
15571 struct buffer *buffer = XBUFFER (w->buffer);
15573 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15574 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15575 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15576 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15577 fprintf (stderr, "=============================================\n");
15578 dump_glyph_matrix (w->current_matrix,
15579 NILP (glyphs) ? 0 : XINT (glyphs));
15580 return Qnil;
15584 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15585 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15588 struct frame *f = XFRAME (selected_frame);
15589 dump_glyph_matrix (f->current_matrix, 1);
15590 return Qnil;
15594 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15595 doc: /* Dump glyph row ROW to stderr.
15596 GLYPH 0 means don't dump glyphs.
15597 GLYPH 1 means dump glyphs in short form.
15598 GLYPH > 1 or omitted means dump glyphs in long form. */)
15599 (row, glyphs)
15600 Lisp_Object row, glyphs;
15602 struct glyph_matrix *matrix;
15603 int vpos;
15605 CHECK_NUMBER (row);
15606 matrix = XWINDOW (selected_window)->current_matrix;
15607 vpos = XINT (row);
15608 if (vpos >= 0 && vpos < matrix->nrows)
15609 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15610 vpos,
15611 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15612 return Qnil;
15616 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15617 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15618 GLYPH 0 means don't dump glyphs.
15619 GLYPH 1 means dump glyphs in short form.
15620 GLYPH > 1 or omitted means dump glyphs in long form. */)
15621 (row, glyphs)
15622 Lisp_Object row, glyphs;
15624 struct frame *sf = SELECTED_FRAME ();
15625 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15626 int vpos;
15628 CHECK_NUMBER (row);
15629 vpos = XINT (row);
15630 if (vpos >= 0 && vpos < m->nrows)
15631 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15632 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15633 return Qnil;
15637 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15638 doc: /* Toggle tracing of redisplay.
15639 With ARG, turn tracing on if and only if ARG is positive. */)
15640 (arg)
15641 Lisp_Object arg;
15643 if (NILP (arg))
15644 trace_redisplay_p = !trace_redisplay_p;
15645 else
15647 arg = Fprefix_numeric_value (arg);
15648 trace_redisplay_p = XINT (arg) > 0;
15651 return Qnil;
15655 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15656 doc: /* Like `format', but print result to stderr.
15657 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15658 (nargs, args)
15659 int nargs;
15660 Lisp_Object *args;
15662 Lisp_Object s = Fformat (nargs, args);
15663 fprintf (stderr, "%s", SDATA (s));
15664 return Qnil;
15667 #endif /* GLYPH_DEBUG */
15671 /***********************************************************************
15672 Building Desired Matrix Rows
15673 ***********************************************************************/
15675 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15676 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15678 static struct glyph_row *
15679 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15680 struct window *w;
15681 Lisp_Object overlay_arrow_string;
15683 struct frame *f = XFRAME (WINDOW_FRAME (w));
15684 struct buffer *buffer = XBUFFER (w->buffer);
15685 struct buffer *old = current_buffer;
15686 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15687 int arrow_len = SCHARS (overlay_arrow_string);
15688 const unsigned char *arrow_end = arrow_string + arrow_len;
15689 const unsigned char *p;
15690 struct it it;
15691 int multibyte_p;
15692 int n_glyphs_before;
15694 set_buffer_temp (buffer);
15695 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15696 it.glyph_row->used[TEXT_AREA] = 0;
15697 SET_TEXT_POS (it.position, 0, 0);
15699 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15700 p = arrow_string;
15701 while (p < arrow_end)
15703 Lisp_Object face, ilisp;
15705 /* Get the next character. */
15706 if (multibyte_p)
15707 it.c = string_char_and_length (p, arrow_len, &it.len);
15708 else
15709 it.c = *p, it.len = 1;
15710 p += it.len;
15712 /* Get its face. */
15713 ilisp = make_number (p - arrow_string);
15714 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15715 it.face_id = compute_char_face (f, it.c, face);
15717 /* Compute its width, get its glyphs. */
15718 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15719 SET_TEXT_POS (it.position, -1, -1);
15720 PRODUCE_GLYPHS (&it);
15722 /* If this character doesn't fit any more in the line, we have
15723 to remove some glyphs. */
15724 if (it.current_x > it.last_visible_x)
15726 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15727 break;
15731 set_buffer_temp (old);
15732 return it.glyph_row;
15736 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15737 glyphs are only inserted for terminal frames since we can't really
15738 win with truncation glyphs when partially visible glyphs are
15739 involved. Which glyphs to insert is determined by
15740 produce_special_glyphs. */
15742 static void
15743 insert_left_trunc_glyphs (it)
15744 struct it *it;
15746 struct it truncate_it;
15747 struct glyph *from, *end, *to, *toend;
15749 xassert (!FRAME_WINDOW_P (it->f));
15751 /* Get the truncation glyphs. */
15752 truncate_it = *it;
15753 truncate_it.current_x = 0;
15754 truncate_it.face_id = DEFAULT_FACE_ID;
15755 truncate_it.glyph_row = &scratch_glyph_row;
15756 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15757 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15758 truncate_it.object = make_number (0);
15759 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15761 /* Overwrite glyphs from IT with truncation glyphs. */
15762 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15763 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15764 to = it->glyph_row->glyphs[TEXT_AREA];
15765 toend = to + it->glyph_row->used[TEXT_AREA];
15767 while (from < end)
15768 *to++ = *from++;
15770 /* There may be padding glyphs left over. Overwrite them too. */
15771 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15773 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15774 while (from < end)
15775 *to++ = *from++;
15778 if (to > toend)
15779 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15783 /* Compute the pixel height and width of IT->glyph_row.
15785 Most of the time, ascent and height of a display line will be equal
15786 to the max_ascent and max_height values of the display iterator
15787 structure. This is not the case if
15789 1. We hit ZV without displaying anything. In this case, max_ascent
15790 and max_height will be zero.
15792 2. We have some glyphs that don't contribute to the line height.
15793 (The glyph row flag contributes_to_line_height_p is for future
15794 pixmap extensions).
15796 The first case is easily covered by using default values because in
15797 these cases, the line height does not really matter, except that it
15798 must not be zero. */
15800 static void
15801 compute_line_metrics (it)
15802 struct it *it;
15804 struct glyph_row *row = it->glyph_row;
15805 int area, i;
15807 if (FRAME_WINDOW_P (it->f))
15809 int i, min_y, max_y;
15811 /* The line may consist of one space only, that was added to
15812 place the cursor on it. If so, the row's height hasn't been
15813 computed yet. */
15814 if (row->height == 0)
15816 if (it->max_ascent + it->max_descent == 0)
15817 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15818 row->ascent = it->max_ascent;
15819 row->height = it->max_ascent + it->max_descent;
15820 row->phys_ascent = it->max_phys_ascent;
15821 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15822 row->extra_line_spacing = it->max_extra_line_spacing;
15825 /* Compute the width of this line. */
15826 row->pixel_width = row->x;
15827 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15828 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15830 xassert (row->pixel_width >= 0);
15831 xassert (row->ascent >= 0 && row->height > 0);
15833 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15834 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15836 /* If first line's physical ascent is larger than its logical
15837 ascent, use the physical ascent, and make the row taller.
15838 This makes accented characters fully visible. */
15839 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15840 && row->phys_ascent > row->ascent)
15842 row->height += row->phys_ascent - row->ascent;
15843 row->ascent = row->phys_ascent;
15846 /* Compute how much of the line is visible. */
15847 row->visible_height = row->height;
15849 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15850 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15852 if (row->y < min_y)
15853 row->visible_height -= min_y - row->y;
15854 if (row->y + row->height > max_y)
15855 row->visible_height -= row->y + row->height - max_y;
15857 else
15859 row->pixel_width = row->used[TEXT_AREA];
15860 if (row->continued_p)
15861 row->pixel_width -= it->continuation_pixel_width;
15862 else if (row->truncated_on_right_p)
15863 row->pixel_width -= it->truncation_pixel_width;
15864 row->ascent = row->phys_ascent = 0;
15865 row->height = row->phys_height = row->visible_height = 1;
15866 row->extra_line_spacing = 0;
15869 /* Compute a hash code for this row. */
15870 row->hash = 0;
15871 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15872 for (i = 0; i < row->used[area]; ++i)
15873 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15874 + row->glyphs[area][i].u.val
15875 + row->glyphs[area][i].face_id
15876 + row->glyphs[area][i].padding_p
15877 + (row->glyphs[area][i].type << 2));
15879 it->max_ascent = it->max_descent = 0;
15880 it->max_phys_ascent = it->max_phys_descent = 0;
15884 /* Append one space to the glyph row of iterator IT if doing a
15885 window-based redisplay. The space has the same face as
15886 IT->face_id. Value is non-zero if a space was added.
15888 This function is called to make sure that there is always one glyph
15889 at the end of a glyph row that the cursor can be set on under
15890 window-systems. (If there weren't such a glyph we would not know
15891 how wide and tall a box cursor should be displayed).
15893 At the same time this space let's a nicely handle clearing to the
15894 end of the line if the row ends in italic text. */
15896 static int
15897 append_space_for_newline (it, default_face_p)
15898 struct it *it;
15899 int default_face_p;
15901 if (FRAME_WINDOW_P (it->f))
15903 int n = it->glyph_row->used[TEXT_AREA];
15905 if (it->glyph_row->glyphs[TEXT_AREA] + n
15906 < it->glyph_row->glyphs[1 + TEXT_AREA])
15908 /* Save some values that must not be changed.
15909 Must save IT->c and IT->len because otherwise
15910 ITERATOR_AT_END_P wouldn't work anymore after
15911 append_space_for_newline has been called. */
15912 enum display_element_type saved_what = it->what;
15913 int saved_c = it->c, saved_len = it->len;
15914 int saved_x = it->current_x;
15915 int saved_face_id = it->face_id;
15916 struct text_pos saved_pos;
15917 Lisp_Object saved_object;
15918 struct face *face;
15920 saved_object = it->object;
15921 saved_pos = it->position;
15923 it->what = IT_CHARACTER;
15924 bzero (&it->position, sizeof it->position);
15925 it->object = make_number (0);
15926 it->c = ' ';
15927 it->len = 1;
15929 if (default_face_p)
15930 it->face_id = DEFAULT_FACE_ID;
15931 else if (it->face_before_selective_p)
15932 it->face_id = it->saved_face_id;
15933 face = FACE_FROM_ID (it->f, it->face_id);
15934 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15936 PRODUCE_GLYPHS (it);
15938 it->override_ascent = -1;
15939 it->constrain_row_ascent_descent_p = 0;
15940 it->current_x = saved_x;
15941 it->object = saved_object;
15942 it->position = saved_pos;
15943 it->what = saved_what;
15944 it->face_id = saved_face_id;
15945 it->len = saved_len;
15946 it->c = saved_c;
15947 return 1;
15951 return 0;
15955 /* Extend the face of the last glyph in the text area of IT->glyph_row
15956 to the end of the display line. Called from display_line.
15957 If the glyph row is empty, add a space glyph to it so that we
15958 know the face to draw. Set the glyph row flag fill_line_p. */
15960 static void
15961 extend_face_to_end_of_line (it)
15962 struct it *it;
15964 struct face *face;
15965 struct frame *f = it->f;
15967 /* If line is already filled, do nothing. */
15968 if (it->current_x >= it->last_visible_x)
15969 return;
15971 /* Face extension extends the background and box of IT->face_id
15972 to the end of the line. If the background equals the background
15973 of the frame, we don't have to do anything. */
15974 if (it->face_before_selective_p)
15975 face = FACE_FROM_ID (it->f, it->saved_face_id);
15976 else
15977 face = FACE_FROM_ID (f, it->face_id);
15979 if (FRAME_WINDOW_P (f)
15980 && it->glyph_row->displays_text_p
15981 && face->box == FACE_NO_BOX
15982 && face->background == FRAME_BACKGROUND_PIXEL (f)
15983 && !face->stipple)
15984 return;
15986 /* Set the glyph row flag indicating that the face of the last glyph
15987 in the text area has to be drawn to the end of the text area. */
15988 it->glyph_row->fill_line_p = 1;
15990 /* If current character of IT is not ASCII, make sure we have the
15991 ASCII face. This will be automatically undone the next time
15992 get_next_display_element returns a multibyte character. Note
15993 that the character will always be single byte in unibyte text. */
15994 if (!ASCII_CHAR_P (it->c))
15996 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15999 if (FRAME_WINDOW_P (f))
16001 /* If the row is empty, add a space with the current face of IT,
16002 so that we know which face to draw. */
16003 if (it->glyph_row->used[TEXT_AREA] == 0)
16005 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16006 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16007 it->glyph_row->used[TEXT_AREA] = 1;
16010 else
16012 /* Save some values that must not be changed. */
16013 int saved_x = it->current_x;
16014 struct text_pos saved_pos;
16015 Lisp_Object saved_object;
16016 enum display_element_type saved_what = it->what;
16017 int saved_face_id = it->face_id;
16019 saved_object = it->object;
16020 saved_pos = it->position;
16022 it->what = IT_CHARACTER;
16023 bzero (&it->position, sizeof it->position);
16024 it->object = make_number (0);
16025 it->c = ' ';
16026 it->len = 1;
16027 it->face_id = face->id;
16029 PRODUCE_GLYPHS (it);
16031 while (it->current_x <= it->last_visible_x)
16032 PRODUCE_GLYPHS (it);
16034 /* Don't count these blanks really. It would let us insert a left
16035 truncation glyph below and make us set the cursor on them, maybe. */
16036 it->current_x = saved_x;
16037 it->object = saved_object;
16038 it->position = saved_pos;
16039 it->what = saved_what;
16040 it->face_id = saved_face_id;
16045 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16046 trailing whitespace. */
16048 static int
16049 trailing_whitespace_p (charpos)
16050 int charpos;
16052 int bytepos = CHAR_TO_BYTE (charpos);
16053 int c = 0;
16055 while (bytepos < ZV_BYTE
16056 && (c = FETCH_CHAR (bytepos),
16057 c == ' ' || c == '\t'))
16058 ++bytepos;
16060 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16062 if (bytepos != PT_BYTE)
16063 return 1;
16065 return 0;
16069 /* Highlight trailing whitespace, if any, in ROW. */
16071 void
16072 highlight_trailing_whitespace (f, row)
16073 struct frame *f;
16074 struct glyph_row *row;
16076 int used = row->used[TEXT_AREA];
16078 if (used)
16080 struct glyph *start = row->glyphs[TEXT_AREA];
16081 struct glyph *glyph = start + used - 1;
16083 /* Skip over glyphs inserted to display the cursor at the
16084 end of a line, for extending the face of the last glyph
16085 to the end of the line on terminals, and for truncation
16086 and continuation glyphs. */
16087 while (glyph >= start
16088 && glyph->type == CHAR_GLYPH
16089 && INTEGERP (glyph->object))
16090 --glyph;
16092 /* If last glyph is a space or stretch, and it's trailing
16093 whitespace, set the face of all trailing whitespace glyphs in
16094 IT->glyph_row to `trailing-whitespace'. */
16095 if (glyph >= start
16096 && BUFFERP (glyph->object)
16097 && (glyph->type == STRETCH_GLYPH
16098 || (glyph->type == CHAR_GLYPH
16099 && glyph->u.ch == ' '))
16100 && trailing_whitespace_p (glyph->charpos))
16102 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16103 if (face_id < 0)
16104 return;
16106 while (glyph >= start
16107 && BUFFERP (glyph->object)
16108 && (glyph->type == STRETCH_GLYPH
16109 || (glyph->type == CHAR_GLYPH
16110 && glyph->u.ch == ' ')))
16111 (glyph--)->face_id = face_id;
16117 /* Value is non-zero if glyph row ROW in window W should be
16118 used to hold the cursor. */
16120 static int
16121 cursor_row_p (w, row)
16122 struct window *w;
16123 struct glyph_row *row;
16125 int cursor_row_p = 1;
16127 if (PT == MATRIX_ROW_END_CHARPOS (row))
16129 /* Suppose the row ends on a string.
16130 Unless the row is continued, that means it ends on a newline
16131 in the string. If it's anything other than a display string
16132 (e.g. a before-string from an overlay), we don't want the
16133 cursor there. (This heuristic seems to give the optimal
16134 behavior for the various types of multi-line strings.) */
16135 if (CHARPOS (row->end.string_pos) >= 0)
16137 if (row->continued_p)
16138 cursor_row_p = 1;
16139 else
16141 /* Check for `display' property. */
16142 struct glyph *beg = row->glyphs[TEXT_AREA];
16143 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16144 struct glyph *glyph;
16146 cursor_row_p = 0;
16147 for (glyph = end; glyph >= beg; --glyph)
16148 if (STRINGP (glyph->object))
16150 Lisp_Object prop
16151 = Fget_char_property (make_number (PT),
16152 Qdisplay, Qnil);
16153 cursor_row_p =
16154 (!NILP (prop)
16155 && display_prop_string_p (prop, glyph->object));
16156 break;
16160 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16162 /* If the row ends in middle of a real character,
16163 and the line is continued, we want the cursor here.
16164 That's because MATRIX_ROW_END_CHARPOS would equal
16165 PT if PT is before the character. */
16166 if (!row->ends_in_ellipsis_p)
16167 cursor_row_p = row->continued_p;
16168 else
16169 /* If the row ends in an ellipsis, then
16170 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16171 We want that position to be displayed after the ellipsis. */
16172 cursor_row_p = 0;
16174 /* If the row ends at ZV, display the cursor at the end of that
16175 row instead of at the start of the row below. */
16176 else if (row->ends_at_zv_p)
16177 cursor_row_p = 1;
16178 else
16179 cursor_row_p = 0;
16182 return cursor_row_p;
16186 /* Construct the glyph row IT->glyph_row in the desired matrix of
16187 IT->w from text at the current position of IT. See dispextern.h
16188 for an overview of struct it. Value is non-zero if
16189 IT->glyph_row displays text, as opposed to a line displaying ZV
16190 only. */
16192 static int
16193 display_line (it)
16194 struct it *it;
16196 struct glyph_row *row = it->glyph_row;
16197 Lisp_Object overlay_arrow_string;
16199 /* We always start displaying at hpos zero even if hscrolled. */
16200 xassert (it->hpos == 0 && it->current_x == 0);
16202 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16203 >= it->w->desired_matrix->nrows)
16205 it->w->nrows_scale_factor++;
16206 fonts_changed_p = 1;
16207 return 0;
16210 /* Is IT->w showing the region? */
16211 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16213 /* Clear the result glyph row and enable it. */
16214 prepare_desired_row (row);
16216 row->y = it->current_y;
16217 row->start = it->start;
16218 row->continuation_lines_width = it->continuation_lines_width;
16219 row->displays_text_p = 1;
16220 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16221 it->starts_in_middle_of_char_p = 0;
16223 /* Arrange the overlays nicely for our purposes. Usually, we call
16224 display_line on only one line at a time, in which case this
16225 can't really hurt too much, or we call it on lines which appear
16226 one after another in the buffer, in which case all calls to
16227 recenter_overlay_lists but the first will be pretty cheap. */
16228 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16230 /* Move over display elements that are not visible because we are
16231 hscrolled. This may stop at an x-position < IT->first_visible_x
16232 if the first glyph is partially visible or if we hit a line end. */
16233 if (it->current_x < it->first_visible_x)
16235 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16236 MOVE_TO_POS | MOVE_TO_X);
16239 /* Get the initial row height. This is either the height of the
16240 text hscrolled, if there is any, or zero. */
16241 row->ascent = it->max_ascent;
16242 row->height = it->max_ascent + it->max_descent;
16243 row->phys_ascent = it->max_phys_ascent;
16244 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16245 row->extra_line_spacing = it->max_extra_line_spacing;
16247 /* Loop generating characters. The loop is left with IT on the next
16248 character to display. */
16249 while (1)
16251 int n_glyphs_before, hpos_before, x_before;
16252 int x, i, nglyphs;
16253 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16255 /* Retrieve the next thing to display. Value is zero if end of
16256 buffer reached. */
16257 if (!get_next_display_element (it))
16259 /* Maybe add a space at the end of this line that is used to
16260 display the cursor there under X. Set the charpos of the
16261 first glyph of blank lines not corresponding to any text
16262 to -1. */
16263 #ifdef HAVE_WINDOW_SYSTEM
16264 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16265 row->exact_window_width_line_p = 1;
16266 else
16267 #endif /* HAVE_WINDOW_SYSTEM */
16268 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16269 || row->used[TEXT_AREA] == 0)
16271 row->glyphs[TEXT_AREA]->charpos = -1;
16272 row->displays_text_p = 0;
16274 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16275 && (!MINI_WINDOW_P (it->w)
16276 || (minibuf_level && EQ (it->window, minibuf_window))))
16277 row->indicate_empty_line_p = 1;
16280 it->continuation_lines_width = 0;
16281 row->ends_at_zv_p = 1;
16282 break;
16285 /* Now, get the metrics of what we want to display. This also
16286 generates glyphs in `row' (which is IT->glyph_row). */
16287 n_glyphs_before = row->used[TEXT_AREA];
16288 x = it->current_x;
16290 /* Remember the line height so far in case the next element doesn't
16291 fit on the line. */
16292 if (!it->truncate_lines_p)
16294 ascent = it->max_ascent;
16295 descent = it->max_descent;
16296 phys_ascent = it->max_phys_ascent;
16297 phys_descent = it->max_phys_descent;
16300 PRODUCE_GLYPHS (it);
16302 /* If this display element was in marginal areas, continue with
16303 the next one. */
16304 if (it->area != TEXT_AREA)
16306 row->ascent = max (row->ascent, it->max_ascent);
16307 row->height = max (row->height, it->max_ascent + it->max_descent);
16308 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16309 row->phys_height = max (row->phys_height,
16310 it->max_phys_ascent + it->max_phys_descent);
16311 row->extra_line_spacing = max (row->extra_line_spacing,
16312 it->max_extra_line_spacing);
16313 set_iterator_to_next (it, 1);
16314 continue;
16317 /* Does the display element fit on the line? If we truncate
16318 lines, we should draw past the right edge of the window. If
16319 we don't truncate, we want to stop so that we can display the
16320 continuation glyph before the right margin. If lines are
16321 continued, there are two possible strategies for characters
16322 resulting in more than 1 glyph (e.g. tabs): Display as many
16323 glyphs as possible in this line and leave the rest for the
16324 continuation line, or display the whole element in the next
16325 line. Original redisplay did the former, so we do it also. */
16326 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16327 hpos_before = it->hpos;
16328 x_before = x;
16330 if (/* Not a newline. */
16331 nglyphs > 0
16332 /* Glyphs produced fit entirely in the line. */
16333 && it->current_x < it->last_visible_x)
16335 it->hpos += nglyphs;
16336 row->ascent = max (row->ascent, it->max_ascent);
16337 row->height = max (row->height, it->max_ascent + it->max_descent);
16338 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16339 row->phys_height = max (row->phys_height,
16340 it->max_phys_ascent + it->max_phys_descent);
16341 row->extra_line_spacing = max (row->extra_line_spacing,
16342 it->max_extra_line_spacing);
16343 if (it->current_x - it->pixel_width < it->first_visible_x)
16344 row->x = x - it->first_visible_x;
16346 else
16348 int new_x;
16349 struct glyph *glyph;
16351 for (i = 0; i < nglyphs; ++i, x = new_x)
16353 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16354 new_x = x + glyph->pixel_width;
16356 if (/* Lines are continued. */
16357 !it->truncate_lines_p
16358 && (/* Glyph doesn't fit on the line. */
16359 new_x > it->last_visible_x
16360 /* Or it fits exactly on a window system frame. */
16361 || (new_x == it->last_visible_x
16362 && FRAME_WINDOW_P (it->f))))
16364 /* End of a continued line. */
16366 if (it->hpos == 0
16367 || (new_x == it->last_visible_x
16368 && FRAME_WINDOW_P (it->f)))
16370 /* Current glyph is the only one on the line or
16371 fits exactly on the line. We must continue
16372 the line because we can't draw the cursor
16373 after the glyph. */
16374 row->continued_p = 1;
16375 it->current_x = new_x;
16376 it->continuation_lines_width += new_x;
16377 ++it->hpos;
16378 if (i == nglyphs - 1)
16380 set_iterator_to_next (it, 1);
16381 #ifdef HAVE_WINDOW_SYSTEM
16382 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16384 if (!get_next_display_element (it))
16386 row->exact_window_width_line_p = 1;
16387 it->continuation_lines_width = 0;
16388 row->continued_p = 0;
16389 row->ends_at_zv_p = 1;
16391 else if (ITERATOR_AT_END_OF_LINE_P (it))
16393 row->continued_p = 0;
16394 row->exact_window_width_line_p = 1;
16397 #endif /* HAVE_WINDOW_SYSTEM */
16400 else if (CHAR_GLYPH_PADDING_P (*glyph)
16401 && !FRAME_WINDOW_P (it->f))
16403 /* A padding glyph that doesn't fit on this line.
16404 This means the whole character doesn't fit
16405 on the line. */
16406 row->used[TEXT_AREA] = n_glyphs_before;
16408 /* Fill the rest of the row with continuation
16409 glyphs like in 20.x. */
16410 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16411 < row->glyphs[1 + TEXT_AREA])
16412 produce_special_glyphs (it, IT_CONTINUATION);
16414 row->continued_p = 1;
16415 it->current_x = x_before;
16416 it->continuation_lines_width += x_before;
16418 /* Restore the height to what it was before the
16419 element not fitting on the line. */
16420 it->max_ascent = ascent;
16421 it->max_descent = descent;
16422 it->max_phys_ascent = phys_ascent;
16423 it->max_phys_descent = phys_descent;
16425 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16427 /* A TAB that extends past the right edge of the
16428 window. This produces a single glyph on
16429 window system frames. We leave the glyph in
16430 this row and let it fill the row, but don't
16431 consume the TAB. */
16432 it->continuation_lines_width += it->last_visible_x;
16433 row->ends_in_middle_of_char_p = 1;
16434 row->continued_p = 1;
16435 glyph->pixel_width = it->last_visible_x - x;
16436 it->starts_in_middle_of_char_p = 1;
16438 else
16440 /* Something other than a TAB that draws past
16441 the right edge of the window. Restore
16442 positions to values before the element. */
16443 row->used[TEXT_AREA] = n_glyphs_before + i;
16445 /* Display continuation glyphs. */
16446 if (!FRAME_WINDOW_P (it->f))
16447 produce_special_glyphs (it, IT_CONTINUATION);
16448 row->continued_p = 1;
16450 it->current_x = x_before;
16451 it->continuation_lines_width += x;
16452 extend_face_to_end_of_line (it);
16454 if (nglyphs > 1 && i > 0)
16456 row->ends_in_middle_of_char_p = 1;
16457 it->starts_in_middle_of_char_p = 1;
16460 /* Restore the height to what it was before the
16461 element not fitting on the line. */
16462 it->max_ascent = ascent;
16463 it->max_descent = descent;
16464 it->max_phys_ascent = phys_ascent;
16465 it->max_phys_descent = phys_descent;
16468 break;
16470 else if (new_x > it->first_visible_x)
16472 /* Increment number of glyphs actually displayed. */
16473 ++it->hpos;
16475 if (x < it->first_visible_x)
16476 /* Glyph is partially visible, i.e. row starts at
16477 negative X position. */
16478 row->x = x - it->first_visible_x;
16480 else
16482 /* Glyph is completely off the left margin of the
16483 window. This should not happen because of the
16484 move_it_in_display_line at the start of this
16485 function, unless the text display area of the
16486 window is empty. */
16487 xassert (it->first_visible_x <= it->last_visible_x);
16491 row->ascent = max (row->ascent, it->max_ascent);
16492 row->height = max (row->height, it->max_ascent + it->max_descent);
16493 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16494 row->phys_height = max (row->phys_height,
16495 it->max_phys_ascent + it->max_phys_descent);
16496 row->extra_line_spacing = max (row->extra_line_spacing,
16497 it->max_extra_line_spacing);
16499 /* End of this display line if row is continued. */
16500 if (row->continued_p || row->ends_at_zv_p)
16501 break;
16504 at_end_of_line:
16505 /* Is this a line end? If yes, we're also done, after making
16506 sure that a non-default face is extended up to the right
16507 margin of the window. */
16508 if (ITERATOR_AT_END_OF_LINE_P (it))
16510 int used_before = row->used[TEXT_AREA];
16512 row->ends_in_newline_from_string_p = STRINGP (it->object);
16514 #ifdef HAVE_WINDOW_SYSTEM
16515 /* Add a space at the end of the line that is used to
16516 display the cursor there. */
16517 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16518 append_space_for_newline (it, 0);
16519 #endif /* HAVE_WINDOW_SYSTEM */
16521 /* Extend the face to the end of the line. */
16522 extend_face_to_end_of_line (it);
16524 /* Make sure we have the position. */
16525 if (used_before == 0)
16526 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16528 /* Consume the line end. This skips over invisible lines. */
16529 set_iterator_to_next (it, 1);
16530 it->continuation_lines_width = 0;
16531 break;
16534 /* Proceed with next display element. Note that this skips
16535 over lines invisible because of selective display. */
16536 set_iterator_to_next (it, 1);
16538 /* If we truncate lines, we are done when the last displayed
16539 glyphs reach past the right margin of the window. */
16540 if (it->truncate_lines_p
16541 && (FRAME_WINDOW_P (it->f)
16542 ? (it->current_x >= it->last_visible_x)
16543 : (it->current_x > it->last_visible_x)))
16545 /* Maybe add truncation glyphs. */
16546 if (!FRAME_WINDOW_P (it->f))
16548 int i, n;
16550 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16551 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16552 break;
16554 for (n = row->used[TEXT_AREA]; i < n; ++i)
16556 row->used[TEXT_AREA] = i;
16557 produce_special_glyphs (it, IT_TRUNCATION);
16560 #ifdef HAVE_WINDOW_SYSTEM
16561 else
16563 /* Don't truncate if we can overflow newline into fringe. */
16564 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16566 if (!get_next_display_element (it))
16568 it->continuation_lines_width = 0;
16569 row->ends_at_zv_p = 1;
16570 row->exact_window_width_line_p = 1;
16571 break;
16573 if (ITERATOR_AT_END_OF_LINE_P (it))
16575 row->exact_window_width_line_p = 1;
16576 goto at_end_of_line;
16580 #endif /* HAVE_WINDOW_SYSTEM */
16582 row->truncated_on_right_p = 1;
16583 it->continuation_lines_width = 0;
16584 reseat_at_next_visible_line_start (it, 0);
16585 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16586 it->hpos = hpos_before;
16587 it->current_x = x_before;
16588 break;
16592 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16593 at the left window margin. */
16594 if (it->first_visible_x
16595 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16597 if (!FRAME_WINDOW_P (it->f))
16598 insert_left_trunc_glyphs (it);
16599 row->truncated_on_left_p = 1;
16602 /* If the start of this line is the overlay arrow-position, then
16603 mark this glyph row as the one containing the overlay arrow.
16604 This is clearly a mess with variable size fonts. It would be
16605 better to let it be displayed like cursors under X. */
16606 if ((row->displays_text_p || !overlay_arrow_seen)
16607 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16608 !NILP (overlay_arrow_string)))
16610 /* Overlay arrow in window redisplay is a fringe bitmap. */
16611 if (STRINGP (overlay_arrow_string))
16613 struct glyph_row *arrow_row
16614 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16615 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16616 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16617 struct glyph *p = row->glyphs[TEXT_AREA];
16618 struct glyph *p2, *end;
16620 /* Copy the arrow glyphs. */
16621 while (glyph < arrow_end)
16622 *p++ = *glyph++;
16624 /* Throw away padding glyphs. */
16625 p2 = p;
16626 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16627 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16628 ++p2;
16629 if (p2 > p)
16631 while (p2 < end)
16632 *p++ = *p2++;
16633 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16636 else
16638 xassert (INTEGERP (overlay_arrow_string));
16639 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16641 overlay_arrow_seen = 1;
16644 /* Compute pixel dimensions of this line. */
16645 compute_line_metrics (it);
16647 /* Remember the position at which this line ends. */
16648 row->end = it->current;
16650 /* Record whether this row ends inside an ellipsis. */
16651 row->ends_in_ellipsis_p
16652 = (it->method == GET_FROM_DISPLAY_VECTOR
16653 && it->ellipsis_p);
16655 /* Save fringe bitmaps in this row. */
16656 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16657 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16658 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16659 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16661 it->left_user_fringe_bitmap = 0;
16662 it->left_user_fringe_face_id = 0;
16663 it->right_user_fringe_bitmap = 0;
16664 it->right_user_fringe_face_id = 0;
16666 /* Maybe set the cursor. */
16667 if (it->w->cursor.vpos < 0
16668 && PT >= MATRIX_ROW_START_CHARPOS (row)
16669 && PT <= MATRIX_ROW_END_CHARPOS (row)
16670 && cursor_row_p (it->w, row))
16671 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16673 /* Highlight trailing whitespace. */
16674 if (!NILP (Vshow_trailing_whitespace))
16675 highlight_trailing_whitespace (it->f, it->glyph_row);
16677 /* Prepare for the next line. This line starts horizontally at (X
16678 HPOS) = (0 0). Vertical positions are incremented. As a
16679 convenience for the caller, IT->glyph_row is set to the next
16680 row to be used. */
16681 it->current_x = it->hpos = 0;
16682 it->current_y += row->height;
16683 ++it->vpos;
16684 ++it->glyph_row;
16685 it->start = it->current;
16686 return row->displays_text_p;
16691 /***********************************************************************
16692 Menu Bar
16693 ***********************************************************************/
16695 /* Redisplay the menu bar in the frame for window W.
16697 The menu bar of X frames that don't have X toolkit support is
16698 displayed in a special window W->frame->menu_bar_window.
16700 The menu bar of terminal frames is treated specially as far as
16701 glyph matrices are concerned. Menu bar lines are not part of
16702 windows, so the update is done directly on the frame matrix rows
16703 for the menu bar. */
16705 static void
16706 display_menu_bar (w)
16707 struct window *w;
16709 struct frame *f = XFRAME (WINDOW_FRAME (w));
16710 struct it it;
16711 Lisp_Object items;
16712 int i;
16714 /* Don't do all this for graphical frames. */
16715 #ifdef HAVE_NTGUI
16716 if (FRAME_W32_P (f))
16717 return;
16718 #endif
16719 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16720 if (FRAME_X_P (f))
16721 return;
16722 #endif
16723 #ifdef MAC_OS
16724 if (FRAME_MAC_P (f))
16725 return;
16726 #endif
16728 #ifdef USE_X_TOOLKIT
16729 xassert (!FRAME_WINDOW_P (f));
16730 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16731 it.first_visible_x = 0;
16732 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16733 #else /* not USE_X_TOOLKIT */
16734 if (FRAME_WINDOW_P (f))
16736 /* Menu bar lines are displayed in the desired matrix of the
16737 dummy window menu_bar_window. */
16738 struct window *menu_w;
16739 xassert (WINDOWP (f->menu_bar_window));
16740 menu_w = XWINDOW (f->menu_bar_window);
16741 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16742 MENU_FACE_ID);
16743 it.first_visible_x = 0;
16744 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16746 else
16748 /* This is a TTY frame, i.e. character hpos/vpos are used as
16749 pixel x/y. */
16750 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16751 MENU_FACE_ID);
16752 it.first_visible_x = 0;
16753 it.last_visible_x = FRAME_COLS (f);
16755 #endif /* not USE_X_TOOLKIT */
16757 if (! mode_line_inverse_video)
16758 /* Force the menu-bar to be displayed in the default face. */
16759 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16761 /* Clear all rows of the menu bar. */
16762 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16764 struct glyph_row *row = it.glyph_row + i;
16765 clear_glyph_row (row);
16766 row->enabled_p = 1;
16767 row->full_width_p = 1;
16770 /* Display all items of the menu bar. */
16771 items = FRAME_MENU_BAR_ITEMS (it.f);
16772 for (i = 0; i < XVECTOR (items)->size; i += 4)
16774 Lisp_Object string;
16776 /* Stop at nil string. */
16777 string = AREF (items, i + 1);
16778 if (NILP (string))
16779 break;
16781 /* Remember where item was displayed. */
16782 ASET (items, i + 3, make_number (it.hpos));
16784 /* Display the item, pad with one space. */
16785 if (it.current_x < it.last_visible_x)
16786 display_string (NULL, string, Qnil, 0, 0, &it,
16787 SCHARS (string) + 1, 0, 0, -1);
16790 /* Fill out the line with spaces. */
16791 if (it.current_x < it.last_visible_x)
16792 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16794 /* Compute the total height of the lines. */
16795 compute_line_metrics (&it);
16800 /***********************************************************************
16801 Mode Line
16802 ***********************************************************************/
16804 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16805 FORCE is non-zero, redisplay mode lines unconditionally.
16806 Otherwise, redisplay only mode lines that are garbaged. Value is
16807 the number of windows whose mode lines were redisplayed. */
16809 static int
16810 redisplay_mode_lines (window, force)
16811 Lisp_Object window;
16812 int force;
16814 int nwindows = 0;
16816 while (!NILP (window))
16818 struct window *w = XWINDOW (window);
16820 if (WINDOWP (w->hchild))
16821 nwindows += redisplay_mode_lines (w->hchild, force);
16822 else if (WINDOWP (w->vchild))
16823 nwindows += redisplay_mode_lines (w->vchild, force);
16824 else if (force
16825 || FRAME_GARBAGED_P (XFRAME (w->frame))
16826 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16828 struct text_pos lpoint;
16829 struct buffer *old = current_buffer;
16831 /* Set the window's buffer for the mode line display. */
16832 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16833 set_buffer_internal_1 (XBUFFER (w->buffer));
16835 /* Point refers normally to the selected window. For any
16836 other window, set up appropriate value. */
16837 if (!EQ (window, selected_window))
16839 struct text_pos pt;
16841 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16842 if (CHARPOS (pt) < BEGV)
16843 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16844 else if (CHARPOS (pt) > (ZV - 1))
16845 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16846 else
16847 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16850 /* Display mode lines. */
16851 clear_glyph_matrix (w->desired_matrix);
16852 if (display_mode_lines (w))
16854 ++nwindows;
16855 w->must_be_updated_p = 1;
16858 /* Restore old settings. */
16859 set_buffer_internal_1 (old);
16860 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16863 window = w->next;
16866 return nwindows;
16870 /* Display the mode and/or top line of window W. Value is the number
16871 of mode lines displayed. */
16873 static int
16874 display_mode_lines (w)
16875 struct window *w;
16877 Lisp_Object old_selected_window, old_selected_frame;
16878 int n = 0;
16880 old_selected_frame = selected_frame;
16881 selected_frame = w->frame;
16882 old_selected_window = selected_window;
16883 XSETWINDOW (selected_window, w);
16885 /* These will be set while the mode line specs are processed. */
16886 line_number_displayed = 0;
16887 w->column_number_displayed = Qnil;
16889 if (WINDOW_WANTS_MODELINE_P (w))
16891 struct window *sel_w = XWINDOW (old_selected_window);
16893 /* Select mode line face based on the real selected window. */
16894 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16895 current_buffer->mode_line_format);
16896 ++n;
16899 if (WINDOW_WANTS_HEADER_LINE_P (w))
16901 display_mode_line (w, HEADER_LINE_FACE_ID,
16902 current_buffer->header_line_format);
16903 ++n;
16906 selected_frame = old_selected_frame;
16907 selected_window = old_selected_window;
16908 return n;
16912 /* Display mode or top line of window W. FACE_ID specifies which line
16913 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16914 FORMAT is the mode line format to display. Value is the pixel
16915 height of the mode line displayed. */
16917 static int
16918 display_mode_line (w, face_id, format)
16919 struct window *w;
16920 enum face_id face_id;
16921 Lisp_Object format;
16923 struct it it;
16924 struct face *face;
16925 int count = SPECPDL_INDEX ();
16927 init_iterator (&it, w, -1, -1, NULL, face_id);
16928 /* Don't extend on a previously drawn mode-line.
16929 This may happen if called from pos_visible_p. */
16930 it.glyph_row->enabled_p = 0;
16931 prepare_desired_row (it.glyph_row);
16933 it.glyph_row->mode_line_p = 1;
16935 if (! mode_line_inverse_video)
16936 /* Force the mode-line to be displayed in the default face. */
16937 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16939 record_unwind_protect (unwind_format_mode_line,
16940 format_mode_line_unwind_data (NULL, 0));
16942 mode_line_target = MODE_LINE_DISPLAY;
16944 /* Temporarily make frame's keyboard the current kboard so that
16945 kboard-local variables in the mode_line_format will get the right
16946 values. */
16947 push_kboard (FRAME_KBOARD (it.f));
16948 record_unwind_save_match_data ();
16949 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16950 pop_kboard ();
16952 unbind_to (count, Qnil);
16954 /* Fill up with spaces. */
16955 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16957 compute_line_metrics (&it);
16958 it.glyph_row->full_width_p = 1;
16959 it.glyph_row->continued_p = 0;
16960 it.glyph_row->truncated_on_left_p = 0;
16961 it.glyph_row->truncated_on_right_p = 0;
16963 /* Make a 3D mode-line have a shadow at its right end. */
16964 face = FACE_FROM_ID (it.f, face_id);
16965 extend_face_to_end_of_line (&it);
16966 if (face->box != FACE_NO_BOX)
16968 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16969 + it.glyph_row->used[TEXT_AREA] - 1);
16970 last->right_box_line_p = 1;
16973 return it.glyph_row->height;
16976 /* Move element ELT in LIST to the front of LIST.
16977 Return the updated list. */
16979 static Lisp_Object
16980 move_elt_to_front (elt, list)
16981 Lisp_Object elt, list;
16983 register Lisp_Object tail, prev;
16984 register Lisp_Object tem;
16986 tail = list;
16987 prev = Qnil;
16988 while (CONSP (tail))
16990 tem = XCAR (tail);
16992 if (EQ (elt, tem))
16994 /* Splice out the link TAIL. */
16995 if (NILP (prev))
16996 list = XCDR (tail);
16997 else
16998 Fsetcdr (prev, XCDR (tail));
17000 /* Now make it the first. */
17001 Fsetcdr (tail, list);
17002 return tail;
17004 else
17005 prev = tail;
17006 tail = XCDR (tail);
17007 QUIT;
17010 /* Not found--return unchanged LIST. */
17011 return list;
17014 /* Contribute ELT to the mode line for window IT->w. How it
17015 translates into text depends on its data type.
17017 IT describes the display environment in which we display, as usual.
17019 DEPTH is the depth in recursion. It is used to prevent
17020 infinite recursion here.
17022 FIELD_WIDTH is the number of characters the display of ELT should
17023 occupy in the mode line, and PRECISION is the maximum number of
17024 characters to display from ELT's representation. See
17025 display_string for details.
17027 Returns the hpos of the end of the text generated by ELT.
17029 PROPS is a property list to add to any string we encounter.
17031 If RISKY is nonzero, remove (disregard) any properties in any string
17032 we encounter, and ignore :eval and :propertize.
17034 The global variable `mode_line_target' determines whether the
17035 output is passed to `store_mode_line_noprop',
17036 `store_mode_line_string', or `display_string'. */
17038 static int
17039 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17040 struct it *it;
17041 int depth;
17042 int field_width, precision;
17043 Lisp_Object elt, props;
17044 int risky;
17046 int n = 0, field, prec;
17047 int literal = 0;
17049 tail_recurse:
17050 if (depth > 100)
17051 elt = build_string ("*too-deep*");
17053 depth++;
17055 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17057 case Lisp_String:
17059 /* A string: output it and check for %-constructs within it. */
17060 unsigned char c;
17061 int offset = 0;
17063 if (SCHARS (elt) > 0
17064 && (!NILP (props) || risky))
17066 Lisp_Object oprops, aelt;
17067 oprops = Ftext_properties_at (make_number (0), elt);
17069 /* If the starting string's properties are not what
17070 we want, translate the string. Also, if the string
17071 is risky, do that anyway. */
17073 if (NILP (Fequal (props, oprops)) || risky)
17075 /* If the starting string has properties,
17076 merge the specified ones onto the existing ones. */
17077 if (! NILP (oprops) && !risky)
17079 Lisp_Object tem;
17081 oprops = Fcopy_sequence (oprops);
17082 tem = props;
17083 while (CONSP (tem))
17085 oprops = Fplist_put (oprops, XCAR (tem),
17086 XCAR (XCDR (tem)));
17087 tem = XCDR (XCDR (tem));
17089 props = oprops;
17092 aelt = Fassoc (elt, mode_line_proptrans_alist);
17093 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17095 /* AELT is what we want. Move it to the front
17096 without consing. */
17097 elt = XCAR (aelt);
17098 mode_line_proptrans_alist
17099 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17101 else
17103 Lisp_Object tem;
17105 /* If AELT has the wrong props, it is useless.
17106 so get rid of it. */
17107 if (! NILP (aelt))
17108 mode_line_proptrans_alist
17109 = Fdelq (aelt, mode_line_proptrans_alist);
17111 elt = Fcopy_sequence (elt);
17112 Fset_text_properties (make_number (0), Flength (elt),
17113 props, elt);
17114 /* Add this item to mode_line_proptrans_alist. */
17115 mode_line_proptrans_alist
17116 = Fcons (Fcons (elt, props),
17117 mode_line_proptrans_alist);
17118 /* Truncate mode_line_proptrans_alist
17119 to at most 50 elements. */
17120 tem = Fnthcdr (make_number (50),
17121 mode_line_proptrans_alist);
17122 if (! NILP (tem))
17123 XSETCDR (tem, Qnil);
17128 offset = 0;
17130 if (literal)
17132 prec = precision - n;
17133 switch (mode_line_target)
17135 case MODE_LINE_NOPROP:
17136 case MODE_LINE_TITLE:
17137 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17138 break;
17139 case MODE_LINE_STRING:
17140 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17141 break;
17142 case MODE_LINE_DISPLAY:
17143 n += display_string (NULL, elt, Qnil, 0, 0, it,
17144 0, prec, 0, STRING_MULTIBYTE (elt));
17145 break;
17148 break;
17151 /* Handle the non-literal case. */
17153 while ((precision <= 0 || n < precision)
17154 && SREF (elt, offset) != 0
17155 && (mode_line_target != MODE_LINE_DISPLAY
17156 || it->current_x < it->last_visible_x))
17158 int last_offset = offset;
17160 /* Advance to end of string or next format specifier. */
17161 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17164 if (offset - 1 != last_offset)
17166 int nchars, nbytes;
17168 /* Output to end of string or up to '%'. Field width
17169 is length of string. Don't output more than
17170 PRECISION allows us. */
17171 offset--;
17173 prec = c_string_width (SDATA (elt) + last_offset,
17174 offset - last_offset, precision - n,
17175 &nchars, &nbytes);
17177 switch (mode_line_target)
17179 case MODE_LINE_NOPROP:
17180 case MODE_LINE_TITLE:
17181 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17182 break;
17183 case MODE_LINE_STRING:
17185 int bytepos = last_offset;
17186 int charpos = string_byte_to_char (elt, bytepos);
17187 int endpos = (precision <= 0
17188 ? string_byte_to_char (elt, offset)
17189 : charpos + nchars);
17191 n += store_mode_line_string (NULL,
17192 Fsubstring (elt, make_number (charpos),
17193 make_number (endpos)),
17194 0, 0, 0, Qnil);
17196 break;
17197 case MODE_LINE_DISPLAY:
17199 int bytepos = last_offset;
17200 int charpos = string_byte_to_char (elt, bytepos);
17202 if (precision <= 0)
17203 nchars = string_byte_to_char (elt, offset) - charpos;
17204 n += display_string (NULL, elt, Qnil, 0, charpos,
17205 it, 0, nchars, 0,
17206 STRING_MULTIBYTE (elt));
17208 break;
17211 else /* c == '%' */
17213 int percent_position = offset;
17215 /* Get the specified minimum width. Zero means
17216 don't pad. */
17217 field = 0;
17218 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17219 field = field * 10 + c - '0';
17221 /* Don't pad beyond the total padding allowed. */
17222 if (field_width - n > 0 && field > field_width - n)
17223 field = field_width - n;
17225 /* Note that either PRECISION <= 0 or N < PRECISION. */
17226 prec = precision - n;
17228 if (c == 'M')
17229 n += display_mode_element (it, depth, field, prec,
17230 Vglobal_mode_string, props,
17231 risky);
17232 else if (c != 0)
17234 int multibyte;
17235 int bytepos, charpos;
17236 unsigned char *spec;
17238 bytepos = percent_position;
17239 charpos = (STRING_MULTIBYTE (elt)
17240 ? string_byte_to_char (elt, bytepos)
17241 : bytepos);
17243 spec
17244 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17246 switch (mode_line_target)
17248 case MODE_LINE_NOPROP:
17249 case MODE_LINE_TITLE:
17250 n += store_mode_line_noprop (spec, field, prec);
17251 break;
17252 case MODE_LINE_STRING:
17254 int len = strlen (spec);
17255 Lisp_Object tem = make_string (spec, len);
17256 props = Ftext_properties_at (make_number (charpos), elt);
17257 /* Should only keep face property in props */
17258 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17260 break;
17261 case MODE_LINE_DISPLAY:
17263 int nglyphs_before, nwritten;
17265 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17266 nwritten = display_string (spec, Qnil, elt,
17267 charpos, 0, it,
17268 field, prec, 0,
17269 multibyte);
17271 /* Assign to the glyphs written above the
17272 string where the `%x' came from, position
17273 of the `%'. */
17274 if (nwritten > 0)
17276 struct glyph *glyph
17277 = (it->glyph_row->glyphs[TEXT_AREA]
17278 + nglyphs_before);
17279 int i;
17281 for (i = 0; i < nwritten; ++i)
17283 glyph[i].object = elt;
17284 glyph[i].charpos = charpos;
17287 n += nwritten;
17290 break;
17293 else /* c == 0 */
17294 break;
17298 break;
17300 case Lisp_Symbol:
17301 /* A symbol: process the value of the symbol recursively
17302 as if it appeared here directly. Avoid error if symbol void.
17303 Special case: if value of symbol is a string, output the string
17304 literally. */
17306 register Lisp_Object tem;
17308 /* If the variable is not marked as risky to set
17309 then its contents are risky to use. */
17310 if (NILP (Fget (elt, Qrisky_local_variable)))
17311 risky = 1;
17313 tem = Fboundp (elt);
17314 if (!NILP (tem))
17316 tem = Fsymbol_value (elt);
17317 /* If value is a string, output that string literally:
17318 don't check for % within it. */
17319 if (STRINGP (tem))
17320 literal = 1;
17322 if (!EQ (tem, elt))
17324 /* Give up right away for nil or t. */
17325 elt = tem;
17326 goto tail_recurse;
17330 break;
17332 case Lisp_Cons:
17334 register Lisp_Object car, tem;
17336 /* A cons cell: five distinct cases.
17337 If first element is :eval or :propertize, do something special.
17338 If first element is a string or a cons, process all the elements
17339 and effectively concatenate them.
17340 If first element is a negative number, truncate displaying cdr to
17341 at most that many characters. If positive, pad (with spaces)
17342 to at least that many characters.
17343 If first element is a symbol, process the cadr or caddr recursively
17344 according to whether the symbol's value is non-nil or nil. */
17345 car = XCAR (elt);
17346 if (EQ (car, QCeval))
17348 /* An element of the form (:eval FORM) means evaluate FORM
17349 and use the result as mode line elements. */
17351 if (risky)
17352 break;
17354 if (CONSP (XCDR (elt)))
17356 Lisp_Object spec;
17357 spec = safe_eval (XCAR (XCDR (elt)));
17358 n += display_mode_element (it, depth, field_width - n,
17359 precision - n, spec, props,
17360 risky);
17363 else if (EQ (car, QCpropertize))
17365 /* An element of the form (:propertize ELT PROPS...)
17366 means display ELT but applying properties PROPS. */
17368 if (risky)
17369 break;
17371 if (CONSP (XCDR (elt)))
17372 n += display_mode_element (it, depth, field_width - n,
17373 precision - n, XCAR (XCDR (elt)),
17374 XCDR (XCDR (elt)), risky);
17376 else if (SYMBOLP (car))
17378 tem = Fboundp (car);
17379 elt = XCDR (elt);
17380 if (!CONSP (elt))
17381 goto invalid;
17382 /* elt is now the cdr, and we know it is a cons cell.
17383 Use its car if CAR has a non-nil value. */
17384 if (!NILP (tem))
17386 tem = Fsymbol_value (car);
17387 if (!NILP (tem))
17389 elt = XCAR (elt);
17390 goto tail_recurse;
17393 /* Symbol's value is nil (or symbol is unbound)
17394 Get the cddr of the original list
17395 and if possible find the caddr and use that. */
17396 elt = XCDR (elt);
17397 if (NILP (elt))
17398 break;
17399 else if (!CONSP (elt))
17400 goto invalid;
17401 elt = XCAR (elt);
17402 goto tail_recurse;
17404 else if (INTEGERP (car))
17406 register int lim = XINT (car);
17407 elt = XCDR (elt);
17408 if (lim < 0)
17410 /* Negative int means reduce maximum width. */
17411 if (precision <= 0)
17412 precision = -lim;
17413 else
17414 precision = min (precision, -lim);
17416 else if (lim > 0)
17418 /* Padding specified. Don't let it be more than
17419 current maximum. */
17420 if (precision > 0)
17421 lim = min (precision, lim);
17423 /* If that's more padding than already wanted, queue it.
17424 But don't reduce padding already specified even if
17425 that is beyond the current truncation point. */
17426 field_width = max (lim, field_width);
17428 goto tail_recurse;
17430 else if (STRINGP (car) || CONSP (car))
17432 register int limit = 50;
17433 /* Limit is to protect against circular lists. */
17434 while (CONSP (elt)
17435 && --limit > 0
17436 && (precision <= 0 || n < precision))
17438 n += display_mode_element (it, depth,
17439 /* Do padding only after the last
17440 element in the list. */
17441 (! CONSP (XCDR (elt))
17442 ? field_width - n
17443 : 0),
17444 precision - n, XCAR (elt),
17445 props, risky);
17446 elt = XCDR (elt);
17450 break;
17452 default:
17453 invalid:
17454 elt = build_string ("*invalid*");
17455 goto tail_recurse;
17458 /* Pad to FIELD_WIDTH. */
17459 if (field_width > 0 && n < field_width)
17461 switch (mode_line_target)
17463 case MODE_LINE_NOPROP:
17464 case MODE_LINE_TITLE:
17465 n += store_mode_line_noprop ("", field_width - n, 0);
17466 break;
17467 case MODE_LINE_STRING:
17468 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17469 break;
17470 case MODE_LINE_DISPLAY:
17471 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17472 0, 0, 0);
17473 break;
17477 return n;
17480 /* Store a mode-line string element in mode_line_string_list.
17482 If STRING is non-null, display that C string. Otherwise, the Lisp
17483 string LISP_STRING is displayed.
17485 FIELD_WIDTH is the minimum number of output glyphs to produce.
17486 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17487 with spaces. FIELD_WIDTH <= 0 means don't pad.
17489 PRECISION is the maximum number of characters to output from
17490 STRING. PRECISION <= 0 means don't truncate the string.
17492 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17493 properties to the string.
17495 PROPS are the properties to add to the string.
17496 The mode_line_string_face face property is always added to the string.
17499 static int
17500 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17501 char *string;
17502 Lisp_Object lisp_string;
17503 int copy_string;
17504 int field_width;
17505 int precision;
17506 Lisp_Object props;
17508 int len;
17509 int n = 0;
17511 if (string != NULL)
17513 len = strlen (string);
17514 if (precision > 0 && len > precision)
17515 len = precision;
17516 lisp_string = make_string (string, len);
17517 if (NILP (props))
17518 props = mode_line_string_face_prop;
17519 else if (!NILP (mode_line_string_face))
17521 Lisp_Object face = Fplist_get (props, Qface);
17522 props = Fcopy_sequence (props);
17523 if (NILP (face))
17524 face = mode_line_string_face;
17525 else
17526 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17527 props = Fplist_put (props, Qface, face);
17529 Fadd_text_properties (make_number (0), make_number (len),
17530 props, lisp_string);
17532 else
17534 len = XFASTINT (Flength (lisp_string));
17535 if (precision > 0 && len > precision)
17537 len = precision;
17538 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17539 precision = -1;
17541 if (!NILP (mode_line_string_face))
17543 Lisp_Object face;
17544 if (NILP (props))
17545 props = Ftext_properties_at (make_number (0), lisp_string);
17546 face = Fplist_get (props, Qface);
17547 if (NILP (face))
17548 face = mode_line_string_face;
17549 else
17550 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17551 props = Fcons (Qface, Fcons (face, Qnil));
17552 if (copy_string)
17553 lisp_string = Fcopy_sequence (lisp_string);
17555 if (!NILP (props))
17556 Fadd_text_properties (make_number (0), make_number (len),
17557 props, lisp_string);
17560 if (len > 0)
17562 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17563 n += len;
17566 if (field_width > len)
17568 field_width -= len;
17569 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17570 if (!NILP (props))
17571 Fadd_text_properties (make_number (0), make_number (field_width),
17572 props, lisp_string);
17573 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17574 n += field_width;
17577 return n;
17581 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17582 1, 4, 0,
17583 doc: /* Format a string out of a mode line format specification.
17584 First arg FORMAT specifies the mode line format (see `mode-line-format'
17585 for details) to use.
17587 Optional second arg FACE specifies the face property to put
17588 on all characters for which no face is specified.
17589 The value t means whatever face the window's mode line currently uses
17590 \(either `mode-line' or `mode-line-inactive', depending).
17591 A value of nil means the default is no face property.
17592 If FACE is an integer, the value string has no text properties.
17594 Optional third and fourth args WINDOW and BUFFER specify the window
17595 and buffer to use as the context for the formatting (defaults
17596 are the selected window and the window's buffer). */)
17597 (format, face, window, buffer)
17598 Lisp_Object format, face, window, buffer;
17600 struct it it;
17601 int len;
17602 struct window *w;
17603 struct buffer *old_buffer = NULL;
17604 int face_id = -1;
17605 int no_props = INTEGERP (face);
17606 int count = SPECPDL_INDEX ();
17607 Lisp_Object str;
17608 int string_start = 0;
17610 if (NILP (window))
17611 window = selected_window;
17612 CHECK_WINDOW (window);
17613 w = XWINDOW (window);
17615 if (NILP (buffer))
17616 buffer = w->buffer;
17617 CHECK_BUFFER (buffer);
17619 /* Make formatting the modeline a non-op when noninteractive, otherwise
17620 there will be problems later caused by a partially initialized frame. */
17621 if (NILP (format) || noninteractive)
17622 return empty_unibyte_string;
17624 if (no_props)
17625 face = Qnil;
17627 if (!NILP (face))
17629 if (EQ (face, Qt))
17630 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17631 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17634 if (face_id < 0)
17635 face_id = DEFAULT_FACE_ID;
17637 if (XBUFFER (buffer) != current_buffer)
17638 old_buffer = current_buffer;
17640 /* Save things including mode_line_proptrans_alist,
17641 and set that to nil so that we don't alter the outer value. */
17642 record_unwind_protect (unwind_format_mode_line,
17643 format_mode_line_unwind_data (old_buffer, 1));
17644 mode_line_proptrans_alist = Qnil;
17646 if (old_buffer)
17647 set_buffer_internal_1 (XBUFFER (buffer));
17649 init_iterator (&it, w, -1, -1, NULL, face_id);
17651 if (no_props)
17653 mode_line_target = MODE_LINE_NOPROP;
17654 mode_line_string_face_prop = Qnil;
17655 mode_line_string_list = Qnil;
17656 string_start = MODE_LINE_NOPROP_LEN (0);
17658 else
17660 mode_line_target = MODE_LINE_STRING;
17661 mode_line_string_list = Qnil;
17662 mode_line_string_face = face;
17663 mode_line_string_face_prop
17664 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17667 push_kboard (FRAME_KBOARD (it.f));
17668 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17669 pop_kboard ();
17671 if (no_props)
17673 len = MODE_LINE_NOPROP_LEN (string_start);
17674 str = make_string (mode_line_noprop_buf + string_start, len);
17676 else
17678 mode_line_string_list = Fnreverse (mode_line_string_list);
17679 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17680 empty_unibyte_string);
17683 unbind_to (count, Qnil);
17684 return str;
17687 /* Write a null-terminated, right justified decimal representation of
17688 the positive integer D to BUF using a minimal field width WIDTH. */
17690 static void
17691 pint2str (buf, width, d)
17692 register char *buf;
17693 register int width;
17694 register int d;
17696 register char *p = buf;
17698 if (d <= 0)
17699 *p++ = '0';
17700 else
17702 while (d > 0)
17704 *p++ = d % 10 + '0';
17705 d /= 10;
17709 for (width -= (int) (p - buf); width > 0; --width)
17710 *p++ = ' ';
17711 *p-- = '\0';
17712 while (p > buf)
17714 d = *buf;
17715 *buf++ = *p;
17716 *p-- = d;
17720 /* Write a null-terminated, right justified decimal and "human
17721 readable" representation of the nonnegative integer D to BUF using
17722 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17724 static const char power_letter[] =
17726 0, /* not used */
17727 'k', /* kilo */
17728 'M', /* mega */
17729 'G', /* giga */
17730 'T', /* tera */
17731 'P', /* peta */
17732 'E', /* exa */
17733 'Z', /* zetta */
17734 'Y' /* yotta */
17737 static void
17738 pint2hrstr (buf, width, d)
17739 char *buf;
17740 int width;
17741 int d;
17743 /* We aim to represent the nonnegative integer D as
17744 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17745 int quotient = d;
17746 int remainder = 0;
17747 /* -1 means: do not use TENTHS. */
17748 int tenths = -1;
17749 int exponent = 0;
17751 /* Length of QUOTIENT.TENTHS as a string. */
17752 int length;
17754 char * psuffix;
17755 char * p;
17757 if (1000 <= quotient)
17759 /* Scale to the appropriate EXPONENT. */
17762 remainder = quotient % 1000;
17763 quotient /= 1000;
17764 exponent++;
17766 while (1000 <= quotient);
17768 /* Round to nearest and decide whether to use TENTHS or not. */
17769 if (quotient <= 9)
17771 tenths = remainder / 100;
17772 if (50 <= remainder % 100)
17774 if (tenths < 9)
17775 tenths++;
17776 else
17778 quotient++;
17779 if (quotient == 10)
17780 tenths = -1;
17781 else
17782 tenths = 0;
17786 else
17787 if (500 <= remainder)
17789 if (quotient < 999)
17790 quotient++;
17791 else
17793 quotient = 1;
17794 exponent++;
17795 tenths = 0;
17800 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17801 if (tenths == -1 && quotient <= 99)
17802 if (quotient <= 9)
17803 length = 1;
17804 else
17805 length = 2;
17806 else
17807 length = 3;
17808 p = psuffix = buf + max (width, length);
17810 /* Print EXPONENT. */
17811 if (exponent)
17812 *psuffix++ = power_letter[exponent];
17813 *psuffix = '\0';
17815 /* Print TENTHS. */
17816 if (tenths >= 0)
17818 *--p = '0' + tenths;
17819 *--p = '.';
17822 /* Print QUOTIENT. */
17825 int digit = quotient % 10;
17826 *--p = '0' + digit;
17828 while ((quotient /= 10) != 0);
17830 /* Print leading spaces. */
17831 while (buf < p)
17832 *--p = ' ';
17835 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17836 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17837 type of CODING_SYSTEM. Return updated pointer into BUF. */
17839 static unsigned char invalid_eol_type[] = "(*invalid*)";
17841 static char *
17842 decode_mode_spec_coding (coding_system, buf, eol_flag)
17843 Lisp_Object coding_system;
17844 register char *buf;
17845 int eol_flag;
17847 Lisp_Object val;
17848 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17849 const unsigned char *eol_str;
17850 int eol_str_len;
17851 /* The EOL conversion we are using. */
17852 Lisp_Object eoltype;
17854 val = CODING_SYSTEM_SPEC (coding_system);
17855 eoltype = Qnil;
17857 if (!VECTORP (val)) /* Not yet decided. */
17859 if (multibyte)
17860 *buf++ = '-';
17861 if (eol_flag)
17862 eoltype = eol_mnemonic_undecided;
17863 /* Don't mention EOL conversion if it isn't decided. */
17865 else
17867 Lisp_Object attrs;
17868 Lisp_Object eolvalue;
17870 attrs = AREF (val, 0);
17871 eolvalue = AREF (val, 2);
17873 if (multibyte)
17874 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17876 if (eol_flag)
17878 /* The EOL conversion that is normal on this system. */
17880 if (NILP (eolvalue)) /* Not yet decided. */
17881 eoltype = eol_mnemonic_undecided;
17882 else if (VECTORP (eolvalue)) /* Not yet decided. */
17883 eoltype = eol_mnemonic_undecided;
17884 else /* eolvalue is Qunix, Qdos, or Qmac. */
17885 eoltype = (EQ (eolvalue, Qunix)
17886 ? eol_mnemonic_unix
17887 : (EQ (eolvalue, Qdos) == 1
17888 ? eol_mnemonic_dos : eol_mnemonic_mac));
17892 if (eol_flag)
17894 /* Mention the EOL conversion if it is not the usual one. */
17895 if (STRINGP (eoltype))
17897 eol_str = SDATA (eoltype);
17898 eol_str_len = SBYTES (eoltype);
17900 else if (CHARACTERP (eoltype))
17902 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17903 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17904 eol_str = tmp;
17906 else
17908 eol_str = invalid_eol_type;
17909 eol_str_len = sizeof (invalid_eol_type) - 1;
17911 bcopy (eol_str, buf, eol_str_len);
17912 buf += eol_str_len;
17915 return buf;
17918 /* Return a string for the output of a mode line %-spec for window W,
17919 generated by character C. PRECISION >= 0 means don't return a
17920 string longer than that value. FIELD_WIDTH > 0 means pad the
17921 string returned with spaces to that value. Return 1 in *MULTIBYTE
17922 if the result is multibyte text.
17924 Note we operate on the current buffer for most purposes,
17925 the exception being w->base_line_pos. */
17927 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17929 static char *
17930 decode_mode_spec (w, c, field_width, precision, multibyte)
17931 struct window *w;
17932 register int c;
17933 int field_width, precision;
17934 int *multibyte;
17936 Lisp_Object obj;
17937 struct frame *f = XFRAME (WINDOW_FRAME (w));
17938 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17939 struct buffer *b = current_buffer;
17941 obj = Qnil;
17942 *multibyte = 0;
17944 switch (c)
17946 case '*':
17947 if (!NILP (b->read_only))
17948 return "%";
17949 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17950 return "*";
17951 return "-";
17953 case '+':
17954 /* This differs from %* only for a modified read-only buffer. */
17955 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17956 return "*";
17957 if (!NILP (b->read_only))
17958 return "%";
17959 return "-";
17961 case '&':
17962 /* This differs from %* in ignoring read-only-ness. */
17963 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17964 return "*";
17965 return "-";
17967 case '%':
17968 return "%";
17970 case '[':
17972 int i;
17973 char *p;
17975 if (command_loop_level > 5)
17976 return "[[[... ";
17977 p = decode_mode_spec_buf;
17978 for (i = 0; i < command_loop_level; i++)
17979 *p++ = '[';
17980 *p = 0;
17981 return decode_mode_spec_buf;
17984 case ']':
17986 int i;
17987 char *p;
17989 if (command_loop_level > 5)
17990 return " ...]]]";
17991 p = decode_mode_spec_buf;
17992 for (i = 0; i < command_loop_level; i++)
17993 *p++ = ']';
17994 *p = 0;
17995 return decode_mode_spec_buf;
17998 case '-':
18000 register int i;
18002 /* Let lots_of_dashes be a string of infinite length. */
18003 if (mode_line_target == MODE_LINE_NOPROP ||
18004 mode_line_target == MODE_LINE_STRING)
18005 return "--";
18006 if (field_width <= 0
18007 || field_width > sizeof (lots_of_dashes))
18009 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18010 decode_mode_spec_buf[i] = '-';
18011 decode_mode_spec_buf[i] = '\0';
18012 return decode_mode_spec_buf;
18014 else
18015 return lots_of_dashes;
18018 case 'b':
18019 obj = b->name;
18020 break;
18022 case 'c':
18023 /* %c and %l are ignored in `frame-title-format'.
18024 (In redisplay_internal, the frame title is drawn _before_ the
18025 windows are updated, so the stuff which depends on actual
18026 window contents (such as %l) may fail to render properly, or
18027 even crash emacs.) */
18028 if (mode_line_target == MODE_LINE_TITLE)
18029 return "";
18030 else
18032 int col = (int) current_column (); /* iftc */
18033 w->column_number_displayed = make_number (col);
18034 pint2str (decode_mode_spec_buf, field_width, col);
18035 return decode_mode_spec_buf;
18038 case 'e':
18039 #ifndef SYSTEM_MALLOC
18041 if (NILP (Vmemory_full))
18042 return "";
18043 else
18044 return "!MEM FULL! ";
18046 #else
18047 return "";
18048 #endif
18050 case 'F':
18051 /* %F displays the frame name. */
18052 if (!NILP (f->title))
18053 return (char *) SDATA (f->title);
18054 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18055 return (char *) SDATA (f->name);
18056 return "Emacs";
18058 case 'f':
18059 obj = b->filename;
18060 break;
18062 case 'i':
18064 int size = ZV - BEGV;
18065 pint2str (decode_mode_spec_buf, field_width, size);
18066 return decode_mode_spec_buf;
18069 case 'I':
18071 int size = ZV - BEGV;
18072 pint2hrstr (decode_mode_spec_buf, field_width, size);
18073 return decode_mode_spec_buf;
18076 case 'l':
18078 int startpos, startpos_byte, line, linepos, linepos_byte;
18079 int topline, nlines, junk, height;
18081 /* %c and %l are ignored in `frame-title-format'. */
18082 if (mode_line_target == MODE_LINE_TITLE)
18083 return "";
18085 startpos = XMARKER (w->start)->charpos;
18086 startpos_byte = marker_byte_position (w->start);
18087 height = WINDOW_TOTAL_LINES (w);
18089 /* If we decided that this buffer isn't suitable for line numbers,
18090 don't forget that too fast. */
18091 if (EQ (w->base_line_pos, w->buffer))
18092 goto no_value;
18093 /* But do forget it, if the window shows a different buffer now. */
18094 else if (BUFFERP (w->base_line_pos))
18095 w->base_line_pos = Qnil;
18097 /* If the buffer is very big, don't waste time. */
18098 if (INTEGERP (Vline_number_display_limit)
18099 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18101 w->base_line_pos = Qnil;
18102 w->base_line_number = Qnil;
18103 goto no_value;
18106 if (!NILP (w->base_line_number)
18107 && !NILP (w->base_line_pos)
18108 && XFASTINT (w->base_line_pos) <= startpos)
18110 line = XFASTINT (w->base_line_number);
18111 linepos = XFASTINT (w->base_line_pos);
18112 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18114 else
18116 line = 1;
18117 linepos = BUF_BEGV (b);
18118 linepos_byte = BUF_BEGV_BYTE (b);
18121 /* Count lines from base line to window start position. */
18122 nlines = display_count_lines (linepos, linepos_byte,
18123 startpos_byte,
18124 startpos, &junk);
18126 topline = nlines + line;
18128 /* Determine a new base line, if the old one is too close
18129 or too far away, or if we did not have one.
18130 "Too close" means it's plausible a scroll-down would
18131 go back past it. */
18132 if (startpos == BUF_BEGV (b))
18134 w->base_line_number = make_number (topline);
18135 w->base_line_pos = make_number (BUF_BEGV (b));
18137 else if (nlines < height + 25 || nlines > height * 3 + 50
18138 || linepos == BUF_BEGV (b))
18140 int limit = BUF_BEGV (b);
18141 int limit_byte = BUF_BEGV_BYTE (b);
18142 int position;
18143 int distance = (height * 2 + 30) * line_number_display_limit_width;
18145 if (startpos - distance > limit)
18147 limit = startpos - distance;
18148 limit_byte = CHAR_TO_BYTE (limit);
18151 nlines = display_count_lines (startpos, startpos_byte,
18152 limit_byte,
18153 - (height * 2 + 30),
18154 &position);
18155 /* If we couldn't find the lines we wanted within
18156 line_number_display_limit_width chars per line,
18157 give up on line numbers for this window. */
18158 if (position == limit_byte && limit == startpos - distance)
18160 w->base_line_pos = w->buffer;
18161 w->base_line_number = Qnil;
18162 goto no_value;
18165 w->base_line_number = make_number (topline - nlines);
18166 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18169 /* Now count lines from the start pos to point. */
18170 nlines = display_count_lines (startpos, startpos_byte,
18171 PT_BYTE, PT, &junk);
18173 /* Record that we did display the line number. */
18174 line_number_displayed = 1;
18176 /* Make the string to show. */
18177 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18178 return decode_mode_spec_buf;
18179 no_value:
18181 char* p = decode_mode_spec_buf;
18182 int pad = field_width - 2;
18183 while (pad-- > 0)
18184 *p++ = ' ';
18185 *p++ = '?';
18186 *p++ = '?';
18187 *p = '\0';
18188 return decode_mode_spec_buf;
18191 break;
18193 case 'm':
18194 obj = b->mode_name;
18195 break;
18197 case 'n':
18198 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18199 return " Narrow";
18200 break;
18202 case 'p':
18204 int pos = marker_position (w->start);
18205 int total = BUF_ZV (b) - BUF_BEGV (b);
18207 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18209 if (pos <= BUF_BEGV (b))
18210 return "All";
18211 else
18212 return "Bottom";
18214 else if (pos <= BUF_BEGV (b))
18215 return "Top";
18216 else
18218 if (total > 1000000)
18219 /* Do it differently for a large value, to avoid overflow. */
18220 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18221 else
18222 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18223 /* We can't normally display a 3-digit number,
18224 so get us a 2-digit number that is close. */
18225 if (total == 100)
18226 total = 99;
18227 sprintf (decode_mode_spec_buf, "%2d%%", total);
18228 return decode_mode_spec_buf;
18232 /* Display percentage of size above the bottom of the screen. */
18233 case 'P':
18235 int toppos = marker_position (w->start);
18236 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18237 int total = BUF_ZV (b) - BUF_BEGV (b);
18239 if (botpos >= BUF_ZV (b))
18241 if (toppos <= BUF_BEGV (b))
18242 return "All";
18243 else
18244 return "Bottom";
18246 else
18248 if (total > 1000000)
18249 /* Do it differently for a large value, to avoid overflow. */
18250 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18251 else
18252 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18253 /* We can't normally display a 3-digit number,
18254 so get us a 2-digit number that is close. */
18255 if (total == 100)
18256 total = 99;
18257 if (toppos <= BUF_BEGV (b))
18258 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18259 else
18260 sprintf (decode_mode_spec_buf, "%2d%%", total);
18261 return decode_mode_spec_buf;
18265 case 's':
18266 /* status of process */
18267 obj = Fget_buffer_process (Fcurrent_buffer ());
18268 if (NILP (obj))
18269 return "no process";
18270 #ifdef subprocesses
18271 obj = Fsymbol_name (Fprocess_status (obj));
18272 #endif
18273 break;
18275 case '@':
18277 Lisp_Object val;
18278 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18279 if (NILP (val))
18280 return "-";
18281 else
18282 return "@";
18285 case 't': /* indicate TEXT or BINARY */
18286 #ifdef MODE_LINE_BINARY_TEXT
18287 return MODE_LINE_BINARY_TEXT (b);
18288 #else
18289 return "T";
18290 #endif
18292 case 'z':
18293 /* coding-system (not including end-of-line format) */
18294 case 'Z':
18295 /* coding-system (including end-of-line type) */
18297 int eol_flag = (c == 'Z');
18298 char *p = decode_mode_spec_buf;
18300 if (! FRAME_WINDOW_P (f))
18302 /* No need to mention EOL here--the terminal never needs
18303 to do EOL conversion. */
18304 p = decode_mode_spec_coding (CODING_ID_NAME
18305 (FRAME_KEYBOARD_CODING (f)->id),
18306 p, 0);
18307 p = decode_mode_spec_coding (CODING_ID_NAME
18308 (FRAME_TERMINAL_CODING (f)->id),
18309 p, 0);
18311 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18312 p, eol_flag);
18314 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18315 #ifdef subprocesses
18316 obj = Fget_buffer_process (Fcurrent_buffer ());
18317 if (PROCESSP (obj))
18319 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18320 p, eol_flag);
18321 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18322 p, eol_flag);
18324 #endif /* subprocesses */
18325 #endif /* 0 */
18326 *p = 0;
18327 return decode_mode_spec_buf;
18331 if (STRINGP (obj))
18333 *multibyte = STRING_MULTIBYTE (obj);
18334 return (char *) SDATA (obj);
18336 else
18337 return "";
18341 /* Count up to COUNT lines starting from START / START_BYTE.
18342 But don't go beyond LIMIT_BYTE.
18343 Return the number of lines thus found (always nonnegative).
18345 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18347 static int
18348 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18349 int start, start_byte, limit_byte, count;
18350 int *byte_pos_ptr;
18352 register unsigned char *cursor;
18353 unsigned char *base;
18355 register int ceiling;
18356 register unsigned char *ceiling_addr;
18357 int orig_count = count;
18359 /* If we are not in selective display mode,
18360 check only for newlines. */
18361 int selective_display = (!NILP (current_buffer->selective_display)
18362 && !INTEGERP (current_buffer->selective_display));
18364 if (count > 0)
18366 while (start_byte < limit_byte)
18368 ceiling = BUFFER_CEILING_OF (start_byte);
18369 ceiling = min (limit_byte - 1, ceiling);
18370 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18371 base = (cursor = BYTE_POS_ADDR (start_byte));
18372 while (1)
18374 if (selective_display)
18375 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18377 else
18378 while (*cursor != '\n' && ++cursor != ceiling_addr)
18381 if (cursor != ceiling_addr)
18383 if (--count == 0)
18385 start_byte += cursor - base + 1;
18386 *byte_pos_ptr = start_byte;
18387 return orig_count;
18389 else
18390 if (++cursor == ceiling_addr)
18391 break;
18393 else
18394 break;
18396 start_byte += cursor - base;
18399 else
18401 while (start_byte > limit_byte)
18403 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18404 ceiling = max (limit_byte, ceiling);
18405 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18406 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18407 while (1)
18409 if (selective_display)
18410 while (--cursor != ceiling_addr
18411 && *cursor != '\n' && *cursor != 015)
18413 else
18414 while (--cursor != ceiling_addr && *cursor != '\n')
18417 if (cursor != ceiling_addr)
18419 if (++count == 0)
18421 start_byte += cursor - base + 1;
18422 *byte_pos_ptr = start_byte;
18423 /* When scanning backwards, we should
18424 not count the newline posterior to which we stop. */
18425 return - orig_count - 1;
18428 else
18429 break;
18431 /* Here we add 1 to compensate for the last decrement
18432 of CURSOR, which took it past the valid range. */
18433 start_byte += cursor - base + 1;
18437 *byte_pos_ptr = limit_byte;
18439 if (count < 0)
18440 return - orig_count + count;
18441 return orig_count - count;
18447 /***********************************************************************
18448 Displaying strings
18449 ***********************************************************************/
18451 /* Display a NUL-terminated string, starting with index START.
18453 If STRING is non-null, display that C string. Otherwise, the Lisp
18454 string LISP_STRING is displayed.
18456 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18457 FACE_STRING. Display STRING or LISP_STRING with the face at
18458 FACE_STRING_POS in FACE_STRING:
18460 Display the string in the environment given by IT, but use the
18461 standard display table, temporarily.
18463 FIELD_WIDTH is the minimum number of output glyphs to produce.
18464 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18465 with spaces. If STRING has more characters, more than FIELD_WIDTH
18466 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18468 PRECISION is the maximum number of characters to output from
18469 STRING. PRECISION < 0 means don't truncate the string.
18471 This is roughly equivalent to printf format specifiers:
18473 FIELD_WIDTH PRECISION PRINTF
18474 ----------------------------------------
18475 -1 -1 %s
18476 -1 10 %.10s
18477 10 -1 %10s
18478 20 10 %20.10s
18480 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18481 display them, and < 0 means obey the current buffer's value of
18482 enable_multibyte_characters.
18484 Value is the number of columns displayed. */
18486 static int
18487 display_string (string, lisp_string, face_string, face_string_pos,
18488 start, it, field_width, precision, max_x, multibyte)
18489 unsigned char *string;
18490 Lisp_Object lisp_string;
18491 Lisp_Object face_string;
18492 int face_string_pos;
18493 int start;
18494 struct it *it;
18495 int field_width, precision, max_x;
18496 int multibyte;
18498 int hpos_at_start = it->hpos;
18499 int saved_face_id = it->face_id;
18500 struct glyph_row *row = it->glyph_row;
18502 /* Initialize the iterator IT for iteration over STRING beginning
18503 with index START. */
18504 reseat_to_string (it, string, lisp_string, start,
18505 precision, field_width, multibyte);
18507 /* If displaying STRING, set up the face of the iterator
18508 from LISP_STRING, if that's given. */
18509 if (STRINGP (face_string))
18511 int endptr;
18512 struct face *face;
18514 it->face_id
18515 = face_at_string_position (it->w, face_string, face_string_pos,
18516 0, it->region_beg_charpos,
18517 it->region_end_charpos,
18518 &endptr, it->base_face_id, 0);
18519 face = FACE_FROM_ID (it->f, it->face_id);
18520 it->face_box_p = face->box != FACE_NO_BOX;
18523 /* Set max_x to the maximum allowed X position. Don't let it go
18524 beyond the right edge of the window. */
18525 if (max_x <= 0)
18526 max_x = it->last_visible_x;
18527 else
18528 max_x = min (max_x, it->last_visible_x);
18530 /* Skip over display elements that are not visible. because IT->w is
18531 hscrolled. */
18532 if (it->current_x < it->first_visible_x)
18533 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18534 MOVE_TO_POS | MOVE_TO_X);
18536 row->ascent = it->max_ascent;
18537 row->height = it->max_ascent + it->max_descent;
18538 row->phys_ascent = it->max_phys_ascent;
18539 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18540 row->extra_line_spacing = it->max_extra_line_spacing;
18542 /* This condition is for the case that we are called with current_x
18543 past last_visible_x. */
18544 while (it->current_x < max_x)
18546 int x_before, x, n_glyphs_before, i, nglyphs;
18548 /* Get the next display element. */
18549 if (!get_next_display_element (it))
18550 break;
18552 /* Produce glyphs. */
18553 x_before = it->current_x;
18554 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18555 PRODUCE_GLYPHS (it);
18557 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18558 i = 0;
18559 x = x_before;
18560 while (i < nglyphs)
18562 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18564 if (!it->truncate_lines_p
18565 && x + glyph->pixel_width > max_x)
18567 /* End of continued line or max_x reached. */
18568 if (CHAR_GLYPH_PADDING_P (*glyph))
18570 /* A wide character is unbreakable. */
18571 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18572 it->current_x = x_before;
18574 else
18576 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18577 it->current_x = x;
18579 break;
18581 else if (x + glyph->pixel_width >= it->first_visible_x)
18583 /* Glyph is at least partially visible. */
18584 ++it->hpos;
18585 if (x < it->first_visible_x)
18586 it->glyph_row->x = x - it->first_visible_x;
18588 else
18590 /* Glyph is off the left margin of the display area.
18591 Should not happen. */
18592 abort ();
18595 row->ascent = max (row->ascent, it->max_ascent);
18596 row->height = max (row->height, it->max_ascent + it->max_descent);
18597 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18598 row->phys_height = max (row->phys_height,
18599 it->max_phys_ascent + it->max_phys_descent);
18600 row->extra_line_spacing = max (row->extra_line_spacing,
18601 it->max_extra_line_spacing);
18602 x += glyph->pixel_width;
18603 ++i;
18606 /* Stop if max_x reached. */
18607 if (i < nglyphs)
18608 break;
18610 /* Stop at line ends. */
18611 if (ITERATOR_AT_END_OF_LINE_P (it))
18613 it->continuation_lines_width = 0;
18614 break;
18617 set_iterator_to_next (it, 1);
18619 /* Stop if truncating at the right edge. */
18620 if (it->truncate_lines_p
18621 && it->current_x >= it->last_visible_x)
18623 /* Add truncation mark, but don't do it if the line is
18624 truncated at a padding space. */
18625 if (IT_CHARPOS (*it) < it->string_nchars)
18627 if (!FRAME_WINDOW_P (it->f))
18629 int i, n;
18631 if (it->current_x > it->last_visible_x)
18633 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18634 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18635 break;
18636 for (n = row->used[TEXT_AREA]; i < n; ++i)
18638 row->used[TEXT_AREA] = i;
18639 produce_special_glyphs (it, IT_TRUNCATION);
18642 produce_special_glyphs (it, IT_TRUNCATION);
18644 it->glyph_row->truncated_on_right_p = 1;
18646 break;
18650 /* Maybe insert a truncation at the left. */
18651 if (it->first_visible_x
18652 && IT_CHARPOS (*it) > 0)
18654 if (!FRAME_WINDOW_P (it->f))
18655 insert_left_trunc_glyphs (it);
18656 it->glyph_row->truncated_on_left_p = 1;
18659 it->face_id = saved_face_id;
18661 /* Value is number of columns displayed. */
18662 return it->hpos - hpos_at_start;
18667 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18668 appears as an element of LIST or as the car of an element of LIST.
18669 If PROPVAL is a list, compare each element against LIST in that
18670 way, and return 1/2 if any element of PROPVAL is found in LIST.
18671 Otherwise return 0. This function cannot quit.
18672 The return value is 2 if the text is invisible but with an ellipsis
18673 and 1 if it's invisible and without an ellipsis. */
18676 invisible_p (propval, list)
18677 register Lisp_Object propval;
18678 Lisp_Object list;
18680 register Lisp_Object tail, proptail;
18682 for (tail = list; CONSP (tail); tail = XCDR (tail))
18684 register Lisp_Object tem;
18685 tem = XCAR (tail);
18686 if (EQ (propval, tem))
18687 return 1;
18688 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18689 return NILP (XCDR (tem)) ? 1 : 2;
18692 if (CONSP (propval))
18694 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18696 Lisp_Object propelt;
18697 propelt = XCAR (proptail);
18698 for (tail = list; CONSP (tail); tail = XCDR (tail))
18700 register Lisp_Object tem;
18701 tem = XCAR (tail);
18702 if (EQ (propelt, tem))
18703 return 1;
18704 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18705 return NILP (XCDR (tem)) ? 1 : 2;
18710 return 0;
18713 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18714 doc: /* Non-nil if the property makes the text invisible.
18715 POS-OR-PROP can be a marker or number, in which case it is taken to be
18716 a position in the current buffer and the value of the `invisible' property
18717 is checked; or it can be some other value, which is then presumed to be the
18718 value of the `invisible' property of the text of interest.
18719 The non-nil value returned can be t for truly invisible text or something
18720 else if the text is replaced by an ellipsis. */)
18721 (pos_or_prop)
18722 Lisp_Object pos_or_prop;
18724 Lisp_Object prop
18725 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18726 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18727 : pos_or_prop);
18728 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18729 return (invis == 0 ? Qnil
18730 : invis == 1 ? Qt
18731 : make_number (invis));
18734 /* Calculate a width or height in pixels from a specification using
18735 the following elements:
18737 SPEC ::=
18738 NUM - a (fractional) multiple of the default font width/height
18739 (NUM) - specifies exactly NUM pixels
18740 UNIT - a fixed number of pixels, see below.
18741 ELEMENT - size of a display element in pixels, see below.
18742 (NUM . SPEC) - equals NUM * SPEC
18743 (+ SPEC SPEC ...) - add pixel values
18744 (- SPEC SPEC ...) - subtract pixel values
18745 (- SPEC) - negate pixel value
18747 NUM ::=
18748 INT or FLOAT - a number constant
18749 SYMBOL - use symbol's (buffer local) variable binding.
18751 UNIT ::=
18752 in - pixels per inch *)
18753 mm - pixels per 1/1000 meter *)
18754 cm - pixels per 1/100 meter *)
18755 width - width of current font in pixels.
18756 height - height of current font in pixels.
18758 *) using the ratio(s) defined in display-pixels-per-inch.
18760 ELEMENT ::=
18762 left-fringe - left fringe width in pixels
18763 right-fringe - right fringe width in pixels
18765 left-margin - left margin width in pixels
18766 right-margin - right margin width in pixels
18768 scroll-bar - scroll-bar area width in pixels
18770 Examples:
18772 Pixels corresponding to 5 inches:
18773 (5 . in)
18775 Total width of non-text areas on left side of window (if scroll-bar is on left):
18776 '(space :width (+ left-fringe left-margin scroll-bar))
18778 Align to first text column (in header line):
18779 '(space :align-to 0)
18781 Align to middle of text area minus half the width of variable `my-image'
18782 containing a loaded image:
18783 '(space :align-to (0.5 . (- text my-image)))
18785 Width of left margin minus width of 1 character in the default font:
18786 '(space :width (- left-margin 1))
18788 Width of left margin minus width of 2 characters in the current font:
18789 '(space :width (- left-margin (2 . width)))
18791 Center 1 character over left-margin (in header line):
18792 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18794 Different ways to express width of left fringe plus left margin minus one pixel:
18795 '(space :width (- (+ left-fringe left-margin) (1)))
18796 '(space :width (+ left-fringe left-margin (- (1))))
18797 '(space :width (+ left-fringe left-margin (-1)))
18801 #define NUMVAL(X) \
18802 ((INTEGERP (X) || FLOATP (X)) \
18803 ? XFLOATINT (X) \
18804 : - 1)
18807 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18808 double *res;
18809 struct it *it;
18810 Lisp_Object prop;
18811 void *font;
18812 int width_p, *align_to;
18814 double pixels;
18816 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18817 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18819 if (NILP (prop))
18820 return OK_PIXELS (0);
18822 xassert (FRAME_LIVE_P (it->f));
18824 if (SYMBOLP (prop))
18826 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18828 char *unit = SDATA (SYMBOL_NAME (prop));
18830 if (unit[0] == 'i' && unit[1] == 'n')
18831 pixels = 1.0;
18832 else if (unit[0] == 'm' && unit[1] == 'm')
18833 pixels = 25.4;
18834 else if (unit[0] == 'c' && unit[1] == 'm')
18835 pixels = 2.54;
18836 else
18837 pixels = 0;
18838 if (pixels > 0)
18840 double ppi;
18841 #ifdef HAVE_WINDOW_SYSTEM
18842 if (FRAME_WINDOW_P (it->f)
18843 && (ppi = (width_p
18844 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18845 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18846 ppi > 0))
18847 return OK_PIXELS (ppi / pixels);
18848 #endif
18850 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18851 || (CONSP (Vdisplay_pixels_per_inch)
18852 && (ppi = (width_p
18853 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18854 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18855 ppi > 0)))
18856 return OK_PIXELS (ppi / pixels);
18858 return 0;
18862 #ifdef HAVE_WINDOW_SYSTEM
18863 if (EQ (prop, Qheight))
18864 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18865 if (EQ (prop, Qwidth))
18866 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18867 #else
18868 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18869 return OK_PIXELS (1);
18870 #endif
18872 if (EQ (prop, Qtext))
18873 return OK_PIXELS (width_p
18874 ? window_box_width (it->w, TEXT_AREA)
18875 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18877 if (align_to && *align_to < 0)
18879 *res = 0;
18880 if (EQ (prop, Qleft))
18881 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18882 if (EQ (prop, Qright))
18883 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18884 if (EQ (prop, Qcenter))
18885 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18886 + window_box_width (it->w, TEXT_AREA) / 2);
18887 if (EQ (prop, Qleft_fringe))
18888 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18889 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18890 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18891 if (EQ (prop, Qright_fringe))
18892 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18893 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18894 : window_box_right_offset (it->w, TEXT_AREA));
18895 if (EQ (prop, Qleft_margin))
18896 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18897 if (EQ (prop, Qright_margin))
18898 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18899 if (EQ (prop, Qscroll_bar))
18900 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18902 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18903 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18904 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18905 : 0)));
18907 else
18909 if (EQ (prop, Qleft_fringe))
18910 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18911 if (EQ (prop, Qright_fringe))
18912 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18913 if (EQ (prop, Qleft_margin))
18914 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18915 if (EQ (prop, Qright_margin))
18916 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18917 if (EQ (prop, Qscroll_bar))
18918 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18921 prop = Fbuffer_local_value (prop, it->w->buffer);
18924 if (INTEGERP (prop) || FLOATP (prop))
18926 int base_unit = (width_p
18927 ? FRAME_COLUMN_WIDTH (it->f)
18928 : FRAME_LINE_HEIGHT (it->f));
18929 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18932 if (CONSP (prop))
18934 Lisp_Object car = XCAR (prop);
18935 Lisp_Object cdr = XCDR (prop);
18937 if (SYMBOLP (car))
18939 #ifdef HAVE_WINDOW_SYSTEM
18940 if (FRAME_WINDOW_P (it->f)
18941 && valid_image_p (prop))
18943 int id = lookup_image (it->f, prop);
18944 struct image *img = IMAGE_FROM_ID (it->f, id);
18946 return OK_PIXELS (width_p ? img->width : img->height);
18948 #endif
18949 if (EQ (car, Qplus) || EQ (car, Qminus))
18951 int first = 1;
18952 double px;
18954 pixels = 0;
18955 while (CONSP (cdr))
18957 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18958 font, width_p, align_to))
18959 return 0;
18960 if (first)
18961 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18962 else
18963 pixels += px;
18964 cdr = XCDR (cdr);
18966 if (EQ (car, Qminus))
18967 pixels = -pixels;
18968 return OK_PIXELS (pixels);
18971 car = Fbuffer_local_value (car, it->w->buffer);
18974 if (INTEGERP (car) || FLOATP (car))
18976 double fact;
18977 pixels = XFLOATINT (car);
18978 if (NILP (cdr))
18979 return OK_PIXELS (pixels);
18980 if (calc_pixel_width_or_height (&fact, it, cdr,
18981 font, width_p, align_to))
18982 return OK_PIXELS (pixels * fact);
18983 return 0;
18986 return 0;
18989 return 0;
18993 /***********************************************************************
18994 Glyph Display
18995 ***********************************************************************/
18997 #ifdef HAVE_WINDOW_SYSTEM
18999 #if GLYPH_DEBUG
19001 void
19002 dump_glyph_string (s)
19003 struct glyph_string *s;
19005 fprintf (stderr, "glyph string\n");
19006 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19007 s->x, s->y, s->width, s->height);
19008 fprintf (stderr, " ybase = %d\n", s->ybase);
19009 fprintf (stderr, " hl = %d\n", s->hl);
19010 fprintf (stderr, " left overhang = %d, right = %d\n",
19011 s->left_overhang, s->right_overhang);
19012 fprintf (stderr, " nchars = %d\n", s->nchars);
19013 fprintf (stderr, " extends to end of line = %d\n",
19014 s->extends_to_end_of_line_p);
19015 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19016 fprintf (stderr, " bg width = %d\n", s->background_width);
19019 #endif /* GLYPH_DEBUG */
19021 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19022 of XChar2b structures for S; it can't be allocated in
19023 init_glyph_string because it must be allocated via `alloca'. W
19024 is the window on which S is drawn. ROW and AREA are the glyph row
19025 and area within the row from which S is constructed. START is the
19026 index of the first glyph structure covered by S. HL is a
19027 face-override for drawing S. */
19029 #ifdef HAVE_NTGUI
19030 #define OPTIONAL_HDC(hdc) hdc,
19031 #define DECLARE_HDC(hdc) HDC hdc;
19032 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19033 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19034 #endif
19036 #ifndef OPTIONAL_HDC
19037 #define OPTIONAL_HDC(hdc)
19038 #define DECLARE_HDC(hdc)
19039 #define ALLOCATE_HDC(hdc, f)
19040 #define RELEASE_HDC(hdc, f)
19041 #endif
19043 static void
19044 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19045 struct glyph_string *s;
19046 DECLARE_HDC (hdc)
19047 XChar2b *char2b;
19048 struct window *w;
19049 struct glyph_row *row;
19050 enum glyph_row_area area;
19051 int start;
19052 enum draw_glyphs_face hl;
19054 bzero (s, sizeof *s);
19055 s->w = w;
19056 s->f = XFRAME (w->frame);
19057 #ifdef HAVE_NTGUI
19058 s->hdc = hdc;
19059 #endif
19060 s->display = FRAME_X_DISPLAY (s->f);
19061 s->window = FRAME_X_WINDOW (s->f);
19062 s->char2b = char2b;
19063 s->hl = hl;
19064 s->row = row;
19065 s->area = area;
19066 s->first_glyph = row->glyphs[area] + start;
19067 s->height = row->height;
19068 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19070 /* Display the internal border below the tool-bar window. */
19071 if (WINDOWP (s->f->tool_bar_window)
19072 && s->w == XWINDOW (s->f->tool_bar_window))
19073 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19075 s->ybase = s->y + row->ascent;
19079 /* Append the list of glyph strings with head H and tail T to the list
19080 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19082 static INLINE void
19083 append_glyph_string_lists (head, tail, h, t)
19084 struct glyph_string **head, **tail;
19085 struct glyph_string *h, *t;
19087 if (h)
19089 if (*head)
19090 (*tail)->next = h;
19091 else
19092 *head = h;
19093 h->prev = *tail;
19094 *tail = t;
19099 /* Prepend the list of glyph strings with head H and tail T to the
19100 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19101 result. */
19103 static INLINE void
19104 prepend_glyph_string_lists (head, tail, h, t)
19105 struct glyph_string **head, **tail;
19106 struct glyph_string *h, *t;
19108 if (h)
19110 if (*head)
19111 (*head)->prev = t;
19112 else
19113 *tail = t;
19114 t->next = *head;
19115 *head = h;
19120 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19121 Set *HEAD and *TAIL to the resulting list. */
19123 static INLINE void
19124 append_glyph_string (head, tail, s)
19125 struct glyph_string **head, **tail;
19126 struct glyph_string *s;
19128 s->next = s->prev = NULL;
19129 append_glyph_string_lists (head, tail, s, s);
19133 /* Get face and two-byte form of character C in face FACE_ID on frame
19134 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19135 means we want to display multibyte text. DISPLAY_P non-zero means
19136 make sure that X resources for the face returned are allocated.
19137 Value is a pointer to a realized face that is ready for display if
19138 DISPLAY_P is non-zero. */
19140 static INLINE struct face *
19141 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19142 struct frame *f;
19143 int c, face_id;
19144 XChar2b *char2b;
19145 int multibyte_p, display_p;
19147 struct face *face = FACE_FROM_ID (f, face_id);
19149 #ifdef USE_FONT_BACKEND
19150 if (enable_font_backend)
19152 struct font *font = (struct font *) face->font_info;
19154 if (font)
19156 unsigned code = font->driver->encode_char (font, c);
19158 if (code != FONT_INVALID_CODE)
19159 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19160 else
19161 STORE_XCHAR2B (char2b, 0, 0);
19164 else
19165 #endif /* USE_FONT_BACKEND */
19166 if (!multibyte_p)
19168 /* Unibyte case. We don't have to encode, but we have to make
19169 sure to use a face suitable for unibyte. */
19170 STORE_XCHAR2B (char2b, 0, c);
19171 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
19172 face = FACE_FROM_ID (f, face_id);
19174 else if (c < 128)
19176 /* Case of ASCII in a face known to fit ASCII. */
19177 STORE_XCHAR2B (char2b, 0, c);
19179 else if (face->font != NULL)
19181 struct font_info *font_info
19182 = FONT_INFO_FROM_ID (f, face->font_info_id);
19183 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19184 unsigned code = ENCODE_CHAR (charset, c);
19186 if (CHARSET_DIMENSION (charset) == 1)
19187 STORE_XCHAR2B (char2b, 0, code);
19188 else
19189 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19190 /* Maybe encode the character in *CHAR2B. */
19191 FRAME_RIF (f)->encode_char (c, char2b, font_info, charset, NULL);
19194 /* Make sure X resources of the face are allocated. */
19195 #ifdef HAVE_X_WINDOWS
19196 if (display_p)
19197 #endif
19199 xassert (face != NULL);
19200 PREPARE_FACE_FOR_DISPLAY (f, face);
19203 return face;
19207 /* Get face and two-byte form of character glyph GLYPH on frame F.
19208 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19209 a pointer to a realized face that is ready for display. */
19211 static INLINE struct face *
19212 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19213 struct frame *f;
19214 struct glyph *glyph;
19215 XChar2b *char2b;
19216 int *two_byte_p;
19218 struct face *face;
19220 xassert (glyph->type == CHAR_GLYPH);
19221 face = FACE_FROM_ID (f, glyph->face_id);
19223 if (two_byte_p)
19224 *two_byte_p = 0;
19226 #ifdef USE_FONT_BACKEND
19227 if (enable_font_backend)
19229 struct font *font = (struct font *) face->font_info;
19231 if (font)
19233 unsigned code = font->driver->encode_char (font, glyph->u.ch);
19235 if (code != FONT_INVALID_CODE)
19236 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19237 else
19238 STORE_XCHAR2B (char2b, 0, code);
19241 else
19242 #endif /* USE_FONT_BACKEND */
19243 if (!glyph->multibyte_p)
19245 /* Unibyte case. We don't have to encode, but we have to make
19246 sure to use a face suitable for unibyte. */
19247 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19249 else if (glyph->u.ch < 128)
19251 /* Case of ASCII in a face known to fit ASCII. */
19252 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19254 else
19256 struct font_info *font_info
19257 = FONT_INFO_FROM_ID (f, face->font_info_id);
19258 if (font_info)
19260 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19261 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
19263 if (CHARSET_DIMENSION (charset) == 1)
19264 STORE_XCHAR2B (char2b, 0, code);
19265 else
19266 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19268 /* Maybe encode the character in *CHAR2B. */
19269 if (CHARSET_ID (charset) != charset_ascii)
19271 glyph->font_type
19272 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info,
19273 charset, two_byte_p);
19278 /* Make sure X resources of the face are allocated. */
19279 xassert (face != NULL);
19280 PREPARE_FACE_FOR_DISPLAY (f, face);
19281 return face;
19285 /* Fill glyph string S with composition components specified by S->cmp.
19287 BASE_FACE is the base face of the composition.
19288 S->gidx is the index of the first component for S.
19290 OVERLAPS non-zero means S should draw the foreground only, and use
19291 its physical height for clipping. See also draw_glyphs.
19293 Value is the index of a component not in S. */
19295 static int
19296 fill_composite_glyph_string (s, base_face, overlaps)
19297 struct glyph_string *s;
19298 struct face *base_face;
19299 int overlaps;
19301 int i;
19303 xassert (s);
19305 s->for_overlaps = overlaps;
19307 #ifdef USE_FONT_BACKEND
19308 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19310 Lisp_Object gstring
19311 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19312 s->cmp->hash_index * 2);
19314 s->face = base_face;
19315 s->font_info = base_face->font_info;
19316 s->font = s->font_info->font;
19317 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19319 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19320 unsigned code;
19321 XChar2b * store_pos;
19322 if (NILP (g))
19323 break;
19324 code = LGLYPH_CODE (g);
19325 store_pos = s->char2b + i;
19326 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19328 s->width = s->cmp->pixel_width;
19330 else
19331 #endif /* USE_FONT_BACKEND */
19333 /* For all glyphs of this composition, starting at the offset
19334 S->gidx, until we reach the end of the definition or encounter a
19335 glyph that requires the different face, add it to S. */
19336 struct face *face;
19338 s->face = NULL;
19339 s->font = NULL;
19340 s->font_info = NULL;
19341 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19343 int c = COMPOSITION_GLYPH (s->cmp, i);
19345 if (c != '\t')
19347 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19348 -1, Qnil);
19350 face = get_char_face_and_encoding (s->f, c, face_id,
19351 s->char2b + i, 1, 1);
19352 if (face)
19354 if (! s->face)
19356 s->face = face;
19357 s->font = s->face->font;
19358 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19360 else if (s->face != face)
19361 break;
19364 ++s->nchars;
19367 /* All glyph strings for the same composition has the same width,
19368 i.e. the width set for the first component of the composition. */
19369 s->width = s->first_glyph->pixel_width;
19372 /* If the specified font could not be loaded, use the frame's
19373 default font, but record the fact that we couldn't load it in
19374 the glyph string so that we can draw rectangles for the
19375 characters of the glyph string. */
19376 if (s->font == NULL)
19378 s->font_not_found_p = 1;
19379 s->font = FRAME_FONT (s->f);
19382 /* Adjust base line for subscript/superscript text. */
19383 s->ybase += s->first_glyph->voffset;
19385 /* This glyph string must always be drawn with 16-bit functions. */
19386 s->two_byte_p = 1;
19388 return s->gidx + s->nchars;
19392 /* Fill glyph string S from a sequence of character glyphs.
19394 FACE_ID is the face id of the string. START is the index of the
19395 first glyph to consider, END is the index of the last + 1.
19396 OVERLAPS non-zero means S should draw the foreground only, and use
19397 its physical height for clipping. See also draw_glyphs.
19399 Value is the index of the first glyph not in S. */
19401 static int
19402 fill_glyph_string (s, face_id, start, end, overlaps)
19403 struct glyph_string *s;
19404 int face_id;
19405 int start, end, overlaps;
19407 struct glyph *glyph, *last;
19408 int voffset;
19409 int glyph_not_available_p;
19411 xassert (s->f == XFRAME (s->w->frame));
19412 xassert (s->nchars == 0);
19413 xassert (start >= 0 && end > start);
19415 s->for_overlaps = overlaps,
19416 glyph = s->row->glyphs[s->area] + start;
19417 last = s->row->glyphs[s->area] + end;
19418 voffset = glyph->voffset;
19420 glyph_not_available_p = glyph->glyph_not_available_p;
19422 while (glyph < last
19423 && glyph->type == CHAR_GLYPH
19424 && glyph->voffset == voffset
19425 /* Same face id implies same font, nowadays. */
19426 && glyph->face_id == face_id
19427 && glyph->glyph_not_available_p == glyph_not_available_p)
19429 int two_byte_p;
19431 s->face = get_glyph_face_and_encoding (s->f, glyph,
19432 s->char2b + s->nchars,
19433 &two_byte_p);
19434 s->two_byte_p = two_byte_p;
19435 ++s->nchars;
19436 xassert (s->nchars <= end - start);
19437 s->width += glyph->pixel_width;
19438 ++glyph;
19441 s->font = s->face->font;
19442 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19444 /* If the specified font could not be loaded, use the frame's font,
19445 but record the fact that we couldn't load it in
19446 S->font_not_found_p so that we can draw rectangles for the
19447 characters of the glyph string. */
19448 if (s->font == NULL || glyph_not_available_p)
19450 s->font_not_found_p = 1;
19451 s->font = FRAME_FONT (s->f);
19454 /* Adjust base line for subscript/superscript text. */
19455 s->ybase += voffset;
19457 xassert (s->face && s->face->gc);
19458 return glyph - s->row->glyphs[s->area];
19462 /* Fill glyph string S from image glyph S->first_glyph. */
19464 static void
19465 fill_image_glyph_string (s)
19466 struct glyph_string *s;
19468 xassert (s->first_glyph->type == IMAGE_GLYPH);
19469 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19470 xassert (s->img);
19471 s->slice = s->first_glyph->slice;
19472 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19473 s->font = s->face->font;
19474 s->width = s->first_glyph->pixel_width;
19476 /* Adjust base line for subscript/superscript text. */
19477 s->ybase += s->first_glyph->voffset;
19481 /* Fill glyph string S from a sequence of stretch glyphs.
19483 ROW is the glyph row in which the glyphs are found, AREA is the
19484 area within the row. START is the index of the first glyph to
19485 consider, END is the index of the last + 1.
19487 Value is the index of the first glyph not in S. */
19489 static int
19490 fill_stretch_glyph_string (s, row, area, start, end)
19491 struct glyph_string *s;
19492 struct glyph_row *row;
19493 enum glyph_row_area area;
19494 int start, end;
19496 struct glyph *glyph, *last;
19497 int voffset, face_id;
19499 xassert (s->first_glyph->type == STRETCH_GLYPH);
19501 glyph = s->row->glyphs[s->area] + start;
19502 last = s->row->glyphs[s->area] + end;
19503 face_id = glyph->face_id;
19504 s->face = FACE_FROM_ID (s->f, face_id);
19505 s->font = s->face->font;
19506 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19507 s->width = glyph->pixel_width;
19508 s->nchars = 1;
19509 voffset = glyph->voffset;
19511 for (++glyph;
19512 (glyph < last
19513 && glyph->type == STRETCH_GLYPH
19514 && glyph->voffset == voffset
19515 && glyph->face_id == face_id);
19516 ++glyph)
19517 s->width += glyph->pixel_width;
19519 /* Adjust base line for subscript/superscript text. */
19520 s->ybase += voffset;
19522 /* The case that face->gc == 0 is handled when drawing the glyph
19523 string by calling PREPARE_FACE_FOR_DISPLAY. */
19524 xassert (s->face);
19525 return glyph - s->row->glyphs[s->area];
19528 static XCharStruct *
19529 get_per_char_metric (f, font, font_info, char2b, font_type)
19530 struct frame *f;
19531 XFontStruct *font;
19532 struct font_info *font_info;
19533 XChar2b *char2b;
19534 int font_type;
19536 #ifdef USE_FONT_BACKEND
19537 if (enable_font_backend)
19539 static XCharStruct pcm_value;
19540 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19541 struct font *fontp;
19542 struct font_metrics metrics;
19544 if (! font_info || code == FONT_INVALID_CODE)
19545 return NULL;
19546 fontp = (struct font *) font_info;
19547 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19548 pcm_value.lbearing = metrics.lbearing;
19549 pcm_value.rbearing = metrics.rbearing;
19550 pcm_value.ascent = metrics.ascent;
19551 pcm_value.descent = metrics.descent;
19552 pcm_value.width = metrics.width;
19553 return &pcm_value;
19555 #endif /* USE_FONT_BACKEND */
19556 return FRAME_RIF (f)->per_char_metric (font, char2b, font_type);
19559 /* EXPORT for RIF:
19560 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19561 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19562 assumed to be zero. */
19564 void
19565 x_get_glyph_overhangs (glyph, f, left, right)
19566 struct glyph *glyph;
19567 struct frame *f;
19568 int *left, *right;
19570 *left = *right = 0;
19572 if (glyph->type == CHAR_GLYPH)
19574 XFontStruct *font;
19575 struct face *face;
19576 struct font_info *font_info;
19577 XChar2b char2b;
19578 XCharStruct *pcm;
19580 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19581 font = face->font;
19582 font_info = FONT_INFO_FROM_FACE (f, face);
19583 if (font /* ++KFS: Should this be font_info ? */
19584 && (pcm = get_per_char_metric (f, font, font_info, &char2b, glyph->font_type)))
19586 if (pcm->rbearing > pcm->width)
19587 *right = pcm->rbearing - pcm->width;
19588 if (pcm->lbearing < 0)
19589 *left = -pcm->lbearing;
19592 else if (glyph->type == COMPOSITE_GLYPH)
19594 struct composition *cmp = composition_table[glyph->u.cmp_id];
19596 *right = cmp->rbearing - cmp->pixel_width;
19597 *left = - cmp->lbearing;
19602 /* Return the index of the first glyph preceding glyph string S that
19603 is overwritten by S because of S's left overhang. Value is -1
19604 if no glyphs are overwritten. */
19606 static int
19607 left_overwritten (s)
19608 struct glyph_string *s;
19610 int k;
19612 if (s->left_overhang)
19614 int x = 0, i;
19615 struct glyph *glyphs = s->row->glyphs[s->area];
19616 int first = s->first_glyph - glyphs;
19618 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19619 x -= glyphs[i].pixel_width;
19621 k = i + 1;
19623 else
19624 k = -1;
19626 return k;
19630 /* Return the index of the first glyph preceding glyph string S that
19631 is overwriting S because of its right overhang. Value is -1 if no
19632 glyph in front of S overwrites S. */
19634 static int
19635 left_overwriting (s)
19636 struct glyph_string *s;
19638 int i, k, x;
19639 struct glyph *glyphs = s->row->glyphs[s->area];
19640 int first = s->first_glyph - glyphs;
19642 k = -1;
19643 x = 0;
19644 for (i = first - 1; i >= 0; --i)
19646 int left, right;
19647 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19648 if (x + right > 0)
19649 k = i;
19650 x -= glyphs[i].pixel_width;
19653 return k;
19657 /* Return the index of the last glyph following glyph string S that is
19658 not overwritten by S because of S's right overhang. Value is -1 if
19659 no such glyph is found. */
19661 static int
19662 right_overwritten (s)
19663 struct glyph_string *s;
19665 int k = -1;
19667 if (s->right_overhang)
19669 int x = 0, i;
19670 struct glyph *glyphs = s->row->glyphs[s->area];
19671 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19672 int end = s->row->used[s->area];
19674 for (i = first; i < end && s->right_overhang > x; ++i)
19675 x += glyphs[i].pixel_width;
19677 k = i;
19680 return k;
19684 /* Return the index of the last glyph following glyph string S that
19685 overwrites S because of its left overhang. Value is negative
19686 if no such glyph is found. */
19688 static int
19689 right_overwriting (s)
19690 struct glyph_string *s;
19692 int i, k, x;
19693 int end = s->row->used[s->area];
19694 struct glyph *glyphs = s->row->glyphs[s->area];
19695 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19697 k = -1;
19698 x = 0;
19699 for (i = first; i < end; ++i)
19701 int left, right;
19702 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19703 if (x - left < 0)
19704 k = i;
19705 x += glyphs[i].pixel_width;
19708 return k;
19712 /* Set background width of glyph string S. START is the index of the
19713 first glyph following S. LAST_X is the right-most x-position + 1
19714 in the drawing area. */
19716 static INLINE void
19717 set_glyph_string_background_width (s, start, last_x)
19718 struct glyph_string *s;
19719 int start;
19720 int last_x;
19722 /* If the face of this glyph string has to be drawn to the end of
19723 the drawing area, set S->extends_to_end_of_line_p. */
19725 if (start == s->row->used[s->area]
19726 && s->area == TEXT_AREA
19727 && ((s->row->fill_line_p
19728 && (s->hl == DRAW_NORMAL_TEXT
19729 || s->hl == DRAW_IMAGE_RAISED
19730 || s->hl == DRAW_IMAGE_SUNKEN))
19731 || s->hl == DRAW_MOUSE_FACE))
19732 s->extends_to_end_of_line_p = 1;
19734 /* If S extends its face to the end of the line, set its
19735 background_width to the distance to the right edge of the drawing
19736 area. */
19737 if (s->extends_to_end_of_line_p)
19738 s->background_width = last_x - s->x + 1;
19739 else
19740 s->background_width = s->width;
19744 /* Compute overhangs and x-positions for glyph string S and its
19745 predecessors, or successors. X is the starting x-position for S.
19746 BACKWARD_P non-zero means process predecessors. */
19748 static void
19749 compute_overhangs_and_x (s, x, backward_p)
19750 struct glyph_string *s;
19751 int x;
19752 int backward_p;
19754 if (backward_p)
19756 while (s)
19758 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19759 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19760 x -= s->width;
19761 s->x = x;
19762 s = s->prev;
19765 else
19767 while (s)
19769 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19770 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19771 s->x = x;
19772 x += s->width;
19773 s = s->next;
19780 /* The following macros are only called from draw_glyphs below.
19781 They reference the following parameters of that function directly:
19782 `w', `row', `area', and `overlap_p'
19783 as well as the following local variables:
19784 `s', `f', and `hdc' (in W32) */
19786 #ifdef HAVE_NTGUI
19787 /* On W32, silently add local `hdc' variable to argument list of
19788 init_glyph_string. */
19789 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19790 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19791 #else
19792 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19793 init_glyph_string (s, char2b, w, row, area, start, hl)
19794 #endif
19796 /* Add a glyph string for a stretch glyph to the list of strings
19797 between HEAD and TAIL. START is the index of the stretch glyph in
19798 row area AREA of glyph row ROW. END is the index of the last glyph
19799 in that glyph row area. X is the current output position assigned
19800 to the new glyph string constructed. HL overrides that face of the
19801 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19802 is the right-most x-position of the drawing area. */
19804 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19805 and below -- keep them on one line. */
19806 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19807 do \
19809 s = (struct glyph_string *) alloca (sizeof *s); \
19810 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19811 START = fill_stretch_glyph_string (s, row, area, START, END); \
19812 append_glyph_string (&HEAD, &TAIL, s); \
19813 s->x = (X); \
19815 while (0)
19818 /* Add a glyph string for an image glyph to the list of strings
19819 between HEAD and TAIL. START is the index of the image glyph in
19820 row area AREA of glyph row ROW. END is the index of the last glyph
19821 in that glyph row area. X is the current output position assigned
19822 to the new glyph string constructed. HL overrides that face of the
19823 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19824 is the right-most x-position of the drawing area. */
19826 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19827 do \
19829 s = (struct glyph_string *) alloca (sizeof *s); \
19830 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19831 fill_image_glyph_string (s); \
19832 append_glyph_string (&HEAD, &TAIL, s); \
19833 ++START; \
19834 s->x = (X); \
19836 while (0)
19839 /* Add a glyph string for a sequence of character glyphs to the list
19840 of strings between HEAD and TAIL. START is the index of the first
19841 glyph in row area AREA of glyph row ROW that is part of the new
19842 glyph string. END is the index of the last glyph in that glyph row
19843 area. X is the current output position assigned to the new glyph
19844 string constructed. HL overrides that face of the glyph; e.g. it
19845 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19846 right-most x-position of the drawing area. */
19848 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19849 do \
19851 int face_id; \
19852 XChar2b *char2b; \
19854 face_id = (row)->glyphs[area][START].face_id; \
19856 s = (struct glyph_string *) alloca (sizeof *s); \
19857 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19858 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19859 append_glyph_string (&HEAD, &TAIL, s); \
19860 s->x = (X); \
19861 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19863 while (0)
19866 /* Add a glyph string for a composite sequence to the list of strings
19867 between HEAD and TAIL. START is the index of the first glyph in
19868 row area AREA of glyph row ROW that is part of the new glyph
19869 string. END is the index of the last glyph in that glyph row area.
19870 X is the current output position assigned to the new glyph string
19871 constructed. HL overrides that face of the glyph; e.g. it is
19872 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19873 x-position of the drawing area. */
19875 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19876 do { \
19877 int face_id = (row)->glyphs[area][START].face_id; \
19878 struct face *base_face = FACE_FROM_ID (f, face_id); \
19879 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19880 struct composition *cmp = composition_table[cmp_id]; \
19881 XChar2b *char2b; \
19882 struct glyph_string *first_s; \
19883 int n; \
19885 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19887 /* Make glyph_strings for each glyph sequence that is drawable by \
19888 the same face, and append them to HEAD/TAIL. */ \
19889 for (n = 0; n < cmp->glyph_len;) \
19891 s = (struct glyph_string *) alloca (sizeof *s); \
19892 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19893 append_glyph_string (&(HEAD), &(TAIL), s); \
19894 s->cmp = cmp; \
19895 s->gidx = n; \
19896 s->x = (X); \
19897 if (n == 0) \
19898 first_s = s; \
19899 n = fill_composite_glyph_string (s, base_face, overlaps); \
19902 ++START; \
19903 s = first_s; \
19904 } while (0)
19907 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19908 of AREA of glyph row ROW on window W between indices START and END.
19909 HL overrides the face for drawing glyph strings, e.g. it is
19910 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19911 x-positions of the drawing area.
19913 This is an ugly monster macro construct because we must use alloca
19914 to allocate glyph strings (because draw_glyphs can be called
19915 asynchronously). */
19917 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19918 do \
19920 HEAD = TAIL = NULL; \
19921 while (START < END) \
19923 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19924 switch (first_glyph->type) \
19926 case CHAR_GLYPH: \
19927 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19928 HL, X, LAST_X); \
19929 break; \
19931 case COMPOSITE_GLYPH: \
19932 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19933 HL, X, LAST_X); \
19934 break; \
19936 case STRETCH_GLYPH: \
19937 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19938 HL, X, LAST_X); \
19939 break; \
19941 case IMAGE_GLYPH: \
19942 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19943 HL, X, LAST_X); \
19944 break; \
19946 default: \
19947 abort (); \
19950 if (s) \
19952 set_glyph_string_background_width (s, START, LAST_X); \
19953 (X) += s->width; \
19957 while (0)
19960 /* Draw glyphs between START and END in AREA of ROW on window W,
19961 starting at x-position X. X is relative to AREA in W. HL is a
19962 face-override with the following meaning:
19964 DRAW_NORMAL_TEXT draw normally
19965 DRAW_CURSOR draw in cursor face
19966 DRAW_MOUSE_FACE draw in mouse face.
19967 DRAW_INVERSE_VIDEO draw in mode line face
19968 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19969 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19971 If OVERLAPS is non-zero, draw only the foreground of characters and
19972 clip to the physical height of ROW. Non-zero value also defines
19973 the overlapping part to be drawn:
19975 OVERLAPS_PRED overlap with preceding rows
19976 OVERLAPS_SUCC overlap with succeeding rows
19977 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19978 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19980 Value is the x-position reached, relative to AREA of W. */
19982 static int
19983 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19984 struct window *w;
19985 int x;
19986 struct glyph_row *row;
19987 enum glyph_row_area area;
19988 EMACS_INT start, end;
19989 enum draw_glyphs_face hl;
19990 int overlaps;
19992 struct glyph_string *head, *tail;
19993 struct glyph_string *s;
19994 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19995 int last_x, area_width;
19996 int x_reached;
19997 int i, j;
19998 struct frame *f = XFRAME (WINDOW_FRAME (w));
19999 DECLARE_HDC (hdc);
20001 ALLOCATE_HDC (hdc, f);
20003 /* Let's rather be paranoid than getting a SEGV. */
20004 end = min (end, row->used[area]);
20005 start = max (0, start);
20006 start = min (end, start);
20008 /* Translate X to frame coordinates. Set last_x to the right
20009 end of the drawing area. */
20010 if (row->full_width_p)
20012 /* X is relative to the left edge of W, without scroll bars
20013 or fringes. */
20014 x += WINDOW_LEFT_EDGE_X (w);
20015 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20017 else
20019 int area_left = window_box_left (w, area);
20020 x += area_left;
20021 area_width = window_box_width (w, area);
20022 last_x = area_left + area_width;
20025 /* Build a doubly-linked list of glyph_string structures between
20026 head and tail from what we have to draw. Note that the macro
20027 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20028 the reason we use a separate variable `i'. */
20029 i = start;
20030 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20031 if (tail)
20032 x_reached = tail->x + tail->background_width;
20033 else
20034 x_reached = x;
20036 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20037 the row, redraw some glyphs in front or following the glyph
20038 strings built above. */
20039 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20041 int dummy_x = 0;
20042 struct glyph_string *h, *t;
20044 /* Compute overhangs for all glyph strings. */
20045 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20046 for (s = head; s; s = s->next)
20047 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20049 /* Prepend glyph strings for glyphs in front of the first glyph
20050 string that are overwritten because of the first glyph
20051 string's left overhang. The background of all strings
20052 prepended must be drawn because the first glyph string
20053 draws over it. */
20054 i = left_overwritten (head);
20055 if (i >= 0)
20057 j = i;
20058 BUILD_GLYPH_STRINGS (j, start, h, t,
20059 DRAW_NORMAL_TEXT, dummy_x, last_x);
20060 start = i;
20061 compute_overhangs_and_x (t, head->x, 1);
20062 prepend_glyph_string_lists (&head, &tail, h, t);
20063 clip_head = head;
20066 /* Prepend glyph strings for glyphs in front of the first glyph
20067 string that overwrite that glyph string because of their
20068 right overhang. For these strings, only the foreground must
20069 be drawn, because it draws over the glyph string at `head'.
20070 The background must not be drawn because this would overwrite
20071 right overhangs of preceding glyphs for which no glyph
20072 strings exist. */
20073 i = left_overwriting (head);
20074 if (i >= 0)
20076 clip_head = head;
20077 BUILD_GLYPH_STRINGS (i, start, h, t,
20078 DRAW_NORMAL_TEXT, dummy_x, last_x);
20079 for (s = h; s; s = s->next)
20080 s->background_filled_p = 1;
20081 compute_overhangs_and_x (t, head->x, 1);
20082 prepend_glyph_string_lists (&head, &tail, h, t);
20085 /* Append glyphs strings for glyphs following the last glyph
20086 string tail that are overwritten by tail. The background of
20087 these strings has to be drawn because tail's foreground draws
20088 over it. */
20089 i = right_overwritten (tail);
20090 if (i >= 0)
20092 BUILD_GLYPH_STRINGS (end, i, h, t,
20093 DRAW_NORMAL_TEXT, x, last_x);
20094 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20095 append_glyph_string_lists (&head, &tail, h, t);
20096 clip_tail = tail;
20099 /* Append glyph strings for glyphs following the last glyph
20100 string tail that overwrite tail. The foreground of such
20101 glyphs has to be drawn because it writes into the background
20102 of tail. The background must not be drawn because it could
20103 paint over the foreground of following glyphs. */
20104 i = right_overwriting (tail);
20105 if (i >= 0)
20107 clip_tail = tail;
20108 i++; /* We must include the Ith glyph. */
20109 BUILD_GLYPH_STRINGS (end, i, h, t,
20110 DRAW_NORMAL_TEXT, x, last_x);
20111 for (s = h; s; s = s->next)
20112 s->background_filled_p = 1;
20113 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20114 append_glyph_string_lists (&head, &tail, h, t);
20116 if (clip_head || clip_tail)
20117 for (s = head; s; s = s->next)
20119 s->clip_head = clip_head;
20120 s->clip_tail = clip_tail;
20124 /* Draw all strings. */
20125 for (s = head; s; s = s->next)
20126 FRAME_RIF (f)->draw_glyph_string (s);
20128 if (area == TEXT_AREA
20129 && !row->full_width_p
20130 /* When drawing overlapping rows, only the glyph strings'
20131 foreground is drawn, which doesn't erase a cursor
20132 completely. */
20133 && !overlaps)
20135 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20136 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20137 : (tail ? tail->x + tail->background_width : x));
20139 int text_left = window_box_left (w, TEXT_AREA);
20140 x0 -= text_left;
20141 x1 -= text_left;
20143 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20144 row->y, MATRIX_ROW_BOTTOM_Y (row));
20147 /* Value is the x-position up to which drawn, relative to AREA of W.
20148 This doesn't include parts drawn because of overhangs. */
20149 if (row->full_width_p)
20150 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20151 else
20152 x_reached -= window_box_left (w, area);
20154 RELEASE_HDC (hdc, f);
20156 return x_reached;
20159 /* Expand row matrix if too narrow. Don't expand if area
20160 is not present. */
20162 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20164 if (!fonts_changed_p \
20165 && (it->glyph_row->glyphs[area] \
20166 < it->glyph_row->glyphs[area + 1])) \
20168 it->w->ncols_scale_factor++; \
20169 fonts_changed_p = 1; \
20173 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20174 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20176 static INLINE void
20177 append_glyph (it)
20178 struct it *it;
20180 struct glyph *glyph;
20181 enum glyph_row_area area = it->area;
20183 xassert (it->glyph_row);
20184 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20186 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20187 if (glyph < it->glyph_row->glyphs[area + 1])
20189 glyph->charpos = CHARPOS (it->position);
20190 glyph->object = it->object;
20191 glyph->pixel_width = it->pixel_width;
20192 glyph->ascent = it->ascent;
20193 glyph->descent = it->descent;
20194 glyph->voffset = it->voffset;
20195 glyph->type = CHAR_GLYPH;
20196 glyph->multibyte_p = it->multibyte_p;
20197 glyph->left_box_line_p = it->start_of_box_run_p;
20198 glyph->right_box_line_p = it->end_of_box_run_p;
20199 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20200 || it->phys_descent > it->descent);
20201 glyph->padding_p = 0;
20202 glyph->glyph_not_available_p = it->glyph_not_available_p;
20203 glyph->face_id = it->face_id;
20204 glyph->u.ch = it->char_to_display;
20205 glyph->slice = null_glyph_slice;
20206 glyph->font_type = FONT_TYPE_UNKNOWN;
20207 ++it->glyph_row->used[area];
20209 else
20210 IT_EXPAND_MATRIX_WIDTH (it, area);
20213 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20214 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20216 static INLINE void
20217 append_composite_glyph (it)
20218 struct it *it;
20220 struct glyph *glyph;
20221 enum glyph_row_area area = it->area;
20223 xassert (it->glyph_row);
20225 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20226 if (glyph < it->glyph_row->glyphs[area + 1])
20228 glyph->charpos = CHARPOS (it->position);
20229 glyph->object = it->object;
20230 glyph->pixel_width = it->pixel_width;
20231 glyph->ascent = it->ascent;
20232 glyph->descent = it->descent;
20233 glyph->voffset = it->voffset;
20234 glyph->type = COMPOSITE_GLYPH;
20235 glyph->multibyte_p = it->multibyte_p;
20236 glyph->left_box_line_p = it->start_of_box_run_p;
20237 glyph->right_box_line_p = it->end_of_box_run_p;
20238 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20239 || it->phys_descent > it->descent);
20240 glyph->padding_p = 0;
20241 glyph->glyph_not_available_p = 0;
20242 glyph->face_id = it->face_id;
20243 glyph->u.cmp_id = it->cmp_id;
20244 glyph->slice = null_glyph_slice;
20245 glyph->font_type = FONT_TYPE_UNKNOWN;
20246 ++it->glyph_row->used[area];
20248 else
20249 IT_EXPAND_MATRIX_WIDTH (it, area);
20253 /* Change IT->ascent and IT->height according to the setting of
20254 IT->voffset. */
20256 static INLINE void
20257 take_vertical_position_into_account (it)
20258 struct it *it;
20260 if (it->voffset)
20262 if (it->voffset < 0)
20263 /* Increase the ascent so that we can display the text higher
20264 in the line. */
20265 it->ascent -= it->voffset;
20266 else
20267 /* Increase the descent so that we can display the text lower
20268 in the line. */
20269 it->descent += it->voffset;
20274 /* Produce glyphs/get display metrics for the image IT is loaded with.
20275 See the description of struct display_iterator in dispextern.h for
20276 an overview of struct display_iterator. */
20278 static void
20279 produce_image_glyph (it)
20280 struct it *it;
20282 struct image *img;
20283 struct face *face;
20284 int glyph_ascent, crop;
20285 struct glyph_slice slice;
20287 xassert (it->what == IT_IMAGE);
20289 face = FACE_FROM_ID (it->f, it->face_id);
20290 xassert (face);
20291 /* Make sure X resources of the face is loaded. */
20292 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20294 if (it->image_id < 0)
20296 /* Fringe bitmap. */
20297 it->ascent = it->phys_ascent = 0;
20298 it->descent = it->phys_descent = 0;
20299 it->pixel_width = 0;
20300 it->nglyphs = 0;
20301 return;
20304 img = IMAGE_FROM_ID (it->f, it->image_id);
20305 xassert (img);
20306 /* Make sure X resources of the image is loaded. */
20307 prepare_image_for_display (it->f, img);
20309 slice.x = slice.y = 0;
20310 slice.width = img->width;
20311 slice.height = img->height;
20313 if (INTEGERP (it->slice.x))
20314 slice.x = XINT (it->slice.x);
20315 else if (FLOATP (it->slice.x))
20316 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20318 if (INTEGERP (it->slice.y))
20319 slice.y = XINT (it->slice.y);
20320 else if (FLOATP (it->slice.y))
20321 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20323 if (INTEGERP (it->slice.width))
20324 slice.width = XINT (it->slice.width);
20325 else if (FLOATP (it->slice.width))
20326 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20328 if (INTEGERP (it->slice.height))
20329 slice.height = XINT (it->slice.height);
20330 else if (FLOATP (it->slice.height))
20331 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20333 if (slice.x >= img->width)
20334 slice.x = img->width;
20335 if (slice.y >= img->height)
20336 slice.y = img->height;
20337 if (slice.x + slice.width >= img->width)
20338 slice.width = img->width - slice.x;
20339 if (slice.y + slice.height > img->height)
20340 slice.height = img->height - slice.y;
20342 if (slice.width == 0 || slice.height == 0)
20343 return;
20345 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20347 it->descent = slice.height - glyph_ascent;
20348 if (slice.y == 0)
20349 it->descent += img->vmargin;
20350 if (slice.y + slice.height == img->height)
20351 it->descent += img->vmargin;
20352 it->phys_descent = it->descent;
20354 it->pixel_width = slice.width;
20355 if (slice.x == 0)
20356 it->pixel_width += img->hmargin;
20357 if (slice.x + slice.width == img->width)
20358 it->pixel_width += img->hmargin;
20360 /* It's quite possible for images to have an ascent greater than
20361 their height, so don't get confused in that case. */
20362 if (it->descent < 0)
20363 it->descent = 0;
20365 #if 0 /* this breaks image tiling */
20366 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20367 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20368 if (face_ascent > it->ascent)
20369 it->ascent = it->phys_ascent = face_ascent;
20370 #endif
20372 it->nglyphs = 1;
20374 if (face->box != FACE_NO_BOX)
20376 if (face->box_line_width > 0)
20378 if (slice.y == 0)
20379 it->ascent += face->box_line_width;
20380 if (slice.y + slice.height == img->height)
20381 it->descent += face->box_line_width;
20384 if (it->start_of_box_run_p && slice.x == 0)
20385 it->pixel_width += eabs (face->box_line_width);
20386 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20387 it->pixel_width += eabs (face->box_line_width);
20390 take_vertical_position_into_account (it);
20392 /* Automatically crop wide image glyphs at right edge so we can
20393 draw the cursor on same display row. */
20394 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20395 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20397 it->pixel_width -= crop;
20398 slice.width -= crop;
20401 if (it->glyph_row)
20403 struct glyph *glyph;
20404 enum glyph_row_area area = it->area;
20406 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20407 if (glyph < it->glyph_row->glyphs[area + 1])
20409 glyph->charpos = CHARPOS (it->position);
20410 glyph->object = it->object;
20411 glyph->pixel_width = it->pixel_width;
20412 glyph->ascent = glyph_ascent;
20413 glyph->descent = it->descent;
20414 glyph->voffset = it->voffset;
20415 glyph->type = IMAGE_GLYPH;
20416 glyph->multibyte_p = it->multibyte_p;
20417 glyph->left_box_line_p = it->start_of_box_run_p;
20418 glyph->right_box_line_p = it->end_of_box_run_p;
20419 glyph->overlaps_vertically_p = 0;
20420 glyph->padding_p = 0;
20421 glyph->glyph_not_available_p = 0;
20422 glyph->face_id = it->face_id;
20423 glyph->u.img_id = img->id;
20424 glyph->slice = slice;
20425 glyph->font_type = FONT_TYPE_UNKNOWN;
20426 ++it->glyph_row->used[area];
20428 else
20429 IT_EXPAND_MATRIX_WIDTH (it, area);
20434 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20435 of the glyph, WIDTH and HEIGHT are the width and height of the
20436 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20438 static void
20439 append_stretch_glyph (it, object, width, height, ascent)
20440 struct it *it;
20441 Lisp_Object object;
20442 int width, height;
20443 int ascent;
20445 struct glyph *glyph;
20446 enum glyph_row_area area = it->area;
20448 xassert (ascent >= 0 && ascent <= height);
20450 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20451 if (glyph < it->glyph_row->glyphs[area + 1])
20453 glyph->charpos = CHARPOS (it->position);
20454 glyph->object = object;
20455 glyph->pixel_width = width;
20456 glyph->ascent = ascent;
20457 glyph->descent = height - ascent;
20458 glyph->voffset = it->voffset;
20459 glyph->type = STRETCH_GLYPH;
20460 glyph->multibyte_p = it->multibyte_p;
20461 glyph->left_box_line_p = it->start_of_box_run_p;
20462 glyph->right_box_line_p = it->end_of_box_run_p;
20463 glyph->overlaps_vertically_p = 0;
20464 glyph->padding_p = 0;
20465 glyph->glyph_not_available_p = 0;
20466 glyph->face_id = it->face_id;
20467 glyph->u.stretch.ascent = ascent;
20468 glyph->u.stretch.height = height;
20469 glyph->slice = null_glyph_slice;
20470 glyph->font_type = FONT_TYPE_UNKNOWN;
20471 ++it->glyph_row->used[area];
20473 else
20474 IT_EXPAND_MATRIX_WIDTH (it, area);
20478 /* Produce a stretch glyph for iterator IT. IT->object is the value
20479 of the glyph property displayed. The value must be a list
20480 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20481 being recognized:
20483 1. `:width WIDTH' specifies that the space should be WIDTH *
20484 canonical char width wide. WIDTH may be an integer or floating
20485 point number.
20487 2. `:relative-width FACTOR' specifies that the width of the stretch
20488 should be computed from the width of the first character having the
20489 `glyph' property, and should be FACTOR times that width.
20491 3. `:align-to HPOS' specifies that the space should be wide enough
20492 to reach HPOS, a value in canonical character units.
20494 Exactly one of the above pairs must be present.
20496 4. `:height HEIGHT' specifies that the height of the stretch produced
20497 should be HEIGHT, measured in canonical character units.
20499 5. `:relative-height FACTOR' specifies that the height of the
20500 stretch should be FACTOR times the height of the characters having
20501 the glyph property.
20503 Either none or exactly one of 4 or 5 must be present.
20505 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20506 of the stretch should be used for the ascent of the stretch.
20507 ASCENT must be in the range 0 <= ASCENT <= 100. */
20509 static void
20510 produce_stretch_glyph (it)
20511 struct it *it;
20513 /* (space :width WIDTH :height HEIGHT ...) */
20514 Lisp_Object prop, plist;
20515 int width = 0, height = 0, align_to = -1;
20516 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20517 int ascent = 0;
20518 double tem;
20519 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20520 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20522 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20524 /* List should start with `space'. */
20525 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20526 plist = XCDR (it->object);
20528 /* Compute the width of the stretch. */
20529 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20530 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20532 /* Absolute width `:width WIDTH' specified and valid. */
20533 zero_width_ok_p = 1;
20534 width = (int)tem;
20536 else if (prop = Fplist_get (plist, QCrelative_width),
20537 NUMVAL (prop) > 0)
20539 /* Relative width `:relative-width FACTOR' specified and valid.
20540 Compute the width of the characters having the `glyph'
20541 property. */
20542 struct it it2;
20543 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20545 it2 = *it;
20546 if (it->multibyte_p)
20548 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20549 - IT_BYTEPOS (*it));
20550 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20552 else
20553 it2.c = *p, it2.len = 1;
20555 it2.glyph_row = NULL;
20556 it2.what = IT_CHARACTER;
20557 x_produce_glyphs (&it2);
20558 width = NUMVAL (prop) * it2.pixel_width;
20560 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20561 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20563 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20564 align_to = (align_to < 0
20566 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20567 else if (align_to < 0)
20568 align_to = window_box_left_offset (it->w, TEXT_AREA);
20569 width = max (0, (int)tem + align_to - it->current_x);
20570 zero_width_ok_p = 1;
20572 else
20573 /* Nothing specified -> width defaults to canonical char width. */
20574 width = FRAME_COLUMN_WIDTH (it->f);
20576 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20577 width = 1;
20579 /* Compute height. */
20580 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20581 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20583 height = (int)tem;
20584 zero_height_ok_p = 1;
20586 else if (prop = Fplist_get (plist, QCrelative_height),
20587 NUMVAL (prop) > 0)
20588 height = FONT_HEIGHT (font) * NUMVAL (prop);
20589 else
20590 height = FONT_HEIGHT (font);
20592 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20593 height = 1;
20595 /* Compute percentage of height used for ascent. If
20596 `:ascent ASCENT' is present and valid, use that. Otherwise,
20597 derive the ascent from the font in use. */
20598 if (prop = Fplist_get (plist, QCascent),
20599 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20600 ascent = height * NUMVAL (prop) / 100.0;
20601 else if (!NILP (prop)
20602 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20603 ascent = min (max (0, (int)tem), height);
20604 else
20605 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20607 if (width > 0 && !it->truncate_lines_p
20608 && it->current_x + width > it->last_visible_x)
20609 width = it->last_visible_x - it->current_x - 1;
20611 if (width > 0 && height > 0 && it->glyph_row)
20613 Lisp_Object object = it->stack[it->sp - 1].string;
20614 if (!STRINGP (object))
20615 object = it->w->buffer;
20616 append_stretch_glyph (it, object, width, height, ascent);
20619 it->pixel_width = width;
20620 it->ascent = it->phys_ascent = ascent;
20621 it->descent = it->phys_descent = height - it->ascent;
20622 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20624 take_vertical_position_into_account (it);
20627 /* Get line-height and line-spacing property at point.
20628 If line-height has format (HEIGHT TOTAL), return TOTAL
20629 in TOTAL_HEIGHT. */
20631 static Lisp_Object
20632 get_line_height_property (it, prop)
20633 struct it *it;
20634 Lisp_Object prop;
20636 Lisp_Object position;
20638 if (STRINGP (it->object))
20639 position = make_number (IT_STRING_CHARPOS (*it));
20640 else if (BUFFERP (it->object))
20641 position = make_number (IT_CHARPOS (*it));
20642 else
20643 return Qnil;
20645 return Fget_char_property (position, prop, it->object);
20648 /* Calculate line-height and line-spacing properties.
20649 An integer value specifies explicit pixel value.
20650 A float value specifies relative value to current face height.
20651 A cons (float . face-name) specifies relative value to
20652 height of specified face font.
20654 Returns height in pixels, or nil. */
20657 static Lisp_Object
20658 calc_line_height_property (it, val, font, boff, override)
20659 struct it *it;
20660 Lisp_Object val;
20661 XFontStruct *font;
20662 int boff, override;
20664 Lisp_Object face_name = Qnil;
20665 int ascent, descent, height;
20667 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20668 return val;
20670 if (CONSP (val))
20672 face_name = XCAR (val);
20673 val = XCDR (val);
20674 if (!NUMBERP (val))
20675 val = make_number (1);
20676 if (NILP (face_name))
20678 height = it->ascent + it->descent;
20679 goto scale;
20683 if (NILP (face_name))
20685 font = FRAME_FONT (it->f);
20686 boff = FRAME_BASELINE_OFFSET (it->f);
20688 else if (EQ (face_name, Qt))
20690 override = 0;
20692 else
20694 int face_id;
20695 struct face *face;
20696 struct font_info *font_info;
20698 face_id = lookup_named_face (it->f, face_name, 0);
20699 if (face_id < 0)
20700 return make_number (-1);
20702 face = FACE_FROM_ID (it->f, face_id);
20703 font = face->font;
20704 if (font == NULL)
20705 return make_number (-1);
20707 font_info = FONT_INFO_FROM_FACE (it->f, face);
20708 boff = font_info->baseline_offset;
20709 if (font_info->vertical_centering)
20710 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20713 ascent = FONT_BASE (font) + boff;
20714 descent = FONT_DESCENT (font) - boff;
20716 if (override)
20718 it->override_ascent = ascent;
20719 it->override_descent = descent;
20720 it->override_boff = boff;
20723 height = ascent + descent;
20725 scale:
20726 if (FLOATP (val))
20727 height = (int)(XFLOAT_DATA (val) * height);
20728 else if (INTEGERP (val))
20729 height *= XINT (val);
20731 return make_number (height);
20735 /* RIF:
20736 Produce glyphs/get display metrics for the display element IT is
20737 loaded with. See the description of struct it in dispextern.h
20738 for an overview of struct it. */
20740 void
20741 x_produce_glyphs (it)
20742 struct it *it;
20744 int extra_line_spacing = it->extra_line_spacing;
20746 it->glyph_not_available_p = 0;
20748 if (it->what == IT_CHARACTER)
20750 XChar2b char2b;
20751 XFontStruct *font;
20752 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20753 XCharStruct *pcm;
20754 int font_not_found_p;
20755 struct font_info *font_info;
20756 int boff; /* baseline offset */
20757 /* We may change it->multibyte_p upon unibyte<->multibyte
20758 conversion. So, save the current value now and restore it
20759 later.
20761 Note: It seems that we don't have to record multibyte_p in
20762 struct glyph because the character code itself tells if or
20763 not the character is multibyte. Thus, in the future, we must
20764 consider eliminating the field `multibyte_p' in the struct
20765 glyph. */
20766 int saved_multibyte_p = it->multibyte_p;
20768 /* Maybe translate single-byte characters to multibyte, or the
20769 other way. */
20770 it->char_to_display = it->c;
20771 if (!ASCII_BYTE_P (it->c)
20772 && ! it->multibyte_p)
20774 if (SINGLE_BYTE_CHAR_P (it->c)
20775 && unibyte_display_via_language_environment)
20776 it->char_to_display = unibyte_char_to_multibyte (it->c);
20777 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20779 it->multibyte_p = 1;
20780 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20781 -1, Qnil);
20782 face = FACE_FROM_ID (it->f, it->face_id);
20786 /* Get font to use. Encode IT->char_to_display. */
20787 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20788 &char2b, it->multibyte_p, 0);
20789 font = face->font;
20791 /* When no suitable font found, use the default font. */
20792 font_not_found_p = font == NULL;
20793 if (font_not_found_p)
20795 font = FRAME_FONT (it->f);
20796 boff = FRAME_BASELINE_OFFSET (it->f);
20797 font_info = NULL;
20799 else
20801 font_info = FONT_INFO_FROM_FACE (it->f, face);
20802 boff = font_info->baseline_offset;
20803 if (font_info->vertical_centering)
20804 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20807 if (it->char_to_display >= ' '
20808 && (!it->multibyte_p || it->char_to_display < 128))
20810 /* Either unibyte or ASCII. */
20811 int stretched_p;
20813 it->nglyphs = 1;
20815 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
20816 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20818 if (it->override_ascent >= 0)
20820 it->ascent = it->override_ascent;
20821 it->descent = it->override_descent;
20822 boff = it->override_boff;
20824 else
20826 it->ascent = FONT_BASE (font) + boff;
20827 it->descent = FONT_DESCENT (font) - boff;
20830 if (pcm)
20832 it->phys_ascent = pcm->ascent + boff;
20833 it->phys_descent = pcm->descent - boff;
20834 it->pixel_width = pcm->width;
20836 else
20838 it->glyph_not_available_p = 1;
20839 it->phys_ascent = it->ascent;
20840 it->phys_descent = it->descent;
20841 it->pixel_width = FONT_WIDTH (font);
20844 if (it->constrain_row_ascent_descent_p)
20846 if (it->descent > it->max_descent)
20848 it->ascent += it->descent - it->max_descent;
20849 it->descent = it->max_descent;
20851 if (it->ascent > it->max_ascent)
20853 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20854 it->ascent = it->max_ascent;
20856 it->phys_ascent = min (it->phys_ascent, it->ascent);
20857 it->phys_descent = min (it->phys_descent, it->descent);
20858 extra_line_spacing = 0;
20861 /* If this is a space inside a region of text with
20862 `space-width' property, change its width. */
20863 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20864 if (stretched_p)
20865 it->pixel_width *= XFLOATINT (it->space_width);
20867 /* If face has a box, add the box thickness to the character
20868 height. If character has a box line to the left and/or
20869 right, add the box line width to the character's width. */
20870 if (face->box != FACE_NO_BOX)
20872 int thick = face->box_line_width;
20874 if (thick > 0)
20876 it->ascent += thick;
20877 it->descent += thick;
20879 else
20880 thick = -thick;
20882 if (it->start_of_box_run_p)
20883 it->pixel_width += thick;
20884 if (it->end_of_box_run_p)
20885 it->pixel_width += thick;
20888 /* If face has an overline, add the height of the overline
20889 (1 pixel) and a 1 pixel margin to the character height. */
20890 if (face->overline_p)
20891 it->ascent += overline_margin;
20893 if (it->constrain_row_ascent_descent_p)
20895 if (it->ascent > it->max_ascent)
20896 it->ascent = it->max_ascent;
20897 if (it->descent > it->max_descent)
20898 it->descent = it->max_descent;
20901 take_vertical_position_into_account (it);
20903 /* If we have to actually produce glyphs, do it. */
20904 if (it->glyph_row)
20906 if (stretched_p)
20908 /* Translate a space with a `space-width' property
20909 into a stretch glyph. */
20910 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20911 / FONT_HEIGHT (font));
20912 append_stretch_glyph (it, it->object, it->pixel_width,
20913 it->ascent + it->descent, ascent);
20915 else
20916 append_glyph (it);
20918 /* If characters with lbearing or rbearing are displayed
20919 in this line, record that fact in a flag of the
20920 glyph row. This is used to optimize X output code. */
20921 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20922 it->glyph_row->contains_overlapping_glyphs_p = 1;
20925 else if (it->char_to_display == '\n')
20927 /* A newline has no width but we need the height of the line.
20928 But if previous part of the line set a height, don't
20929 increase that height */
20931 Lisp_Object height;
20932 Lisp_Object total_height = Qnil;
20934 it->override_ascent = -1;
20935 it->pixel_width = 0;
20936 it->nglyphs = 0;
20938 height = get_line_height_property(it, Qline_height);
20939 /* Split (line-height total-height) list */
20940 if (CONSP (height)
20941 && CONSP (XCDR (height))
20942 && NILP (XCDR (XCDR (height))))
20944 total_height = XCAR (XCDR (height));
20945 height = XCAR (height);
20947 height = calc_line_height_property(it, height, font, boff, 1);
20949 if (it->override_ascent >= 0)
20951 it->ascent = it->override_ascent;
20952 it->descent = it->override_descent;
20953 boff = it->override_boff;
20955 else
20957 it->ascent = FONT_BASE (font) + boff;
20958 it->descent = FONT_DESCENT (font) - boff;
20961 if (EQ (height, Qt))
20963 if (it->descent > it->max_descent)
20965 it->ascent += it->descent - it->max_descent;
20966 it->descent = it->max_descent;
20968 if (it->ascent > it->max_ascent)
20970 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20971 it->ascent = it->max_ascent;
20973 it->phys_ascent = min (it->phys_ascent, it->ascent);
20974 it->phys_descent = min (it->phys_descent, it->descent);
20975 it->constrain_row_ascent_descent_p = 1;
20976 extra_line_spacing = 0;
20978 else
20980 Lisp_Object spacing;
20982 it->phys_ascent = it->ascent;
20983 it->phys_descent = it->descent;
20985 if ((it->max_ascent > 0 || it->max_descent > 0)
20986 && face->box != FACE_NO_BOX
20987 && face->box_line_width > 0)
20989 it->ascent += face->box_line_width;
20990 it->descent += face->box_line_width;
20992 if (!NILP (height)
20993 && XINT (height) > it->ascent + it->descent)
20994 it->ascent = XINT (height) - it->descent;
20996 if (!NILP (total_height))
20997 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20998 else
21000 spacing = get_line_height_property(it, Qline_spacing);
21001 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21003 if (INTEGERP (spacing))
21005 extra_line_spacing = XINT (spacing);
21006 if (!NILP (total_height))
21007 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21011 else if (it->char_to_display == '\t')
21013 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
21014 int x = it->current_x + it->continuation_lines_width;
21015 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21017 /* If the distance from the current position to the next tab
21018 stop is less than a space character width, use the
21019 tab stop after that. */
21020 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
21021 next_tab_x += tab_width;
21023 it->pixel_width = next_tab_x - x;
21024 it->nglyphs = 1;
21025 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21026 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21028 if (it->glyph_row)
21030 append_stretch_glyph (it, it->object, it->pixel_width,
21031 it->ascent + it->descent, it->ascent);
21034 else
21036 /* A multi-byte character. Assume that the display width of the
21037 character is the width of the character multiplied by the
21038 width of the font. */
21040 /* If we found a font, this font should give us the right
21041 metrics. If we didn't find a font, use the frame's
21042 default font and calculate the width of the character by
21043 multiplying the width of font by the width of the
21044 character. */
21046 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21047 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
21049 if (font_not_found_p || !pcm)
21051 int char_width = CHAR_WIDTH (it->char_to_display);
21053 if (char_width == 0)
21054 /* This is a non spacing character. But, as we are
21055 going to display an empty box, the box must occupy
21056 at least one column. */
21057 char_width = 1;
21058 it->glyph_not_available_p = 1;
21059 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21060 it->phys_ascent = FONT_BASE (font) + boff;
21061 it->phys_descent = FONT_DESCENT (font) - boff;
21063 else
21065 it->pixel_width = pcm->width;
21066 it->phys_ascent = pcm->ascent + boff;
21067 it->phys_descent = pcm->descent - boff;
21068 if (it->glyph_row
21069 && (pcm->lbearing < 0
21070 || pcm->rbearing > pcm->width))
21071 it->glyph_row->contains_overlapping_glyphs_p = 1;
21073 it->nglyphs = 1;
21074 it->ascent = FONT_BASE (font) + boff;
21075 it->descent = FONT_DESCENT (font) - boff;
21076 if (face->box != FACE_NO_BOX)
21078 int thick = face->box_line_width;
21080 if (thick > 0)
21082 it->ascent += thick;
21083 it->descent += thick;
21085 else
21086 thick = - thick;
21088 if (it->start_of_box_run_p)
21089 it->pixel_width += thick;
21090 if (it->end_of_box_run_p)
21091 it->pixel_width += thick;
21094 /* If face has an overline, add the height of the overline
21095 (1 pixel) and a 1 pixel margin to the character height. */
21096 if (face->overline_p)
21097 it->ascent += overline_margin;
21099 take_vertical_position_into_account (it);
21101 if (it->ascent < 0)
21102 it->ascent = 0;
21103 if (it->descent < 0)
21104 it->descent = 0;
21106 if (it->glyph_row)
21107 append_glyph (it);
21109 it->multibyte_p = saved_multibyte_p;
21111 else if (it->what == IT_COMPOSITION)
21113 /* Note: A composition is represented as one glyph in the
21114 glyph matrix. There are no padding glyphs.
21116 Important is that pixel_width, ascent, and descent are the
21117 values of what is drawn by draw_glyphs (i.e. the values of
21118 the overall glyphs composed). */
21119 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21120 int boff; /* baseline offset */
21121 struct composition *cmp = composition_table[it->cmp_id];
21122 int glyph_len = cmp->glyph_len;
21123 XFontStruct *font = face->font;
21125 it->nglyphs = 1;
21127 #ifdef USE_FONT_BACKEND
21128 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21130 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21131 font_prepare_composition (cmp, it->f);
21133 else
21134 #endif /* USE_FONT_BACKEND */
21135 /* If we have not yet calculated pixel size data of glyphs of
21136 the composition for the current face font, calculate them
21137 now. Theoretically, we have to check all fonts for the
21138 glyphs, but that requires much time and memory space. So,
21139 here we check only the font of the first glyph. This leads
21140 to incorrect display, but it's very rare, and C-l (recenter)
21141 can correct the display anyway. */
21142 if (! cmp->font || cmp->font != font)
21144 /* Ascent and descent of the font of the first character
21145 of this composition (adjusted by baseline offset).
21146 Ascent and descent of overall glyphs should not be less
21147 than them respectively. */
21148 int font_ascent, font_descent, font_height;
21149 /* Bounding box of the overall glyphs. */
21150 int leftmost, rightmost, lowest, highest;
21151 int lbearing, rbearing;
21152 int i, width, ascent, descent;
21153 int left_padded = 0, right_padded = 0;
21154 int face_id;
21155 int c;
21156 XChar2b char2b;
21157 XCharStruct *pcm;
21158 int font_not_found_p;
21159 struct font_info *font_info;
21160 int pos;
21162 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21163 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21164 break;
21165 if (glyph_len < cmp->glyph_len)
21166 right_padded = 1;
21167 for (i = 0; i < glyph_len; i++)
21169 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21170 break;
21171 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21173 if (i > 0)
21174 left_padded = 1;
21176 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21177 : IT_CHARPOS (*it));
21178 /* When no suitable font found, use the default font. */
21179 font_not_found_p = font == NULL;
21180 if (font_not_found_p)
21182 face = face->ascii_face;
21183 font = face->font;
21185 font_info = FONT_INFO_FROM_FACE (it->f, face);
21186 boff = font_info->baseline_offset;
21187 if (font_info->vertical_centering)
21188 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21189 font_ascent = FONT_BASE (font) + boff;
21190 font_descent = FONT_DESCENT (font) - boff;
21191 font_height = FONT_HEIGHT (font);
21193 cmp->font = (void *) font;
21195 pcm = NULL;
21196 if (! font_not_found_p)
21198 get_char_face_and_encoding (it->f, c, it->face_id,
21199 &char2b, it->multibyte_p, 0);
21200 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21201 FONT_TYPE_FOR_MULTIBYTE (font, c));
21204 /* Initialize the bounding box. */
21205 if (pcm)
21207 width = pcm->width;
21208 ascent = pcm->ascent;
21209 descent = pcm->descent;
21210 lbearing = pcm->lbearing;
21211 rbearing = pcm->rbearing;
21213 else
21215 width = FONT_WIDTH (font);
21216 ascent = FONT_BASE (font);
21217 descent = FONT_DESCENT (font);
21218 lbearing = 0;
21219 rbearing = width;
21222 rightmost = width;
21223 leftmost = 0;
21224 lowest = - descent + boff;
21225 highest = ascent + boff;
21227 if (! font_not_found_p
21228 && font_info->default_ascent
21229 && CHAR_TABLE_P (Vuse_default_ascent)
21230 && !NILP (Faref (Vuse_default_ascent,
21231 make_number (it->char_to_display))))
21232 highest = font_info->default_ascent + boff;
21234 /* Draw the first glyph at the normal position. It may be
21235 shifted to right later if some other glyphs are drawn
21236 at the left. */
21237 cmp->offsets[i * 2] = 0;
21238 cmp->offsets[i * 2 + 1] = boff;
21239 cmp->lbearing = lbearing;
21240 cmp->rbearing = rbearing;
21242 /* Set cmp->offsets for the remaining glyphs. */
21243 for (i++; i < glyph_len; i++)
21245 int left, right, btm, top;
21246 int ch = COMPOSITION_GLYPH (cmp, i);
21247 int face_id;
21248 struct face *this_face;
21249 int this_boff;
21251 if (ch == '\t')
21252 ch = ' ';
21253 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21254 this_face = FACE_FROM_ID (it->f, face_id);
21255 font = this_face->font;
21257 if (font == NULL)
21258 pcm = NULL;
21259 else
21261 font_info = FONT_INFO_FROM_FACE (it->f, this_face);
21262 this_boff = font_info->baseline_offset;
21263 if (font_info->vertical_centering)
21264 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21265 get_char_face_and_encoding (it->f, ch, face_id,
21266 &char2b, it->multibyte_p, 0);
21267 pcm = get_per_char_metric (it->f, font, font_info, &char2b,
21268 FONT_TYPE_FOR_MULTIBYTE (font,
21269 ch));
21271 if (! pcm)
21272 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21273 else
21275 width = pcm->width;
21276 ascent = pcm->ascent;
21277 descent = pcm->descent;
21278 lbearing = pcm->lbearing;
21279 rbearing = pcm->rbearing;
21280 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21282 /* Relative composition with or without
21283 alternate chars. */
21284 left = (leftmost + rightmost - width) / 2;
21285 btm = - descent + boff;
21286 if (font_info->relative_compose
21287 && (! CHAR_TABLE_P (Vignore_relative_composition)
21288 || NILP (Faref (Vignore_relative_composition,
21289 make_number (ch)))))
21292 if (- descent >= font_info->relative_compose)
21293 /* One extra pixel between two glyphs. */
21294 btm = highest + 1;
21295 else if (ascent <= 0)
21296 /* One extra pixel between two glyphs. */
21297 btm = lowest - 1 - ascent - descent;
21300 else
21302 /* A composition rule is specified by an integer
21303 value that encodes global and new reference
21304 points (GREF and NREF). GREF and NREF are
21305 specified by numbers as below:
21307 0---1---2 -- ascent
21311 9--10--11 -- center
21313 ---3---4---5--- baseline
21315 6---7---8 -- descent
21317 int rule = COMPOSITION_RULE (cmp, i);
21318 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21320 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21321 grefx = gref % 3, nrefx = nref % 3;
21322 grefy = gref / 3, nrefy = nref / 3;
21323 if (xoff)
21324 xoff = font_height * (xoff - 128) / 256;
21325 if (yoff)
21326 yoff = font_height * (yoff - 128) / 256;
21328 left = (leftmost
21329 + grefx * (rightmost - leftmost) / 2
21330 - nrefx * width / 2
21331 + xoff);
21333 btm = ((grefy == 0 ? highest
21334 : grefy == 1 ? 0
21335 : grefy == 2 ? lowest
21336 : (highest + lowest) / 2)
21337 - (nrefy == 0 ? ascent + descent
21338 : nrefy == 1 ? descent - boff
21339 : nrefy == 2 ? 0
21340 : (ascent + descent) / 2)
21341 + yoff);
21344 cmp->offsets[i * 2] = left;
21345 cmp->offsets[i * 2 + 1] = btm + descent;
21347 /* Update the bounding box of the overall glyphs. */
21348 if (width > 0)
21350 right = left + width;
21351 if (left < leftmost)
21352 leftmost = left;
21353 if (right > rightmost)
21354 rightmost = right;
21356 top = btm + descent + ascent;
21357 if (top > highest)
21358 highest = top;
21359 if (btm < lowest)
21360 lowest = btm;
21362 if (cmp->lbearing > left + lbearing)
21363 cmp->lbearing = left + lbearing;
21364 if (cmp->rbearing < left + rbearing)
21365 cmp->rbearing = left + rbearing;
21369 /* If there are glyphs whose x-offsets are negative,
21370 shift all glyphs to the right and make all x-offsets
21371 non-negative. */
21372 if (leftmost < 0)
21374 for (i = 0; i < cmp->glyph_len; i++)
21375 cmp->offsets[i * 2] -= leftmost;
21376 rightmost -= leftmost;
21377 cmp->lbearing -= leftmost;
21378 cmp->rbearing -= leftmost;
21381 if (left_padded && cmp->lbearing < 0)
21383 for (i = 0; i < cmp->glyph_len; i++)
21384 cmp->offsets[i * 2] -= cmp->lbearing;
21385 rightmost -= cmp->lbearing;
21386 cmp->rbearing -= cmp->lbearing;
21387 cmp->lbearing = 0;
21389 if (right_padded && rightmost < cmp->rbearing)
21391 rightmost = cmp->rbearing;
21394 cmp->pixel_width = rightmost;
21395 cmp->ascent = highest;
21396 cmp->descent = - lowest;
21397 if (cmp->ascent < font_ascent)
21398 cmp->ascent = font_ascent;
21399 if (cmp->descent < font_descent)
21400 cmp->descent = font_descent;
21403 if (it->glyph_row
21404 && (cmp->lbearing < 0
21405 || cmp->rbearing > cmp->pixel_width))
21406 it->glyph_row->contains_overlapping_glyphs_p = 1;
21408 it->pixel_width = cmp->pixel_width;
21409 it->ascent = it->phys_ascent = cmp->ascent;
21410 it->descent = it->phys_descent = cmp->descent;
21411 if (face->box != FACE_NO_BOX)
21413 int thick = face->box_line_width;
21415 if (thick > 0)
21417 it->ascent += thick;
21418 it->descent += thick;
21420 else
21421 thick = - thick;
21423 if (it->start_of_box_run_p)
21424 it->pixel_width += thick;
21425 if (it->end_of_box_run_p)
21426 it->pixel_width += thick;
21429 /* If face has an overline, add the height of the overline
21430 (1 pixel) and a 1 pixel margin to the character height. */
21431 if (face->overline_p)
21432 it->ascent += overline_margin;
21434 take_vertical_position_into_account (it);
21435 if (it->ascent < 0)
21436 it->ascent = 0;
21437 if (it->descent < 0)
21438 it->descent = 0;
21440 if (it->glyph_row)
21441 append_composite_glyph (it);
21443 else if (it->what == IT_IMAGE)
21444 produce_image_glyph (it);
21445 else if (it->what == IT_STRETCH)
21446 produce_stretch_glyph (it);
21448 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21449 because this isn't true for images with `:ascent 100'. */
21450 xassert (it->ascent >= 0 && it->descent >= 0);
21451 if (it->area == TEXT_AREA)
21452 it->current_x += it->pixel_width;
21454 if (extra_line_spacing > 0)
21456 it->descent += extra_line_spacing;
21457 if (extra_line_spacing > it->max_extra_line_spacing)
21458 it->max_extra_line_spacing = extra_line_spacing;
21461 it->max_ascent = max (it->max_ascent, it->ascent);
21462 it->max_descent = max (it->max_descent, it->descent);
21463 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21464 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21467 /* EXPORT for RIF:
21468 Output LEN glyphs starting at START at the nominal cursor position.
21469 Advance the nominal cursor over the text. The global variable
21470 updated_window contains the window being updated, updated_row is
21471 the glyph row being updated, and updated_area is the area of that
21472 row being updated. */
21474 void
21475 x_write_glyphs (start, len)
21476 struct glyph *start;
21477 int len;
21479 int x, hpos;
21481 xassert (updated_window && updated_row);
21482 BLOCK_INPUT;
21484 /* Write glyphs. */
21486 hpos = start - updated_row->glyphs[updated_area];
21487 x = draw_glyphs (updated_window, output_cursor.x,
21488 updated_row, updated_area,
21489 hpos, hpos + len,
21490 DRAW_NORMAL_TEXT, 0);
21492 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21493 if (updated_area == TEXT_AREA
21494 && updated_window->phys_cursor_on_p
21495 && updated_window->phys_cursor.vpos == output_cursor.vpos
21496 && updated_window->phys_cursor.hpos >= hpos
21497 && updated_window->phys_cursor.hpos < hpos + len)
21498 updated_window->phys_cursor_on_p = 0;
21500 UNBLOCK_INPUT;
21502 /* Advance the output cursor. */
21503 output_cursor.hpos += len;
21504 output_cursor.x = x;
21508 /* EXPORT for RIF:
21509 Insert LEN glyphs from START at the nominal cursor position. */
21511 void
21512 x_insert_glyphs (start, len)
21513 struct glyph *start;
21514 int len;
21516 struct frame *f;
21517 struct window *w;
21518 int line_height, shift_by_width, shifted_region_width;
21519 struct glyph_row *row;
21520 struct glyph *glyph;
21521 int frame_x, frame_y;
21522 EMACS_INT hpos;
21524 xassert (updated_window && updated_row);
21525 BLOCK_INPUT;
21526 w = updated_window;
21527 f = XFRAME (WINDOW_FRAME (w));
21529 /* Get the height of the line we are in. */
21530 row = updated_row;
21531 line_height = row->height;
21533 /* Get the width of the glyphs to insert. */
21534 shift_by_width = 0;
21535 for (glyph = start; glyph < start + len; ++glyph)
21536 shift_by_width += glyph->pixel_width;
21538 /* Get the width of the region to shift right. */
21539 shifted_region_width = (window_box_width (w, updated_area)
21540 - output_cursor.x
21541 - shift_by_width);
21543 /* Shift right. */
21544 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21545 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21547 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21548 line_height, shift_by_width);
21550 /* Write the glyphs. */
21551 hpos = start - row->glyphs[updated_area];
21552 draw_glyphs (w, output_cursor.x, row, updated_area,
21553 hpos, hpos + len,
21554 DRAW_NORMAL_TEXT, 0);
21556 /* Advance the output cursor. */
21557 output_cursor.hpos += len;
21558 output_cursor.x += shift_by_width;
21559 UNBLOCK_INPUT;
21563 /* EXPORT for RIF:
21564 Erase the current text line from the nominal cursor position
21565 (inclusive) to pixel column TO_X (exclusive). The idea is that
21566 everything from TO_X onward is already erased.
21568 TO_X is a pixel position relative to updated_area of
21569 updated_window. TO_X == -1 means clear to the end of this area. */
21571 void
21572 x_clear_end_of_line (to_x)
21573 int to_x;
21575 struct frame *f;
21576 struct window *w = updated_window;
21577 int max_x, min_y, max_y;
21578 int from_x, from_y, to_y;
21580 xassert (updated_window && updated_row);
21581 f = XFRAME (w->frame);
21583 if (updated_row->full_width_p)
21584 max_x = WINDOW_TOTAL_WIDTH (w);
21585 else
21586 max_x = window_box_width (w, updated_area);
21587 max_y = window_text_bottom_y (w);
21589 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21590 of window. For TO_X > 0, truncate to end of drawing area. */
21591 if (to_x == 0)
21592 return;
21593 else if (to_x < 0)
21594 to_x = max_x;
21595 else
21596 to_x = min (to_x, max_x);
21598 to_y = min (max_y, output_cursor.y + updated_row->height);
21600 /* Notice if the cursor will be cleared by this operation. */
21601 if (!updated_row->full_width_p)
21602 notice_overwritten_cursor (w, updated_area,
21603 output_cursor.x, -1,
21604 updated_row->y,
21605 MATRIX_ROW_BOTTOM_Y (updated_row));
21607 from_x = output_cursor.x;
21609 /* Translate to frame coordinates. */
21610 if (updated_row->full_width_p)
21612 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21613 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21615 else
21617 int area_left = window_box_left (w, updated_area);
21618 from_x += area_left;
21619 to_x += area_left;
21622 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21623 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21624 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21626 /* Prevent inadvertently clearing to end of the X window. */
21627 if (to_x > from_x && to_y > from_y)
21629 BLOCK_INPUT;
21630 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21631 to_x - from_x, to_y - from_y);
21632 UNBLOCK_INPUT;
21636 #endif /* HAVE_WINDOW_SYSTEM */
21640 /***********************************************************************
21641 Cursor types
21642 ***********************************************************************/
21644 /* Value is the internal representation of the specified cursor type
21645 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21646 of the bar cursor. */
21648 static enum text_cursor_kinds
21649 get_specified_cursor_type (arg, width)
21650 Lisp_Object arg;
21651 int *width;
21653 enum text_cursor_kinds type;
21655 if (NILP (arg))
21656 return NO_CURSOR;
21658 if (EQ (arg, Qbox))
21659 return FILLED_BOX_CURSOR;
21661 if (EQ (arg, Qhollow))
21662 return HOLLOW_BOX_CURSOR;
21664 if (EQ (arg, Qbar))
21666 *width = 2;
21667 return BAR_CURSOR;
21670 if (CONSP (arg)
21671 && EQ (XCAR (arg), Qbar)
21672 && INTEGERP (XCDR (arg))
21673 && XINT (XCDR (arg)) >= 0)
21675 *width = XINT (XCDR (arg));
21676 return BAR_CURSOR;
21679 if (EQ (arg, Qhbar))
21681 *width = 2;
21682 return HBAR_CURSOR;
21685 if (CONSP (arg)
21686 && EQ (XCAR (arg), Qhbar)
21687 && INTEGERP (XCDR (arg))
21688 && XINT (XCDR (arg)) >= 0)
21690 *width = XINT (XCDR (arg));
21691 return HBAR_CURSOR;
21694 /* Treat anything unknown as "hollow box cursor".
21695 It was bad to signal an error; people have trouble fixing
21696 .Xdefaults with Emacs, when it has something bad in it. */
21697 type = HOLLOW_BOX_CURSOR;
21699 return type;
21702 /* Set the default cursor types for specified frame. */
21703 void
21704 set_frame_cursor_types (f, arg)
21705 struct frame *f;
21706 Lisp_Object arg;
21708 int width;
21709 Lisp_Object tem;
21711 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21712 FRAME_CURSOR_WIDTH (f) = width;
21714 /* By default, set up the blink-off state depending on the on-state. */
21716 tem = Fassoc (arg, Vblink_cursor_alist);
21717 if (!NILP (tem))
21719 FRAME_BLINK_OFF_CURSOR (f)
21720 = get_specified_cursor_type (XCDR (tem), &width);
21721 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21723 else
21724 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21728 /* Return the cursor we want to be displayed in window W. Return
21729 width of bar/hbar cursor through WIDTH arg. Return with
21730 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21731 (i.e. if the `system caret' should track this cursor).
21733 In a mini-buffer window, we want the cursor only to appear if we
21734 are reading input from this window. For the selected window, we
21735 want the cursor type given by the frame parameter or buffer local
21736 setting of cursor-type. If explicitly marked off, draw no cursor.
21737 In all other cases, we want a hollow box cursor. */
21739 static enum text_cursor_kinds
21740 get_window_cursor_type (w, glyph, width, active_cursor)
21741 struct window *w;
21742 struct glyph *glyph;
21743 int *width;
21744 int *active_cursor;
21746 struct frame *f = XFRAME (w->frame);
21747 struct buffer *b = XBUFFER (w->buffer);
21748 int cursor_type = DEFAULT_CURSOR;
21749 Lisp_Object alt_cursor;
21750 int non_selected = 0;
21752 *active_cursor = 1;
21754 /* Echo area */
21755 if (cursor_in_echo_area
21756 && FRAME_HAS_MINIBUF_P (f)
21757 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21759 if (w == XWINDOW (echo_area_window))
21761 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21763 *width = FRAME_CURSOR_WIDTH (f);
21764 return FRAME_DESIRED_CURSOR (f);
21766 else
21767 return get_specified_cursor_type (b->cursor_type, width);
21770 *active_cursor = 0;
21771 non_selected = 1;
21774 /* Detect a nonselected window or nonselected frame. */
21775 else if (w != XWINDOW (f->selected_window)
21776 #ifdef HAVE_WINDOW_SYSTEM
21777 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21778 #endif
21781 *active_cursor = 0;
21783 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21784 return NO_CURSOR;
21786 non_selected = 1;
21789 /* Never display a cursor in a window in which cursor-type is nil. */
21790 if (NILP (b->cursor_type))
21791 return NO_CURSOR;
21793 /* Get the normal cursor type for this window. */
21794 if (EQ (b->cursor_type, Qt))
21796 cursor_type = FRAME_DESIRED_CURSOR (f);
21797 *width = FRAME_CURSOR_WIDTH (f);
21799 else
21800 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21802 /* Use cursor-in-non-selected-windows instead
21803 for non-selected window or frame. */
21804 if (non_selected)
21806 alt_cursor = b->cursor_in_non_selected_windows;
21807 if (!EQ (Qt, alt_cursor))
21808 return get_specified_cursor_type (alt_cursor, width);
21809 /* t means modify the normal cursor type. */
21810 if (cursor_type == FILLED_BOX_CURSOR)
21811 cursor_type = HOLLOW_BOX_CURSOR;
21812 else if (cursor_type == BAR_CURSOR && *width > 1)
21813 --*width;
21814 return cursor_type;
21817 /* Use normal cursor if not blinked off. */
21818 if (!w->cursor_off_p)
21820 #ifdef HAVE_WINDOW_SYSTEM
21821 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21823 if (cursor_type == FILLED_BOX_CURSOR)
21825 /* Using a block cursor on large images can be very annoying.
21826 So use a hollow cursor for "large" images.
21827 If image is not transparent (no mask), also use hollow cursor. */
21828 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21829 if (img != NULL && IMAGEP (img->spec))
21831 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21832 where N = size of default frame font size.
21833 This should cover most of the "tiny" icons people may use. */
21834 if (!img->mask
21835 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21836 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21837 cursor_type = HOLLOW_BOX_CURSOR;
21840 else if (cursor_type != NO_CURSOR)
21842 /* Display current only supports BOX and HOLLOW cursors for images.
21843 So for now, unconditionally use a HOLLOW cursor when cursor is
21844 not a solid box cursor. */
21845 cursor_type = HOLLOW_BOX_CURSOR;
21848 #endif
21849 return cursor_type;
21852 /* Cursor is blinked off, so determine how to "toggle" it. */
21854 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21855 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21856 return get_specified_cursor_type (XCDR (alt_cursor), width);
21858 /* Then see if frame has specified a specific blink off cursor type. */
21859 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21861 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21862 return FRAME_BLINK_OFF_CURSOR (f);
21865 #if 0
21866 /* Some people liked having a permanently visible blinking cursor,
21867 while others had very strong opinions against it. So it was
21868 decided to remove it. KFS 2003-09-03 */
21870 /* Finally perform built-in cursor blinking:
21871 filled box <-> hollow box
21872 wide [h]bar <-> narrow [h]bar
21873 narrow [h]bar <-> no cursor
21874 other type <-> no cursor */
21876 if (cursor_type == FILLED_BOX_CURSOR)
21877 return HOLLOW_BOX_CURSOR;
21879 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21881 *width = 1;
21882 return cursor_type;
21884 #endif
21886 return NO_CURSOR;
21890 #ifdef HAVE_WINDOW_SYSTEM
21892 /* Notice when the text cursor of window W has been completely
21893 overwritten by a drawing operation that outputs glyphs in AREA
21894 starting at X0 and ending at X1 in the line starting at Y0 and
21895 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21896 the rest of the line after X0 has been written. Y coordinates
21897 are window-relative. */
21899 static void
21900 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21901 struct window *w;
21902 enum glyph_row_area area;
21903 int x0, y0, x1, y1;
21905 int cx0, cx1, cy0, cy1;
21906 struct glyph_row *row;
21908 if (!w->phys_cursor_on_p)
21909 return;
21910 if (area != TEXT_AREA)
21911 return;
21913 if (w->phys_cursor.vpos < 0
21914 || w->phys_cursor.vpos >= w->current_matrix->nrows
21915 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21916 !(row->enabled_p && row->displays_text_p)))
21917 return;
21919 if (row->cursor_in_fringe_p)
21921 row->cursor_in_fringe_p = 0;
21922 draw_fringe_bitmap (w, row, 0);
21923 w->phys_cursor_on_p = 0;
21924 return;
21927 cx0 = w->phys_cursor.x;
21928 cx1 = cx0 + w->phys_cursor_width;
21929 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21930 return;
21932 /* The cursor image will be completely removed from the
21933 screen if the output area intersects the cursor area in
21934 y-direction. When we draw in [y0 y1[, and some part of
21935 the cursor is at y < y0, that part must have been drawn
21936 before. When scrolling, the cursor is erased before
21937 actually scrolling, so we don't come here. When not
21938 scrolling, the rows above the old cursor row must have
21939 changed, and in this case these rows must have written
21940 over the cursor image.
21942 Likewise if part of the cursor is below y1, with the
21943 exception of the cursor being in the first blank row at
21944 the buffer and window end because update_text_area
21945 doesn't draw that row. (Except when it does, but
21946 that's handled in update_text_area.) */
21948 cy0 = w->phys_cursor.y;
21949 cy1 = cy0 + w->phys_cursor_height;
21950 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21951 return;
21953 w->phys_cursor_on_p = 0;
21956 #endif /* HAVE_WINDOW_SYSTEM */
21959 /************************************************************************
21960 Mouse Face
21961 ************************************************************************/
21963 #ifdef HAVE_WINDOW_SYSTEM
21965 /* EXPORT for RIF:
21966 Fix the display of area AREA of overlapping row ROW in window W
21967 with respect to the overlapping part OVERLAPS. */
21969 void
21970 x_fix_overlapping_area (w, row, area, overlaps)
21971 struct window *w;
21972 struct glyph_row *row;
21973 enum glyph_row_area area;
21974 int overlaps;
21976 int i, x;
21978 BLOCK_INPUT;
21980 x = 0;
21981 for (i = 0; i < row->used[area];)
21983 if (row->glyphs[area][i].overlaps_vertically_p)
21985 int start = i, start_x = x;
21989 x += row->glyphs[area][i].pixel_width;
21990 ++i;
21992 while (i < row->used[area]
21993 && row->glyphs[area][i].overlaps_vertically_p);
21995 draw_glyphs (w, start_x, row, area,
21996 start, i,
21997 DRAW_NORMAL_TEXT, overlaps);
21999 else
22001 x += row->glyphs[area][i].pixel_width;
22002 ++i;
22006 UNBLOCK_INPUT;
22010 /* EXPORT:
22011 Draw the cursor glyph of window W in glyph row ROW. See the
22012 comment of draw_glyphs for the meaning of HL. */
22014 void
22015 draw_phys_cursor_glyph (w, row, hl)
22016 struct window *w;
22017 struct glyph_row *row;
22018 enum draw_glyphs_face hl;
22020 /* If cursor hpos is out of bounds, don't draw garbage. This can
22021 happen in mini-buffer windows when switching between echo area
22022 glyphs and mini-buffer. */
22023 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22025 int on_p = w->phys_cursor_on_p;
22026 int x1;
22027 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22028 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22029 hl, 0);
22030 w->phys_cursor_on_p = on_p;
22032 if (hl == DRAW_CURSOR)
22033 w->phys_cursor_width = x1 - w->phys_cursor.x;
22034 /* When we erase the cursor, and ROW is overlapped by other
22035 rows, make sure that these overlapping parts of other rows
22036 are redrawn. */
22037 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22039 w->phys_cursor_width = x1 - w->phys_cursor.x;
22041 if (row > w->current_matrix->rows
22042 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22043 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22044 OVERLAPS_ERASED_CURSOR);
22046 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22047 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22048 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22049 OVERLAPS_ERASED_CURSOR);
22055 /* EXPORT:
22056 Erase the image of a cursor of window W from the screen. */
22058 void
22059 erase_phys_cursor (w)
22060 struct window *w;
22062 struct frame *f = XFRAME (w->frame);
22063 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22064 int hpos = w->phys_cursor.hpos;
22065 int vpos = w->phys_cursor.vpos;
22066 int mouse_face_here_p = 0;
22067 struct glyph_matrix *active_glyphs = w->current_matrix;
22068 struct glyph_row *cursor_row;
22069 struct glyph *cursor_glyph;
22070 enum draw_glyphs_face hl;
22072 /* No cursor displayed or row invalidated => nothing to do on the
22073 screen. */
22074 if (w->phys_cursor_type == NO_CURSOR)
22075 goto mark_cursor_off;
22077 /* VPOS >= active_glyphs->nrows means that window has been resized.
22078 Don't bother to erase the cursor. */
22079 if (vpos >= active_glyphs->nrows)
22080 goto mark_cursor_off;
22082 /* If row containing cursor is marked invalid, there is nothing we
22083 can do. */
22084 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22085 if (!cursor_row->enabled_p)
22086 goto mark_cursor_off;
22088 /* If line spacing is > 0, old cursor may only be partially visible in
22089 window after split-window. So adjust visible height. */
22090 cursor_row->visible_height = min (cursor_row->visible_height,
22091 window_text_bottom_y (w) - cursor_row->y);
22093 /* If row is completely invisible, don't attempt to delete a cursor which
22094 isn't there. This can happen if cursor is at top of a window, and
22095 we switch to a buffer with a header line in that window. */
22096 if (cursor_row->visible_height <= 0)
22097 goto mark_cursor_off;
22099 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22100 if (cursor_row->cursor_in_fringe_p)
22102 cursor_row->cursor_in_fringe_p = 0;
22103 draw_fringe_bitmap (w, cursor_row, 0);
22104 goto mark_cursor_off;
22107 /* This can happen when the new row is shorter than the old one.
22108 In this case, either draw_glyphs or clear_end_of_line
22109 should have cleared the cursor. Note that we wouldn't be
22110 able to erase the cursor in this case because we don't have a
22111 cursor glyph at hand. */
22112 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22113 goto mark_cursor_off;
22115 /* If the cursor is in the mouse face area, redisplay that when
22116 we clear the cursor. */
22117 if (! NILP (dpyinfo->mouse_face_window)
22118 && w == XWINDOW (dpyinfo->mouse_face_window)
22119 && (vpos > dpyinfo->mouse_face_beg_row
22120 || (vpos == dpyinfo->mouse_face_beg_row
22121 && hpos >= dpyinfo->mouse_face_beg_col))
22122 && (vpos < dpyinfo->mouse_face_end_row
22123 || (vpos == dpyinfo->mouse_face_end_row
22124 && hpos < dpyinfo->mouse_face_end_col))
22125 /* Don't redraw the cursor's spot in mouse face if it is at the
22126 end of a line (on a newline). The cursor appears there, but
22127 mouse highlighting does not. */
22128 && cursor_row->used[TEXT_AREA] > hpos)
22129 mouse_face_here_p = 1;
22131 /* Maybe clear the display under the cursor. */
22132 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22134 int x, y, left_x;
22135 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22136 int width;
22138 cursor_glyph = get_phys_cursor_glyph (w);
22139 if (cursor_glyph == NULL)
22140 goto mark_cursor_off;
22142 width = cursor_glyph->pixel_width;
22143 left_x = window_box_left_offset (w, TEXT_AREA);
22144 x = w->phys_cursor.x;
22145 if (x < left_x)
22146 width -= left_x - x;
22147 width = min (width, window_box_width (w, TEXT_AREA) - x);
22148 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22149 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22151 if (width > 0)
22152 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22155 /* Erase the cursor by redrawing the character underneath it. */
22156 if (mouse_face_here_p)
22157 hl = DRAW_MOUSE_FACE;
22158 else
22159 hl = DRAW_NORMAL_TEXT;
22160 draw_phys_cursor_glyph (w, cursor_row, hl);
22162 mark_cursor_off:
22163 w->phys_cursor_on_p = 0;
22164 w->phys_cursor_type = NO_CURSOR;
22168 /* EXPORT:
22169 Display or clear cursor of window W. If ON is zero, clear the
22170 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22171 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22173 void
22174 display_and_set_cursor (w, on, hpos, vpos, x, y)
22175 struct window *w;
22176 int on, hpos, vpos, x, y;
22178 struct frame *f = XFRAME (w->frame);
22179 int new_cursor_type;
22180 int new_cursor_width;
22181 int active_cursor;
22182 struct glyph_row *glyph_row;
22183 struct glyph *glyph;
22185 /* This is pointless on invisible frames, and dangerous on garbaged
22186 windows and frames; in the latter case, the frame or window may
22187 be in the midst of changing its size, and x and y may be off the
22188 window. */
22189 if (! FRAME_VISIBLE_P (f)
22190 || FRAME_GARBAGED_P (f)
22191 || vpos >= w->current_matrix->nrows
22192 || hpos >= w->current_matrix->matrix_w)
22193 return;
22195 /* If cursor is off and we want it off, return quickly. */
22196 if (!on && !w->phys_cursor_on_p)
22197 return;
22199 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22200 /* If cursor row is not enabled, we don't really know where to
22201 display the cursor. */
22202 if (!glyph_row->enabled_p)
22204 w->phys_cursor_on_p = 0;
22205 return;
22208 glyph = NULL;
22209 if (!glyph_row->exact_window_width_line_p
22210 || hpos < glyph_row->used[TEXT_AREA])
22211 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22213 xassert (interrupt_input_blocked);
22215 /* Set new_cursor_type to the cursor we want to be displayed. */
22216 new_cursor_type = get_window_cursor_type (w, glyph,
22217 &new_cursor_width, &active_cursor);
22219 /* If cursor is currently being shown and we don't want it to be or
22220 it is in the wrong place, or the cursor type is not what we want,
22221 erase it. */
22222 if (w->phys_cursor_on_p
22223 && (!on
22224 || w->phys_cursor.x != x
22225 || w->phys_cursor.y != y
22226 || new_cursor_type != w->phys_cursor_type
22227 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22228 && new_cursor_width != w->phys_cursor_width)))
22229 erase_phys_cursor (w);
22231 /* Don't check phys_cursor_on_p here because that flag is only set
22232 to zero in some cases where we know that the cursor has been
22233 completely erased, to avoid the extra work of erasing the cursor
22234 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22235 still not be visible, or it has only been partly erased. */
22236 if (on)
22238 w->phys_cursor_ascent = glyph_row->ascent;
22239 w->phys_cursor_height = glyph_row->height;
22241 /* Set phys_cursor_.* before x_draw_.* is called because some
22242 of them may need the information. */
22243 w->phys_cursor.x = x;
22244 w->phys_cursor.y = glyph_row->y;
22245 w->phys_cursor.hpos = hpos;
22246 w->phys_cursor.vpos = vpos;
22249 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22250 new_cursor_type, new_cursor_width,
22251 on, active_cursor);
22255 /* Switch the display of W's cursor on or off, according to the value
22256 of ON. */
22258 static void
22259 update_window_cursor (w, on)
22260 struct window *w;
22261 int on;
22263 /* Don't update cursor in windows whose frame is in the process
22264 of being deleted. */
22265 if (w->current_matrix)
22267 BLOCK_INPUT;
22268 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22269 w->phys_cursor.x, w->phys_cursor.y);
22270 UNBLOCK_INPUT;
22275 /* Call update_window_cursor with parameter ON_P on all leaf windows
22276 in the window tree rooted at W. */
22278 static void
22279 update_cursor_in_window_tree (w, on_p)
22280 struct window *w;
22281 int on_p;
22283 while (w)
22285 if (!NILP (w->hchild))
22286 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22287 else if (!NILP (w->vchild))
22288 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22289 else
22290 update_window_cursor (w, on_p);
22292 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22297 /* EXPORT:
22298 Display the cursor on window W, or clear it, according to ON_P.
22299 Don't change the cursor's position. */
22301 void
22302 x_update_cursor (f, on_p)
22303 struct frame *f;
22304 int on_p;
22306 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22310 /* EXPORT:
22311 Clear the cursor of window W to background color, and mark the
22312 cursor as not shown. This is used when the text where the cursor
22313 is is about to be rewritten. */
22315 void
22316 x_clear_cursor (w)
22317 struct window *w;
22319 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22320 update_window_cursor (w, 0);
22324 /* EXPORT:
22325 Display the active region described by mouse_face_* according to DRAW. */
22327 void
22328 show_mouse_face (dpyinfo, draw)
22329 Display_Info *dpyinfo;
22330 enum draw_glyphs_face draw;
22332 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22333 struct frame *f = XFRAME (WINDOW_FRAME (w));
22335 if (/* If window is in the process of being destroyed, don't bother
22336 to do anything. */
22337 w->current_matrix != NULL
22338 /* Don't update mouse highlight if hidden */
22339 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22340 /* Recognize when we are called to operate on rows that don't exist
22341 anymore. This can happen when a window is split. */
22342 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22344 int phys_cursor_on_p = w->phys_cursor_on_p;
22345 struct glyph_row *row, *first, *last;
22347 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22348 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22350 for (row = first; row <= last && row->enabled_p; ++row)
22352 int start_hpos, end_hpos, start_x;
22354 /* For all but the first row, the highlight starts at column 0. */
22355 if (row == first)
22357 start_hpos = dpyinfo->mouse_face_beg_col;
22358 start_x = dpyinfo->mouse_face_beg_x;
22360 else
22362 start_hpos = 0;
22363 start_x = 0;
22366 if (row == last)
22367 end_hpos = dpyinfo->mouse_face_end_col;
22368 else
22370 end_hpos = row->used[TEXT_AREA];
22371 if (draw == DRAW_NORMAL_TEXT)
22372 row->fill_line_p = 1; /* Clear to end of line */
22375 if (end_hpos > start_hpos)
22377 draw_glyphs (w, start_x, row, TEXT_AREA,
22378 start_hpos, end_hpos,
22379 draw, 0);
22381 row->mouse_face_p
22382 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22386 /* When we've written over the cursor, arrange for it to
22387 be displayed again. */
22388 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22390 BLOCK_INPUT;
22391 display_and_set_cursor (w, 1,
22392 w->phys_cursor.hpos, w->phys_cursor.vpos,
22393 w->phys_cursor.x, w->phys_cursor.y);
22394 UNBLOCK_INPUT;
22398 /* Change the mouse cursor. */
22399 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22400 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22401 else if (draw == DRAW_MOUSE_FACE)
22402 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22403 else
22404 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22407 /* EXPORT:
22408 Clear out the mouse-highlighted active region.
22409 Redraw it un-highlighted first. Value is non-zero if mouse
22410 face was actually drawn unhighlighted. */
22413 clear_mouse_face (dpyinfo)
22414 Display_Info *dpyinfo;
22416 int cleared = 0;
22418 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22420 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22421 cleared = 1;
22424 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22425 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22426 dpyinfo->mouse_face_window = Qnil;
22427 dpyinfo->mouse_face_overlay = Qnil;
22428 return cleared;
22432 /* EXPORT:
22433 Non-zero if physical cursor of window W is within mouse face. */
22436 cursor_in_mouse_face_p (w)
22437 struct window *w;
22439 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22440 int in_mouse_face = 0;
22442 if (WINDOWP (dpyinfo->mouse_face_window)
22443 && XWINDOW (dpyinfo->mouse_face_window) == w)
22445 int hpos = w->phys_cursor.hpos;
22446 int vpos = w->phys_cursor.vpos;
22448 if (vpos >= dpyinfo->mouse_face_beg_row
22449 && vpos <= dpyinfo->mouse_face_end_row
22450 && (vpos > dpyinfo->mouse_face_beg_row
22451 || hpos >= dpyinfo->mouse_face_beg_col)
22452 && (vpos < dpyinfo->mouse_face_end_row
22453 || hpos < dpyinfo->mouse_face_end_col
22454 || dpyinfo->mouse_face_past_end))
22455 in_mouse_face = 1;
22458 return in_mouse_face;
22464 /* Find the glyph matrix position of buffer position CHARPOS in window
22465 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22466 current glyphs must be up to date. If CHARPOS is above window
22467 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22468 of last line in W. In the row containing CHARPOS, stop before glyphs
22469 having STOP as object. */
22471 #if 1 /* This is a version of fast_find_position that's more correct
22472 in the presence of hscrolling, for example. I didn't install
22473 it right away because the problem fixed is minor, it failed
22474 in 20.x as well, and I think it's too risky to install
22475 so near the release of 21.1. 2001-09-25 gerd. */
22477 static int
22478 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22479 struct window *w;
22480 EMACS_INT charpos;
22481 int *hpos, *vpos, *x, *y;
22482 Lisp_Object stop;
22484 struct glyph_row *row, *first;
22485 struct glyph *glyph, *end;
22486 int past_end = 0;
22488 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22489 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22491 *x = first->x;
22492 *y = first->y;
22493 *hpos = 0;
22494 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22495 return 1;
22498 row = row_containing_pos (w, charpos, first, NULL, 0);
22499 if (row == NULL)
22501 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22502 past_end = 1;
22505 /* If whole rows or last part of a row came from a display overlay,
22506 row_containing_pos will skip over such rows because their end pos
22507 equals the start pos of the overlay or interval.
22509 Move back if we have a STOP object and previous row's
22510 end glyph came from STOP. */
22511 if (!NILP (stop))
22513 struct glyph_row *prev;
22514 while ((prev = row - 1, prev >= first)
22515 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22516 && prev->used[TEXT_AREA] > 0)
22518 struct glyph *beg = prev->glyphs[TEXT_AREA];
22519 glyph = beg + prev->used[TEXT_AREA];
22520 while (--glyph >= beg
22521 && INTEGERP (glyph->object));
22522 if (glyph < beg
22523 || !EQ (stop, glyph->object))
22524 break;
22525 row = prev;
22529 *x = row->x;
22530 *y = row->y;
22531 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22533 glyph = row->glyphs[TEXT_AREA];
22534 end = glyph + row->used[TEXT_AREA];
22536 /* Skip over glyphs not having an object at the start of the row.
22537 These are special glyphs like truncation marks on terminal
22538 frames. */
22539 if (row->displays_text_p)
22540 while (glyph < end
22541 && INTEGERP (glyph->object)
22542 && !EQ (stop, glyph->object)
22543 && glyph->charpos < 0)
22545 *x += glyph->pixel_width;
22546 ++glyph;
22549 while (glyph < end
22550 && !INTEGERP (glyph->object)
22551 && !EQ (stop, glyph->object)
22552 && (!BUFFERP (glyph->object)
22553 || glyph->charpos < charpos))
22555 *x += glyph->pixel_width;
22556 ++glyph;
22559 *hpos = glyph - row->glyphs[TEXT_AREA];
22560 return !past_end;
22563 #else /* not 1 */
22565 static int
22566 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22567 struct window *w;
22568 EMACS_INT pos;
22569 int *hpos, *vpos, *x, *y;
22570 Lisp_Object stop;
22572 int i;
22573 int lastcol;
22574 int maybe_next_line_p = 0;
22575 int line_start_position;
22576 int yb = window_text_bottom_y (w);
22577 struct glyph_row *row, *best_row;
22578 int row_vpos, best_row_vpos;
22579 int current_x;
22581 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22582 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22584 while (row->y < yb)
22586 if (row->used[TEXT_AREA])
22587 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22588 else
22589 line_start_position = 0;
22591 if (line_start_position > pos)
22592 break;
22593 /* If the position sought is the end of the buffer,
22594 don't include the blank lines at the bottom of the window. */
22595 else if (line_start_position == pos
22596 && pos == BUF_ZV (XBUFFER (w->buffer)))
22598 maybe_next_line_p = 1;
22599 break;
22601 else if (line_start_position > 0)
22603 best_row = row;
22604 best_row_vpos = row_vpos;
22607 if (row->y + row->height >= yb)
22608 break;
22610 ++row;
22611 ++row_vpos;
22614 /* Find the right column within BEST_ROW. */
22615 lastcol = 0;
22616 current_x = best_row->x;
22617 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22619 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22620 int charpos = glyph->charpos;
22622 if (BUFFERP (glyph->object))
22624 if (charpos == pos)
22626 *hpos = i;
22627 *vpos = best_row_vpos;
22628 *x = current_x;
22629 *y = best_row->y;
22630 return 1;
22632 else if (charpos > pos)
22633 break;
22635 else if (EQ (glyph->object, stop))
22636 break;
22638 if (charpos > 0)
22639 lastcol = i;
22640 current_x += glyph->pixel_width;
22643 /* If we're looking for the end of the buffer,
22644 and we didn't find it in the line we scanned,
22645 use the start of the following line. */
22646 if (maybe_next_line_p)
22648 ++best_row;
22649 ++best_row_vpos;
22650 lastcol = 0;
22651 current_x = best_row->x;
22654 *vpos = best_row_vpos;
22655 *hpos = lastcol + 1;
22656 *x = current_x;
22657 *y = best_row->y;
22658 return 0;
22661 #endif /* not 1 */
22664 /* Find the position of the glyph for position POS in OBJECT in
22665 window W's current matrix, and return in *X, *Y the pixel
22666 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22668 RIGHT_P non-zero means return the position of the right edge of the
22669 glyph, RIGHT_P zero means return the left edge position.
22671 If no glyph for POS exists in the matrix, return the position of
22672 the glyph with the next smaller position that is in the matrix, if
22673 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22674 exists in the matrix, return the position of the glyph with the
22675 next larger position in OBJECT.
22677 Value is non-zero if a glyph was found. */
22679 static int
22680 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22681 struct window *w;
22682 EMACS_INT pos;
22683 Lisp_Object object;
22684 int *hpos, *vpos, *x, *y;
22685 int right_p;
22687 int yb = window_text_bottom_y (w);
22688 struct glyph_row *r;
22689 struct glyph *best_glyph = NULL;
22690 struct glyph_row *best_row = NULL;
22691 int best_x = 0;
22693 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22694 r->enabled_p && r->y < yb;
22695 ++r)
22697 struct glyph *g = r->glyphs[TEXT_AREA];
22698 struct glyph *e = g + r->used[TEXT_AREA];
22699 int gx;
22701 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22702 if (EQ (g->object, object))
22704 if (g->charpos == pos)
22706 best_glyph = g;
22707 best_x = gx;
22708 best_row = r;
22709 goto found;
22711 else if (best_glyph == NULL
22712 || ((eabs (g->charpos - pos)
22713 < eabs (best_glyph->charpos - pos))
22714 && (right_p
22715 ? g->charpos < pos
22716 : g->charpos > pos)))
22718 best_glyph = g;
22719 best_x = gx;
22720 best_row = r;
22725 found:
22727 if (best_glyph)
22729 *x = best_x;
22730 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22732 if (right_p)
22734 *x += best_glyph->pixel_width;
22735 ++*hpos;
22738 *y = best_row->y;
22739 *vpos = best_row - w->current_matrix->rows;
22742 return best_glyph != NULL;
22746 /* See if position X, Y is within a hot-spot of an image. */
22748 static int
22749 on_hot_spot_p (hot_spot, x, y)
22750 Lisp_Object hot_spot;
22751 int x, y;
22753 if (!CONSP (hot_spot))
22754 return 0;
22756 if (EQ (XCAR (hot_spot), Qrect))
22758 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22759 Lisp_Object rect = XCDR (hot_spot);
22760 Lisp_Object tem;
22761 if (!CONSP (rect))
22762 return 0;
22763 if (!CONSP (XCAR (rect)))
22764 return 0;
22765 if (!CONSP (XCDR (rect)))
22766 return 0;
22767 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22768 return 0;
22769 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22770 return 0;
22771 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22772 return 0;
22773 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22774 return 0;
22775 return 1;
22777 else if (EQ (XCAR (hot_spot), Qcircle))
22779 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22780 Lisp_Object circ = XCDR (hot_spot);
22781 Lisp_Object lr, lx0, ly0;
22782 if (CONSP (circ)
22783 && CONSP (XCAR (circ))
22784 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22785 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22786 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22788 double r = XFLOATINT (lr);
22789 double dx = XINT (lx0) - x;
22790 double dy = XINT (ly0) - y;
22791 return (dx * dx + dy * dy <= r * r);
22794 else if (EQ (XCAR (hot_spot), Qpoly))
22796 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22797 if (VECTORP (XCDR (hot_spot)))
22799 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22800 Lisp_Object *poly = v->contents;
22801 int n = v->size;
22802 int i;
22803 int inside = 0;
22804 Lisp_Object lx, ly;
22805 int x0, y0;
22807 /* Need an even number of coordinates, and at least 3 edges. */
22808 if (n < 6 || n & 1)
22809 return 0;
22811 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22812 If count is odd, we are inside polygon. Pixels on edges
22813 may or may not be included depending on actual geometry of the
22814 polygon. */
22815 if ((lx = poly[n-2], !INTEGERP (lx))
22816 || (ly = poly[n-1], !INTEGERP (lx)))
22817 return 0;
22818 x0 = XINT (lx), y0 = XINT (ly);
22819 for (i = 0; i < n; i += 2)
22821 int x1 = x0, y1 = y0;
22822 if ((lx = poly[i], !INTEGERP (lx))
22823 || (ly = poly[i+1], !INTEGERP (ly)))
22824 return 0;
22825 x0 = XINT (lx), y0 = XINT (ly);
22827 /* Does this segment cross the X line? */
22828 if (x0 >= x)
22830 if (x1 >= x)
22831 continue;
22833 else if (x1 < x)
22834 continue;
22835 if (y > y0 && y > y1)
22836 continue;
22837 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22838 inside = !inside;
22840 return inside;
22843 return 0;
22846 Lisp_Object
22847 find_hot_spot (map, x, y)
22848 Lisp_Object map;
22849 int x, y;
22851 while (CONSP (map))
22853 if (CONSP (XCAR (map))
22854 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22855 return XCAR (map);
22856 map = XCDR (map);
22859 return Qnil;
22862 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22863 3, 3, 0,
22864 doc: /* Lookup in image map MAP coordinates X and Y.
22865 An image map is an alist where each element has the format (AREA ID PLIST).
22866 An AREA is specified as either a rectangle, a circle, or a polygon:
22867 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22868 pixel coordinates of the upper left and bottom right corners.
22869 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22870 and the radius of the circle; r may be a float or integer.
22871 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22872 vector describes one corner in the polygon.
22873 Returns the alist element for the first matching AREA in MAP. */)
22874 (map, x, y)
22875 Lisp_Object map;
22876 Lisp_Object x, y;
22878 if (NILP (map))
22879 return Qnil;
22881 CHECK_NUMBER (x);
22882 CHECK_NUMBER (y);
22884 return find_hot_spot (map, XINT (x), XINT (y));
22888 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22889 static void
22890 define_frame_cursor1 (f, cursor, pointer)
22891 struct frame *f;
22892 Cursor cursor;
22893 Lisp_Object pointer;
22895 /* Do not change cursor shape while dragging mouse. */
22896 if (!NILP (do_mouse_tracking))
22897 return;
22899 if (!NILP (pointer))
22901 if (EQ (pointer, Qarrow))
22902 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22903 else if (EQ (pointer, Qhand))
22904 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22905 else if (EQ (pointer, Qtext))
22906 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22907 else if (EQ (pointer, intern ("hdrag")))
22908 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22909 #ifdef HAVE_X_WINDOWS
22910 else if (EQ (pointer, intern ("vdrag")))
22911 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22912 #endif
22913 else if (EQ (pointer, intern ("hourglass")))
22914 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22915 else if (EQ (pointer, Qmodeline))
22916 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22917 else
22918 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22921 if (cursor != No_Cursor)
22922 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22925 /* Take proper action when mouse has moved to the mode or header line
22926 or marginal area AREA of window W, x-position X and y-position Y.
22927 X is relative to the start of the text display area of W, so the
22928 width of bitmap areas and scroll bars must be subtracted to get a
22929 position relative to the start of the mode line. */
22931 static void
22932 note_mode_line_or_margin_highlight (window, x, y, area)
22933 Lisp_Object window;
22934 int x, y;
22935 enum window_part area;
22937 struct window *w = XWINDOW (window);
22938 struct frame *f = XFRAME (w->frame);
22939 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22940 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22941 Lisp_Object pointer = Qnil;
22942 int charpos, dx, dy, width, height;
22943 Lisp_Object string, object = Qnil;
22944 Lisp_Object pos, help;
22946 Lisp_Object mouse_face;
22947 int original_x_pixel = x;
22948 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22949 struct glyph_row *row;
22951 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22953 int x0;
22954 struct glyph *end;
22956 string = mode_line_string (w, area, &x, &y, &charpos,
22957 &object, &dx, &dy, &width, &height);
22959 row = (area == ON_MODE_LINE
22960 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22961 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22963 /* Find glyph */
22964 if (row->mode_line_p && row->enabled_p)
22966 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22967 end = glyph + row->used[TEXT_AREA];
22969 for (x0 = original_x_pixel;
22970 glyph < end && x0 >= glyph->pixel_width;
22971 ++glyph)
22972 x0 -= glyph->pixel_width;
22974 if (glyph >= end)
22975 glyph = NULL;
22978 else
22980 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22981 string = marginal_area_string (w, area, &x, &y, &charpos,
22982 &object, &dx, &dy, &width, &height);
22985 help = Qnil;
22987 if (IMAGEP (object))
22989 Lisp_Object image_map, hotspot;
22990 if ((image_map = Fplist_get (XCDR (object), QCmap),
22991 !NILP (image_map))
22992 && (hotspot = find_hot_spot (image_map, dx, dy),
22993 CONSP (hotspot))
22994 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22996 Lisp_Object area_id, plist;
22998 area_id = XCAR (hotspot);
22999 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23000 If so, we could look for mouse-enter, mouse-leave
23001 properties in PLIST (and do something...). */
23002 hotspot = XCDR (hotspot);
23003 if (CONSP (hotspot)
23004 && (plist = XCAR (hotspot), CONSP (plist)))
23006 pointer = Fplist_get (plist, Qpointer);
23007 if (NILP (pointer))
23008 pointer = Qhand;
23009 help = Fplist_get (plist, Qhelp_echo);
23010 if (!NILP (help))
23012 help_echo_string = help;
23013 /* Is this correct? ++kfs */
23014 XSETWINDOW (help_echo_window, w);
23015 help_echo_object = w->buffer;
23016 help_echo_pos = charpos;
23020 if (NILP (pointer))
23021 pointer = Fplist_get (XCDR (object), QCpointer);
23024 if (STRINGP (string))
23026 pos = make_number (charpos);
23027 /* If we're on a string with `help-echo' text property, arrange
23028 for the help to be displayed. This is done by setting the
23029 global variable help_echo_string to the help string. */
23030 if (NILP (help))
23032 help = Fget_text_property (pos, Qhelp_echo, string);
23033 if (!NILP (help))
23035 help_echo_string = help;
23036 XSETWINDOW (help_echo_window, w);
23037 help_echo_object = string;
23038 help_echo_pos = charpos;
23042 if (NILP (pointer))
23043 pointer = Fget_text_property (pos, Qpointer, string);
23045 /* Change the mouse pointer according to what is under X/Y. */
23046 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23048 Lisp_Object map;
23049 map = Fget_text_property (pos, Qlocal_map, string);
23050 if (!KEYMAPP (map))
23051 map = Fget_text_property (pos, Qkeymap, string);
23052 if (!KEYMAPP (map))
23053 cursor = dpyinfo->vertical_scroll_bar_cursor;
23056 /* Change the mouse face according to what is under X/Y. */
23057 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23058 if (!NILP (mouse_face)
23059 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23060 && glyph)
23062 Lisp_Object b, e;
23064 struct glyph * tmp_glyph;
23066 int gpos;
23067 int gseq_length;
23068 int total_pixel_width;
23069 int ignore;
23071 int vpos, hpos;
23073 b = Fprevious_single_property_change (make_number (charpos + 1),
23074 Qmouse_face, string, Qnil);
23075 if (NILP (b))
23076 b = make_number (0);
23078 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23079 if (NILP (e))
23080 e = make_number (SCHARS (string));
23082 /* Calculate the position(glyph position: GPOS) of GLYPH in
23083 displayed string. GPOS is different from CHARPOS.
23085 CHARPOS is the position of glyph in internal string
23086 object. A mode line string format has structures which
23087 is converted to a flatten by emacs lisp interpreter.
23088 The internal string is an element of the structures.
23089 The displayed string is the flatten string. */
23090 gpos = 0;
23091 if (glyph > row_start_glyph)
23093 tmp_glyph = glyph - 1;
23094 while (tmp_glyph >= row_start_glyph
23095 && tmp_glyph->charpos >= XINT (b)
23096 && EQ (tmp_glyph->object, glyph->object))
23098 tmp_glyph--;
23099 gpos++;
23103 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23104 displayed string holding GLYPH.
23106 GSEQ_LENGTH is different from SCHARS (STRING).
23107 SCHARS (STRING) returns the length of the internal string. */
23108 for (tmp_glyph = glyph, gseq_length = gpos;
23109 tmp_glyph->charpos < XINT (e);
23110 tmp_glyph++, gseq_length++)
23112 if (!EQ (tmp_glyph->object, glyph->object))
23113 break;
23116 total_pixel_width = 0;
23117 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23118 total_pixel_width += tmp_glyph->pixel_width;
23120 /* Pre calculation of re-rendering position */
23121 vpos = (x - gpos);
23122 hpos = (area == ON_MODE_LINE
23123 ? (w->current_matrix)->nrows - 1
23124 : 0);
23126 /* If the re-rendering position is included in the last
23127 re-rendering area, we should do nothing. */
23128 if ( EQ (window, dpyinfo->mouse_face_window)
23129 && dpyinfo->mouse_face_beg_col <= vpos
23130 && vpos < dpyinfo->mouse_face_end_col
23131 && dpyinfo->mouse_face_beg_row == hpos )
23132 return;
23134 if (clear_mouse_face (dpyinfo))
23135 cursor = No_Cursor;
23137 dpyinfo->mouse_face_beg_col = vpos;
23138 dpyinfo->mouse_face_beg_row = hpos;
23140 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23141 dpyinfo->mouse_face_beg_y = 0;
23143 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23144 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23146 dpyinfo->mouse_face_end_x = 0;
23147 dpyinfo->mouse_face_end_y = 0;
23149 dpyinfo->mouse_face_past_end = 0;
23150 dpyinfo->mouse_face_window = window;
23152 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23153 charpos,
23154 0, 0, 0, &ignore,
23155 glyph->face_id, 1);
23156 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23158 if (NILP (pointer))
23159 pointer = Qhand;
23161 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23162 clear_mouse_face (dpyinfo);
23164 define_frame_cursor1 (f, cursor, pointer);
23168 /* EXPORT:
23169 Take proper action when the mouse has moved to position X, Y on
23170 frame F as regards highlighting characters that have mouse-face
23171 properties. Also de-highlighting chars where the mouse was before.
23172 X and Y can be negative or out of range. */
23174 void
23175 note_mouse_highlight (f, x, y)
23176 struct frame *f;
23177 int x, y;
23179 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23180 enum window_part part;
23181 Lisp_Object window;
23182 struct window *w;
23183 Cursor cursor = No_Cursor;
23184 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23185 struct buffer *b;
23187 /* When a menu is active, don't highlight because this looks odd. */
23188 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23189 if (popup_activated ())
23190 return;
23191 #endif
23193 if (NILP (Vmouse_highlight)
23194 || !f->glyphs_initialized_p)
23195 return;
23197 dpyinfo->mouse_face_mouse_x = x;
23198 dpyinfo->mouse_face_mouse_y = y;
23199 dpyinfo->mouse_face_mouse_frame = f;
23201 if (dpyinfo->mouse_face_defer)
23202 return;
23204 if (gc_in_progress)
23206 dpyinfo->mouse_face_deferred_gc = 1;
23207 return;
23210 /* Which window is that in? */
23211 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23213 /* If we were displaying active text in another window, clear that.
23214 Also clear if we move out of text area in same window. */
23215 if (! EQ (window, dpyinfo->mouse_face_window)
23216 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23217 && !NILP (dpyinfo->mouse_face_window)))
23218 clear_mouse_face (dpyinfo);
23220 /* Not on a window -> return. */
23221 if (!WINDOWP (window))
23222 return;
23224 /* Reset help_echo_string. It will get recomputed below. */
23225 help_echo_string = Qnil;
23227 /* Convert to window-relative pixel coordinates. */
23228 w = XWINDOW (window);
23229 frame_to_window_pixel_xy (w, &x, &y);
23231 /* Handle tool-bar window differently since it doesn't display a
23232 buffer. */
23233 if (EQ (window, f->tool_bar_window))
23235 note_tool_bar_highlight (f, x, y);
23236 return;
23239 /* Mouse is on the mode, header line or margin? */
23240 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23241 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23243 note_mode_line_or_margin_highlight (window, x, y, part);
23244 return;
23247 if (part == ON_VERTICAL_BORDER)
23249 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23250 help_echo_string = build_string ("drag-mouse-1: resize");
23252 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23253 || part == ON_SCROLL_BAR)
23254 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23255 else
23256 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23258 /* Are we in a window whose display is up to date?
23259 And verify the buffer's text has not changed. */
23260 b = XBUFFER (w->buffer);
23261 if (part == ON_TEXT
23262 && EQ (w->window_end_valid, w->buffer)
23263 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23264 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23266 int hpos, vpos, pos, i, dx, dy, area;
23267 struct glyph *glyph;
23268 Lisp_Object object;
23269 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23270 Lisp_Object *overlay_vec = NULL;
23271 int noverlays;
23272 struct buffer *obuf;
23273 int obegv, ozv, same_region;
23275 /* Find the glyph under X/Y. */
23276 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23278 /* Look for :pointer property on image. */
23279 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23281 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23282 if (img != NULL && IMAGEP (img->spec))
23284 Lisp_Object image_map, hotspot;
23285 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23286 !NILP (image_map))
23287 && (hotspot = find_hot_spot (image_map,
23288 glyph->slice.x + dx,
23289 glyph->slice.y + dy),
23290 CONSP (hotspot))
23291 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23293 Lisp_Object area_id, plist;
23295 area_id = XCAR (hotspot);
23296 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23297 If so, we could look for mouse-enter, mouse-leave
23298 properties in PLIST (and do something...). */
23299 hotspot = XCDR (hotspot);
23300 if (CONSP (hotspot)
23301 && (plist = XCAR (hotspot), CONSP (plist)))
23303 pointer = Fplist_get (plist, Qpointer);
23304 if (NILP (pointer))
23305 pointer = Qhand;
23306 help_echo_string = Fplist_get (plist, Qhelp_echo);
23307 if (!NILP (help_echo_string))
23309 help_echo_window = window;
23310 help_echo_object = glyph->object;
23311 help_echo_pos = glyph->charpos;
23315 if (NILP (pointer))
23316 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23320 /* Clear mouse face if X/Y not over text. */
23321 if (glyph == NULL
23322 || area != TEXT_AREA
23323 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23325 if (clear_mouse_face (dpyinfo))
23326 cursor = No_Cursor;
23327 if (NILP (pointer))
23329 if (area != TEXT_AREA)
23330 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23331 else
23332 pointer = Vvoid_text_area_pointer;
23334 goto set_cursor;
23337 pos = glyph->charpos;
23338 object = glyph->object;
23339 if (!STRINGP (object) && !BUFFERP (object))
23340 goto set_cursor;
23342 /* If we get an out-of-range value, return now; avoid an error. */
23343 if (BUFFERP (object) && pos > BUF_Z (b))
23344 goto set_cursor;
23346 /* Make the window's buffer temporarily current for
23347 overlays_at and compute_char_face. */
23348 obuf = current_buffer;
23349 current_buffer = b;
23350 obegv = BEGV;
23351 ozv = ZV;
23352 BEGV = BEG;
23353 ZV = Z;
23355 /* Is this char mouse-active or does it have help-echo? */
23356 position = make_number (pos);
23358 if (BUFFERP (object))
23360 /* Put all the overlays we want in a vector in overlay_vec. */
23361 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23362 /* Sort overlays into increasing priority order. */
23363 noverlays = sort_overlays (overlay_vec, noverlays, w);
23365 else
23366 noverlays = 0;
23368 same_region = (EQ (window, dpyinfo->mouse_face_window)
23369 && vpos >= dpyinfo->mouse_face_beg_row
23370 && vpos <= dpyinfo->mouse_face_end_row
23371 && (vpos > dpyinfo->mouse_face_beg_row
23372 || hpos >= dpyinfo->mouse_face_beg_col)
23373 && (vpos < dpyinfo->mouse_face_end_row
23374 || hpos < dpyinfo->mouse_face_end_col
23375 || dpyinfo->mouse_face_past_end));
23377 if (same_region)
23378 cursor = No_Cursor;
23380 /* Check mouse-face highlighting. */
23381 if (! same_region
23382 /* If there exists an overlay with mouse-face overlapping
23383 the one we are currently highlighting, we have to
23384 check if we enter the overlapping overlay, and then
23385 highlight only that. */
23386 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23387 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23389 /* Find the highest priority overlay that has a mouse-face
23390 property. */
23391 overlay = Qnil;
23392 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23394 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23395 if (!NILP (mouse_face))
23396 overlay = overlay_vec[i];
23399 /* If we're actually highlighting the same overlay as
23400 before, there's no need to do that again. */
23401 if (!NILP (overlay)
23402 && EQ (overlay, dpyinfo->mouse_face_overlay))
23403 goto check_help_echo;
23405 dpyinfo->mouse_face_overlay = overlay;
23407 /* Clear the display of the old active region, if any. */
23408 if (clear_mouse_face (dpyinfo))
23409 cursor = No_Cursor;
23411 /* If no overlay applies, get a text property. */
23412 if (NILP (overlay))
23413 mouse_face = Fget_text_property (position, Qmouse_face, object);
23415 /* Handle the overlay case. */
23416 if (!NILP (overlay))
23418 /* Find the range of text around this char that
23419 should be active. */
23420 Lisp_Object before, after;
23421 int ignore;
23423 before = Foverlay_start (overlay);
23424 after = Foverlay_end (overlay);
23425 /* Record this as the current active region. */
23426 fast_find_position (w, XFASTINT (before),
23427 &dpyinfo->mouse_face_beg_col,
23428 &dpyinfo->mouse_face_beg_row,
23429 &dpyinfo->mouse_face_beg_x,
23430 &dpyinfo->mouse_face_beg_y, Qnil);
23432 dpyinfo->mouse_face_past_end
23433 = !fast_find_position (w, XFASTINT (after),
23434 &dpyinfo->mouse_face_end_col,
23435 &dpyinfo->mouse_face_end_row,
23436 &dpyinfo->mouse_face_end_x,
23437 &dpyinfo->mouse_face_end_y, Qnil);
23438 dpyinfo->mouse_face_window = window;
23440 dpyinfo->mouse_face_face_id
23441 = face_at_buffer_position (w, pos, 0, 0,
23442 &ignore, pos + 1,
23443 !dpyinfo->mouse_face_hidden);
23445 /* Display it as active. */
23446 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23447 cursor = No_Cursor;
23449 /* Handle the text property case. */
23450 else if (!NILP (mouse_face) && BUFFERP (object))
23452 /* Find the range of text around this char that
23453 should be active. */
23454 Lisp_Object before, after, beginning, end;
23455 int ignore;
23457 beginning = Fmarker_position (w->start);
23458 end = make_number (BUF_Z (XBUFFER (object))
23459 - XFASTINT (w->window_end_pos));
23460 before
23461 = Fprevious_single_property_change (make_number (pos + 1),
23462 Qmouse_face,
23463 object, beginning);
23464 after
23465 = Fnext_single_property_change (position, Qmouse_face,
23466 object, end);
23468 /* Record this as the current active region. */
23469 fast_find_position (w, XFASTINT (before),
23470 &dpyinfo->mouse_face_beg_col,
23471 &dpyinfo->mouse_face_beg_row,
23472 &dpyinfo->mouse_face_beg_x,
23473 &dpyinfo->mouse_face_beg_y, Qnil);
23474 dpyinfo->mouse_face_past_end
23475 = !fast_find_position (w, XFASTINT (after),
23476 &dpyinfo->mouse_face_end_col,
23477 &dpyinfo->mouse_face_end_row,
23478 &dpyinfo->mouse_face_end_x,
23479 &dpyinfo->mouse_face_end_y, Qnil);
23480 dpyinfo->mouse_face_window = window;
23482 if (BUFFERP (object))
23483 dpyinfo->mouse_face_face_id
23484 = face_at_buffer_position (w, pos, 0, 0,
23485 &ignore, pos + 1,
23486 !dpyinfo->mouse_face_hidden);
23488 /* Display it as active. */
23489 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23490 cursor = No_Cursor;
23492 else if (!NILP (mouse_face) && STRINGP (object))
23494 Lisp_Object b, e;
23495 int ignore;
23497 b = Fprevious_single_property_change (make_number (pos + 1),
23498 Qmouse_face,
23499 object, Qnil);
23500 e = Fnext_single_property_change (position, Qmouse_face,
23501 object, Qnil);
23502 if (NILP (b))
23503 b = make_number (0);
23504 if (NILP (e))
23505 e = make_number (SCHARS (object) - 1);
23507 fast_find_string_pos (w, XINT (b), object,
23508 &dpyinfo->mouse_face_beg_col,
23509 &dpyinfo->mouse_face_beg_row,
23510 &dpyinfo->mouse_face_beg_x,
23511 &dpyinfo->mouse_face_beg_y, 0);
23512 fast_find_string_pos (w, XINT (e), object,
23513 &dpyinfo->mouse_face_end_col,
23514 &dpyinfo->mouse_face_end_row,
23515 &dpyinfo->mouse_face_end_x,
23516 &dpyinfo->mouse_face_end_y, 1);
23517 dpyinfo->mouse_face_past_end = 0;
23518 dpyinfo->mouse_face_window = window;
23519 dpyinfo->mouse_face_face_id
23520 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23521 glyph->face_id, 1);
23522 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23523 cursor = No_Cursor;
23525 else if (STRINGP (object) && NILP (mouse_face))
23527 /* A string which doesn't have mouse-face, but
23528 the text ``under'' it might have. */
23529 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23530 int start = MATRIX_ROW_START_CHARPOS (r);
23532 pos = string_buffer_position (w, object, start);
23533 if (pos > 0)
23534 mouse_face = get_char_property_and_overlay (make_number (pos),
23535 Qmouse_face,
23536 w->buffer,
23537 &overlay);
23538 if (!NILP (mouse_face) && !NILP (overlay))
23540 Lisp_Object before = Foverlay_start (overlay);
23541 Lisp_Object after = Foverlay_end (overlay);
23542 int ignore;
23544 /* Note that we might not be able to find position
23545 BEFORE in the glyph matrix if the overlay is
23546 entirely covered by a `display' property. In
23547 this case, we overshoot. So let's stop in
23548 the glyph matrix before glyphs for OBJECT. */
23549 fast_find_position (w, XFASTINT (before),
23550 &dpyinfo->mouse_face_beg_col,
23551 &dpyinfo->mouse_face_beg_row,
23552 &dpyinfo->mouse_face_beg_x,
23553 &dpyinfo->mouse_face_beg_y,
23554 object);
23556 dpyinfo->mouse_face_past_end
23557 = !fast_find_position (w, XFASTINT (after),
23558 &dpyinfo->mouse_face_end_col,
23559 &dpyinfo->mouse_face_end_row,
23560 &dpyinfo->mouse_face_end_x,
23561 &dpyinfo->mouse_face_end_y,
23562 Qnil);
23563 dpyinfo->mouse_face_window = window;
23564 dpyinfo->mouse_face_face_id
23565 = face_at_buffer_position (w, pos, 0, 0,
23566 &ignore, pos + 1,
23567 !dpyinfo->mouse_face_hidden);
23569 /* Display it as active. */
23570 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23571 cursor = No_Cursor;
23576 check_help_echo:
23578 /* Look for a `help-echo' property. */
23579 if (NILP (help_echo_string)) {
23580 Lisp_Object help, overlay;
23582 /* Check overlays first. */
23583 help = overlay = Qnil;
23584 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23586 overlay = overlay_vec[i];
23587 help = Foverlay_get (overlay, Qhelp_echo);
23590 if (!NILP (help))
23592 help_echo_string = help;
23593 help_echo_window = window;
23594 help_echo_object = overlay;
23595 help_echo_pos = pos;
23597 else
23599 Lisp_Object object = glyph->object;
23600 int charpos = glyph->charpos;
23602 /* Try text properties. */
23603 if (STRINGP (object)
23604 && charpos >= 0
23605 && charpos < SCHARS (object))
23607 help = Fget_text_property (make_number (charpos),
23608 Qhelp_echo, object);
23609 if (NILP (help))
23611 /* If the string itself doesn't specify a help-echo,
23612 see if the buffer text ``under'' it does. */
23613 struct glyph_row *r
23614 = MATRIX_ROW (w->current_matrix, vpos);
23615 int start = MATRIX_ROW_START_CHARPOS (r);
23616 int pos = string_buffer_position (w, object, start);
23617 if (pos > 0)
23619 help = Fget_char_property (make_number (pos),
23620 Qhelp_echo, w->buffer);
23621 if (!NILP (help))
23623 charpos = pos;
23624 object = w->buffer;
23629 else if (BUFFERP (object)
23630 && charpos >= BEGV
23631 && charpos < ZV)
23632 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23633 object);
23635 if (!NILP (help))
23637 help_echo_string = help;
23638 help_echo_window = window;
23639 help_echo_object = object;
23640 help_echo_pos = charpos;
23645 /* Look for a `pointer' property. */
23646 if (NILP (pointer))
23648 /* Check overlays first. */
23649 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23650 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23652 if (NILP (pointer))
23654 Lisp_Object object = glyph->object;
23655 int charpos = glyph->charpos;
23657 /* Try text properties. */
23658 if (STRINGP (object)
23659 && charpos >= 0
23660 && charpos < SCHARS (object))
23662 pointer = Fget_text_property (make_number (charpos),
23663 Qpointer, object);
23664 if (NILP (pointer))
23666 /* If the string itself doesn't specify a pointer,
23667 see if the buffer text ``under'' it does. */
23668 struct glyph_row *r
23669 = MATRIX_ROW (w->current_matrix, vpos);
23670 int start = MATRIX_ROW_START_CHARPOS (r);
23671 int pos = string_buffer_position (w, object, start);
23672 if (pos > 0)
23673 pointer = Fget_char_property (make_number (pos),
23674 Qpointer, w->buffer);
23677 else if (BUFFERP (object)
23678 && charpos >= BEGV
23679 && charpos < ZV)
23680 pointer = Fget_text_property (make_number (charpos),
23681 Qpointer, object);
23685 BEGV = obegv;
23686 ZV = ozv;
23687 current_buffer = obuf;
23690 set_cursor:
23692 define_frame_cursor1 (f, cursor, pointer);
23696 /* EXPORT for RIF:
23697 Clear any mouse-face on window W. This function is part of the
23698 redisplay interface, and is called from try_window_id and similar
23699 functions to ensure the mouse-highlight is off. */
23701 void
23702 x_clear_window_mouse_face (w)
23703 struct window *w;
23705 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23706 Lisp_Object window;
23708 BLOCK_INPUT;
23709 XSETWINDOW (window, w);
23710 if (EQ (window, dpyinfo->mouse_face_window))
23711 clear_mouse_face (dpyinfo);
23712 UNBLOCK_INPUT;
23716 /* EXPORT:
23717 Just discard the mouse face information for frame F, if any.
23718 This is used when the size of F is changed. */
23720 void
23721 cancel_mouse_face (f)
23722 struct frame *f;
23724 Lisp_Object window;
23725 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23727 window = dpyinfo->mouse_face_window;
23728 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23730 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23731 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23732 dpyinfo->mouse_face_window = Qnil;
23737 #endif /* HAVE_WINDOW_SYSTEM */
23740 /***********************************************************************
23741 Exposure Events
23742 ***********************************************************************/
23744 #ifdef HAVE_WINDOW_SYSTEM
23746 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23747 which intersects rectangle R. R is in window-relative coordinates. */
23749 static void
23750 expose_area (w, row, r, area)
23751 struct window *w;
23752 struct glyph_row *row;
23753 XRectangle *r;
23754 enum glyph_row_area area;
23756 struct glyph *first = row->glyphs[area];
23757 struct glyph *end = row->glyphs[area] + row->used[area];
23758 struct glyph *last;
23759 int first_x, start_x, x;
23761 if (area == TEXT_AREA && row->fill_line_p)
23762 /* If row extends face to end of line write the whole line. */
23763 draw_glyphs (w, 0, row, area,
23764 0, row->used[area],
23765 DRAW_NORMAL_TEXT, 0);
23766 else
23768 /* Set START_X to the window-relative start position for drawing glyphs of
23769 AREA. The first glyph of the text area can be partially visible.
23770 The first glyphs of other areas cannot. */
23771 start_x = window_box_left_offset (w, area);
23772 x = start_x;
23773 if (area == TEXT_AREA)
23774 x += row->x;
23776 /* Find the first glyph that must be redrawn. */
23777 while (first < end
23778 && x + first->pixel_width < r->x)
23780 x += first->pixel_width;
23781 ++first;
23784 /* Find the last one. */
23785 last = first;
23786 first_x = x;
23787 while (last < end
23788 && x < r->x + r->width)
23790 x += last->pixel_width;
23791 ++last;
23794 /* Repaint. */
23795 if (last > first)
23796 draw_glyphs (w, first_x - start_x, row, area,
23797 first - row->glyphs[area], last - row->glyphs[area],
23798 DRAW_NORMAL_TEXT, 0);
23803 /* Redraw the parts of the glyph row ROW on window W intersecting
23804 rectangle R. R is in window-relative coordinates. Value is
23805 non-zero if mouse-face was overwritten. */
23807 static int
23808 expose_line (w, row, r)
23809 struct window *w;
23810 struct glyph_row *row;
23811 XRectangle *r;
23813 xassert (row->enabled_p);
23815 if (row->mode_line_p || w->pseudo_window_p)
23816 draw_glyphs (w, 0, row, TEXT_AREA,
23817 0, row->used[TEXT_AREA],
23818 DRAW_NORMAL_TEXT, 0);
23819 else
23821 if (row->used[LEFT_MARGIN_AREA])
23822 expose_area (w, row, r, LEFT_MARGIN_AREA);
23823 if (row->used[TEXT_AREA])
23824 expose_area (w, row, r, TEXT_AREA);
23825 if (row->used[RIGHT_MARGIN_AREA])
23826 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23827 draw_row_fringe_bitmaps (w, row);
23830 return row->mouse_face_p;
23834 /* Redraw those parts of glyphs rows during expose event handling that
23835 overlap other rows. Redrawing of an exposed line writes over parts
23836 of lines overlapping that exposed line; this function fixes that.
23838 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23839 row in W's current matrix that is exposed and overlaps other rows.
23840 LAST_OVERLAPPING_ROW is the last such row. */
23842 static void
23843 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
23844 struct window *w;
23845 struct glyph_row *first_overlapping_row;
23846 struct glyph_row *last_overlapping_row;
23847 XRectangle *r;
23849 struct glyph_row *row;
23851 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23852 if (row->overlapping_p)
23854 xassert (row->enabled_p && !row->mode_line_p);
23856 row->clip = r;
23857 if (row->used[LEFT_MARGIN_AREA])
23858 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23860 if (row->used[TEXT_AREA])
23861 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23863 if (row->used[RIGHT_MARGIN_AREA])
23864 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23865 row->clip = NULL;
23870 /* Return non-zero if W's cursor intersects rectangle R. */
23872 static int
23873 phys_cursor_in_rect_p (w, r)
23874 struct window *w;
23875 XRectangle *r;
23877 XRectangle cr, result;
23878 struct glyph *cursor_glyph;
23879 struct glyph_row *row;
23881 if (w->phys_cursor.vpos >= 0
23882 && w->phys_cursor.vpos < w->current_matrix->nrows
23883 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23884 row->enabled_p)
23885 && row->cursor_in_fringe_p)
23887 /* Cursor is in the fringe. */
23888 cr.x = window_box_right_offset (w,
23889 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23890 ? RIGHT_MARGIN_AREA
23891 : TEXT_AREA));
23892 cr.y = row->y;
23893 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23894 cr.height = row->height;
23895 return x_intersect_rectangles (&cr, r, &result);
23898 cursor_glyph = get_phys_cursor_glyph (w);
23899 if (cursor_glyph)
23901 /* r is relative to W's box, but w->phys_cursor.x is relative
23902 to left edge of W's TEXT area. Adjust it. */
23903 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23904 cr.y = w->phys_cursor.y;
23905 cr.width = cursor_glyph->pixel_width;
23906 cr.height = w->phys_cursor_height;
23907 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23908 I assume the effect is the same -- and this is portable. */
23909 return x_intersect_rectangles (&cr, r, &result);
23911 /* If we don't understand the format, pretend we're not in the hot-spot. */
23912 return 0;
23916 /* EXPORT:
23917 Draw a vertical window border to the right of window W if W doesn't
23918 have vertical scroll bars. */
23920 void
23921 x_draw_vertical_border (w)
23922 struct window *w;
23924 struct frame *f = XFRAME (WINDOW_FRAME (w));
23926 /* We could do better, if we knew what type of scroll-bar the adjacent
23927 windows (on either side) have... But we don't :-(
23928 However, I think this works ok. ++KFS 2003-04-25 */
23930 /* Redraw borders between horizontally adjacent windows. Don't
23931 do it for frames with vertical scroll bars because either the
23932 right scroll bar of a window, or the left scroll bar of its
23933 neighbor will suffice as a border. */
23934 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23935 return;
23937 if (!WINDOW_RIGHTMOST_P (w)
23938 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23940 int x0, x1, y0, y1;
23942 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23943 y1 -= 1;
23945 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23946 x1 -= 1;
23948 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23950 else if (!WINDOW_LEFTMOST_P (w)
23951 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23953 int x0, x1, y0, y1;
23955 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23956 y1 -= 1;
23958 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23959 x0 -= 1;
23961 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23966 /* Redraw the part of window W intersection rectangle FR. Pixel
23967 coordinates in FR are frame-relative. Call this function with
23968 input blocked. Value is non-zero if the exposure overwrites
23969 mouse-face. */
23971 static int
23972 expose_window (w, fr)
23973 struct window *w;
23974 XRectangle *fr;
23976 struct frame *f = XFRAME (w->frame);
23977 XRectangle wr, r;
23978 int mouse_face_overwritten_p = 0;
23980 /* If window is not yet fully initialized, do nothing. This can
23981 happen when toolkit scroll bars are used and a window is split.
23982 Reconfiguring the scroll bar will generate an expose for a newly
23983 created window. */
23984 if (w->current_matrix == NULL)
23985 return 0;
23987 /* When we're currently updating the window, display and current
23988 matrix usually don't agree. Arrange for a thorough display
23989 later. */
23990 if (w == updated_window)
23992 SET_FRAME_GARBAGED (f);
23993 return 0;
23996 /* Frame-relative pixel rectangle of W. */
23997 wr.x = WINDOW_LEFT_EDGE_X (w);
23998 wr.y = WINDOW_TOP_EDGE_Y (w);
23999 wr.width = WINDOW_TOTAL_WIDTH (w);
24000 wr.height = WINDOW_TOTAL_HEIGHT (w);
24002 if (x_intersect_rectangles (fr, &wr, &r))
24004 int yb = window_text_bottom_y (w);
24005 struct glyph_row *row;
24006 int cursor_cleared_p;
24007 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24009 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24010 r.x, r.y, r.width, r.height));
24012 /* Convert to window coordinates. */
24013 r.x -= WINDOW_LEFT_EDGE_X (w);
24014 r.y -= WINDOW_TOP_EDGE_Y (w);
24016 /* Turn off the cursor. */
24017 if (!w->pseudo_window_p
24018 && phys_cursor_in_rect_p (w, &r))
24020 x_clear_cursor (w);
24021 cursor_cleared_p = 1;
24023 else
24024 cursor_cleared_p = 0;
24026 /* Update lines intersecting rectangle R. */
24027 first_overlapping_row = last_overlapping_row = NULL;
24028 for (row = w->current_matrix->rows;
24029 row->enabled_p;
24030 ++row)
24032 int y0 = row->y;
24033 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24035 if ((y0 >= r.y && y0 < r.y + r.height)
24036 || (y1 > r.y && y1 < r.y + r.height)
24037 || (r.y >= y0 && r.y < y1)
24038 || (r.y + r.height > y0 && r.y + r.height < y1))
24040 /* A header line may be overlapping, but there is no need
24041 to fix overlapping areas for them. KFS 2005-02-12 */
24042 if (row->overlapping_p && !row->mode_line_p)
24044 if (first_overlapping_row == NULL)
24045 first_overlapping_row = row;
24046 last_overlapping_row = row;
24049 row->clip = fr;
24050 if (expose_line (w, row, &r))
24051 mouse_face_overwritten_p = 1;
24052 row->clip = NULL;
24054 else if (row->overlapping_p)
24056 /* We must redraw a row overlapping the exposed area. */
24057 if (y0 < r.y
24058 ? y0 + row->phys_height > r.y
24059 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24061 if (first_overlapping_row == NULL)
24062 first_overlapping_row = row;
24063 last_overlapping_row = row;
24067 if (y1 >= yb)
24068 break;
24071 /* Display the mode line if there is one. */
24072 if (WINDOW_WANTS_MODELINE_P (w)
24073 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24074 row->enabled_p)
24075 && row->y < r.y + r.height)
24077 if (expose_line (w, row, &r))
24078 mouse_face_overwritten_p = 1;
24081 if (!w->pseudo_window_p)
24083 /* Fix the display of overlapping rows. */
24084 if (first_overlapping_row)
24085 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24086 fr);
24088 /* Draw border between windows. */
24089 x_draw_vertical_border (w);
24091 /* Turn the cursor on again. */
24092 if (cursor_cleared_p)
24093 update_window_cursor (w, 1);
24097 return mouse_face_overwritten_p;
24102 /* Redraw (parts) of all windows in the window tree rooted at W that
24103 intersect R. R contains frame pixel coordinates. Value is
24104 non-zero if the exposure overwrites mouse-face. */
24106 static int
24107 expose_window_tree (w, r)
24108 struct window *w;
24109 XRectangle *r;
24111 struct frame *f = XFRAME (w->frame);
24112 int mouse_face_overwritten_p = 0;
24114 while (w && !FRAME_GARBAGED_P (f))
24116 if (!NILP (w->hchild))
24117 mouse_face_overwritten_p
24118 |= expose_window_tree (XWINDOW (w->hchild), r);
24119 else if (!NILP (w->vchild))
24120 mouse_face_overwritten_p
24121 |= expose_window_tree (XWINDOW (w->vchild), r);
24122 else
24123 mouse_face_overwritten_p |= expose_window (w, r);
24125 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24128 return mouse_face_overwritten_p;
24132 /* EXPORT:
24133 Redisplay an exposed area of frame F. X and Y are the upper-left
24134 corner of the exposed rectangle. W and H are width and height of
24135 the exposed area. All are pixel values. W or H zero means redraw
24136 the entire frame. */
24138 void
24139 expose_frame (f, x, y, w, h)
24140 struct frame *f;
24141 int x, y, w, h;
24143 XRectangle r;
24144 int mouse_face_overwritten_p = 0;
24146 TRACE ((stderr, "expose_frame "));
24148 /* No need to redraw if frame will be redrawn soon. */
24149 if (FRAME_GARBAGED_P (f))
24151 TRACE ((stderr, " garbaged\n"));
24152 return;
24155 /* If basic faces haven't been realized yet, there is no point in
24156 trying to redraw anything. This can happen when we get an expose
24157 event while Emacs is starting, e.g. by moving another window. */
24158 if (FRAME_FACE_CACHE (f) == NULL
24159 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24161 TRACE ((stderr, " no faces\n"));
24162 return;
24165 if (w == 0 || h == 0)
24167 r.x = r.y = 0;
24168 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24169 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24171 else
24173 r.x = x;
24174 r.y = y;
24175 r.width = w;
24176 r.height = h;
24179 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24180 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24182 if (WINDOWP (f->tool_bar_window))
24183 mouse_face_overwritten_p
24184 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24186 #ifdef HAVE_X_WINDOWS
24187 #ifndef MSDOS
24188 #ifndef USE_X_TOOLKIT
24189 if (WINDOWP (f->menu_bar_window))
24190 mouse_face_overwritten_p
24191 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24192 #endif /* not USE_X_TOOLKIT */
24193 #endif
24194 #endif
24196 /* Some window managers support a focus-follows-mouse style with
24197 delayed raising of frames. Imagine a partially obscured frame,
24198 and moving the mouse into partially obscured mouse-face on that
24199 frame. The visible part of the mouse-face will be highlighted,
24200 then the WM raises the obscured frame. With at least one WM, KDE
24201 2.1, Emacs is not getting any event for the raising of the frame
24202 (even tried with SubstructureRedirectMask), only Expose events.
24203 These expose events will draw text normally, i.e. not
24204 highlighted. Which means we must redo the highlight here.
24205 Subsume it under ``we love X''. --gerd 2001-08-15 */
24206 /* Included in Windows version because Windows most likely does not
24207 do the right thing if any third party tool offers
24208 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24209 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24211 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24212 if (f == dpyinfo->mouse_face_mouse_frame)
24214 int x = dpyinfo->mouse_face_mouse_x;
24215 int y = dpyinfo->mouse_face_mouse_y;
24216 clear_mouse_face (dpyinfo);
24217 note_mouse_highlight (f, x, y);
24223 /* EXPORT:
24224 Determine the intersection of two rectangles R1 and R2. Return
24225 the intersection in *RESULT. Value is non-zero if RESULT is not
24226 empty. */
24229 x_intersect_rectangles (r1, r2, result)
24230 XRectangle *r1, *r2, *result;
24232 XRectangle *left, *right;
24233 XRectangle *upper, *lower;
24234 int intersection_p = 0;
24236 /* Rearrange so that R1 is the left-most rectangle. */
24237 if (r1->x < r2->x)
24238 left = r1, right = r2;
24239 else
24240 left = r2, right = r1;
24242 /* X0 of the intersection is right.x0, if this is inside R1,
24243 otherwise there is no intersection. */
24244 if (right->x <= left->x + left->width)
24246 result->x = right->x;
24248 /* The right end of the intersection is the minimum of the
24249 the right ends of left and right. */
24250 result->width = (min (left->x + left->width, right->x + right->width)
24251 - result->x);
24253 /* Same game for Y. */
24254 if (r1->y < r2->y)
24255 upper = r1, lower = r2;
24256 else
24257 upper = r2, lower = r1;
24259 /* The upper end of the intersection is lower.y0, if this is inside
24260 of upper. Otherwise, there is no intersection. */
24261 if (lower->y <= upper->y + upper->height)
24263 result->y = lower->y;
24265 /* The lower end of the intersection is the minimum of the lower
24266 ends of upper and lower. */
24267 result->height = (min (lower->y + lower->height,
24268 upper->y + upper->height)
24269 - result->y);
24270 intersection_p = 1;
24274 return intersection_p;
24277 #endif /* HAVE_WINDOW_SYSTEM */
24280 /***********************************************************************
24281 Initialization
24282 ***********************************************************************/
24284 void
24285 syms_of_xdisp ()
24287 Vwith_echo_area_save_vector = Qnil;
24288 staticpro (&Vwith_echo_area_save_vector);
24290 Vmessage_stack = Qnil;
24291 staticpro (&Vmessage_stack);
24293 Qinhibit_redisplay = intern ("inhibit-redisplay");
24294 staticpro (&Qinhibit_redisplay);
24296 message_dolog_marker1 = Fmake_marker ();
24297 staticpro (&message_dolog_marker1);
24298 message_dolog_marker2 = Fmake_marker ();
24299 staticpro (&message_dolog_marker2);
24300 message_dolog_marker3 = Fmake_marker ();
24301 staticpro (&message_dolog_marker3);
24303 #if GLYPH_DEBUG
24304 defsubr (&Sdump_frame_glyph_matrix);
24305 defsubr (&Sdump_glyph_matrix);
24306 defsubr (&Sdump_glyph_row);
24307 defsubr (&Sdump_tool_bar_row);
24308 defsubr (&Strace_redisplay);
24309 defsubr (&Strace_to_stderr);
24310 #endif
24311 #ifdef HAVE_WINDOW_SYSTEM
24312 defsubr (&Stool_bar_lines_needed);
24313 defsubr (&Slookup_image_map);
24314 #endif
24315 defsubr (&Sformat_mode_line);
24316 defsubr (&Sinvisible_p);
24318 staticpro (&Qmenu_bar_update_hook);
24319 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24321 staticpro (&Qoverriding_terminal_local_map);
24322 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24324 staticpro (&Qoverriding_local_map);
24325 Qoverriding_local_map = intern ("overriding-local-map");
24327 staticpro (&Qwindow_scroll_functions);
24328 Qwindow_scroll_functions = intern ("window-scroll-functions");
24330 staticpro (&Qwindow_text_change_functions);
24331 Qwindow_text_change_functions = intern ("window-text-change-functions");
24333 staticpro (&Qredisplay_end_trigger_functions);
24334 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24336 staticpro (&Qinhibit_point_motion_hooks);
24337 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24339 QCdata = intern (":data");
24340 staticpro (&QCdata);
24341 Qdisplay = intern ("display");
24342 staticpro (&Qdisplay);
24343 Qspace_width = intern ("space-width");
24344 staticpro (&Qspace_width);
24345 Qraise = intern ("raise");
24346 staticpro (&Qraise);
24347 Qslice = intern ("slice");
24348 staticpro (&Qslice);
24349 Qspace = intern ("space");
24350 staticpro (&Qspace);
24351 Qmargin = intern ("margin");
24352 staticpro (&Qmargin);
24353 Qpointer = intern ("pointer");
24354 staticpro (&Qpointer);
24355 Qleft_margin = intern ("left-margin");
24356 staticpro (&Qleft_margin);
24357 Qright_margin = intern ("right-margin");
24358 staticpro (&Qright_margin);
24359 Qcenter = intern ("center");
24360 staticpro (&Qcenter);
24361 Qline_height = intern ("line-height");
24362 staticpro (&Qline_height);
24363 QCalign_to = intern (":align-to");
24364 staticpro (&QCalign_to);
24365 QCrelative_width = intern (":relative-width");
24366 staticpro (&QCrelative_width);
24367 QCrelative_height = intern (":relative-height");
24368 staticpro (&QCrelative_height);
24369 QCeval = intern (":eval");
24370 staticpro (&QCeval);
24371 QCpropertize = intern (":propertize");
24372 staticpro (&QCpropertize);
24373 QCfile = intern (":file");
24374 staticpro (&QCfile);
24375 Qfontified = intern ("fontified");
24376 staticpro (&Qfontified);
24377 Qfontification_functions = intern ("fontification-functions");
24378 staticpro (&Qfontification_functions);
24379 Qtrailing_whitespace = intern ("trailing-whitespace");
24380 staticpro (&Qtrailing_whitespace);
24381 Qescape_glyph = intern ("escape-glyph");
24382 staticpro (&Qescape_glyph);
24383 Qnobreak_space = intern ("nobreak-space");
24384 staticpro (&Qnobreak_space);
24385 Qimage = intern ("image");
24386 staticpro (&Qimage);
24387 QCmap = intern (":map");
24388 staticpro (&QCmap);
24389 QCpointer = intern (":pointer");
24390 staticpro (&QCpointer);
24391 Qrect = intern ("rect");
24392 staticpro (&Qrect);
24393 Qcircle = intern ("circle");
24394 staticpro (&Qcircle);
24395 Qpoly = intern ("poly");
24396 staticpro (&Qpoly);
24397 Qmessage_truncate_lines = intern ("message-truncate-lines");
24398 staticpro (&Qmessage_truncate_lines);
24399 Qgrow_only = intern ("grow-only");
24400 staticpro (&Qgrow_only);
24401 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24402 staticpro (&Qinhibit_menubar_update);
24403 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24404 staticpro (&Qinhibit_eval_during_redisplay);
24405 Qposition = intern ("position");
24406 staticpro (&Qposition);
24407 Qbuffer_position = intern ("buffer-position");
24408 staticpro (&Qbuffer_position);
24409 Qobject = intern ("object");
24410 staticpro (&Qobject);
24411 Qbar = intern ("bar");
24412 staticpro (&Qbar);
24413 Qhbar = intern ("hbar");
24414 staticpro (&Qhbar);
24415 Qbox = intern ("box");
24416 staticpro (&Qbox);
24417 Qhollow = intern ("hollow");
24418 staticpro (&Qhollow);
24419 Qhand = intern ("hand");
24420 staticpro (&Qhand);
24421 Qarrow = intern ("arrow");
24422 staticpro (&Qarrow);
24423 Qtext = intern ("text");
24424 staticpro (&Qtext);
24425 Qrisky_local_variable = intern ("risky-local-variable");
24426 staticpro (&Qrisky_local_variable);
24427 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24428 staticpro (&Qinhibit_free_realized_faces);
24430 list_of_error = Fcons (Fcons (intern ("error"),
24431 Fcons (intern ("void-variable"), Qnil)),
24432 Qnil);
24433 staticpro (&list_of_error);
24435 Qlast_arrow_position = intern ("last-arrow-position");
24436 staticpro (&Qlast_arrow_position);
24437 Qlast_arrow_string = intern ("last-arrow-string");
24438 staticpro (&Qlast_arrow_string);
24440 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24441 staticpro (&Qoverlay_arrow_string);
24442 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24443 staticpro (&Qoverlay_arrow_bitmap);
24445 echo_buffer[0] = echo_buffer[1] = Qnil;
24446 staticpro (&echo_buffer[0]);
24447 staticpro (&echo_buffer[1]);
24449 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24450 staticpro (&echo_area_buffer[0]);
24451 staticpro (&echo_area_buffer[1]);
24453 Vmessages_buffer_name = build_string ("*Messages*");
24454 staticpro (&Vmessages_buffer_name);
24456 mode_line_proptrans_alist = Qnil;
24457 staticpro (&mode_line_proptrans_alist);
24458 mode_line_string_list = Qnil;
24459 staticpro (&mode_line_string_list);
24460 mode_line_string_face = Qnil;
24461 staticpro (&mode_line_string_face);
24462 mode_line_string_face_prop = Qnil;
24463 staticpro (&mode_line_string_face_prop);
24464 Vmode_line_unwind_vector = Qnil;
24465 staticpro (&Vmode_line_unwind_vector);
24467 help_echo_string = Qnil;
24468 staticpro (&help_echo_string);
24469 help_echo_object = Qnil;
24470 staticpro (&help_echo_object);
24471 help_echo_window = Qnil;
24472 staticpro (&help_echo_window);
24473 previous_help_echo_string = Qnil;
24474 staticpro (&previous_help_echo_string);
24475 help_echo_pos = -1;
24477 #ifdef HAVE_WINDOW_SYSTEM
24478 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24479 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24480 For example, if a block cursor is over a tab, it will be drawn as
24481 wide as that tab on the display. */);
24482 x_stretch_cursor_p = 0;
24483 #endif
24485 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24486 doc: /* *Non-nil means highlight trailing whitespace.
24487 The face used for trailing whitespace is `trailing-whitespace'. */);
24488 Vshow_trailing_whitespace = Qnil;
24490 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24491 doc: /* *Control highlighting of nobreak space and soft hyphen.
24492 A value of t means highlight the character itself (for nobreak space,
24493 use face `nobreak-space').
24494 A value of nil means no highlighting.
24495 Other values mean display the escape glyph followed by an ordinary
24496 space or ordinary hyphen. */);
24497 Vnobreak_char_display = Qt;
24499 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24500 doc: /* *The pointer shape to show in void text areas.
24501 A value of nil means to show the text pointer. Other options are `arrow',
24502 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24503 Vvoid_text_area_pointer = Qarrow;
24505 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24506 doc: /* Non-nil means don't actually do any redisplay.
24507 This is used for internal purposes. */);
24508 Vinhibit_redisplay = Qnil;
24510 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24511 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24512 Vglobal_mode_string = Qnil;
24514 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24515 doc: /* Marker for where to display an arrow on top of the buffer text.
24516 This must be the beginning of a line in order to work.
24517 See also `overlay-arrow-string'. */);
24518 Voverlay_arrow_position = Qnil;
24520 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24521 doc: /* String to display as an arrow in non-window frames.
24522 See also `overlay-arrow-position'. */);
24523 Voverlay_arrow_string = build_string ("=>");
24525 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24526 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24527 The symbols on this list are examined during redisplay to determine
24528 where to display overlay arrows. */);
24529 Voverlay_arrow_variable_list
24530 = Fcons (intern ("overlay-arrow-position"), Qnil);
24532 DEFVAR_INT ("scroll-step", &scroll_step,
24533 doc: /* *The number of lines to try scrolling a window by when point moves out.
24534 If that fails to bring point back on frame, point is centered instead.
24535 If this is zero, point is always centered after it moves off frame.
24536 If you want scrolling to always be a line at a time, you should set
24537 `scroll-conservatively' to a large value rather than set this to 1. */);
24539 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24540 doc: /* *Scroll up to this many lines, to bring point back on screen.
24541 If point moves off-screen, redisplay will scroll by up to
24542 `scroll-conservatively' lines in order to bring point just barely
24543 onto the screen again. If that cannot be done, then redisplay
24544 recenters point as usual.
24546 A value of zero means always recenter point if it moves off screen. */);
24547 scroll_conservatively = 0;
24549 DEFVAR_INT ("scroll-margin", &scroll_margin,
24550 doc: /* *Number of lines of margin at the top and bottom of a window.
24551 Recenter the window whenever point gets within this many lines
24552 of the top or bottom of the window. */);
24553 scroll_margin = 0;
24555 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24556 doc: /* Pixels per inch value for non-window system displays.
24557 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24558 Vdisplay_pixels_per_inch = make_float (72.0);
24560 #if GLYPH_DEBUG
24561 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24562 #endif
24564 DEFVAR_BOOL ("truncate-partial-width-windows",
24565 &truncate_partial_width_windows,
24566 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24567 truncate_partial_width_windows = 1;
24569 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24570 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24571 Any other value means to use the appropriate face, `mode-line',
24572 `header-line', or `menu' respectively. */);
24573 mode_line_inverse_video = 1;
24575 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24576 doc: /* *Maximum buffer size for which line number should be displayed.
24577 If the buffer is bigger than this, the line number does not appear
24578 in the mode line. A value of nil means no limit. */);
24579 Vline_number_display_limit = Qnil;
24581 DEFVAR_INT ("line-number-display-limit-width",
24582 &line_number_display_limit_width,
24583 doc: /* *Maximum line width (in characters) for line number display.
24584 If the average length of the lines near point is bigger than this, then the
24585 line number may be omitted from the mode line. */);
24586 line_number_display_limit_width = 200;
24588 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24589 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24590 highlight_nonselected_windows = 0;
24592 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24593 doc: /* Non-nil if more than one frame is visible on this display.
24594 Minibuffer-only frames don't count, but iconified frames do.
24595 This variable is not guaranteed to be accurate except while processing
24596 `frame-title-format' and `icon-title-format'. */);
24598 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24599 doc: /* Template for displaying the title bar of visible frames.
24600 \(Assuming the window manager supports this feature.)
24602 This variable has the same structure as `mode-line-format', except that
24603 the %c and %l constructs are ignored. It is used only on frames for
24604 which no explicit name has been set \(see `modify-frame-parameters'). */);
24606 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24607 doc: /* Template for displaying the title bar of an iconified frame.
24608 \(Assuming the window manager supports this feature.)
24609 This variable has the same structure as `mode-line-format' (which see),
24610 and is used only on frames for which no explicit name has been set
24611 \(see `modify-frame-parameters'). */);
24612 Vicon_title_format
24613 = Vframe_title_format
24614 = Fcons (intern ("multiple-frames"),
24615 Fcons (build_string ("%b"),
24616 Fcons (Fcons (empty_unibyte_string,
24617 Fcons (intern ("invocation-name"),
24618 Fcons (build_string ("@"),
24619 Fcons (intern ("system-name"),
24620 Qnil)))),
24621 Qnil)));
24623 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24624 doc: /* Maximum number of lines to keep in the message log buffer.
24625 If nil, disable message logging. If t, log messages but don't truncate
24626 the buffer when it becomes large. */);
24627 Vmessage_log_max = make_number (100);
24629 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24630 doc: /* Functions called before redisplay, if window sizes have changed.
24631 The value should be a list of functions that take one argument.
24632 Just before redisplay, for each frame, if any of its windows have changed
24633 size since the last redisplay, or have been split or deleted,
24634 all the functions in the list are called, with the frame as argument. */);
24635 Vwindow_size_change_functions = Qnil;
24637 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24638 doc: /* List of functions to call before redisplaying a window with scrolling.
24639 Each function is called with two arguments, the window
24640 and its new display-start position. Note that the value of `window-end'
24641 is not valid when these functions are called. */);
24642 Vwindow_scroll_functions = Qnil;
24644 DEFVAR_LISP ("window-text-change-functions",
24645 &Vwindow_text_change_functions,
24646 doc: /* Functions to call in redisplay when text in the window might change. */);
24647 Vwindow_text_change_functions = Qnil;
24649 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24650 doc: /* Functions called when redisplay of a window reaches the end trigger.
24651 Each function is called with two arguments, the window and the end trigger value.
24652 See `set-window-redisplay-end-trigger'. */);
24653 Vredisplay_end_trigger_functions = Qnil;
24655 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24656 doc: /* *Non-nil means autoselect window with mouse pointer.
24657 If nil, do not autoselect windows.
24658 A positive number means delay autoselection by that many seconds: a
24659 window is autoselected only after the mouse has remained in that
24660 window for the duration of the delay.
24661 A negative number has a similar effect, but causes windows to be
24662 autoselected only after the mouse has stopped moving. \(Because of
24663 the way Emacs compares mouse events, you will occasionally wait twice
24664 that time before the window gets selected.\)
24665 Any other value means to autoselect window instantaneously when the
24666 mouse pointer enters it.
24668 Autoselection selects the minibuffer only if it is active, and never
24669 unselects the minibuffer if it is active.
24671 When customizing this variable make sure that the actual value of
24672 `focus-follows-mouse' matches the behavior of your window manager. */);
24673 Vmouse_autoselect_window = Qnil;
24675 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24676 doc: /* *Non-nil means automatically resize tool-bars.
24677 This dynamically changes the tool-bar's height to the minimum height
24678 that is needed to make all tool-bar items visible.
24679 If value is `grow-only', the tool-bar's height is only increased
24680 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24681 Vauto_resize_tool_bars = Qt;
24683 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24684 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24685 auto_raise_tool_bar_buttons_p = 1;
24687 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24688 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24689 make_cursor_line_fully_visible_p = 1;
24691 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24692 doc: /* *Border below tool-bar in pixels.
24693 If an integer, use it as the height of the border.
24694 If it is one of `internal-border-width' or `border-width', use the
24695 value of the corresponding frame parameter.
24696 Otherwise, no border is added below the tool-bar. */);
24697 Vtool_bar_border = Qinternal_border_width;
24699 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24700 doc: /* *Margin around tool-bar buttons in pixels.
24701 If an integer, use that for both horizontal and vertical margins.
24702 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24703 HORZ specifying the horizontal margin, and VERT specifying the
24704 vertical margin. */);
24705 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24707 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24708 doc: /* *Relief thickness of tool-bar buttons. */);
24709 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24711 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24712 doc: /* List of functions to call to fontify regions of text.
24713 Each function is called with one argument POS. Functions must
24714 fontify a region starting at POS in the current buffer, and give
24715 fontified regions the property `fontified'. */);
24716 Vfontification_functions = Qnil;
24717 Fmake_variable_buffer_local (Qfontification_functions);
24719 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24720 &unibyte_display_via_language_environment,
24721 doc: /* *Non-nil means display unibyte text according to language environment.
24722 Specifically this means that unibyte non-ASCII characters
24723 are displayed by converting them to the equivalent multibyte characters
24724 according to the current language environment. As a result, they are
24725 displayed according to the current fontset. */);
24726 unibyte_display_via_language_environment = 0;
24728 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24729 doc: /* *Maximum height for resizing mini-windows.
24730 If a float, it specifies a fraction of the mini-window frame's height.
24731 If an integer, it specifies a number of lines. */);
24732 Vmax_mini_window_height = make_float (0.25);
24734 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24735 doc: /* *How to resize mini-windows.
24736 A value of nil means don't automatically resize mini-windows.
24737 A value of t means resize them to fit the text displayed in them.
24738 A value of `grow-only', the default, means let mini-windows grow
24739 only, until their display becomes empty, at which point the windows
24740 go back to their normal size. */);
24741 Vresize_mini_windows = Qgrow_only;
24743 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24744 doc: /* Alist specifying how to blink the cursor off.
24745 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24746 `cursor-type' frame-parameter or variable equals ON-STATE,
24747 comparing using `equal', Emacs uses OFF-STATE to specify
24748 how to blink it off. ON-STATE and OFF-STATE are values for
24749 the `cursor-type' frame parameter.
24751 If a frame's ON-STATE has no entry in this list,
24752 the frame's other specifications determine how to blink the cursor off. */);
24753 Vblink_cursor_alist = Qnil;
24755 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24756 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24757 automatic_hscrolling_p = 1;
24758 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
24759 staticpro (&Qauto_hscroll_mode);
24761 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24762 doc: /* *How many columns away from the window edge point is allowed to get
24763 before automatic hscrolling will horizontally scroll the window. */);
24764 hscroll_margin = 5;
24766 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24767 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24768 When point is less than `hscroll-margin' columns from the window
24769 edge, automatic hscrolling will scroll the window by the amount of columns
24770 determined by this variable. If its value is a positive integer, scroll that
24771 many columns. If it's a positive floating-point number, it specifies the
24772 fraction of the window's width to scroll. If it's nil or zero, point will be
24773 centered horizontally after the scroll. Any other value, including negative
24774 numbers, are treated as if the value were zero.
24776 Automatic hscrolling always moves point outside the scroll margin, so if
24777 point was more than scroll step columns inside the margin, the window will
24778 scroll more than the value given by the scroll step.
24780 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24781 and `scroll-right' overrides this variable's effect. */);
24782 Vhscroll_step = make_number (0);
24784 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24785 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24786 Bind this around calls to `message' to let it take effect. */);
24787 message_truncate_lines = 0;
24789 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24790 doc: /* Normal hook run to update the menu bar definitions.
24791 Redisplay runs this hook before it redisplays the menu bar.
24792 This is used to update submenus such as Buffers,
24793 whose contents depend on various data. */);
24794 Vmenu_bar_update_hook = Qnil;
24796 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24797 doc: /* Frame for which we are updating a menu.
24798 The enable predicate for a menu binding should check this variable. */);
24799 Vmenu_updating_frame = Qnil;
24801 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24802 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24803 inhibit_menubar_update = 0;
24805 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24806 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24807 inhibit_eval_during_redisplay = 0;
24809 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24810 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24811 inhibit_free_realized_faces = 0;
24813 #if GLYPH_DEBUG
24814 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24815 doc: /* Inhibit try_window_id display optimization. */);
24816 inhibit_try_window_id = 0;
24818 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24819 doc: /* Inhibit try_window_reusing display optimization. */);
24820 inhibit_try_window_reusing = 0;
24822 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24823 doc: /* Inhibit try_cursor_movement display optimization. */);
24824 inhibit_try_cursor_movement = 0;
24825 #endif /* GLYPH_DEBUG */
24827 DEFVAR_INT ("overline-margin", &overline_margin,
24828 doc: /* *Space between overline and text, in pixels.
24829 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24830 margin to the caracter height. */);
24831 overline_margin = 2;
24835 /* Initialize this module when Emacs starts. */
24837 void
24838 init_xdisp ()
24840 Lisp_Object root_window;
24841 struct window *mini_w;
24843 current_header_line_height = current_mode_line_height = -1;
24845 CHARPOS (this_line_start_pos) = 0;
24847 mini_w = XWINDOW (minibuf_window);
24848 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24850 if (!noninteractive)
24852 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24853 int i;
24855 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24856 set_window_height (root_window,
24857 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24859 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24860 set_window_height (minibuf_window, 1, 0);
24862 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24863 mini_w->total_cols = make_number (FRAME_COLS (f));
24865 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24866 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24867 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24869 /* The default ellipsis glyphs `...'. */
24870 for (i = 0; i < 3; ++i)
24871 default_invis_vector[i] = make_number ('.');
24875 /* Allocate the buffer for frame titles.
24876 Also used for `format-mode-line'. */
24877 int size = 100;
24878 mode_line_noprop_buf = (char *) xmalloc (size);
24879 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24880 mode_line_noprop_ptr = mode_line_noprop_buf;
24881 mode_line_target = MODE_LINE_DISPLAY;
24884 help_echo_showing_p = 0;
24888 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24889 (do not change this comment) */