(FRAME_OUTER_TO_INNER_DIFF_X, FRAME_OUTER_TO_INNER_DIFF_Y):
[emacs.git] / src / xdisp.c
blobf20aaedd2f7936011ee5e7d7f32a2c0a00aef236
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 "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
208 #define INFINITY 10000000
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
216 extern int interrupt_input;
217 extern int command_loop_level;
219 extern Lisp_Object do_mouse_tracking;
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
249 Lisp_Object Qrisky_local_variable;
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
254 /* Functions called to fontify regions of text. */
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
259 /* Non-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_autoselect_window;
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
266 int auto_raise_tool_bar_buttons_p;
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
270 int make_cursor_line_fully_visible_p;
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
276 Lisp_Object Vtool_bar_border;
278 /* Margin around tool bar buttons in pixels. */
280 Lisp_Object Vtool_bar_button_margin;
282 /* Thickness of shadow to draw around tool bar buttons. */
284 EMACS_INT tool_bar_button_relief;
286 /* Non-nil means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain.
289 If value is `grow-only', only make tool-bar bigger. */
291 Lisp_Object Vauto_resize_tool_bars;
293 /* Non-zero means draw block and hollow cursor as wide as the glyph
294 under it. For example, if a block cursor is over a tab, it will be
295 drawn as wide as that tab on the display. */
297 int x_stretch_cursor_p;
299 /* Non-nil means don't actually do any redisplay. */
301 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
303 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
305 int inhibit_eval_during_redisplay;
307 /* Names of text properties relevant for redisplay. */
309 Lisp_Object Qdisplay;
310 extern Lisp_Object Qface, Qinvisible, Qwidth;
312 /* Symbols used in text property values. */
314 Lisp_Object Vdisplay_pixels_per_inch;
315 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
316 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
317 Lisp_Object Qslice;
318 Lisp_Object Qcenter;
319 Lisp_Object Qmargin, Qpointer;
320 Lisp_Object Qline_height;
321 extern Lisp_Object Qheight;
322 extern Lisp_Object QCwidth, QCheight, QCascent;
323 extern Lisp_Object Qscroll_bar;
324 extern Lisp_Object Qcursor;
326 /* Non-nil means highlight trailing whitespace. */
328 Lisp_Object Vshow_trailing_whitespace;
330 /* Non-nil means escape non-break space and hyphens. */
332 Lisp_Object Vnobreak_char_display;
334 #ifdef HAVE_WINDOW_SYSTEM
335 extern Lisp_Object Voverflow_newline_into_fringe;
337 /* Test if overflow newline into fringe. Called with iterator IT
338 at or past right window margin, and with IT->current_x set. */
340 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
341 (!NILP (Voverflow_newline_into_fringe) \
342 && FRAME_WINDOW_P (it->f) \
343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
344 && it->current_x == it->last_visible_x)
346 #endif /* HAVE_WINDOW_SYSTEM */
348 /* Non-nil means show the text cursor in void text areas
349 i.e. in blank areas after eol and eob. This used to be
350 the default in 21.3. */
352 Lisp_Object Vvoid_text_area_pointer;
354 /* Name of the face used to highlight trailing whitespace. */
356 Lisp_Object Qtrailing_whitespace;
358 /* Name and number of the face used to highlight escape glyphs. */
360 Lisp_Object Qescape_glyph;
362 /* Name and number of the face used to highlight non-breaking spaces. */
364 Lisp_Object Qnobreak_space;
366 /* The symbol `image' which is the car of the lists used to represent
367 images in Lisp. */
369 Lisp_Object Qimage;
371 /* The image map types. */
372 Lisp_Object QCmap, QCpointer;
373 Lisp_Object Qrect, Qcircle, Qpoly;
375 /* Non-zero means print newline to stdout before next mini-buffer
376 message. */
378 int noninteractive_need_newline;
380 /* Non-zero means print newline to message log before next message. */
382 static int message_log_need_newline;
384 /* Three markers that message_dolog uses.
385 It could allocate them itself, but that causes trouble
386 in handling memory-full errors. */
387 static Lisp_Object message_dolog_marker1;
388 static Lisp_Object message_dolog_marker2;
389 static Lisp_Object message_dolog_marker3;
391 /* The buffer position of the first character appearing entirely or
392 partially on the line of the selected window which contains the
393 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
394 redisplay optimization in redisplay_internal. */
396 static struct text_pos this_line_start_pos;
398 /* Number of characters past the end of the line above, including the
399 terminating newline. */
401 static struct text_pos this_line_end_pos;
403 /* The vertical positions and the height of this line. */
405 static int this_line_vpos;
406 static int this_line_y;
407 static int this_line_pixel_height;
409 /* X position at which this display line starts. Usually zero;
410 negative if first character is partially visible. */
412 static int this_line_start_x;
414 /* Buffer that this_line_.* variables are referring to. */
416 static struct buffer *this_line_buffer;
418 /* Nonzero means truncate lines in all windows less wide than the
419 frame. */
421 int truncate_partial_width_windows;
423 /* A flag to control how to display unibyte 8-bit character. */
425 int unibyte_display_via_language_environment;
427 /* Nonzero means we have more than one non-mini-buffer-only frame.
428 Not guaranteed to be accurate except while parsing
429 frame-title-format. */
431 int multiple_frames;
433 Lisp_Object Vglobal_mode_string;
436 /* List of variables (symbols) which hold markers for overlay arrows.
437 The symbols on this list are examined during redisplay to determine
438 where to display overlay arrows. */
440 Lisp_Object Voverlay_arrow_variable_list;
442 /* Marker for where to display an arrow on top of the buffer text. */
444 Lisp_Object Voverlay_arrow_position;
446 /* String to display for the arrow. Only used on terminal frames. */
448 Lisp_Object Voverlay_arrow_string;
450 /* Values of those variables at last redisplay are stored as
451 properties on `overlay-arrow-position' symbol. However, if
452 Voverlay_arrow_position is a marker, last-arrow-position is its
453 numerical position. */
455 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
457 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
458 properties on a symbol in overlay-arrow-variable-list. */
460 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
462 /* Like mode-line-format, but for the title bar on a visible frame. */
464 Lisp_Object Vframe_title_format;
466 /* Like mode-line-format, but for the title bar on an iconified frame. */
468 Lisp_Object Vicon_title_format;
470 /* List of functions to call when a window's size changes. These
471 functions get one arg, a frame on which one or more windows' sizes
472 have changed. */
474 static Lisp_Object Vwindow_size_change_functions;
476 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
478 /* Nonzero if an overlay arrow has been displayed in this window. */
480 static int overlay_arrow_seen;
482 /* Nonzero means highlight the region even in nonselected windows. */
484 int highlight_nonselected_windows;
486 /* If cursor motion alone moves point off frame, try scrolling this
487 many lines up or down if that will bring it back. */
489 static EMACS_INT scroll_step;
491 /* Nonzero means scroll just far enough to bring point back on the
492 screen, when appropriate. */
494 static EMACS_INT scroll_conservatively;
496 /* Recenter the window whenever point gets within this many lines of
497 the top or bottom of the window. This value is translated into a
498 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
499 that there is really a fixed pixel height scroll margin. */
501 EMACS_INT scroll_margin;
503 /* Number of windows showing the buffer of the selected window (or
504 another buffer with the same base buffer). keyboard.c refers to
505 this. */
507 int buffer_shared;
509 /* Vector containing glyphs for an ellipsis `...'. */
511 static Lisp_Object default_invis_vector[3];
513 /* Zero means display the mode-line/header-line/menu-bar in the default face
514 (this slightly odd definition is for compatibility with previous versions
515 of emacs), non-zero means display them using their respective faces.
517 This variable is deprecated. */
519 int mode_line_inverse_video;
521 /* Prompt to display in front of the mini-buffer contents. */
523 Lisp_Object minibuf_prompt;
525 /* Width of current mini-buffer prompt. Only set after display_line
526 of the line that contains the prompt. */
528 int minibuf_prompt_width;
530 /* This is the window where the echo area message was displayed. It
531 is always a mini-buffer window, but it may not be the same window
532 currently active as a mini-buffer. */
534 Lisp_Object echo_area_window;
536 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
537 pushes the current message and the value of
538 message_enable_multibyte on the stack, the function restore_message
539 pops the stack and displays MESSAGE again. */
541 Lisp_Object Vmessage_stack;
543 /* Nonzero means multibyte characters were enabled when the echo area
544 message was specified. */
546 int message_enable_multibyte;
548 /* Nonzero if we should redraw the mode lines on the next redisplay. */
550 int update_mode_lines;
552 /* Nonzero if window sizes or contents have changed since last
553 redisplay that finished. */
555 int windows_or_buffers_changed;
557 /* Nonzero means a frame's cursor type has been changed. */
559 int cursor_type_changed;
561 /* Nonzero after display_mode_line if %l was used and it displayed a
562 line number. */
564 int line_number_displayed;
566 /* Maximum buffer size for which to display line numbers. */
568 Lisp_Object Vline_number_display_limit;
570 /* Line width to consider when repositioning for line number display. */
572 static EMACS_INT line_number_display_limit_width;
574 /* Number of lines to keep in the message log buffer. t means
575 infinite. nil means don't log at all. */
577 Lisp_Object Vmessage_log_max;
579 /* The name of the *Messages* buffer, a string. */
581 static Lisp_Object Vmessages_buffer_name;
583 /* Index 0 is the buffer that holds the current (desired) echo area message,
584 or nil if none is desired right now.
586 Index 1 is the buffer that holds the previously displayed echo area message,
587 or nil to indicate no message. This is normally what's on the screen now.
589 These two can point to the same buffer. That happens when the last
590 message output by the user (or made by echoing) has been displayed. */
592 Lisp_Object echo_area_buffer[2];
594 /* Permanent pointers to the two buffers that are used for echo area
595 purposes. Once the two buffers are made, and their pointers are
596 placed here, these two slots remain unchanged unless those buffers
597 need to be created afresh. */
599 static Lisp_Object echo_buffer[2];
601 /* A vector saved used in with_area_buffer to reduce consing. */
603 static Lisp_Object Vwith_echo_area_save_vector;
605 /* Non-zero means display_echo_area should display the last echo area
606 message again. Set by redisplay_preserve_echo_area. */
608 static int display_last_displayed_message_p;
610 /* Nonzero if echo area is being used by print; zero if being used by
611 message. */
613 int message_buf_print;
615 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
617 Lisp_Object Qinhibit_menubar_update;
618 int inhibit_menubar_update;
620 /* When evaluating expressions from menu bar items (enable conditions,
621 for instance), this is the frame they are being processed for. */
623 Lisp_Object Vmenu_updating_frame;
625 /* Maximum height for resizing mini-windows. Either a float
626 specifying a fraction of the available height, or an integer
627 specifying a number of lines. */
629 Lisp_Object Vmax_mini_window_height;
631 /* Non-zero means messages should be displayed with truncated
632 lines instead of being continued. */
634 int message_truncate_lines;
635 Lisp_Object Qmessage_truncate_lines;
637 /* Set to 1 in clear_message to make redisplay_internal aware
638 of an emptied echo area. */
640 static int message_cleared_p;
642 /* How to blink the default frame cursor off. */
643 Lisp_Object Vblink_cursor_alist;
645 /* A scratch glyph row with contents used for generating truncation
646 glyphs. Also used in direct_output_for_insert. */
648 #define MAX_SCRATCH_GLYPHS 100
649 struct glyph_row scratch_glyph_row;
650 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
652 /* Ascent and height of the last line processed by move_it_to. */
654 static int last_max_ascent, last_height;
656 /* Non-zero if there's a help-echo in the echo area. */
658 int help_echo_showing_p;
660 /* If >= 0, computed, exact values of mode-line and header-line height
661 to use in the macros CURRENT_MODE_LINE_HEIGHT and
662 CURRENT_HEADER_LINE_HEIGHT. */
664 int current_mode_line_height, current_header_line_height;
666 /* The maximum distance to look ahead for text properties. Values
667 that are too small let us call compute_char_face and similar
668 functions too often which is expensive. Values that are too large
669 let us call compute_char_face and alike too often because we
670 might not be interested in text properties that far away. */
672 #define TEXT_PROP_DISTANCE_LIMIT 100
674 #if GLYPH_DEBUG
676 /* Variables to turn off display optimizations from Lisp. */
678 int inhibit_try_window_id, inhibit_try_window_reusing;
679 int inhibit_try_cursor_movement;
681 /* Non-zero means print traces of redisplay if compiled with
682 GLYPH_DEBUG != 0. */
684 int trace_redisplay_p;
686 #endif /* GLYPH_DEBUG */
688 #ifdef DEBUG_TRACE_MOVE
689 /* Non-zero means trace with TRACE_MOVE to stderr. */
690 int trace_move;
692 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
693 #else
694 #define TRACE_MOVE(x) (void) 0
695 #endif
697 /* Non-zero means automatically scroll windows horizontally to make
698 point visible. */
700 int automatic_hscrolling_p;
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 *));
757 /* Properties handled by iterators. */
759 static struct props it_props[] =
761 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
762 /* Handle `face' before `display' because some sub-properties of
763 `display' need to know the face. */
764 {&Qface, FACE_PROP_IDX, handle_face_prop},
765 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
766 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
767 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
768 {NULL, 0, NULL}
771 /* Value is the position described by X. If X is a marker, value is
772 the marker_position of X. Otherwise, value is X. */
774 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
776 /* Enumeration returned by some move_it_.* functions internally. */
778 enum move_it_result
780 /* Not used. Undefined value. */
781 MOVE_UNDEFINED,
783 /* Move ended at the requested buffer position or ZV. */
784 MOVE_POS_MATCH_OR_ZV,
786 /* Move ended at the requested X pixel position. */
787 MOVE_X_REACHED,
789 /* Move within a line ended at the end of a line that must be
790 continued. */
791 MOVE_LINE_CONTINUED,
793 /* Move within a line ended at the end of a line that would
794 be displayed truncated. */
795 MOVE_LINE_TRUNCATED,
797 /* Move within a line ended at a line end. */
798 MOVE_NEWLINE_OR_CR
801 /* This counter is used to clear the face cache every once in a while
802 in redisplay_internal. It is incremented for each redisplay.
803 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
804 cleared. */
806 #define CLEAR_FACE_CACHE_COUNT 500
807 static int clear_face_cache_count;
809 /* Similarly for the image cache. */
811 #ifdef HAVE_WINDOW_SYSTEM
812 #define CLEAR_IMAGE_CACHE_COUNT 101
813 static int clear_image_cache_count;
814 #endif
816 /* Record the previous terminal frame we displayed. */
818 static struct frame *previous_terminal_frame;
820 /* Non-zero while redisplay_internal is in progress. */
822 int redisplaying_p;
824 /* Non-zero means don't free realized faces. Bound while freeing
825 realized faces is dangerous because glyph matrices might still
826 reference them. */
828 int inhibit_free_realized_faces;
829 Lisp_Object Qinhibit_free_realized_faces;
831 /* If a string, XTread_socket generates an event to display that string.
832 (The display is done in read_char.) */
834 Lisp_Object help_echo_string;
835 Lisp_Object help_echo_window;
836 Lisp_Object help_echo_object;
837 int help_echo_pos;
839 /* Temporary variable for XTread_socket. */
841 Lisp_Object previous_help_echo_string;
843 /* Null glyph slice */
845 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
848 /* Function prototypes. */
850 static void setup_for_ellipsis P_ ((struct it *, int));
851 static void mark_window_display_accurate_1 P_ ((struct window *, int));
852 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
853 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
854 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
855 static int redisplay_mode_lines P_ ((Lisp_Object, int));
856 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
858 #if 0
859 static int invisible_text_between_p P_ ((struct it *, int, int));
860 #endif
862 static void pint2str P_ ((char *, int, int));
863 static void pint2hrstr P_ ((char *, int, int));
864 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
865 struct text_pos));
866 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
867 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
868 static void store_mode_line_noprop_char P_ ((char));
869 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
870 static void x_consider_frame_title P_ ((Lisp_Object));
871 static void handle_stop P_ ((struct it *));
872 static int tool_bar_lines_needed P_ ((struct frame *, int *));
873 static int single_display_spec_intangible_p P_ ((Lisp_Object));
874 static void ensure_echo_area_buffers P_ ((void));
875 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
876 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
877 static int with_echo_area_buffer P_ ((struct window *, int,
878 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
879 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static void clear_garbaged_frames P_ ((void));
881 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
883 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
884 static int display_echo_area P_ ((struct window *));
885 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
886 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
887 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
888 static int string_char_and_length P_ ((const unsigned char *, int, int *));
889 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
890 struct text_pos));
891 static int compute_window_start_on_continuation_line P_ ((struct window *));
892 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
893 static void insert_left_trunc_glyphs P_ ((struct it *));
894 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
895 Lisp_Object));
896 static void extend_face_to_end_of_line P_ ((struct it *));
897 static int append_space_for_newline P_ ((struct it *, int));
898 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
899 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
900 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
901 static int trailing_whitespace_p P_ ((int));
902 static int message_log_check_duplicate P_ ((int, int, int, int));
903 static void push_it P_ ((struct it *));
904 static void pop_it P_ ((struct it *));
905 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
906 static void select_frame_for_redisplay P_ ((Lisp_Object));
907 static void redisplay_internal P_ ((int));
908 static int echo_area_display P_ ((int));
909 static void redisplay_windows P_ ((Lisp_Object));
910 static void redisplay_window P_ ((Lisp_Object, int));
911 static Lisp_Object redisplay_window_error ();
912 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
913 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
914 static int update_menu_bar P_ ((struct frame *, int, int));
915 static int try_window_reusing_current_matrix P_ ((struct window *));
916 static int try_window_id P_ ((struct window *));
917 static int display_line P_ ((struct it *));
918 static int display_mode_lines P_ ((struct window *));
919 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
920 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
921 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
922 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
923 static void display_menu_bar P_ ((struct window *));
924 static int display_count_lines P_ ((int, int, int, int, int *));
925 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
926 int, int, struct it *, int, int, int, int));
927 static void compute_line_metrics P_ ((struct it *));
928 static void run_redisplay_end_trigger_hook P_ ((struct it *));
929 static int get_overlay_strings P_ ((struct it *, int));
930 static int get_overlay_strings_1 P_ ((struct it *, int, int));
931 static void next_overlay_string P_ ((struct it *));
932 static void reseat P_ ((struct it *, struct text_pos, int));
933 static void reseat_1 P_ ((struct it *, struct text_pos, int));
934 static void back_to_previous_visible_line_start P_ ((struct it *));
935 void reseat_at_previous_visible_line_start P_ ((struct it *));
936 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
937 static int next_element_from_ellipsis P_ ((struct it *));
938 static int next_element_from_display_vector P_ ((struct it *));
939 static int next_element_from_string P_ ((struct it *));
940 static int next_element_from_c_string P_ ((struct it *));
941 static int next_element_from_buffer P_ ((struct it *));
942 static int next_element_from_composition P_ ((struct it *));
943 static int next_element_from_image P_ ((struct it *));
944 static int next_element_from_stretch P_ ((struct it *));
945 static void load_overlay_strings P_ ((struct it *, int));
946 static int init_from_display_pos P_ ((struct it *, struct window *,
947 struct display_pos *));
948 static void reseat_to_string P_ ((struct it *, unsigned char *,
949 Lisp_Object, int, int, int, int));
950 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
951 int, int, int));
952 void move_it_vertically_backward P_ ((struct it *, int));
953 static void init_to_row_start P_ ((struct it *, struct window *,
954 struct glyph_row *));
955 static int init_to_row_end P_ ((struct it *, struct window *,
956 struct glyph_row *));
957 static void back_to_previous_line_start P_ ((struct it *));
958 static int forward_to_next_line_start P_ ((struct it *, int *));
959 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
960 Lisp_Object, int));
961 static struct text_pos string_pos P_ ((int, Lisp_Object));
962 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
963 static int number_of_chars P_ ((unsigned char *, int));
964 static void compute_stop_pos P_ ((struct it *));
965 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
966 Lisp_Object));
967 static int face_before_or_after_it_pos P_ ((struct it *, int));
968 static int next_overlay_change P_ ((int));
969 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
970 Lisp_Object, Lisp_Object,
971 struct text_pos *, int));
972 static int underlying_face_id P_ ((struct it *));
973 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
974 struct window *));
976 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
977 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
979 #ifdef HAVE_WINDOW_SYSTEM
981 static void update_tool_bar P_ ((struct frame *, int));
982 static void build_desired_tool_bar_string P_ ((struct frame *f));
983 static int redisplay_tool_bar P_ ((struct frame *));
984 static void display_tool_bar_line P_ ((struct it *, int));
985 static void notice_overwritten_cursor P_ ((struct window *,
986 enum glyph_row_area,
987 int, int, int, int));
991 #endif /* HAVE_WINDOW_SYSTEM */
994 /***********************************************************************
995 Window display dimensions
996 ***********************************************************************/
998 /* Return the bottom boundary y-position for text lines in window W.
999 This is the first y position at which a line cannot start.
1000 It is relative to the top of the window.
1002 This is the height of W minus the height of a mode line, if any. */
1004 INLINE int
1005 window_text_bottom_y (w)
1006 struct window *w;
1008 int height = WINDOW_TOTAL_HEIGHT (w);
1010 if (WINDOW_WANTS_MODELINE_P (w))
1011 height -= CURRENT_MODE_LINE_HEIGHT (w);
1012 return height;
1015 /* Return the pixel width of display area AREA of window W. AREA < 0
1016 means return the total width of W, not including fringes to
1017 the left and right of the window. */
1019 INLINE int
1020 window_box_width (w, area)
1021 struct window *w;
1022 int area;
1024 int cols = XFASTINT (w->total_cols);
1025 int pixels = 0;
1027 if (!w->pseudo_window_p)
1029 cols -= WINDOW_SCROLL_BAR_COLS (w);
1031 if (area == TEXT_AREA)
1033 if (INTEGERP (w->left_margin_cols))
1034 cols -= XFASTINT (w->left_margin_cols);
1035 if (INTEGERP (w->right_margin_cols))
1036 cols -= XFASTINT (w->right_margin_cols);
1037 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1039 else if (area == LEFT_MARGIN_AREA)
1041 cols = (INTEGERP (w->left_margin_cols)
1042 ? XFASTINT (w->left_margin_cols) : 0);
1043 pixels = 0;
1045 else if (area == RIGHT_MARGIN_AREA)
1047 cols = (INTEGERP (w->right_margin_cols)
1048 ? XFASTINT (w->right_margin_cols) : 0);
1049 pixels = 0;
1053 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1057 /* Return the pixel height of the display area of window W, not
1058 including mode lines of W, if any. */
1060 INLINE int
1061 window_box_height (w)
1062 struct window *w;
1064 struct frame *f = XFRAME (w->frame);
1065 int height = WINDOW_TOTAL_HEIGHT (w);
1067 xassert (height >= 0);
1069 /* Note: the code below that determines the mode-line/header-line
1070 height is essentially the same as that contained in the macro
1071 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1072 the appropriate glyph row has its `mode_line_p' flag set,
1073 and if it doesn't, uses estimate_mode_line_height instead. */
1075 if (WINDOW_WANTS_MODELINE_P (w))
1077 struct glyph_row *ml_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (ml_row && ml_row->mode_line_p)
1082 height -= ml_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1087 if (WINDOW_WANTS_HEADER_LINE_P (w))
1089 struct glyph_row *hl_row
1090 = (w->current_matrix && w->current_matrix->rows
1091 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1092 : 0);
1093 if (hl_row && hl_row->mode_line_p)
1094 height -= hl_row->height;
1095 else
1096 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1099 /* With a very small font and a mode-line that's taller than
1100 default, we might end up with a negative height. */
1101 return max (0, height);
1104 /* Return the window-relative coordinate of the left edge of display
1105 area AREA of window W. AREA < 0 means return the left edge of the
1106 whole window, to the right of the left fringe of W. */
1108 INLINE int
1109 window_box_left_offset (w, area)
1110 struct window *w;
1111 int area;
1113 int x;
1115 if (w->pseudo_window_p)
1116 return 0;
1118 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1120 if (area == TEXT_AREA)
1121 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1122 + window_box_width (w, LEFT_MARGIN_AREA));
1123 else if (area == RIGHT_MARGIN_AREA)
1124 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1125 + window_box_width (w, LEFT_MARGIN_AREA)
1126 + window_box_width (w, TEXT_AREA)
1127 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1129 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1130 else if (area == LEFT_MARGIN_AREA
1131 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1132 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1134 return x;
1138 /* Return the window-relative coordinate of the right edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the left of the right fringe of W. */
1142 INLINE int
1143 window_box_right_offset (w, area)
1144 struct window *w;
1145 int area;
1147 return window_box_left_offset (w, area) + window_box_width (w, area);
1150 /* Return the frame-relative coordinate of the left edge of display
1151 area AREA of window W. AREA < 0 means return the left edge of the
1152 whole window, to the right of the left fringe of W. */
1154 INLINE int
1155 window_box_left (w, area)
1156 struct window *w;
1157 int area;
1159 struct frame *f = XFRAME (w->frame);
1160 int x;
1162 if (w->pseudo_window_p)
1163 return FRAME_INTERNAL_BORDER_WIDTH (f);
1165 x = (WINDOW_LEFT_EDGE_X (w)
1166 + window_box_left_offset (w, area));
1168 return x;
1172 /* Return the frame-relative coordinate of the right edge of display
1173 area AREA of window W. AREA < 0 means return the left edge of the
1174 whole window, to the left of the right fringe of W. */
1176 INLINE int
1177 window_box_right (w, area)
1178 struct window *w;
1179 int area;
1181 return window_box_left (w, area) + window_box_width (w, area);
1184 /* Get the bounding box of the display area AREA of window W, without
1185 mode lines, in frame-relative coordinates. AREA < 0 means the
1186 whole window, not including the left and right fringes of
1187 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1188 coordinates of the upper-left corner of the box. Return in
1189 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1191 INLINE void
1192 window_box (w, area, box_x, box_y, box_width, box_height)
1193 struct window *w;
1194 int area;
1195 int *box_x, *box_y, *box_width, *box_height;
1197 if (box_width)
1198 *box_width = window_box_width (w, area);
1199 if (box_height)
1200 *box_height = window_box_height (w);
1201 if (box_x)
1202 *box_x = window_box_left (w, area);
1203 if (box_y)
1205 *box_y = WINDOW_TOP_EDGE_Y (w);
1206 if (WINDOW_WANTS_HEADER_LINE_P (w))
1207 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1212 /* Get the bounding box of the display area AREA of window W, without
1213 mode lines. AREA < 0 means the whole window, not including the
1214 left and right fringe of the window. Return in *TOP_LEFT_X
1215 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1216 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1217 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1218 box. */
1220 INLINE void
1221 window_box_edges (w, area, top_left_x, top_left_y,
1222 bottom_right_x, bottom_right_y)
1223 struct window *w;
1224 int area;
1225 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1227 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1228 bottom_right_y);
1229 *bottom_right_x += *top_left_x;
1230 *bottom_right_y += *top_left_y;
1235 /***********************************************************************
1236 Utilities
1237 ***********************************************************************/
1239 /* Return the bottom y-position of the line the iterator IT is in.
1240 This can modify IT's settings. */
1243 line_bottom_y (it)
1244 struct it *it;
1246 int line_height = it->max_ascent + it->max_descent;
1247 int line_top_y = it->current_y;
1249 if (line_height == 0)
1251 if (last_height)
1252 line_height = last_height;
1253 else if (IT_CHARPOS (*it) < ZV)
1255 move_it_by_lines (it, 1, 1);
1256 line_height = (it->max_ascent || it->max_descent
1257 ? it->max_ascent + it->max_descent
1258 : last_height);
1260 else
1262 struct glyph_row *row = it->glyph_row;
1264 /* Use the default character height. */
1265 it->glyph_row = NULL;
1266 it->what = IT_CHARACTER;
1267 it->c = ' ';
1268 it->len = 1;
1269 PRODUCE_GLYPHS (it);
1270 line_height = it->ascent + it->descent;
1271 it->glyph_row = row;
1275 return line_top_y + line_height;
1279 /* Return 1 if position CHARPOS is visible in window W.
1280 CHARPOS < 0 means return info about WINDOW_END position.
1281 If visible, set *X and *Y to pixel coordinates of top left corner.
1282 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1283 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1286 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1287 struct window *w;
1288 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1290 struct it it;
1291 struct text_pos top;
1292 int visible_p = 0;
1293 struct buffer *old_buffer = NULL;
1295 if (noninteractive)
1296 return visible_p;
1298 if (XBUFFER (w->buffer) != current_buffer)
1300 old_buffer = current_buffer;
1301 set_buffer_internal_1 (XBUFFER (w->buffer));
1304 SET_TEXT_POS_FROM_MARKER (top, w->start);
1306 /* Compute exact mode line heights. */
1307 if (WINDOW_WANTS_MODELINE_P (w))
1308 current_mode_line_height
1309 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1310 current_buffer->mode_line_format);
1312 if (WINDOW_WANTS_HEADER_LINE_P (w))
1313 current_header_line_height
1314 = display_mode_line (w, HEADER_LINE_FACE_ID,
1315 current_buffer->header_line_format);
1317 start_display (&it, w, top);
1318 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1319 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1321 /* Note that we may overshoot because of invisible text. */
1322 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1324 int top_x = it.current_x;
1325 int top_y = it.current_y;
1326 int bottom_y = (last_height = 0, line_bottom_y (&it));
1327 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1329 if (top_y < window_top_y)
1330 visible_p = bottom_y > window_top_y;
1331 else if (top_y < it.last_visible_y)
1332 visible_p = 1;
1333 if (visible_p)
1335 *x = top_x;
1336 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1337 *rtop = max (0, window_top_y - top_y);
1338 *rbot = max (0, bottom_y - it.last_visible_y);
1339 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1340 - max (top_y, window_top_y)));
1341 *vpos = it.vpos;
1344 else
1346 struct it it2;
1348 it2 = it;
1349 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1350 move_it_by_lines (&it, 1, 0);
1351 if (charpos < IT_CHARPOS (it)
1352 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1354 visible_p = 1;
1355 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1356 *x = it2.current_x;
1357 *y = it2.current_y + it2.max_ascent - it2.ascent;
1358 *rtop = max (0, -it2.current_y);
1359 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1360 - it.last_visible_y));
1361 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1362 it.last_visible_y)
1363 - max (it2.current_y,
1364 WINDOW_HEADER_LINE_HEIGHT (w))));
1365 *vpos = it2.vpos;
1369 if (old_buffer)
1370 set_buffer_internal_1 (old_buffer);
1372 current_header_line_height = current_mode_line_height = -1;
1374 if (visible_p && XFASTINT (w->hscroll) > 0)
1375 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1377 #if 0
1378 /* Debugging code. */
1379 if (visible_p)
1380 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1381 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1382 else
1383 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1384 #endif
1386 return visible_p;
1390 /* Return the next character from STR which is MAXLEN bytes long.
1391 Return in *LEN the length of the character. This is like
1392 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1393 we find one, we return a `?', but with the length of the invalid
1394 character. */
1396 static INLINE int
1397 string_char_and_length (str, maxlen, len)
1398 const unsigned char *str;
1399 int maxlen, *len;
1401 int c;
1403 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1404 if (!CHAR_VALID_P (c, 1))
1405 /* We may not change the length here because other places in Emacs
1406 don't use this function, i.e. they silently accept invalid
1407 characters. */
1408 c = '?';
1410 return c;
1415 /* Given a position POS containing a valid character and byte position
1416 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1418 static struct text_pos
1419 string_pos_nchars_ahead (pos, string, nchars)
1420 struct text_pos pos;
1421 Lisp_Object string;
1422 int nchars;
1424 xassert (STRINGP (string) && nchars >= 0);
1426 if (STRING_MULTIBYTE (string))
1428 int rest = SBYTES (string) - BYTEPOS (pos);
1429 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1430 int len;
1432 while (nchars--)
1434 string_char_and_length (p, rest, &len);
1435 p += len, rest -= len;
1436 xassert (rest >= 0);
1437 CHARPOS (pos) += 1;
1438 BYTEPOS (pos) += len;
1441 else
1442 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1444 return pos;
1448 /* Value is the text position, i.e. character and byte position,
1449 for character position CHARPOS in STRING. */
1451 static INLINE struct text_pos
1452 string_pos (charpos, string)
1453 int charpos;
1454 Lisp_Object string;
1456 struct text_pos pos;
1457 xassert (STRINGP (string));
1458 xassert (charpos >= 0);
1459 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1460 return pos;
1464 /* Value is a text position, i.e. character and byte position, for
1465 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1466 means recognize multibyte characters. */
1468 static struct text_pos
1469 c_string_pos (charpos, s, multibyte_p)
1470 int charpos;
1471 unsigned char *s;
1472 int multibyte_p;
1474 struct text_pos pos;
1476 xassert (s != NULL);
1477 xassert (charpos >= 0);
1479 if (multibyte_p)
1481 int rest = strlen (s), len;
1483 SET_TEXT_POS (pos, 0, 0);
1484 while (charpos--)
1486 string_char_and_length (s, rest, &len);
1487 s += len, rest -= len;
1488 xassert (rest >= 0);
1489 CHARPOS (pos) += 1;
1490 BYTEPOS (pos) += len;
1493 else
1494 SET_TEXT_POS (pos, charpos, charpos);
1496 return pos;
1500 /* Value is the number of characters in C string S. MULTIBYTE_P
1501 non-zero means recognize multibyte characters. */
1503 static int
1504 number_of_chars (s, multibyte_p)
1505 unsigned char *s;
1506 int multibyte_p;
1508 int nchars;
1510 if (multibyte_p)
1512 int rest = strlen (s), len;
1513 unsigned char *p = (unsigned char *) s;
1515 for (nchars = 0; rest > 0; ++nchars)
1517 string_char_and_length (p, rest, &len);
1518 rest -= len, p += len;
1521 else
1522 nchars = strlen (s);
1524 return nchars;
1528 /* Compute byte position NEWPOS->bytepos corresponding to
1529 NEWPOS->charpos. POS is a known position in string STRING.
1530 NEWPOS->charpos must be >= POS.charpos. */
1532 static void
1533 compute_string_pos (newpos, pos, string)
1534 struct text_pos *newpos, pos;
1535 Lisp_Object string;
1537 xassert (STRINGP (string));
1538 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1540 if (STRING_MULTIBYTE (string))
1541 *newpos = string_pos_nchars_ahead (pos, string,
1542 CHARPOS (*newpos) - CHARPOS (pos));
1543 else
1544 BYTEPOS (*newpos) = CHARPOS (*newpos);
1547 /* EXPORT:
1548 Return an estimation of the pixel height of mode or top lines on
1549 frame F. FACE_ID specifies what line's height to estimate. */
1552 estimate_mode_line_height (f, face_id)
1553 struct frame *f;
1554 enum face_id face_id;
1556 #ifdef HAVE_WINDOW_SYSTEM
1557 if (FRAME_WINDOW_P (f))
1559 int height = FONT_HEIGHT (FRAME_FONT (f));
1561 /* This function is called so early when Emacs starts that the face
1562 cache and mode line face are not yet initialized. */
1563 if (FRAME_FACE_CACHE (f))
1565 struct face *face = FACE_FROM_ID (f, face_id);
1566 if (face)
1568 if (face->font)
1569 height = FONT_HEIGHT (face->font);
1570 if (face->box_line_width > 0)
1571 height += 2 * face->box_line_width;
1575 return height;
1577 #endif
1579 return 1;
1582 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1583 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1584 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1585 not force the value into range. */
1587 void
1588 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1589 FRAME_PTR f;
1590 register int pix_x, pix_y;
1591 int *x, *y;
1592 NativeRectangle *bounds;
1593 int noclip;
1596 #ifdef HAVE_WINDOW_SYSTEM
1597 if (FRAME_WINDOW_P (f))
1599 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1600 even for negative values. */
1601 if (pix_x < 0)
1602 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1603 if (pix_y < 0)
1604 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1606 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1607 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1609 if (bounds)
1610 STORE_NATIVE_RECT (*bounds,
1611 FRAME_COL_TO_PIXEL_X (f, pix_x),
1612 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1613 FRAME_COLUMN_WIDTH (f) - 1,
1614 FRAME_LINE_HEIGHT (f) - 1);
1616 if (!noclip)
1618 if (pix_x < 0)
1619 pix_x = 0;
1620 else if (pix_x > FRAME_TOTAL_COLS (f))
1621 pix_x = FRAME_TOTAL_COLS (f);
1623 if (pix_y < 0)
1624 pix_y = 0;
1625 else if (pix_y > FRAME_LINES (f))
1626 pix_y = FRAME_LINES (f);
1629 #endif
1631 *x = pix_x;
1632 *y = pix_y;
1636 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1637 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1638 can't tell the positions because W's display is not up to date,
1639 return 0. */
1642 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1643 struct window *w;
1644 int hpos, vpos;
1645 int *frame_x, *frame_y;
1647 #ifdef HAVE_WINDOW_SYSTEM
1648 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1650 int success_p;
1652 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1653 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1655 if (display_completed)
1657 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1658 struct glyph *glyph = row->glyphs[TEXT_AREA];
1659 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1661 hpos = row->x;
1662 vpos = row->y;
1663 while (glyph < end)
1665 hpos += glyph->pixel_width;
1666 ++glyph;
1669 /* If first glyph is partially visible, its first visible position is still 0. */
1670 if (hpos < 0)
1671 hpos = 0;
1673 success_p = 1;
1675 else
1677 hpos = vpos = 0;
1678 success_p = 0;
1681 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1682 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1683 return success_p;
1685 #endif
1687 *frame_x = hpos;
1688 *frame_y = vpos;
1689 return 1;
1693 #ifdef HAVE_WINDOW_SYSTEM
1695 /* Find the glyph under window-relative coordinates X/Y in window W.
1696 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1697 strings. Return in *HPOS and *VPOS the row and column number of
1698 the glyph found. Return in *AREA the glyph area containing X.
1699 Value is a pointer to the glyph found or null if X/Y is not on
1700 text, or we can't tell because W's current matrix is not up to
1701 date. */
1703 static struct glyph *
1704 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1705 struct window *w;
1706 int x, y;
1707 int *hpos, *vpos, *dx, *dy, *area;
1709 struct glyph *glyph, *end;
1710 struct glyph_row *row = NULL;
1711 int x0, i;
1713 /* Find row containing Y. Give up if some row is not enabled. */
1714 for (i = 0; i < w->current_matrix->nrows; ++i)
1716 row = MATRIX_ROW (w->current_matrix, i);
1717 if (!row->enabled_p)
1718 return NULL;
1719 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1720 break;
1723 *vpos = i;
1724 *hpos = 0;
1726 /* Give up if Y is not in the window. */
1727 if (i == w->current_matrix->nrows)
1728 return NULL;
1730 /* Get the glyph area containing X. */
1731 if (w->pseudo_window_p)
1733 *area = TEXT_AREA;
1734 x0 = 0;
1736 else
1738 if (x < window_box_left_offset (w, TEXT_AREA))
1740 *area = LEFT_MARGIN_AREA;
1741 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1743 else if (x < window_box_right_offset (w, TEXT_AREA))
1745 *area = TEXT_AREA;
1746 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1748 else
1750 *area = RIGHT_MARGIN_AREA;
1751 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1755 /* Find glyph containing X. */
1756 glyph = row->glyphs[*area];
1757 end = glyph + row->used[*area];
1758 x -= x0;
1759 while (glyph < end && x >= glyph->pixel_width)
1761 x -= glyph->pixel_width;
1762 ++glyph;
1765 if (glyph == end)
1766 return NULL;
1768 if (dx)
1770 *dx = x;
1771 *dy = y - (row->y + row->ascent - glyph->ascent);
1774 *hpos = glyph - row->glyphs[*area];
1775 return glyph;
1779 /* EXPORT:
1780 Convert frame-relative x/y to coordinates relative to window W.
1781 Takes pseudo-windows into account. */
1783 void
1784 frame_to_window_pixel_xy (w, x, y)
1785 struct window *w;
1786 int *x, *y;
1788 if (w->pseudo_window_p)
1790 /* A pseudo-window is always full-width, and starts at the
1791 left edge of the frame, plus a frame border. */
1792 struct frame *f = XFRAME (w->frame);
1793 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1794 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1796 else
1798 *x -= WINDOW_LEFT_EDGE_X (w);
1799 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1803 /* EXPORT:
1804 Return in RECTS[] at most N clipping rectangles for glyph string S.
1805 Return the number of stored rectangles. */
1808 get_glyph_string_clip_rects (s, rects, n)
1809 struct glyph_string *s;
1810 NativeRectangle *rects;
1811 int n;
1813 XRectangle r;
1815 if (n <= 0)
1816 return 0;
1818 if (s->row->full_width_p)
1820 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1821 r.x = WINDOW_LEFT_EDGE_X (s->w);
1822 r.width = WINDOW_TOTAL_WIDTH (s->w);
1824 /* Unless displaying a mode or menu bar line, which are always
1825 fully visible, clip to the visible part of the row. */
1826 if (s->w->pseudo_window_p)
1827 r.height = s->row->visible_height;
1828 else
1829 r.height = s->height;
1831 else
1833 /* This is a text line that may be partially visible. */
1834 r.x = window_box_left (s->w, s->area);
1835 r.width = window_box_width (s->w, s->area);
1836 r.height = s->row->visible_height;
1839 if (s->clip_head)
1840 if (r.x < s->clip_head->x)
1842 if (r.width >= s->clip_head->x - r.x)
1843 r.width -= s->clip_head->x - r.x;
1844 else
1845 r.width = 0;
1846 r.x = s->clip_head->x;
1848 if (s->clip_tail)
1849 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1851 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1852 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1853 else
1854 r.width = 0;
1857 /* If S draws overlapping rows, it's sufficient to use the top and
1858 bottom of the window for clipping because this glyph string
1859 intentionally draws over other lines. */
1860 if (s->for_overlaps)
1862 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1863 r.height = window_text_bottom_y (s->w) - r.y;
1865 /* Alas, the above simple strategy does not work for the
1866 environments with anti-aliased text: if the same text is
1867 drawn onto the same place multiple times, it gets thicker.
1868 If the overlap we are processing is for the erased cursor, we
1869 take the intersection with the rectagle of the cursor. */
1870 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1872 XRectangle rc, r_save = r;
1874 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1875 rc.y = s->w->phys_cursor.y;
1876 rc.width = s->w->phys_cursor_width;
1877 rc.height = s->w->phys_cursor_height;
1879 x_intersect_rectangles (&r_save, &rc, &r);
1882 else
1884 /* Don't use S->y for clipping because it doesn't take partially
1885 visible lines into account. For example, it can be negative for
1886 partially visible lines at the top of a window. */
1887 if (!s->row->full_width_p
1888 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1889 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1890 else
1891 r.y = max (0, s->row->y);
1893 /* If drawing a tool-bar window, draw it over the internal border
1894 at the top of the window. */
1895 if (WINDOWP (s->f->tool_bar_window)
1896 && s->w == XWINDOW (s->f->tool_bar_window))
1897 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1900 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1902 /* If drawing the cursor, don't let glyph draw outside its
1903 advertised boundaries. Cleartype does this under some circumstances. */
1904 if (s->hl == DRAW_CURSOR)
1906 struct glyph *glyph = s->first_glyph;
1907 int height, max_y;
1909 if (s->x > r.x)
1911 r.width -= s->x - r.x;
1912 r.x = s->x;
1914 r.width = min (r.width, glyph->pixel_width);
1916 /* If r.y is below window bottom, ensure that we still see a cursor. */
1917 height = min (glyph->ascent + glyph->descent,
1918 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1919 max_y = window_text_bottom_y (s->w) - height;
1920 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1921 if (s->ybase - glyph->ascent > max_y)
1923 r.y = max_y;
1924 r.height = height;
1926 else
1928 /* Don't draw cursor glyph taller than our actual glyph. */
1929 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1930 if (height < r.height)
1932 max_y = r.y + r.height;
1933 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1934 r.height = min (max_y - r.y, height);
1939 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1940 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1942 #ifdef CONVERT_FROM_XRECT
1943 CONVERT_FROM_XRECT (r, *rects);
1944 #else
1945 *rects = r;
1946 #endif
1947 return 1;
1949 else
1951 /* If we are processing overlapping and allowed to return
1952 multiple clipping rectangles, we exclude the row of the glyph
1953 string from the clipping rectangle. This is to avoid drawing
1954 the same text on the environment with anti-aliasing. */
1955 #ifdef CONVERT_FROM_XRECT
1956 XRectangle rs[2];
1957 #else
1958 XRectangle *rs = rects;
1959 #endif
1960 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1962 if (s->for_overlaps & OVERLAPS_PRED)
1964 rs[i] = r;
1965 if (r.y + r.height > row_y)
1967 if (r.y < row_y)
1968 rs[i].height = row_y - r.y;
1969 else
1970 rs[i].height = 0;
1972 i++;
1974 if (s->for_overlaps & OVERLAPS_SUCC)
1976 rs[i] = r;
1977 if (r.y < row_y + s->row->visible_height)
1979 if (r.y + r.height > row_y + s->row->visible_height)
1981 rs[i].y = row_y + s->row->visible_height;
1982 rs[i].height = r.y + r.height - rs[i].y;
1984 else
1985 rs[i].height = 0;
1987 i++;
1990 n = i;
1991 #ifdef CONVERT_FROM_XRECT
1992 for (i = 0; i < n; i++)
1993 CONVERT_FROM_XRECT (rs[i], rects[i]);
1994 #endif
1995 return n;
1999 /* EXPORT:
2000 Return in *NR the clipping rectangle for glyph string S. */
2002 void
2003 get_glyph_string_clip_rect (s, nr)
2004 struct glyph_string *s;
2005 NativeRectangle *nr;
2007 get_glyph_string_clip_rects (s, nr, 1);
2011 /* EXPORT:
2012 Return the position and height of the phys cursor in window W.
2013 Set w->phys_cursor_width to width of phys cursor.
2016 void
2017 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2018 struct window *w;
2019 struct glyph_row *row;
2020 struct glyph *glyph;
2021 int *xp, *yp, *heightp;
2023 struct frame *f = XFRAME (WINDOW_FRAME (w));
2024 int x, y, wd, h, h0, y0;
2026 /* Compute the width of the rectangle to draw. If on a stretch
2027 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2028 rectangle as wide as the glyph, but use a canonical character
2029 width instead. */
2030 wd = glyph->pixel_width - 1;
2031 #ifdef HAVE_NTGUI
2032 wd++; /* Why? */
2033 #endif
2035 x = w->phys_cursor.x;
2036 if (x < 0)
2038 wd += x;
2039 x = 0;
2042 if (glyph->type == STRETCH_GLYPH
2043 && !x_stretch_cursor_p)
2044 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2045 w->phys_cursor_width = wd;
2047 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2049 /* If y is below window bottom, ensure that we still see a cursor. */
2050 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2052 h = max (h0, glyph->ascent + glyph->descent);
2053 h0 = min (h0, glyph->ascent + glyph->descent);
2055 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2056 if (y < y0)
2058 h = max (h - (y0 - y) + 1, h0);
2059 y = y0 - 1;
2061 else
2063 y0 = window_text_bottom_y (w) - h0;
2064 if (y > y0)
2066 h += y - y0;
2067 y = y0;
2071 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2072 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2073 *heightp = h;
2077 * Remember which glyph the mouse is over.
2080 void
2081 remember_mouse_glyph (f, gx, gy, rect)
2082 struct frame *f;
2083 int gx, gy;
2084 NativeRectangle *rect;
2086 Lisp_Object window;
2087 struct window *w;
2088 struct glyph_row *r, *gr, *end_row;
2089 enum window_part part;
2090 enum glyph_row_area area;
2091 int x, y, width, height;
2093 /* Try to determine frame pixel position and size of the glyph under
2094 frame pixel coordinates X/Y on frame F. */
2096 if (!f->glyphs_initialized_p
2097 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2098 NILP (window)))
2100 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2101 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2102 goto virtual_glyph;
2105 w = XWINDOW (window);
2106 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2107 height = WINDOW_FRAME_LINE_HEIGHT (w);
2109 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2110 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2112 if (w->pseudo_window_p)
2114 area = TEXT_AREA;
2115 part = ON_MODE_LINE; /* Don't adjust margin. */
2116 goto text_glyph;
2119 switch (part)
2121 case ON_LEFT_MARGIN:
2122 area = LEFT_MARGIN_AREA;
2123 goto text_glyph;
2125 case ON_RIGHT_MARGIN:
2126 area = RIGHT_MARGIN_AREA;
2127 goto text_glyph;
2129 case ON_HEADER_LINE:
2130 case ON_MODE_LINE:
2131 gr = (part == ON_HEADER_LINE
2132 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2133 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2134 gy = gr->y;
2135 area = TEXT_AREA;
2136 goto text_glyph_row_found;
2138 case ON_TEXT:
2139 area = TEXT_AREA;
2141 text_glyph:
2142 gr = 0; gy = 0;
2143 for (; r <= end_row && r->enabled_p; ++r)
2144 if (r->y + r->height > y)
2146 gr = r; gy = r->y;
2147 break;
2150 text_glyph_row_found:
2151 if (gr && gy <= y)
2153 struct glyph *g = gr->glyphs[area];
2154 struct glyph *end = g + gr->used[area];
2156 height = gr->height;
2157 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2158 if (gx + g->pixel_width > x)
2159 break;
2161 if (g < end)
2163 if (g->type == IMAGE_GLYPH)
2165 /* Don't remember when mouse is over image, as
2166 image may have hot-spots. */
2167 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2168 return;
2170 width = g->pixel_width;
2172 else
2174 /* Use nominal char spacing at end of line. */
2175 x -= gx;
2176 gx += (x / width) * width;
2179 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2180 gx += window_box_left_offset (w, area);
2182 else
2184 /* Use nominal line height at end of window. */
2185 gx = (x / width) * width;
2186 y -= gy;
2187 gy += (y / height) * height;
2189 break;
2191 case ON_LEFT_FRINGE:
2192 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2193 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2194 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2195 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2196 goto row_glyph;
2198 case ON_RIGHT_FRINGE:
2199 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2200 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2201 : window_box_right_offset (w, TEXT_AREA));
2202 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2203 goto row_glyph;
2205 case ON_SCROLL_BAR:
2206 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2208 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2209 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2210 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2211 : 0)));
2212 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2214 row_glyph:
2215 gr = 0, gy = 0;
2216 for (; r <= end_row && r->enabled_p; ++r)
2217 if (r->y + r->height > y)
2219 gr = r; gy = r->y;
2220 break;
2223 if (gr && gy <= y)
2224 height = gr->height;
2225 else
2227 /* Use nominal line height at end of window. */
2228 y -= gy;
2229 gy += (y / height) * height;
2231 break;
2233 default:
2235 virtual_glyph:
2236 /* If there is no glyph under the mouse, then we divide the screen
2237 into a grid of the smallest glyph in the frame, and use that
2238 as our "glyph". */
2240 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2241 round down even for negative values. */
2242 if (gx < 0)
2243 gx -= width - 1;
2244 if (gy < 0)
2245 gy -= height - 1;
2247 gx = (gx / width) * width;
2248 gy = (gy / height) * height;
2250 goto store_rect;
2253 gx += WINDOW_LEFT_EDGE_X (w);
2254 gy += WINDOW_TOP_EDGE_Y (w);
2256 store_rect:
2257 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2259 /* Visible feedback for debugging. */
2260 #if 0
2261 #if HAVE_X_WINDOWS
2262 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2263 f->output_data.x->normal_gc,
2264 gx, gy, width, height);
2265 #endif
2266 #endif
2270 #endif /* HAVE_WINDOW_SYSTEM */
2273 /***********************************************************************
2274 Lisp form evaluation
2275 ***********************************************************************/
2277 /* Error handler for safe_eval and safe_call. */
2279 static Lisp_Object
2280 safe_eval_handler (arg)
2281 Lisp_Object arg;
2283 add_to_log ("Error during redisplay: %s", arg, Qnil);
2284 return Qnil;
2288 /* Evaluate SEXPR and return the result, or nil if something went
2289 wrong. Prevent redisplay during the evaluation. */
2291 Lisp_Object
2292 safe_eval (sexpr)
2293 Lisp_Object sexpr;
2295 Lisp_Object val;
2297 if (inhibit_eval_during_redisplay)
2298 val = Qnil;
2299 else
2301 int count = SPECPDL_INDEX ();
2302 struct gcpro gcpro1;
2304 GCPRO1 (sexpr);
2305 specbind (Qinhibit_redisplay, Qt);
2306 /* Use Qt to ensure debugger does not run,
2307 so there is no possibility of wanting to redisplay. */
2308 val = internal_condition_case_1 (Feval, sexpr, Qt,
2309 safe_eval_handler);
2310 UNGCPRO;
2311 val = unbind_to (count, val);
2314 return val;
2318 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2319 Return the result, or nil if something went wrong. Prevent
2320 redisplay during the evaluation. */
2322 Lisp_Object
2323 safe_call (nargs, args)
2324 int nargs;
2325 Lisp_Object *args;
2327 Lisp_Object val;
2329 if (inhibit_eval_during_redisplay)
2330 val = Qnil;
2331 else
2333 int count = SPECPDL_INDEX ();
2334 struct gcpro gcpro1;
2336 GCPRO1 (args[0]);
2337 gcpro1.nvars = nargs;
2338 specbind (Qinhibit_redisplay, Qt);
2339 /* Use Qt to ensure debugger does not run,
2340 so there is no possibility of wanting to redisplay. */
2341 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2342 safe_eval_handler);
2343 UNGCPRO;
2344 val = unbind_to (count, val);
2347 return val;
2351 /* Call function FN with one argument ARG.
2352 Return the result, or nil if something went wrong. */
2354 Lisp_Object
2355 safe_call1 (fn, arg)
2356 Lisp_Object fn, arg;
2358 Lisp_Object args[2];
2359 args[0] = fn;
2360 args[1] = arg;
2361 return safe_call (2, args);
2366 /***********************************************************************
2367 Debugging
2368 ***********************************************************************/
2370 #if 0
2372 /* Define CHECK_IT to perform sanity checks on iterators.
2373 This is for debugging. It is too slow to do unconditionally. */
2375 static void
2376 check_it (it)
2377 struct it *it;
2379 if (it->method == GET_FROM_STRING)
2381 xassert (STRINGP (it->string));
2382 xassert (IT_STRING_CHARPOS (*it) >= 0);
2384 else
2386 xassert (IT_STRING_CHARPOS (*it) < 0);
2387 if (it->method == GET_FROM_BUFFER)
2389 /* Check that character and byte positions agree. */
2390 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2394 if (it->dpvec)
2395 xassert (it->current.dpvec_index >= 0);
2396 else
2397 xassert (it->current.dpvec_index < 0);
2400 #define CHECK_IT(IT) check_it ((IT))
2402 #else /* not 0 */
2404 #define CHECK_IT(IT) (void) 0
2406 #endif /* not 0 */
2409 #if GLYPH_DEBUG
2411 /* Check that the window end of window W is what we expect it
2412 to be---the last row in the current matrix displaying text. */
2414 static void
2415 check_window_end (w)
2416 struct window *w;
2418 if (!MINI_WINDOW_P (w)
2419 && !NILP (w->window_end_valid))
2421 struct glyph_row *row;
2422 xassert ((row = MATRIX_ROW (w->current_matrix,
2423 XFASTINT (w->window_end_vpos)),
2424 !row->enabled_p
2425 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2426 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2430 #define CHECK_WINDOW_END(W) check_window_end ((W))
2432 #else /* not GLYPH_DEBUG */
2434 #define CHECK_WINDOW_END(W) (void) 0
2436 #endif /* not GLYPH_DEBUG */
2440 /***********************************************************************
2441 Iterator initialization
2442 ***********************************************************************/
2444 /* Initialize IT for displaying current_buffer in window W, starting
2445 at character position CHARPOS. CHARPOS < 0 means that no buffer
2446 position is specified which is useful when the iterator is assigned
2447 a position later. BYTEPOS is the byte position corresponding to
2448 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2450 If ROW is not null, calls to produce_glyphs with IT as parameter
2451 will produce glyphs in that row.
2453 BASE_FACE_ID is the id of a base face to use. It must be one of
2454 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2455 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2456 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2458 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2459 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2460 will be initialized to use the corresponding mode line glyph row of
2461 the desired matrix of W. */
2463 void
2464 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2465 struct it *it;
2466 struct window *w;
2467 int charpos, bytepos;
2468 struct glyph_row *row;
2469 enum face_id base_face_id;
2471 int highlight_region_p;
2473 /* Some precondition checks. */
2474 xassert (w != NULL && it != NULL);
2475 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2476 && charpos <= ZV));
2478 /* If face attributes have been changed since the last redisplay,
2479 free realized faces now because they depend on face definitions
2480 that might have changed. Don't free faces while there might be
2481 desired matrices pending which reference these faces. */
2482 if (face_change_count && !inhibit_free_realized_faces)
2484 face_change_count = 0;
2485 free_all_realized_faces (Qnil);
2488 /* Use one of the mode line rows of W's desired matrix if
2489 appropriate. */
2490 if (row == NULL)
2492 if (base_face_id == MODE_LINE_FACE_ID
2493 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2494 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2495 else if (base_face_id == HEADER_LINE_FACE_ID)
2496 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2499 /* Clear IT. */
2500 bzero (it, sizeof *it);
2501 it->current.overlay_string_index = -1;
2502 it->current.dpvec_index = -1;
2503 it->base_face_id = base_face_id;
2504 it->string = Qnil;
2505 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2507 /* The window in which we iterate over current_buffer: */
2508 XSETWINDOW (it->window, w);
2509 it->w = w;
2510 it->f = XFRAME (w->frame);
2512 /* Extra space between lines (on window systems only). */
2513 if (base_face_id == DEFAULT_FACE_ID
2514 && FRAME_WINDOW_P (it->f))
2516 if (NATNUMP (current_buffer->extra_line_spacing))
2517 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2518 else if (FLOATP (current_buffer->extra_line_spacing))
2519 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2520 * FRAME_LINE_HEIGHT (it->f));
2521 else if (it->f->extra_line_spacing > 0)
2522 it->extra_line_spacing = it->f->extra_line_spacing;
2523 it->max_extra_line_spacing = 0;
2526 /* If realized faces have been removed, e.g. because of face
2527 attribute changes of named faces, recompute them. When running
2528 in batch mode, the face cache of Vterminal_frame is null. If
2529 we happen to get called, make a dummy face cache. */
2530 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2531 init_frame_faces (it->f);
2532 if (FRAME_FACE_CACHE (it->f)->used == 0)
2533 recompute_basic_faces (it->f);
2535 /* Current value of the `slice', `space-width', and 'height' properties. */
2536 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2537 it->space_width = Qnil;
2538 it->font_height = Qnil;
2539 it->override_ascent = -1;
2541 /* Are control characters displayed as `^C'? */
2542 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2544 /* -1 means everything between a CR and the following line end
2545 is invisible. >0 means lines indented more than this value are
2546 invisible. */
2547 it->selective = (INTEGERP (current_buffer->selective_display)
2548 ? XFASTINT (current_buffer->selective_display)
2549 : (!NILP (current_buffer->selective_display)
2550 ? -1 : 0));
2551 it->selective_display_ellipsis_p
2552 = !NILP (current_buffer->selective_display_ellipses);
2554 /* Display table to use. */
2555 it->dp = window_display_table (w);
2557 /* Are multibyte characters enabled in current_buffer? */
2558 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2560 /* Non-zero if we should highlight the region. */
2561 highlight_region_p
2562 = (!NILP (Vtransient_mark_mode)
2563 && !NILP (current_buffer->mark_active)
2564 && XMARKER (current_buffer->mark)->buffer != 0);
2566 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2567 start and end of a visible region in window IT->w. Set both to
2568 -1 to indicate no region. */
2569 if (highlight_region_p
2570 /* Maybe highlight only in selected window. */
2571 && (/* Either show region everywhere. */
2572 highlight_nonselected_windows
2573 /* Or show region in the selected window. */
2574 || w == XWINDOW (selected_window)
2575 /* Or show the region if we are in the mini-buffer and W is
2576 the window the mini-buffer refers to. */
2577 || (MINI_WINDOW_P (XWINDOW (selected_window))
2578 && WINDOWP (minibuf_selected_window)
2579 && w == XWINDOW (minibuf_selected_window))))
2581 int charpos = marker_position (current_buffer->mark);
2582 it->region_beg_charpos = min (PT, charpos);
2583 it->region_end_charpos = max (PT, charpos);
2585 else
2586 it->region_beg_charpos = it->region_end_charpos = -1;
2588 /* Get the position at which the redisplay_end_trigger hook should
2589 be run, if it is to be run at all. */
2590 if (MARKERP (w->redisplay_end_trigger)
2591 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2592 it->redisplay_end_trigger_charpos
2593 = marker_position (w->redisplay_end_trigger);
2594 else if (INTEGERP (w->redisplay_end_trigger))
2595 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2597 /* Correct bogus values of tab_width. */
2598 it->tab_width = XINT (current_buffer->tab_width);
2599 if (it->tab_width <= 0 || it->tab_width > 1000)
2600 it->tab_width = 8;
2602 /* Are lines in the display truncated? */
2603 it->truncate_lines_p
2604 = (base_face_id != DEFAULT_FACE_ID
2605 || XINT (it->w->hscroll)
2606 || (truncate_partial_width_windows
2607 && !WINDOW_FULL_WIDTH_P (it->w))
2608 || !NILP (current_buffer->truncate_lines));
2610 /* Get dimensions of truncation and continuation glyphs. These are
2611 displayed as fringe bitmaps under X, so we don't need them for such
2612 frames. */
2613 if (!FRAME_WINDOW_P (it->f))
2615 if (it->truncate_lines_p)
2617 /* We will need the truncation glyph. */
2618 xassert (it->glyph_row == NULL);
2619 produce_special_glyphs (it, IT_TRUNCATION);
2620 it->truncation_pixel_width = it->pixel_width;
2622 else
2624 /* We will need the continuation glyph. */
2625 xassert (it->glyph_row == NULL);
2626 produce_special_glyphs (it, IT_CONTINUATION);
2627 it->continuation_pixel_width = it->pixel_width;
2630 /* Reset these values to zero because the produce_special_glyphs
2631 above has changed them. */
2632 it->pixel_width = it->ascent = it->descent = 0;
2633 it->phys_ascent = it->phys_descent = 0;
2636 /* Set this after getting the dimensions of truncation and
2637 continuation glyphs, so that we don't produce glyphs when calling
2638 produce_special_glyphs, above. */
2639 it->glyph_row = row;
2640 it->area = TEXT_AREA;
2642 /* Get the dimensions of the display area. The display area
2643 consists of the visible window area plus a horizontally scrolled
2644 part to the left of the window. All x-values are relative to the
2645 start of this total display area. */
2646 if (base_face_id != DEFAULT_FACE_ID)
2648 /* Mode lines, menu bar in terminal frames. */
2649 it->first_visible_x = 0;
2650 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2652 else
2654 it->first_visible_x
2655 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2656 it->last_visible_x = (it->first_visible_x
2657 + window_box_width (w, TEXT_AREA));
2659 /* If we truncate lines, leave room for the truncator glyph(s) at
2660 the right margin. Otherwise, leave room for the continuation
2661 glyph(s). Truncation and continuation glyphs are not inserted
2662 for window-based redisplay. */
2663 if (!FRAME_WINDOW_P (it->f))
2665 if (it->truncate_lines_p)
2666 it->last_visible_x -= it->truncation_pixel_width;
2667 else
2668 it->last_visible_x -= it->continuation_pixel_width;
2671 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2672 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2675 /* Leave room for a border glyph. */
2676 if (!FRAME_WINDOW_P (it->f)
2677 && !WINDOW_RIGHTMOST_P (it->w))
2678 it->last_visible_x -= 1;
2680 it->last_visible_y = window_text_bottom_y (w);
2682 /* For mode lines and alike, arrange for the first glyph having a
2683 left box line if the face specifies a box. */
2684 if (base_face_id != DEFAULT_FACE_ID)
2686 struct face *face;
2688 it->face_id = base_face_id;
2690 /* If we have a boxed mode line, make the first character appear
2691 with a left box line. */
2692 face = FACE_FROM_ID (it->f, base_face_id);
2693 if (face->box != FACE_NO_BOX)
2694 it->start_of_box_run_p = 1;
2697 /* If a buffer position was specified, set the iterator there,
2698 getting overlays and face properties from that position. */
2699 if (charpos >= BUF_BEG (current_buffer))
2701 it->end_charpos = ZV;
2702 it->face_id = -1;
2703 IT_CHARPOS (*it) = charpos;
2705 /* Compute byte position if not specified. */
2706 if (bytepos < charpos)
2707 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2708 else
2709 IT_BYTEPOS (*it) = bytepos;
2711 it->start = it->current;
2713 /* Compute faces etc. */
2714 reseat (it, it->current.pos, 1);
2717 CHECK_IT (it);
2721 /* Initialize IT for the display of window W with window start POS. */
2723 void
2724 start_display (it, w, pos)
2725 struct it *it;
2726 struct window *w;
2727 struct text_pos pos;
2729 struct glyph_row *row;
2730 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2732 row = w->desired_matrix->rows + first_vpos;
2733 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2734 it->first_vpos = first_vpos;
2736 /* Don't reseat to previous visible line start if current start
2737 position is in a string or image. */
2738 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2740 int start_at_line_beg_p;
2741 int first_y = it->current_y;
2743 /* If window start is not at a line start, skip forward to POS to
2744 get the correct continuation lines width. */
2745 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2746 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2747 if (!start_at_line_beg_p)
2749 int new_x;
2751 reseat_at_previous_visible_line_start (it);
2752 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2754 new_x = it->current_x + it->pixel_width;
2756 /* If lines are continued, this line may end in the middle
2757 of a multi-glyph character (e.g. a control character
2758 displayed as \003, or in the middle of an overlay
2759 string). In this case move_it_to above will not have
2760 taken us to the start of the continuation line but to the
2761 end of the continued line. */
2762 if (it->current_x > 0
2763 && !it->truncate_lines_p /* Lines are continued. */
2764 && (/* And glyph doesn't fit on the line. */
2765 new_x > it->last_visible_x
2766 /* Or it fits exactly and we're on a window
2767 system frame. */
2768 || (new_x == it->last_visible_x
2769 && FRAME_WINDOW_P (it->f))))
2771 if (it->current.dpvec_index >= 0
2772 || it->current.overlay_string_index >= 0)
2774 set_iterator_to_next (it, 1);
2775 move_it_in_display_line_to (it, -1, -1, 0);
2778 it->continuation_lines_width += it->current_x;
2781 /* We're starting a new display line, not affected by the
2782 height of the continued line, so clear the appropriate
2783 fields in the iterator structure. */
2784 it->max_ascent = it->max_descent = 0;
2785 it->max_phys_ascent = it->max_phys_descent = 0;
2787 it->current_y = first_y;
2788 it->vpos = 0;
2789 it->current_x = it->hpos = 0;
2793 #if 0 /* Don't assert the following because start_display is sometimes
2794 called intentionally with a window start that is not at a
2795 line start. Please leave this code in as a comment. */
2797 /* Window start should be on a line start, now. */
2798 xassert (it->continuation_lines_width
2799 || IT_CHARPOS (it) == BEGV
2800 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2801 #endif /* 0 */
2805 /* Return 1 if POS is a position in ellipses displayed for invisible
2806 text. W is the window we display, for text property lookup. */
2808 static int
2809 in_ellipses_for_invisible_text_p (pos, w)
2810 struct display_pos *pos;
2811 struct window *w;
2813 Lisp_Object prop, window;
2814 int ellipses_p = 0;
2815 int charpos = CHARPOS (pos->pos);
2817 /* If POS specifies a position in a display vector, this might
2818 be for an ellipsis displayed for invisible text. We won't
2819 get the iterator set up for delivering that ellipsis unless
2820 we make sure that it gets aware of the invisible text. */
2821 if (pos->dpvec_index >= 0
2822 && pos->overlay_string_index < 0
2823 && CHARPOS (pos->string_pos) < 0
2824 && charpos > BEGV
2825 && (XSETWINDOW (window, w),
2826 prop = Fget_char_property (make_number (charpos),
2827 Qinvisible, window),
2828 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2830 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2831 window);
2832 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2835 return ellipses_p;
2839 /* Initialize IT for stepping through current_buffer in window W,
2840 starting at position POS that includes overlay string and display
2841 vector/ control character translation position information. Value
2842 is zero if there are overlay strings with newlines at POS. */
2844 static int
2845 init_from_display_pos (it, w, pos)
2846 struct it *it;
2847 struct window *w;
2848 struct display_pos *pos;
2850 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2851 int i, overlay_strings_with_newlines = 0;
2853 /* If POS specifies a position in a display vector, this might
2854 be for an ellipsis displayed for invisible text. We won't
2855 get the iterator set up for delivering that ellipsis unless
2856 we make sure that it gets aware of the invisible text. */
2857 if (in_ellipses_for_invisible_text_p (pos, w))
2859 --charpos;
2860 bytepos = 0;
2863 /* Keep in mind: the call to reseat in init_iterator skips invisible
2864 text, so we might end up at a position different from POS. This
2865 is only a problem when POS is a row start after a newline and an
2866 overlay starts there with an after-string, and the overlay has an
2867 invisible property. Since we don't skip invisible text in
2868 display_line and elsewhere immediately after consuming the
2869 newline before the row start, such a POS will not be in a string,
2870 but the call to init_iterator below will move us to the
2871 after-string. */
2872 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2874 /* This only scans the current chunk -- it should scan all chunks.
2875 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2876 to 16 in 22.1 to make this a lesser problem. */
2877 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2879 const char *s = SDATA (it->overlay_strings[i]);
2880 const char *e = s + SBYTES (it->overlay_strings[i]);
2882 while (s < e && *s != '\n')
2883 ++s;
2885 if (s < e)
2887 overlay_strings_with_newlines = 1;
2888 break;
2892 /* If position is within an overlay string, set up IT to the right
2893 overlay string. */
2894 if (pos->overlay_string_index >= 0)
2896 int relative_index;
2898 /* If the first overlay string happens to have a `display'
2899 property for an image, the iterator will be set up for that
2900 image, and we have to undo that setup first before we can
2901 correct the overlay string index. */
2902 if (it->method == GET_FROM_IMAGE)
2903 pop_it (it);
2905 /* We already have the first chunk of overlay strings in
2906 IT->overlay_strings. Load more until the one for
2907 pos->overlay_string_index is in IT->overlay_strings. */
2908 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2910 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2911 it->current.overlay_string_index = 0;
2912 while (n--)
2914 load_overlay_strings (it, 0);
2915 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2919 it->current.overlay_string_index = pos->overlay_string_index;
2920 relative_index = (it->current.overlay_string_index
2921 % OVERLAY_STRING_CHUNK_SIZE);
2922 it->string = it->overlay_strings[relative_index];
2923 xassert (STRINGP (it->string));
2924 it->current.string_pos = pos->string_pos;
2925 it->method = GET_FROM_STRING;
2928 #if 0 /* This is bogus because POS not having an overlay string
2929 position does not mean it's after the string. Example: A
2930 line starting with a before-string and initialization of IT
2931 to the previous row's end position. */
2932 else if (it->current.overlay_string_index >= 0)
2934 /* If POS says we're already after an overlay string ending at
2935 POS, make sure to pop the iterator because it will be in
2936 front of that overlay string. When POS is ZV, we've thereby
2937 also ``processed'' overlay strings at ZV. */
2938 while (it->sp)
2939 pop_it (it);
2940 xassert (it->current.overlay_string_index == -1);
2941 xassert (it->method == GET_FROM_BUFFER);
2942 if (CHARPOS (pos->pos) == ZV)
2943 it->overlay_strings_at_end_processed_p = 1;
2945 #endif /* 0 */
2947 if (CHARPOS (pos->string_pos) >= 0)
2949 /* Recorded position is not in an overlay string, but in another
2950 string. This can only be a string from a `display' property.
2951 IT should already be filled with that string. */
2952 it->current.string_pos = pos->string_pos;
2953 xassert (STRINGP (it->string));
2956 /* Restore position in display vector translations, control
2957 character translations or ellipses. */
2958 if (pos->dpvec_index >= 0)
2960 if (it->dpvec == NULL)
2961 get_next_display_element (it);
2962 xassert (it->dpvec && it->current.dpvec_index == 0);
2963 it->current.dpvec_index = pos->dpvec_index;
2966 CHECK_IT (it);
2967 return !overlay_strings_with_newlines;
2971 /* Initialize IT for stepping through current_buffer in window W
2972 starting at ROW->start. */
2974 static void
2975 init_to_row_start (it, w, row)
2976 struct it *it;
2977 struct window *w;
2978 struct glyph_row *row;
2980 init_from_display_pos (it, w, &row->start);
2981 it->start = row->start;
2982 it->continuation_lines_width = row->continuation_lines_width;
2983 CHECK_IT (it);
2987 /* Initialize IT for stepping through current_buffer in window W
2988 starting in the line following ROW, i.e. starting at ROW->end.
2989 Value is zero if there are overlay strings with newlines at ROW's
2990 end position. */
2992 static int
2993 init_to_row_end (it, w, row)
2994 struct it *it;
2995 struct window *w;
2996 struct glyph_row *row;
2998 int success = 0;
3000 if (init_from_display_pos (it, w, &row->end))
3002 if (row->continued_p)
3003 it->continuation_lines_width
3004 = row->continuation_lines_width + row->pixel_width;
3005 CHECK_IT (it);
3006 success = 1;
3009 return success;
3015 /***********************************************************************
3016 Text properties
3017 ***********************************************************************/
3019 /* Called when IT reaches IT->stop_charpos. Handle text property and
3020 overlay changes. Set IT->stop_charpos to the next position where
3021 to stop. */
3023 static void
3024 handle_stop (it)
3025 struct it *it;
3027 enum prop_handled handled;
3028 int handle_overlay_change_p;
3029 struct props *p;
3031 it->dpvec = NULL;
3032 it->current.dpvec_index = -1;
3033 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3034 it->ignore_overlay_strings_at_pos_p = 0;
3036 /* Use face of preceding text for ellipsis (if invisible) */
3037 if (it->selective_display_ellipsis_p)
3038 it->saved_face_id = it->face_id;
3042 handled = HANDLED_NORMALLY;
3044 /* Call text property handlers. */
3045 for (p = it_props; p->handler; ++p)
3047 handled = p->handler (it);
3049 if (handled == HANDLED_RECOMPUTE_PROPS)
3050 break;
3051 else if (handled == HANDLED_RETURN)
3053 /* We still want to show before and after strings from
3054 overlays even if the actual buffer text is replaced. */
3055 if (!handle_overlay_change_p || it->sp > 1)
3056 return;
3057 if (!get_overlay_strings_1 (it, 0, 0))
3058 return;
3059 it->ignore_overlay_strings_at_pos_p = 1;
3060 it->string_from_display_prop_p = 0;
3061 handle_overlay_change_p = 0;
3062 handled = HANDLED_RECOMPUTE_PROPS;
3063 break;
3065 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3066 handle_overlay_change_p = 0;
3069 if (handled != HANDLED_RECOMPUTE_PROPS)
3071 /* Don't check for overlay strings below when set to deliver
3072 characters from a display vector. */
3073 if (it->method == GET_FROM_DISPLAY_VECTOR)
3074 handle_overlay_change_p = 0;
3076 /* Handle overlay changes.
3077 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3078 if it finds overlays. */
3079 if (handle_overlay_change_p)
3080 handled = handle_overlay_change (it);
3083 while (handled == HANDLED_RECOMPUTE_PROPS);
3085 /* Determine where to stop next. */
3086 if (handled == HANDLED_NORMALLY)
3087 compute_stop_pos (it);
3091 /* Compute IT->stop_charpos from text property and overlay change
3092 information for IT's current position. */
3094 static void
3095 compute_stop_pos (it)
3096 struct it *it;
3098 register INTERVAL iv, next_iv;
3099 Lisp_Object object, limit, position;
3101 /* If nowhere else, stop at the end. */
3102 it->stop_charpos = it->end_charpos;
3104 if (STRINGP (it->string))
3106 /* Strings are usually short, so don't limit the search for
3107 properties. */
3108 object = it->string;
3109 limit = Qnil;
3110 position = make_number (IT_STRING_CHARPOS (*it));
3112 else
3114 int charpos;
3116 /* If next overlay change is in front of the current stop pos
3117 (which is IT->end_charpos), stop there. Note: value of
3118 next_overlay_change is point-max if no overlay change
3119 follows. */
3120 charpos = next_overlay_change (IT_CHARPOS (*it));
3121 if (charpos < it->stop_charpos)
3122 it->stop_charpos = charpos;
3124 /* If showing the region, we have to stop at the region
3125 start or end because the face might change there. */
3126 if (it->region_beg_charpos > 0)
3128 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3129 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3130 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3131 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3134 /* Set up variables for computing the stop position from text
3135 property changes. */
3136 XSETBUFFER (object, current_buffer);
3137 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3138 position = make_number (IT_CHARPOS (*it));
3142 /* Get the interval containing IT's position. Value is a null
3143 interval if there isn't such an interval. */
3144 iv = validate_interval_range (object, &position, &position, 0);
3145 if (!NULL_INTERVAL_P (iv))
3147 Lisp_Object values_here[LAST_PROP_IDX];
3148 struct props *p;
3150 /* Get properties here. */
3151 for (p = it_props; p->handler; ++p)
3152 values_here[p->idx] = textget (iv->plist, *p->name);
3154 /* Look for an interval following iv that has different
3155 properties. */
3156 for (next_iv = next_interval (iv);
3157 (!NULL_INTERVAL_P (next_iv)
3158 && (NILP (limit)
3159 || XFASTINT (limit) > next_iv->position));
3160 next_iv = next_interval (next_iv))
3162 for (p = it_props; p->handler; ++p)
3164 Lisp_Object new_value;
3166 new_value = textget (next_iv->plist, *p->name);
3167 if (!EQ (values_here[p->idx], new_value))
3168 break;
3171 if (p->handler)
3172 break;
3175 if (!NULL_INTERVAL_P (next_iv))
3177 if (INTEGERP (limit)
3178 && next_iv->position >= XFASTINT (limit))
3179 /* No text property change up to limit. */
3180 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3181 else
3182 /* Text properties change in next_iv. */
3183 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3187 xassert (STRINGP (it->string)
3188 || (it->stop_charpos >= BEGV
3189 && it->stop_charpos >= IT_CHARPOS (*it)));
3193 /* Return the position of the next overlay change after POS in
3194 current_buffer. Value is point-max if no overlay change
3195 follows. This is like `next-overlay-change' but doesn't use
3196 xmalloc. */
3198 static int
3199 next_overlay_change (pos)
3200 int pos;
3202 int noverlays;
3203 int endpos;
3204 Lisp_Object *overlays;
3205 int i;
3207 /* Get all overlays at the given position. */
3208 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3210 /* If any of these overlays ends before endpos,
3211 use its ending point instead. */
3212 for (i = 0; i < noverlays; ++i)
3214 Lisp_Object oend;
3215 int oendpos;
3217 oend = OVERLAY_END (overlays[i]);
3218 oendpos = OVERLAY_POSITION (oend);
3219 endpos = min (endpos, oendpos);
3222 return endpos;
3227 /***********************************************************************
3228 Fontification
3229 ***********************************************************************/
3231 /* Handle changes in the `fontified' property of the current buffer by
3232 calling hook functions from Qfontification_functions to fontify
3233 regions of text. */
3235 static enum prop_handled
3236 handle_fontified_prop (it)
3237 struct it *it;
3239 Lisp_Object prop, pos;
3240 enum prop_handled handled = HANDLED_NORMALLY;
3242 if (!NILP (Vmemory_full))
3243 return handled;
3245 /* Get the value of the `fontified' property at IT's current buffer
3246 position. (The `fontified' property doesn't have a special
3247 meaning in strings.) If the value is nil, call functions from
3248 Qfontification_functions. */
3249 if (!STRINGP (it->string)
3250 && it->s == NULL
3251 && !NILP (Vfontification_functions)
3252 && !NILP (Vrun_hooks)
3253 && (pos = make_number (IT_CHARPOS (*it)),
3254 prop = Fget_char_property (pos, Qfontified, Qnil),
3255 /* Ignore the special cased nil value always present at EOB since
3256 no amount of fontifying will be able to change it. */
3257 NILP (prop) && IT_CHARPOS (*it) < Z))
3259 int count = SPECPDL_INDEX ();
3260 Lisp_Object val;
3262 val = Vfontification_functions;
3263 specbind (Qfontification_functions, Qnil);
3265 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3266 safe_call1 (val, pos);
3267 else
3269 Lisp_Object globals, fn;
3270 struct gcpro gcpro1, gcpro2;
3272 globals = Qnil;
3273 GCPRO2 (val, globals);
3275 for (; CONSP (val); val = XCDR (val))
3277 fn = XCAR (val);
3279 if (EQ (fn, Qt))
3281 /* A value of t indicates this hook has a local
3282 binding; it means to run the global binding too.
3283 In a global value, t should not occur. If it
3284 does, we must ignore it to avoid an endless
3285 loop. */
3286 for (globals = Fdefault_value (Qfontification_functions);
3287 CONSP (globals);
3288 globals = XCDR (globals))
3290 fn = XCAR (globals);
3291 if (!EQ (fn, Qt))
3292 safe_call1 (fn, pos);
3295 else
3296 safe_call1 (fn, pos);
3299 UNGCPRO;
3302 unbind_to (count, Qnil);
3304 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3305 something. This avoids an endless loop if they failed to
3306 fontify the text for which reason ever. */
3307 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3308 handled = HANDLED_RECOMPUTE_PROPS;
3311 return handled;
3316 /***********************************************************************
3317 Faces
3318 ***********************************************************************/
3320 /* Set up iterator IT from face properties at its current position.
3321 Called from handle_stop. */
3323 static enum prop_handled
3324 handle_face_prop (it)
3325 struct it *it;
3327 int new_face_id, next_stop;
3329 if (!STRINGP (it->string))
3331 new_face_id
3332 = face_at_buffer_position (it->w,
3333 IT_CHARPOS (*it),
3334 it->region_beg_charpos,
3335 it->region_end_charpos,
3336 &next_stop,
3337 (IT_CHARPOS (*it)
3338 + TEXT_PROP_DISTANCE_LIMIT),
3341 /* Is this a start of a run of characters with box face?
3342 Caveat: this can be called for a freshly initialized
3343 iterator; face_id is -1 in this case. We know that the new
3344 face will not change until limit, i.e. if the new face has a
3345 box, all characters up to limit will have one. But, as
3346 usual, we don't know whether limit is really the end. */
3347 if (new_face_id != it->face_id)
3349 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3351 /* If new face has a box but old face has not, this is
3352 the start of a run of characters with box, i.e. it has
3353 a shadow on the left side. The value of face_id of the
3354 iterator will be -1 if this is the initial call that gets
3355 the face. In this case, we have to look in front of IT's
3356 position and see whether there is a face != new_face_id. */
3357 it->start_of_box_run_p
3358 = (new_face->box != FACE_NO_BOX
3359 && (it->face_id >= 0
3360 || IT_CHARPOS (*it) == BEG
3361 || new_face_id != face_before_it_pos (it)));
3362 it->face_box_p = new_face->box != FACE_NO_BOX;
3365 else
3367 int base_face_id, bufpos;
3368 int i;
3369 Lisp_Object from_overlay
3370 = (it->current.overlay_string_index >= 0
3371 ? it->string_overlays[it->current.overlay_string_index]
3372 : Qnil);
3374 /* See if we got to this string directly or indirectly from
3375 an overlay property. That includes the before-string or
3376 after-string of an overlay, strings in display properties
3377 provided by an overlay, their text properties, etc.
3379 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3380 if (! NILP (from_overlay))
3381 for (i = it->sp - 1; i >= 0; i--)
3383 if (it->stack[i].current.overlay_string_index >= 0)
3384 from_overlay
3385 = it->string_overlays[it->stack[i].current.overlay_string_index];
3386 else if (! NILP (it->stack[i].from_overlay))
3387 from_overlay = it->stack[i].from_overlay;
3389 if (!NILP (from_overlay))
3390 break;
3393 if (! NILP (from_overlay))
3395 bufpos = IT_CHARPOS (*it);
3396 /* For a string from an overlay, the base face depends
3397 only on text properties and ignores overlays. */
3398 base_face_id
3399 = face_for_overlay_string (it->w,
3400 IT_CHARPOS (*it),
3401 it->region_beg_charpos,
3402 it->region_end_charpos,
3403 &next_stop,
3404 (IT_CHARPOS (*it)
3405 + TEXT_PROP_DISTANCE_LIMIT),
3407 from_overlay);
3409 else
3411 bufpos = 0;
3413 /* For strings from a `display' property, use the face at
3414 IT's current buffer position as the base face to merge
3415 with, so that overlay strings appear in the same face as
3416 surrounding text, unless they specify their own
3417 faces. */
3418 base_face_id = underlying_face_id (it);
3421 new_face_id = face_at_string_position (it->w,
3422 it->string,
3423 IT_STRING_CHARPOS (*it),
3424 bufpos,
3425 it->region_beg_charpos,
3426 it->region_end_charpos,
3427 &next_stop,
3428 base_face_id, 0);
3430 #if 0 /* This shouldn't be neccessary. Let's check it. */
3431 /* If IT is used to display a mode line we would really like to
3432 use the mode line face instead of the frame's default face. */
3433 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3434 && new_face_id == DEFAULT_FACE_ID)
3435 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3436 #endif
3438 /* Is this a start of a run of characters with box? Caveat:
3439 this can be called for a freshly allocated iterator; face_id
3440 is -1 is this case. We know that the new face will not
3441 change until the next check pos, i.e. if the new face has a
3442 box, all characters up to that position will have a
3443 box. But, as usual, we don't know whether that position
3444 is really the end. */
3445 if (new_face_id != it->face_id)
3447 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3448 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3450 /* If new face has a box but old face hasn't, this is the
3451 start of a run of characters with box, i.e. it has a
3452 shadow on the left side. */
3453 it->start_of_box_run_p
3454 = new_face->box && (old_face == NULL || !old_face->box);
3455 it->face_box_p = new_face->box != FACE_NO_BOX;
3459 it->face_id = new_face_id;
3460 return HANDLED_NORMALLY;
3464 /* Return the ID of the face ``underlying'' IT's current position,
3465 which is in a string. If the iterator is associated with a
3466 buffer, return the face at IT's current buffer position.
3467 Otherwise, use the iterator's base_face_id. */
3469 static int
3470 underlying_face_id (it)
3471 struct it *it;
3473 int face_id = it->base_face_id, i;
3475 xassert (STRINGP (it->string));
3477 for (i = it->sp - 1; i >= 0; --i)
3478 if (NILP (it->stack[i].string))
3479 face_id = it->stack[i].face_id;
3481 return face_id;
3485 /* Compute the face one character before or after the current position
3486 of IT. BEFORE_P non-zero means get the face in front of IT's
3487 position. Value is the id of the face. */
3489 static int
3490 face_before_or_after_it_pos (it, before_p)
3491 struct it *it;
3492 int before_p;
3494 int face_id, limit;
3495 int next_check_charpos;
3496 struct text_pos pos;
3498 xassert (it->s == NULL);
3500 if (STRINGP (it->string))
3502 int bufpos, base_face_id;
3504 /* No face change past the end of the string (for the case
3505 we are padding with spaces). No face change before the
3506 string start. */
3507 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3508 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3509 return it->face_id;
3511 /* Set pos to the position before or after IT's current position. */
3512 if (before_p)
3513 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3514 else
3515 /* For composition, we must check the character after the
3516 composition. */
3517 pos = (it->what == IT_COMPOSITION
3518 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3519 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3521 if (it->current.overlay_string_index >= 0)
3522 bufpos = IT_CHARPOS (*it);
3523 else
3524 bufpos = 0;
3526 base_face_id = underlying_face_id (it);
3528 /* Get the face for ASCII, or unibyte. */
3529 face_id = face_at_string_position (it->w,
3530 it->string,
3531 CHARPOS (pos),
3532 bufpos,
3533 it->region_beg_charpos,
3534 it->region_end_charpos,
3535 &next_check_charpos,
3536 base_face_id, 0);
3538 /* Correct the face for charsets different from ASCII. Do it
3539 for the multibyte case only. The face returned above is
3540 suitable for unibyte text if IT->string is unibyte. */
3541 if (STRING_MULTIBYTE (it->string))
3543 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3544 int rest = SBYTES (it->string) - BYTEPOS (pos);
3545 int c, len;
3546 struct face *face = FACE_FROM_ID (it->f, face_id);
3548 c = string_char_and_length (p, rest, &len);
3549 face_id = FACE_FOR_CHAR (it->f, face, c);
3552 else
3554 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3555 || (IT_CHARPOS (*it) <= BEGV && before_p))
3556 return it->face_id;
3558 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3559 pos = it->current.pos;
3561 if (before_p)
3562 DEC_TEXT_POS (pos, it->multibyte_p);
3563 else
3565 if (it->what == IT_COMPOSITION)
3566 /* For composition, we must check the position after the
3567 composition. */
3568 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3569 else
3570 INC_TEXT_POS (pos, it->multibyte_p);
3573 /* Determine face for CHARSET_ASCII, or unibyte. */
3574 face_id = face_at_buffer_position (it->w,
3575 CHARPOS (pos),
3576 it->region_beg_charpos,
3577 it->region_end_charpos,
3578 &next_check_charpos,
3579 limit, 0);
3581 /* Correct the face for charsets different from ASCII. Do it
3582 for the multibyte case only. The face returned above is
3583 suitable for unibyte text if current_buffer is unibyte. */
3584 if (it->multibyte_p)
3586 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3587 struct face *face = FACE_FROM_ID (it->f, face_id);
3588 face_id = FACE_FOR_CHAR (it->f, face, c);
3592 return face_id;
3597 /***********************************************************************
3598 Invisible text
3599 ***********************************************************************/
3601 /* Set up iterator IT from invisible properties at its current
3602 position. Called from handle_stop. */
3604 static enum prop_handled
3605 handle_invisible_prop (it)
3606 struct it *it;
3608 enum prop_handled handled = HANDLED_NORMALLY;
3610 if (STRINGP (it->string))
3612 extern Lisp_Object Qinvisible;
3613 Lisp_Object prop, end_charpos, limit, charpos;
3615 /* Get the value of the invisible text property at the
3616 current position. Value will be nil if there is no such
3617 property. */
3618 charpos = make_number (IT_STRING_CHARPOS (*it));
3619 prop = Fget_text_property (charpos, Qinvisible, it->string);
3621 if (!NILP (prop)
3622 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3624 handled = HANDLED_RECOMPUTE_PROPS;
3626 /* Get the position at which the next change of the
3627 invisible text property can be found in IT->string.
3628 Value will be nil if the property value is the same for
3629 all the rest of IT->string. */
3630 XSETINT (limit, SCHARS (it->string));
3631 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3632 it->string, limit);
3634 /* Text at current position is invisible. The next
3635 change in the property is at position end_charpos.
3636 Move IT's current position to that position. */
3637 if (INTEGERP (end_charpos)
3638 && XFASTINT (end_charpos) < XFASTINT (limit))
3640 struct text_pos old;
3641 old = it->current.string_pos;
3642 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3643 compute_string_pos (&it->current.string_pos, old, it->string);
3645 else
3647 /* The rest of the string is invisible. If this is an
3648 overlay string, proceed with the next overlay string
3649 or whatever comes and return a character from there. */
3650 if (it->current.overlay_string_index >= 0)
3652 next_overlay_string (it);
3653 /* Don't check for overlay strings when we just
3654 finished processing them. */
3655 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3657 else
3659 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3660 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3665 else
3667 int invis_p, newpos, next_stop, start_charpos;
3668 Lisp_Object pos, prop, overlay;
3670 /* First of all, is there invisible text at this position? */
3671 start_charpos = IT_CHARPOS (*it);
3672 pos = make_number (IT_CHARPOS (*it));
3673 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3674 &overlay);
3675 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3677 /* If we are on invisible text, skip over it. */
3678 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3680 /* Record whether we have to display an ellipsis for the
3681 invisible text. */
3682 int display_ellipsis_p = invis_p == 2;
3684 handled = HANDLED_RECOMPUTE_PROPS;
3686 /* Loop skipping over invisible text. The loop is left at
3687 ZV or with IT on the first char being visible again. */
3690 /* Try to skip some invisible text. Return value is the
3691 position reached which can be equal to IT's position
3692 if there is nothing invisible here. This skips both
3693 over invisible text properties and overlays with
3694 invisible property. */
3695 newpos = skip_invisible (IT_CHARPOS (*it),
3696 &next_stop, ZV, it->window);
3698 /* If we skipped nothing at all we weren't at invisible
3699 text in the first place. If everything to the end of
3700 the buffer was skipped, end the loop. */
3701 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3702 invis_p = 0;
3703 else
3705 /* We skipped some characters but not necessarily
3706 all there are. Check if we ended up on visible
3707 text. Fget_char_property returns the property of
3708 the char before the given position, i.e. if we
3709 get invis_p = 0, this means that the char at
3710 newpos is visible. */
3711 pos = make_number (newpos);
3712 prop = Fget_char_property (pos, Qinvisible, it->window);
3713 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3716 /* If we ended up on invisible text, proceed to
3717 skip starting with next_stop. */
3718 if (invis_p)
3719 IT_CHARPOS (*it) = next_stop;
3721 /* If there are adjacent invisible texts, don't lose the
3722 second one's ellipsis. */
3723 if (invis_p == 2)
3724 display_ellipsis_p = 1;
3726 while (invis_p);
3728 /* The position newpos is now either ZV or on visible text. */
3729 IT_CHARPOS (*it) = newpos;
3730 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3732 /* If there are before-strings at the start of invisible
3733 text, and the text is invisible because of a text
3734 property, arrange to show before-strings because 20.x did
3735 it that way. (If the text is invisible because of an
3736 overlay property instead of a text property, this is
3737 already handled in the overlay code.) */
3738 if (NILP (overlay)
3739 && get_overlay_strings (it, start_charpos))
3741 handled = HANDLED_RECOMPUTE_PROPS;
3742 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3744 else if (display_ellipsis_p)
3746 /* Make sure that the glyphs of the ellipsis will get
3747 correct `charpos' values. If we would not update
3748 it->position here, the glyphs would belong to the
3749 last visible character _before_ the invisible
3750 text, which confuses `set_cursor_from_row'.
3752 We use the last invisible position instead of the
3753 first because this way the cursor is always drawn on
3754 the first "." of the ellipsis, whenever PT is inside
3755 the invisible text. Otherwise the cursor would be
3756 placed _after_ the ellipsis when the point is after the
3757 first invisible character. */
3758 if (!STRINGP (it->object))
3760 it->position.charpos = IT_CHARPOS (*it) - 1;
3761 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3763 setup_for_ellipsis (it, 0);
3764 /* Let the ellipsis display before
3765 considering any properties of the following char.
3766 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3767 handled = HANDLED_RETURN;
3772 return handled;
3776 /* Make iterator IT return `...' next.
3777 Replaces LEN characters from buffer. */
3779 static void
3780 setup_for_ellipsis (it, len)
3781 struct it *it;
3782 int len;
3784 /* Use the display table definition for `...'. Invalid glyphs
3785 will be handled by the method returning elements from dpvec. */
3786 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3788 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3789 it->dpvec = v->contents;
3790 it->dpend = v->contents + v->size;
3792 else
3794 /* Default `...'. */
3795 it->dpvec = default_invis_vector;
3796 it->dpend = default_invis_vector + 3;
3799 it->dpvec_char_len = len;
3800 it->current.dpvec_index = 0;
3801 it->dpvec_face_id = -1;
3803 /* Remember the current face id in case glyphs specify faces.
3804 IT's face is restored in set_iterator_to_next.
3805 saved_face_id was set to preceding char's face in handle_stop. */
3806 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3807 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3809 it->method = GET_FROM_DISPLAY_VECTOR;
3810 it->ellipsis_p = 1;
3815 /***********************************************************************
3816 'display' property
3817 ***********************************************************************/
3819 /* Set up iterator IT from `display' property at its current position.
3820 Called from handle_stop.
3821 We return HANDLED_RETURN if some part of the display property
3822 overrides the display of the buffer text itself.
3823 Otherwise we return HANDLED_NORMALLY. */
3825 static enum prop_handled
3826 handle_display_prop (it)
3827 struct it *it;
3829 Lisp_Object prop, object, overlay;
3830 struct text_pos *position;
3831 /* Nonzero if some property replaces the display of the text itself. */
3832 int display_replaced_p = 0;
3834 if (STRINGP (it->string))
3836 object = it->string;
3837 position = &it->current.string_pos;
3839 else
3841 XSETWINDOW (object, it->w);
3842 position = &it->current.pos;
3845 /* Reset those iterator values set from display property values. */
3846 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3847 it->space_width = Qnil;
3848 it->font_height = Qnil;
3849 it->voffset = 0;
3851 /* We don't support recursive `display' properties, i.e. string
3852 values that have a string `display' property, that have a string
3853 `display' property etc. */
3854 if (!it->string_from_display_prop_p)
3855 it->area = TEXT_AREA;
3857 prop = get_char_property_and_overlay (make_number (position->charpos),
3858 Qdisplay, object, &overlay);
3859 if (NILP (prop))
3860 return HANDLED_NORMALLY;
3861 /* Now OVERLAY is the overlay that gave us this property, or nil
3862 if it was a text property. */
3864 if (!STRINGP (it->string))
3865 object = it->w->buffer;
3867 if (CONSP (prop)
3868 /* Simple properties. */
3869 && !EQ (XCAR (prop), Qimage)
3870 && !EQ (XCAR (prop), Qspace)
3871 && !EQ (XCAR (prop), Qwhen)
3872 && !EQ (XCAR (prop), Qslice)
3873 && !EQ (XCAR (prop), Qspace_width)
3874 && !EQ (XCAR (prop), Qheight)
3875 && !EQ (XCAR (prop), Qraise)
3876 /* Marginal area specifications. */
3877 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3878 && !EQ (XCAR (prop), Qleft_fringe)
3879 && !EQ (XCAR (prop), Qright_fringe)
3880 && !NILP (XCAR (prop)))
3882 for (; CONSP (prop); prop = XCDR (prop))
3884 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3885 position, display_replaced_p))
3887 display_replaced_p = 1;
3888 /* If some text in a string is replaced, `position' no
3889 longer points to the position of `object'. */
3890 if (STRINGP (object))
3891 break;
3895 else if (VECTORP (prop))
3897 int i;
3898 for (i = 0; i < ASIZE (prop); ++i)
3899 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3900 position, display_replaced_p))
3902 display_replaced_p = 1;
3903 /* If some text in a string is replaced, `position' no
3904 longer points to the position of `object'. */
3905 if (STRINGP (object))
3906 break;
3909 else
3911 int ret = handle_single_display_spec (it, prop, object, overlay,
3912 position, 0);
3913 if (ret < 0) /* Replaced by "", i.e. nothing. */
3914 return HANDLED_RECOMPUTE_PROPS;
3915 if (ret)
3916 display_replaced_p = 1;
3919 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3923 /* Value is the position of the end of the `display' property starting
3924 at START_POS in OBJECT. */
3926 static struct text_pos
3927 display_prop_end (it, object, start_pos)
3928 struct it *it;
3929 Lisp_Object object;
3930 struct text_pos start_pos;
3932 Lisp_Object end;
3933 struct text_pos end_pos;
3935 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3936 Qdisplay, object, Qnil);
3937 CHARPOS (end_pos) = XFASTINT (end);
3938 if (STRINGP (object))
3939 compute_string_pos (&end_pos, start_pos, it->string);
3940 else
3941 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3943 return end_pos;
3947 /* Set up IT from a single `display' specification PROP. OBJECT
3948 is the object in which the `display' property was found. *POSITION
3949 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3950 means that we previously saw a display specification which already
3951 replaced text display with something else, for example an image;
3952 we ignore such properties after the first one has been processed.
3954 OVERLAY is the overlay this `display' property came from,
3955 or nil if it was a text property.
3957 If PROP is a `space' or `image' specification, and in some other
3958 cases too, set *POSITION to the position where the `display'
3959 property ends.
3961 Value is non-zero if something was found which replaces the display
3962 of buffer or string text. Specifically, the value is -1 if that
3963 "something" is "nothing". */
3965 static int
3966 handle_single_display_spec (it, spec, object, overlay, position,
3967 display_replaced_before_p)
3968 struct it *it;
3969 Lisp_Object spec;
3970 Lisp_Object object;
3971 Lisp_Object overlay;
3972 struct text_pos *position;
3973 int display_replaced_before_p;
3975 Lisp_Object form;
3976 Lisp_Object location, value;
3977 struct text_pos start_pos, save_pos;
3978 int valid_p;
3980 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3981 If the result is non-nil, use VALUE instead of SPEC. */
3982 form = Qt;
3983 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3985 spec = XCDR (spec);
3986 if (!CONSP (spec))
3987 return 0;
3988 form = XCAR (spec);
3989 spec = XCDR (spec);
3992 if (!NILP (form) && !EQ (form, Qt))
3994 int count = SPECPDL_INDEX ();
3995 struct gcpro gcpro1;
3997 /* Bind `object' to the object having the `display' property, a
3998 buffer or string. Bind `position' to the position in the
3999 object where the property was found, and `buffer-position'
4000 to the current position in the buffer. */
4001 specbind (Qobject, object);
4002 specbind (Qposition, make_number (CHARPOS (*position)));
4003 specbind (Qbuffer_position,
4004 make_number (STRINGP (object)
4005 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4006 GCPRO1 (form);
4007 form = safe_eval (form);
4008 UNGCPRO;
4009 unbind_to (count, Qnil);
4012 if (NILP (form))
4013 return 0;
4015 /* Handle `(height HEIGHT)' specifications. */
4016 if (CONSP (spec)
4017 && EQ (XCAR (spec), Qheight)
4018 && CONSP (XCDR (spec)))
4020 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4021 return 0;
4023 it->font_height = XCAR (XCDR (spec));
4024 if (!NILP (it->font_height))
4026 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4027 int new_height = -1;
4029 if (CONSP (it->font_height)
4030 && (EQ (XCAR (it->font_height), Qplus)
4031 || EQ (XCAR (it->font_height), Qminus))
4032 && CONSP (XCDR (it->font_height))
4033 && INTEGERP (XCAR (XCDR (it->font_height))))
4035 /* `(+ N)' or `(- N)' where N is an integer. */
4036 int steps = XINT (XCAR (XCDR (it->font_height)));
4037 if (EQ (XCAR (it->font_height), Qplus))
4038 steps = - steps;
4039 it->face_id = smaller_face (it->f, it->face_id, steps);
4041 else if (FUNCTIONP (it->font_height))
4043 /* Call function with current height as argument.
4044 Value is the new height. */
4045 Lisp_Object height;
4046 height = safe_call1 (it->font_height,
4047 face->lface[LFACE_HEIGHT_INDEX]);
4048 if (NUMBERP (height))
4049 new_height = XFLOATINT (height);
4051 else if (NUMBERP (it->font_height))
4053 /* Value is a multiple of the canonical char height. */
4054 struct face *face;
4056 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4057 new_height = (XFLOATINT (it->font_height)
4058 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4060 else
4062 /* Evaluate IT->font_height with `height' bound to the
4063 current specified height to get the new height. */
4064 int count = SPECPDL_INDEX ();
4066 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4067 value = safe_eval (it->font_height);
4068 unbind_to (count, Qnil);
4070 if (NUMBERP (value))
4071 new_height = XFLOATINT (value);
4074 if (new_height > 0)
4075 it->face_id = face_with_height (it->f, it->face_id, new_height);
4078 return 0;
4081 /* Handle `(space-width WIDTH)'. */
4082 if (CONSP (spec)
4083 && EQ (XCAR (spec), Qspace_width)
4084 && CONSP (XCDR (spec)))
4086 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4087 return 0;
4089 value = XCAR (XCDR (spec));
4090 if (NUMBERP (value) && XFLOATINT (value) > 0)
4091 it->space_width = value;
4093 return 0;
4096 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4097 if (CONSP (spec)
4098 && EQ (XCAR (spec), Qslice))
4100 Lisp_Object tem;
4102 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4103 return 0;
4105 if (tem = XCDR (spec), CONSP (tem))
4107 it->slice.x = XCAR (tem);
4108 if (tem = XCDR (tem), CONSP (tem))
4110 it->slice.y = XCAR (tem);
4111 if (tem = XCDR (tem), CONSP (tem))
4113 it->slice.width = XCAR (tem);
4114 if (tem = XCDR (tem), CONSP (tem))
4115 it->slice.height = XCAR (tem);
4120 return 0;
4123 /* Handle `(raise FACTOR)'. */
4124 if (CONSP (spec)
4125 && EQ (XCAR (spec), Qraise)
4126 && CONSP (XCDR (spec)))
4128 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4129 return 0;
4131 #ifdef HAVE_WINDOW_SYSTEM
4132 value = XCAR (XCDR (spec));
4133 if (NUMBERP (value))
4135 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4136 it->voffset = - (XFLOATINT (value)
4137 * (FONT_HEIGHT (face->font)));
4139 #endif /* HAVE_WINDOW_SYSTEM */
4141 return 0;
4144 /* Don't handle the other kinds of display specifications
4145 inside a string that we got from a `display' property. */
4146 if (it->string_from_display_prop_p)
4147 return 0;
4149 /* Characters having this form of property are not displayed, so
4150 we have to find the end of the property. */
4151 start_pos = *position;
4152 *position = display_prop_end (it, object, start_pos);
4153 value = Qnil;
4155 /* Stop the scan at that end position--we assume that all
4156 text properties change there. */
4157 it->stop_charpos = position->charpos;
4159 /* Handle `(left-fringe BITMAP [FACE])'
4160 and `(right-fringe BITMAP [FACE])'. */
4161 if (CONSP (spec)
4162 && (EQ (XCAR (spec), Qleft_fringe)
4163 || EQ (XCAR (spec), Qright_fringe))
4164 && CONSP (XCDR (spec)))
4166 int face_id = DEFAULT_FACE_ID;
4167 int fringe_bitmap;
4169 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4170 /* If we return here, POSITION has been advanced
4171 across the text with this property. */
4172 return 0;
4174 #ifdef HAVE_WINDOW_SYSTEM
4175 value = XCAR (XCDR (spec));
4176 if (!SYMBOLP (value)
4177 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4178 /* If we return here, POSITION has been advanced
4179 across the text with this property. */
4180 return 0;
4182 if (CONSP (XCDR (XCDR (spec))))
4184 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4185 int face_id2 = lookup_derived_face (it->f, face_name,
4186 'A', FRINGE_FACE_ID, 0);
4187 if (face_id2 >= 0)
4188 face_id = face_id2;
4191 /* Save current settings of IT so that we can restore them
4192 when we are finished with the glyph property value. */
4194 save_pos = it->position;
4195 it->position = *position;
4196 push_it (it);
4197 it->position = save_pos;
4199 it->area = TEXT_AREA;
4200 it->what = IT_IMAGE;
4201 it->image_id = -1; /* no image */
4202 it->position = start_pos;
4203 it->object = NILP (object) ? it->w->buffer : object;
4204 it->method = GET_FROM_IMAGE;
4205 it->from_overlay = Qnil;
4206 it->face_id = face_id;
4208 /* Say that we haven't consumed the characters with
4209 `display' property yet. The call to pop_it in
4210 set_iterator_to_next will clean this up. */
4211 *position = start_pos;
4213 if (EQ (XCAR (spec), Qleft_fringe))
4215 it->left_user_fringe_bitmap = fringe_bitmap;
4216 it->left_user_fringe_face_id = face_id;
4218 else
4220 it->right_user_fringe_bitmap = fringe_bitmap;
4221 it->right_user_fringe_face_id = face_id;
4223 #endif /* HAVE_WINDOW_SYSTEM */
4224 return 1;
4227 /* Prepare to handle `((margin left-margin) ...)',
4228 `((margin right-margin) ...)' and `((margin nil) ...)'
4229 prefixes for display specifications. */
4230 location = Qunbound;
4231 if (CONSP (spec) && CONSP (XCAR (spec)))
4233 Lisp_Object tem;
4235 value = XCDR (spec);
4236 if (CONSP (value))
4237 value = XCAR (value);
4239 tem = XCAR (spec);
4240 if (EQ (XCAR (tem), Qmargin)
4241 && (tem = XCDR (tem),
4242 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4243 (NILP (tem)
4244 || EQ (tem, Qleft_margin)
4245 || EQ (tem, Qright_margin))))
4246 location = tem;
4249 if (EQ (location, Qunbound))
4251 location = Qnil;
4252 value = spec;
4255 /* After this point, VALUE is the property after any
4256 margin prefix has been stripped. It must be a string,
4257 an image specification, or `(space ...)'.
4259 LOCATION specifies where to display: `left-margin',
4260 `right-margin' or nil. */
4262 valid_p = (STRINGP (value)
4263 #ifdef HAVE_WINDOW_SYSTEM
4264 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4265 #endif /* not HAVE_WINDOW_SYSTEM */
4266 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4268 if (valid_p && !display_replaced_before_p)
4270 /* Save current settings of IT so that we can restore them
4271 when we are finished with the glyph property value. */
4272 save_pos = it->position;
4273 it->position = *position;
4274 push_it (it);
4275 it->position = save_pos;
4276 it->from_overlay = overlay;
4278 if (NILP (location))
4279 it->area = TEXT_AREA;
4280 else if (EQ (location, Qleft_margin))
4281 it->area = LEFT_MARGIN_AREA;
4282 else
4283 it->area = RIGHT_MARGIN_AREA;
4285 if (STRINGP (value))
4287 if (SCHARS (value) == 0)
4289 pop_it (it);
4290 return -1; /* Replaced by "", i.e. nothing. */
4292 it->string = value;
4293 it->multibyte_p = STRING_MULTIBYTE (it->string);
4294 it->current.overlay_string_index = -1;
4295 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4296 it->end_charpos = it->string_nchars = SCHARS (it->string);
4297 it->method = GET_FROM_STRING;
4298 it->stop_charpos = 0;
4299 it->string_from_display_prop_p = 1;
4300 /* Say that we haven't consumed the characters with
4301 `display' property yet. The call to pop_it in
4302 set_iterator_to_next will clean this up. */
4303 if (BUFFERP (object))
4304 *position = start_pos;
4306 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4308 it->method = GET_FROM_STRETCH;
4309 it->object = value;
4310 *position = it->position = start_pos;
4312 #ifdef HAVE_WINDOW_SYSTEM
4313 else
4315 it->what = IT_IMAGE;
4316 it->image_id = lookup_image (it->f, value);
4317 it->position = start_pos;
4318 it->object = NILP (object) ? it->w->buffer : object;
4319 it->method = GET_FROM_IMAGE;
4321 /* Say that we haven't consumed the characters with
4322 `display' property yet. The call to pop_it in
4323 set_iterator_to_next will clean this up. */
4324 *position = start_pos;
4326 #endif /* HAVE_WINDOW_SYSTEM */
4328 return 1;
4331 /* Invalid property or property not supported. Restore
4332 POSITION to what it was before. */
4333 *position = start_pos;
4334 return 0;
4338 /* Check if SPEC is a display specification value whose text should be
4339 treated as intangible. */
4341 static int
4342 single_display_spec_intangible_p (prop)
4343 Lisp_Object prop;
4345 /* Skip over `when FORM'. */
4346 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4348 prop = XCDR (prop);
4349 if (!CONSP (prop))
4350 return 0;
4351 prop = XCDR (prop);
4354 if (STRINGP (prop))
4355 return 1;
4357 if (!CONSP (prop))
4358 return 0;
4360 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4361 we don't need to treat text as intangible. */
4362 if (EQ (XCAR (prop), Qmargin))
4364 prop = XCDR (prop);
4365 if (!CONSP (prop))
4366 return 0;
4368 prop = XCDR (prop);
4369 if (!CONSP (prop)
4370 || EQ (XCAR (prop), Qleft_margin)
4371 || EQ (XCAR (prop), Qright_margin))
4372 return 0;
4375 return (CONSP (prop)
4376 && (EQ (XCAR (prop), Qimage)
4377 || EQ (XCAR (prop), Qspace)));
4381 /* Check if PROP is a display property value whose text should be
4382 treated as intangible. */
4385 display_prop_intangible_p (prop)
4386 Lisp_Object prop;
4388 if (CONSP (prop)
4389 && CONSP (XCAR (prop))
4390 && !EQ (Qmargin, XCAR (XCAR (prop))))
4392 /* A list of sub-properties. */
4393 while (CONSP (prop))
4395 if (single_display_spec_intangible_p (XCAR (prop)))
4396 return 1;
4397 prop = XCDR (prop);
4400 else if (VECTORP (prop))
4402 /* A vector of sub-properties. */
4403 int i;
4404 for (i = 0; i < ASIZE (prop); ++i)
4405 if (single_display_spec_intangible_p (AREF (prop, i)))
4406 return 1;
4408 else
4409 return single_display_spec_intangible_p (prop);
4411 return 0;
4415 /* Return 1 if PROP is a display sub-property value containing STRING. */
4417 static int
4418 single_display_spec_string_p (prop, string)
4419 Lisp_Object prop, string;
4421 if (EQ (string, prop))
4422 return 1;
4424 /* Skip over `when FORM'. */
4425 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4427 prop = XCDR (prop);
4428 if (!CONSP (prop))
4429 return 0;
4430 prop = XCDR (prop);
4433 if (CONSP (prop))
4434 /* Skip over `margin LOCATION'. */
4435 if (EQ (XCAR (prop), Qmargin))
4437 prop = XCDR (prop);
4438 if (!CONSP (prop))
4439 return 0;
4441 prop = XCDR (prop);
4442 if (!CONSP (prop))
4443 return 0;
4446 return CONSP (prop) && EQ (XCAR (prop), string);
4450 /* Return 1 if STRING appears in the `display' property PROP. */
4452 static int
4453 display_prop_string_p (prop, string)
4454 Lisp_Object prop, string;
4456 if (CONSP (prop)
4457 && CONSP (XCAR (prop))
4458 && !EQ (Qmargin, XCAR (XCAR (prop))))
4460 /* A list of sub-properties. */
4461 while (CONSP (prop))
4463 if (single_display_spec_string_p (XCAR (prop), string))
4464 return 1;
4465 prop = XCDR (prop);
4468 else if (VECTORP (prop))
4470 /* A vector of sub-properties. */
4471 int i;
4472 for (i = 0; i < ASIZE (prop); ++i)
4473 if (single_display_spec_string_p (AREF (prop, i), string))
4474 return 1;
4476 else
4477 return single_display_spec_string_p (prop, string);
4479 return 0;
4483 /* Determine from which buffer position in W's buffer STRING comes
4484 from. AROUND_CHARPOS is an approximate position where it could
4485 be from. Value is the buffer position or 0 if it couldn't be
4486 determined.
4488 W's buffer must be current.
4490 This function is necessary because we don't record buffer positions
4491 in glyphs generated from strings (to keep struct glyph small).
4492 This function may only use code that doesn't eval because it is
4493 called asynchronously from note_mouse_highlight. */
4496 string_buffer_position (w, string, around_charpos)
4497 struct window *w;
4498 Lisp_Object string;
4499 int around_charpos;
4501 Lisp_Object limit, prop, pos;
4502 const int MAX_DISTANCE = 1000;
4503 int found = 0;
4505 pos = make_number (around_charpos);
4506 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4507 while (!found && !EQ (pos, limit))
4509 prop = Fget_char_property (pos, Qdisplay, Qnil);
4510 if (!NILP (prop) && display_prop_string_p (prop, string))
4511 found = 1;
4512 else
4513 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4516 if (!found)
4518 pos = make_number (around_charpos);
4519 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4520 while (!found && !EQ (pos, limit))
4522 prop = Fget_char_property (pos, Qdisplay, Qnil);
4523 if (!NILP (prop) && display_prop_string_p (prop, string))
4524 found = 1;
4525 else
4526 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4527 limit);
4531 return found ? XINT (pos) : 0;
4536 /***********************************************************************
4537 `composition' property
4538 ***********************************************************************/
4540 /* Set up iterator IT from `composition' property at its current
4541 position. Called from handle_stop. */
4543 static enum prop_handled
4544 handle_composition_prop (it)
4545 struct it *it;
4547 Lisp_Object prop, string;
4548 int pos, pos_byte, end;
4549 enum prop_handled handled = HANDLED_NORMALLY;
4551 if (STRINGP (it->string))
4553 pos = IT_STRING_CHARPOS (*it);
4554 pos_byte = IT_STRING_BYTEPOS (*it);
4555 string = it->string;
4557 else
4559 pos = IT_CHARPOS (*it);
4560 pos_byte = IT_BYTEPOS (*it);
4561 string = Qnil;
4564 /* If there's a valid composition and point is not inside of the
4565 composition (in the case that the composition is from the current
4566 buffer), draw a glyph composed from the composition components. */
4567 if (find_composition (pos, -1, &pos, &end, &prop, string)
4568 && COMPOSITION_VALID_P (pos, end, prop)
4569 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4571 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4573 if (id >= 0)
4575 struct composition *cmp = composition_table[id];
4577 if (cmp->glyph_len == 0)
4579 /* No glyph. */
4580 if (STRINGP (it->string))
4582 IT_STRING_CHARPOS (*it) = end;
4583 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4584 end);
4586 else
4588 IT_CHARPOS (*it) = end;
4589 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4591 return HANDLED_RECOMPUTE_PROPS;
4594 it->stop_charpos = end;
4595 push_it (it);
4597 it->method = GET_FROM_COMPOSITION;
4598 it->cmp_id = id;
4599 it->cmp_len = COMPOSITION_LENGTH (prop);
4600 /* For a terminal, draw only the first character of the
4601 components. */
4602 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4603 it->len = (STRINGP (it->string)
4604 ? string_char_to_byte (it->string, end)
4605 : CHAR_TO_BYTE (end)) - pos_byte;
4606 handled = HANDLED_RETURN;
4610 return handled;
4615 /***********************************************************************
4616 Overlay strings
4617 ***********************************************************************/
4619 /* The following structure is used to record overlay strings for
4620 later sorting in load_overlay_strings. */
4622 struct overlay_entry
4624 Lisp_Object overlay;
4625 Lisp_Object string;
4626 int priority;
4627 int after_string_p;
4631 /* Set up iterator IT from overlay strings at its current position.
4632 Called from handle_stop. */
4634 static enum prop_handled
4635 handle_overlay_change (it)
4636 struct it *it;
4638 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4639 return HANDLED_RECOMPUTE_PROPS;
4640 else
4641 return HANDLED_NORMALLY;
4645 /* Set up the next overlay string for delivery by IT, if there is an
4646 overlay string to deliver. Called by set_iterator_to_next when the
4647 end of the current overlay string is reached. If there are more
4648 overlay strings to display, IT->string and
4649 IT->current.overlay_string_index are set appropriately here.
4650 Otherwise IT->string is set to nil. */
4652 static void
4653 next_overlay_string (it)
4654 struct it *it;
4656 ++it->current.overlay_string_index;
4657 if (it->current.overlay_string_index == it->n_overlay_strings)
4659 /* No more overlay strings. Restore IT's settings to what
4660 they were before overlay strings were processed, and
4661 continue to deliver from current_buffer. */
4662 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4664 pop_it (it);
4665 xassert (it->sp > 0
4666 || it->method == GET_FROM_COMPOSITION
4667 || (NILP (it->string)
4668 && it->method == GET_FROM_BUFFER
4669 && it->stop_charpos >= BEGV
4670 && it->stop_charpos <= it->end_charpos));
4671 it->current.overlay_string_index = -1;
4672 it->n_overlay_strings = 0;
4674 /* If we're at the end of the buffer, record that we have
4675 processed the overlay strings there already, so that
4676 next_element_from_buffer doesn't try it again. */
4677 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4678 it->overlay_strings_at_end_processed_p = 1;
4680 /* If we have to display `...' for invisible text, set
4681 the iterator up for that. */
4682 if (display_ellipsis_p)
4683 setup_for_ellipsis (it, 0);
4685 else
4687 /* There are more overlay strings to process. If
4688 IT->current.overlay_string_index has advanced to a position
4689 where we must load IT->overlay_strings with more strings, do
4690 it. */
4691 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4693 if (it->current.overlay_string_index && i == 0)
4694 load_overlay_strings (it, 0);
4696 /* Initialize IT to deliver display elements from the overlay
4697 string. */
4698 it->string = it->overlay_strings[i];
4699 it->multibyte_p = STRING_MULTIBYTE (it->string);
4700 SET_TEXT_POS (it->current.string_pos, 0, 0);
4701 it->method = GET_FROM_STRING;
4702 it->stop_charpos = 0;
4705 CHECK_IT (it);
4709 /* Compare two overlay_entry structures E1 and E2. Used as a
4710 comparison function for qsort in load_overlay_strings. Overlay
4711 strings for the same position are sorted so that
4713 1. All after-strings come in front of before-strings, except
4714 when they come from the same overlay.
4716 2. Within after-strings, strings are sorted so that overlay strings
4717 from overlays with higher priorities come first.
4719 2. Within before-strings, strings are sorted so that overlay
4720 strings from overlays with higher priorities come last.
4722 Value is analogous to strcmp. */
4725 static int
4726 compare_overlay_entries (e1, e2)
4727 void *e1, *e2;
4729 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4730 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4731 int result;
4733 if (entry1->after_string_p != entry2->after_string_p)
4735 /* Let after-strings appear in front of before-strings if
4736 they come from different overlays. */
4737 if (EQ (entry1->overlay, entry2->overlay))
4738 result = entry1->after_string_p ? 1 : -1;
4739 else
4740 result = entry1->after_string_p ? -1 : 1;
4742 else if (entry1->after_string_p)
4743 /* After-strings sorted in order of decreasing priority. */
4744 result = entry2->priority - entry1->priority;
4745 else
4746 /* Before-strings sorted in order of increasing priority. */
4747 result = entry1->priority - entry2->priority;
4749 return result;
4753 /* Load the vector IT->overlay_strings with overlay strings from IT's
4754 current buffer position, or from CHARPOS if that is > 0. Set
4755 IT->n_overlays to the total number of overlay strings found.
4757 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4758 a time. On entry into load_overlay_strings,
4759 IT->current.overlay_string_index gives the number of overlay
4760 strings that have already been loaded by previous calls to this
4761 function.
4763 IT->add_overlay_start contains an additional overlay start
4764 position to consider for taking overlay strings from, if non-zero.
4765 This position comes into play when the overlay has an `invisible'
4766 property, and both before and after-strings. When we've skipped to
4767 the end of the overlay, because of its `invisible' property, we
4768 nevertheless want its before-string to appear.
4769 IT->add_overlay_start will contain the overlay start position
4770 in this case.
4772 Overlay strings are sorted so that after-string strings come in
4773 front of before-string strings. Within before and after-strings,
4774 strings are sorted by overlay priority. See also function
4775 compare_overlay_entries. */
4777 static void
4778 load_overlay_strings (it, charpos)
4779 struct it *it;
4780 int charpos;
4782 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4783 Lisp_Object overlay, window, str, invisible;
4784 struct Lisp_Overlay *ov;
4785 int start, end;
4786 int size = 20;
4787 int n = 0, i, j, invis_p;
4788 struct overlay_entry *entries
4789 = (struct overlay_entry *) alloca (size * sizeof *entries);
4791 if (charpos <= 0)
4792 charpos = IT_CHARPOS (*it);
4794 /* Append the overlay string STRING of overlay OVERLAY to vector
4795 `entries' which has size `size' and currently contains `n'
4796 elements. AFTER_P non-zero means STRING is an after-string of
4797 OVERLAY. */
4798 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4799 do \
4801 Lisp_Object priority; \
4803 if (n == size) \
4805 int new_size = 2 * size; \
4806 struct overlay_entry *old = entries; \
4807 entries = \
4808 (struct overlay_entry *) alloca (new_size \
4809 * sizeof *entries); \
4810 bcopy (old, entries, size * sizeof *entries); \
4811 size = new_size; \
4814 entries[n].string = (STRING); \
4815 entries[n].overlay = (OVERLAY); \
4816 priority = Foverlay_get ((OVERLAY), Qpriority); \
4817 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4818 entries[n].after_string_p = (AFTER_P); \
4819 ++n; \
4821 while (0)
4823 /* Process overlay before the overlay center. */
4824 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4826 XSETMISC (overlay, ov);
4827 xassert (OVERLAYP (overlay));
4828 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4829 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4831 if (end < charpos)
4832 break;
4834 /* Skip this overlay if it doesn't start or end at IT's current
4835 position. */
4836 if (end != charpos && start != charpos)
4837 continue;
4839 /* Skip this overlay if it doesn't apply to IT->w. */
4840 window = Foverlay_get (overlay, Qwindow);
4841 if (WINDOWP (window) && XWINDOW (window) != it->w)
4842 continue;
4844 /* If the text ``under'' the overlay is invisible, both before-
4845 and after-strings from this overlay are visible; start and
4846 end position are indistinguishable. */
4847 invisible = Foverlay_get (overlay, Qinvisible);
4848 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4850 /* If overlay has a non-empty before-string, record it. */
4851 if ((start == charpos || (end == charpos && invis_p))
4852 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4853 && SCHARS (str))
4854 RECORD_OVERLAY_STRING (overlay, str, 0);
4856 /* If overlay has a non-empty after-string, record it. */
4857 if ((end == charpos || (start == charpos && invis_p))
4858 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4859 && SCHARS (str))
4860 RECORD_OVERLAY_STRING (overlay, str, 1);
4863 /* Process overlays after the overlay center. */
4864 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4866 XSETMISC (overlay, ov);
4867 xassert (OVERLAYP (overlay));
4868 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4869 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4871 if (start > charpos)
4872 break;
4874 /* Skip this overlay if it doesn't start or end at IT's current
4875 position. */
4876 if (end != charpos && start != charpos)
4877 continue;
4879 /* Skip this overlay if it doesn't apply to IT->w. */
4880 window = Foverlay_get (overlay, Qwindow);
4881 if (WINDOWP (window) && XWINDOW (window) != it->w)
4882 continue;
4884 /* If the text ``under'' the overlay is invisible, it has a zero
4885 dimension, and both before- and after-strings apply. */
4886 invisible = Foverlay_get (overlay, Qinvisible);
4887 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4889 /* If overlay has a non-empty before-string, record it. */
4890 if ((start == charpos || (end == charpos && invis_p))
4891 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4892 && SCHARS (str))
4893 RECORD_OVERLAY_STRING (overlay, str, 0);
4895 /* If overlay has a non-empty after-string, record it. */
4896 if ((end == charpos || (start == charpos && invis_p))
4897 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4898 && SCHARS (str))
4899 RECORD_OVERLAY_STRING (overlay, str, 1);
4902 #undef RECORD_OVERLAY_STRING
4904 /* Sort entries. */
4905 if (n > 1)
4906 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4908 /* Record the total number of strings to process. */
4909 it->n_overlay_strings = n;
4911 /* IT->current.overlay_string_index is the number of overlay strings
4912 that have already been consumed by IT. Copy some of the
4913 remaining overlay strings to IT->overlay_strings. */
4914 i = 0;
4915 j = it->current.overlay_string_index;
4916 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4918 it->overlay_strings[i] = entries[j].string;
4919 it->string_overlays[i++] = entries[j++].overlay;
4922 CHECK_IT (it);
4926 /* Get the first chunk of overlay strings at IT's current buffer
4927 position, or at CHARPOS if that is > 0. Value is non-zero if at
4928 least one overlay string was found. */
4930 static int
4931 get_overlay_strings_1 (it, charpos, compute_stop_p)
4932 struct it *it;
4933 int charpos;
4934 int compute_stop_p;
4936 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4937 process. This fills IT->overlay_strings with strings, and sets
4938 IT->n_overlay_strings to the total number of strings to process.
4939 IT->pos.overlay_string_index has to be set temporarily to zero
4940 because load_overlay_strings needs this; it must be set to -1
4941 when no overlay strings are found because a zero value would
4942 indicate a position in the first overlay string. */
4943 it->current.overlay_string_index = 0;
4944 load_overlay_strings (it, charpos);
4946 /* If we found overlay strings, set up IT to deliver display
4947 elements from the first one. Otherwise set up IT to deliver
4948 from current_buffer. */
4949 if (it->n_overlay_strings)
4951 /* Make sure we know settings in current_buffer, so that we can
4952 restore meaningful values when we're done with the overlay
4953 strings. */
4954 if (compute_stop_p)
4955 compute_stop_pos (it);
4956 xassert (it->face_id >= 0);
4958 /* Save IT's settings. They are restored after all overlay
4959 strings have been processed. */
4960 xassert (!compute_stop_p || it->sp == 0);
4961 push_it (it);
4963 /* Set up IT to deliver display elements from the first overlay
4964 string. */
4965 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4966 it->string = it->overlay_strings[0];
4967 it->from_overlay = Qnil;
4968 it->stop_charpos = 0;
4969 xassert (STRINGP (it->string));
4970 it->end_charpos = SCHARS (it->string);
4971 it->multibyte_p = STRING_MULTIBYTE (it->string);
4972 it->method = GET_FROM_STRING;
4973 return 1;
4976 it->current.overlay_string_index = -1;
4977 return 0;
4980 static int
4981 get_overlay_strings (it, charpos)
4982 struct it *it;
4983 int charpos;
4985 it->string = Qnil;
4986 it->method = GET_FROM_BUFFER;
4988 (void) get_overlay_strings_1 (it, charpos, 1);
4990 CHECK_IT (it);
4992 /* Value is non-zero if we found at least one overlay string. */
4993 return STRINGP (it->string);
4998 /***********************************************************************
4999 Saving and restoring state
5000 ***********************************************************************/
5002 /* Save current settings of IT on IT->stack. Called, for example,
5003 before setting up IT for an overlay string, to be able to restore
5004 IT's settings to what they were after the overlay string has been
5005 processed. */
5007 static void
5008 push_it (it)
5009 struct it *it;
5011 struct iterator_stack_entry *p;
5013 xassert (it->sp < IT_STACK_SIZE);
5014 p = it->stack + it->sp;
5016 p->stop_charpos = it->stop_charpos;
5017 xassert (it->face_id >= 0);
5018 p->face_id = it->face_id;
5019 p->string = it->string;
5020 p->method = it->method;
5021 p->from_overlay = it->from_overlay;
5022 switch (p->method)
5024 case GET_FROM_IMAGE:
5025 p->u.image.object = it->object;
5026 p->u.image.image_id = it->image_id;
5027 p->u.image.slice = it->slice;
5028 break;
5029 case GET_FROM_COMPOSITION:
5030 p->u.comp.object = it->object;
5031 p->u.comp.c = it->c;
5032 p->u.comp.len = it->len;
5033 p->u.comp.cmp_id = it->cmp_id;
5034 p->u.comp.cmp_len = it->cmp_len;
5035 break;
5036 case GET_FROM_STRETCH:
5037 p->u.stretch.object = it->object;
5038 break;
5040 p->position = it->position;
5041 p->current = it->current;
5042 p->end_charpos = it->end_charpos;
5043 p->string_nchars = it->string_nchars;
5044 p->area = it->area;
5045 p->multibyte_p = it->multibyte_p;
5046 p->space_width = it->space_width;
5047 p->font_height = it->font_height;
5048 p->voffset = it->voffset;
5049 p->string_from_display_prop_p = it->string_from_display_prop_p;
5050 p->display_ellipsis_p = 0;
5051 ++it->sp;
5055 /* Restore IT's settings from IT->stack. Called, for example, when no
5056 more overlay strings must be processed, and we return to delivering
5057 display elements from a buffer, or when the end of a string from a
5058 `display' property is reached and we return to delivering display
5059 elements from an overlay string, or from a buffer. */
5061 static void
5062 pop_it (it)
5063 struct it *it;
5065 struct iterator_stack_entry *p;
5067 xassert (it->sp > 0);
5068 --it->sp;
5069 p = it->stack + it->sp;
5070 it->stop_charpos = p->stop_charpos;
5071 it->face_id = p->face_id;
5072 it->current = p->current;
5073 it->position = p->position;
5074 it->string = p->string;
5075 it->from_overlay = p->from_overlay;
5076 if (NILP (it->string))
5077 SET_TEXT_POS (it->current.string_pos, -1, -1);
5078 it->method = p->method;
5079 switch (it->method)
5081 case GET_FROM_IMAGE:
5082 it->image_id = p->u.image.image_id;
5083 it->object = p->u.image.object;
5084 it->slice = p->u.image.slice;
5085 break;
5086 case GET_FROM_COMPOSITION:
5087 it->object = p->u.comp.object;
5088 it->c = p->u.comp.c;
5089 it->len = p->u.comp.len;
5090 it->cmp_id = p->u.comp.cmp_id;
5091 it->cmp_len = p->u.comp.cmp_len;
5092 break;
5093 case GET_FROM_STRETCH:
5094 it->object = p->u.comp.object;
5095 break;
5096 case GET_FROM_BUFFER:
5097 it->object = it->w->buffer;
5098 break;
5099 case GET_FROM_STRING:
5100 it->object = it->string;
5101 break;
5103 it->end_charpos = p->end_charpos;
5104 it->string_nchars = p->string_nchars;
5105 it->area = p->area;
5106 it->multibyte_p = p->multibyte_p;
5107 it->space_width = p->space_width;
5108 it->font_height = p->font_height;
5109 it->voffset = p->voffset;
5110 it->string_from_display_prop_p = p->string_from_display_prop_p;
5115 /***********************************************************************
5116 Moving over lines
5117 ***********************************************************************/
5119 /* Set IT's current position to the previous line start. */
5121 static void
5122 back_to_previous_line_start (it)
5123 struct it *it;
5125 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5126 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5130 /* Move IT to the next line start.
5132 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5133 we skipped over part of the text (as opposed to moving the iterator
5134 continuously over the text). Otherwise, don't change the value
5135 of *SKIPPED_P.
5137 Newlines may come from buffer text, overlay strings, or strings
5138 displayed via the `display' property. That's the reason we can't
5139 simply use find_next_newline_no_quit.
5141 Note that this function may not skip over invisible text that is so
5142 because of text properties and immediately follows a newline. If
5143 it would, function reseat_at_next_visible_line_start, when called
5144 from set_iterator_to_next, would effectively make invisible
5145 characters following a newline part of the wrong glyph row, which
5146 leads to wrong cursor motion. */
5148 static int
5149 forward_to_next_line_start (it, skipped_p)
5150 struct it *it;
5151 int *skipped_p;
5153 int old_selective, newline_found_p, n;
5154 const int MAX_NEWLINE_DISTANCE = 500;
5156 /* If already on a newline, just consume it to avoid unintended
5157 skipping over invisible text below. */
5158 if (it->what == IT_CHARACTER
5159 && it->c == '\n'
5160 && CHARPOS (it->position) == IT_CHARPOS (*it))
5162 set_iterator_to_next (it, 0);
5163 it->c = 0;
5164 return 1;
5167 /* Don't handle selective display in the following. It's (a)
5168 unnecessary because it's done by the caller, and (b) leads to an
5169 infinite recursion because next_element_from_ellipsis indirectly
5170 calls this function. */
5171 old_selective = it->selective;
5172 it->selective = 0;
5174 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5175 from buffer text. */
5176 for (n = newline_found_p = 0;
5177 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5178 n += STRINGP (it->string) ? 0 : 1)
5180 if (!get_next_display_element (it))
5181 return 0;
5182 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5183 set_iterator_to_next (it, 0);
5186 /* If we didn't find a newline near enough, see if we can use a
5187 short-cut. */
5188 if (!newline_found_p)
5190 int start = IT_CHARPOS (*it);
5191 int limit = find_next_newline_no_quit (start, 1);
5192 Lisp_Object pos;
5194 xassert (!STRINGP (it->string));
5196 /* If there isn't any `display' property in sight, and no
5197 overlays, we can just use the position of the newline in
5198 buffer text. */
5199 if (it->stop_charpos >= limit
5200 || ((pos = Fnext_single_property_change (make_number (start),
5201 Qdisplay,
5202 Qnil, make_number (limit)),
5203 NILP (pos))
5204 && next_overlay_change (start) == ZV))
5206 IT_CHARPOS (*it) = limit;
5207 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5208 *skipped_p = newline_found_p = 1;
5210 else
5212 while (get_next_display_element (it)
5213 && !newline_found_p)
5215 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5216 set_iterator_to_next (it, 0);
5221 it->selective = old_selective;
5222 return newline_found_p;
5226 /* Set IT's current position to the previous visible line start. Skip
5227 invisible text that is so either due to text properties or due to
5228 selective display. Caution: this does not change IT->current_x and
5229 IT->hpos. */
5231 static void
5232 back_to_previous_visible_line_start (it)
5233 struct it *it;
5235 while (IT_CHARPOS (*it) > BEGV)
5237 back_to_previous_line_start (it);
5239 if (IT_CHARPOS (*it) <= BEGV)
5240 break;
5242 /* If selective > 0, then lines indented more than that values
5243 are invisible. */
5244 if (it->selective > 0
5245 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5246 (double) it->selective)) /* iftc */
5247 continue;
5249 /* Check the newline before point for invisibility. */
5251 Lisp_Object prop;
5252 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5253 Qinvisible, it->window);
5254 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5255 continue;
5258 if (IT_CHARPOS (*it) <= BEGV)
5259 break;
5262 struct it it2;
5263 int pos;
5264 int beg, end;
5265 Lisp_Object val, overlay;
5267 /* If newline is part of a composition, continue from start of composition */
5268 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5269 && beg < IT_CHARPOS (*it))
5270 goto replaced;
5272 /* If newline is replaced by a display property, find start of overlay
5273 or interval and continue search from that point. */
5274 it2 = *it;
5275 pos = --IT_CHARPOS (it2);
5276 --IT_BYTEPOS (it2);
5277 it2.sp = 0;
5278 if (handle_display_prop (&it2) == HANDLED_RETURN
5279 && !NILP (val = get_char_property_and_overlay
5280 (make_number (pos), Qdisplay, Qnil, &overlay))
5281 && (OVERLAYP (overlay)
5282 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5283 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5284 goto replaced;
5286 /* Newline is not replaced by anything -- so we are done. */
5287 break;
5289 replaced:
5290 if (beg < BEGV)
5291 beg = BEGV;
5292 IT_CHARPOS (*it) = beg;
5293 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5297 it->continuation_lines_width = 0;
5299 xassert (IT_CHARPOS (*it) >= BEGV);
5300 xassert (IT_CHARPOS (*it) == BEGV
5301 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5302 CHECK_IT (it);
5306 /* Reseat iterator IT at the previous visible line start. Skip
5307 invisible text that is so either due to text properties or due to
5308 selective display. At the end, update IT's overlay information,
5309 face information etc. */
5311 void
5312 reseat_at_previous_visible_line_start (it)
5313 struct it *it;
5315 back_to_previous_visible_line_start (it);
5316 reseat (it, it->current.pos, 1);
5317 CHECK_IT (it);
5321 /* Reseat iterator IT on the next visible line start in the current
5322 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5323 preceding the line start. Skip over invisible text that is so
5324 because of selective display. Compute faces, overlays etc at the
5325 new position. Note that this function does not skip over text that
5326 is invisible because of text properties. */
5328 static void
5329 reseat_at_next_visible_line_start (it, on_newline_p)
5330 struct it *it;
5331 int on_newline_p;
5333 int newline_found_p, skipped_p = 0;
5335 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5337 /* Skip over lines that are invisible because they are indented
5338 more than the value of IT->selective. */
5339 if (it->selective > 0)
5340 while (IT_CHARPOS (*it) < ZV
5341 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5342 (double) it->selective)) /* iftc */
5344 xassert (IT_BYTEPOS (*it) == BEGV
5345 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5346 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5349 /* Position on the newline if that's what's requested. */
5350 if (on_newline_p && newline_found_p)
5352 if (STRINGP (it->string))
5354 if (IT_STRING_CHARPOS (*it) > 0)
5356 --IT_STRING_CHARPOS (*it);
5357 --IT_STRING_BYTEPOS (*it);
5360 else if (IT_CHARPOS (*it) > BEGV)
5362 --IT_CHARPOS (*it);
5363 --IT_BYTEPOS (*it);
5364 reseat (it, it->current.pos, 0);
5367 else if (skipped_p)
5368 reseat (it, it->current.pos, 0);
5370 CHECK_IT (it);
5375 /***********************************************************************
5376 Changing an iterator's position
5377 ***********************************************************************/
5379 /* Change IT's current position to POS in current_buffer. If FORCE_P
5380 is non-zero, always check for text properties at the new position.
5381 Otherwise, text properties are only looked up if POS >=
5382 IT->check_charpos of a property. */
5384 static void
5385 reseat (it, pos, force_p)
5386 struct it *it;
5387 struct text_pos pos;
5388 int force_p;
5390 int original_pos = IT_CHARPOS (*it);
5392 reseat_1 (it, pos, 0);
5394 /* Determine where to check text properties. Avoid doing it
5395 where possible because text property lookup is very expensive. */
5396 if (force_p
5397 || CHARPOS (pos) > it->stop_charpos
5398 || CHARPOS (pos) < original_pos)
5399 handle_stop (it);
5401 CHECK_IT (it);
5405 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5406 IT->stop_pos to POS, also. */
5408 static void
5409 reseat_1 (it, pos, set_stop_p)
5410 struct it *it;
5411 struct text_pos pos;
5412 int set_stop_p;
5414 /* Don't call this function when scanning a C string. */
5415 xassert (it->s == NULL);
5417 /* POS must be a reasonable value. */
5418 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5420 it->current.pos = it->position = pos;
5421 it->end_charpos = ZV;
5422 it->dpvec = NULL;
5423 it->current.dpvec_index = -1;
5424 it->current.overlay_string_index = -1;
5425 IT_STRING_CHARPOS (*it) = -1;
5426 IT_STRING_BYTEPOS (*it) = -1;
5427 it->string = Qnil;
5428 it->method = GET_FROM_BUFFER;
5429 it->object = it->w->buffer;
5430 it->area = TEXT_AREA;
5431 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5432 it->sp = 0;
5433 it->string_from_display_prop_p = 0;
5434 it->face_before_selective_p = 0;
5436 if (set_stop_p)
5437 it->stop_charpos = CHARPOS (pos);
5441 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5442 If S is non-null, it is a C string to iterate over. Otherwise,
5443 STRING gives a Lisp string to iterate over.
5445 If PRECISION > 0, don't return more then PRECISION number of
5446 characters from the string.
5448 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5449 characters have been returned. FIELD_WIDTH < 0 means an infinite
5450 field width.
5452 MULTIBYTE = 0 means disable processing of multibyte characters,
5453 MULTIBYTE > 0 means enable it,
5454 MULTIBYTE < 0 means use IT->multibyte_p.
5456 IT must be initialized via a prior call to init_iterator before
5457 calling this function. */
5459 static void
5460 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5461 struct it *it;
5462 unsigned char *s;
5463 Lisp_Object string;
5464 int charpos;
5465 int precision, field_width, multibyte;
5467 /* No region in strings. */
5468 it->region_beg_charpos = it->region_end_charpos = -1;
5470 /* No text property checks performed by default, but see below. */
5471 it->stop_charpos = -1;
5473 /* Set iterator position and end position. */
5474 bzero (&it->current, sizeof it->current);
5475 it->current.overlay_string_index = -1;
5476 it->current.dpvec_index = -1;
5477 xassert (charpos >= 0);
5479 /* If STRING is specified, use its multibyteness, otherwise use the
5480 setting of MULTIBYTE, if specified. */
5481 if (multibyte >= 0)
5482 it->multibyte_p = multibyte > 0;
5484 if (s == NULL)
5486 xassert (STRINGP (string));
5487 it->string = string;
5488 it->s = NULL;
5489 it->end_charpos = it->string_nchars = SCHARS (string);
5490 it->method = GET_FROM_STRING;
5491 it->current.string_pos = string_pos (charpos, string);
5493 else
5495 it->s = s;
5496 it->string = Qnil;
5498 /* Note that we use IT->current.pos, not it->current.string_pos,
5499 for displaying C strings. */
5500 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5501 if (it->multibyte_p)
5503 it->current.pos = c_string_pos (charpos, s, 1);
5504 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5506 else
5508 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5509 it->end_charpos = it->string_nchars = strlen (s);
5512 it->method = GET_FROM_C_STRING;
5515 /* PRECISION > 0 means don't return more than PRECISION characters
5516 from the string. */
5517 if (precision > 0 && it->end_charpos - charpos > precision)
5518 it->end_charpos = it->string_nchars = charpos + precision;
5520 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5521 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5522 FIELD_WIDTH < 0 means infinite field width. This is useful for
5523 padding with `-' at the end of a mode line. */
5524 if (field_width < 0)
5525 field_width = INFINITY;
5526 if (field_width > it->end_charpos - charpos)
5527 it->end_charpos = charpos + field_width;
5529 /* Use the standard display table for displaying strings. */
5530 if (DISP_TABLE_P (Vstandard_display_table))
5531 it->dp = XCHAR_TABLE (Vstandard_display_table);
5533 it->stop_charpos = charpos;
5534 CHECK_IT (it);
5539 /***********************************************************************
5540 Iteration
5541 ***********************************************************************/
5543 /* Map enum it_method value to corresponding next_element_from_* function. */
5545 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5547 next_element_from_buffer,
5548 next_element_from_display_vector,
5549 next_element_from_composition,
5550 next_element_from_string,
5551 next_element_from_c_string,
5552 next_element_from_image,
5553 next_element_from_stretch
5557 /* Load IT's display element fields with information about the next
5558 display element from the current position of IT. Value is zero if
5559 end of buffer (or C string) is reached. */
5561 static struct frame *last_escape_glyph_frame = NULL;
5562 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5563 static int last_escape_glyph_merged_face_id = 0;
5566 get_next_display_element (it)
5567 struct it *it;
5569 /* Non-zero means that we found a display element. Zero means that
5570 we hit the end of what we iterate over. Performance note: the
5571 function pointer `method' used here turns out to be faster than
5572 using a sequence of if-statements. */
5573 int success_p;
5575 get_next:
5576 success_p = (*get_next_element[it->method]) (it);
5578 if (it->what == IT_CHARACTER)
5580 /* Map via display table or translate control characters.
5581 IT->c, IT->len etc. have been set to the next character by
5582 the function call above. If we have a display table, and it
5583 contains an entry for IT->c, translate it. Don't do this if
5584 IT->c itself comes from a display table, otherwise we could
5585 end up in an infinite recursion. (An alternative could be to
5586 count the recursion depth of this function and signal an
5587 error when a certain maximum depth is reached.) Is it worth
5588 it? */
5589 if (success_p && it->dpvec == NULL)
5591 Lisp_Object dv;
5593 if (it->dp
5594 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5595 VECTORP (dv)))
5597 struct Lisp_Vector *v = XVECTOR (dv);
5599 /* Return the first character from the display table
5600 entry, if not empty. If empty, don't display the
5601 current character. */
5602 if (v->size)
5604 it->dpvec_char_len = it->len;
5605 it->dpvec = v->contents;
5606 it->dpend = v->contents + v->size;
5607 it->current.dpvec_index = 0;
5608 it->dpvec_face_id = -1;
5609 it->saved_face_id = it->face_id;
5610 it->method = GET_FROM_DISPLAY_VECTOR;
5611 it->ellipsis_p = 0;
5613 else
5615 set_iterator_to_next (it, 0);
5617 goto get_next;
5620 /* Translate control characters into `\003' or `^C' form.
5621 Control characters coming from a display table entry are
5622 currently not translated because we use IT->dpvec to hold
5623 the translation. This could easily be changed but I
5624 don't believe that it is worth doing.
5626 If it->multibyte_p is nonzero, eight-bit characters and
5627 non-printable multibyte characters are also translated to
5628 octal form.
5630 If it->multibyte_p is zero, eight-bit characters that
5631 don't have corresponding multibyte char code are also
5632 translated to octal form. */
5633 else if ((it->c < ' '
5634 && (it->area != TEXT_AREA
5635 /* In mode line, treat \n like other crl chars. */
5636 || (it->c != '\t'
5637 && it->glyph_row && it->glyph_row->mode_line_p)
5638 || (it->c != '\n' && it->c != '\t')))
5639 || (it->multibyte_p
5640 ? ((it->c >= 127
5641 && it->len == 1)
5642 || !CHAR_PRINTABLE_P (it->c)
5643 || (!NILP (Vnobreak_char_display)
5644 && (it->c == 0x8a0 || it->c == 0x8ad
5645 || it->c == 0x920 || it->c == 0x92d
5646 || it->c == 0xe20 || it->c == 0xe2d
5647 || it->c == 0xf20 || it->c == 0xf2d)))
5648 : (it->c >= 127
5649 && (!unibyte_display_via_language_environment
5650 || it->c == unibyte_char_to_multibyte (it->c)))))
5652 /* IT->c is a control character which must be displayed
5653 either as '\003' or as `^C' where the '\\' and '^'
5654 can be defined in the display table. Fill
5655 IT->ctl_chars with glyphs for what we have to
5656 display. Then, set IT->dpvec to these glyphs. */
5657 GLYPH g;
5658 int ctl_len;
5659 int face_id, lface_id = 0 ;
5660 GLYPH escape_glyph;
5662 /* Handle control characters with ^. */
5664 if (it->c < 128 && it->ctl_arrow_p)
5666 g = '^'; /* default glyph for Control */
5667 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5668 if (it->dp
5669 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5670 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5672 g = XINT (DISP_CTRL_GLYPH (it->dp));
5673 lface_id = FAST_GLYPH_FACE (g);
5675 if (lface_id)
5677 g = FAST_GLYPH_CHAR (g);
5678 face_id = merge_faces (it->f, Qt, lface_id,
5679 it->face_id);
5681 else if (it->f == last_escape_glyph_frame
5682 && it->face_id == last_escape_glyph_face_id)
5684 face_id = last_escape_glyph_merged_face_id;
5686 else
5688 /* Merge the escape-glyph face into the current face. */
5689 face_id = merge_faces (it->f, Qescape_glyph, 0,
5690 it->face_id);
5691 last_escape_glyph_frame = it->f;
5692 last_escape_glyph_face_id = it->face_id;
5693 last_escape_glyph_merged_face_id = face_id;
5696 XSETINT (it->ctl_chars[0], g);
5697 g = it->c ^ 0100;
5698 XSETINT (it->ctl_chars[1], g);
5699 ctl_len = 2;
5700 goto display_control;
5703 /* Handle non-break space in the mode where it only gets
5704 highlighting. */
5706 if (EQ (Vnobreak_char_display, Qt)
5707 && (it->c == 0x8a0 || it->c == 0x920
5708 || it->c == 0xe20 || it->c == 0xf20))
5710 /* Merge the no-break-space face into the current face. */
5711 face_id = merge_faces (it->f, Qnobreak_space, 0,
5712 it->face_id);
5714 g = it->c = ' ';
5715 XSETINT (it->ctl_chars[0], g);
5716 ctl_len = 1;
5717 goto display_control;
5720 /* Handle sequences that start with the "escape glyph". */
5722 /* the default escape glyph is \. */
5723 escape_glyph = '\\';
5725 if (it->dp
5726 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5727 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5729 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5730 lface_id = FAST_GLYPH_FACE (escape_glyph);
5732 if (lface_id)
5734 /* The display table specified a face.
5735 Merge it into face_id and also into escape_glyph. */
5736 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5737 face_id = merge_faces (it->f, Qt, lface_id,
5738 it->face_id);
5740 else if (it->f == last_escape_glyph_frame
5741 && it->face_id == last_escape_glyph_face_id)
5743 face_id = last_escape_glyph_merged_face_id;
5745 else
5747 /* Merge the escape-glyph face into the current face. */
5748 face_id = merge_faces (it->f, Qescape_glyph, 0,
5749 it->face_id);
5750 last_escape_glyph_frame = it->f;
5751 last_escape_glyph_face_id = it->face_id;
5752 last_escape_glyph_merged_face_id = face_id;
5755 /* Handle soft hyphens in the mode where they only get
5756 highlighting. */
5758 if (EQ (Vnobreak_char_display, Qt)
5759 && (it->c == 0x8ad || it->c == 0x92d
5760 || it->c == 0xe2d || it->c == 0xf2d))
5762 g = it->c = '-';
5763 XSETINT (it->ctl_chars[0], g);
5764 ctl_len = 1;
5765 goto display_control;
5768 /* Handle non-break space and soft hyphen
5769 with the escape glyph. */
5771 if (it->c == 0x8a0 || it->c == 0x8ad
5772 || it->c == 0x920 || it->c == 0x92d
5773 || it->c == 0xe20 || it->c == 0xe2d
5774 || it->c == 0xf20 || it->c == 0xf2d)
5776 XSETINT (it->ctl_chars[0], escape_glyph);
5777 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5778 XSETINT (it->ctl_chars[1], g);
5779 ctl_len = 2;
5780 goto display_control;
5784 unsigned char str[MAX_MULTIBYTE_LENGTH];
5785 int len;
5786 int i;
5788 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5789 if (SINGLE_BYTE_CHAR_P (it->c))
5790 str[0] = it->c, len = 1;
5791 else
5793 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5794 if (len < 0)
5796 /* It's an invalid character, which shouldn't
5797 happen actually, but due to bugs it may
5798 happen. Let's print the char as is, there's
5799 not much meaningful we can do with it. */
5800 str[0] = it->c;
5801 str[1] = it->c >> 8;
5802 str[2] = it->c >> 16;
5803 str[3] = it->c >> 24;
5804 len = 4;
5808 for (i = 0; i < len; i++)
5810 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5811 /* Insert three more glyphs into IT->ctl_chars for
5812 the octal display of the character. */
5813 g = ((str[i] >> 6) & 7) + '0';
5814 XSETINT (it->ctl_chars[i * 4 + 1], g);
5815 g = ((str[i] >> 3) & 7) + '0';
5816 XSETINT (it->ctl_chars[i * 4 + 2], g);
5817 g = (str[i] & 7) + '0';
5818 XSETINT (it->ctl_chars[i * 4 + 3], g);
5820 ctl_len = len * 4;
5823 display_control:
5824 /* Set up IT->dpvec and return first character from it. */
5825 it->dpvec_char_len = it->len;
5826 it->dpvec = it->ctl_chars;
5827 it->dpend = it->dpvec + ctl_len;
5828 it->current.dpvec_index = 0;
5829 it->dpvec_face_id = face_id;
5830 it->saved_face_id = it->face_id;
5831 it->method = GET_FROM_DISPLAY_VECTOR;
5832 it->ellipsis_p = 0;
5833 goto get_next;
5837 /* Adjust face id for a multibyte character. There are no
5838 multibyte character in unibyte text. */
5839 if (it->multibyte_p
5840 && success_p
5841 && FRAME_WINDOW_P (it->f))
5843 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5844 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5848 /* Is this character the last one of a run of characters with
5849 box? If yes, set IT->end_of_box_run_p to 1. */
5850 if (it->face_box_p
5851 && it->s == NULL)
5853 int face_id;
5854 struct face *face;
5856 it->end_of_box_run_p
5857 = ((face_id = face_after_it_pos (it),
5858 face_id != it->face_id)
5859 && (face = FACE_FROM_ID (it->f, face_id),
5860 face->box == FACE_NO_BOX));
5863 /* Value is 0 if end of buffer or string reached. */
5864 return success_p;
5868 /* Move IT to the next display element.
5870 RESEAT_P non-zero means if called on a newline in buffer text,
5871 skip to the next visible line start.
5873 Functions get_next_display_element and set_iterator_to_next are
5874 separate because I find this arrangement easier to handle than a
5875 get_next_display_element function that also increments IT's
5876 position. The way it is we can first look at an iterator's current
5877 display element, decide whether it fits on a line, and if it does,
5878 increment the iterator position. The other way around we probably
5879 would either need a flag indicating whether the iterator has to be
5880 incremented the next time, or we would have to implement a
5881 decrement position function which would not be easy to write. */
5883 void
5884 set_iterator_to_next (it, reseat_p)
5885 struct it *it;
5886 int reseat_p;
5888 /* Reset flags indicating start and end of a sequence of characters
5889 with box. Reset them at the start of this function because
5890 moving the iterator to a new position might set them. */
5891 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5893 switch (it->method)
5895 case GET_FROM_BUFFER:
5896 /* The current display element of IT is a character from
5897 current_buffer. Advance in the buffer, and maybe skip over
5898 invisible lines that are so because of selective display. */
5899 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5900 reseat_at_next_visible_line_start (it, 0);
5901 else
5903 xassert (it->len != 0);
5904 IT_BYTEPOS (*it) += it->len;
5905 IT_CHARPOS (*it) += 1;
5906 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5908 break;
5910 case GET_FROM_COMPOSITION:
5911 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5912 xassert (it->sp > 0);
5913 pop_it (it);
5914 if (it->method == GET_FROM_STRING)
5916 IT_STRING_BYTEPOS (*it) += it->len;
5917 IT_STRING_CHARPOS (*it) += it->cmp_len;
5918 goto consider_string_end;
5920 else if (it->method == GET_FROM_BUFFER)
5922 IT_BYTEPOS (*it) += it->len;
5923 IT_CHARPOS (*it) += it->cmp_len;
5925 break;
5927 case GET_FROM_C_STRING:
5928 /* Current display element of IT is from a C string. */
5929 IT_BYTEPOS (*it) += it->len;
5930 IT_CHARPOS (*it) += 1;
5931 break;
5933 case GET_FROM_DISPLAY_VECTOR:
5934 /* Current display element of IT is from a display table entry.
5935 Advance in the display table definition. Reset it to null if
5936 end reached, and continue with characters from buffers/
5937 strings. */
5938 ++it->current.dpvec_index;
5940 /* Restore face of the iterator to what they were before the
5941 display vector entry (these entries may contain faces). */
5942 it->face_id = it->saved_face_id;
5944 if (it->dpvec + it->current.dpvec_index == it->dpend)
5946 int recheck_faces = it->ellipsis_p;
5948 if (it->s)
5949 it->method = GET_FROM_C_STRING;
5950 else if (STRINGP (it->string))
5951 it->method = GET_FROM_STRING;
5952 else
5954 it->method = GET_FROM_BUFFER;
5955 it->object = it->w->buffer;
5958 it->dpvec = NULL;
5959 it->current.dpvec_index = -1;
5961 /* Skip over characters which were displayed via IT->dpvec. */
5962 if (it->dpvec_char_len < 0)
5963 reseat_at_next_visible_line_start (it, 1);
5964 else if (it->dpvec_char_len > 0)
5966 if (it->method == GET_FROM_STRING
5967 && it->n_overlay_strings > 0)
5968 it->ignore_overlay_strings_at_pos_p = 1;
5969 it->len = it->dpvec_char_len;
5970 set_iterator_to_next (it, reseat_p);
5973 /* Maybe recheck faces after display vector */
5974 if (recheck_faces)
5975 it->stop_charpos = IT_CHARPOS (*it);
5977 break;
5979 case GET_FROM_STRING:
5980 /* Current display element is a character from a Lisp string. */
5981 xassert (it->s == NULL && STRINGP (it->string));
5982 IT_STRING_BYTEPOS (*it) += it->len;
5983 IT_STRING_CHARPOS (*it) += 1;
5985 consider_string_end:
5987 if (it->current.overlay_string_index >= 0)
5989 /* IT->string is an overlay string. Advance to the
5990 next, if there is one. */
5991 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5992 next_overlay_string (it);
5994 else
5996 /* IT->string is not an overlay string. If we reached
5997 its end, and there is something on IT->stack, proceed
5998 with what is on the stack. This can be either another
5999 string, this time an overlay string, or a buffer. */
6000 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6001 && it->sp > 0)
6003 pop_it (it);
6004 if (it->method == GET_FROM_STRING)
6005 goto consider_string_end;
6008 break;
6010 case GET_FROM_IMAGE:
6011 case GET_FROM_STRETCH:
6012 /* The position etc with which we have to proceed are on
6013 the stack. The position may be at the end of a string,
6014 if the `display' property takes up the whole string. */
6015 xassert (it->sp > 0);
6016 pop_it (it);
6017 if (it->method == GET_FROM_STRING)
6018 goto consider_string_end;
6019 break;
6021 default:
6022 /* There are no other methods defined, so this should be a bug. */
6023 abort ();
6026 xassert (it->method != GET_FROM_STRING
6027 || (STRINGP (it->string)
6028 && IT_STRING_CHARPOS (*it) >= 0));
6031 /* Load IT's display element fields with information about the next
6032 display element which comes from a display table entry or from the
6033 result of translating a control character to one of the forms `^C'
6034 or `\003'.
6036 IT->dpvec holds the glyphs to return as characters.
6037 IT->saved_face_id holds the face id before the display vector--
6038 it is restored into IT->face_idin set_iterator_to_next. */
6040 static int
6041 next_element_from_display_vector (it)
6042 struct it *it;
6044 /* Precondition. */
6045 xassert (it->dpvec && it->current.dpvec_index >= 0);
6047 it->face_id = it->saved_face_id;
6049 if (INTEGERP (*it->dpvec)
6050 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6052 GLYPH g;
6054 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6055 it->c = FAST_GLYPH_CHAR (g);
6056 it->len = CHAR_BYTES (it->c);
6058 /* The entry may contain a face id to use. Such a face id is
6059 the id of a Lisp face, not a realized face. A face id of
6060 zero means no face is specified. */
6061 if (it->dpvec_face_id >= 0)
6062 it->face_id = it->dpvec_face_id;
6063 else
6065 int lface_id = FAST_GLYPH_FACE (g);
6066 if (lface_id > 0)
6067 it->face_id = merge_faces (it->f, Qt, lface_id,
6068 it->saved_face_id);
6071 else
6072 /* Display table entry is invalid. Return a space. */
6073 it->c = ' ', it->len = 1;
6075 /* Don't change position and object of the iterator here. They are
6076 still the values of the character that had this display table
6077 entry or was translated, and that's what we want. */
6078 it->what = IT_CHARACTER;
6079 return 1;
6083 /* Load IT with the next display element from Lisp string IT->string.
6084 IT->current.string_pos is the current position within the string.
6085 If IT->current.overlay_string_index >= 0, the Lisp string is an
6086 overlay string. */
6088 static int
6089 next_element_from_string (it)
6090 struct it *it;
6092 struct text_pos position;
6094 xassert (STRINGP (it->string));
6095 xassert (IT_STRING_CHARPOS (*it) >= 0);
6096 position = it->current.string_pos;
6098 /* Time to check for invisible text? */
6099 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6100 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6102 handle_stop (it);
6104 /* Since a handler may have changed IT->method, we must
6105 recurse here. */
6106 return get_next_display_element (it);
6109 if (it->current.overlay_string_index >= 0)
6111 /* Get the next character from an overlay string. In overlay
6112 strings, There is no field width or padding with spaces to
6113 do. */
6114 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6116 it->what = IT_EOB;
6117 return 0;
6119 else if (STRING_MULTIBYTE (it->string))
6121 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6122 const unsigned char *s = (SDATA (it->string)
6123 + IT_STRING_BYTEPOS (*it));
6124 it->c = string_char_and_length (s, remaining, &it->len);
6126 else
6128 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6129 it->len = 1;
6132 else
6134 /* Get the next character from a Lisp string that is not an
6135 overlay string. Such strings come from the mode line, for
6136 example. We may have to pad with spaces, or truncate the
6137 string. See also next_element_from_c_string. */
6138 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6140 it->what = IT_EOB;
6141 return 0;
6143 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6145 /* Pad with spaces. */
6146 it->c = ' ', it->len = 1;
6147 CHARPOS (position) = BYTEPOS (position) = -1;
6149 else if (STRING_MULTIBYTE (it->string))
6151 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6152 const unsigned char *s = (SDATA (it->string)
6153 + IT_STRING_BYTEPOS (*it));
6154 it->c = string_char_and_length (s, maxlen, &it->len);
6156 else
6158 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6159 it->len = 1;
6163 /* Record what we have and where it came from. */
6164 it->what = IT_CHARACTER;
6165 it->object = it->string;
6166 it->position = position;
6167 return 1;
6171 /* Load IT with next display element from C string IT->s.
6172 IT->string_nchars is the maximum number of characters to return
6173 from the string. IT->end_charpos may be greater than
6174 IT->string_nchars when this function is called, in which case we
6175 may have to return padding spaces. Value is zero if end of string
6176 reached, including padding spaces. */
6178 static int
6179 next_element_from_c_string (it)
6180 struct it *it;
6182 int success_p = 1;
6184 xassert (it->s);
6185 it->what = IT_CHARACTER;
6186 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6187 it->object = Qnil;
6189 /* IT's position can be greater IT->string_nchars in case a field
6190 width or precision has been specified when the iterator was
6191 initialized. */
6192 if (IT_CHARPOS (*it) >= it->end_charpos)
6194 /* End of the game. */
6195 it->what = IT_EOB;
6196 success_p = 0;
6198 else if (IT_CHARPOS (*it) >= it->string_nchars)
6200 /* Pad with spaces. */
6201 it->c = ' ', it->len = 1;
6202 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6204 else if (it->multibyte_p)
6206 /* Implementation note: The calls to strlen apparently aren't a
6207 performance problem because there is no noticeable performance
6208 difference between Emacs running in unibyte or multibyte mode. */
6209 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6210 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6211 maxlen, &it->len);
6213 else
6214 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6216 return success_p;
6220 /* Set up IT to return characters from an ellipsis, if appropriate.
6221 The definition of the ellipsis glyphs may come from a display table
6222 entry. This function Fills IT with the first glyph from the
6223 ellipsis if an ellipsis is to be displayed. */
6225 static int
6226 next_element_from_ellipsis (it)
6227 struct it *it;
6229 if (it->selective_display_ellipsis_p)
6230 setup_for_ellipsis (it, it->len);
6231 else
6233 /* The face at the current position may be different from the
6234 face we find after the invisible text. Remember what it
6235 was in IT->saved_face_id, and signal that it's there by
6236 setting face_before_selective_p. */
6237 it->saved_face_id = it->face_id;
6238 it->method = GET_FROM_BUFFER;
6239 it->object = it->w->buffer;
6240 reseat_at_next_visible_line_start (it, 1);
6241 it->face_before_selective_p = 1;
6244 return get_next_display_element (it);
6248 /* Deliver an image display element. The iterator IT is already
6249 filled with image information (done in handle_display_prop). Value
6250 is always 1. */
6253 static int
6254 next_element_from_image (it)
6255 struct it *it;
6257 it->what = IT_IMAGE;
6258 return 1;
6262 /* Fill iterator IT with next display element from a stretch glyph
6263 property. IT->object is the value of the text property. Value is
6264 always 1. */
6266 static int
6267 next_element_from_stretch (it)
6268 struct it *it;
6270 it->what = IT_STRETCH;
6271 return 1;
6275 /* Load IT with the next display element from current_buffer. Value
6276 is zero if end of buffer reached. IT->stop_charpos is the next
6277 position at which to stop and check for text properties or buffer
6278 end. */
6280 static int
6281 next_element_from_buffer (it)
6282 struct it *it;
6284 int success_p = 1;
6286 /* Check this assumption, otherwise, we would never enter the
6287 if-statement, below. */
6288 xassert (IT_CHARPOS (*it) >= BEGV
6289 && IT_CHARPOS (*it) <= it->stop_charpos);
6291 if (IT_CHARPOS (*it) >= it->stop_charpos)
6293 if (IT_CHARPOS (*it) >= it->end_charpos)
6295 int overlay_strings_follow_p;
6297 /* End of the game, except when overlay strings follow that
6298 haven't been returned yet. */
6299 if (it->overlay_strings_at_end_processed_p)
6300 overlay_strings_follow_p = 0;
6301 else
6303 it->overlay_strings_at_end_processed_p = 1;
6304 overlay_strings_follow_p = get_overlay_strings (it, 0);
6307 if (overlay_strings_follow_p)
6308 success_p = get_next_display_element (it);
6309 else
6311 it->what = IT_EOB;
6312 it->position = it->current.pos;
6313 success_p = 0;
6316 else
6318 handle_stop (it);
6319 return get_next_display_element (it);
6322 else
6324 /* No face changes, overlays etc. in sight, so just return a
6325 character from current_buffer. */
6326 unsigned char *p;
6328 /* Maybe run the redisplay end trigger hook. Performance note:
6329 This doesn't seem to cost measurable time. */
6330 if (it->redisplay_end_trigger_charpos
6331 && it->glyph_row
6332 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6333 run_redisplay_end_trigger_hook (it);
6335 /* Get the next character, maybe multibyte. */
6336 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6337 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6339 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6340 - IT_BYTEPOS (*it));
6341 it->c = string_char_and_length (p, maxlen, &it->len);
6343 else
6344 it->c = *p, it->len = 1;
6346 /* Record what we have and where it came from. */
6347 it->what = IT_CHARACTER;
6348 it->object = it->w->buffer;
6349 it->position = it->current.pos;
6351 /* Normally we return the character found above, except when we
6352 really want to return an ellipsis for selective display. */
6353 if (it->selective)
6355 if (it->c == '\n')
6357 /* A value of selective > 0 means hide lines indented more
6358 than that number of columns. */
6359 if (it->selective > 0
6360 && IT_CHARPOS (*it) + 1 < ZV
6361 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6362 IT_BYTEPOS (*it) + 1,
6363 (double) it->selective)) /* iftc */
6365 success_p = next_element_from_ellipsis (it);
6366 it->dpvec_char_len = -1;
6369 else if (it->c == '\r' && it->selective == -1)
6371 /* A value of selective == -1 means that everything from the
6372 CR to the end of the line is invisible, with maybe an
6373 ellipsis displayed for it. */
6374 success_p = next_element_from_ellipsis (it);
6375 it->dpvec_char_len = -1;
6380 /* Value is zero if end of buffer reached. */
6381 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6382 return success_p;
6386 /* Run the redisplay end trigger hook for IT. */
6388 static void
6389 run_redisplay_end_trigger_hook (it)
6390 struct it *it;
6392 Lisp_Object args[3];
6394 /* IT->glyph_row should be non-null, i.e. we should be actually
6395 displaying something, or otherwise we should not run the hook. */
6396 xassert (it->glyph_row);
6398 /* Set up hook arguments. */
6399 args[0] = Qredisplay_end_trigger_functions;
6400 args[1] = it->window;
6401 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6402 it->redisplay_end_trigger_charpos = 0;
6404 /* Since we are *trying* to run these functions, don't try to run
6405 them again, even if they get an error. */
6406 it->w->redisplay_end_trigger = Qnil;
6407 Frun_hook_with_args (3, args);
6409 /* Notice if it changed the face of the character we are on. */
6410 handle_face_prop (it);
6414 /* Deliver a composition display element. The iterator IT is already
6415 filled with composition information (done in
6416 handle_composition_prop). Value is always 1. */
6418 static int
6419 next_element_from_composition (it)
6420 struct it *it;
6422 it->what = IT_COMPOSITION;
6423 it->position = (STRINGP (it->string)
6424 ? it->current.string_pos
6425 : it->current.pos);
6426 if (STRINGP (it->string))
6427 it->object = it->string;
6428 else
6429 it->object = it->w->buffer;
6430 return 1;
6435 /***********************************************************************
6436 Moving an iterator without producing glyphs
6437 ***********************************************************************/
6439 /* Check if iterator is at a position corresponding to a valid buffer
6440 position after some move_it_ call. */
6442 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6443 ((it)->method == GET_FROM_STRING \
6444 ? IT_STRING_CHARPOS (*it) == 0 \
6445 : 1)
6448 /* Move iterator IT to a specified buffer or X position within one
6449 line on the display without producing glyphs.
6451 OP should be a bit mask including some or all of these bits:
6452 MOVE_TO_X: Stop on reaching x-position TO_X.
6453 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6454 Regardless of OP's value, stop in reaching the end of the display line.
6456 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6457 This means, in particular, that TO_X includes window's horizontal
6458 scroll amount.
6460 The return value has several possible values that
6461 say what condition caused the scan to stop:
6463 MOVE_POS_MATCH_OR_ZV
6464 - when TO_POS or ZV was reached.
6466 MOVE_X_REACHED
6467 -when TO_X was reached before TO_POS or ZV were reached.
6469 MOVE_LINE_CONTINUED
6470 - when we reached the end of the display area and the line must
6471 be continued.
6473 MOVE_LINE_TRUNCATED
6474 - when we reached the end of the display area and the line is
6475 truncated.
6477 MOVE_NEWLINE_OR_CR
6478 - when we stopped at a line end, i.e. a newline or a CR and selective
6479 display is on. */
6481 static enum move_it_result
6482 move_it_in_display_line_to (it, to_charpos, to_x, op)
6483 struct it *it;
6484 int to_charpos, to_x, op;
6486 enum move_it_result result = MOVE_UNDEFINED;
6487 struct glyph_row *saved_glyph_row;
6489 /* Don't produce glyphs in produce_glyphs. */
6490 saved_glyph_row = it->glyph_row;
6491 it->glyph_row = NULL;
6493 #define BUFFER_POS_REACHED_P() \
6494 ((op & MOVE_TO_POS) != 0 \
6495 && BUFFERP (it->object) \
6496 && IT_CHARPOS (*it) >= to_charpos \
6497 && (it->method == GET_FROM_BUFFER \
6498 || (it->method == GET_FROM_DISPLAY_VECTOR \
6499 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6502 while (1)
6504 int x, i, ascent = 0, descent = 0;
6506 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6507 if ((op & MOVE_TO_POS) != 0
6508 && BUFFERP (it->object)
6509 && it->method == GET_FROM_BUFFER
6510 && IT_CHARPOS (*it) > to_charpos)
6512 result = MOVE_POS_MATCH_OR_ZV;
6513 break;
6516 /* Stop when ZV reached.
6517 We used to stop here when TO_CHARPOS reached as well, but that is
6518 too soon if this glyph does not fit on this line. So we handle it
6519 explicitly below. */
6520 if (!get_next_display_element (it)
6521 || (it->truncate_lines_p
6522 && BUFFER_POS_REACHED_P ()))
6524 result = MOVE_POS_MATCH_OR_ZV;
6525 break;
6528 /* The call to produce_glyphs will get the metrics of the
6529 display element IT is loaded with. We record in x the
6530 x-position before this display element in case it does not
6531 fit on the line. */
6532 x = it->current_x;
6534 /* Remember the line height so far in case the next element doesn't
6535 fit on the line. */
6536 if (!it->truncate_lines_p)
6538 ascent = it->max_ascent;
6539 descent = it->max_descent;
6542 PRODUCE_GLYPHS (it);
6544 if (it->area != TEXT_AREA)
6546 set_iterator_to_next (it, 1);
6547 continue;
6550 /* The number of glyphs we get back in IT->nglyphs will normally
6551 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6552 character on a terminal frame, or (iii) a line end. For the
6553 second case, IT->nglyphs - 1 padding glyphs will be present
6554 (on X frames, there is only one glyph produced for a
6555 composite character.
6557 The behavior implemented below means, for continuation lines,
6558 that as many spaces of a TAB as fit on the current line are
6559 displayed there. For terminal frames, as many glyphs of a
6560 multi-glyph character are displayed in the current line, too.
6561 This is what the old redisplay code did, and we keep it that
6562 way. Under X, the whole shape of a complex character must
6563 fit on the line or it will be completely displayed in the
6564 next line.
6566 Note that both for tabs and padding glyphs, all glyphs have
6567 the same width. */
6568 if (it->nglyphs)
6570 /* More than one glyph or glyph doesn't fit on line. All
6571 glyphs have the same width. */
6572 int single_glyph_width = it->pixel_width / it->nglyphs;
6573 int new_x;
6574 int x_before_this_char = x;
6575 int hpos_before_this_char = it->hpos;
6577 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6579 new_x = x + single_glyph_width;
6581 /* We want to leave anything reaching TO_X to the caller. */
6582 if ((op & MOVE_TO_X) && new_x > to_x)
6584 if (BUFFER_POS_REACHED_P ())
6585 goto buffer_pos_reached;
6586 it->current_x = x;
6587 result = MOVE_X_REACHED;
6588 break;
6590 else if (/* Lines are continued. */
6591 !it->truncate_lines_p
6592 && (/* And glyph doesn't fit on the line. */
6593 new_x > it->last_visible_x
6594 /* Or it fits exactly and we're on a window
6595 system frame. */
6596 || (new_x == it->last_visible_x
6597 && FRAME_WINDOW_P (it->f))))
6599 if (/* IT->hpos == 0 means the very first glyph
6600 doesn't fit on the line, e.g. a wide image. */
6601 it->hpos == 0
6602 || (new_x == it->last_visible_x
6603 && FRAME_WINDOW_P (it->f)))
6605 ++it->hpos;
6606 it->current_x = new_x;
6608 /* The character's last glyph just barely fits
6609 in this row. */
6610 if (i == it->nglyphs - 1)
6612 /* If this is the destination position,
6613 return a position *before* it in this row,
6614 now that we know it fits in this row. */
6615 if (BUFFER_POS_REACHED_P ())
6617 it->hpos = hpos_before_this_char;
6618 it->current_x = x_before_this_char;
6619 result = MOVE_POS_MATCH_OR_ZV;
6620 break;
6623 set_iterator_to_next (it, 1);
6624 #ifdef HAVE_WINDOW_SYSTEM
6625 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6627 if (!get_next_display_element (it))
6629 result = MOVE_POS_MATCH_OR_ZV;
6630 break;
6632 if (BUFFER_POS_REACHED_P ())
6634 if (ITERATOR_AT_END_OF_LINE_P (it))
6635 result = MOVE_POS_MATCH_OR_ZV;
6636 else
6637 result = MOVE_LINE_CONTINUED;
6638 break;
6640 if (ITERATOR_AT_END_OF_LINE_P (it))
6642 result = MOVE_NEWLINE_OR_CR;
6643 break;
6646 #endif /* HAVE_WINDOW_SYSTEM */
6649 else
6651 it->current_x = x;
6652 it->max_ascent = ascent;
6653 it->max_descent = descent;
6656 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6657 IT_CHARPOS (*it)));
6658 result = MOVE_LINE_CONTINUED;
6659 break;
6661 else if (BUFFER_POS_REACHED_P ())
6662 goto buffer_pos_reached;
6663 else if (new_x > it->first_visible_x)
6665 /* Glyph is visible. Increment number of glyphs that
6666 would be displayed. */
6667 ++it->hpos;
6669 else
6671 /* Glyph is completely off the left margin of the display
6672 area. Nothing to do. */
6676 if (result != MOVE_UNDEFINED)
6677 break;
6679 else if (BUFFER_POS_REACHED_P ())
6681 buffer_pos_reached:
6682 it->current_x = x;
6683 it->max_ascent = ascent;
6684 it->max_descent = descent;
6685 result = MOVE_POS_MATCH_OR_ZV;
6686 break;
6688 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6690 /* Stop when TO_X specified and reached. This check is
6691 necessary here because of lines consisting of a line end,
6692 only. The line end will not produce any glyphs and we
6693 would never get MOVE_X_REACHED. */
6694 xassert (it->nglyphs == 0);
6695 result = MOVE_X_REACHED;
6696 break;
6699 /* Is this a line end? If yes, we're done. */
6700 if (ITERATOR_AT_END_OF_LINE_P (it))
6702 result = MOVE_NEWLINE_OR_CR;
6703 break;
6706 /* The current display element has been consumed. Advance
6707 to the next. */
6708 set_iterator_to_next (it, 1);
6710 /* Stop if lines are truncated and IT's current x-position is
6711 past the right edge of the window now. */
6712 if (it->truncate_lines_p
6713 && it->current_x >= it->last_visible_x)
6715 #ifdef HAVE_WINDOW_SYSTEM
6716 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6718 if (!get_next_display_element (it)
6719 || BUFFER_POS_REACHED_P ())
6721 result = MOVE_POS_MATCH_OR_ZV;
6722 break;
6724 if (ITERATOR_AT_END_OF_LINE_P (it))
6726 result = MOVE_NEWLINE_OR_CR;
6727 break;
6730 #endif /* HAVE_WINDOW_SYSTEM */
6731 result = MOVE_LINE_TRUNCATED;
6732 break;
6736 #undef BUFFER_POS_REACHED_P
6738 /* Restore the iterator settings altered at the beginning of this
6739 function. */
6740 it->glyph_row = saved_glyph_row;
6741 return result;
6745 /* Move IT forward until it satisfies one or more of the criteria in
6746 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6748 OP is a bit-mask that specifies where to stop, and in particular,
6749 which of those four position arguments makes a difference. See the
6750 description of enum move_operation_enum.
6752 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6753 screen line, this function will set IT to the next position >
6754 TO_CHARPOS. */
6756 void
6757 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6758 struct it *it;
6759 int to_charpos, to_x, to_y, to_vpos;
6760 int op;
6762 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6763 int line_height;
6764 int reached = 0;
6766 for (;;)
6768 if (op & MOVE_TO_VPOS)
6770 /* If no TO_CHARPOS and no TO_X specified, stop at the
6771 start of the line TO_VPOS. */
6772 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6774 if (it->vpos == to_vpos)
6776 reached = 1;
6777 break;
6779 else
6780 skip = move_it_in_display_line_to (it, -1, -1, 0);
6782 else
6784 /* TO_VPOS >= 0 means stop at TO_X in the line at
6785 TO_VPOS, or at TO_POS, whichever comes first. */
6786 if (it->vpos == to_vpos)
6788 reached = 2;
6789 break;
6792 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6794 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6796 reached = 3;
6797 break;
6799 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6801 /* We have reached TO_X but not in the line we want. */
6802 skip = move_it_in_display_line_to (it, to_charpos,
6803 -1, MOVE_TO_POS);
6804 if (skip == MOVE_POS_MATCH_OR_ZV)
6806 reached = 4;
6807 break;
6812 else if (op & MOVE_TO_Y)
6814 struct it it_backup;
6816 /* TO_Y specified means stop at TO_X in the line containing
6817 TO_Y---or at TO_CHARPOS if this is reached first. The
6818 problem is that we can't really tell whether the line
6819 contains TO_Y before we have completely scanned it, and
6820 this may skip past TO_X. What we do is to first scan to
6821 TO_X.
6823 If TO_X is not specified, use a TO_X of zero. The reason
6824 is to make the outcome of this function more predictable.
6825 If we didn't use TO_X == 0, we would stop at the end of
6826 the line which is probably not what a caller would expect
6827 to happen. */
6828 skip = move_it_in_display_line_to (it, to_charpos,
6829 ((op & MOVE_TO_X)
6830 ? to_x : 0),
6831 (MOVE_TO_X
6832 | (op & MOVE_TO_POS)));
6834 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6835 if (skip == MOVE_POS_MATCH_OR_ZV)
6837 reached = 5;
6838 break;
6841 /* If TO_X was reached, we would like to know whether TO_Y
6842 is in the line. This can only be said if we know the
6843 total line height which requires us to scan the rest of
6844 the line. */
6845 if (skip == MOVE_X_REACHED)
6847 it_backup = *it;
6848 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6849 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6850 op & MOVE_TO_POS);
6851 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6854 /* Now, decide whether TO_Y is in this line. */
6855 line_height = it->max_ascent + it->max_descent;
6856 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6858 if (to_y >= it->current_y
6859 && to_y < it->current_y + line_height)
6861 if (skip == MOVE_X_REACHED)
6862 /* If TO_Y is in this line and TO_X was reached above,
6863 we scanned too far. We have to restore IT's settings
6864 to the ones before skipping. */
6865 *it = it_backup;
6866 reached = 6;
6868 else if (skip == MOVE_X_REACHED)
6870 skip = skip2;
6871 if (skip == MOVE_POS_MATCH_OR_ZV)
6872 reached = 7;
6875 if (reached)
6876 break;
6878 else if (BUFFERP (it->object)
6879 && it->method == GET_FROM_BUFFER
6880 && IT_CHARPOS (*it) >= to_charpos)
6881 skip = MOVE_POS_MATCH_OR_ZV;
6882 else
6883 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6885 switch (skip)
6887 case MOVE_POS_MATCH_OR_ZV:
6888 reached = 8;
6889 goto out;
6891 case MOVE_NEWLINE_OR_CR:
6892 set_iterator_to_next (it, 1);
6893 it->continuation_lines_width = 0;
6894 break;
6896 case MOVE_LINE_TRUNCATED:
6897 it->continuation_lines_width = 0;
6898 reseat_at_next_visible_line_start (it, 0);
6899 if ((op & MOVE_TO_POS) != 0
6900 && IT_CHARPOS (*it) > to_charpos)
6902 reached = 9;
6903 goto out;
6905 break;
6907 case MOVE_LINE_CONTINUED:
6908 /* For continued lines ending in a tab, some of the glyphs
6909 associated with the tab are displayed on the current
6910 line. Since it->current_x does not include these glyphs,
6911 we use it->last_visible_x instead. */
6912 it->continuation_lines_width +=
6913 (it->c == '\t') ? it->last_visible_x : it->current_x;
6914 break;
6916 default:
6917 abort ();
6920 /* Reset/increment for the next run. */
6921 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6922 it->current_x = it->hpos = 0;
6923 it->current_y += it->max_ascent + it->max_descent;
6924 ++it->vpos;
6925 last_height = it->max_ascent + it->max_descent;
6926 last_max_ascent = it->max_ascent;
6927 it->max_ascent = it->max_descent = 0;
6930 out:
6932 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6936 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6938 If DY > 0, move IT backward at least that many pixels. DY = 0
6939 means move IT backward to the preceding line start or BEGV. This
6940 function may move over more than DY pixels if IT->current_y - DY
6941 ends up in the middle of a line; in this case IT->current_y will be
6942 set to the top of the line moved to. */
6944 void
6945 move_it_vertically_backward (it, dy)
6946 struct it *it;
6947 int dy;
6949 int nlines, h;
6950 struct it it2, it3;
6951 int start_pos;
6953 move_further_back:
6954 xassert (dy >= 0);
6956 start_pos = IT_CHARPOS (*it);
6958 /* Estimate how many newlines we must move back. */
6959 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6961 /* Set the iterator's position that many lines back. */
6962 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6963 back_to_previous_visible_line_start (it);
6965 /* Reseat the iterator here. When moving backward, we don't want
6966 reseat to skip forward over invisible text, set up the iterator
6967 to deliver from overlay strings at the new position etc. So,
6968 use reseat_1 here. */
6969 reseat_1 (it, it->current.pos, 1);
6971 /* We are now surely at a line start. */
6972 it->current_x = it->hpos = 0;
6973 it->continuation_lines_width = 0;
6975 /* Move forward and see what y-distance we moved. First move to the
6976 start of the next line so that we get its height. We need this
6977 height to be able to tell whether we reached the specified
6978 y-distance. */
6979 it2 = *it;
6980 it2.max_ascent = it2.max_descent = 0;
6983 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6984 MOVE_TO_POS | MOVE_TO_VPOS);
6986 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6987 xassert (IT_CHARPOS (*it) >= BEGV);
6988 it3 = it2;
6990 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6991 xassert (IT_CHARPOS (*it) >= BEGV);
6992 /* H is the actual vertical distance from the position in *IT
6993 and the starting position. */
6994 h = it2.current_y - it->current_y;
6995 /* NLINES is the distance in number of lines. */
6996 nlines = it2.vpos - it->vpos;
6998 /* Correct IT's y and vpos position
6999 so that they are relative to the starting point. */
7000 it->vpos -= nlines;
7001 it->current_y -= h;
7003 if (dy == 0)
7005 /* DY == 0 means move to the start of the screen line. The
7006 value of nlines is > 0 if continuation lines were involved. */
7007 if (nlines > 0)
7008 move_it_by_lines (it, nlines, 1);
7009 #if 0
7010 /* I think this assert is bogus if buffer contains
7011 invisible text or images. KFS. */
7012 xassert (IT_CHARPOS (*it) <= start_pos);
7013 #endif
7015 else
7017 /* The y-position we try to reach, relative to *IT.
7018 Note that H has been subtracted in front of the if-statement. */
7019 int target_y = it->current_y + h - dy;
7020 int y0 = it3.current_y;
7021 int y1 = line_bottom_y (&it3);
7022 int line_height = y1 - y0;
7024 /* If we did not reach target_y, try to move further backward if
7025 we can. If we moved too far backward, try to move forward. */
7026 if (target_y < it->current_y
7027 /* This is heuristic. In a window that's 3 lines high, with
7028 a line height of 13 pixels each, recentering with point
7029 on the bottom line will try to move -39/2 = 19 pixels
7030 backward. Try to avoid moving into the first line. */
7031 && (it->current_y - target_y
7032 > min (window_box_height (it->w), line_height * 2 / 3))
7033 && IT_CHARPOS (*it) > BEGV)
7035 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7036 target_y - it->current_y));
7037 dy = it->current_y - target_y;
7038 goto move_further_back;
7040 else if (target_y >= it->current_y + line_height
7041 && IT_CHARPOS (*it) < ZV)
7043 /* Should move forward by at least one line, maybe more.
7045 Note: Calling move_it_by_lines can be expensive on
7046 terminal frames, where compute_motion is used (via
7047 vmotion) to do the job, when there are very long lines
7048 and truncate-lines is nil. That's the reason for
7049 treating terminal frames specially here. */
7051 if (!FRAME_WINDOW_P (it->f))
7052 move_it_vertically (it, target_y - (it->current_y + line_height));
7053 else
7057 move_it_by_lines (it, 1, 1);
7059 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7062 #if 0
7063 /* I think this assert is bogus if buffer contains
7064 invisible text or images. KFS. */
7065 xassert (IT_CHARPOS (*it) >= BEGV);
7066 #endif
7072 /* Move IT by a specified amount of pixel lines DY. DY negative means
7073 move backwards. DY = 0 means move to start of screen line. At the
7074 end, IT will be on the start of a screen line. */
7076 void
7077 move_it_vertically (it, dy)
7078 struct it *it;
7079 int dy;
7081 if (dy <= 0)
7082 move_it_vertically_backward (it, -dy);
7083 else
7085 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7086 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7087 MOVE_TO_POS | MOVE_TO_Y);
7088 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7090 /* If buffer ends in ZV without a newline, move to the start of
7091 the line to satisfy the post-condition. */
7092 if (IT_CHARPOS (*it) == ZV
7093 && ZV > BEGV
7094 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7095 move_it_by_lines (it, 0, 0);
7100 /* Move iterator IT past the end of the text line it is in. */
7102 void
7103 move_it_past_eol (it)
7104 struct it *it;
7106 enum move_it_result rc;
7108 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7109 if (rc == MOVE_NEWLINE_OR_CR)
7110 set_iterator_to_next (it, 0);
7114 #if 0 /* Currently not used. */
7116 /* Return non-zero if some text between buffer positions START_CHARPOS
7117 and END_CHARPOS is invisible. IT->window is the window for text
7118 property lookup. */
7120 static int
7121 invisible_text_between_p (it, start_charpos, end_charpos)
7122 struct it *it;
7123 int start_charpos, end_charpos;
7125 Lisp_Object prop, limit;
7126 int invisible_found_p;
7128 xassert (it != NULL && start_charpos <= end_charpos);
7130 /* Is text at START invisible? */
7131 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7132 it->window);
7133 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7134 invisible_found_p = 1;
7135 else
7137 limit = Fnext_single_char_property_change (make_number (start_charpos),
7138 Qinvisible, Qnil,
7139 make_number (end_charpos));
7140 invisible_found_p = XFASTINT (limit) < end_charpos;
7143 return invisible_found_p;
7146 #endif /* 0 */
7149 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7150 negative means move up. DVPOS == 0 means move to the start of the
7151 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7152 NEED_Y_P is zero, IT->current_y will be left unchanged.
7154 Further optimization ideas: If we would know that IT->f doesn't use
7155 a face with proportional font, we could be faster for
7156 truncate-lines nil. */
7158 void
7159 move_it_by_lines (it, dvpos, need_y_p)
7160 struct it *it;
7161 int dvpos, need_y_p;
7163 struct position pos;
7165 /* The commented-out optimization uses vmotion on terminals. This
7166 gives bad results, because elements like it->what, on which
7167 callers such as pos_visible_p rely, aren't updated. */
7168 /* if (!FRAME_WINDOW_P (it->f))
7170 struct text_pos textpos;
7172 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7173 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7174 reseat (it, textpos, 1);
7175 it->vpos += pos.vpos;
7176 it->current_y += pos.vpos;
7178 else */
7180 if (dvpos == 0)
7182 /* DVPOS == 0 means move to the start of the screen line. */
7183 move_it_vertically_backward (it, 0);
7184 xassert (it->current_x == 0 && it->hpos == 0);
7185 /* Let next call to line_bottom_y calculate real line height */
7186 last_height = 0;
7188 else if (dvpos > 0)
7190 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7191 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7192 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7194 else
7196 struct it it2;
7197 int start_charpos, i;
7199 /* Start at the beginning of the screen line containing IT's
7200 position. This may actually move vertically backwards,
7201 in case of overlays, so adjust dvpos accordingly. */
7202 dvpos += it->vpos;
7203 move_it_vertically_backward (it, 0);
7204 dvpos -= it->vpos;
7206 /* Go back -DVPOS visible lines and reseat the iterator there. */
7207 start_charpos = IT_CHARPOS (*it);
7208 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7209 back_to_previous_visible_line_start (it);
7210 reseat (it, it->current.pos, 1);
7212 /* Move further back if we end up in a string or an image. */
7213 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7215 /* First try to move to start of display line. */
7216 dvpos += it->vpos;
7217 move_it_vertically_backward (it, 0);
7218 dvpos -= it->vpos;
7219 if (IT_POS_VALID_AFTER_MOVE_P (it))
7220 break;
7221 /* If start of line is still in string or image,
7222 move further back. */
7223 back_to_previous_visible_line_start (it);
7224 reseat (it, it->current.pos, 1);
7225 dvpos--;
7228 it->current_x = it->hpos = 0;
7230 /* Above call may have moved too far if continuation lines
7231 are involved. Scan forward and see if it did. */
7232 it2 = *it;
7233 it2.vpos = it2.current_y = 0;
7234 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7235 it->vpos -= it2.vpos;
7236 it->current_y -= it2.current_y;
7237 it->current_x = it->hpos = 0;
7239 /* If we moved too far back, move IT some lines forward. */
7240 if (it2.vpos > -dvpos)
7242 int delta = it2.vpos + dvpos;
7243 it2 = *it;
7244 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7245 /* Move back again if we got too far ahead. */
7246 if (IT_CHARPOS (*it) >= start_charpos)
7247 *it = it2;
7252 /* Return 1 if IT points into the middle of a display vector. */
7255 in_display_vector_p (it)
7256 struct it *it;
7258 return (it->method == GET_FROM_DISPLAY_VECTOR
7259 && it->current.dpvec_index > 0
7260 && it->dpvec + it->current.dpvec_index != it->dpend);
7264 /***********************************************************************
7265 Messages
7266 ***********************************************************************/
7269 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7270 to *Messages*. */
7272 void
7273 add_to_log (format, arg1, arg2)
7274 char *format;
7275 Lisp_Object arg1, arg2;
7277 Lisp_Object args[3];
7278 Lisp_Object msg, fmt;
7279 char *buffer;
7280 int len;
7281 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7282 USE_SAFE_ALLOCA;
7284 /* Do nothing if called asynchronously. Inserting text into
7285 a buffer may call after-change-functions and alike and
7286 that would means running Lisp asynchronously. */
7287 if (handling_signal)
7288 return;
7290 fmt = msg = Qnil;
7291 GCPRO4 (fmt, msg, arg1, arg2);
7293 args[0] = fmt = build_string (format);
7294 args[1] = arg1;
7295 args[2] = arg2;
7296 msg = Fformat (3, args);
7298 len = SBYTES (msg) + 1;
7299 SAFE_ALLOCA (buffer, char *, len);
7300 bcopy (SDATA (msg), buffer, len);
7302 message_dolog (buffer, len - 1, 1, 0);
7303 SAFE_FREE ();
7305 UNGCPRO;
7309 /* Output a newline in the *Messages* buffer if "needs" one. */
7311 void
7312 message_log_maybe_newline ()
7314 if (message_log_need_newline)
7315 message_dolog ("", 0, 1, 0);
7319 /* Add a string M of length NBYTES to the message log, optionally
7320 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7321 nonzero, means interpret the contents of M as multibyte. This
7322 function calls low-level routines in order to bypass text property
7323 hooks, etc. which might not be safe to run.
7325 This may GC (insert may run before/after change hooks),
7326 so the buffer M must NOT point to a Lisp string. */
7328 void
7329 message_dolog (m, nbytes, nlflag, multibyte)
7330 const char *m;
7331 int nbytes, nlflag, multibyte;
7333 if (!NILP (Vmemory_full))
7334 return;
7336 if (!NILP (Vmessage_log_max))
7338 struct buffer *oldbuf;
7339 Lisp_Object oldpoint, oldbegv, oldzv;
7340 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7341 int point_at_end = 0;
7342 int zv_at_end = 0;
7343 Lisp_Object old_deactivate_mark, tem;
7344 struct gcpro gcpro1;
7346 old_deactivate_mark = Vdeactivate_mark;
7347 oldbuf = current_buffer;
7348 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7349 current_buffer->undo_list = Qt;
7351 oldpoint = message_dolog_marker1;
7352 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7353 oldbegv = message_dolog_marker2;
7354 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7355 oldzv = message_dolog_marker3;
7356 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7357 GCPRO1 (old_deactivate_mark);
7359 if (PT == Z)
7360 point_at_end = 1;
7361 if (ZV == Z)
7362 zv_at_end = 1;
7364 BEGV = BEG;
7365 BEGV_BYTE = BEG_BYTE;
7366 ZV = Z;
7367 ZV_BYTE = Z_BYTE;
7368 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7370 /* Insert the string--maybe converting multibyte to single byte
7371 or vice versa, so that all the text fits the buffer. */
7372 if (multibyte
7373 && NILP (current_buffer->enable_multibyte_characters))
7375 int i, c, char_bytes;
7376 unsigned char work[1];
7378 /* Convert a multibyte string to single-byte
7379 for the *Message* buffer. */
7380 for (i = 0; i < nbytes; i += char_bytes)
7382 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7383 work[0] = (SINGLE_BYTE_CHAR_P (c)
7385 : multibyte_char_to_unibyte (c, Qnil));
7386 insert_1_both (work, 1, 1, 1, 0, 0);
7389 else if (! multibyte
7390 && ! NILP (current_buffer->enable_multibyte_characters))
7392 int i, c, char_bytes;
7393 unsigned char *msg = (unsigned char *) m;
7394 unsigned char str[MAX_MULTIBYTE_LENGTH];
7395 /* Convert a single-byte string to multibyte
7396 for the *Message* buffer. */
7397 for (i = 0; i < nbytes; i++)
7399 c = unibyte_char_to_multibyte (msg[i]);
7400 char_bytes = CHAR_STRING (c, str);
7401 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7404 else if (nbytes)
7405 insert_1 (m, nbytes, 1, 0, 0);
7407 if (nlflag)
7409 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7410 insert_1 ("\n", 1, 1, 0, 0);
7412 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7413 this_bol = PT;
7414 this_bol_byte = PT_BYTE;
7416 /* See if this line duplicates the previous one.
7417 If so, combine duplicates. */
7418 if (this_bol > BEG)
7420 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7421 prev_bol = PT;
7422 prev_bol_byte = PT_BYTE;
7424 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7425 this_bol, this_bol_byte);
7426 if (dup)
7428 del_range_both (prev_bol, prev_bol_byte,
7429 this_bol, this_bol_byte, 0);
7430 if (dup > 1)
7432 char dupstr[40];
7433 int duplen;
7435 /* If you change this format, don't forget to also
7436 change message_log_check_duplicate. */
7437 sprintf (dupstr, " [%d times]", dup);
7438 duplen = strlen (dupstr);
7439 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7440 insert_1 (dupstr, duplen, 1, 0, 1);
7445 /* If we have more than the desired maximum number of lines
7446 in the *Messages* buffer now, delete the oldest ones.
7447 This is safe because we don't have undo in this buffer. */
7449 if (NATNUMP (Vmessage_log_max))
7451 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7452 -XFASTINT (Vmessage_log_max) - 1, 0);
7453 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7456 BEGV = XMARKER (oldbegv)->charpos;
7457 BEGV_BYTE = marker_byte_position (oldbegv);
7459 if (zv_at_end)
7461 ZV = Z;
7462 ZV_BYTE = Z_BYTE;
7464 else
7466 ZV = XMARKER (oldzv)->charpos;
7467 ZV_BYTE = marker_byte_position (oldzv);
7470 if (point_at_end)
7471 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7472 else
7473 /* We can't do Fgoto_char (oldpoint) because it will run some
7474 Lisp code. */
7475 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7476 XMARKER (oldpoint)->bytepos);
7478 UNGCPRO;
7479 unchain_marker (XMARKER (oldpoint));
7480 unchain_marker (XMARKER (oldbegv));
7481 unchain_marker (XMARKER (oldzv));
7483 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7484 set_buffer_internal (oldbuf);
7485 if (NILP (tem))
7486 windows_or_buffers_changed = old_windows_or_buffers_changed;
7487 message_log_need_newline = !nlflag;
7488 Vdeactivate_mark = old_deactivate_mark;
7493 /* We are at the end of the buffer after just having inserted a newline.
7494 (Note: We depend on the fact we won't be crossing the gap.)
7495 Check to see if the most recent message looks a lot like the previous one.
7496 Return 0 if different, 1 if the new one should just replace it, or a
7497 value N > 1 if we should also append " [N times]". */
7499 static int
7500 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7501 int prev_bol, this_bol;
7502 int prev_bol_byte, this_bol_byte;
7504 int i;
7505 int len = Z_BYTE - 1 - this_bol_byte;
7506 int seen_dots = 0;
7507 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7508 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7510 for (i = 0; i < len; i++)
7512 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7513 seen_dots = 1;
7514 if (p1[i] != p2[i])
7515 return seen_dots;
7517 p1 += len;
7518 if (*p1 == '\n')
7519 return 2;
7520 if (*p1++ == ' ' && *p1++ == '[')
7522 int n = 0;
7523 while (*p1 >= '0' && *p1 <= '9')
7524 n = n * 10 + *p1++ - '0';
7525 if (strncmp (p1, " times]\n", 8) == 0)
7526 return n+1;
7528 return 0;
7532 /* Display an echo area message M with a specified length of NBYTES
7533 bytes. The string may include null characters. If M is 0, clear
7534 out any existing message, and let the mini-buffer text show
7535 through.
7537 This may GC, so the buffer M must NOT point to a Lisp string. */
7539 void
7540 message2 (m, nbytes, multibyte)
7541 const char *m;
7542 int nbytes;
7543 int multibyte;
7545 /* First flush out any partial line written with print. */
7546 message_log_maybe_newline ();
7547 if (m)
7548 message_dolog (m, nbytes, 1, multibyte);
7549 message2_nolog (m, nbytes, multibyte);
7553 /* The non-logging counterpart of message2. */
7555 void
7556 message2_nolog (m, nbytes, multibyte)
7557 const char *m;
7558 int nbytes, multibyte;
7560 struct frame *sf = SELECTED_FRAME ();
7561 message_enable_multibyte = multibyte;
7563 if (noninteractive)
7565 if (noninteractive_need_newline)
7566 putc ('\n', stderr);
7567 noninteractive_need_newline = 0;
7568 if (m)
7569 fwrite (m, nbytes, 1, stderr);
7570 if (cursor_in_echo_area == 0)
7571 fprintf (stderr, "\n");
7572 fflush (stderr);
7574 /* A null message buffer means that the frame hasn't really been
7575 initialized yet. Error messages get reported properly by
7576 cmd_error, so this must be just an informative message; toss it. */
7577 else if (INTERACTIVE
7578 && sf->glyphs_initialized_p
7579 && FRAME_MESSAGE_BUF (sf))
7581 Lisp_Object mini_window;
7582 struct frame *f;
7584 /* Get the frame containing the mini-buffer
7585 that the selected frame is using. */
7586 mini_window = FRAME_MINIBUF_WINDOW (sf);
7587 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7589 FRAME_SAMPLE_VISIBILITY (f);
7590 if (FRAME_VISIBLE_P (sf)
7591 && ! FRAME_VISIBLE_P (f))
7592 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7594 if (m)
7596 set_message (m, Qnil, nbytes, multibyte);
7597 if (minibuffer_auto_raise)
7598 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7600 else
7601 clear_message (1, 1);
7603 do_pending_window_change (0);
7604 echo_area_display (1);
7605 do_pending_window_change (0);
7606 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7607 (*frame_up_to_date_hook) (f);
7612 /* Display an echo area message M with a specified length of NBYTES
7613 bytes. The string may include null characters. If M is not a
7614 string, clear out any existing message, and let the mini-buffer
7615 text show through.
7617 This function cancels echoing. */
7619 void
7620 message3 (m, nbytes, multibyte)
7621 Lisp_Object m;
7622 int nbytes;
7623 int multibyte;
7625 struct gcpro gcpro1;
7627 GCPRO1 (m);
7628 clear_message (1,1);
7629 cancel_echoing ();
7631 /* First flush out any partial line written with print. */
7632 message_log_maybe_newline ();
7633 if (STRINGP (m))
7635 char *buffer;
7636 USE_SAFE_ALLOCA;
7638 SAFE_ALLOCA (buffer, char *, nbytes);
7639 bcopy (SDATA (m), buffer, nbytes);
7640 message_dolog (buffer, nbytes, 1, multibyte);
7641 SAFE_FREE ();
7643 message3_nolog (m, nbytes, multibyte);
7645 UNGCPRO;
7649 /* The non-logging version of message3.
7650 This does not cancel echoing, because it is used for echoing.
7651 Perhaps we need to make a separate function for echoing
7652 and make this cancel echoing. */
7654 void
7655 message3_nolog (m, nbytes, multibyte)
7656 Lisp_Object m;
7657 int nbytes, multibyte;
7659 struct frame *sf = SELECTED_FRAME ();
7660 message_enable_multibyte = multibyte;
7662 if (noninteractive)
7664 if (noninteractive_need_newline)
7665 putc ('\n', stderr);
7666 noninteractive_need_newline = 0;
7667 if (STRINGP (m))
7668 fwrite (SDATA (m), nbytes, 1, stderr);
7669 if (cursor_in_echo_area == 0)
7670 fprintf (stderr, "\n");
7671 fflush (stderr);
7673 /* A null message buffer means that the frame hasn't really been
7674 initialized yet. Error messages get reported properly by
7675 cmd_error, so this must be just an informative message; toss it. */
7676 else if (INTERACTIVE
7677 && sf->glyphs_initialized_p
7678 && FRAME_MESSAGE_BUF (sf))
7680 Lisp_Object mini_window;
7681 Lisp_Object frame;
7682 struct frame *f;
7684 /* Get the frame containing the mini-buffer
7685 that the selected frame is using. */
7686 mini_window = FRAME_MINIBUF_WINDOW (sf);
7687 frame = XWINDOW (mini_window)->frame;
7688 f = XFRAME (frame);
7690 FRAME_SAMPLE_VISIBILITY (f);
7691 if (FRAME_VISIBLE_P (sf)
7692 && !FRAME_VISIBLE_P (f))
7693 Fmake_frame_visible (frame);
7695 if (STRINGP (m) && SCHARS (m) > 0)
7697 set_message (NULL, m, nbytes, multibyte);
7698 if (minibuffer_auto_raise)
7699 Fraise_frame (frame);
7700 /* Assume we are not echoing.
7701 (If we are, echo_now will override this.) */
7702 echo_message_buffer = Qnil;
7704 else
7705 clear_message (1, 1);
7707 do_pending_window_change (0);
7708 echo_area_display (1);
7709 do_pending_window_change (0);
7710 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7711 (*frame_up_to_date_hook) (f);
7716 /* Display a null-terminated echo area message M. If M is 0, clear
7717 out any existing message, and let the mini-buffer text show through.
7719 The buffer M must continue to exist until after the echo area gets
7720 cleared or some other message gets displayed there. Do not pass
7721 text that is stored in a Lisp string. Do not pass text in a buffer
7722 that was alloca'd. */
7724 void
7725 message1 (m)
7726 char *m;
7728 message2 (m, (m ? strlen (m) : 0), 0);
7732 /* The non-logging counterpart of message1. */
7734 void
7735 message1_nolog (m)
7736 char *m;
7738 message2_nolog (m, (m ? strlen (m) : 0), 0);
7741 /* Display a message M which contains a single %s
7742 which gets replaced with STRING. */
7744 void
7745 message_with_string (m, string, log)
7746 char *m;
7747 Lisp_Object string;
7748 int log;
7750 CHECK_STRING (string);
7752 if (noninteractive)
7754 if (m)
7756 if (noninteractive_need_newline)
7757 putc ('\n', stderr);
7758 noninteractive_need_newline = 0;
7759 fprintf (stderr, m, SDATA (string));
7760 if (cursor_in_echo_area == 0)
7761 fprintf (stderr, "\n");
7762 fflush (stderr);
7765 else if (INTERACTIVE)
7767 /* The frame whose minibuffer we're going to display the message on.
7768 It may be larger than the selected frame, so we need
7769 to use its buffer, not the selected frame's buffer. */
7770 Lisp_Object mini_window;
7771 struct frame *f, *sf = SELECTED_FRAME ();
7773 /* Get the frame containing the minibuffer
7774 that the selected frame is using. */
7775 mini_window = FRAME_MINIBUF_WINDOW (sf);
7776 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7778 /* A null message buffer means that the frame hasn't really been
7779 initialized yet. Error messages get reported properly by
7780 cmd_error, so this must be just an informative message; toss it. */
7781 if (FRAME_MESSAGE_BUF (f))
7783 Lisp_Object args[2], message;
7784 struct gcpro gcpro1, gcpro2;
7786 args[0] = build_string (m);
7787 args[1] = message = string;
7788 GCPRO2 (args[0], message);
7789 gcpro1.nvars = 2;
7791 message = Fformat (2, args);
7793 if (log)
7794 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7795 else
7796 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7798 UNGCPRO;
7800 /* Print should start at the beginning of the message
7801 buffer next time. */
7802 message_buf_print = 0;
7808 /* Dump an informative message to the minibuf. If M is 0, clear out
7809 any existing message, and let the mini-buffer text show through. */
7811 /* VARARGS 1 */
7812 void
7813 message (m, a1, a2, a3)
7814 char *m;
7815 EMACS_INT a1, a2, a3;
7817 if (noninteractive)
7819 if (m)
7821 if (noninteractive_need_newline)
7822 putc ('\n', stderr);
7823 noninteractive_need_newline = 0;
7824 fprintf (stderr, m, a1, a2, a3);
7825 if (cursor_in_echo_area == 0)
7826 fprintf (stderr, "\n");
7827 fflush (stderr);
7830 else if (INTERACTIVE)
7832 /* The frame whose mini-buffer we're going to display the message
7833 on. It may be larger than the selected frame, so we need to
7834 use its buffer, not the selected frame's buffer. */
7835 Lisp_Object mini_window;
7836 struct frame *f, *sf = SELECTED_FRAME ();
7838 /* Get the frame containing the mini-buffer
7839 that the selected frame is using. */
7840 mini_window = FRAME_MINIBUF_WINDOW (sf);
7841 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7843 /* A null message buffer means that the frame hasn't really been
7844 initialized yet. Error messages get reported properly by
7845 cmd_error, so this must be just an informative message; toss
7846 it. */
7847 if (FRAME_MESSAGE_BUF (f))
7849 if (m)
7851 int len;
7852 #ifdef NO_ARG_ARRAY
7853 char *a[3];
7854 a[0] = (char *) a1;
7855 a[1] = (char *) a2;
7856 a[2] = (char *) a3;
7858 len = doprnt (FRAME_MESSAGE_BUF (f),
7859 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7860 #else
7861 len = doprnt (FRAME_MESSAGE_BUF (f),
7862 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7863 (char **) &a1);
7864 #endif /* NO_ARG_ARRAY */
7866 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7868 else
7869 message1 (0);
7871 /* Print should start at the beginning of the message
7872 buffer next time. */
7873 message_buf_print = 0;
7879 /* The non-logging version of message. */
7881 void
7882 message_nolog (m, a1, a2, a3)
7883 char *m;
7884 EMACS_INT a1, a2, a3;
7886 Lisp_Object old_log_max;
7887 old_log_max = Vmessage_log_max;
7888 Vmessage_log_max = Qnil;
7889 message (m, a1, a2, a3);
7890 Vmessage_log_max = old_log_max;
7894 /* Display the current message in the current mini-buffer. This is
7895 only called from error handlers in process.c, and is not time
7896 critical. */
7898 void
7899 update_echo_area ()
7901 if (!NILP (echo_area_buffer[0]))
7903 Lisp_Object string;
7904 string = Fcurrent_message ();
7905 message3 (string, SBYTES (string),
7906 !NILP (current_buffer->enable_multibyte_characters));
7911 /* Make sure echo area buffers in `echo_buffers' are live.
7912 If they aren't, make new ones. */
7914 static void
7915 ensure_echo_area_buffers ()
7917 int i;
7919 for (i = 0; i < 2; ++i)
7920 if (!BUFFERP (echo_buffer[i])
7921 || NILP (XBUFFER (echo_buffer[i])->name))
7923 char name[30];
7924 Lisp_Object old_buffer;
7925 int j;
7927 old_buffer = echo_buffer[i];
7928 sprintf (name, " *Echo Area %d*", i);
7929 echo_buffer[i] = Fget_buffer_create (build_string (name));
7930 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7932 for (j = 0; j < 2; ++j)
7933 if (EQ (old_buffer, echo_area_buffer[j]))
7934 echo_area_buffer[j] = echo_buffer[i];
7939 /* Call FN with args A1..A4 with either the current or last displayed
7940 echo_area_buffer as current buffer.
7942 WHICH zero means use the current message buffer
7943 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7944 from echo_buffer[] and clear it.
7946 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7947 suitable buffer from echo_buffer[] and clear it.
7949 Value is what FN returns. */
7951 static int
7952 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7953 struct window *w;
7954 int which;
7955 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7956 EMACS_INT a1;
7957 Lisp_Object a2;
7958 EMACS_INT a3, a4;
7960 Lisp_Object buffer;
7961 int this_one, the_other, clear_buffer_p, rc;
7962 int count = SPECPDL_INDEX ();
7964 /* If buffers aren't live, make new ones. */
7965 ensure_echo_area_buffers ();
7967 clear_buffer_p = 0;
7969 if (which == 0)
7970 this_one = 0, the_other = 1;
7971 else if (which > 0)
7972 this_one = 1, the_other = 0;
7974 /* Choose a suitable buffer from echo_buffer[] is we don't
7975 have one. */
7976 if (NILP (echo_area_buffer[this_one]))
7978 echo_area_buffer[this_one]
7979 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7980 ? echo_buffer[the_other]
7981 : echo_buffer[this_one]);
7982 clear_buffer_p = 1;
7985 buffer = echo_area_buffer[this_one];
7987 /* Don't get confused by reusing the buffer used for echoing
7988 for a different purpose. */
7989 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7990 cancel_echoing ();
7992 record_unwind_protect (unwind_with_echo_area_buffer,
7993 with_echo_area_buffer_unwind_data (w));
7995 /* Make the echo area buffer current. Note that for display
7996 purposes, it is not necessary that the displayed window's buffer
7997 == current_buffer, except for text property lookup. So, let's
7998 only set that buffer temporarily here without doing a full
7999 Fset_window_buffer. We must also change w->pointm, though,
8000 because otherwise an assertions in unshow_buffer fails, and Emacs
8001 aborts. */
8002 set_buffer_internal_1 (XBUFFER (buffer));
8003 if (w)
8005 w->buffer = buffer;
8006 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8009 current_buffer->undo_list = Qt;
8010 current_buffer->read_only = Qnil;
8011 specbind (Qinhibit_read_only, Qt);
8012 specbind (Qinhibit_modification_hooks, Qt);
8014 if (clear_buffer_p && Z > BEG)
8015 del_range (BEG, Z);
8017 xassert (BEGV >= BEG);
8018 xassert (ZV <= Z && ZV >= BEGV);
8020 rc = fn (a1, a2, a3, a4);
8022 xassert (BEGV >= BEG);
8023 xassert (ZV <= Z && ZV >= BEGV);
8025 unbind_to (count, Qnil);
8026 return rc;
8030 /* Save state that should be preserved around the call to the function
8031 FN called in with_echo_area_buffer. */
8033 static Lisp_Object
8034 with_echo_area_buffer_unwind_data (w)
8035 struct window *w;
8037 int i = 0;
8038 Lisp_Object vector;
8040 /* Reduce consing by keeping one vector in
8041 Vwith_echo_area_save_vector. */
8042 vector = Vwith_echo_area_save_vector;
8043 Vwith_echo_area_save_vector = Qnil;
8045 if (NILP (vector))
8046 vector = Fmake_vector (make_number (7), Qnil);
8048 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8049 AREF (vector, i) = Vdeactivate_mark, ++i;
8050 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8052 if (w)
8054 XSETWINDOW (AREF (vector, i), w); ++i;
8055 AREF (vector, i) = w->buffer; ++i;
8056 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8057 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8059 else
8061 int end = i + 4;
8062 for (; i < end; ++i)
8063 AREF (vector, i) = Qnil;
8066 xassert (i == ASIZE (vector));
8067 return vector;
8071 /* Restore global state from VECTOR which was created by
8072 with_echo_area_buffer_unwind_data. */
8074 static Lisp_Object
8075 unwind_with_echo_area_buffer (vector)
8076 Lisp_Object vector;
8078 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8079 Vdeactivate_mark = AREF (vector, 1);
8080 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8082 if (WINDOWP (AREF (vector, 3)))
8084 struct window *w;
8085 Lisp_Object buffer, charpos, bytepos;
8087 w = XWINDOW (AREF (vector, 3));
8088 buffer = AREF (vector, 4);
8089 charpos = AREF (vector, 5);
8090 bytepos = AREF (vector, 6);
8092 w->buffer = buffer;
8093 set_marker_both (w->pointm, buffer,
8094 XFASTINT (charpos), XFASTINT (bytepos));
8097 Vwith_echo_area_save_vector = vector;
8098 return Qnil;
8102 /* Set up the echo area for use by print functions. MULTIBYTE_P
8103 non-zero means we will print multibyte. */
8105 void
8106 setup_echo_area_for_printing (multibyte_p)
8107 int multibyte_p;
8109 /* If we can't find an echo area any more, exit. */
8110 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8111 Fkill_emacs (Qnil);
8113 ensure_echo_area_buffers ();
8115 if (!message_buf_print)
8117 /* A message has been output since the last time we printed.
8118 Choose a fresh echo area buffer. */
8119 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8120 echo_area_buffer[0] = echo_buffer[1];
8121 else
8122 echo_area_buffer[0] = echo_buffer[0];
8124 /* Switch to that buffer and clear it. */
8125 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8126 current_buffer->truncate_lines = Qnil;
8128 if (Z > BEG)
8130 int count = SPECPDL_INDEX ();
8131 specbind (Qinhibit_read_only, Qt);
8132 /* Note that undo recording is always disabled. */
8133 del_range (BEG, Z);
8134 unbind_to (count, Qnil);
8136 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8138 /* Set up the buffer for the multibyteness we need. */
8139 if (multibyte_p
8140 != !NILP (current_buffer->enable_multibyte_characters))
8141 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8143 /* Raise the frame containing the echo area. */
8144 if (minibuffer_auto_raise)
8146 struct frame *sf = SELECTED_FRAME ();
8147 Lisp_Object mini_window;
8148 mini_window = FRAME_MINIBUF_WINDOW (sf);
8149 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8152 message_log_maybe_newline ();
8153 message_buf_print = 1;
8155 else
8157 if (NILP (echo_area_buffer[0]))
8159 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8160 echo_area_buffer[0] = echo_buffer[1];
8161 else
8162 echo_area_buffer[0] = echo_buffer[0];
8165 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8167 /* Someone switched buffers between print requests. */
8168 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8169 current_buffer->truncate_lines = Qnil;
8175 /* Display an echo area message in window W. Value is non-zero if W's
8176 height is changed. If display_last_displayed_message_p is
8177 non-zero, display the message that was last displayed, otherwise
8178 display the current message. */
8180 static int
8181 display_echo_area (w)
8182 struct window *w;
8184 int i, no_message_p, window_height_changed_p, count;
8186 /* Temporarily disable garbage collections while displaying the echo
8187 area. This is done because a GC can print a message itself.
8188 That message would modify the echo area buffer's contents while a
8189 redisplay of the buffer is going on, and seriously confuse
8190 redisplay. */
8191 count = inhibit_garbage_collection ();
8193 /* If there is no message, we must call display_echo_area_1
8194 nevertheless because it resizes the window. But we will have to
8195 reset the echo_area_buffer in question to nil at the end because
8196 with_echo_area_buffer will sets it to an empty buffer. */
8197 i = display_last_displayed_message_p ? 1 : 0;
8198 no_message_p = NILP (echo_area_buffer[i]);
8200 window_height_changed_p
8201 = with_echo_area_buffer (w, display_last_displayed_message_p,
8202 display_echo_area_1,
8203 (EMACS_INT) w, Qnil, 0, 0);
8205 if (no_message_p)
8206 echo_area_buffer[i] = Qnil;
8208 unbind_to (count, Qnil);
8209 return window_height_changed_p;
8213 /* Helper for display_echo_area. Display the current buffer which
8214 contains the current echo area message in window W, a mini-window,
8215 a pointer to which is passed in A1. A2..A4 are currently not used.
8216 Change the height of W so that all of the message is displayed.
8217 Value is non-zero if height of W was changed. */
8219 static int
8220 display_echo_area_1 (a1, a2, a3, a4)
8221 EMACS_INT a1;
8222 Lisp_Object a2;
8223 EMACS_INT a3, a4;
8225 struct window *w = (struct window *) a1;
8226 Lisp_Object window;
8227 struct text_pos start;
8228 int window_height_changed_p = 0;
8230 /* Do this before displaying, so that we have a large enough glyph
8231 matrix for the display. If we can't get enough space for the
8232 whole text, display the last N lines. That works by setting w->start. */
8233 window_height_changed_p = resize_mini_window (w, 0);
8235 /* Use the starting position chosen by resize_mini_window. */
8236 SET_TEXT_POS_FROM_MARKER (start, w->start);
8238 /* Display. */
8239 clear_glyph_matrix (w->desired_matrix);
8240 XSETWINDOW (window, w);
8241 try_window (window, start, 0);
8243 return window_height_changed_p;
8247 /* Resize the echo area window to exactly the size needed for the
8248 currently displayed message, if there is one. If a mini-buffer
8249 is active, don't shrink it. */
8251 void
8252 resize_echo_area_exactly ()
8254 if (BUFFERP (echo_area_buffer[0])
8255 && WINDOWP (echo_area_window))
8257 struct window *w = XWINDOW (echo_area_window);
8258 int resized_p;
8259 Lisp_Object resize_exactly;
8261 if (minibuf_level == 0)
8262 resize_exactly = Qt;
8263 else
8264 resize_exactly = Qnil;
8266 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8267 (EMACS_INT) w, resize_exactly, 0, 0);
8268 if (resized_p)
8270 ++windows_or_buffers_changed;
8271 ++update_mode_lines;
8272 redisplay_internal (0);
8278 /* Callback function for with_echo_area_buffer, when used from
8279 resize_echo_area_exactly. A1 contains a pointer to the window to
8280 resize, EXACTLY non-nil means resize the mini-window exactly to the
8281 size of the text displayed. A3 and A4 are not used. Value is what
8282 resize_mini_window returns. */
8284 static int
8285 resize_mini_window_1 (a1, exactly, a3, a4)
8286 EMACS_INT a1;
8287 Lisp_Object exactly;
8288 EMACS_INT a3, a4;
8290 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8294 /* Resize mini-window W to fit the size of its contents. EXACT_P
8295 means size the window exactly to the size needed. Otherwise, it's
8296 only enlarged until W's buffer is empty.
8298 Set W->start to the right place to begin display. If the whole
8299 contents fit, start at the beginning. Otherwise, start so as
8300 to make the end of the contents appear. This is particularly
8301 important for y-or-n-p, but seems desirable generally.
8303 Value is non-zero if the window height has been changed. */
8306 resize_mini_window (w, exact_p)
8307 struct window *w;
8308 int exact_p;
8310 struct frame *f = XFRAME (w->frame);
8311 int window_height_changed_p = 0;
8313 xassert (MINI_WINDOW_P (w));
8315 /* By default, start display at the beginning. */
8316 set_marker_both (w->start, w->buffer,
8317 BUF_BEGV (XBUFFER (w->buffer)),
8318 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8320 /* Don't resize windows while redisplaying a window; it would
8321 confuse redisplay functions when the size of the window they are
8322 displaying changes from under them. Such a resizing can happen,
8323 for instance, when which-func prints a long message while
8324 we are running fontification-functions. We're running these
8325 functions with safe_call which binds inhibit-redisplay to t. */
8326 if (!NILP (Vinhibit_redisplay))
8327 return 0;
8329 /* Nil means don't try to resize. */
8330 if (NILP (Vresize_mini_windows)
8331 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8332 return 0;
8334 if (!FRAME_MINIBUF_ONLY_P (f))
8336 struct it it;
8337 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8338 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8339 int height, max_height;
8340 int unit = FRAME_LINE_HEIGHT (f);
8341 struct text_pos start;
8342 struct buffer *old_current_buffer = NULL;
8344 if (current_buffer != XBUFFER (w->buffer))
8346 old_current_buffer = current_buffer;
8347 set_buffer_internal (XBUFFER (w->buffer));
8350 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8352 /* Compute the max. number of lines specified by the user. */
8353 if (FLOATP (Vmax_mini_window_height))
8354 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8355 else if (INTEGERP (Vmax_mini_window_height))
8356 max_height = XINT (Vmax_mini_window_height);
8357 else
8358 max_height = total_height / 4;
8360 /* Correct that max. height if it's bogus. */
8361 max_height = max (1, max_height);
8362 max_height = min (total_height, max_height);
8364 /* Find out the height of the text in the window. */
8365 if (it.truncate_lines_p)
8366 height = 1;
8367 else
8369 last_height = 0;
8370 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8371 if (it.max_ascent == 0 && it.max_descent == 0)
8372 height = it.current_y + last_height;
8373 else
8374 height = it.current_y + it.max_ascent + it.max_descent;
8375 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8376 height = (height + unit - 1) / unit;
8379 /* Compute a suitable window start. */
8380 if (height > max_height)
8382 height = max_height;
8383 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8384 move_it_vertically_backward (&it, (height - 1) * unit);
8385 start = it.current.pos;
8387 else
8388 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8389 SET_MARKER_FROM_TEXT_POS (w->start, start);
8391 if (EQ (Vresize_mini_windows, Qgrow_only))
8393 /* Let it grow only, until we display an empty message, in which
8394 case the window shrinks again. */
8395 if (height > WINDOW_TOTAL_LINES (w))
8397 int old_height = WINDOW_TOTAL_LINES (w);
8398 freeze_window_starts (f, 1);
8399 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8400 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8402 else if (height < WINDOW_TOTAL_LINES (w)
8403 && (exact_p || BEGV == ZV))
8405 int old_height = WINDOW_TOTAL_LINES (w);
8406 freeze_window_starts (f, 0);
8407 shrink_mini_window (w);
8408 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8411 else
8413 /* Always resize to exact size needed. */
8414 if (height > WINDOW_TOTAL_LINES (w))
8416 int old_height = WINDOW_TOTAL_LINES (w);
8417 freeze_window_starts (f, 1);
8418 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8419 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8421 else if (height < WINDOW_TOTAL_LINES (w))
8423 int old_height = WINDOW_TOTAL_LINES (w);
8424 freeze_window_starts (f, 0);
8425 shrink_mini_window (w);
8427 if (height)
8429 freeze_window_starts (f, 1);
8430 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8433 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8437 if (old_current_buffer)
8438 set_buffer_internal (old_current_buffer);
8441 return window_height_changed_p;
8445 /* Value is the current message, a string, or nil if there is no
8446 current message. */
8448 Lisp_Object
8449 current_message ()
8451 Lisp_Object msg;
8453 if (NILP (echo_area_buffer[0]))
8454 msg = Qnil;
8455 else
8457 with_echo_area_buffer (0, 0, current_message_1,
8458 (EMACS_INT) &msg, Qnil, 0, 0);
8459 if (NILP (msg))
8460 echo_area_buffer[0] = Qnil;
8463 return msg;
8467 static int
8468 current_message_1 (a1, a2, a3, a4)
8469 EMACS_INT a1;
8470 Lisp_Object a2;
8471 EMACS_INT a3, a4;
8473 Lisp_Object *msg = (Lisp_Object *) a1;
8475 if (Z > BEG)
8476 *msg = make_buffer_string (BEG, Z, 1);
8477 else
8478 *msg = Qnil;
8479 return 0;
8483 /* Push the current message on Vmessage_stack for later restauration
8484 by restore_message. Value is non-zero if the current message isn't
8485 empty. This is a relatively infrequent operation, so it's not
8486 worth optimizing. */
8489 push_message ()
8491 Lisp_Object msg;
8492 msg = current_message ();
8493 Vmessage_stack = Fcons (msg, Vmessage_stack);
8494 return STRINGP (msg);
8498 /* Restore message display from the top of Vmessage_stack. */
8500 void
8501 restore_message ()
8503 Lisp_Object msg;
8505 xassert (CONSP (Vmessage_stack));
8506 msg = XCAR (Vmessage_stack);
8507 if (STRINGP (msg))
8508 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8509 else
8510 message3_nolog (msg, 0, 0);
8514 /* Handler for record_unwind_protect calling pop_message. */
8516 Lisp_Object
8517 pop_message_unwind (dummy)
8518 Lisp_Object dummy;
8520 pop_message ();
8521 return Qnil;
8524 /* Pop the top-most entry off Vmessage_stack. */
8526 void
8527 pop_message ()
8529 xassert (CONSP (Vmessage_stack));
8530 Vmessage_stack = XCDR (Vmessage_stack);
8534 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8535 exits. If the stack is not empty, we have a missing pop_message
8536 somewhere. */
8538 void
8539 check_message_stack ()
8541 if (!NILP (Vmessage_stack))
8542 abort ();
8546 /* Truncate to NCHARS what will be displayed in the echo area the next
8547 time we display it---but don't redisplay it now. */
8549 void
8550 truncate_echo_area (nchars)
8551 int nchars;
8553 if (nchars == 0)
8554 echo_area_buffer[0] = Qnil;
8555 /* A null message buffer means that the frame hasn't really been
8556 initialized yet. Error messages get reported properly by
8557 cmd_error, so this must be just an informative message; toss it. */
8558 else if (!noninteractive
8559 && INTERACTIVE
8560 && !NILP (echo_area_buffer[0]))
8562 struct frame *sf = SELECTED_FRAME ();
8563 if (FRAME_MESSAGE_BUF (sf))
8564 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8569 /* Helper function for truncate_echo_area. Truncate the current
8570 message to at most NCHARS characters. */
8572 static int
8573 truncate_message_1 (nchars, a2, a3, a4)
8574 EMACS_INT nchars;
8575 Lisp_Object a2;
8576 EMACS_INT a3, a4;
8578 if (BEG + nchars < Z)
8579 del_range (BEG + nchars, Z);
8580 if (Z == BEG)
8581 echo_area_buffer[0] = Qnil;
8582 return 0;
8586 /* Set the current message to a substring of S or STRING.
8588 If STRING is a Lisp string, set the message to the first NBYTES
8589 bytes from STRING. NBYTES zero means use the whole string. If
8590 STRING is multibyte, the message will be displayed multibyte.
8592 If S is not null, set the message to the first LEN bytes of S. LEN
8593 zero means use the whole string. MULTIBYTE_P non-zero means S is
8594 multibyte. Display the message multibyte in that case.
8596 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8597 to t before calling set_message_1 (which calls insert).
8600 void
8601 set_message (s, string, nbytes, multibyte_p)
8602 const char *s;
8603 Lisp_Object string;
8604 int nbytes, multibyte_p;
8606 message_enable_multibyte
8607 = ((s && multibyte_p)
8608 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8610 with_echo_area_buffer (0, 0, set_message_1,
8611 (EMACS_INT) s, string, nbytes, multibyte_p);
8612 message_buf_print = 0;
8613 help_echo_showing_p = 0;
8617 /* Helper function for set_message. Arguments have the same meaning
8618 as there, with A1 corresponding to S and A2 corresponding to STRING
8619 This function is called with the echo area buffer being
8620 current. */
8622 static int
8623 set_message_1 (a1, a2, nbytes, multibyte_p)
8624 EMACS_INT a1;
8625 Lisp_Object a2;
8626 EMACS_INT nbytes, multibyte_p;
8628 const char *s = (const char *) a1;
8629 Lisp_Object string = a2;
8631 /* Change multibyteness of the echo buffer appropriately. */
8632 if (message_enable_multibyte
8633 != !NILP (current_buffer->enable_multibyte_characters))
8634 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8636 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8638 /* Insert new message at BEG. */
8639 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8640 Ferase_buffer ();
8642 if (STRINGP (string))
8644 int nchars;
8646 if (nbytes == 0)
8647 nbytes = SBYTES (string);
8648 nchars = string_byte_to_char (string, nbytes);
8650 /* This function takes care of single/multibyte conversion. We
8651 just have to ensure that the echo area buffer has the right
8652 setting of enable_multibyte_characters. */
8653 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8655 else if (s)
8657 if (nbytes == 0)
8658 nbytes = strlen (s);
8660 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8662 /* Convert from multi-byte to single-byte. */
8663 int i, c, n;
8664 unsigned char work[1];
8666 /* Convert a multibyte string to single-byte. */
8667 for (i = 0; i < nbytes; i += n)
8669 c = string_char_and_length (s + i, nbytes - i, &n);
8670 work[0] = (SINGLE_BYTE_CHAR_P (c)
8672 : multibyte_char_to_unibyte (c, Qnil));
8673 insert_1_both (work, 1, 1, 1, 0, 0);
8676 else if (!multibyte_p
8677 && !NILP (current_buffer->enable_multibyte_characters))
8679 /* Convert from single-byte to multi-byte. */
8680 int i, c, n;
8681 const unsigned char *msg = (const unsigned char *) s;
8682 unsigned char str[MAX_MULTIBYTE_LENGTH];
8684 /* Convert a single-byte string to multibyte. */
8685 for (i = 0; i < nbytes; i++)
8687 c = unibyte_char_to_multibyte (msg[i]);
8688 n = CHAR_STRING (c, str);
8689 insert_1_both (str, 1, n, 1, 0, 0);
8692 else
8693 insert_1 (s, nbytes, 1, 0, 0);
8696 return 0;
8700 /* Clear messages. CURRENT_P non-zero means clear the current
8701 message. LAST_DISPLAYED_P non-zero means clear the message
8702 last displayed. */
8704 void
8705 clear_message (current_p, last_displayed_p)
8706 int current_p, last_displayed_p;
8708 if (current_p)
8710 echo_area_buffer[0] = Qnil;
8711 message_cleared_p = 1;
8714 if (last_displayed_p)
8715 echo_area_buffer[1] = Qnil;
8717 message_buf_print = 0;
8720 /* Clear garbaged frames.
8722 This function is used where the old redisplay called
8723 redraw_garbaged_frames which in turn called redraw_frame which in
8724 turn called clear_frame. The call to clear_frame was a source of
8725 flickering. I believe a clear_frame is not necessary. It should
8726 suffice in the new redisplay to invalidate all current matrices,
8727 and ensure a complete redisplay of all windows. */
8729 static void
8730 clear_garbaged_frames ()
8732 if (frame_garbaged)
8734 Lisp_Object tail, frame;
8735 int changed_count = 0;
8737 FOR_EACH_FRAME (tail, frame)
8739 struct frame *f = XFRAME (frame);
8741 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8743 if (f->resized_p)
8745 Fredraw_frame (frame);
8746 f->force_flush_display_p = 1;
8748 clear_current_matrices (f);
8749 changed_count++;
8750 f->garbaged = 0;
8751 f->resized_p = 0;
8755 frame_garbaged = 0;
8756 if (changed_count)
8757 ++windows_or_buffers_changed;
8762 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8763 is non-zero update selected_frame. Value is non-zero if the
8764 mini-windows height has been changed. */
8766 static int
8767 echo_area_display (update_frame_p)
8768 int update_frame_p;
8770 Lisp_Object mini_window;
8771 struct window *w;
8772 struct frame *f;
8773 int window_height_changed_p = 0;
8774 struct frame *sf = SELECTED_FRAME ();
8776 mini_window = FRAME_MINIBUF_WINDOW (sf);
8777 w = XWINDOW (mini_window);
8778 f = XFRAME (WINDOW_FRAME (w));
8780 /* Don't display if frame is invisible or not yet initialized. */
8781 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8782 return 0;
8784 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8785 #ifndef MAC_OS8
8786 #ifdef HAVE_WINDOW_SYSTEM
8787 /* When Emacs starts, selected_frame may be a visible terminal
8788 frame, even if we run under a window system. If we let this
8789 through, a message would be displayed on the terminal. */
8790 if (EQ (selected_frame, Vterminal_frame)
8791 && !NILP (Vwindow_system))
8792 return 0;
8793 #endif /* HAVE_WINDOW_SYSTEM */
8794 #endif
8796 /* Redraw garbaged frames. */
8797 if (frame_garbaged)
8798 clear_garbaged_frames ();
8800 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8802 echo_area_window = mini_window;
8803 window_height_changed_p = display_echo_area (w);
8804 w->must_be_updated_p = 1;
8806 /* Update the display, unless called from redisplay_internal.
8807 Also don't update the screen during redisplay itself. The
8808 update will happen at the end of redisplay, and an update
8809 here could cause confusion. */
8810 if (update_frame_p && !redisplaying_p)
8812 int n = 0;
8814 /* If the display update has been interrupted by pending
8815 input, update mode lines in the frame. Due to the
8816 pending input, it might have been that redisplay hasn't
8817 been called, so that mode lines above the echo area are
8818 garbaged. This looks odd, so we prevent it here. */
8819 if (!display_completed)
8820 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8822 if (window_height_changed_p
8823 /* Don't do this if Emacs is shutting down. Redisplay
8824 needs to run hooks. */
8825 && !NILP (Vrun_hooks))
8827 /* Must update other windows. Likewise as in other
8828 cases, don't let this update be interrupted by
8829 pending input. */
8830 int count = SPECPDL_INDEX ();
8831 specbind (Qredisplay_dont_pause, Qt);
8832 windows_or_buffers_changed = 1;
8833 redisplay_internal (0);
8834 unbind_to (count, Qnil);
8836 else if (FRAME_WINDOW_P (f) && n == 0)
8838 /* Window configuration is the same as before.
8839 Can do with a display update of the echo area,
8840 unless we displayed some mode lines. */
8841 update_single_window (w, 1);
8842 rif->flush_display (f);
8844 else
8845 update_frame (f, 1, 1);
8847 /* If cursor is in the echo area, make sure that the next
8848 redisplay displays the minibuffer, so that the cursor will
8849 be replaced with what the minibuffer wants. */
8850 if (cursor_in_echo_area)
8851 ++windows_or_buffers_changed;
8854 else if (!EQ (mini_window, selected_window))
8855 windows_or_buffers_changed++;
8857 /* The current message is now also the last one displayed. */
8858 echo_area_buffer[1] = echo_area_buffer[0];
8860 /* Prevent redisplay optimization in redisplay_internal by resetting
8861 this_line_start_pos. This is done because the mini-buffer now
8862 displays the message instead of its buffer text. */
8863 if (EQ (mini_window, selected_window))
8864 CHARPOS (this_line_start_pos) = 0;
8866 return window_height_changed_p;
8871 /***********************************************************************
8872 Mode Lines and Frame Titles
8873 ***********************************************************************/
8875 /* A buffer for constructing non-propertized mode-line strings and
8876 frame titles in it; allocated from the heap in init_xdisp and
8877 resized as needed in store_mode_line_noprop_char. */
8879 static char *mode_line_noprop_buf;
8881 /* The buffer's end, and a current output position in it. */
8883 static char *mode_line_noprop_buf_end;
8884 static char *mode_line_noprop_ptr;
8886 #define MODE_LINE_NOPROP_LEN(start) \
8887 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8889 static enum {
8890 MODE_LINE_DISPLAY = 0,
8891 MODE_LINE_TITLE,
8892 MODE_LINE_NOPROP,
8893 MODE_LINE_STRING
8894 } mode_line_target;
8896 /* Alist that caches the results of :propertize.
8897 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8898 static Lisp_Object mode_line_proptrans_alist;
8900 /* List of strings making up the mode-line. */
8901 static Lisp_Object mode_line_string_list;
8903 /* Base face property when building propertized mode line string. */
8904 static Lisp_Object mode_line_string_face;
8905 static Lisp_Object mode_line_string_face_prop;
8908 /* Unwind data for mode line strings */
8910 static Lisp_Object Vmode_line_unwind_vector;
8912 static Lisp_Object
8913 format_mode_line_unwind_data (obuf, save_proptrans)
8914 struct buffer *obuf;
8916 Lisp_Object vector;
8918 /* Reduce consing by keeping one vector in
8919 Vwith_echo_area_save_vector. */
8920 vector = Vmode_line_unwind_vector;
8921 Vmode_line_unwind_vector = Qnil;
8923 if (NILP (vector))
8924 vector = Fmake_vector (make_number (7), Qnil);
8926 AREF (vector, 0) = make_number (mode_line_target);
8927 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8928 AREF (vector, 2) = mode_line_string_list;
8929 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8930 AREF (vector, 4) = mode_line_string_face;
8931 AREF (vector, 5) = mode_line_string_face_prop;
8933 if (obuf)
8934 XSETBUFFER (AREF (vector, 6), obuf);
8935 else
8936 AREF (vector, 6) = Qnil;
8938 return vector;
8941 static Lisp_Object
8942 unwind_format_mode_line (vector)
8943 Lisp_Object vector;
8945 mode_line_target = XINT (AREF (vector, 0));
8946 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8947 mode_line_string_list = AREF (vector, 2);
8948 if (! EQ (AREF (vector, 3), Qt))
8949 mode_line_proptrans_alist = AREF (vector, 3);
8950 mode_line_string_face = AREF (vector, 4);
8951 mode_line_string_face_prop = AREF (vector, 5);
8953 if (!NILP (AREF (vector, 6)))
8955 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8956 AREF (vector, 6) = Qnil;
8959 Vmode_line_unwind_vector = vector;
8960 return Qnil;
8964 /* Store a single character C for the frame title in mode_line_noprop_buf.
8965 Re-allocate mode_line_noprop_buf if necessary. */
8967 static void
8968 #ifdef PROTOTYPES
8969 store_mode_line_noprop_char (char c)
8970 #else
8971 store_mode_line_noprop_char (c)
8972 char c;
8973 #endif
8975 /* If output position has reached the end of the allocated buffer,
8976 double the buffer's size. */
8977 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8979 int len = MODE_LINE_NOPROP_LEN (0);
8980 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8981 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8982 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8983 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8986 *mode_line_noprop_ptr++ = c;
8990 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8991 mode_line_noprop_ptr. STR is the string to store. Do not copy
8992 characters that yield more columns than PRECISION; PRECISION <= 0
8993 means copy the whole string. Pad with spaces until FIELD_WIDTH
8994 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8995 pad. Called from display_mode_element when it is used to build a
8996 frame title. */
8998 static int
8999 store_mode_line_noprop (str, field_width, precision)
9000 const unsigned char *str;
9001 int field_width, precision;
9003 int n = 0;
9004 int dummy, nbytes;
9006 /* Copy at most PRECISION chars from STR. */
9007 nbytes = strlen (str);
9008 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9009 while (nbytes--)
9010 store_mode_line_noprop_char (*str++);
9012 /* Fill up with spaces until FIELD_WIDTH reached. */
9013 while (field_width > 0
9014 && n < field_width)
9016 store_mode_line_noprop_char (' ');
9017 ++n;
9020 return n;
9023 /***********************************************************************
9024 Frame Titles
9025 ***********************************************************************/
9027 #ifdef HAVE_WINDOW_SYSTEM
9029 /* Set the title of FRAME, if it has changed. The title format is
9030 Vicon_title_format if FRAME is iconified, otherwise it is
9031 frame_title_format. */
9033 static void
9034 x_consider_frame_title (frame)
9035 Lisp_Object frame;
9037 struct frame *f = XFRAME (frame);
9039 if (FRAME_WINDOW_P (f)
9040 || FRAME_MINIBUF_ONLY_P (f)
9041 || f->explicit_name)
9043 /* Do we have more than one visible frame on this X display? */
9044 Lisp_Object tail;
9045 Lisp_Object fmt;
9046 int title_start;
9047 char *title;
9048 int len;
9049 struct it it;
9050 int count = SPECPDL_INDEX ();
9052 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9054 Lisp_Object other_frame = XCAR (tail);
9055 struct frame *tf = XFRAME (other_frame);
9057 if (tf != f
9058 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9059 && !FRAME_MINIBUF_ONLY_P (tf)
9060 && !EQ (other_frame, tip_frame)
9061 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9062 break;
9065 /* Set global variable indicating that multiple frames exist. */
9066 multiple_frames = CONSP (tail);
9068 /* Switch to the buffer of selected window of the frame. Set up
9069 mode_line_target so that display_mode_element will output into
9070 mode_line_noprop_buf; then display the title. */
9071 record_unwind_protect (unwind_format_mode_line,
9072 format_mode_line_unwind_data (current_buffer, 0));
9074 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9075 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9077 mode_line_target = MODE_LINE_TITLE;
9078 title_start = MODE_LINE_NOPROP_LEN (0);
9079 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9080 NULL, DEFAULT_FACE_ID);
9081 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9082 len = MODE_LINE_NOPROP_LEN (title_start);
9083 title = mode_line_noprop_buf + title_start;
9084 unbind_to (count, Qnil);
9086 /* Set the title only if it's changed. This avoids consing in
9087 the common case where it hasn't. (If it turns out that we've
9088 already wasted too much time by walking through the list with
9089 display_mode_element, then we might need to optimize at a
9090 higher level than this.) */
9091 if (! STRINGP (f->name)
9092 || SBYTES (f->name) != len
9093 || bcmp (title, SDATA (f->name), len) != 0)
9094 x_implicitly_set_name (f, make_string (title, len), Qnil);
9098 #endif /* not HAVE_WINDOW_SYSTEM */
9103 /***********************************************************************
9104 Menu Bars
9105 ***********************************************************************/
9108 /* Prepare for redisplay by updating menu-bar item lists when
9109 appropriate. This can call eval. */
9111 void
9112 prepare_menu_bars ()
9114 int all_windows;
9115 struct gcpro gcpro1, gcpro2;
9116 struct frame *f;
9117 Lisp_Object tooltip_frame;
9119 #ifdef HAVE_WINDOW_SYSTEM
9120 tooltip_frame = tip_frame;
9121 #else
9122 tooltip_frame = Qnil;
9123 #endif
9125 /* Update all frame titles based on their buffer names, etc. We do
9126 this before the menu bars so that the buffer-menu will show the
9127 up-to-date frame titles. */
9128 #ifdef HAVE_WINDOW_SYSTEM
9129 if (windows_or_buffers_changed || update_mode_lines)
9131 Lisp_Object tail, frame;
9133 FOR_EACH_FRAME (tail, frame)
9135 f = XFRAME (frame);
9136 if (!EQ (frame, tooltip_frame)
9137 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9138 x_consider_frame_title (frame);
9141 #endif /* HAVE_WINDOW_SYSTEM */
9143 /* Update the menu bar item lists, if appropriate. This has to be
9144 done before any actual redisplay or generation of display lines. */
9145 all_windows = (update_mode_lines
9146 || buffer_shared > 1
9147 || windows_or_buffers_changed);
9148 if (all_windows)
9150 Lisp_Object tail, frame;
9151 int count = SPECPDL_INDEX ();
9152 /* 1 means that update_menu_bar has run its hooks
9153 so any further calls to update_menu_bar shouldn't do so again. */
9154 int menu_bar_hooks_run = 0;
9156 record_unwind_save_match_data ();
9158 FOR_EACH_FRAME (tail, frame)
9160 f = XFRAME (frame);
9162 /* Ignore tooltip frame. */
9163 if (EQ (frame, tooltip_frame))
9164 continue;
9166 /* If a window on this frame changed size, report that to
9167 the user and clear the size-change flag. */
9168 if (FRAME_WINDOW_SIZES_CHANGED (f))
9170 Lisp_Object functions;
9172 /* Clear flag first in case we get an error below. */
9173 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9174 functions = Vwindow_size_change_functions;
9175 GCPRO2 (tail, functions);
9177 while (CONSP (functions))
9179 call1 (XCAR (functions), frame);
9180 functions = XCDR (functions);
9182 UNGCPRO;
9185 GCPRO1 (tail);
9186 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9187 #ifdef HAVE_WINDOW_SYSTEM
9188 update_tool_bar (f, 0);
9189 #ifdef MAC_OS
9190 mac_update_title_bar (f, 0);
9191 #endif
9192 #endif
9193 UNGCPRO;
9196 unbind_to (count, Qnil);
9198 else
9200 struct frame *sf = SELECTED_FRAME ();
9201 update_menu_bar (sf, 1, 0);
9202 #ifdef HAVE_WINDOW_SYSTEM
9203 update_tool_bar (sf, 1);
9204 #ifdef MAC_OS
9205 mac_update_title_bar (sf, 1);
9206 #endif
9207 #endif
9210 /* Motif needs this. See comment in xmenu.c. Turn it off when
9211 pending_menu_activation is not defined. */
9212 #ifdef USE_X_TOOLKIT
9213 pending_menu_activation = 0;
9214 #endif
9218 /* Update the menu bar item list for frame F. This has to be done
9219 before we start to fill in any display lines, because it can call
9220 eval.
9222 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9224 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9225 already ran the menu bar hooks for this redisplay, so there
9226 is no need to run them again. The return value is the
9227 updated value of this flag, to pass to the next call. */
9229 static int
9230 update_menu_bar (f, save_match_data, hooks_run)
9231 struct frame *f;
9232 int save_match_data;
9233 int hooks_run;
9235 Lisp_Object window;
9236 register struct window *w;
9238 /* If called recursively during a menu update, do nothing. This can
9239 happen when, for instance, an activate-menubar-hook causes a
9240 redisplay. */
9241 if (inhibit_menubar_update)
9242 return hooks_run;
9244 window = FRAME_SELECTED_WINDOW (f);
9245 w = XWINDOW (window);
9247 #if 0 /* The if statement below this if statement used to include the
9248 condition !NILP (w->update_mode_line), rather than using
9249 update_mode_lines directly, and this if statement may have
9250 been added to make that condition work. Now the if
9251 statement below matches its comment, this isn't needed. */
9252 if (update_mode_lines)
9253 w->update_mode_line = Qt;
9254 #endif
9256 if (FRAME_WINDOW_P (f)
9258 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9259 || defined (USE_GTK)
9260 FRAME_EXTERNAL_MENU_BAR (f)
9261 #else
9262 FRAME_MENU_BAR_LINES (f) > 0
9263 #endif
9264 : FRAME_MENU_BAR_LINES (f) > 0)
9266 /* If the user has switched buffers or windows, we need to
9267 recompute to reflect the new bindings. But we'll
9268 recompute when update_mode_lines is set too; that means
9269 that people can use force-mode-line-update to request
9270 that the menu bar be recomputed. The adverse effect on
9271 the rest of the redisplay algorithm is about the same as
9272 windows_or_buffers_changed anyway. */
9273 if (windows_or_buffers_changed
9274 /* This used to test w->update_mode_line, but we believe
9275 there is no need to recompute the menu in that case. */
9276 || update_mode_lines
9277 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9278 < BUF_MODIFF (XBUFFER (w->buffer)))
9279 != !NILP (w->last_had_star))
9280 || ((!NILP (Vtransient_mark_mode)
9281 && !NILP (XBUFFER (w->buffer)->mark_active))
9282 != !NILP (w->region_showing)))
9284 struct buffer *prev = current_buffer;
9285 int count = SPECPDL_INDEX ();
9287 specbind (Qinhibit_menubar_update, Qt);
9289 set_buffer_internal_1 (XBUFFER (w->buffer));
9290 if (save_match_data)
9291 record_unwind_save_match_data ();
9292 if (NILP (Voverriding_local_map_menu_flag))
9294 specbind (Qoverriding_terminal_local_map, Qnil);
9295 specbind (Qoverriding_local_map, Qnil);
9298 if (!hooks_run)
9300 /* Run the Lucid hook. */
9301 safe_run_hooks (Qactivate_menubar_hook);
9303 /* If it has changed current-menubar from previous value,
9304 really recompute the menu-bar from the value. */
9305 if (! NILP (Vlucid_menu_bar_dirty_flag))
9306 call0 (Qrecompute_lucid_menubar);
9308 safe_run_hooks (Qmenu_bar_update_hook);
9310 hooks_run = 1;
9313 XSETFRAME (Vmenu_updating_frame, f);
9314 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9316 /* Redisplay the menu bar in case we changed it. */
9317 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9318 || defined (USE_GTK)
9319 if (FRAME_WINDOW_P (f))
9321 #ifdef MAC_OS
9322 /* All frames on Mac OS share the same menubar. So only
9323 the selected frame should be allowed to set it. */
9324 if (f == SELECTED_FRAME ())
9325 #endif
9326 set_frame_menubar (f, 0, 0);
9328 else
9329 /* On a terminal screen, the menu bar is an ordinary screen
9330 line, and this makes it get updated. */
9331 w->update_mode_line = Qt;
9332 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9333 /* In the non-toolkit version, the menu bar is an ordinary screen
9334 line, and this makes it get updated. */
9335 w->update_mode_line = Qt;
9336 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9338 unbind_to (count, Qnil);
9339 set_buffer_internal_1 (prev);
9343 return hooks_run;
9348 /***********************************************************************
9349 Output Cursor
9350 ***********************************************************************/
9352 #ifdef HAVE_WINDOW_SYSTEM
9354 /* EXPORT:
9355 Nominal cursor position -- where to draw output.
9356 HPOS and VPOS are window relative glyph matrix coordinates.
9357 X and Y are window relative pixel coordinates. */
9359 struct cursor_pos output_cursor;
9362 /* EXPORT:
9363 Set the global variable output_cursor to CURSOR. All cursor
9364 positions are relative to updated_window. */
9366 void
9367 set_output_cursor (cursor)
9368 struct cursor_pos *cursor;
9370 output_cursor.hpos = cursor->hpos;
9371 output_cursor.vpos = cursor->vpos;
9372 output_cursor.x = cursor->x;
9373 output_cursor.y = cursor->y;
9377 /* EXPORT for RIF:
9378 Set a nominal cursor position.
9380 HPOS and VPOS are column/row positions in a window glyph matrix. X
9381 and Y are window text area relative pixel positions.
9383 If this is done during an update, updated_window will contain the
9384 window that is being updated and the position is the future output
9385 cursor position for that window. If updated_window is null, use
9386 selected_window and display the cursor at the given position. */
9388 void
9389 x_cursor_to (vpos, hpos, y, x)
9390 int vpos, hpos, y, x;
9392 struct window *w;
9394 /* If updated_window is not set, work on selected_window. */
9395 if (updated_window)
9396 w = updated_window;
9397 else
9398 w = XWINDOW (selected_window);
9400 /* Set the output cursor. */
9401 output_cursor.hpos = hpos;
9402 output_cursor.vpos = vpos;
9403 output_cursor.x = x;
9404 output_cursor.y = y;
9406 /* If not called as part of an update, really display the cursor.
9407 This will also set the cursor position of W. */
9408 if (updated_window == NULL)
9410 BLOCK_INPUT;
9411 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9412 if (rif->flush_display_optional)
9413 rif->flush_display_optional (SELECTED_FRAME ());
9414 UNBLOCK_INPUT;
9418 #endif /* HAVE_WINDOW_SYSTEM */
9421 /***********************************************************************
9422 Tool-bars
9423 ***********************************************************************/
9425 #ifdef HAVE_WINDOW_SYSTEM
9427 /* Where the mouse was last time we reported a mouse event. */
9429 FRAME_PTR last_mouse_frame;
9431 /* Tool-bar item index of the item on which a mouse button was pressed
9432 or -1. */
9434 int last_tool_bar_item;
9437 /* Update the tool-bar item list for frame F. This has to be done
9438 before we start to fill in any display lines. Called from
9439 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9440 and restore it here. */
9442 static void
9443 update_tool_bar (f, save_match_data)
9444 struct frame *f;
9445 int save_match_data;
9447 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9448 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9449 #else
9450 int do_update = WINDOWP (f->tool_bar_window)
9451 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9452 #endif
9454 if (do_update)
9456 Lisp_Object window;
9457 struct window *w;
9459 window = FRAME_SELECTED_WINDOW (f);
9460 w = XWINDOW (window);
9462 /* If the user has switched buffers or windows, we need to
9463 recompute to reflect the new bindings. But we'll
9464 recompute when update_mode_lines is set too; that means
9465 that people can use force-mode-line-update to request
9466 that the menu bar be recomputed. The adverse effect on
9467 the rest of the redisplay algorithm is about the same as
9468 windows_or_buffers_changed anyway. */
9469 if (windows_or_buffers_changed
9470 || !NILP (w->update_mode_line)
9471 || update_mode_lines
9472 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9473 < BUF_MODIFF (XBUFFER (w->buffer)))
9474 != !NILP (w->last_had_star))
9475 || ((!NILP (Vtransient_mark_mode)
9476 && !NILP (XBUFFER (w->buffer)->mark_active))
9477 != !NILP (w->region_showing)))
9479 struct buffer *prev = current_buffer;
9480 int count = SPECPDL_INDEX ();
9481 Lisp_Object new_tool_bar;
9482 int new_n_tool_bar;
9483 struct gcpro gcpro1;
9485 /* Set current_buffer to the buffer of the selected
9486 window of the frame, so that we get the right local
9487 keymaps. */
9488 set_buffer_internal_1 (XBUFFER (w->buffer));
9490 /* Save match data, if we must. */
9491 if (save_match_data)
9492 record_unwind_save_match_data ();
9494 /* Make sure that we don't accidentally use bogus keymaps. */
9495 if (NILP (Voverriding_local_map_menu_flag))
9497 specbind (Qoverriding_terminal_local_map, Qnil);
9498 specbind (Qoverriding_local_map, Qnil);
9501 GCPRO1 (new_tool_bar);
9503 /* Build desired tool-bar items from keymaps. */
9504 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9505 &new_n_tool_bar);
9507 /* Redisplay the tool-bar if we changed it. */
9508 if (new_n_tool_bar != f->n_tool_bar_items
9509 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9511 /* Redisplay that happens asynchronously due to an expose event
9512 may access f->tool_bar_items. Make sure we update both
9513 variables within BLOCK_INPUT so no such event interrupts. */
9514 BLOCK_INPUT;
9515 f->tool_bar_items = new_tool_bar;
9516 f->n_tool_bar_items = new_n_tool_bar;
9517 w->update_mode_line = Qt;
9518 UNBLOCK_INPUT;
9521 UNGCPRO;
9523 unbind_to (count, Qnil);
9524 set_buffer_internal_1 (prev);
9530 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9531 F's desired tool-bar contents. F->tool_bar_items must have
9532 been set up previously by calling prepare_menu_bars. */
9534 static void
9535 build_desired_tool_bar_string (f)
9536 struct frame *f;
9538 int i, size, size_needed;
9539 struct gcpro gcpro1, gcpro2, gcpro3;
9540 Lisp_Object image, plist, props;
9542 image = plist = props = Qnil;
9543 GCPRO3 (image, plist, props);
9545 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9546 Otherwise, make a new string. */
9548 /* The size of the string we might be able to reuse. */
9549 size = (STRINGP (f->desired_tool_bar_string)
9550 ? SCHARS (f->desired_tool_bar_string)
9551 : 0);
9553 /* We need one space in the string for each image. */
9554 size_needed = f->n_tool_bar_items;
9556 /* Reuse f->desired_tool_bar_string, if possible. */
9557 if (size < size_needed || NILP (f->desired_tool_bar_string))
9558 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9559 make_number (' '));
9560 else
9562 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9563 Fremove_text_properties (make_number (0), make_number (size),
9564 props, f->desired_tool_bar_string);
9567 /* Put a `display' property on the string for the images to display,
9568 put a `menu_item' property on tool-bar items with a value that
9569 is the index of the item in F's tool-bar item vector. */
9570 for (i = 0; i < f->n_tool_bar_items; ++i)
9572 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9574 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9575 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9576 int hmargin, vmargin, relief, idx, end;
9577 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9579 /* If image is a vector, choose the image according to the
9580 button state. */
9581 image = PROP (TOOL_BAR_ITEM_IMAGES);
9582 if (VECTORP (image))
9584 if (enabled_p)
9585 idx = (selected_p
9586 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9587 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9588 else
9589 idx = (selected_p
9590 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9591 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9593 xassert (ASIZE (image) >= idx);
9594 image = AREF (image, idx);
9596 else
9597 idx = -1;
9599 /* Ignore invalid image specifications. */
9600 if (!valid_image_p (image))
9601 continue;
9603 /* Display the tool-bar button pressed, or depressed. */
9604 plist = Fcopy_sequence (XCDR (image));
9606 /* Compute margin and relief to draw. */
9607 relief = (tool_bar_button_relief >= 0
9608 ? tool_bar_button_relief
9609 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9610 hmargin = vmargin = relief;
9612 if (INTEGERP (Vtool_bar_button_margin)
9613 && XINT (Vtool_bar_button_margin) > 0)
9615 hmargin += XFASTINT (Vtool_bar_button_margin);
9616 vmargin += XFASTINT (Vtool_bar_button_margin);
9618 else if (CONSP (Vtool_bar_button_margin))
9620 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9621 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9622 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9624 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9625 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9626 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9629 if (auto_raise_tool_bar_buttons_p)
9631 /* Add a `:relief' property to the image spec if the item is
9632 selected. */
9633 if (selected_p)
9635 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9636 hmargin -= relief;
9637 vmargin -= relief;
9640 else
9642 /* If image is selected, display it pressed, i.e. with a
9643 negative relief. If it's not selected, display it with a
9644 raised relief. */
9645 plist = Fplist_put (plist, QCrelief,
9646 (selected_p
9647 ? make_number (-relief)
9648 : make_number (relief)));
9649 hmargin -= relief;
9650 vmargin -= relief;
9653 /* Put a margin around the image. */
9654 if (hmargin || vmargin)
9656 if (hmargin == vmargin)
9657 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9658 else
9659 plist = Fplist_put (plist, QCmargin,
9660 Fcons (make_number (hmargin),
9661 make_number (vmargin)));
9664 /* If button is not enabled, and we don't have special images
9665 for the disabled state, make the image appear disabled by
9666 applying an appropriate algorithm to it. */
9667 if (!enabled_p && idx < 0)
9668 plist = Fplist_put (plist, QCconversion, Qdisabled);
9670 /* Put a `display' text property on the string for the image to
9671 display. Put a `menu-item' property on the string that gives
9672 the start of this item's properties in the tool-bar items
9673 vector. */
9674 image = Fcons (Qimage, plist);
9675 props = list4 (Qdisplay, image,
9676 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9678 /* Let the last image hide all remaining spaces in the tool bar
9679 string. The string can be longer than needed when we reuse a
9680 previous string. */
9681 if (i + 1 == f->n_tool_bar_items)
9682 end = SCHARS (f->desired_tool_bar_string);
9683 else
9684 end = i + 1;
9685 Fadd_text_properties (make_number (i), make_number (end),
9686 props, f->desired_tool_bar_string);
9687 #undef PROP
9690 UNGCPRO;
9694 /* Display one line of the tool-bar of frame IT->f.
9696 HEIGHT specifies the desired height of the tool-bar line.
9697 If the actual height of the glyph row is less than HEIGHT, the
9698 row's height is increased to HEIGHT, and the icons are centered
9699 vertically in the new height.
9701 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9702 count a final empty row in case the tool-bar width exactly matches
9703 the window width.
9706 static void
9707 display_tool_bar_line (it, height)
9708 struct it *it;
9709 int height;
9711 struct glyph_row *row = it->glyph_row;
9712 int max_x = it->last_visible_x;
9713 struct glyph *last;
9715 prepare_desired_row (row);
9716 row->y = it->current_y;
9718 /* Note that this isn't made use of if the face hasn't a box,
9719 so there's no need to check the face here. */
9720 it->start_of_box_run_p = 1;
9722 while (it->current_x < max_x)
9724 int x, n_glyphs_before, i, nglyphs;
9725 struct it it_before;
9727 /* Get the next display element. */
9728 if (!get_next_display_element (it))
9730 /* Don't count empty row if we are counting needed tool-bar lines. */
9731 if (height < 0 && !it->hpos)
9732 return;
9733 break;
9736 /* Produce glyphs. */
9737 n_glyphs_before = row->used[TEXT_AREA];
9738 it_before = *it;
9740 PRODUCE_GLYPHS (it);
9742 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9743 i = 0;
9744 x = it_before.current_x;
9745 while (i < nglyphs)
9747 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9749 if (x + glyph->pixel_width > max_x)
9751 /* Glyph doesn't fit on line. Backtrack. */
9752 row->used[TEXT_AREA] = n_glyphs_before;
9753 *it = it_before;
9754 /* If this is the only glyph on this line, it will never fit on the
9755 toolbar, so skip it. But ensure there is at least one glyph,
9756 so we don't accidentally disable the tool-bar. */
9757 if (n_glyphs_before == 0
9758 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9759 break;
9760 goto out;
9763 ++it->hpos;
9764 x += glyph->pixel_width;
9765 ++i;
9768 /* Stop at line ends. */
9769 if (ITERATOR_AT_END_OF_LINE_P (it))
9770 break;
9772 set_iterator_to_next (it, 1);
9775 out:;
9777 row->displays_text_p = row->used[TEXT_AREA] != 0;
9779 /* Use default face for the border below the tool bar.
9781 FIXME: When auto-resize-tool-bars is grow-only, there is
9782 no additional border below the possibly empty tool-bar lines.
9783 So to make the extra empty lines look "normal", we have to
9784 use the tool-bar face for the border too. */
9785 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9786 it->face_id = DEFAULT_FACE_ID;
9788 extend_face_to_end_of_line (it);
9789 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9790 last->right_box_line_p = 1;
9791 if (last == row->glyphs[TEXT_AREA])
9792 last->left_box_line_p = 1;
9794 /* Make line the desired height and center it vertically. */
9795 if ((height -= it->max_ascent + it->max_descent) > 0)
9797 /* Don't add more than one line height. */
9798 height %= FRAME_LINE_HEIGHT (it->f);
9799 it->max_ascent += height / 2;
9800 it->max_descent += (height + 1) / 2;
9803 compute_line_metrics (it);
9805 /* If line is empty, make it occupy the rest of the tool-bar. */
9806 if (!row->displays_text_p)
9808 row->height = row->phys_height = it->last_visible_y - row->y;
9809 row->visible_height = row->height;
9810 row->ascent = row->phys_ascent = 0;
9811 row->extra_line_spacing = 0;
9814 row->full_width_p = 1;
9815 row->continued_p = 0;
9816 row->truncated_on_left_p = 0;
9817 row->truncated_on_right_p = 0;
9819 it->current_x = it->hpos = 0;
9820 it->current_y += row->height;
9821 ++it->vpos;
9822 ++it->glyph_row;
9826 /* Max tool-bar height. */
9828 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9829 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9831 /* Value is the number of screen lines needed to make all tool-bar
9832 items of frame F visible. The number of actual rows needed is
9833 returned in *N_ROWS if non-NULL. */
9835 static int
9836 tool_bar_lines_needed (f, n_rows)
9837 struct frame *f;
9838 int *n_rows;
9840 struct window *w = XWINDOW (f->tool_bar_window);
9841 struct it it;
9842 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9843 the desired matrix, so use (unused) mode-line row as temporary row to
9844 avoid destroying the first tool-bar row. */
9845 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9847 /* Initialize an iterator for iteration over
9848 F->desired_tool_bar_string in the tool-bar window of frame F. */
9849 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9850 it.first_visible_x = 0;
9851 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9852 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9854 while (!ITERATOR_AT_END_P (&it))
9856 clear_glyph_row (temp_row);
9857 it.glyph_row = temp_row;
9858 display_tool_bar_line (&it, -1);
9860 clear_glyph_row (temp_row);
9862 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9863 if (n_rows)
9864 *n_rows = it.vpos > 0 ? it.vpos : -1;
9866 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9870 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9871 0, 1, 0,
9872 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9873 (frame)
9874 Lisp_Object frame;
9876 struct frame *f;
9877 struct window *w;
9878 int nlines = 0;
9880 if (NILP (frame))
9881 frame = selected_frame;
9882 else
9883 CHECK_FRAME (frame);
9884 f = XFRAME (frame);
9886 if (WINDOWP (f->tool_bar_window)
9887 || (w = XWINDOW (f->tool_bar_window),
9888 WINDOW_TOTAL_LINES (w) > 0))
9890 update_tool_bar (f, 1);
9891 if (f->n_tool_bar_items)
9893 build_desired_tool_bar_string (f);
9894 nlines = tool_bar_lines_needed (f, NULL);
9898 return make_number (nlines);
9902 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9903 height should be changed. */
9905 static int
9906 redisplay_tool_bar (f)
9907 struct frame *f;
9909 struct window *w;
9910 struct it it;
9911 struct glyph_row *row;
9913 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9914 if (FRAME_EXTERNAL_TOOL_BAR (f))
9915 update_frame_tool_bar (f);
9916 return 0;
9917 #endif
9919 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9920 do anything. This means you must start with tool-bar-lines
9921 non-zero to get the auto-sizing effect. Or in other words, you
9922 can turn off tool-bars by specifying tool-bar-lines zero. */
9923 if (!WINDOWP (f->tool_bar_window)
9924 || (w = XWINDOW (f->tool_bar_window),
9925 WINDOW_TOTAL_LINES (w) == 0))
9926 return 0;
9928 /* Set up an iterator for the tool-bar window. */
9929 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9930 it.first_visible_x = 0;
9931 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9932 row = it.glyph_row;
9934 /* Build a string that represents the contents of the tool-bar. */
9935 build_desired_tool_bar_string (f);
9936 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9938 if (f->n_tool_bar_rows == 0)
9940 int nlines;
9942 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9943 nlines != WINDOW_TOTAL_LINES (w)))
9945 extern Lisp_Object Qtool_bar_lines;
9946 Lisp_Object frame;
9947 int old_height = WINDOW_TOTAL_LINES (w);
9949 XSETFRAME (frame, f);
9950 Fmodify_frame_parameters (frame,
9951 Fcons (Fcons (Qtool_bar_lines,
9952 make_number (nlines)),
9953 Qnil));
9954 if (WINDOW_TOTAL_LINES (w) != old_height)
9956 clear_glyph_matrix (w->desired_matrix);
9957 fonts_changed_p = 1;
9958 return 1;
9963 /* Display as many lines as needed to display all tool-bar items. */
9965 if (f->n_tool_bar_rows > 0)
9967 int border, rows, height, extra;
9969 if (INTEGERP (Vtool_bar_border))
9970 border = XINT (Vtool_bar_border);
9971 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9972 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9973 else if (EQ (Vtool_bar_border, Qborder_width))
9974 border = f->border_width;
9975 else
9976 border = 0;
9977 if (border < 0)
9978 border = 0;
9980 rows = f->n_tool_bar_rows;
9981 height = max (1, (it.last_visible_y - border) / rows);
9982 extra = it.last_visible_y - border - height * rows;
9984 while (it.current_y < it.last_visible_y)
9986 int h = 0;
9987 if (extra > 0 && rows-- > 0)
9989 h = (extra + rows - 1) / rows;
9990 extra -= h;
9992 display_tool_bar_line (&it, height + h);
9995 else
9997 while (it.current_y < it.last_visible_y)
9998 display_tool_bar_line (&it, 0);
10001 /* It doesn't make much sense to try scrolling in the tool-bar
10002 window, so don't do it. */
10003 w->desired_matrix->no_scrolling_p = 1;
10004 w->must_be_updated_p = 1;
10006 if (!NILP (Vauto_resize_tool_bars))
10008 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10009 int change_height_p = 0;
10011 /* If we couldn't display everything, change the tool-bar's
10012 height if there is room for more. */
10013 if (IT_STRING_CHARPOS (it) < it.end_charpos
10014 && it.current_y < max_tool_bar_height)
10015 change_height_p = 1;
10017 row = it.glyph_row - 1;
10019 /* If there are blank lines at the end, except for a partially
10020 visible blank line at the end that is smaller than
10021 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10022 if (!row->displays_text_p
10023 && row->height >= FRAME_LINE_HEIGHT (f))
10024 change_height_p = 1;
10026 /* If row displays tool-bar items, but is partially visible,
10027 change the tool-bar's height. */
10028 if (row->displays_text_p
10029 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10030 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10031 change_height_p = 1;
10033 /* Resize windows as needed by changing the `tool-bar-lines'
10034 frame parameter. */
10035 if (change_height_p)
10037 extern Lisp_Object Qtool_bar_lines;
10038 Lisp_Object frame;
10039 int old_height = WINDOW_TOTAL_LINES (w);
10040 int nrows;
10041 int nlines = tool_bar_lines_needed (f, &nrows);
10043 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10044 && !f->minimize_tool_bar_window_p)
10045 ? (nlines > old_height)
10046 : (nlines != old_height));
10047 f->minimize_tool_bar_window_p = 0;
10049 if (change_height_p)
10051 XSETFRAME (frame, f);
10052 Fmodify_frame_parameters (frame,
10053 Fcons (Fcons (Qtool_bar_lines,
10054 make_number (nlines)),
10055 Qnil));
10056 if (WINDOW_TOTAL_LINES (w) != old_height)
10058 clear_glyph_matrix (w->desired_matrix);
10059 f->n_tool_bar_rows = nrows;
10060 fonts_changed_p = 1;
10061 return 1;
10067 f->minimize_tool_bar_window_p = 0;
10068 return 0;
10072 /* Get information about the tool-bar item which is displayed in GLYPH
10073 on frame F. Return in *PROP_IDX the index where tool-bar item
10074 properties start in F->tool_bar_items. Value is zero if
10075 GLYPH doesn't display a tool-bar item. */
10077 static int
10078 tool_bar_item_info (f, glyph, prop_idx)
10079 struct frame *f;
10080 struct glyph *glyph;
10081 int *prop_idx;
10083 Lisp_Object prop;
10084 int success_p;
10085 int charpos;
10087 /* This function can be called asynchronously, which means we must
10088 exclude any possibility that Fget_text_property signals an
10089 error. */
10090 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10091 charpos = max (0, charpos);
10093 /* Get the text property `menu-item' at pos. The value of that
10094 property is the start index of this item's properties in
10095 F->tool_bar_items. */
10096 prop = Fget_text_property (make_number (charpos),
10097 Qmenu_item, f->current_tool_bar_string);
10098 if (INTEGERP (prop))
10100 *prop_idx = XINT (prop);
10101 success_p = 1;
10103 else
10104 success_p = 0;
10106 return success_p;
10110 /* Get information about the tool-bar item at position X/Y on frame F.
10111 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10112 the current matrix of the tool-bar window of F, or NULL if not
10113 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10114 item in F->tool_bar_items. Value is
10116 -1 if X/Y is not on a tool-bar item
10117 0 if X/Y is on the same item that was highlighted before.
10118 1 otherwise. */
10120 static int
10121 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10122 struct frame *f;
10123 int x, y;
10124 struct glyph **glyph;
10125 int *hpos, *vpos, *prop_idx;
10127 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10128 struct window *w = XWINDOW (f->tool_bar_window);
10129 int area;
10131 /* Find the glyph under X/Y. */
10132 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10133 if (*glyph == NULL)
10134 return -1;
10136 /* Get the start of this tool-bar item's properties in
10137 f->tool_bar_items. */
10138 if (!tool_bar_item_info (f, *glyph, prop_idx))
10139 return -1;
10141 /* Is mouse on the highlighted item? */
10142 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10143 && *vpos >= dpyinfo->mouse_face_beg_row
10144 && *vpos <= dpyinfo->mouse_face_end_row
10145 && (*vpos > dpyinfo->mouse_face_beg_row
10146 || *hpos >= dpyinfo->mouse_face_beg_col)
10147 && (*vpos < dpyinfo->mouse_face_end_row
10148 || *hpos < dpyinfo->mouse_face_end_col
10149 || dpyinfo->mouse_face_past_end))
10150 return 0;
10152 return 1;
10156 /* EXPORT:
10157 Handle mouse button event on the tool-bar of frame F, at
10158 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10159 0 for button release. MODIFIERS is event modifiers for button
10160 release. */
10162 void
10163 handle_tool_bar_click (f, x, y, down_p, modifiers)
10164 struct frame *f;
10165 int x, y, down_p;
10166 unsigned int modifiers;
10168 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10169 struct window *w = XWINDOW (f->tool_bar_window);
10170 int hpos, vpos, prop_idx;
10171 struct glyph *glyph;
10172 Lisp_Object enabled_p;
10174 /* If not on the highlighted tool-bar item, return. */
10175 frame_to_window_pixel_xy (w, &x, &y);
10176 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10177 return;
10179 /* If item is disabled, do nothing. */
10180 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10181 if (NILP (enabled_p))
10182 return;
10184 if (down_p)
10186 /* Show item in pressed state. */
10187 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10188 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10189 last_tool_bar_item = prop_idx;
10191 else
10193 Lisp_Object key, frame;
10194 struct input_event event;
10195 EVENT_INIT (event);
10197 /* Show item in released state. */
10198 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10199 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10201 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10203 XSETFRAME (frame, f);
10204 event.kind = TOOL_BAR_EVENT;
10205 event.frame_or_window = frame;
10206 event.arg = frame;
10207 kbd_buffer_store_event (&event);
10209 event.kind = TOOL_BAR_EVENT;
10210 event.frame_or_window = frame;
10211 event.arg = key;
10212 event.modifiers = modifiers;
10213 kbd_buffer_store_event (&event);
10214 last_tool_bar_item = -1;
10219 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10220 tool-bar window-relative coordinates X/Y. Called from
10221 note_mouse_highlight. */
10223 static void
10224 note_tool_bar_highlight (f, x, y)
10225 struct frame *f;
10226 int x, y;
10228 Lisp_Object window = f->tool_bar_window;
10229 struct window *w = XWINDOW (window);
10230 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10231 int hpos, vpos;
10232 struct glyph *glyph;
10233 struct glyph_row *row;
10234 int i;
10235 Lisp_Object enabled_p;
10236 int prop_idx;
10237 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10238 int mouse_down_p, rc;
10240 /* Function note_mouse_highlight is called with negative x(y
10241 values when mouse moves outside of the frame. */
10242 if (x <= 0 || y <= 0)
10244 clear_mouse_face (dpyinfo);
10245 return;
10248 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10249 if (rc < 0)
10251 /* Not on tool-bar item. */
10252 clear_mouse_face (dpyinfo);
10253 return;
10255 else if (rc == 0)
10256 /* On same tool-bar item as before. */
10257 goto set_help_echo;
10259 clear_mouse_face (dpyinfo);
10261 /* Mouse is down, but on different tool-bar item? */
10262 mouse_down_p = (dpyinfo->grabbed
10263 && f == last_mouse_frame
10264 && FRAME_LIVE_P (f));
10265 if (mouse_down_p
10266 && last_tool_bar_item != prop_idx)
10267 return;
10269 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10270 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10272 /* If tool-bar item is not enabled, don't highlight it. */
10273 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10274 if (!NILP (enabled_p))
10276 /* Compute the x-position of the glyph. In front and past the
10277 image is a space. We include this in the highlighted area. */
10278 row = MATRIX_ROW (w->current_matrix, vpos);
10279 for (i = x = 0; i < hpos; ++i)
10280 x += row->glyphs[TEXT_AREA][i].pixel_width;
10282 /* Record this as the current active region. */
10283 dpyinfo->mouse_face_beg_col = hpos;
10284 dpyinfo->mouse_face_beg_row = vpos;
10285 dpyinfo->mouse_face_beg_x = x;
10286 dpyinfo->mouse_face_beg_y = row->y;
10287 dpyinfo->mouse_face_past_end = 0;
10289 dpyinfo->mouse_face_end_col = hpos + 1;
10290 dpyinfo->mouse_face_end_row = vpos;
10291 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10292 dpyinfo->mouse_face_end_y = row->y;
10293 dpyinfo->mouse_face_window = window;
10294 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10296 /* Display it as active. */
10297 show_mouse_face (dpyinfo, draw);
10298 dpyinfo->mouse_face_image_state = draw;
10301 set_help_echo:
10303 /* Set help_echo_string to a help string to display for this tool-bar item.
10304 XTread_socket does the rest. */
10305 help_echo_object = help_echo_window = Qnil;
10306 help_echo_pos = -1;
10307 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10308 if (NILP (help_echo_string))
10309 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10312 #endif /* HAVE_WINDOW_SYSTEM */
10316 /************************************************************************
10317 Horizontal scrolling
10318 ************************************************************************/
10320 static int hscroll_window_tree P_ ((Lisp_Object));
10321 static int hscroll_windows P_ ((Lisp_Object));
10323 /* For all leaf windows in the window tree rooted at WINDOW, set their
10324 hscroll value so that PT is (i) visible in the window, and (ii) so
10325 that it is not within a certain margin at the window's left and
10326 right border. Value is non-zero if any window's hscroll has been
10327 changed. */
10329 static int
10330 hscroll_window_tree (window)
10331 Lisp_Object window;
10333 int hscrolled_p = 0;
10334 int hscroll_relative_p = FLOATP (Vhscroll_step);
10335 int hscroll_step_abs = 0;
10336 double hscroll_step_rel = 0;
10338 if (hscroll_relative_p)
10340 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10341 if (hscroll_step_rel < 0)
10343 hscroll_relative_p = 0;
10344 hscroll_step_abs = 0;
10347 else if (INTEGERP (Vhscroll_step))
10349 hscroll_step_abs = XINT (Vhscroll_step);
10350 if (hscroll_step_abs < 0)
10351 hscroll_step_abs = 0;
10353 else
10354 hscroll_step_abs = 0;
10356 while (WINDOWP (window))
10358 struct window *w = XWINDOW (window);
10360 if (WINDOWP (w->hchild))
10361 hscrolled_p |= hscroll_window_tree (w->hchild);
10362 else if (WINDOWP (w->vchild))
10363 hscrolled_p |= hscroll_window_tree (w->vchild);
10364 else if (w->cursor.vpos >= 0)
10366 int h_margin;
10367 int text_area_width;
10368 struct glyph_row *current_cursor_row
10369 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10370 struct glyph_row *desired_cursor_row
10371 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10372 struct glyph_row *cursor_row
10373 = (desired_cursor_row->enabled_p
10374 ? desired_cursor_row
10375 : current_cursor_row);
10377 text_area_width = window_box_width (w, TEXT_AREA);
10379 /* Scroll when cursor is inside this scroll margin. */
10380 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10382 if ((XFASTINT (w->hscroll)
10383 && w->cursor.x <= h_margin)
10384 || (cursor_row->enabled_p
10385 && cursor_row->truncated_on_right_p
10386 && (w->cursor.x >= text_area_width - h_margin)))
10388 struct it it;
10389 int hscroll;
10390 struct buffer *saved_current_buffer;
10391 int pt;
10392 int wanted_x;
10394 /* Find point in a display of infinite width. */
10395 saved_current_buffer = current_buffer;
10396 current_buffer = XBUFFER (w->buffer);
10398 if (w == XWINDOW (selected_window))
10399 pt = BUF_PT (current_buffer);
10400 else
10402 pt = marker_position (w->pointm);
10403 pt = max (BEGV, pt);
10404 pt = min (ZV, pt);
10407 /* Move iterator to pt starting at cursor_row->start in
10408 a line with infinite width. */
10409 init_to_row_start (&it, w, cursor_row);
10410 it.last_visible_x = INFINITY;
10411 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10412 current_buffer = saved_current_buffer;
10414 /* Position cursor in window. */
10415 if (!hscroll_relative_p && hscroll_step_abs == 0)
10416 hscroll = max (0, (it.current_x
10417 - (ITERATOR_AT_END_OF_LINE_P (&it)
10418 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10419 : (text_area_width / 2))))
10420 / FRAME_COLUMN_WIDTH (it.f);
10421 else if (w->cursor.x >= text_area_width - h_margin)
10423 if (hscroll_relative_p)
10424 wanted_x = text_area_width * (1 - hscroll_step_rel)
10425 - h_margin;
10426 else
10427 wanted_x = text_area_width
10428 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10429 - h_margin;
10430 hscroll
10431 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10433 else
10435 if (hscroll_relative_p)
10436 wanted_x = text_area_width * hscroll_step_rel
10437 + h_margin;
10438 else
10439 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10440 + h_margin;
10441 hscroll
10442 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10444 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10446 /* Don't call Fset_window_hscroll if value hasn't
10447 changed because it will prevent redisplay
10448 optimizations. */
10449 if (XFASTINT (w->hscroll) != hscroll)
10451 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10452 w->hscroll = make_number (hscroll);
10453 hscrolled_p = 1;
10458 window = w->next;
10461 /* Value is non-zero if hscroll of any leaf window has been changed. */
10462 return hscrolled_p;
10466 /* Set hscroll so that cursor is visible and not inside horizontal
10467 scroll margins for all windows in the tree rooted at WINDOW. See
10468 also hscroll_window_tree above. Value is non-zero if any window's
10469 hscroll has been changed. If it has, desired matrices on the frame
10470 of WINDOW are cleared. */
10472 static int
10473 hscroll_windows (window)
10474 Lisp_Object window;
10476 int hscrolled_p;
10478 if (automatic_hscrolling_p)
10480 hscrolled_p = hscroll_window_tree (window);
10481 if (hscrolled_p)
10482 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10484 else
10485 hscrolled_p = 0;
10486 return hscrolled_p;
10491 /************************************************************************
10492 Redisplay
10493 ************************************************************************/
10495 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10496 to a non-zero value. This is sometimes handy to have in a debugger
10497 session. */
10499 #if GLYPH_DEBUG
10501 /* First and last unchanged row for try_window_id. */
10503 int debug_first_unchanged_at_end_vpos;
10504 int debug_last_unchanged_at_beg_vpos;
10506 /* Delta vpos and y. */
10508 int debug_dvpos, debug_dy;
10510 /* Delta in characters and bytes for try_window_id. */
10512 int debug_delta, debug_delta_bytes;
10514 /* Values of window_end_pos and window_end_vpos at the end of
10515 try_window_id. */
10517 EMACS_INT debug_end_pos, debug_end_vpos;
10519 /* Append a string to W->desired_matrix->method. FMT is a printf
10520 format string. A1...A9 are a supplement for a variable-length
10521 argument list. If trace_redisplay_p is non-zero also printf the
10522 resulting string to stderr. */
10524 static void
10525 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10526 struct window *w;
10527 char *fmt;
10528 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10530 char buffer[512];
10531 char *method = w->desired_matrix->method;
10532 int len = strlen (method);
10533 int size = sizeof w->desired_matrix->method;
10534 int remaining = size - len - 1;
10536 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10537 if (len && remaining)
10539 method[len] = '|';
10540 --remaining, ++len;
10543 strncpy (method + len, buffer, remaining);
10545 if (trace_redisplay_p)
10546 fprintf (stderr, "%p (%s): %s\n",
10548 ((BUFFERP (w->buffer)
10549 && STRINGP (XBUFFER (w->buffer)->name))
10550 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10551 : "no buffer"),
10552 buffer);
10555 #endif /* GLYPH_DEBUG */
10558 /* Value is non-zero if all changes in window W, which displays
10559 current_buffer, are in the text between START and END. START is a
10560 buffer position, END is given as a distance from Z. Used in
10561 redisplay_internal for display optimization. */
10563 static INLINE int
10564 text_outside_line_unchanged_p (w, start, end)
10565 struct window *w;
10566 int start, end;
10568 int unchanged_p = 1;
10570 /* If text or overlays have changed, see where. */
10571 if (XFASTINT (w->last_modified) < MODIFF
10572 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10574 /* Gap in the line? */
10575 if (GPT < start || Z - GPT < end)
10576 unchanged_p = 0;
10578 /* Changes start in front of the line, or end after it? */
10579 if (unchanged_p
10580 && (BEG_UNCHANGED < start - 1
10581 || END_UNCHANGED < end))
10582 unchanged_p = 0;
10584 /* If selective display, can't optimize if changes start at the
10585 beginning of the line. */
10586 if (unchanged_p
10587 && INTEGERP (current_buffer->selective_display)
10588 && XINT (current_buffer->selective_display) > 0
10589 && (BEG_UNCHANGED < start || GPT <= start))
10590 unchanged_p = 0;
10592 /* If there are overlays at the start or end of the line, these
10593 may have overlay strings with newlines in them. A change at
10594 START, for instance, may actually concern the display of such
10595 overlay strings as well, and they are displayed on different
10596 lines. So, quickly rule out this case. (For the future, it
10597 might be desirable to implement something more telling than
10598 just BEG/END_UNCHANGED.) */
10599 if (unchanged_p)
10601 if (BEG + BEG_UNCHANGED == start
10602 && overlay_touches_p (start))
10603 unchanged_p = 0;
10604 if (END_UNCHANGED == end
10605 && overlay_touches_p (Z - end))
10606 unchanged_p = 0;
10610 return unchanged_p;
10614 /* Do a frame update, taking possible shortcuts into account. This is
10615 the main external entry point for redisplay.
10617 If the last redisplay displayed an echo area message and that message
10618 is no longer requested, we clear the echo area or bring back the
10619 mini-buffer if that is in use. */
10621 void
10622 redisplay ()
10624 redisplay_internal (0);
10628 static Lisp_Object
10629 overlay_arrow_string_or_property (var)
10630 Lisp_Object var;
10632 Lisp_Object val;
10634 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10635 return val;
10637 return Voverlay_arrow_string;
10640 /* Return 1 if there are any overlay-arrows in current_buffer. */
10641 static int
10642 overlay_arrow_in_current_buffer_p ()
10644 Lisp_Object vlist;
10646 for (vlist = Voverlay_arrow_variable_list;
10647 CONSP (vlist);
10648 vlist = XCDR (vlist))
10650 Lisp_Object var = XCAR (vlist);
10651 Lisp_Object val;
10653 if (!SYMBOLP (var))
10654 continue;
10655 val = find_symbol_value (var);
10656 if (MARKERP (val)
10657 && current_buffer == XMARKER (val)->buffer)
10658 return 1;
10660 return 0;
10664 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10665 has changed. */
10667 static int
10668 overlay_arrows_changed_p ()
10670 Lisp_Object vlist;
10672 for (vlist = Voverlay_arrow_variable_list;
10673 CONSP (vlist);
10674 vlist = XCDR (vlist))
10676 Lisp_Object var = XCAR (vlist);
10677 Lisp_Object val, pstr;
10679 if (!SYMBOLP (var))
10680 continue;
10681 val = find_symbol_value (var);
10682 if (!MARKERP (val))
10683 continue;
10684 if (! EQ (COERCE_MARKER (val),
10685 Fget (var, Qlast_arrow_position))
10686 || ! (pstr = overlay_arrow_string_or_property (var),
10687 EQ (pstr, Fget (var, Qlast_arrow_string))))
10688 return 1;
10690 return 0;
10693 /* Mark overlay arrows to be updated on next redisplay. */
10695 static void
10696 update_overlay_arrows (up_to_date)
10697 int up_to_date;
10699 Lisp_Object vlist;
10701 for (vlist = Voverlay_arrow_variable_list;
10702 CONSP (vlist);
10703 vlist = XCDR (vlist))
10705 Lisp_Object var = XCAR (vlist);
10707 if (!SYMBOLP (var))
10708 continue;
10710 if (up_to_date > 0)
10712 Lisp_Object val = find_symbol_value (var);
10713 Fput (var, Qlast_arrow_position,
10714 COERCE_MARKER (val));
10715 Fput (var, Qlast_arrow_string,
10716 overlay_arrow_string_or_property (var));
10718 else if (up_to_date < 0
10719 || !NILP (Fget (var, Qlast_arrow_position)))
10721 Fput (var, Qlast_arrow_position, Qt);
10722 Fput (var, Qlast_arrow_string, Qt);
10728 /* Return overlay arrow string to display at row.
10729 Return integer (bitmap number) for arrow bitmap in left fringe.
10730 Return nil if no overlay arrow. */
10732 static Lisp_Object
10733 overlay_arrow_at_row (it, row)
10734 struct it *it;
10735 struct glyph_row *row;
10737 Lisp_Object vlist;
10739 for (vlist = Voverlay_arrow_variable_list;
10740 CONSP (vlist);
10741 vlist = XCDR (vlist))
10743 Lisp_Object var = XCAR (vlist);
10744 Lisp_Object val;
10746 if (!SYMBOLP (var))
10747 continue;
10749 val = find_symbol_value (var);
10751 if (MARKERP (val)
10752 && current_buffer == XMARKER (val)->buffer
10753 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10755 if (FRAME_WINDOW_P (it->f)
10756 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10758 #ifdef HAVE_WINDOW_SYSTEM
10759 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10761 int fringe_bitmap;
10762 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10763 return make_number (fringe_bitmap);
10765 #endif
10766 return make_number (-1); /* Use default arrow bitmap */
10768 return overlay_arrow_string_or_property (var);
10772 return Qnil;
10775 /* Return 1 if point moved out of or into a composition. Otherwise
10776 return 0. PREV_BUF and PREV_PT are the last point buffer and
10777 position. BUF and PT are the current point buffer and position. */
10780 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10781 struct buffer *prev_buf, *buf;
10782 int prev_pt, pt;
10784 int start, end;
10785 Lisp_Object prop;
10786 Lisp_Object buffer;
10788 XSETBUFFER (buffer, buf);
10789 /* Check a composition at the last point if point moved within the
10790 same buffer. */
10791 if (prev_buf == buf)
10793 if (prev_pt == pt)
10794 /* Point didn't move. */
10795 return 0;
10797 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10798 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10799 && COMPOSITION_VALID_P (start, end, prop)
10800 && start < prev_pt && end > prev_pt)
10801 /* The last point was within the composition. Return 1 iff
10802 point moved out of the composition. */
10803 return (pt <= start || pt >= end);
10806 /* Check a composition at the current point. */
10807 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10808 && find_composition (pt, -1, &start, &end, &prop, buffer)
10809 && COMPOSITION_VALID_P (start, end, prop)
10810 && start < pt && end > pt);
10814 /* Reconsider the setting of B->clip_changed which is displayed
10815 in window W. */
10817 static INLINE void
10818 reconsider_clip_changes (w, b)
10819 struct window *w;
10820 struct buffer *b;
10822 if (b->clip_changed
10823 && !NILP (w->window_end_valid)
10824 && w->current_matrix->buffer == b
10825 && w->current_matrix->zv == BUF_ZV (b)
10826 && w->current_matrix->begv == BUF_BEGV (b))
10827 b->clip_changed = 0;
10829 /* If display wasn't paused, and W is not a tool bar window, see if
10830 point has been moved into or out of a composition. In that case,
10831 we set b->clip_changed to 1 to force updating the screen. If
10832 b->clip_changed has already been set to 1, we can skip this
10833 check. */
10834 if (!b->clip_changed
10835 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10837 int pt;
10839 if (w == XWINDOW (selected_window))
10840 pt = BUF_PT (current_buffer);
10841 else
10842 pt = marker_position (w->pointm);
10844 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10845 || pt != XINT (w->last_point))
10846 && check_point_in_composition (w->current_matrix->buffer,
10847 XINT (w->last_point),
10848 XBUFFER (w->buffer), pt))
10849 b->clip_changed = 1;
10854 /* Select FRAME to forward the values of frame-local variables into C
10855 variables so that the redisplay routines can access those values
10856 directly. */
10858 static void
10859 select_frame_for_redisplay (frame)
10860 Lisp_Object frame;
10862 Lisp_Object tail, sym, val;
10863 Lisp_Object old = selected_frame;
10865 selected_frame = frame;
10867 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10868 if (CONSP (XCAR (tail))
10869 && (sym = XCAR (XCAR (tail)),
10870 SYMBOLP (sym))
10871 && (sym = indirect_variable (sym),
10872 val = SYMBOL_VALUE (sym),
10873 (BUFFER_LOCAL_VALUEP (val)
10874 || SOME_BUFFER_LOCAL_VALUEP (val)))
10875 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10876 /* Use find_symbol_value rather than Fsymbol_value
10877 to avoid an error if it is void. */
10878 find_symbol_value (sym);
10880 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10881 if (CONSP (XCAR (tail))
10882 && (sym = XCAR (XCAR (tail)),
10883 SYMBOLP (sym))
10884 && (sym = indirect_variable (sym),
10885 val = SYMBOL_VALUE (sym),
10886 (BUFFER_LOCAL_VALUEP (val)
10887 || SOME_BUFFER_LOCAL_VALUEP (val)))
10888 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10889 find_symbol_value (sym);
10893 #define STOP_POLLING \
10894 do { if (! polling_stopped_here) stop_polling (); \
10895 polling_stopped_here = 1; } while (0)
10897 #define RESUME_POLLING \
10898 do { if (polling_stopped_here) start_polling (); \
10899 polling_stopped_here = 0; } while (0)
10902 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10903 response to any user action; therefore, we should preserve the echo
10904 area. (Actually, our caller does that job.) Perhaps in the future
10905 avoid recentering windows if it is not necessary; currently that
10906 causes some problems. */
10908 static void
10909 redisplay_internal (preserve_echo_area)
10910 int preserve_echo_area;
10912 struct window *w = XWINDOW (selected_window);
10913 struct frame *f;
10914 int pause;
10915 int must_finish = 0;
10916 struct text_pos tlbufpos, tlendpos;
10917 int number_of_visible_frames;
10918 int count, count1;
10919 struct frame *sf;
10920 int polling_stopped_here = 0;
10922 /* Non-zero means redisplay has to consider all windows on all
10923 frames. Zero means, only selected_window is considered. */
10924 int consider_all_windows_p;
10926 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10928 /* No redisplay if running in batch mode or frame is not yet fully
10929 initialized, or redisplay is explicitly turned off by setting
10930 Vinhibit_redisplay. */
10931 if (noninteractive
10932 || !NILP (Vinhibit_redisplay))
10933 return;
10935 /* Don't examine these until after testing Vinhibit_redisplay.
10936 When Emacs is shutting down, perhaps because its connection to
10937 X has dropped, we should not look at them at all. */
10938 f = XFRAME (w->frame);
10939 sf = SELECTED_FRAME ();
10941 if (!f->glyphs_initialized_p)
10942 return;
10944 /* The flag redisplay_performed_directly_p is set by
10945 direct_output_for_insert when it already did the whole screen
10946 update necessary. */
10947 if (redisplay_performed_directly_p)
10949 redisplay_performed_directly_p = 0;
10950 if (!hscroll_windows (selected_window))
10951 return;
10954 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10955 if (popup_activated ())
10956 return;
10957 #endif
10959 /* I don't think this happens but let's be paranoid. */
10960 if (redisplaying_p)
10961 return;
10963 /* Record a function that resets redisplaying_p to its old value
10964 when we leave this function. */
10965 count = SPECPDL_INDEX ();
10966 record_unwind_protect (unwind_redisplay,
10967 Fcons (make_number (redisplaying_p), selected_frame));
10968 ++redisplaying_p;
10969 specbind (Qinhibit_free_realized_faces, Qnil);
10972 Lisp_Object tail, frame;
10974 FOR_EACH_FRAME (tail, frame)
10976 struct frame *f = XFRAME (frame);
10977 f->already_hscrolled_p = 0;
10981 retry:
10982 pause = 0;
10983 reconsider_clip_changes (w, current_buffer);
10984 last_escape_glyph_frame = NULL;
10985 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10987 /* If new fonts have been loaded that make a glyph matrix adjustment
10988 necessary, do it. */
10989 if (fonts_changed_p)
10991 adjust_glyphs (NULL);
10992 ++windows_or_buffers_changed;
10993 fonts_changed_p = 0;
10996 /* If face_change_count is non-zero, init_iterator will free all
10997 realized faces, which includes the faces referenced from current
10998 matrices. So, we can't reuse current matrices in this case. */
10999 if (face_change_count)
11000 ++windows_or_buffers_changed;
11002 if (! FRAME_WINDOW_P (sf)
11003 && previous_terminal_frame != sf)
11005 /* Since frames on an ASCII terminal share the same display
11006 area, displaying a different frame means redisplay the whole
11007 thing. */
11008 windows_or_buffers_changed++;
11009 SET_FRAME_GARBAGED (sf);
11010 XSETFRAME (Vterminal_frame, sf);
11012 previous_terminal_frame = sf;
11014 /* Set the visible flags for all frames. Do this before checking
11015 for resized or garbaged frames; they want to know if their frames
11016 are visible. See the comment in frame.h for
11017 FRAME_SAMPLE_VISIBILITY. */
11019 Lisp_Object tail, frame;
11021 number_of_visible_frames = 0;
11023 FOR_EACH_FRAME (tail, frame)
11025 struct frame *f = XFRAME (frame);
11027 FRAME_SAMPLE_VISIBILITY (f);
11028 if (FRAME_VISIBLE_P (f))
11029 ++number_of_visible_frames;
11030 clear_desired_matrices (f);
11034 /* Notice any pending interrupt request to change frame size. */
11035 do_pending_window_change (1);
11037 /* Clear frames marked as garbaged. */
11038 if (frame_garbaged)
11039 clear_garbaged_frames ();
11041 /* Build menubar and tool-bar items. */
11042 if (NILP (Vmemory_full))
11043 prepare_menu_bars ();
11045 if (windows_or_buffers_changed)
11046 update_mode_lines++;
11048 /* Detect case that we need to write or remove a star in the mode line. */
11049 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11051 w->update_mode_line = Qt;
11052 if (buffer_shared > 1)
11053 update_mode_lines++;
11056 /* Avoid invocation of point motion hooks by `current_column' below. */
11057 count1 = SPECPDL_INDEX ();
11058 specbind (Qinhibit_point_motion_hooks, Qt);
11060 /* If %c is in the mode line, update it if needed. */
11061 if (!NILP (w->column_number_displayed)
11062 /* This alternative quickly identifies a common case
11063 where no change is needed. */
11064 && !(PT == XFASTINT (w->last_point)
11065 && XFASTINT (w->last_modified) >= MODIFF
11066 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11067 && (XFASTINT (w->column_number_displayed)
11068 != (int) current_column ())) /* iftc */
11069 w->update_mode_line = Qt;
11071 unbind_to (count1, Qnil);
11073 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11075 /* The variable buffer_shared is set in redisplay_window and
11076 indicates that we redisplay a buffer in different windows. See
11077 there. */
11078 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11079 || cursor_type_changed);
11081 /* If specs for an arrow have changed, do thorough redisplay
11082 to ensure we remove any arrow that should no longer exist. */
11083 if (overlay_arrows_changed_p ())
11084 consider_all_windows_p = windows_or_buffers_changed = 1;
11086 /* Normally the message* functions will have already displayed and
11087 updated the echo area, but the frame may have been trashed, or
11088 the update may have been preempted, so display the echo area
11089 again here. Checking message_cleared_p captures the case that
11090 the echo area should be cleared. */
11091 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11092 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11093 || (message_cleared_p
11094 && minibuf_level == 0
11095 /* If the mini-window is currently selected, this means the
11096 echo-area doesn't show through. */
11097 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11099 int window_height_changed_p = echo_area_display (0);
11100 must_finish = 1;
11102 /* If we don't display the current message, don't clear the
11103 message_cleared_p flag, because, if we did, we wouldn't clear
11104 the echo area in the next redisplay which doesn't preserve
11105 the echo area. */
11106 if (!display_last_displayed_message_p)
11107 message_cleared_p = 0;
11109 if (fonts_changed_p)
11110 goto retry;
11111 else if (window_height_changed_p)
11113 consider_all_windows_p = 1;
11114 ++update_mode_lines;
11115 ++windows_or_buffers_changed;
11117 /* If window configuration was changed, frames may have been
11118 marked garbaged. Clear them or we will experience
11119 surprises wrt scrolling. */
11120 if (frame_garbaged)
11121 clear_garbaged_frames ();
11124 else if (EQ (selected_window, minibuf_window)
11125 && (current_buffer->clip_changed
11126 || XFASTINT (w->last_modified) < MODIFF
11127 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11128 && resize_mini_window (w, 0))
11130 /* Resized active mini-window to fit the size of what it is
11131 showing if its contents might have changed. */
11132 must_finish = 1;
11133 consider_all_windows_p = 1;
11134 ++windows_or_buffers_changed;
11135 ++update_mode_lines;
11137 /* If window configuration was changed, frames may have been
11138 marked garbaged. Clear them or we will experience
11139 surprises wrt scrolling. */
11140 if (frame_garbaged)
11141 clear_garbaged_frames ();
11145 /* If showing the region, and mark has changed, we must redisplay
11146 the whole window. The assignment to this_line_start_pos prevents
11147 the optimization directly below this if-statement. */
11148 if (((!NILP (Vtransient_mark_mode)
11149 && !NILP (XBUFFER (w->buffer)->mark_active))
11150 != !NILP (w->region_showing))
11151 || (!NILP (w->region_showing)
11152 && !EQ (w->region_showing,
11153 Fmarker_position (XBUFFER (w->buffer)->mark))))
11154 CHARPOS (this_line_start_pos) = 0;
11156 /* Optimize the case that only the line containing the cursor in the
11157 selected window has changed. Variables starting with this_ are
11158 set in display_line and record information about the line
11159 containing the cursor. */
11160 tlbufpos = this_line_start_pos;
11161 tlendpos = this_line_end_pos;
11162 if (!consider_all_windows_p
11163 && CHARPOS (tlbufpos) > 0
11164 && NILP (w->update_mode_line)
11165 && !current_buffer->clip_changed
11166 && !current_buffer->prevent_redisplay_optimizations_p
11167 && FRAME_VISIBLE_P (XFRAME (w->frame))
11168 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11169 /* Make sure recorded data applies to current buffer, etc. */
11170 && this_line_buffer == current_buffer
11171 && current_buffer == XBUFFER (w->buffer)
11172 && NILP (w->force_start)
11173 && NILP (w->optional_new_start)
11174 /* Point must be on the line that we have info recorded about. */
11175 && PT >= CHARPOS (tlbufpos)
11176 && PT <= Z - CHARPOS (tlendpos)
11177 /* All text outside that line, including its final newline,
11178 must be unchanged */
11179 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11180 CHARPOS (tlendpos)))
11182 if (CHARPOS (tlbufpos) > BEGV
11183 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11184 && (CHARPOS (tlbufpos) == ZV
11185 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11186 /* Former continuation line has disappeared by becoming empty */
11187 goto cancel;
11188 else if (XFASTINT (w->last_modified) < MODIFF
11189 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11190 || MINI_WINDOW_P (w))
11192 /* We have to handle the case of continuation around a
11193 wide-column character (See the comment in indent.c around
11194 line 885).
11196 For instance, in the following case:
11198 -------- Insert --------
11199 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11200 J_I_ ==> J_I_ `^^' are cursors.
11201 ^^ ^^
11202 -------- --------
11204 As we have to redraw the line above, we should goto cancel. */
11206 struct it it;
11207 int line_height_before = this_line_pixel_height;
11209 /* Note that start_display will handle the case that the
11210 line starting at tlbufpos is a continuation lines. */
11211 start_display (&it, w, tlbufpos);
11213 /* Implementation note: It this still necessary? */
11214 if (it.current_x != this_line_start_x)
11215 goto cancel;
11217 TRACE ((stderr, "trying display optimization 1\n"));
11218 w->cursor.vpos = -1;
11219 overlay_arrow_seen = 0;
11220 it.vpos = this_line_vpos;
11221 it.current_y = this_line_y;
11222 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11223 display_line (&it);
11225 /* If line contains point, is not continued,
11226 and ends at same distance from eob as before, we win */
11227 if (w->cursor.vpos >= 0
11228 /* Line is not continued, otherwise this_line_start_pos
11229 would have been set to 0 in display_line. */
11230 && CHARPOS (this_line_start_pos)
11231 /* Line ends as before. */
11232 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11233 /* Line has same height as before. Otherwise other lines
11234 would have to be shifted up or down. */
11235 && this_line_pixel_height == line_height_before)
11237 /* If this is not the window's last line, we must adjust
11238 the charstarts of the lines below. */
11239 if (it.current_y < it.last_visible_y)
11241 struct glyph_row *row
11242 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11243 int delta, delta_bytes;
11245 if (Z - CHARPOS (tlendpos) == ZV)
11247 /* This line ends at end of (accessible part of)
11248 buffer. There is no newline to count. */
11249 delta = (Z
11250 - CHARPOS (tlendpos)
11251 - MATRIX_ROW_START_CHARPOS (row));
11252 delta_bytes = (Z_BYTE
11253 - BYTEPOS (tlendpos)
11254 - MATRIX_ROW_START_BYTEPOS (row));
11256 else
11258 /* This line ends in a newline. Must take
11259 account of the newline and the rest of the
11260 text that follows. */
11261 delta = (Z
11262 - CHARPOS (tlendpos)
11263 - MATRIX_ROW_START_CHARPOS (row));
11264 delta_bytes = (Z_BYTE
11265 - BYTEPOS (tlendpos)
11266 - MATRIX_ROW_START_BYTEPOS (row));
11269 increment_matrix_positions (w->current_matrix,
11270 this_line_vpos + 1,
11271 w->current_matrix->nrows,
11272 delta, delta_bytes);
11275 /* If this row displays text now but previously didn't,
11276 or vice versa, w->window_end_vpos may have to be
11277 adjusted. */
11278 if ((it.glyph_row - 1)->displays_text_p)
11280 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11281 XSETINT (w->window_end_vpos, this_line_vpos);
11283 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11284 && this_line_vpos > 0)
11285 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11286 w->window_end_valid = Qnil;
11288 /* Update hint: No need to try to scroll in update_window. */
11289 w->desired_matrix->no_scrolling_p = 1;
11291 #if GLYPH_DEBUG
11292 *w->desired_matrix->method = 0;
11293 debug_method_add (w, "optimization 1");
11294 #endif
11295 #ifdef HAVE_WINDOW_SYSTEM
11296 update_window_fringes (w, 0);
11297 #endif
11298 goto update;
11300 else
11301 goto cancel;
11303 else if (/* Cursor position hasn't changed. */
11304 PT == XFASTINT (w->last_point)
11305 /* Make sure the cursor was last displayed
11306 in this window. Otherwise we have to reposition it. */
11307 && 0 <= w->cursor.vpos
11308 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11310 if (!must_finish)
11312 do_pending_window_change (1);
11314 /* We used to always goto end_of_redisplay here, but this
11315 isn't enough if we have a blinking cursor. */
11316 if (w->cursor_off_p == w->last_cursor_off_p)
11317 goto end_of_redisplay;
11319 goto update;
11321 /* If highlighting the region, or if the cursor is in the echo area,
11322 then we can't just move the cursor. */
11323 else if (! (!NILP (Vtransient_mark_mode)
11324 && !NILP (current_buffer->mark_active))
11325 && (EQ (selected_window, current_buffer->last_selected_window)
11326 || highlight_nonselected_windows)
11327 && NILP (w->region_showing)
11328 && NILP (Vshow_trailing_whitespace)
11329 && !cursor_in_echo_area)
11331 struct it it;
11332 struct glyph_row *row;
11334 /* Skip from tlbufpos to PT and see where it is. Note that
11335 PT may be in invisible text. If so, we will end at the
11336 next visible position. */
11337 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11338 NULL, DEFAULT_FACE_ID);
11339 it.current_x = this_line_start_x;
11340 it.current_y = this_line_y;
11341 it.vpos = this_line_vpos;
11343 /* The call to move_it_to stops in front of PT, but
11344 moves over before-strings. */
11345 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11347 if (it.vpos == this_line_vpos
11348 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11349 row->enabled_p))
11351 xassert (this_line_vpos == it.vpos);
11352 xassert (this_line_y == it.current_y);
11353 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11354 #if GLYPH_DEBUG
11355 *w->desired_matrix->method = 0;
11356 debug_method_add (w, "optimization 3");
11357 #endif
11358 goto update;
11360 else
11361 goto cancel;
11364 cancel:
11365 /* Text changed drastically or point moved off of line. */
11366 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11369 CHARPOS (this_line_start_pos) = 0;
11370 consider_all_windows_p |= buffer_shared > 1;
11371 ++clear_face_cache_count;
11372 #ifdef HAVE_WINDOW_SYSTEM
11373 ++clear_image_cache_count;
11374 #endif
11376 /* Build desired matrices, and update the display. If
11377 consider_all_windows_p is non-zero, do it for all windows on all
11378 frames. Otherwise do it for selected_window, only. */
11380 if (consider_all_windows_p)
11382 Lisp_Object tail, frame;
11384 FOR_EACH_FRAME (tail, frame)
11385 XFRAME (frame)->updated_p = 0;
11387 /* Recompute # windows showing selected buffer. This will be
11388 incremented each time such a window is displayed. */
11389 buffer_shared = 0;
11391 FOR_EACH_FRAME (tail, frame)
11393 struct frame *f = XFRAME (frame);
11395 if (FRAME_WINDOW_P (f) || f == sf)
11397 if (! EQ (frame, selected_frame))
11398 /* Select the frame, for the sake of frame-local
11399 variables. */
11400 select_frame_for_redisplay (frame);
11402 /* Mark all the scroll bars to be removed; we'll redeem
11403 the ones we want when we redisplay their windows. */
11404 if (condemn_scroll_bars_hook)
11405 condemn_scroll_bars_hook (f);
11407 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11408 redisplay_windows (FRAME_ROOT_WINDOW (f));
11410 /* Any scroll bars which redisplay_windows should have
11411 nuked should now go away. */
11412 if (judge_scroll_bars_hook)
11413 judge_scroll_bars_hook (f);
11415 /* If fonts changed, display again. */
11416 /* ??? rms: I suspect it is a mistake to jump all the way
11417 back to retry here. It should just retry this frame. */
11418 if (fonts_changed_p)
11419 goto retry;
11421 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11423 /* See if we have to hscroll. */
11424 if (!f->already_hscrolled_p)
11426 f->already_hscrolled_p = 1;
11427 if (hscroll_windows (f->root_window))
11428 goto retry;
11431 /* Prevent various kinds of signals during display
11432 update. stdio is not robust about handling
11433 signals, which can cause an apparent I/O
11434 error. */
11435 if (interrupt_input)
11436 unrequest_sigio ();
11437 STOP_POLLING;
11439 /* Update the display. */
11440 set_window_update_flags (XWINDOW (f->root_window), 1);
11441 pause |= update_frame (f, 0, 0);
11442 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11443 if (pause)
11444 break;
11445 #endif
11447 f->updated_p = 1;
11452 if (!pause)
11454 /* Do the mark_window_display_accurate after all windows have
11455 been redisplayed because this call resets flags in buffers
11456 which are needed for proper redisplay. */
11457 FOR_EACH_FRAME (tail, frame)
11459 struct frame *f = XFRAME (frame);
11460 if (f->updated_p)
11462 mark_window_display_accurate (f->root_window, 1);
11463 if (frame_up_to_date_hook)
11464 frame_up_to_date_hook (f);
11469 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11471 Lisp_Object mini_window;
11472 struct frame *mini_frame;
11474 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11475 /* Use list_of_error, not Qerror, so that
11476 we catch only errors and don't run the debugger. */
11477 internal_condition_case_1 (redisplay_window_1, selected_window,
11478 list_of_error,
11479 redisplay_window_error);
11481 /* Compare desired and current matrices, perform output. */
11483 update:
11484 /* If fonts changed, display again. */
11485 if (fonts_changed_p)
11486 goto retry;
11488 /* Prevent various kinds of signals during display update.
11489 stdio is not robust about handling signals,
11490 which can cause an apparent I/O error. */
11491 if (interrupt_input)
11492 unrequest_sigio ();
11493 STOP_POLLING;
11495 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11497 if (hscroll_windows (selected_window))
11498 goto retry;
11500 XWINDOW (selected_window)->must_be_updated_p = 1;
11501 pause = update_frame (sf, 0, 0);
11504 /* We may have called echo_area_display at the top of this
11505 function. If the echo area is on another frame, that may
11506 have put text on a frame other than the selected one, so the
11507 above call to update_frame would not have caught it. Catch
11508 it here. */
11509 mini_window = FRAME_MINIBUF_WINDOW (sf);
11510 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11512 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11514 XWINDOW (mini_window)->must_be_updated_p = 1;
11515 pause |= update_frame (mini_frame, 0, 0);
11516 if (!pause && hscroll_windows (mini_window))
11517 goto retry;
11521 /* If display was paused because of pending input, make sure we do a
11522 thorough update the next time. */
11523 if (pause)
11525 /* Prevent the optimization at the beginning of
11526 redisplay_internal that tries a single-line update of the
11527 line containing the cursor in the selected window. */
11528 CHARPOS (this_line_start_pos) = 0;
11530 /* Let the overlay arrow be updated the next time. */
11531 update_overlay_arrows (0);
11533 /* If we pause after scrolling, some rows in the current
11534 matrices of some windows are not valid. */
11535 if (!WINDOW_FULL_WIDTH_P (w)
11536 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11537 update_mode_lines = 1;
11539 else
11541 if (!consider_all_windows_p)
11543 /* This has already been done above if
11544 consider_all_windows_p is set. */
11545 mark_window_display_accurate_1 (w, 1);
11547 /* Say overlay arrows are up to date. */
11548 update_overlay_arrows (1);
11550 if (frame_up_to_date_hook != 0)
11551 frame_up_to_date_hook (sf);
11554 update_mode_lines = 0;
11555 windows_or_buffers_changed = 0;
11556 cursor_type_changed = 0;
11559 /* Start SIGIO interrupts coming again. Having them off during the
11560 code above makes it less likely one will discard output, but not
11561 impossible, since there might be stuff in the system buffer here.
11562 But it is much hairier to try to do anything about that. */
11563 if (interrupt_input)
11564 request_sigio ();
11565 RESUME_POLLING;
11567 /* If a frame has become visible which was not before, redisplay
11568 again, so that we display it. Expose events for such a frame
11569 (which it gets when becoming visible) don't call the parts of
11570 redisplay constructing glyphs, so simply exposing a frame won't
11571 display anything in this case. So, we have to display these
11572 frames here explicitly. */
11573 if (!pause)
11575 Lisp_Object tail, frame;
11576 int new_count = 0;
11578 FOR_EACH_FRAME (tail, frame)
11580 int this_is_visible = 0;
11582 if (XFRAME (frame)->visible)
11583 this_is_visible = 1;
11584 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11585 if (XFRAME (frame)->visible)
11586 this_is_visible = 1;
11588 if (this_is_visible)
11589 new_count++;
11592 if (new_count != number_of_visible_frames)
11593 windows_or_buffers_changed++;
11596 /* Change frame size now if a change is pending. */
11597 do_pending_window_change (1);
11599 /* If we just did a pending size change, or have additional
11600 visible frames, redisplay again. */
11601 if (windows_or_buffers_changed && !pause)
11602 goto retry;
11604 /* Clear the face cache eventually. */
11605 if (consider_all_windows_p)
11607 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11609 clear_face_cache (0);
11610 clear_face_cache_count = 0;
11612 #ifdef HAVE_WINDOW_SYSTEM
11613 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11615 Lisp_Object tail, frame;
11616 FOR_EACH_FRAME (tail, frame)
11618 struct frame *f = XFRAME (frame);
11619 if (FRAME_WINDOW_P (f))
11620 clear_image_cache (f, 0);
11622 clear_image_cache_count = 0;
11624 #endif /* HAVE_WINDOW_SYSTEM */
11627 end_of_redisplay:
11628 unbind_to (count, Qnil);
11629 RESUME_POLLING;
11633 /* Redisplay, but leave alone any recent echo area message unless
11634 another message has been requested in its place.
11636 This is useful in situations where you need to redisplay but no
11637 user action has occurred, making it inappropriate for the message
11638 area to be cleared. See tracking_off and
11639 wait_reading_process_output for examples of these situations.
11641 FROM_WHERE is an integer saying from where this function was
11642 called. This is useful for debugging. */
11644 void
11645 redisplay_preserve_echo_area (from_where)
11646 int from_where;
11648 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11650 if (!NILP (echo_area_buffer[1]))
11652 /* We have a previously displayed message, but no current
11653 message. Redisplay the previous message. */
11654 display_last_displayed_message_p = 1;
11655 redisplay_internal (1);
11656 display_last_displayed_message_p = 0;
11658 else
11659 redisplay_internal (1);
11661 if (rif != NULL && rif->flush_display_optional)
11662 rif->flush_display_optional (NULL);
11666 /* Function registered with record_unwind_protect in
11667 redisplay_internal. Reset redisplaying_p to the value it had
11668 before redisplay_internal was called, and clear
11669 prevent_freeing_realized_faces_p. It also selects the previously
11670 selected frame. */
11672 static Lisp_Object
11673 unwind_redisplay (val)
11674 Lisp_Object val;
11676 Lisp_Object old_redisplaying_p, old_frame;
11678 old_redisplaying_p = XCAR (val);
11679 redisplaying_p = XFASTINT (old_redisplaying_p);
11680 old_frame = XCDR (val);
11681 if (! EQ (old_frame, selected_frame))
11682 select_frame_for_redisplay (old_frame);
11683 return Qnil;
11687 /* Mark the display of window W as accurate or inaccurate. If
11688 ACCURATE_P is non-zero mark display of W as accurate. If
11689 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11690 redisplay_internal is called. */
11692 static void
11693 mark_window_display_accurate_1 (w, accurate_p)
11694 struct window *w;
11695 int accurate_p;
11697 if (BUFFERP (w->buffer))
11699 struct buffer *b = XBUFFER (w->buffer);
11701 w->last_modified
11702 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11703 w->last_overlay_modified
11704 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11705 w->last_had_star
11706 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11708 if (accurate_p)
11710 b->clip_changed = 0;
11711 b->prevent_redisplay_optimizations_p = 0;
11713 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11714 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11715 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11716 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11718 w->current_matrix->buffer = b;
11719 w->current_matrix->begv = BUF_BEGV (b);
11720 w->current_matrix->zv = BUF_ZV (b);
11722 w->last_cursor = w->cursor;
11723 w->last_cursor_off_p = w->cursor_off_p;
11725 if (w == XWINDOW (selected_window))
11726 w->last_point = make_number (BUF_PT (b));
11727 else
11728 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11732 if (accurate_p)
11734 w->window_end_valid = w->buffer;
11735 #if 0 /* This is incorrect with variable-height lines. */
11736 xassert (XINT (w->window_end_vpos)
11737 < (WINDOW_TOTAL_LINES (w)
11738 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11739 #endif
11740 w->update_mode_line = Qnil;
11745 /* Mark the display of windows in the window tree rooted at WINDOW as
11746 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11747 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11748 be redisplayed the next time redisplay_internal is called. */
11750 void
11751 mark_window_display_accurate (window, accurate_p)
11752 Lisp_Object window;
11753 int accurate_p;
11755 struct window *w;
11757 for (; !NILP (window); window = w->next)
11759 w = XWINDOW (window);
11760 mark_window_display_accurate_1 (w, accurate_p);
11762 if (!NILP (w->vchild))
11763 mark_window_display_accurate (w->vchild, accurate_p);
11764 if (!NILP (w->hchild))
11765 mark_window_display_accurate (w->hchild, accurate_p);
11768 if (accurate_p)
11770 update_overlay_arrows (1);
11772 else
11774 /* Force a thorough redisplay the next time by setting
11775 last_arrow_position and last_arrow_string to t, which is
11776 unequal to any useful value of Voverlay_arrow_... */
11777 update_overlay_arrows (-1);
11782 /* Return value in display table DP (Lisp_Char_Table *) for character
11783 C. Since a display table doesn't have any parent, we don't have to
11784 follow parent. Do not call this function directly but use the
11785 macro DISP_CHAR_VECTOR. */
11787 Lisp_Object
11788 disp_char_vector (dp, c)
11789 struct Lisp_Char_Table *dp;
11790 int c;
11792 int code[4], i;
11793 Lisp_Object val;
11795 if (SINGLE_BYTE_CHAR_P (c))
11796 return (dp->contents[c]);
11798 SPLIT_CHAR (c, code[0], code[1], code[2]);
11799 if (code[1] < 32)
11800 code[1] = -1;
11801 else if (code[2] < 32)
11802 code[2] = -1;
11804 /* Here, the possible range of code[0] (== charset ID) is
11805 128..max_charset. Since the top level char table contains data
11806 for multibyte characters after 256th element, we must increment
11807 code[0] by 128 to get a correct index. */
11808 code[0] += 128;
11809 code[3] = -1; /* anchor */
11811 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11813 val = dp->contents[code[i]];
11814 if (!SUB_CHAR_TABLE_P (val))
11815 return (NILP (val) ? dp->defalt : val);
11818 /* Here, val is a sub char table. We return the default value of
11819 it. */
11820 return (dp->defalt);
11825 /***********************************************************************
11826 Window Redisplay
11827 ***********************************************************************/
11829 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11831 static void
11832 redisplay_windows (window)
11833 Lisp_Object window;
11835 while (!NILP (window))
11837 struct window *w = XWINDOW (window);
11839 if (!NILP (w->hchild))
11840 redisplay_windows (w->hchild);
11841 else if (!NILP (w->vchild))
11842 redisplay_windows (w->vchild);
11843 else
11845 displayed_buffer = XBUFFER (w->buffer);
11846 /* Use list_of_error, not Qerror, so that
11847 we catch only errors and don't run the debugger. */
11848 internal_condition_case_1 (redisplay_window_0, window,
11849 list_of_error,
11850 redisplay_window_error);
11853 window = w->next;
11857 static Lisp_Object
11858 redisplay_window_error ()
11860 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11861 return Qnil;
11864 static Lisp_Object
11865 redisplay_window_0 (window)
11866 Lisp_Object window;
11868 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11869 redisplay_window (window, 0);
11870 return Qnil;
11873 static Lisp_Object
11874 redisplay_window_1 (window)
11875 Lisp_Object window;
11877 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11878 redisplay_window (window, 1);
11879 return Qnil;
11883 /* Increment GLYPH until it reaches END or CONDITION fails while
11884 adding (GLYPH)->pixel_width to X. */
11886 #define SKIP_GLYPHS(glyph, end, x, condition) \
11887 do \
11889 (x) += (glyph)->pixel_width; \
11890 ++(glyph); \
11892 while ((glyph) < (end) && (condition))
11895 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11896 DELTA is the number of bytes by which positions recorded in ROW
11897 differ from current buffer positions.
11899 Return 0 if cursor is not on this row. 1 otherwise. */
11902 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11903 struct window *w;
11904 struct glyph_row *row;
11905 struct glyph_matrix *matrix;
11906 int delta, delta_bytes, dy, dvpos;
11908 struct glyph *glyph = row->glyphs[TEXT_AREA];
11909 struct glyph *end = glyph + row->used[TEXT_AREA];
11910 struct glyph *cursor = NULL;
11911 /* The first glyph that starts a sequence of glyphs from string. */
11912 struct glyph *string_start;
11913 /* The X coordinate of string_start. */
11914 int string_start_x;
11915 /* The last known character position. */
11916 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11917 /* The last known character position before string_start. */
11918 int string_before_pos;
11919 int x = row->x;
11920 int cursor_x = x;
11921 int cursor_from_overlay_pos = 0;
11922 int pt_old = PT - delta;
11924 /* Skip over glyphs not having an object at the start of the row.
11925 These are special glyphs like truncation marks on terminal
11926 frames. */
11927 if (row->displays_text_p)
11928 while (glyph < end
11929 && INTEGERP (glyph->object)
11930 && glyph->charpos < 0)
11932 x += glyph->pixel_width;
11933 ++glyph;
11936 string_start = NULL;
11937 while (glyph < end
11938 && !INTEGERP (glyph->object)
11939 && (!BUFFERP (glyph->object)
11940 || (last_pos = glyph->charpos) < pt_old))
11942 if (! STRINGP (glyph->object))
11944 string_start = NULL;
11945 x += glyph->pixel_width;
11946 ++glyph;
11947 if (cursor_from_overlay_pos
11948 && last_pos >= cursor_from_overlay_pos)
11950 cursor_from_overlay_pos = 0;
11951 cursor = 0;
11954 else
11956 if (string_start == NULL)
11958 string_before_pos = last_pos;
11959 string_start = glyph;
11960 string_start_x = x;
11962 /* Skip all glyphs from string. */
11965 Lisp_Object cprop;
11966 int pos;
11967 if ((cursor == NULL || glyph > cursor)
11968 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11969 Qcursor, (glyph)->object),
11970 !NILP (cprop))
11971 && (pos = string_buffer_position (w, glyph->object,
11972 string_before_pos),
11973 (pos == 0 /* From overlay */
11974 || pos == pt_old)))
11976 /* Estimate overlay buffer position from the buffer
11977 positions of the glyphs before and after the overlay.
11978 Add 1 to last_pos so that if point corresponds to the
11979 glyph right after the overlay, we still use a 'cursor'
11980 property found in that overlay. */
11981 cursor_from_overlay_pos = (pos ? 0 : last_pos
11982 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11983 cursor = glyph;
11984 cursor_x = x;
11986 x += glyph->pixel_width;
11987 ++glyph;
11989 while (glyph < end && EQ (glyph->object, string_start->object));
11993 if (cursor != NULL)
11995 glyph = cursor;
11996 x = cursor_x;
11998 else if (row->ends_in_ellipsis_p && glyph == end)
12000 /* Scan back over the ellipsis glyphs, decrementing positions. */
12001 while (glyph > row->glyphs[TEXT_AREA]
12002 && (glyph - 1)->charpos == last_pos)
12003 glyph--, x -= glyph->pixel_width;
12004 /* That loop always goes one position too far,
12005 including the glyph before the ellipsis.
12006 So scan forward over that one. */
12007 x += glyph->pixel_width;
12008 glyph++;
12010 else if (string_start
12011 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12013 /* We may have skipped over point because the previous glyphs
12014 are from string. As there's no easy way to know the
12015 character position of the current glyph, find the correct
12016 glyph on point by scanning from string_start again. */
12017 Lisp_Object limit;
12018 Lisp_Object string;
12019 struct glyph *stop = glyph;
12020 int pos;
12022 limit = make_number (pt_old + 1);
12023 glyph = string_start;
12024 x = string_start_x;
12025 string = glyph->object;
12026 pos = string_buffer_position (w, string, string_before_pos);
12027 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12028 because we always put cursor after overlay strings. */
12029 while (pos == 0 && glyph < stop)
12031 string = glyph->object;
12032 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12033 if (glyph < stop)
12034 pos = string_buffer_position (w, glyph->object, string_before_pos);
12037 while (glyph < stop)
12039 pos = XINT (Fnext_single_char_property_change
12040 (make_number (pos), Qdisplay, Qnil, limit));
12041 if (pos > pt_old)
12042 break;
12043 /* Skip glyphs from the same string. */
12044 string = glyph->object;
12045 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12046 /* Skip glyphs from an overlay. */
12047 while (glyph < stop
12048 && ! string_buffer_position (w, glyph->object, pos))
12050 string = glyph->object;
12051 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12055 /* If we reached the end of the line, and end was from a string,
12056 cursor is not on this line. */
12057 if (glyph == end && row->continued_p)
12058 return 0;
12061 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12062 w->cursor.x = x;
12063 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12064 w->cursor.y = row->y + dy;
12066 if (w == XWINDOW (selected_window))
12068 if (!row->continued_p
12069 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12070 && row->x == 0)
12072 this_line_buffer = XBUFFER (w->buffer);
12074 CHARPOS (this_line_start_pos)
12075 = MATRIX_ROW_START_CHARPOS (row) + delta;
12076 BYTEPOS (this_line_start_pos)
12077 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12079 CHARPOS (this_line_end_pos)
12080 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12081 BYTEPOS (this_line_end_pos)
12082 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12084 this_line_y = w->cursor.y;
12085 this_line_pixel_height = row->height;
12086 this_line_vpos = w->cursor.vpos;
12087 this_line_start_x = row->x;
12089 else
12090 CHARPOS (this_line_start_pos) = 0;
12093 return 1;
12097 /* Run window scroll functions, if any, for WINDOW with new window
12098 start STARTP. Sets the window start of WINDOW to that position.
12100 We assume that the window's buffer is really current. */
12102 static INLINE struct text_pos
12103 run_window_scroll_functions (window, startp)
12104 Lisp_Object window;
12105 struct text_pos startp;
12107 struct window *w = XWINDOW (window);
12108 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12110 if (current_buffer != XBUFFER (w->buffer))
12111 abort ();
12113 if (!NILP (Vwindow_scroll_functions))
12115 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12116 make_number (CHARPOS (startp)));
12117 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12118 /* In case the hook functions switch buffers. */
12119 if (current_buffer != XBUFFER (w->buffer))
12120 set_buffer_internal_1 (XBUFFER (w->buffer));
12123 return startp;
12127 /* Make sure the line containing the cursor is fully visible.
12128 A value of 1 means there is nothing to be done.
12129 (Either the line is fully visible, or it cannot be made so,
12130 or we cannot tell.)
12132 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12133 is higher than window.
12135 A value of 0 means the caller should do scrolling
12136 as if point had gone off the screen. */
12138 static int
12139 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12140 struct window *w;
12141 int force_p;
12142 int current_matrix_p;
12144 struct glyph_matrix *matrix;
12145 struct glyph_row *row;
12146 int window_height;
12148 if (!make_cursor_line_fully_visible_p)
12149 return 1;
12151 /* It's not always possible to find the cursor, e.g, when a window
12152 is full of overlay strings. Don't do anything in that case. */
12153 if (w->cursor.vpos < 0)
12154 return 1;
12156 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12157 row = MATRIX_ROW (matrix, w->cursor.vpos);
12159 /* If the cursor row is not partially visible, there's nothing to do. */
12160 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12161 return 1;
12163 /* If the row the cursor is in is taller than the window's height,
12164 it's not clear what to do, so do nothing. */
12165 window_height = window_box_height (w);
12166 if (row->height >= window_height)
12168 if (!force_p || MINI_WINDOW_P (w)
12169 || w->vscroll || w->cursor.vpos == 0)
12170 return 1;
12172 return 0;
12174 #if 0
12175 /* This code used to try to scroll the window just enough to make
12176 the line visible. It returned 0 to say that the caller should
12177 allocate larger glyph matrices. */
12179 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12181 int dy = row->height - row->visible_height;
12182 w->vscroll = 0;
12183 w->cursor.y += dy;
12184 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12186 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12188 int dy = - (row->height - row->visible_height);
12189 w->vscroll = dy;
12190 w->cursor.y += dy;
12191 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12194 /* When we change the cursor y-position of the selected window,
12195 change this_line_y as well so that the display optimization for
12196 the cursor line of the selected window in redisplay_internal uses
12197 the correct y-position. */
12198 if (w == XWINDOW (selected_window))
12199 this_line_y = w->cursor.y;
12201 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12202 redisplay with larger matrices. */
12203 if (matrix->nrows < required_matrix_height (w))
12205 fonts_changed_p = 1;
12206 return 0;
12209 return 1;
12210 #endif /* 0 */
12214 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12215 non-zero means only WINDOW is redisplayed in redisplay_internal.
12216 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12217 in redisplay_window to bring a partially visible line into view in
12218 the case that only the cursor has moved.
12220 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12221 last screen line's vertical height extends past the end of the screen.
12223 Value is
12225 1 if scrolling succeeded
12227 0 if scrolling didn't find point.
12229 -1 if new fonts have been loaded so that we must interrupt
12230 redisplay, adjust glyph matrices, and try again. */
12232 enum
12234 SCROLLING_SUCCESS,
12235 SCROLLING_FAILED,
12236 SCROLLING_NEED_LARGER_MATRICES
12239 static int
12240 try_scrolling (window, just_this_one_p, scroll_conservatively,
12241 scroll_step, temp_scroll_step, last_line_misfit)
12242 Lisp_Object window;
12243 int just_this_one_p;
12244 EMACS_INT scroll_conservatively, scroll_step;
12245 int temp_scroll_step;
12246 int last_line_misfit;
12248 struct window *w = XWINDOW (window);
12249 struct frame *f = XFRAME (w->frame);
12250 struct text_pos scroll_margin_pos;
12251 struct text_pos pos;
12252 struct text_pos startp;
12253 struct it it;
12254 Lisp_Object window_end;
12255 int this_scroll_margin;
12256 int dy = 0;
12257 int scroll_max;
12258 int rc;
12259 int amount_to_scroll = 0;
12260 Lisp_Object aggressive;
12261 int height;
12262 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12264 #if GLYPH_DEBUG
12265 debug_method_add (w, "try_scrolling");
12266 #endif
12268 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12270 /* Compute scroll margin height in pixels. We scroll when point is
12271 within this distance from the top or bottom of the window. */
12272 if (scroll_margin > 0)
12274 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12275 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12277 else
12278 this_scroll_margin = 0;
12280 /* Force scroll_conservatively to have a reasonable value so it doesn't
12281 cause an overflow while computing how much to scroll. */
12282 if (scroll_conservatively)
12283 scroll_conservatively = min (scroll_conservatively,
12284 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12286 /* Compute how much we should try to scroll maximally to bring point
12287 into view. */
12288 if (scroll_step || scroll_conservatively || temp_scroll_step)
12289 scroll_max = max (scroll_step,
12290 max (scroll_conservatively, temp_scroll_step));
12291 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12292 || NUMBERP (current_buffer->scroll_up_aggressively))
12293 /* We're trying to scroll because of aggressive scrolling
12294 but no scroll_step is set. Choose an arbitrary one. Maybe
12295 there should be a variable for this. */
12296 scroll_max = 10;
12297 else
12298 scroll_max = 0;
12299 scroll_max *= FRAME_LINE_HEIGHT (f);
12301 /* Decide whether we have to scroll down. Start at the window end
12302 and move this_scroll_margin up to find the position of the scroll
12303 margin. */
12304 window_end = Fwindow_end (window, Qt);
12306 too_near_end:
12308 CHARPOS (scroll_margin_pos) = XINT (window_end);
12309 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12311 if (this_scroll_margin || extra_scroll_margin_lines)
12313 start_display (&it, w, scroll_margin_pos);
12314 if (this_scroll_margin)
12315 move_it_vertically_backward (&it, this_scroll_margin);
12316 if (extra_scroll_margin_lines)
12317 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12318 scroll_margin_pos = it.current.pos;
12321 if (PT >= CHARPOS (scroll_margin_pos))
12323 int y0;
12325 /* Point is in the scroll margin at the bottom of the window, or
12326 below. Compute a new window start that makes point visible. */
12328 /* Compute the distance from the scroll margin to PT.
12329 Give up if the distance is greater than scroll_max. */
12330 start_display (&it, w, scroll_margin_pos);
12331 y0 = it.current_y;
12332 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12333 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12335 /* To make point visible, we have to move the window start
12336 down so that the line the cursor is in is visible, which
12337 means we have to add in the height of the cursor line. */
12338 dy = line_bottom_y (&it) - y0;
12340 if (dy > scroll_max)
12341 return SCROLLING_FAILED;
12343 /* Move the window start down. If scrolling conservatively,
12344 move it just enough down to make point visible. If
12345 scroll_step is set, move it down by scroll_step. */
12346 start_display (&it, w, startp);
12348 if (scroll_conservatively)
12349 /* Set AMOUNT_TO_SCROLL to at least one line,
12350 and at most scroll_conservatively lines. */
12351 amount_to_scroll
12352 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12353 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12354 else if (scroll_step || temp_scroll_step)
12355 amount_to_scroll = scroll_max;
12356 else
12358 aggressive = current_buffer->scroll_up_aggressively;
12359 height = WINDOW_BOX_TEXT_HEIGHT (w);
12360 if (NUMBERP (aggressive))
12362 double float_amount = XFLOATINT (aggressive) * height;
12363 amount_to_scroll = float_amount;
12364 if (amount_to_scroll == 0 && float_amount > 0)
12365 amount_to_scroll = 1;
12369 if (amount_to_scroll <= 0)
12370 return SCROLLING_FAILED;
12372 /* If moving by amount_to_scroll leaves STARTP unchanged,
12373 move it down one screen line. */
12375 move_it_vertically (&it, amount_to_scroll);
12376 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12377 move_it_by_lines (&it, 1, 1);
12378 startp = it.current.pos;
12380 else
12382 /* See if point is inside the scroll margin at the top of the
12383 window. */
12384 scroll_margin_pos = startp;
12385 if (this_scroll_margin)
12387 start_display (&it, w, startp);
12388 move_it_vertically (&it, this_scroll_margin);
12389 scroll_margin_pos = it.current.pos;
12392 if (PT < CHARPOS (scroll_margin_pos))
12394 /* Point is in the scroll margin at the top of the window or
12395 above what is displayed in the window. */
12396 int y0;
12398 /* Compute the vertical distance from PT to the scroll
12399 margin position. Give up if distance is greater than
12400 scroll_max. */
12401 SET_TEXT_POS (pos, PT, PT_BYTE);
12402 start_display (&it, w, pos);
12403 y0 = it.current_y;
12404 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12405 it.last_visible_y, -1,
12406 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12407 dy = it.current_y - y0;
12408 if (dy > scroll_max)
12409 return SCROLLING_FAILED;
12411 /* Compute new window start. */
12412 start_display (&it, w, startp);
12414 if (scroll_conservatively)
12415 amount_to_scroll
12416 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12417 else if (scroll_step || temp_scroll_step)
12418 amount_to_scroll = scroll_max;
12419 else
12421 aggressive = current_buffer->scroll_down_aggressively;
12422 height = WINDOW_BOX_TEXT_HEIGHT (w);
12423 if (NUMBERP (aggressive))
12425 double float_amount = XFLOATINT (aggressive) * height;
12426 amount_to_scroll = float_amount;
12427 if (amount_to_scroll == 0 && float_amount > 0)
12428 amount_to_scroll = 1;
12432 if (amount_to_scroll <= 0)
12433 return SCROLLING_FAILED;
12435 move_it_vertically_backward (&it, amount_to_scroll);
12436 startp = it.current.pos;
12440 /* Run window scroll functions. */
12441 startp = run_window_scroll_functions (window, startp);
12443 /* Display the window. Give up if new fonts are loaded, or if point
12444 doesn't appear. */
12445 if (!try_window (window, startp, 0))
12446 rc = SCROLLING_NEED_LARGER_MATRICES;
12447 else if (w->cursor.vpos < 0)
12449 clear_glyph_matrix (w->desired_matrix);
12450 rc = SCROLLING_FAILED;
12452 else
12454 /* Maybe forget recorded base line for line number display. */
12455 if (!just_this_one_p
12456 || current_buffer->clip_changed
12457 || BEG_UNCHANGED < CHARPOS (startp))
12458 w->base_line_number = Qnil;
12460 /* If cursor ends up on a partially visible line,
12461 treat that as being off the bottom of the screen. */
12462 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12464 clear_glyph_matrix (w->desired_matrix);
12465 ++extra_scroll_margin_lines;
12466 goto too_near_end;
12468 rc = SCROLLING_SUCCESS;
12471 return rc;
12475 /* Compute a suitable window start for window W if display of W starts
12476 on a continuation line. Value is non-zero if a new window start
12477 was computed.
12479 The new window start will be computed, based on W's width, starting
12480 from the start of the continued line. It is the start of the
12481 screen line with the minimum distance from the old start W->start. */
12483 static int
12484 compute_window_start_on_continuation_line (w)
12485 struct window *w;
12487 struct text_pos pos, start_pos;
12488 int window_start_changed_p = 0;
12490 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12492 /* If window start is on a continuation line... Window start may be
12493 < BEGV in case there's invisible text at the start of the
12494 buffer (M-x rmail, for example). */
12495 if (CHARPOS (start_pos) > BEGV
12496 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12498 struct it it;
12499 struct glyph_row *row;
12501 /* Handle the case that the window start is out of range. */
12502 if (CHARPOS (start_pos) < BEGV)
12503 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12504 else if (CHARPOS (start_pos) > ZV)
12505 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12507 /* Find the start of the continued line. This should be fast
12508 because scan_buffer is fast (newline cache). */
12509 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12510 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12511 row, DEFAULT_FACE_ID);
12512 reseat_at_previous_visible_line_start (&it);
12514 /* If the line start is "too far" away from the window start,
12515 say it takes too much time to compute a new window start. */
12516 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12517 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12519 int min_distance, distance;
12521 /* Move forward by display lines to find the new window
12522 start. If window width was enlarged, the new start can
12523 be expected to be > the old start. If window width was
12524 decreased, the new window start will be < the old start.
12525 So, we're looking for the display line start with the
12526 minimum distance from the old window start. */
12527 pos = it.current.pos;
12528 min_distance = INFINITY;
12529 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12530 distance < min_distance)
12532 min_distance = distance;
12533 pos = it.current.pos;
12534 move_it_by_lines (&it, 1, 0);
12537 /* Set the window start there. */
12538 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12539 window_start_changed_p = 1;
12543 return window_start_changed_p;
12547 /* Try cursor movement in case text has not changed in window WINDOW,
12548 with window start STARTP. Value is
12550 CURSOR_MOVEMENT_SUCCESS if successful
12552 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12554 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12555 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12556 we want to scroll as if scroll-step were set to 1. See the code.
12558 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12559 which case we have to abort this redisplay, and adjust matrices
12560 first. */
12562 enum
12564 CURSOR_MOVEMENT_SUCCESS,
12565 CURSOR_MOVEMENT_CANNOT_BE_USED,
12566 CURSOR_MOVEMENT_MUST_SCROLL,
12567 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12570 static int
12571 try_cursor_movement (window, startp, scroll_step)
12572 Lisp_Object window;
12573 struct text_pos startp;
12574 int *scroll_step;
12576 struct window *w = XWINDOW (window);
12577 struct frame *f = XFRAME (w->frame);
12578 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12580 #if GLYPH_DEBUG
12581 if (inhibit_try_cursor_movement)
12582 return rc;
12583 #endif
12585 /* Handle case where text has not changed, only point, and it has
12586 not moved off the frame. */
12587 if (/* Point may be in this window. */
12588 PT >= CHARPOS (startp)
12589 /* Selective display hasn't changed. */
12590 && !current_buffer->clip_changed
12591 /* Function force-mode-line-update is used to force a thorough
12592 redisplay. It sets either windows_or_buffers_changed or
12593 update_mode_lines. So don't take a shortcut here for these
12594 cases. */
12595 && !update_mode_lines
12596 && !windows_or_buffers_changed
12597 && !cursor_type_changed
12598 /* Can't use this case if highlighting a region. When a
12599 region exists, cursor movement has to do more than just
12600 set the cursor. */
12601 && !(!NILP (Vtransient_mark_mode)
12602 && !NILP (current_buffer->mark_active))
12603 && NILP (w->region_showing)
12604 && NILP (Vshow_trailing_whitespace)
12605 /* Right after splitting windows, last_point may be nil. */
12606 && INTEGERP (w->last_point)
12607 /* This code is not used for mini-buffer for the sake of the case
12608 of redisplaying to replace an echo area message; since in
12609 that case the mini-buffer contents per se are usually
12610 unchanged. This code is of no real use in the mini-buffer
12611 since the handling of this_line_start_pos, etc., in redisplay
12612 handles the same cases. */
12613 && !EQ (window, minibuf_window)
12614 /* When splitting windows or for new windows, it happens that
12615 redisplay is called with a nil window_end_vpos or one being
12616 larger than the window. This should really be fixed in
12617 window.c. I don't have this on my list, now, so we do
12618 approximately the same as the old redisplay code. --gerd. */
12619 && INTEGERP (w->window_end_vpos)
12620 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12621 && (FRAME_WINDOW_P (f)
12622 || !overlay_arrow_in_current_buffer_p ()))
12624 int this_scroll_margin, top_scroll_margin;
12625 struct glyph_row *row = NULL;
12627 #if GLYPH_DEBUG
12628 debug_method_add (w, "cursor movement");
12629 #endif
12631 /* Scroll if point within this distance from the top or bottom
12632 of the window. This is a pixel value. */
12633 this_scroll_margin = max (0, scroll_margin);
12634 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12635 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12637 top_scroll_margin = this_scroll_margin;
12638 if (WINDOW_WANTS_HEADER_LINE_P (w))
12639 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12641 /* Start with the row the cursor was displayed during the last
12642 not paused redisplay. Give up if that row is not valid. */
12643 if (w->last_cursor.vpos < 0
12644 || w->last_cursor.vpos >= w->current_matrix->nrows)
12645 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12646 else
12648 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12649 if (row->mode_line_p)
12650 ++row;
12651 if (!row->enabled_p)
12652 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12655 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12657 int scroll_p = 0;
12658 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12660 if (PT > XFASTINT (w->last_point))
12662 /* Point has moved forward. */
12663 while (MATRIX_ROW_END_CHARPOS (row) < PT
12664 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12666 xassert (row->enabled_p);
12667 ++row;
12670 /* The end position of a row equals the start position
12671 of the next row. If PT is there, we would rather
12672 display it in the next line. */
12673 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12674 && MATRIX_ROW_END_CHARPOS (row) == PT
12675 && !cursor_row_p (w, row))
12676 ++row;
12678 /* If within the scroll margin, scroll. Note that
12679 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12680 the next line would be drawn, and that
12681 this_scroll_margin can be zero. */
12682 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12683 || PT > MATRIX_ROW_END_CHARPOS (row)
12684 /* Line is completely visible last line in window
12685 and PT is to be set in the next line. */
12686 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12687 && PT == MATRIX_ROW_END_CHARPOS (row)
12688 && !row->ends_at_zv_p
12689 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12690 scroll_p = 1;
12692 else if (PT < XFASTINT (w->last_point))
12694 /* Cursor has to be moved backward. Note that PT >=
12695 CHARPOS (startp) because of the outer if-statement. */
12696 while (!row->mode_line_p
12697 && (MATRIX_ROW_START_CHARPOS (row) > PT
12698 || (MATRIX_ROW_START_CHARPOS (row) == PT
12699 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12700 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12701 row > w->current_matrix->rows
12702 && (row-1)->ends_in_newline_from_string_p))))
12703 && (row->y > top_scroll_margin
12704 || CHARPOS (startp) == BEGV))
12706 xassert (row->enabled_p);
12707 --row;
12710 /* Consider the following case: Window starts at BEGV,
12711 there is invisible, intangible text at BEGV, so that
12712 display starts at some point START > BEGV. It can
12713 happen that we are called with PT somewhere between
12714 BEGV and START. Try to handle that case. */
12715 if (row < w->current_matrix->rows
12716 || row->mode_line_p)
12718 row = w->current_matrix->rows;
12719 if (row->mode_line_p)
12720 ++row;
12723 /* Due to newlines in overlay strings, we may have to
12724 skip forward over overlay strings. */
12725 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12726 && MATRIX_ROW_END_CHARPOS (row) == PT
12727 && !cursor_row_p (w, row))
12728 ++row;
12730 /* If within the scroll margin, scroll. */
12731 if (row->y < top_scroll_margin
12732 && CHARPOS (startp) != BEGV)
12733 scroll_p = 1;
12735 else
12737 /* Cursor did not move. So don't scroll even if cursor line
12738 is partially visible, as it was so before. */
12739 rc = CURSOR_MOVEMENT_SUCCESS;
12742 if (PT < MATRIX_ROW_START_CHARPOS (row)
12743 || PT > MATRIX_ROW_END_CHARPOS (row))
12745 /* if PT is not in the glyph row, give up. */
12746 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12748 else if (rc != CURSOR_MOVEMENT_SUCCESS
12749 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12750 && make_cursor_line_fully_visible_p)
12752 if (PT == MATRIX_ROW_END_CHARPOS (row)
12753 && !row->ends_at_zv_p
12754 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12755 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12756 else if (row->height > window_box_height (w))
12758 /* If we end up in a partially visible line, let's
12759 make it fully visible, except when it's taller
12760 than the window, in which case we can't do much
12761 about it. */
12762 *scroll_step = 1;
12763 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12765 else
12767 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12768 if (!cursor_row_fully_visible_p (w, 0, 1))
12769 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12770 else
12771 rc = CURSOR_MOVEMENT_SUCCESS;
12774 else if (scroll_p)
12775 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12776 else
12780 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12782 rc = CURSOR_MOVEMENT_SUCCESS;
12783 break;
12785 ++row;
12787 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12788 && MATRIX_ROW_START_CHARPOS (row) == PT
12789 && cursor_row_p (w, row));
12794 return rc;
12797 void
12798 set_vertical_scroll_bar (w)
12799 struct window *w;
12801 int start, end, whole;
12803 /* Calculate the start and end positions for the current window.
12804 At some point, it would be nice to choose between scrollbars
12805 which reflect the whole buffer size, with special markers
12806 indicating narrowing, and scrollbars which reflect only the
12807 visible region.
12809 Note that mini-buffers sometimes aren't displaying any text. */
12810 if (!MINI_WINDOW_P (w)
12811 || (w == XWINDOW (minibuf_window)
12812 && NILP (echo_area_buffer[0])))
12814 struct buffer *buf = XBUFFER (w->buffer);
12815 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12816 start = marker_position (w->start) - BUF_BEGV (buf);
12817 /* I don't think this is guaranteed to be right. For the
12818 moment, we'll pretend it is. */
12819 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12821 if (end < start)
12822 end = start;
12823 if (whole < (end - start))
12824 whole = end - start;
12826 else
12827 start = end = whole = 0;
12829 /* Indicate what this scroll bar ought to be displaying now. */
12830 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12834 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12835 selected_window is redisplayed.
12837 We can return without actually redisplaying the window if
12838 fonts_changed_p is nonzero. In that case, redisplay_internal will
12839 retry. */
12841 static void
12842 redisplay_window (window, just_this_one_p)
12843 Lisp_Object window;
12844 int just_this_one_p;
12846 struct window *w = XWINDOW (window);
12847 struct frame *f = XFRAME (w->frame);
12848 struct buffer *buffer = XBUFFER (w->buffer);
12849 struct buffer *old = current_buffer;
12850 struct text_pos lpoint, opoint, startp;
12851 int update_mode_line;
12852 int tem;
12853 struct it it;
12854 /* Record it now because it's overwritten. */
12855 int current_matrix_up_to_date_p = 0;
12856 int used_current_matrix_p = 0;
12857 /* This is less strict than current_matrix_up_to_date_p.
12858 It indictes that the buffer contents and narrowing are unchanged. */
12859 int buffer_unchanged_p = 0;
12860 int temp_scroll_step = 0;
12861 int count = SPECPDL_INDEX ();
12862 int rc;
12863 int centering_position = -1;
12864 int last_line_misfit = 0;
12865 int save_beg_unchanged, save_end_unchanged;
12867 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12868 opoint = lpoint;
12870 /* W must be a leaf window here. */
12871 xassert (!NILP (w->buffer));
12872 #if GLYPH_DEBUG
12873 *w->desired_matrix->method = 0;
12874 #endif
12876 specbind (Qinhibit_point_motion_hooks, Qt);
12878 reconsider_clip_changes (w, buffer);
12880 /* Has the mode line to be updated? */
12881 update_mode_line = (!NILP (w->update_mode_line)
12882 || update_mode_lines
12883 || buffer->clip_changed
12884 || buffer->prevent_redisplay_optimizations_p);
12886 if (MINI_WINDOW_P (w))
12888 if (w == XWINDOW (echo_area_window)
12889 && !NILP (echo_area_buffer[0]))
12891 if (update_mode_line)
12892 /* We may have to update a tty frame's menu bar or a
12893 tool-bar. Example `M-x C-h C-h C-g'. */
12894 goto finish_menu_bars;
12895 else
12896 /* We've already displayed the echo area glyphs in this window. */
12897 goto finish_scroll_bars;
12899 else if ((w != XWINDOW (minibuf_window)
12900 || minibuf_level == 0)
12901 /* When buffer is nonempty, redisplay window normally. */
12902 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12903 /* Quail displays non-mini buffers in minibuffer window.
12904 In that case, redisplay the window normally. */
12905 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12907 /* W is a mini-buffer window, but it's not active, so clear
12908 it. */
12909 int yb = window_text_bottom_y (w);
12910 struct glyph_row *row;
12911 int y;
12913 for (y = 0, row = w->desired_matrix->rows;
12914 y < yb;
12915 y += row->height, ++row)
12916 blank_row (w, row, y);
12917 goto finish_scroll_bars;
12920 clear_glyph_matrix (w->desired_matrix);
12923 /* Otherwise set up data on this window; select its buffer and point
12924 value. */
12925 /* Really select the buffer, for the sake of buffer-local
12926 variables. */
12927 set_buffer_internal_1 (XBUFFER (w->buffer));
12928 SET_TEXT_POS (opoint, PT, PT_BYTE);
12930 save_beg_unchanged = BEG_UNCHANGED;
12931 save_end_unchanged = END_UNCHANGED;
12933 current_matrix_up_to_date_p
12934 = (!NILP (w->window_end_valid)
12935 && !current_buffer->clip_changed
12936 && !current_buffer->prevent_redisplay_optimizations_p
12937 && XFASTINT (w->last_modified) >= MODIFF
12938 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12940 buffer_unchanged_p
12941 = (!NILP (w->window_end_valid)
12942 && !current_buffer->clip_changed
12943 && XFASTINT (w->last_modified) >= MODIFF
12944 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12946 /* When windows_or_buffers_changed is non-zero, we can't rely on
12947 the window end being valid, so set it to nil there. */
12948 if (windows_or_buffers_changed)
12950 /* If window starts on a continuation line, maybe adjust the
12951 window start in case the window's width changed. */
12952 if (XMARKER (w->start)->buffer == current_buffer)
12953 compute_window_start_on_continuation_line (w);
12955 w->window_end_valid = Qnil;
12958 /* Some sanity checks. */
12959 CHECK_WINDOW_END (w);
12960 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12961 abort ();
12962 if (BYTEPOS (opoint) < CHARPOS (opoint))
12963 abort ();
12965 /* If %c is in mode line, update it if needed. */
12966 if (!NILP (w->column_number_displayed)
12967 /* This alternative quickly identifies a common case
12968 where no change is needed. */
12969 && !(PT == XFASTINT (w->last_point)
12970 && XFASTINT (w->last_modified) >= MODIFF
12971 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12972 && (XFASTINT (w->column_number_displayed)
12973 != (int) current_column ())) /* iftc */
12974 update_mode_line = 1;
12976 /* Count number of windows showing the selected buffer. An indirect
12977 buffer counts as its base buffer. */
12978 if (!just_this_one_p)
12980 struct buffer *current_base, *window_base;
12981 current_base = current_buffer;
12982 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12983 if (current_base->base_buffer)
12984 current_base = current_base->base_buffer;
12985 if (window_base->base_buffer)
12986 window_base = window_base->base_buffer;
12987 if (current_base == window_base)
12988 buffer_shared++;
12991 /* Point refers normally to the selected window. For any other
12992 window, set up appropriate value. */
12993 if (!EQ (window, selected_window))
12995 int new_pt = XMARKER (w->pointm)->charpos;
12996 int new_pt_byte = marker_byte_position (w->pointm);
12997 if (new_pt < BEGV)
12999 new_pt = BEGV;
13000 new_pt_byte = BEGV_BYTE;
13001 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13003 else if (new_pt > (ZV - 1))
13005 new_pt = ZV;
13006 new_pt_byte = ZV_BYTE;
13007 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13010 /* We don't use SET_PT so that the point-motion hooks don't run. */
13011 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13014 /* If any of the character widths specified in the display table
13015 have changed, invalidate the width run cache. It's true that
13016 this may be a bit late to catch such changes, but the rest of
13017 redisplay goes (non-fatally) haywire when the display table is
13018 changed, so why should we worry about doing any better? */
13019 if (current_buffer->width_run_cache)
13021 struct Lisp_Char_Table *disptab = buffer_display_table ();
13023 if (! disptab_matches_widthtab (disptab,
13024 XVECTOR (current_buffer->width_table)))
13026 invalidate_region_cache (current_buffer,
13027 current_buffer->width_run_cache,
13028 BEG, Z);
13029 recompute_width_table (current_buffer, disptab);
13033 /* If window-start is screwed up, choose a new one. */
13034 if (XMARKER (w->start)->buffer != current_buffer)
13035 goto recenter;
13037 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13039 /* If someone specified a new starting point but did not insist,
13040 check whether it can be used. */
13041 if (!NILP (w->optional_new_start)
13042 && CHARPOS (startp) >= BEGV
13043 && CHARPOS (startp) <= ZV)
13045 w->optional_new_start = Qnil;
13046 start_display (&it, w, startp);
13047 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13048 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13049 if (IT_CHARPOS (it) == PT)
13050 w->force_start = Qt;
13051 /* IT may overshoot PT if text at PT is invisible. */
13052 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13053 w->force_start = Qt;
13056 force_start:
13058 /* Handle case where place to start displaying has been specified,
13059 unless the specified location is outside the accessible range. */
13060 if (!NILP (w->force_start)
13061 || w->frozen_window_start_p)
13063 /* We set this later on if we have to adjust point. */
13064 int new_vpos = -1;
13065 int val;
13067 w->force_start = Qnil;
13068 w->vscroll = 0;
13069 w->window_end_valid = Qnil;
13071 /* Forget any recorded base line for line number display. */
13072 if (!buffer_unchanged_p)
13073 w->base_line_number = Qnil;
13075 /* Redisplay the mode line. Select the buffer properly for that.
13076 Also, run the hook window-scroll-functions
13077 because we have scrolled. */
13078 /* Note, we do this after clearing force_start because
13079 if there's an error, it is better to forget about force_start
13080 than to get into an infinite loop calling the hook functions
13081 and having them get more errors. */
13082 if (!update_mode_line
13083 || ! NILP (Vwindow_scroll_functions))
13085 update_mode_line = 1;
13086 w->update_mode_line = Qt;
13087 startp = run_window_scroll_functions (window, startp);
13090 w->last_modified = make_number (0);
13091 w->last_overlay_modified = make_number (0);
13092 if (CHARPOS (startp) < BEGV)
13093 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13094 else if (CHARPOS (startp) > ZV)
13095 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13097 /* Redisplay, then check if cursor has been set during the
13098 redisplay. Give up if new fonts were loaded. */
13099 val = try_window (window, startp, 1);
13100 if (!val)
13102 w->force_start = Qt;
13103 clear_glyph_matrix (w->desired_matrix);
13104 goto need_larger_matrices;
13106 /* Point was outside the scroll margins. */
13107 if (val < 0)
13108 new_vpos = window_box_height (w) / 2;
13110 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13112 /* If point does not appear, try to move point so it does
13113 appear. The desired matrix has been built above, so we
13114 can use it here. */
13115 new_vpos = window_box_height (w) / 2;
13118 if (!cursor_row_fully_visible_p (w, 0, 0))
13120 /* Point does appear, but on a line partly visible at end of window.
13121 Move it back to a fully-visible line. */
13122 new_vpos = window_box_height (w);
13125 /* If we need to move point for either of the above reasons,
13126 now actually do it. */
13127 if (new_vpos >= 0)
13129 struct glyph_row *row;
13131 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13132 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13133 ++row;
13135 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13136 MATRIX_ROW_START_BYTEPOS (row));
13138 if (w != XWINDOW (selected_window))
13139 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13140 else if (current_buffer == old)
13141 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13143 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13145 /* If we are highlighting the region, then we just changed
13146 the region, so redisplay to show it. */
13147 if (!NILP (Vtransient_mark_mode)
13148 && !NILP (current_buffer->mark_active))
13150 clear_glyph_matrix (w->desired_matrix);
13151 if (!try_window (window, startp, 0))
13152 goto need_larger_matrices;
13156 #if GLYPH_DEBUG
13157 debug_method_add (w, "forced window start");
13158 #endif
13159 goto done;
13162 /* Handle case where text has not changed, only point, and it has
13163 not moved off the frame, and we are not retrying after hscroll.
13164 (current_matrix_up_to_date_p is nonzero when retrying.) */
13165 if (current_matrix_up_to_date_p
13166 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13167 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13169 switch (rc)
13171 case CURSOR_MOVEMENT_SUCCESS:
13172 used_current_matrix_p = 1;
13173 goto done;
13175 #if 0 /* try_cursor_movement never returns this value. */
13176 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13177 goto need_larger_matrices;
13178 #endif
13180 case CURSOR_MOVEMENT_MUST_SCROLL:
13181 goto try_to_scroll;
13183 default:
13184 abort ();
13187 /* If current starting point was originally the beginning of a line
13188 but no longer is, find a new starting point. */
13189 else if (!NILP (w->start_at_line_beg)
13190 && !(CHARPOS (startp) <= BEGV
13191 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13193 #if GLYPH_DEBUG
13194 debug_method_add (w, "recenter 1");
13195 #endif
13196 goto recenter;
13199 /* Try scrolling with try_window_id. Value is > 0 if update has
13200 been done, it is -1 if we know that the same window start will
13201 not work. It is 0 if unsuccessful for some other reason. */
13202 else if ((tem = try_window_id (w)) != 0)
13204 #if GLYPH_DEBUG
13205 debug_method_add (w, "try_window_id %d", tem);
13206 #endif
13208 if (fonts_changed_p)
13209 goto need_larger_matrices;
13210 if (tem > 0)
13211 goto done;
13213 /* Otherwise try_window_id has returned -1 which means that we
13214 don't want the alternative below this comment to execute. */
13216 else if (CHARPOS (startp) >= BEGV
13217 && CHARPOS (startp) <= ZV
13218 && PT >= CHARPOS (startp)
13219 && (CHARPOS (startp) < ZV
13220 /* Avoid starting at end of buffer. */
13221 || CHARPOS (startp) == BEGV
13222 || (XFASTINT (w->last_modified) >= MODIFF
13223 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13226 /* If first window line is a continuation line, and window start
13227 is inside the modified region, but the first change is before
13228 current window start, we must select a new window start.
13230 However, if this is the result of a down-mouse event (e.g. by
13231 extending the mouse-drag-overlay), we don't want to select a
13232 new window start, since that would change the position under
13233 the mouse, resulting in an unwanted mouse-movement rather
13234 than a simple mouse-click. */
13235 if (NILP (w->start_at_line_beg)
13236 && NILP (do_mouse_tracking)
13237 && CHARPOS (startp) > BEGV
13238 && CHARPOS (startp) > BEG + save_beg_unchanged
13239 && CHARPOS (startp) <= Z - save_end_unchanged)
13241 w->force_start = Qt;
13242 if (XMARKER (w->start)->buffer == current_buffer)
13243 compute_window_start_on_continuation_line (w);
13244 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13245 goto force_start;
13248 #if GLYPH_DEBUG
13249 debug_method_add (w, "same window start");
13250 #endif
13252 /* Try to redisplay starting at same place as before.
13253 If point has not moved off frame, accept the results. */
13254 if (!current_matrix_up_to_date_p
13255 /* Don't use try_window_reusing_current_matrix in this case
13256 because a window scroll function can have changed the
13257 buffer. */
13258 || !NILP (Vwindow_scroll_functions)
13259 || MINI_WINDOW_P (w)
13260 || !(used_current_matrix_p
13261 = try_window_reusing_current_matrix (w)))
13263 IF_DEBUG (debug_method_add (w, "1"));
13264 if (try_window (window, startp, 1) < 0)
13265 /* -1 means we need to scroll.
13266 0 means we need new matrices, but fonts_changed_p
13267 is set in that case, so we will detect it below. */
13268 goto try_to_scroll;
13271 if (fonts_changed_p)
13272 goto need_larger_matrices;
13274 if (w->cursor.vpos >= 0)
13276 if (!just_this_one_p
13277 || current_buffer->clip_changed
13278 || BEG_UNCHANGED < CHARPOS (startp))
13279 /* Forget any recorded base line for line number display. */
13280 w->base_line_number = Qnil;
13282 if (!cursor_row_fully_visible_p (w, 1, 0))
13284 clear_glyph_matrix (w->desired_matrix);
13285 last_line_misfit = 1;
13287 /* Drop through and scroll. */
13288 else
13289 goto done;
13291 else
13292 clear_glyph_matrix (w->desired_matrix);
13295 try_to_scroll:
13297 w->last_modified = make_number (0);
13298 w->last_overlay_modified = make_number (0);
13300 /* Redisplay the mode line. Select the buffer properly for that. */
13301 if (!update_mode_line)
13303 update_mode_line = 1;
13304 w->update_mode_line = Qt;
13307 /* Try to scroll by specified few lines. */
13308 if ((scroll_conservatively
13309 || scroll_step
13310 || temp_scroll_step
13311 || NUMBERP (current_buffer->scroll_up_aggressively)
13312 || NUMBERP (current_buffer->scroll_down_aggressively))
13313 && !current_buffer->clip_changed
13314 && CHARPOS (startp) >= BEGV
13315 && CHARPOS (startp) <= ZV)
13317 /* The function returns -1 if new fonts were loaded, 1 if
13318 successful, 0 if not successful. */
13319 int rc = try_scrolling (window, just_this_one_p,
13320 scroll_conservatively,
13321 scroll_step,
13322 temp_scroll_step, last_line_misfit);
13323 switch (rc)
13325 case SCROLLING_SUCCESS:
13326 goto done;
13328 case SCROLLING_NEED_LARGER_MATRICES:
13329 goto need_larger_matrices;
13331 case SCROLLING_FAILED:
13332 break;
13334 default:
13335 abort ();
13339 /* Finally, just choose place to start which centers point */
13341 recenter:
13342 if (centering_position < 0)
13343 centering_position = window_box_height (w) / 2;
13345 #if GLYPH_DEBUG
13346 debug_method_add (w, "recenter");
13347 #endif
13349 /* w->vscroll = 0; */
13351 /* Forget any previously recorded base line for line number display. */
13352 if (!buffer_unchanged_p)
13353 w->base_line_number = Qnil;
13355 /* Move backward half the height of the window. */
13356 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13357 it.current_y = it.last_visible_y;
13358 move_it_vertically_backward (&it, centering_position);
13359 xassert (IT_CHARPOS (it) >= BEGV);
13361 /* The function move_it_vertically_backward may move over more
13362 than the specified y-distance. If it->w is small, e.g. a
13363 mini-buffer window, we may end up in front of the window's
13364 display area. Start displaying at the start of the line
13365 containing PT in this case. */
13366 if (it.current_y <= 0)
13368 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13369 move_it_vertically_backward (&it, 0);
13370 #if 0
13371 /* I think this assert is bogus if buffer contains
13372 invisible text or images. KFS. */
13373 xassert (IT_CHARPOS (it) <= PT);
13374 #endif
13375 it.current_y = 0;
13378 it.current_x = it.hpos = 0;
13380 /* Set startp here explicitly in case that helps avoid an infinite loop
13381 in case the window-scroll-functions functions get errors. */
13382 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13384 /* Run scroll hooks. */
13385 startp = run_window_scroll_functions (window, it.current.pos);
13387 /* Redisplay the window. */
13388 if (!current_matrix_up_to_date_p
13389 || windows_or_buffers_changed
13390 || cursor_type_changed
13391 /* Don't use try_window_reusing_current_matrix in this case
13392 because it can have changed the buffer. */
13393 || !NILP (Vwindow_scroll_functions)
13394 || !just_this_one_p
13395 || MINI_WINDOW_P (w)
13396 || !(used_current_matrix_p
13397 = try_window_reusing_current_matrix (w)))
13398 try_window (window, startp, 0);
13400 /* If new fonts have been loaded (due to fontsets), give up. We
13401 have to start a new redisplay since we need to re-adjust glyph
13402 matrices. */
13403 if (fonts_changed_p)
13404 goto need_larger_matrices;
13406 /* If cursor did not appear assume that the middle of the window is
13407 in the first line of the window. Do it again with the next line.
13408 (Imagine a window of height 100, displaying two lines of height
13409 60. Moving back 50 from it->last_visible_y will end in the first
13410 line.) */
13411 if (w->cursor.vpos < 0)
13413 if (!NILP (w->window_end_valid)
13414 && PT >= Z - XFASTINT (w->window_end_pos))
13416 clear_glyph_matrix (w->desired_matrix);
13417 move_it_by_lines (&it, 1, 0);
13418 try_window (window, it.current.pos, 0);
13420 else if (PT < IT_CHARPOS (it))
13422 clear_glyph_matrix (w->desired_matrix);
13423 move_it_by_lines (&it, -1, 0);
13424 try_window (window, it.current.pos, 0);
13426 else
13428 /* Not much we can do about it. */
13432 /* Consider the following case: Window starts at BEGV, there is
13433 invisible, intangible text at BEGV, so that display starts at
13434 some point START > BEGV. It can happen that we are called with
13435 PT somewhere between BEGV and START. Try to handle that case. */
13436 if (w->cursor.vpos < 0)
13438 struct glyph_row *row = w->current_matrix->rows;
13439 if (row->mode_line_p)
13440 ++row;
13441 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13444 if (!cursor_row_fully_visible_p (w, 0, 0))
13446 /* If vscroll is enabled, disable it and try again. */
13447 if (w->vscroll)
13449 w->vscroll = 0;
13450 clear_glyph_matrix (w->desired_matrix);
13451 goto recenter;
13454 /* If centering point failed to make the whole line visible,
13455 put point at the top instead. That has to make the whole line
13456 visible, if it can be done. */
13457 if (centering_position == 0)
13458 goto done;
13460 clear_glyph_matrix (w->desired_matrix);
13461 centering_position = 0;
13462 goto recenter;
13465 done:
13467 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13468 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13469 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13470 ? Qt : Qnil);
13472 /* Display the mode line, if we must. */
13473 if ((update_mode_line
13474 /* If window not full width, must redo its mode line
13475 if (a) the window to its side is being redone and
13476 (b) we do a frame-based redisplay. This is a consequence
13477 of how inverted lines are drawn in frame-based redisplay. */
13478 || (!just_this_one_p
13479 && !FRAME_WINDOW_P (f)
13480 && !WINDOW_FULL_WIDTH_P (w))
13481 /* Line number to display. */
13482 || INTEGERP (w->base_line_pos)
13483 /* Column number is displayed and different from the one displayed. */
13484 || (!NILP (w->column_number_displayed)
13485 && (XFASTINT (w->column_number_displayed)
13486 != (int) current_column ()))) /* iftc */
13487 /* This means that the window has a mode line. */
13488 && (WINDOW_WANTS_MODELINE_P (w)
13489 || WINDOW_WANTS_HEADER_LINE_P (w)))
13491 display_mode_lines (w);
13493 /* If mode line height has changed, arrange for a thorough
13494 immediate redisplay using the correct mode line height. */
13495 if (WINDOW_WANTS_MODELINE_P (w)
13496 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13498 fonts_changed_p = 1;
13499 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13500 = DESIRED_MODE_LINE_HEIGHT (w);
13503 /* If top line height has changed, arrange for a thorough
13504 immediate redisplay using the correct mode line height. */
13505 if (WINDOW_WANTS_HEADER_LINE_P (w)
13506 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13508 fonts_changed_p = 1;
13509 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13510 = DESIRED_HEADER_LINE_HEIGHT (w);
13513 if (fonts_changed_p)
13514 goto need_larger_matrices;
13517 if (!line_number_displayed
13518 && !BUFFERP (w->base_line_pos))
13520 w->base_line_pos = Qnil;
13521 w->base_line_number = Qnil;
13524 finish_menu_bars:
13526 /* When we reach a frame's selected window, redo the frame's menu bar. */
13527 if (update_mode_line
13528 && EQ (FRAME_SELECTED_WINDOW (f), window))
13530 int redisplay_menu_p = 0;
13531 int redisplay_tool_bar_p = 0;
13533 if (FRAME_WINDOW_P (f))
13535 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13536 || defined (USE_GTK)
13537 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13538 #else
13539 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13540 #endif
13542 else
13543 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13545 if (redisplay_menu_p)
13546 display_menu_bar (w);
13548 #ifdef HAVE_WINDOW_SYSTEM
13549 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13550 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13551 #else
13552 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13553 && (FRAME_TOOL_BAR_LINES (f) > 0
13554 || !NILP (Vauto_resize_tool_bars));
13556 #endif
13558 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13560 extern int ignore_mouse_drag_p;
13561 ignore_mouse_drag_p = 1;
13563 #endif
13566 #ifdef HAVE_WINDOW_SYSTEM
13567 if (FRAME_WINDOW_P (f)
13568 && update_window_fringes (w, (just_this_one_p
13569 || (!used_current_matrix_p && !overlay_arrow_seen)
13570 || w->pseudo_window_p)))
13572 update_begin (f);
13573 BLOCK_INPUT;
13574 if (draw_window_fringes (w, 1))
13575 x_draw_vertical_border (w);
13576 UNBLOCK_INPUT;
13577 update_end (f);
13579 #endif /* HAVE_WINDOW_SYSTEM */
13581 /* We go to this label, with fonts_changed_p nonzero,
13582 if it is necessary to try again using larger glyph matrices.
13583 We have to redeem the scroll bar even in this case,
13584 because the loop in redisplay_internal expects that. */
13585 need_larger_matrices:
13587 finish_scroll_bars:
13589 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13591 /* Set the thumb's position and size. */
13592 set_vertical_scroll_bar (w);
13594 /* Note that we actually used the scroll bar attached to this
13595 window, so it shouldn't be deleted at the end of redisplay. */
13596 redeem_scroll_bar_hook (w);
13599 /* Restore current_buffer and value of point in it. */
13600 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13601 set_buffer_internal_1 (old);
13602 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13603 shorter. This can be caused by log truncation in *Messages*. */
13604 if (CHARPOS (lpoint) <= ZV)
13605 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13607 unbind_to (count, Qnil);
13611 /* Build the complete desired matrix of WINDOW with a window start
13612 buffer position POS.
13614 Value is 1 if successful. It is zero if fonts were loaded during
13615 redisplay which makes re-adjusting glyph matrices necessary, and -1
13616 if point would appear in the scroll margins.
13617 (We check that only if CHECK_MARGINS is nonzero. */
13620 try_window (window, pos, check_margins)
13621 Lisp_Object window;
13622 struct text_pos pos;
13623 int check_margins;
13625 struct window *w = XWINDOW (window);
13626 struct it it;
13627 struct glyph_row *last_text_row = NULL;
13628 struct frame *f = XFRAME (w->frame);
13630 /* Make POS the new window start. */
13631 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13633 /* Mark cursor position as unknown. No overlay arrow seen. */
13634 w->cursor.vpos = -1;
13635 overlay_arrow_seen = 0;
13637 /* Initialize iterator and info to start at POS. */
13638 start_display (&it, w, pos);
13640 /* Display all lines of W. */
13641 while (it.current_y < it.last_visible_y)
13643 if (display_line (&it))
13644 last_text_row = it.glyph_row - 1;
13645 if (fonts_changed_p)
13646 return 0;
13649 /* Don't let the cursor end in the scroll margins. */
13650 if (check_margins
13651 && !MINI_WINDOW_P (w))
13653 int this_scroll_margin;
13655 this_scroll_margin = max (0, scroll_margin);
13656 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13657 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13659 if ((w->cursor.y >= 0 /* not vscrolled */
13660 && w->cursor.y < this_scroll_margin
13661 && CHARPOS (pos) > BEGV
13662 && IT_CHARPOS (it) < ZV)
13663 /* rms: considering make_cursor_line_fully_visible_p here
13664 seems to give wrong results. We don't want to recenter
13665 when the last line is partly visible, we want to allow
13666 that case to be handled in the usual way. */
13667 || (w->cursor.y + 1) > it.last_visible_y)
13669 w->cursor.vpos = -1;
13670 clear_glyph_matrix (w->desired_matrix);
13671 return -1;
13675 /* If bottom moved off end of frame, change mode line percentage. */
13676 if (XFASTINT (w->window_end_pos) <= 0
13677 && Z != IT_CHARPOS (it))
13678 w->update_mode_line = Qt;
13680 /* Set window_end_pos to the offset of the last character displayed
13681 on the window from the end of current_buffer. Set
13682 window_end_vpos to its row number. */
13683 if (last_text_row)
13685 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13686 w->window_end_bytepos
13687 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13688 w->window_end_pos
13689 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13690 w->window_end_vpos
13691 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13692 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13693 ->displays_text_p);
13695 else
13697 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13698 w->window_end_pos = make_number (Z - ZV);
13699 w->window_end_vpos = make_number (0);
13702 /* But that is not valid info until redisplay finishes. */
13703 w->window_end_valid = Qnil;
13704 return 1;
13709 /************************************************************************
13710 Window redisplay reusing current matrix when buffer has not changed
13711 ************************************************************************/
13713 /* Try redisplay of window W showing an unchanged buffer with a
13714 different window start than the last time it was displayed by
13715 reusing its current matrix. Value is non-zero if successful.
13716 W->start is the new window start. */
13718 static int
13719 try_window_reusing_current_matrix (w)
13720 struct window *w;
13722 struct frame *f = XFRAME (w->frame);
13723 struct glyph_row *row, *bottom_row;
13724 struct it it;
13725 struct run run;
13726 struct text_pos start, new_start;
13727 int nrows_scrolled, i;
13728 struct glyph_row *last_text_row;
13729 struct glyph_row *last_reused_text_row;
13730 struct glyph_row *start_row;
13731 int start_vpos, min_y, max_y;
13733 #if GLYPH_DEBUG
13734 if (inhibit_try_window_reusing)
13735 return 0;
13736 #endif
13738 if (/* This function doesn't handle terminal frames. */
13739 !FRAME_WINDOW_P (f)
13740 /* Don't try to reuse the display if windows have been split
13741 or such. */
13742 || windows_or_buffers_changed
13743 || cursor_type_changed)
13744 return 0;
13746 /* Can't do this if region may have changed. */
13747 if ((!NILP (Vtransient_mark_mode)
13748 && !NILP (current_buffer->mark_active))
13749 || !NILP (w->region_showing)
13750 || !NILP (Vshow_trailing_whitespace))
13751 return 0;
13753 /* If top-line visibility has changed, give up. */
13754 if (WINDOW_WANTS_HEADER_LINE_P (w)
13755 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13756 return 0;
13758 /* Give up if old or new display is scrolled vertically. We could
13759 make this function handle this, but right now it doesn't. */
13760 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13761 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13762 return 0;
13764 /* The variable new_start now holds the new window start. The old
13765 start `start' can be determined from the current matrix. */
13766 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13767 start = start_row->start.pos;
13768 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13770 /* Clear the desired matrix for the display below. */
13771 clear_glyph_matrix (w->desired_matrix);
13773 if (CHARPOS (new_start) <= CHARPOS (start))
13775 int first_row_y;
13777 /* Don't use this method if the display starts with an ellipsis
13778 displayed for invisible text. It's not easy to handle that case
13779 below, and it's certainly not worth the effort since this is
13780 not a frequent case. */
13781 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13782 return 0;
13784 IF_DEBUG (debug_method_add (w, "twu1"));
13786 /* Display up to a row that can be reused. The variable
13787 last_text_row is set to the last row displayed that displays
13788 text. Note that it.vpos == 0 if or if not there is a
13789 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13790 start_display (&it, w, new_start);
13791 first_row_y = it.current_y;
13792 w->cursor.vpos = -1;
13793 last_text_row = last_reused_text_row = NULL;
13795 while (it.current_y < it.last_visible_y
13796 && !fonts_changed_p)
13798 /* If we have reached into the characters in the START row,
13799 that means the line boundaries have changed. So we
13800 can't start copying with the row START. Maybe it will
13801 work to start copying with the following row. */
13802 while (IT_CHARPOS (it) > CHARPOS (start))
13804 /* Advance to the next row as the "start". */
13805 start_row++;
13806 start = start_row->start.pos;
13807 /* If there are no more rows to try, or just one, give up. */
13808 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13809 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13810 || CHARPOS (start) == ZV)
13812 clear_glyph_matrix (w->desired_matrix);
13813 return 0;
13816 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13818 /* If we have reached alignment,
13819 we can copy the rest of the rows. */
13820 if (IT_CHARPOS (it) == CHARPOS (start))
13821 break;
13823 if (display_line (&it))
13824 last_text_row = it.glyph_row - 1;
13827 /* A value of current_y < last_visible_y means that we stopped
13828 at the previous window start, which in turn means that we
13829 have at least one reusable row. */
13830 if (it.current_y < it.last_visible_y)
13832 /* IT.vpos always starts from 0; it counts text lines. */
13833 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13835 /* Find PT if not already found in the lines displayed. */
13836 if (w->cursor.vpos < 0)
13838 int dy = it.current_y - start_row->y;
13840 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13841 row = row_containing_pos (w, PT, row, NULL, dy);
13842 if (row)
13843 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13844 dy, nrows_scrolled);
13845 else
13847 clear_glyph_matrix (w->desired_matrix);
13848 return 0;
13852 /* Scroll the display. Do it before the current matrix is
13853 changed. The problem here is that update has not yet
13854 run, i.e. part of the current matrix is not up to date.
13855 scroll_run_hook will clear the cursor, and use the
13856 current matrix to get the height of the row the cursor is
13857 in. */
13858 run.current_y = start_row->y;
13859 run.desired_y = it.current_y;
13860 run.height = it.last_visible_y - it.current_y;
13862 if (run.height > 0 && run.current_y != run.desired_y)
13864 update_begin (f);
13865 rif->update_window_begin_hook (w);
13866 rif->clear_window_mouse_face (w);
13867 rif->scroll_run_hook (w, &run);
13868 rif->update_window_end_hook (w, 0, 0);
13869 update_end (f);
13872 /* Shift current matrix down by nrows_scrolled lines. */
13873 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13874 rotate_matrix (w->current_matrix,
13875 start_vpos,
13876 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13877 nrows_scrolled);
13879 /* Disable lines that must be updated. */
13880 for (i = 0; i < nrows_scrolled; ++i)
13881 (start_row + i)->enabled_p = 0;
13883 /* Re-compute Y positions. */
13884 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13885 max_y = it.last_visible_y;
13886 for (row = start_row + nrows_scrolled;
13887 row < bottom_row;
13888 ++row)
13890 row->y = it.current_y;
13891 row->visible_height = row->height;
13893 if (row->y < min_y)
13894 row->visible_height -= min_y - row->y;
13895 if (row->y + row->height > max_y)
13896 row->visible_height -= row->y + row->height - max_y;
13897 row->redraw_fringe_bitmaps_p = 1;
13899 it.current_y += row->height;
13901 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13902 last_reused_text_row = row;
13903 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13904 break;
13907 /* Disable lines in the current matrix which are now
13908 below the window. */
13909 for (++row; row < bottom_row; ++row)
13910 row->enabled_p = row->mode_line_p = 0;
13913 /* Update window_end_pos etc.; last_reused_text_row is the last
13914 reused row from the current matrix containing text, if any.
13915 The value of last_text_row is the last displayed line
13916 containing text. */
13917 if (last_reused_text_row)
13919 w->window_end_bytepos
13920 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13921 w->window_end_pos
13922 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13923 w->window_end_vpos
13924 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13925 w->current_matrix));
13927 else if (last_text_row)
13929 w->window_end_bytepos
13930 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13931 w->window_end_pos
13932 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13933 w->window_end_vpos
13934 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13936 else
13938 /* This window must be completely empty. */
13939 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13940 w->window_end_pos = make_number (Z - ZV);
13941 w->window_end_vpos = make_number (0);
13943 w->window_end_valid = Qnil;
13945 /* Update hint: don't try scrolling again in update_window. */
13946 w->desired_matrix->no_scrolling_p = 1;
13948 #if GLYPH_DEBUG
13949 debug_method_add (w, "try_window_reusing_current_matrix 1");
13950 #endif
13951 return 1;
13953 else if (CHARPOS (new_start) > CHARPOS (start))
13955 struct glyph_row *pt_row, *row;
13956 struct glyph_row *first_reusable_row;
13957 struct glyph_row *first_row_to_display;
13958 int dy;
13959 int yb = window_text_bottom_y (w);
13961 /* Find the row starting at new_start, if there is one. Don't
13962 reuse a partially visible line at the end. */
13963 first_reusable_row = start_row;
13964 while (first_reusable_row->enabled_p
13965 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13966 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13967 < CHARPOS (new_start)))
13968 ++first_reusable_row;
13970 /* Give up if there is no row to reuse. */
13971 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13972 || !first_reusable_row->enabled_p
13973 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13974 != CHARPOS (new_start)))
13975 return 0;
13977 /* We can reuse fully visible rows beginning with
13978 first_reusable_row to the end of the window. Set
13979 first_row_to_display to the first row that cannot be reused.
13980 Set pt_row to the row containing point, if there is any. */
13981 pt_row = NULL;
13982 for (first_row_to_display = first_reusable_row;
13983 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13984 ++first_row_to_display)
13986 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13987 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13988 pt_row = first_row_to_display;
13991 /* Start displaying at the start of first_row_to_display. */
13992 xassert (first_row_to_display->y < yb);
13993 init_to_row_start (&it, w, first_row_to_display);
13995 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13996 - start_vpos);
13997 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13998 - nrows_scrolled);
13999 it.current_y = (first_row_to_display->y - first_reusable_row->y
14000 + WINDOW_HEADER_LINE_HEIGHT (w));
14002 /* Display lines beginning with first_row_to_display in the
14003 desired matrix. Set last_text_row to the last row displayed
14004 that displays text. */
14005 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14006 if (pt_row == NULL)
14007 w->cursor.vpos = -1;
14008 last_text_row = NULL;
14009 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14010 if (display_line (&it))
14011 last_text_row = it.glyph_row - 1;
14013 /* Give up If point isn't in a row displayed or reused. */
14014 if (w->cursor.vpos < 0)
14016 clear_glyph_matrix (w->desired_matrix);
14017 return 0;
14020 /* If point is in a reused row, adjust y and vpos of the cursor
14021 position. */
14022 if (pt_row)
14024 w->cursor.vpos -= nrows_scrolled;
14025 w->cursor.y -= first_reusable_row->y - start_row->y;
14028 /* Scroll the display. */
14029 run.current_y = first_reusable_row->y;
14030 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14031 run.height = it.last_visible_y - run.current_y;
14032 dy = run.current_y - run.desired_y;
14034 if (run.height)
14036 update_begin (f);
14037 rif->update_window_begin_hook (w);
14038 rif->clear_window_mouse_face (w);
14039 rif->scroll_run_hook (w, &run);
14040 rif->update_window_end_hook (w, 0, 0);
14041 update_end (f);
14044 /* Adjust Y positions of reused rows. */
14045 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14046 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14047 max_y = it.last_visible_y;
14048 for (row = first_reusable_row; row < first_row_to_display; ++row)
14050 row->y -= dy;
14051 row->visible_height = row->height;
14052 if (row->y < min_y)
14053 row->visible_height -= min_y - row->y;
14054 if (row->y + row->height > max_y)
14055 row->visible_height -= row->y + row->height - max_y;
14056 row->redraw_fringe_bitmaps_p = 1;
14059 /* Scroll the current matrix. */
14060 xassert (nrows_scrolled > 0);
14061 rotate_matrix (w->current_matrix,
14062 start_vpos,
14063 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14064 -nrows_scrolled);
14066 /* Disable rows not reused. */
14067 for (row -= nrows_scrolled; row < bottom_row; ++row)
14068 row->enabled_p = 0;
14070 /* Point may have moved to a different line, so we cannot assume that
14071 the previous cursor position is valid; locate the correct row. */
14072 if (pt_row)
14074 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14075 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14076 row++)
14078 w->cursor.vpos++;
14079 w->cursor.y = row->y;
14081 if (row < bottom_row)
14083 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14084 while (glyph->charpos < PT)
14086 w->cursor.hpos++;
14087 w->cursor.x += glyph->pixel_width;
14088 glyph++;
14093 /* Adjust window end. A null value of last_text_row means that
14094 the window end is in reused rows which in turn means that
14095 only its vpos can have changed. */
14096 if (last_text_row)
14098 w->window_end_bytepos
14099 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14100 w->window_end_pos
14101 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14102 w->window_end_vpos
14103 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14105 else
14107 w->window_end_vpos
14108 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14111 w->window_end_valid = Qnil;
14112 w->desired_matrix->no_scrolling_p = 1;
14114 #if GLYPH_DEBUG
14115 debug_method_add (w, "try_window_reusing_current_matrix 2");
14116 #endif
14117 return 1;
14120 return 0;
14125 /************************************************************************
14126 Window redisplay reusing current matrix when buffer has changed
14127 ************************************************************************/
14129 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14130 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14131 int *, int *));
14132 static struct glyph_row *
14133 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14134 struct glyph_row *));
14137 /* Return the last row in MATRIX displaying text. If row START is
14138 non-null, start searching with that row. IT gives the dimensions
14139 of the display. Value is null if matrix is empty; otherwise it is
14140 a pointer to the row found. */
14142 static struct glyph_row *
14143 find_last_row_displaying_text (matrix, it, start)
14144 struct glyph_matrix *matrix;
14145 struct it *it;
14146 struct glyph_row *start;
14148 struct glyph_row *row, *row_found;
14150 /* Set row_found to the last row in IT->w's current matrix
14151 displaying text. The loop looks funny but think of partially
14152 visible lines. */
14153 row_found = NULL;
14154 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14155 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14157 xassert (row->enabled_p);
14158 row_found = row;
14159 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14160 break;
14161 ++row;
14164 return row_found;
14168 /* Return the last row in the current matrix of W that is not affected
14169 by changes at the start of current_buffer that occurred since W's
14170 current matrix was built. Value is null if no such row exists.
14172 BEG_UNCHANGED us the number of characters unchanged at the start of
14173 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14174 first changed character in current_buffer. Characters at positions <
14175 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14176 when the current matrix was built. */
14178 static struct glyph_row *
14179 find_last_unchanged_at_beg_row (w)
14180 struct window *w;
14182 int first_changed_pos = BEG + BEG_UNCHANGED;
14183 struct glyph_row *row;
14184 struct glyph_row *row_found = NULL;
14185 int yb = window_text_bottom_y (w);
14187 /* Find the last row displaying unchanged text. */
14188 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14189 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14190 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14192 if (/* If row ends before first_changed_pos, it is unchanged,
14193 except in some case. */
14194 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14195 /* When row ends in ZV and we write at ZV it is not
14196 unchanged. */
14197 && !row->ends_at_zv_p
14198 /* When first_changed_pos is the end of a continued line,
14199 row is not unchanged because it may be no longer
14200 continued. */
14201 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14202 && (row->continued_p
14203 || row->exact_window_width_line_p)))
14204 row_found = row;
14206 /* Stop if last visible row. */
14207 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14208 break;
14210 ++row;
14213 return row_found;
14217 /* Find the first glyph row in the current matrix of W that is not
14218 affected by changes at the end of current_buffer since the
14219 time W's current matrix was built.
14221 Return in *DELTA the number of chars by which buffer positions in
14222 unchanged text at the end of current_buffer must be adjusted.
14224 Return in *DELTA_BYTES the corresponding number of bytes.
14226 Value is null if no such row exists, i.e. all rows are affected by
14227 changes. */
14229 static struct glyph_row *
14230 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14231 struct window *w;
14232 int *delta, *delta_bytes;
14234 struct glyph_row *row;
14235 struct glyph_row *row_found = NULL;
14237 *delta = *delta_bytes = 0;
14239 /* Display must not have been paused, otherwise the current matrix
14240 is not up to date. */
14241 if (NILP (w->window_end_valid))
14242 abort ();
14244 /* A value of window_end_pos >= END_UNCHANGED means that the window
14245 end is in the range of changed text. If so, there is no
14246 unchanged row at the end of W's current matrix. */
14247 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14248 return NULL;
14250 /* Set row to the last row in W's current matrix displaying text. */
14251 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14253 /* If matrix is entirely empty, no unchanged row exists. */
14254 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14256 /* The value of row is the last glyph row in the matrix having a
14257 meaningful buffer position in it. The end position of row
14258 corresponds to window_end_pos. This allows us to translate
14259 buffer positions in the current matrix to current buffer
14260 positions for characters not in changed text. */
14261 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14262 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14263 int last_unchanged_pos, last_unchanged_pos_old;
14264 struct glyph_row *first_text_row
14265 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14267 *delta = Z - Z_old;
14268 *delta_bytes = Z_BYTE - Z_BYTE_old;
14270 /* Set last_unchanged_pos to the buffer position of the last
14271 character in the buffer that has not been changed. Z is the
14272 index + 1 of the last character in current_buffer, i.e. by
14273 subtracting END_UNCHANGED we get the index of the last
14274 unchanged character, and we have to add BEG to get its buffer
14275 position. */
14276 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14277 last_unchanged_pos_old = last_unchanged_pos - *delta;
14279 /* Search backward from ROW for a row displaying a line that
14280 starts at a minimum position >= last_unchanged_pos_old. */
14281 for (; row > first_text_row; --row)
14283 /* This used to abort, but it can happen.
14284 It is ok to just stop the search instead here. KFS. */
14285 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14286 break;
14288 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14289 row_found = row;
14293 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14294 abort ();
14296 return row_found;
14300 /* Make sure that glyph rows in the current matrix of window W
14301 reference the same glyph memory as corresponding rows in the
14302 frame's frame matrix. This function is called after scrolling W's
14303 current matrix on a terminal frame in try_window_id and
14304 try_window_reusing_current_matrix. */
14306 static void
14307 sync_frame_with_window_matrix_rows (w)
14308 struct window *w;
14310 struct frame *f = XFRAME (w->frame);
14311 struct glyph_row *window_row, *window_row_end, *frame_row;
14313 /* Preconditions: W must be a leaf window and full-width. Its frame
14314 must have a frame matrix. */
14315 xassert (NILP (w->hchild) && NILP (w->vchild));
14316 xassert (WINDOW_FULL_WIDTH_P (w));
14317 xassert (!FRAME_WINDOW_P (f));
14319 /* If W is a full-width window, glyph pointers in W's current matrix
14320 have, by definition, to be the same as glyph pointers in the
14321 corresponding frame matrix. Note that frame matrices have no
14322 marginal areas (see build_frame_matrix). */
14323 window_row = w->current_matrix->rows;
14324 window_row_end = window_row + w->current_matrix->nrows;
14325 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14326 while (window_row < window_row_end)
14328 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14329 struct glyph *end = window_row->glyphs[LAST_AREA];
14331 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14332 frame_row->glyphs[TEXT_AREA] = start;
14333 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14334 frame_row->glyphs[LAST_AREA] = end;
14336 /* Disable frame rows whose corresponding window rows have
14337 been disabled in try_window_id. */
14338 if (!window_row->enabled_p)
14339 frame_row->enabled_p = 0;
14341 ++window_row, ++frame_row;
14346 /* Find the glyph row in window W containing CHARPOS. Consider all
14347 rows between START and END (not inclusive). END null means search
14348 all rows to the end of the display area of W. Value is the row
14349 containing CHARPOS or null. */
14351 struct glyph_row *
14352 row_containing_pos (w, charpos, start, end, dy)
14353 struct window *w;
14354 int charpos;
14355 struct glyph_row *start, *end;
14356 int dy;
14358 struct glyph_row *row = start;
14359 int last_y;
14361 /* If we happen to start on a header-line, skip that. */
14362 if (row->mode_line_p)
14363 ++row;
14365 if ((end && row >= end) || !row->enabled_p)
14366 return NULL;
14368 last_y = window_text_bottom_y (w) - dy;
14370 while (1)
14372 /* Give up if we have gone too far. */
14373 if (end && row >= end)
14374 return NULL;
14375 /* This formerly returned if they were equal.
14376 I think that both quantities are of a "last plus one" type;
14377 if so, when they are equal, the row is within the screen. -- rms. */
14378 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14379 return NULL;
14381 /* If it is in this row, return this row. */
14382 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14383 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14384 /* The end position of a row equals the start
14385 position of the next row. If CHARPOS is there, we
14386 would rather display it in the next line, except
14387 when this line ends in ZV. */
14388 && !row->ends_at_zv_p
14389 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14390 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14391 return row;
14392 ++row;
14397 /* Try to redisplay window W by reusing its existing display. W's
14398 current matrix must be up to date when this function is called,
14399 i.e. window_end_valid must not be nil.
14401 Value is
14403 1 if display has been updated
14404 0 if otherwise unsuccessful
14405 -1 if redisplay with same window start is known not to succeed
14407 The following steps are performed:
14409 1. Find the last row in the current matrix of W that is not
14410 affected by changes at the start of current_buffer. If no such row
14411 is found, give up.
14413 2. Find the first row in W's current matrix that is not affected by
14414 changes at the end of current_buffer. Maybe there is no such row.
14416 3. Display lines beginning with the row + 1 found in step 1 to the
14417 row found in step 2 or, if step 2 didn't find a row, to the end of
14418 the window.
14420 4. If cursor is not known to appear on the window, give up.
14422 5. If display stopped at the row found in step 2, scroll the
14423 display and current matrix as needed.
14425 6. Maybe display some lines at the end of W, if we must. This can
14426 happen under various circumstances, like a partially visible line
14427 becoming fully visible, or because newly displayed lines are displayed
14428 in smaller font sizes.
14430 7. Update W's window end information. */
14432 static int
14433 try_window_id (w)
14434 struct window *w;
14436 struct frame *f = XFRAME (w->frame);
14437 struct glyph_matrix *current_matrix = w->current_matrix;
14438 struct glyph_matrix *desired_matrix = w->desired_matrix;
14439 struct glyph_row *last_unchanged_at_beg_row;
14440 struct glyph_row *first_unchanged_at_end_row;
14441 struct glyph_row *row;
14442 struct glyph_row *bottom_row;
14443 int bottom_vpos;
14444 struct it it;
14445 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14446 struct text_pos start_pos;
14447 struct run run;
14448 int first_unchanged_at_end_vpos = 0;
14449 struct glyph_row *last_text_row, *last_text_row_at_end;
14450 struct text_pos start;
14451 int first_changed_charpos, last_changed_charpos;
14453 #if GLYPH_DEBUG
14454 if (inhibit_try_window_id)
14455 return 0;
14456 #endif
14458 /* This is handy for debugging. */
14459 #if 0
14460 #define GIVE_UP(X) \
14461 do { \
14462 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14463 return 0; \
14464 } while (0)
14465 #else
14466 #define GIVE_UP(X) return 0
14467 #endif
14469 SET_TEXT_POS_FROM_MARKER (start, w->start);
14471 /* Don't use this for mini-windows because these can show
14472 messages and mini-buffers, and we don't handle that here. */
14473 if (MINI_WINDOW_P (w))
14474 GIVE_UP (1);
14476 /* This flag is used to prevent redisplay optimizations. */
14477 if (windows_or_buffers_changed || cursor_type_changed)
14478 GIVE_UP (2);
14480 /* Verify that narrowing has not changed.
14481 Also verify that we were not told to prevent redisplay optimizations.
14482 It would be nice to further
14483 reduce the number of cases where this prevents try_window_id. */
14484 if (current_buffer->clip_changed
14485 || current_buffer->prevent_redisplay_optimizations_p)
14486 GIVE_UP (3);
14488 /* Window must either use window-based redisplay or be full width. */
14489 if (!FRAME_WINDOW_P (f)
14490 && (!line_ins_del_ok
14491 || !WINDOW_FULL_WIDTH_P (w)))
14492 GIVE_UP (4);
14494 /* Give up if point is not known NOT to appear in W. */
14495 if (PT < CHARPOS (start))
14496 GIVE_UP (5);
14498 /* Another way to prevent redisplay optimizations. */
14499 if (XFASTINT (w->last_modified) == 0)
14500 GIVE_UP (6);
14502 /* Verify that window is not hscrolled. */
14503 if (XFASTINT (w->hscroll) != 0)
14504 GIVE_UP (7);
14506 /* Verify that display wasn't paused. */
14507 if (NILP (w->window_end_valid))
14508 GIVE_UP (8);
14510 /* Can't use this if highlighting a region because a cursor movement
14511 will do more than just set the cursor. */
14512 if (!NILP (Vtransient_mark_mode)
14513 && !NILP (current_buffer->mark_active))
14514 GIVE_UP (9);
14516 /* Likewise if highlighting trailing whitespace. */
14517 if (!NILP (Vshow_trailing_whitespace))
14518 GIVE_UP (11);
14520 /* Likewise if showing a region. */
14521 if (!NILP (w->region_showing))
14522 GIVE_UP (10);
14524 /* Can use this if overlay arrow position and or string have changed. */
14525 if (overlay_arrows_changed_p ())
14526 GIVE_UP (12);
14529 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14530 only if buffer has really changed. The reason is that the gap is
14531 initially at Z for freshly visited files. The code below would
14532 set end_unchanged to 0 in that case. */
14533 if (MODIFF > SAVE_MODIFF
14534 /* This seems to happen sometimes after saving a buffer. */
14535 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14537 if (GPT - BEG < BEG_UNCHANGED)
14538 BEG_UNCHANGED = GPT - BEG;
14539 if (Z - GPT < END_UNCHANGED)
14540 END_UNCHANGED = Z - GPT;
14543 /* The position of the first and last character that has been changed. */
14544 first_changed_charpos = BEG + BEG_UNCHANGED;
14545 last_changed_charpos = Z - END_UNCHANGED;
14547 /* If window starts after a line end, and the last change is in
14548 front of that newline, then changes don't affect the display.
14549 This case happens with stealth-fontification. Note that although
14550 the display is unchanged, glyph positions in the matrix have to
14551 be adjusted, of course. */
14552 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14553 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14554 && ((last_changed_charpos < CHARPOS (start)
14555 && CHARPOS (start) == BEGV)
14556 || (last_changed_charpos < CHARPOS (start) - 1
14557 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14559 int Z_old, delta, Z_BYTE_old, delta_bytes;
14560 struct glyph_row *r0;
14562 /* Compute how many chars/bytes have been added to or removed
14563 from the buffer. */
14564 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14565 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14566 delta = Z - Z_old;
14567 delta_bytes = Z_BYTE - Z_BYTE_old;
14569 /* Give up if PT is not in the window. Note that it already has
14570 been checked at the start of try_window_id that PT is not in
14571 front of the window start. */
14572 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14573 GIVE_UP (13);
14575 /* If window start is unchanged, we can reuse the whole matrix
14576 as is, after adjusting glyph positions. No need to compute
14577 the window end again, since its offset from Z hasn't changed. */
14578 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14579 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14580 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14581 /* PT must not be in a partially visible line. */
14582 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14583 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14585 /* Adjust positions in the glyph matrix. */
14586 if (delta || delta_bytes)
14588 struct glyph_row *r1
14589 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14590 increment_matrix_positions (w->current_matrix,
14591 MATRIX_ROW_VPOS (r0, current_matrix),
14592 MATRIX_ROW_VPOS (r1, current_matrix),
14593 delta, delta_bytes);
14596 /* Set the cursor. */
14597 row = row_containing_pos (w, PT, r0, NULL, 0);
14598 if (row)
14599 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14600 else
14601 abort ();
14602 return 1;
14606 /* Handle the case that changes are all below what is displayed in
14607 the window, and that PT is in the window. This shortcut cannot
14608 be taken if ZV is visible in the window, and text has been added
14609 there that is visible in the window. */
14610 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14611 /* ZV is not visible in the window, or there are no
14612 changes at ZV, actually. */
14613 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14614 || first_changed_charpos == last_changed_charpos))
14616 struct glyph_row *r0;
14618 /* Give up if PT is not in the window. Note that it already has
14619 been checked at the start of try_window_id that PT is not in
14620 front of the window start. */
14621 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14622 GIVE_UP (14);
14624 /* If window start is unchanged, we can reuse the whole matrix
14625 as is, without changing glyph positions since no text has
14626 been added/removed in front of the window end. */
14627 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14628 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14629 /* PT must not be in a partially visible line. */
14630 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14631 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14633 /* We have to compute the window end anew since text
14634 can have been added/removed after it. */
14635 w->window_end_pos
14636 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14637 w->window_end_bytepos
14638 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14640 /* Set the cursor. */
14641 row = row_containing_pos (w, PT, r0, NULL, 0);
14642 if (row)
14643 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14644 else
14645 abort ();
14646 return 2;
14650 /* Give up if window start is in the changed area.
14652 The condition used to read
14654 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14656 but why that was tested escapes me at the moment. */
14657 if (CHARPOS (start) >= first_changed_charpos
14658 && CHARPOS (start) <= last_changed_charpos)
14659 GIVE_UP (15);
14661 /* Check that window start agrees with the start of the first glyph
14662 row in its current matrix. Check this after we know the window
14663 start is not in changed text, otherwise positions would not be
14664 comparable. */
14665 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14666 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14667 GIVE_UP (16);
14669 /* Give up if the window ends in strings. Overlay strings
14670 at the end are difficult to handle, so don't try. */
14671 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14672 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14673 GIVE_UP (20);
14675 /* Compute the position at which we have to start displaying new
14676 lines. Some of the lines at the top of the window might be
14677 reusable because they are not displaying changed text. Find the
14678 last row in W's current matrix not affected by changes at the
14679 start of current_buffer. Value is null if changes start in the
14680 first line of window. */
14681 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14682 if (last_unchanged_at_beg_row)
14684 /* Avoid starting to display in the moddle of a character, a TAB
14685 for instance. This is easier than to set up the iterator
14686 exactly, and it's not a frequent case, so the additional
14687 effort wouldn't really pay off. */
14688 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14689 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14690 && last_unchanged_at_beg_row > w->current_matrix->rows)
14691 --last_unchanged_at_beg_row;
14693 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14694 GIVE_UP (17);
14696 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14697 GIVE_UP (18);
14698 start_pos = it.current.pos;
14700 /* Start displaying new lines in the desired matrix at the same
14701 vpos we would use in the current matrix, i.e. below
14702 last_unchanged_at_beg_row. */
14703 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14704 current_matrix);
14705 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14706 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14708 xassert (it.hpos == 0 && it.current_x == 0);
14710 else
14712 /* There are no reusable lines at the start of the window.
14713 Start displaying in the first text line. */
14714 start_display (&it, w, start);
14715 it.vpos = it.first_vpos;
14716 start_pos = it.current.pos;
14719 /* Find the first row that is not affected by changes at the end of
14720 the buffer. Value will be null if there is no unchanged row, in
14721 which case we must redisplay to the end of the window. delta
14722 will be set to the value by which buffer positions beginning with
14723 first_unchanged_at_end_row have to be adjusted due to text
14724 changes. */
14725 first_unchanged_at_end_row
14726 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14727 IF_DEBUG (debug_delta = delta);
14728 IF_DEBUG (debug_delta_bytes = delta_bytes);
14730 /* Set stop_pos to the buffer position up to which we will have to
14731 display new lines. If first_unchanged_at_end_row != NULL, this
14732 is the buffer position of the start of the line displayed in that
14733 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14734 that we don't stop at a buffer position. */
14735 stop_pos = 0;
14736 if (first_unchanged_at_end_row)
14738 xassert (last_unchanged_at_beg_row == NULL
14739 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14741 /* If this is a continuation line, move forward to the next one
14742 that isn't. Changes in lines above affect this line.
14743 Caution: this may move first_unchanged_at_end_row to a row
14744 not displaying text. */
14745 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14746 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14747 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14748 < it.last_visible_y))
14749 ++first_unchanged_at_end_row;
14751 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14752 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14753 >= it.last_visible_y))
14754 first_unchanged_at_end_row = NULL;
14755 else
14757 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14758 + delta);
14759 first_unchanged_at_end_vpos
14760 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14761 xassert (stop_pos >= Z - END_UNCHANGED);
14764 else if (last_unchanged_at_beg_row == NULL)
14765 GIVE_UP (19);
14768 #if GLYPH_DEBUG
14770 /* Either there is no unchanged row at the end, or the one we have
14771 now displays text. This is a necessary condition for the window
14772 end pos calculation at the end of this function. */
14773 xassert (first_unchanged_at_end_row == NULL
14774 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14776 debug_last_unchanged_at_beg_vpos
14777 = (last_unchanged_at_beg_row
14778 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14779 : -1);
14780 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14782 #endif /* GLYPH_DEBUG != 0 */
14785 /* Display new lines. Set last_text_row to the last new line
14786 displayed which has text on it, i.e. might end up as being the
14787 line where the window_end_vpos is. */
14788 w->cursor.vpos = -1;
14789 last_text_row = NULL;
14790 overlay_arrow_seen = 0;
14791 while (it.current_y < it.last_visible_y
14792 && !fonts_changed_p
14793 && (first_unchanged_at_end_row == NULL
14794 || IT_CHARPOS (it) < stop_pos))
14796 if (display_line (&it))
14797 last_text_row = it.glyph_row - 1;
14800 if (fonts_changed_p)
14801 return -1;
14804 /* Compute differences in buffer positions, y-positions etc. for
14805 lines reused at the bottom of the window. Compute what we can
14806 scroll. */
14807 if (first_unchanged_at_end_row
14808 /* No lines reused because we displayed everything up to the
14809 bottom of the window. */
14810 && it.current_y < it.last_visible_y)
14812 dvpos = (it.vpos
14813 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14814 current_matrix));
14815 dy = it.current_y - first_unchanged_at_end_row->y;
14816 run.current_y = first_unchanged_at_end_row->y;
14817 run.desired_y = run.current_y + dy;
14818 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14820 else
14822 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14823 first_unchanged_at_end_row = NULL;
14825 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14828 /* Find the cursor if not already found. We have to decide whether
14829 PT will appear on this window (it sometimes doesn't, but this is
14830 not a very frequent case.) This decision has to be made before
14831 the current matrix is altered. A value of cursor.vpos < 0 means
14832 that PT is either in one of the lines beginning at
14833 first_unchanged_at_end_row or below the window. Don't care for
14834 lines that might be displayed later at the window end; as
14835 mentioned, this is not a frequent case. */
14836 if (w->cursor.vpos < 0)
14838 /* Cursor in unchanged rows at the top? */
14839 if (PT < CHARPOS (start_pos)
14840 && last_unchanged_at_beg_row)
14842 row = row_containing_pos (w, PT,
14843 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14844 last_unchanged_at_beg_row + 1, 0);
14845 if (row)
14846 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14849 /* Start from first_unchanged_at_end_row looking for PT. */
14850 else if (first_unchanged_at_end_row)
14852 row = row_containing_pos (w, PT - delta,
14853 first_unchanged_at_end_row, NULL, 0);
14854 if (row)
14855 set_cursor_from_row (w, row, w->current_matrix, delta,
14856 delta_bytes, dy, dvpos);
14859 /* Give up if cursor was not found. */
14860 if (w->cursor.vpos < 0)
14862 clear_glyph_matrix (w->desired_matrix);
14863 return -1;
14867 /* Don't let the cursor end in the scroll margins. */
14869 int this_scroll_margin, cursor_height;
14871 this_scroll_margin = max (0, scroll_margin);
14872 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14873 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14874 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14876 if ((w->cursor.y < this_scroll_margin
14877 && CHARPOS (start) > BEGV)
14878 /* Old redisplay didn't take scroll margin into account at the bottom,
14879 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14880 || (w->cursor.y + (make_cursor_line_fully_visible_p
14881 ? cursor_height + this_scroll_margin
14882 : 1)) > it.last_visible_y)
14884 w->cursor.vpos = -1;
14885 clear_glyph_matrix (w->desired_matrix);
14886 return -1;
14890 /* Scroll the display. Do it before changing the current matrix so
14891 that xterm.c doesn't get confused about where the cursor glyph is
14892 found. */
14893 if (dy && run.height)
14895 update_begin (f);
14897 if (FRAME_WINDOW_P (f))
14899 rif->update_window_begin_hook (w);
14900 rif->clear_window_mouse_face (w);
14901 rif->scroll_run_hook (w, &run);
14902 rif->update_window_end_hook (w, 0, 0);
14904 else
14906 /* Terminal frame. In this case, dvpos gives the number of
14907 lines to scroll by; dvpos < 0 means scroll up. */
14908 int first_unchanged_at_end_vpos
14909 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14910 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14911 int end = (WINDOW_TOP_EDGE_LINE (w)
14912 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14913 + window_internal_height (w));
14915 /* Perform the operation on the screen. */
14916 if (dvpos > 0)
14918 /* Scroll last_unchanged_at_beg_row to the end of the
14919 window down dvpos lines. */
14920 set_terminal_window (end);
14922 /* On dumb terminals delete dvpos lines at the end
14923 before inserting dvpos empty lines. */
14924 if (!scroll_region_ok)
14925 ins_del_lines (end - dvpos, -dvpos);
14927 /* Insert dvpos empty lines in front of
14928 last_unchanged_at_beg_row. */
14929 ins_del_lines (from, dvpos);
14931 else if (dvpos < 0)
14933 /* Scroll up last_unchanged_at_beg_vpos to the end of
14934 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14935 set_terminal_window (end);
14937 /* Delete dvpos lines in front of
14938 last_unchanged_at_beg_vpos. ins_del_lines will set
14939 the cursor to the given vpos and emit |dvpos| delete
14940 line sequences. */
14941 ins_del_lines (from + dvpos, dvpos);
14943 /* On a dumb terminal insert dvpos empty lines at the
14944 end. */
14945 if (!scroll_region_ok)
14946 ins_del_lines (end + dvpos, -dvpos);
14949 set_terminal_window (0);
14952 update_end (f);
14955 /* Shift reused rows of the current matrix to the right position.
14956 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14957 text. */
14958 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14959 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14960 if (dvpos < 0)
14962 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14963 bottom_vpos, dvpos);
14964 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14965 bottom_vpos, 0);
14967 else if (dvpos > 0)
14969 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14970 bottom_vpos, dvpos);
14971 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14972 first_unchanged_at_end_vpos + dvpos, 0);
14975 /* For frame-based redisplay, make sure that current frame and window
14976 matrix are in sync with respect to glyph memory. */
14977 if (!FRAME_WINDOW_P (f))
14978 sync_frame_with_window_matrix_rows (w);
14980 /* Adjust buffer positions in reused rows. */
14981 if (delta || delta_bytes)
14982 increment_matrix_positions (current_matrix,
14983 first_unchanged_at_end_vpos + dvpos,
14984 bottom_vpos, delta, delta_bytes);
14986 /* Adjust Y positions. */
14987 if (dy)
14988 shift_glyph_matrix (w, current_matrix,
14989 first_unchanged_at_end_vpos + dvpos,
14990 bottom_vpos, dy);
14992 if (first_unchanged_at_end_row)
14994 first_unchanged_at_end_row += dvpos;
14995 if (first_unchanged_at_end_row->y >= it.last_visible_y
14996 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14997 first_unchanged_at_end_row = NULL;
15000 /* If scrolling up, there may be some lines to display at the end of
15001 the window. */
15002 last_text_row_at_end = NULL;
15003 if (dy < 0)
15005 /* Scrolling up can leave for example a partially visible line
15006 at the end of the window to be redisplayed. */
15007 /* Set last_row to the glyph row in the current matrix where the
15008 window end line is found. It has been moved up or down in
15009 the matrix by dvpos. */
15010 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15011 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15013 /* If last_row is the window end line, it should display text. */
15014 xassert (last_row->displays_text_p);
15016 /* If window end line was partially visible before, begin
15017 displaying at that line. Otherwise begin displaying with the
15018 line following it. */
15019 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15021 init_to_row_start (&it, w, last_row);
15022 it.vpos = last_vpos;
15023 it.current_y = last_row->y;
15025 else
15027 init_to_row_end (&it, w, last_row);
15028 it.vpos = 1 + last_vpos;
15029 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15030 ++last_row;
15033 /* We may start in a continuation line. If so, we have to
15034 get the right continuation_lines_width and current_x. */
15035 it.continuation_lines_width = last_row->continuation_lines_width;
15036 it.hpos = it.current_x = 0;
15038 /* Display the rest of the lines at the window end. */
15039 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15040 while (it.current_y < it.last_visible_y
15041 && !fonts_changed_p)
15043 /* Is it always sure that the display agrees with lines in
15044 the current matrix? I don't think so, so we mark rows
15045 displayed invalid in the current matrix by setting their
15046 enabled_p flag to zero. */
15047 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15048 if (display_line (&it))
15049 last_text_row_at_end = it.glyph_row - 1;
15053 /* Update window_end_pos and window_end_vpos. */
15054 if (first_unchanged_at_end_row
15055 && !last_text_row_at_end)
15057 /* Window end line if one of the preserved rows from the current
15058 matrix. Set row to the last row displaying text in current
15059 matrix starting at first_unchanged_at_end_row, after
15060 scrolling. */
15061 xassert (first_unchanged_at_end_row->displays_text_p);
15062 row = find_last_row_displaying_text (w->current_matrix, &it,
15063 first_unchanged_at_end_row);
15064 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15066 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15067 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15068 w->window_end_vpos
15069 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15070 xassert (w->window_end_bytepos >= 0);
15071 IF_DEBUG (debug_method_add (w, "A"));
15073 else if (last_text_row_at_end)
15075 w->window_end_pos
15076 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15077 w->window_end_bytepos
15078 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15079 w->window_end_vpos
15080 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15081 xassert (w->window_end_bytepos >= 0);
15082 IF_DEBUG (debug_method_add (w, "B"));
15084 else if (last_text_row)
15086 /* We have displayed either to the end of the window or at the
15087 end of the window, i.e. the last row with text is to be found
15088 in the desired matrix. */
15089 w->window_end_pos
15090 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15091 w->window_end_bytepos
15092 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15093 w->window_end_vpos
15094 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15095 xassert (w->window_end_bytepos >= 0);
15097 else if (first_unchanged_at_end_row == NULL
15098 && last_text_row == NULL
15099 && last_text_row_at_end == NULL)
15101 /* Displayed to end of window, but no line containing text was
15102 displayed. Lines were deleted at the end of the window. */
15103 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15104 int vpos = XFASTINT (w->window_end_vpos);
15105 struct glyph_row *current_row = current_matrix->rows + vpos;
15106 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15108 for (row = NULL;
15109 row == NULL && vpos >= first_vpos;
15110 --vpos, --current_row, --desired_row)
15112 if (desired_row->enabled_p)
15114 if (desired_row->displays_text_p)
15115 row = desired_row;
15117 else if (current_row->displays_text_p)
15118 row = current_row;
15121 xassert (row != NULL);
15122 w->window_end_vpos = make_number (vpos + 1);
15123 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15124 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15125 xassert (w->window_end_bytepos >= 0);
15126 IF_DEBUG (debug_method_add (w, "C"));
15128 else
15129 abort ();
15131 #if 0 /* This leads to problems, for instance when the cursor is
15132 at ZV, and the cursor line displays no text. */
15133 /* Disable rows below what's displayed in the window. This makes
15134 debugging easier. */
15135 enable_glyph_matrix_rows (current_matrix,
15136 XFASTINT (w->window_end_vpos) + 1,
15137 bottom_vpos, 0);
15138 #endif
15140 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15141 debug_end_vpos = XFASTINT (w->window_end_vpos));
15143 /* Record that display has not been completed. */
15144 w->window_end_valid = Qnil;
15145 w->desired_matrix->no_scrolling_p = 1;
15146 return 3;
15148 #undef GIVE_UP
15153 /***********************************************************************
15154 More debugging support
15155 ***********************************************************************/
15157 #if GLYPH_DEBUG
15159 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15160 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15161 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15164 /* Dump the contents of glyph matrix MATRIX on stderr.
15166 GLYPHS 0 means don't show glyph contents.
15167 GLYPHS 1 means show glyphs in short form
15168 GLYPHS > 1 means show glyphs in long form. */
15170 void
15171 dump_glyph_matrix (matrix, glyphs)
15172 struct glyph_matrix *matrix;
15173 int glyphs;
15175 int i;
15176 for (i = 0; i < matrix->nrows; ++i)
15177 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15181 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15182 the glyph row and area where the glyph comes from. */
15184 void
15185 dump_glyph (row, glyph, area)
15186 struct glyph_row *row;
15187 struct glyph *glyph;
15188 int area;
15190 if (glyph->type == CHAR_GLYPH)
15192 fprintf (stderr,
15193 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15194 glyph - row->glyphs[TEXT_AREA],
15195 'C',
15196 glyph->charpos,
15197 (BUFFERP (glyph->object)
15198 ? 'B'
15199 : (STRINGP (glyph->object)
15200 ? 'S'
15201 : '-')),
15202 glyph->pixel_width,
15203 glyph->u.ch,
15204 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15205 ? glyph->u.ch
15206 : '.'),
15207 glyph->face_id,
15208 glyph->left_box_line_p,
15209 glyph->right_box_line_p);
15211 else if (glyph->type == STRETCH_GLYPH)
15213 fprintf (stderr,
15214 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15215 glyph - row->glyphs[TEXT_AREA],
15216 'S',
15217 glyph->charpos,
15218 (BUFFERP (glyph->object)
15219 ? 'B'
15220 : (STRINGP (glyph->object)
15221 ? 'S'
15222 : '-')),
15223 glyph->pixel_width,
15225 '.',
15226 glyph->face_id,
15227 glyph->left_box_line_p,
15228 glyph->right_box_line_p);
15230 else if (glyph->type == IMAGE_GLYPH)
15232 fprintf (stderr,
15233 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15234 glyph - row->glyphs[TEXT_AREA],
15235 'I',
15236 glyph->charpos,
15237 (BUFFERP (glyph->object)
15238 ? 'B'
15239 : (STRINGP (glyph->object)
15240 ? 'S'
15241 : '-')),
15242 glyph->pixel_width,
15243 glyph->u.img_id,
15244 '.',
15245 glyph->face_id,
15246 glyph->left_box_line_p,
15247 glyph->right_box_line_p);
15249 else if (glyph->type == COMPOSITE_GLYPH)
15251 fprintf (stderr,
15252 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15253 glyph - row->glyphs[TEXT_AREA],
15254 '+',
15255 glyph->charpos,
15256 (BUFFERP (glyph->object)
15257 ? 'B'
15258 : (STRINGP (glyph->object)
15259 ? 'S'
15260 : '-')),
15261 glyph->pixel_width,
15262 glyph->u.cmp_id,
15263 '.',
15264 glyph->face_id,
15265 glyph->left_box_line_p,
15266 glyph->right_box_line_p);
15271 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15272 GLYPHS 0 means don't show glyph contents.
15273 GLYPHS 1 means show glyphs in short form
15274 GLYPHS > 1 means show glyphs in long form. */
15276 void
15277 dump_glyph_row (row, vpos, glyphs)
15278 struct glyph_row *row;
15279 int vpos, glyphs;
15281 if (glyphs != 1)
15283 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15284 fprintf (stderr, "======================================================================\n");
15286 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15287 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15288 vpos,
15289 MATRIX_ROW_START_CHARPOS (row),
15290 MATRIX_ROW_END_CHARPOS (row),
15291 row->used[TEXT_AREA],
15292 row->contains_overlapping_glyphs_p,
15293 row->enabled_p,
15294 row->truncated_on_left_p,
15295 row->truncated_on_right_p,
15296 row->continued_p,
15297 MATRIX_ROW_CONTINUATION_LINE_P (row),
15298 row->displays_text_p,
15299 row->ends_at_zv_p,
15300 row->fill_line_p,
15301 row->ends_in_middle_of_char_p,
15302 row->starts_in_middle_of_char_p,
15303 row->mouse_face_p,
15304 row->x,
15305 row->y,
15306 row->pixel_width,
15307 row->height,
15308 row->visible_height,
15309 row->ascent,
15310 row->phys_ascent);
15311 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15312 row->end.overlay_string_index,
15313 row->continuation_lines_width);
15314 fprintf (stderr, "%9d %5d\n",
15315 CHARPOS (row->start.string_pos),
15316 CHARPOS (row->end.string_pos));
15317 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15318 row->end.dpvec_index);
15321 if (glyphs > 1)
15323 int area;
15325 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15327 struct glyph *glyph = row->glyphs[area];
15328 struct glyph *glyph_end = glyph + row->used[area];
15330 /* Glyph for a line end in text. */
15331 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15332 ++glyph_end;
15334 if (glyph < glyph_end)
15335 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15337 for (; glyph < glyph_end; ++glyph)
15338 dump_glyph (row, glyph, area);
15341 else if (glyphs == 1)
15343 int area;
15345 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15347 char *s = (char *) alloca (row->used[area] + 1);
15348 int i;
15350 for (i = 0; i < row->used[area]; ++i)
15352 struct glyph *glyph = row->glyphs[area] + i;
15353 if (glyph->type == CHAR_GLYPH
15354 && glyph->u.ch < 0x80
15355 && glyph->u.ch >= ' ')
15356 s[i] = glyph->u.ch;
15357 else
15358 s[i] = '.';
15361 s[i] = '\0';
15362 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15368 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15369 Sdump_glyph_matrix, 0, 1, "p",
15370 doc: /* Dump the current matrix of the selected window to stderr.
15371 Shows contents of glyph row structures. With non-nil
15372 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15373 glyphs in short form, otherwise show glyphs in long form. */)
15374 (glyphs)
15375 Lisp_Object glyphs;
15377 struct window *w = XWINDOW (selected_window);
15378 struct buffer *buffer = XBUFFER (w->buffer);
15380 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15381 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15382 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15383 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15384 fprintf (stderr, "=============================================\n");
15385 dump_glyph_matrix (w->current_matrix,
15386 NILP (glyphs) ? 0 : XINT (glyphs));
15387 return Qnil;
15391 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15392 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15395 struct frame *f = XFRAME (selected_frame);
15396 dump_glyph_matrix (f->current_matrix, 1);
15397 return Qnil;
15401 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15402 doc: /* Dump glyph row ROW to stderr.
15403 GLYPH 0 means don't dump glyphs.
15404 GLYPH 1 means dump glyphs in short form.
15405 GLYPH > 1 or omitted means dump glyphs in long form. */)
15406 (row, glyphs)
15407 Lisp_Object row, glyphs;
15409 struct glyph_matrix *matrix;
15410 int vpos;
15412 CHECK_NUMBER (row);
15413 matrix = XWINDOW (selected_window)->current_matrix;
15414 vpos = XINT (row);
15415 if (vpos >= 0 && vpos < matrix->nrows)
15416 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15417 vpos,
15418 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15419 return Qnil;
15423 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15424 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15425 GLYPH 0 means don't dump glyphs.
15426 GLYPH 1 means dump glyphs in short form.
15427 GLYPH > 1 or omitted means dump glyphs in long form. */)
15428 (row, glyphs)
15429 Lisp_Object row, glyphs;
15431 struct frame *sf = SELECTED_FRAME ();
15432 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15433 int vpos;
15435 CHECK_NUMBER (row);
15436 vpos = XINT (row);
15437 if (vpos >= 0 && vpos < m->nrows)
15438 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15439 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15440 return Qnil;
15444 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15445 doc: /* Toggle tracing of redisplay.
15446 With ARG, turn tracing on if and only if ARG is positive. */)
15447 (arg)
15448 Lisp_Object arg;
15450 if (NILP (arg))
15451 trace_redisplay_p = !trace_redisplay_p;
15452 else
15454 arg = Fprefix_numeric_value (arg);
15455 trace_redisplay_p = XINT (arg) > 0;
15458 return Qnil;
15462 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15463 doc: /* Like `format', but print result to stderr.
15464 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15465 (nargs, args)
15466 int nargs;
15467 Lisp_Object *args;
15469 Lisp_Object s = Fformat (nargs, args);
15470 fprintf (stderr, "%s", SDATA (s));
15471 return Qnil;
15474 #endif /* GLYPH_DEBUG */
15478 /***********************************************************************
15479 Building Desired Matrix Rows
15480 ***********************************************************************/
15482 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15483 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15485 static struct glyph_row *
15486 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15487 struct window *w;
15488 Lisp_Object overlay_arrow_string;
15490 struct frame *f = XFRAME (WINDOW_FRAME (w));
15491 struct buffer *buffer = XBUFFER (w->buffer);
15492 struct buffer *old = current_buffer;
15493 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15494 int arrow_len = SCHARS (overlay_arrow_string);
15495 const unsigned char *arrow_end = arrow_string + arrow_len;
15496 const unsigned char *p;
15497 struct it it;
15498 int multibyte_p;
15499 int n_glyphs_before;
15501 set_buffer_temp (buffer);
15502 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15503 it.glyph_row->used[TEXT_AREA] = 0;
15504 SET_TEXT_POS (it.position, 0, 0);
15506 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15507 p = arrow_string;
15508 while (p < arrow_end)
15510 Lisp_Object face, ilisp;
15512 /* Get the next character. */
15513 if (multibyte_p)
15514 it.c = string_char_and_length (p, arrow_len, &it.len);
15515 else
15516 it.c = *p, it.len = 1;
15517 p += it.len;
15519 /* Get its face. */
15520 ilisp = make_number (p - arrow_string);
15521 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15522 it.face_id = compute_char_face (f, it.c, face);
15524 /* Compute its width, get its glyphs. */
15525 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15526 SET_TEXT_POS (it.position, -1, -1);
15527 PRODUCE_GLYPHS (&it);
15529 /* If this character doesn't fit any more in the line, we have
15530 to remove some glyphs. */
15531 if (it.current_x > it.last_visible_x)
15533 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15534 break;
15538 set_buffer_temp (old);
15539 return it.glyph_row;
15543 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15544 glyphs are only inserted for terminal frames since we can't really
15545 win with truncation glyphs when partially visible glyphs are
15546 involved. Which glyphs to insert is determined by
15547 produce_special_glyphs. */
15549 static void
15550 insert_left_trunc_glyphs (it)
15551 struct it *it;
15553 struct it truncate_it;
15554 struct glyph *from, *end, *to, *toend;
15556 xassert (!FRAME_WINDOW_P (it->f));
15558 /* Get the truncation glyphs. */
15559 truncate_it = *it;
15560 truncate_it.current_x = 0;
15561 truncate_it.face_id = DEFAULT_FACE_ID;
15562 truncate_it.glyph_row = &scratch_glyph_row;
15563 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15564 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15565 truncate_it.object = make_number (0);
15566 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15568 /* Overwrite glyphs from IT with truncation glyphs. */
15569 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15570 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15571 to = it->glyph_row->glyphs[TEXT_AREA];
15572 toend = to + it->glyph_row->used[TEXT_AREA];
15574 while (from < end)
15575 *to++ = *from++;
15577 /* There may be padding glyphs left over. Overwrite them too. */
15578 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15580 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15581 while (from < end)
15582 *to++ = *from++;
15585 if (to > toend)
15586 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15590 /* Compute the pixel height and width of IT->glyph_row.
15592 Most of the time, ascent and height of a display line will be equal
15593 to the max_ascent and max_height values of the display iterator
15594 structure. This is not the case if
15596 1. We hit ZV without displaying anything. In this case, max_ascent
15597 and max_height will be zero.
15599 2. We have some glyphs that don't contribute to the line height.
15600 (The glyph row flag contributes_to_line_height_p is for future
15601 pixmap extensions).
15603 The first case is easily covered by using default values because in
15604 these cases, the line height does not really matter, except that it
15605 must not be zero. */
15607 static void
15608 compute_line_metrics (it)
15609 struct it *it;
15611 struct glyph_row *row = it->glyph_row;
15612 int area, i;
15614 if (FRAME_WINDOW_P (it->f))
15616 int i, min_y, max_y;
15618 /* The line may consist of one space only, that was added to
15619 place the cursor on it. If so, the row's height hasn't been
15620 computed yet. */
15621 if (row->height == 0)
15623 if (it->max_ascent + it->max_descent == 0)
15624 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15625 row->ascent = it->max_ascent;
15626 row->height = it->max_ascent + it->max_descent;
15627 row->phys_ascent = it->max_phys_ascent;
15628 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15629 row->extra_line_spacing = it->max_extra_line_spacing;
15632 /* Compute the width of this line. */
15633 row->pixel_width = row->x;
15634 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15635 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15637 xassert (row->pixel_width >= 0);
15638 xassert (row->ascent >= 0 && row->height > 0);
15640 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15641 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15643 /* If first line's physical ascent is larger than its logical
15644 ascent, use the physical ascent, and make the row taller.
15645 This makes accented characters fully visible. */
15646 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15647 && row->phys_ascent > row->ascent)
15649 row->height += row->phys_ascent - row->ascent;
15650 row->ascent = row->phys_ascent;
15653 /* Compute how much of the line is visible. */
15654 row->visible_height = row->height;
15656 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15657 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15659 if (row->y < min_y)
15660 row->visible_height -= min_y - row->y;
15661 if (row->y + row->height > max_y)
15662 row->visible_height -= row->y + row->height - max_y;
15664 else
15666 row->pixel_width = row->used[TEXT_AREA];
15667 if (row->continued_p)
15668 row->pixel_width -= it->continuation_pixel_width;
15669 else if (row->truncated_on_right_p)
15670 row->pixel_width -= it->truncation_pixel_width;
15671 row->ascent = row->phys_ascent = 0;
15672 row->height = row->phys_height = row->visible_height = 1;
15673 row->extra_line_spacing = 0;
15676 /* Compute a hash code for this row. */
15677 row->hash = 0;
15678 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15679 for (i = 0; i < row->used[area]; ++i)
15680 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15681 + row->glyphs[area][i].u.val
15682 + row->glyphs[area][i].face_id
15683 + row->glyphs[area][i].padding_p
15684 + (row->glyphs[area][i].type << 2));
15686 it->max_ascent = it->max_descent = 0;
15687 it->max_phys_ascent = it->max_phys_descent = 0;
15691 /* Append one space to the glyph row of iterator IT if doing a
15692 window-based redisplay. The space has the same face as
15693 IT->face_id. Value is non-zero if a space was added.
15695 This function is called to make sure that there is always one glyph
15696 at the end of a glyph row that the cursor can be set on under
15697 window-systems. (If there weren't such a glyph we would not know
15698 how wide and tall a box cursor should be displayed).
15700 At the same time this space let's a nicely handle clearing to the
15701 end of the line if the row ends in italic text. */
15703 static int
15704 append_space_for_newline (it, default_face_p)
15705 struct it *it;
15706 int default_face_p;
15708 if (FRAME_WINDOW_P (it->f))
15710 int n = it->glyph_row->used[TEXT_AREA];
15712 if (it->glyph_row->glyphs[TEXT_AREA] + n
15713 < it->glyph_row->glyphs[1 + TEXT_AREA])
15715 /* Save some values that must not be changed.
15716 Must save IT->c and IT->len because otherwise
15717 ITERATOR_AT_END_P wouldn't work anymore after
15718 append_space_for_newline has been called. */
15719 enum display_element_type saved_what = it->what;
15720 int saved_c = it->c, saved_len = it->len;
15721 int saved_x = it->current_x;
15722 int saved_face_id = it->face_id;
15723 struct text_pos saved_pos;
15724 Lisp_Object saved_object;
15725 struct face *face;
15727 saved_object = it->object;
15728 saved_pos = it->position;
15730 it->what = IT_CHARACTER;
15731 bzero (&it->position, sizeof it->position);
15732 it->object = make_number (0);
15733 it->c = ' ';
15734 it->len = 1;
15736 if (default_face_p)
15737 it->face_id = DEFAULT_FACE_ID;
15738 else if (it->face_before_selective_p)
15739 it->face_id = it->saved_face_id;
15740 face = FACE_FROM_ID (it->f, it->face_id);
15741 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15743 PRODUCE_GLYPHS (it);
15745 it->override_ascent = -1;
15746 it->constrain_row_ascent_descent_p = 0;
15747 it->current_x = saved_x;
15748 it->object = saved_object;
15749 it->position = saved_pos;
15750 it->what = saved_what;
15751 it->face_id = saved_face_id;
15752 it->len = saved_len;
15753 it->c = saved_c;
15754 return 1;
15758 return 0;
15762 /* Extend the face of the last glyph in the text area of IT->glyph_row
15763 to the end of the display line. Called from display_line.
15764 If the glyph row is empty, add a space glyph to it so that we
15765 know the face to draw. Set the glyph row flag fill_line_p. */
15767 static void
15768 extend_face_to_end_of_line (it)
15769 struct it *it;
15771 struct face *face;
15772 struct frame *f = it->f;
15774 /* If line is already filled, do nothing. */
15775 if (it->current_x >= it->last_visible_x)
15776 return;
15778 /* Face extension extends the background and box of IT->face_id
15779 to the end of the line. If the background equals the background
15780 of the frame, we don't have to do anything. */
15781 if (it->face_before_selective_p)
15782 face = FACE_FROM_ID (it->f, it->saved_face_id);
15783 else
15784 face = FACE_FROM_ID (f, it->face_id);
15786 if (FRAME_WINDOW_P (f)
15787 && it->glyph_row->displays_text_p
15788 && face->box == FACE_NO_BOX
15789 && face->background == FRAME_BACKGROUND_PIXEL (f)
15790 && !face->stipple)
15791 return;
15793 /* Set the glyph row flag indicating that the face of the last glyph
15794 in the text area has to be drawn to the end of the text area. */
15795 it->glyph_row->fill_line_p = 1;
15797 /* If current character of IT is not ASCII, make sure we have the
15798 ASCII face. This will be automatically undone the next time
15799 get_next_display_element returns a multibyte character. Note
15800 that the character will always be single byte in unibyte text. */
15801 if (!SINGLE_BYTE_CHAR_P (it->c))
15803 it->face_id = FACE_FOR_CHAR (f, face, 0);
15806 if (FRAME_WINDOW_P (f))
15808 /* If the row is empty, add a space with the current face of IT,
15809 so that we know which face to draw. */
15810 if (it->glyph_row->used[TEXT_AREA] == 0)
15812 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15813 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15814 it->glyph_row->used[TEXT_AREA] = 1;
15817 else
15819 /* Save some values that must not be changed. */
15820 int saved_x = it->current_x;
15821 struct text_pos saved_pos;
15822 Lisp_Object saved_object;
15823 enum display_element_type saved_what = it->what;
15824 int saved_face_id = it->face_id;
15826 saved_object = it->object;
15827 saved_pos = it->position;
15829 it->what = IT_CHARACTER;
15830 bzero (&it->position, sizeof it->position);
15831 it->object = make_number (0);
15832 it->c = ' ';
15833 it->len = 1;
15834 it->face_id = face->id;
15836 PRODUCE_GLYPHS (it);
15838 while (it->current_x <= it->last_visible_x)
15839 PRODUCE_GLYPHS (it);
15841 /* Don't count these blanks really. It would let us insert a left
15842 truncation glyph below and make us set the cursor on them, maybe. */
15843 it->current_x = saved_x;
15844 it->object = saved_object;
15845 it->position = saved_pos;
15846 it->what = saved_what;
15847 it->face_id = saved_face_id;
15852 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15853 trailing whitespace. */
15855 static int
15856 trailing_whitespace_p (charpos)
15857 int charpos;
15859 int bytepos = CHAR_TO_BYTE (charpos);
15860 int c = 0;
15862 while (bytepos < ZV_BYTE
15863 && (c = FETCH_CHAR (bytepos),
15864 c == ' ' || c == '\t'))
15865 ++bytepos;
15867 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15869 if (bytepos != PT_BYTE)
15870 return 1;
15872 return 0;
15876 /* Highlight trailing whitespace, if any, in ROW. */
15878 void
15879 highlight_trailing_whitespace (f, row)
15880 struct frame *f;
15881 struct glyph_row *row;
15883 int used = row->used[TEXT_AREA];
15885 if (used)
15887 struct glyph *start = row->glyphs[TEXT_AREA];
15888 struct glyph *glyph = start + used - 1;
15890 /* Skip over glyphs inserted to display the cursor at the
15891 end of a line, for extending the face of the last glyph
15892 to the end of the line on terminals, and for truncation
15893 and continuation glyphs. */
15894 while (glyph >= start
15895 && glyph->type == CHAR_GLYPH
15896 && INTEGERP (glyph->object))
15897 --glyph;
15899 /* If last glyph is a space or stretch, and it's trailing
15900 whitespace, set the face of all trailing whitespace glyphs in
15901 IT->glyph_row to `trailing-whitespace'. */
15902 if (glyph >= start
15903 && BUFFERP (glyph->object)
15904 && (glyph->type == STRETCH_GLYPH
15905 || (glyph->type == CHAR_GLYPH
15906 && glyph->u.ch == ' '))
15907 && trailing_whitespace_p (glyph->charpos))
15909 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15910 if (face_id < 0)
15911 return;
15913 while (glyph >= start
15914 && BUFFERP (glyph->object)
15915 && (glyph->type == STRETCH_GLYPH
15916 || (glyph->type == CHAR_GLYPH
15917 && glyph->u.ch == ' ')))
15918 (glyph--)->face_id = face_id;
15924 /* Value is non-zero if glyph row ROW in window W should be
15925 used to hold the cursor. */
15927 static int
15928 cursor_row_p (w, row)
15929 struct window *w;
15930 struct glyph_row *row;
15932 int cursor_row_p = 1;
15934 if (PT == MATRIX_ROW_END_CHARPOS (row))
15936 /* Suppose the row ends on a string.
15937 Unless the row is continued, that means it ends on a newline
15938 in the string. If it's anything other than a display string
15939 (e.g. a before-string from an overlay), we don't want the
15940 cursor there. (This heuristic seems to give the optimal
15941 behavior for the various types of multi-line strings.) */
15942 if (CHARPOS (row->end.string_pos) >= 0)
15944 if (row->continued_p)
15945 cursor_row_p = 1;
15946 else
15948 /* Check for `display' property. */
15949 struct glyph *beg = row->glyphs[TEXT_AREA];
15950 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15951 struct glyph *glyph;
15953 cursor_row_p = 0;
15954 for (glyph = end; glyph >= beg; --glyph)
15955 if (STRINGP (glyph->object))
15957 Lisp_Object prop
15958 = Fget_char_property (make_number (PT),
15959 Qdisplay, Qnil);
15960 cursor_row_p =
15961 (!NILP (prop)
15962 && display_prop_string_p (prop, glyph->object));
15963 break;
15967 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15969 /* If the row ends in middle of a real character,
15970 and the line is continued, we want the cursor here.
15971 That's because MATRIX_ROW_END_CHARPOS would equal
15972 PT if PT is before the character. */
15973 if (!row->ends_in_ellipsis_p)
15974 cursor_row_p = row->continued_p;
15975 else
15976 /* If the row ends in an ellipsis, then
15977 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15978 We want that position to be displayed after the ellipsis. */
15979 cursor_row_p = 0;
15981 /* If the row ends at ZV, display the cursor at the end of that
15982 row instead of at the start of the row below. */
15983 else if (row->ends_at_zv_p)
15984 cursor_row_p = 1;
15985 else
15986 cursor_row_p = 0;
15989 return cursor_row_p;
15993 /* Construct the glyph row IT->glyph_row in the desired matrix of
15994 IT->w from text at the current position of IT. See dispextern.h
15995 for an overview of struct it. Value is non-zero if
15996 IT->glyph_row displays text, as opposed to a line displaying ZV
15997 only. */
15999 static int
16000 display_line (it)
16001 struct it *it;
16003 struct glyph_row *row = it->glyph_row;
16004 Lisp_Object overlay_arrow_string;
16006 /* We always start displaying at hpos zero even if hscrolled. */
16007 xassert (it->hpos == 0 && it->current_x == 0);
16009 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16010 >= it->w->desired_matrix->nrows)
16012 it->w->nrows_scale_factor++;
16013 fonts_changed_p = 1;
16014 return 0;
16017 /* Is IT->w showing the region? */
16018 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16020 /* Clear the result glyph row and enable it. */
16021 prepare_desired_row (row);
16023 row->y = it->current_y;
16024 row->start = it->start;
16025 row->continuation_lines_width = it->continuation_lines_width;
16026 row->displays_text_p = 1;
16027 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16028 it->starts_in_middle_of_char_p = 0;
16030 /* Arrange the overlays nicely for our purposes. Usually, we call
16031 display_line on only one line at a time, in which case this
16032 can't really hurt too much, or we call it on lines which appear
16033 one after another in the buffer, in which case all calls to
16034 recenter_overlay_lists but the first will be pretty cheap. */
16035 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16037 /* Move over display elements that are not visible because we are
16038 hscrolled. This may stop at an x-position < IT->first_visible_x
16039 if the first glyph is partially visible or if we hit a line end. */
16040 if (it->current_x < it->first_visible_x)
16042 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16043 MOVE_TO_POS | MOVE_TO_X);
16046 /* Get the initial row height. This is either the height of the
16047 text hscrolled, if there is any, or zero. */
16048 row->ascent = it->max_ascent;
16049 row->height = it->max_ascent + it->max_descent;
16050 row->phys_ascent = it->max_phys_ascent;
16051 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16052 row->extra_line_spacing = it->max_extra_line_spacing;
16054 /* Loop generating characters. The loop is left with IT on the next
16055 character to display. */
16056 while (1)
16058 int n_glyphs_before, hpos_before, x_before;
16059 int x, i, nglyphs;
16060 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16062 /* Retrieve the next thing to display. Value is zero if end of
16063 buffer reached. */
16064 if (!get_next_display_element (it))
16066 /* Maybe add a space at the end of this line that is used to
16067 display the cursor there under X. Set the charpos of the
16068 first glyph of blank lines not corresponding to any text
16069 to -1. */
16070 #ifdef HAVE_WINDOW_SYSTEM
16071 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16072 row->exact_window_width_line_p = 1;
16073 else
16074 #endif /* HAVE_WINDOW_SYSTEM */
16075 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16076 || row->used[TEXT_AREA] == 0)
16078 row->glyphs[TEXT_AREA]->charpos = -1;
16079 row->displays_text_p = 0;
16081 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16082 && (!MINI_WINDOW_P (it->w)
16083 || (minibuf_level && EQ (it->window, minibuf_window))))
16084 row->indicate_empty_line_p = 1;
16087 it->continuation_lines_width = 0;
16088 row->ends_at_zv_p = 1;
16089 break;
16092 /* Now, get the metrics of what we want to display. This also
16093 generates glyphs in `row' (which is IT->glyph_row). */
16094 n_glyphs_before = row->used[TEXT_AREA];
16095 x = it->current_x;
16097 /* Remember the line height so far in case the next element doesn't
16098 fit on the line. */
16099 if (!it->truncate_lines_p)
16101 ascent = it->max_ascent;
16102 descent = it->max_descent;
16103 phys_ascent = it->max_phys_ascent;
16104 phys_descent = it->max_phys_descent;
16107 PRODUCE_GLYPHS (it);
16109 /* If this display element was in marginal areas, continue with
16110 the next one. */
16111 if (it->area != TEXT_AREA)
16113 row->ascent = max (row->ascent, it->max_ascent);
16114 row->height = max (row->height, it->max_ascent + it->max_descent);
16115 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16116 row->phys_height = max (row->phys_height,
16117 it->max_phys_ascent + it->max_phys_descent);
16118 row->extra_line_spacing = max (row->extra_line_spacing,
16119 it->max_extra_line_spacing);
16120 set_iterator_to_next (it, 1);
16121 continue;
16124 /* Does the display element fit on the line? If we truncate
16125 lines, we should draw past the right edge of the window. If
16126 we don't truncate, we want to stop so that we can display the
16127 continuation glyph before the right margin. If lines are
16128 continued, there are two possible strategies for characters
16129 resulting in more than 1 glyph (e.g. tabs): Display as many
16130 glyphs as possible in this line and leave the rest for the
16131 continuation line, or display the whole element in the next
16132 line. Original redisplay did the former, so we do it also. */
16133 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16134 hpos_before = it->hpos;
16135 x_before = x;
16137 if (/* Not a newline. */
16138 nglyphs > 0
16139 /* Glyphs produced fit entirely in the line. */
16140 && it->current_x < it->last_visible_x)
16142 it->hpos += nglyphs;
16143 row->ascent = max (row->ascent, it->max_ascent);
16144 row->height = max (row->height, it->max_ascent + it->max_descent);
16145 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16146 row->phys_height = max (row->phys_height,
16147 it->max_phys_ascent + it->max_phys_descent);
16148 row->extra_line_spacing = max (row->extra_line_spacing,
16149 it->max_extra_line_spacing);
16150 if (it->current_x - it->pixel_width < it->first_visible_x)
16151 row->x = x - it->first_visible_x;
16153 else
16155 int new_x;
16156 struct glyph *glyph;
16158 for (i = 0; i < nglyphs; ++i, x = new_x)
16160 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16161 new_x = x + glyph->pixel_width;
16163 if (/* Lines are continued. */
16164 !it->truncate_lines_p
16165 && (/* Glyph doesn't fit on the line. */
16166 new_x > it->last_visible_x
16167 /* Or it fits exactly on a window system frame. */
16168 || (new_x == it->last_visible_x
16169 && FRAME_WINDOW_P (it->f))))
16171 /* End of a continued line. */
16173 if (it->hpos == 0
16174 || (new_x == it->last_visible_x
16175 && FRAME_WINDOW_P (it->f)))
16177 /* Current glyph is the only one on the line or
16178 fits exactly on the line. We must continue
16179 the line because we can't draw the cursor
16180 after the glyph. */
16181 row->continued_p = 1;
16182 it->current_x = new_x;
16183 it->continuation_lines_width += new_x;
16184 ++it->hpos;
16185 if (i == nglyphs - 1)
16187 set_iterator_to_next (it, 1);
16188 #ifdef HAVE_WINDOW_SYSTEM
16189 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16191 if (!get_next_display_element (it))
16193 row->exact_window_width_line_p = 1;
16194 it->continuation_lines_width = 0;
16195 row->continued_p = 0;
16196 row->ends_at_zv_p = 1;
16198 else if (ITERATOR_AT_END_OF_LINE_P (it))
16200 row->continued_p = 0;
16201 row->exact_window_width_line_p = 1;
16204 #endif /* HAVE_WINDOW_SYSTEM */
16207 else if (CHAR_GLYPH_PADDING_P (*glyph)
16208 && !FRAME_WINDOW_P (it->f))
16210 /* A padding glyph that doesn't fit on this line.
16211 This means the whole character doesn't fit
16212 on the line. */
16213 row->used[TEXT_AREA] = n_glyphs_before;
16215 /* Fill the rest of the row with continuation
16216 glyphs like in 20.x. */
16217 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16218 < row->glyphs[1 + TEXT_AREA])
16219 produce_special_glyphs (it, IT_CONTINUATION);
16221 row->continued_p = 1;
16222 it->current_x = x_before;
16223 it->continuation_lines_width += x_before;
16225 /* Restore the height to what it was before the
16226 element not fitting on the line. */
16227 it->max_ascent = ascent;
16228 it->max_descent = descent;
16229 it->max_phys_ascent = phys_ascent;
16230 it->max_phys_descent = phys_descent;
16232 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16234 /* A TAB that extends past the right edge of the
16235 window. This produces a single glyph on
16236 window system frames. We leave the glyph in
16237 this row and let it fill the row, but don't
16238 consume the TAB. */
16239 it->continuation_lines_width += it->last_visible_x;
16240 row->ends_in_middle_of_char_p = 1;
16241 row->continued_p = 1;
16242 glyph->pixel_width = it->last_visible_x - x;
16243 it->starts_in_middle_of_char_p = 1;
16245 else
16247 /* Something other than a TAB that draws past
16248 the right edge of the window. Restore
16249 positions to values before the element. */
16250 row->used[TEXT_AREA] = n_glyphs_before + i;
16252 /* Display continuation glyphs. */
16253 if (!FRAME_WINDOW_P (it->f))
16254 produce_special_glyphs (it, IT_CONTINUATION);
16255 row->continued_p = 1;
16257 it->current_x = x_before;
16258 it->continuation_lines_width += x;
16259 extend_face_to_end_of_line (it);
16261 if (nglyphs > 1 && i > 0)
16263 row->ends_in_middle_of_char_p = 1;
16264 it->starts_in_middle_of_char_p = 1;
16267 /* Restore the height to what it was before the
16268 element not fitting on the line. */
16269 it->max_ascent = ascent;
16270 it->max_descent = descent;
16271 it->max_phys_ascent = phys_ascent;
16272 it->max_phys_descent = phys_descent;
16275 break;
16277 else if (new_x > it->first_visible_x)
16279 /* Increment number of glyphs actually displayed. */
16280 ++it->hpos;
16282 if (x < it->first_visible_x)
16283 /* Glyph is partially visible, i.e. row starts at
16284 negative X position. */
16285 row->x = x - it->first_visible_x;
16287 else
16289 /* Glyph is completely off the left margin of the
16290 window. This should not happen because of the
16291 move_it_in_display_line at the start of this
16292 function, unless the text display area of the
16293 window is empty. */
16294 xassert (it->first_visible_x <= it->last_visible_x);
16298 row->ascent = max (row->ascent, it->max_ascent);
16299 row->height = max (row->height, it->max_ascent + it->max_descent);
16300 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16301 row->phys_height = max (row->phys_height,
16302 it->max_phys_ascent + it->max_phys_descent);
16303 row->extra_line_spacing = max (row->extra_line_spacing,
16304 it->max_extra_line_spacing);
16306 /* End of this display line if row is continued. */
16307 if (row->continued_p || row->ends_at_zv_p)
16308 break;
16311 at_end_of_line:
16312 /* Is this a line end? If yes, we're also done, after making
16313 sure that a non-default face is extended up to the right
16314 margin of the window. */
16315 if (ITERATOR_AT_END_OF_LINE_P (it))
16317 int used_before = row->used[TEXT_AREA];
16319 row->ends_in_newline_from_string_p = STRINGP (it->object);
16321 #ifdef HAVE_WINDOW_SYSTEM
16322 /* Add a space at the end of the line that is used to
16323 display the cursor there. */
16324 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16325 append_space_for_newline (it, 0);
16326 #endif /* HAVE_WINDOW_SYSTEM */
16328 /* Extend the face to the end of the line. */
16329 extend_face_to_end_of_line (it);
16331 /* Make sure we have the position. */
16332 if (used_before == 0)
16333 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16335 /* Consume the line end. This skips over invisible lines. */
16336 set_iterator_to_next (it, 1);
16337 it->continuation_lines_width = 0;
16338 break;
16341 /* Proceed with next display element. Note that this skips
16342 over lines invisible because of selective display. */
16343 set_iterator_to_next (it, 1);
16345 /* If we truncate lines, we are done when the last displayed
16346 glyphs reach past the right margin of the window. */
16347 if (it->truncate_lines_p
16348 && (FRAME_WINDOW_P (it->f)
16349 ? (it->current_x >= it->last_visible_x)
16350 : (it->current_x > it->last_visible_x)))
16352 /* Maybe add truncation glyphs. */
16353 if (!FRAME_WINDOW_P (it->f))
16355 int i, n;
16357 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16358 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16359 break;
16361 for (n = row->used[TEXT_AREA]; i < n; ++i)
16363 row->used[TEXT_AREA] = i;
16364 produce_special_glyphs (it, IT_TRUNCATION);
16367 #ifdef HAVE_WINDOW_SYSTEM
16368 else
16370 /* Don't truncate if we can overflow newline into fringe. */
16371 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16373 if (!get_next_display_element (it))
16375 it->continuation_lines_width = 0;
16376 row->ends_at_zv_p = 1;
16377 row->exact_window_width_line_p = 1;
16378 break;
16380 if (ITERATOR_AT_END_OF_LINE_P (it))
16382 row->exact_window_width_line_p = 1;
16383 goto at_end_of_line;
16387 #endif /* HAVE_WINDOW_SYSTEM */
16389 row->truncated_on_right_p = 1;
16390 it->continuation_lines_width = 0;
16391 reseat_at_next_visible_line_start (it, 0);
16392 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16393 it->hpos = hpos_before;
16394 it->current_x = x_before;
16395 break;
16399 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16400 at the left window margin. */
16401 if (it->first_visible_x
16402 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16404 if (!FRAME_WINDOW_P (it->f))
16405 insert_left_trunc_glyphs (it);
16406 row->truncated_on_left_p = 1;
16409 /* If the start of this line is the overlay arrow-position, then
16410 mark this glyph row as the one containing the overlay arrow.
16411 This is clearly a mess with variable size fonts. It would be
16412 better to let it be displayed like cursors under X. */
16413 if ((row->displays_text_p || !overlay_arrow_seen)
16414 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16415 !NILP (overlay_arrow_string)))
16417 /* Overlay arrow in window redisplay is a fringe bitmap. */
16418 if (STRINGP (overlay_arrow_string))
16420 struct glyph_row *arrow_row
16421 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16422 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16423 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16424 struct glyph *p = row->glyphs[TEXT_AREA];
16425 struct glyph *p2, *end;
16427 /* Copy the arrow glyphs. */
16428 while (glyph < arrow_end)
16429 *p++ = *glyph++;
16431 /* Throw away padding glyphs. */
16432 p2 = p;
16433 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16434 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16435 ++p2;
16436 if (p2 > p)
16438 while (p2 < end)
16439 *p++ = *p2++;
16440 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16443 else
16445 xassert (INTEGERP (overlay_arrow_string));
16446 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16448 overlay_arrow_seen = 1;
16451 /* Compute pixel dimensions of this line. */
16452 compute_line_metrics (it);
16454 /* Remember the position at which this line ends. */
16455 row->end = it->current;
16457 /* Record whether this row ends inside an ellipsis. */
16458 row->ends_in_ellipsis_p
16459 = (it->method == GET_FROM_DISPLAY_VECTOR
16460 && it->ellipsis_p);
16462 /* Save fringe bitmaps in this row. */
16463 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16464 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16465 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16466 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16468 it->left_user_fringe_bitmap = 0;
16469 it->left_user_fringe_face_id = 0;
16470 it->right_user_fringe_bitmap = 0;
16471 it->right_user_fringe_face_id = 0;
16473 /* Maybe set the cursor. */
16474 if (it->w->cursor.vpos < 0
16475 && PT >= MATRIX_ROW_START_CHARPOS (row)
16476 && PT <= MATRIX_ROW_END_CHARPOS (row)
16477 && cursor_row_p (it->w, row))
16478 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16480 /* Highlight trailing whitespace. */
16481 if (!NILP (Vshow_trailing_whitespace))
16482 highlight_trailing_whitespace (it->f, it->glyph_row);
16484 /* Prepare for the next line. This line starts horizontally at (X
16485 HPOS) = (0 0). Vertical positions are incremented. As a
16486 convenience for the caller, IT->glyph_row is set to the next
16487 row to be used. */
16488 it->current_x = it->hpos = 0;
16489 it->current_y += row->height;
16490 ++it->vpos;
16491 ++it->glyph_row;
16492 it->start = it->current;
16493 return row->displays_text_p;
16498 /***********************************************************************
16499 Menu Bar
16500 ***********************************************************************/
16502 /* Redisplay the menu bar in the frame for window W.
16504 The menu bar of X frames that don't have X toolkit support is
16505 displayed in a special window W->frame->menu_bar_window.
16507 The menu bar of terminal frames is treated specially as far as
16508 glyph matrices are concerned. Menu bar lines are not part of
16509 windows, so the update is done directly on the frame matrix rows
16510 for the menu bar. */
16512 static void
16513 display_menu_bar (w)
16514 struct window *w;
16516 struct frame *f = XFRAME (WINDOW_FRAME (w));
16517 struct it it;
16518 Lisp_Object items;
16519 int i;
16521 /* Don't do all this for graphical frames. */
16522 #ifdef HAVE_NTGUI
16523 if (!NILP (Vwindow_system))
16524 return;
16525 #endif
16526 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16527 if (FRAME_X_P (f))
16528 return;
16529 #endif
16530 #ifdef MAC_OS
16531 if (FRAME_MAC_P (f))
16532 return;
16533 #endif
16535 #ifdef USE_X_TOOLKIT
16536 xassert (!FRAME_WINDOW_P (f));
16537 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16538 it.first_visible_x = 0;
16539 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16540 #else /* not USE_X_TOOLKIT */
16541 if (FRAME_WINDOW_P (f))
16543 /* Menu bar lines are displayed in the desired matrix of the
16544 dummy window menu_bar_window. */
16545 struct window *menu_w;
16546 xassert (WINDOWP (f->menu_bar_window));
16547 menu_w = XWINDOW (f->menu_bar_window);
16548 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16549 MENU_FACE_ID);
16550 it.first_visible_x = 0;
16551 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16553 else
16555 /* This is a TTY frame, i.e. character hpos/vpos are used as
16556 pixel x/y. */
16557 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16558 MENU_FACE_ID);
16559 it.first_visible_x = 0;
16560 it.last_visible_x = FRAME_COLS (f);
16562 #endif /* not USE_X_TOOLKIT */
16564 if (! mode_line_inverse_video)
16565 /* Force the menu-bar to be displayed in the default face. */
16566 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16568 /* Clear all rows of the menu bar. */
16569 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16571 struct glyph_row *row = it.glyph_row + i;
16572 clear_glyph_row (row);
16573 row->enabled_p = 1;
16574 row->full_width_p = 1;
16577 /* Display all items of the menu bar. */
16578 items = FRAME_MENU_BAR_ITEMS (it.f);
16579 for (i = 0; i < XVECTOR (items)->size; i += 4)
16581 Lisp_Object string;
16583 /* Stop at nil string. */
16584 string = AREF (items, i + 1);
16585 if (NILP (string))
16586 break;
16588 /* Remember where item was displayed. */
16589 AREF (items, i + 3) = make_number (it.hpos);
16591 /* Display the item, pad with one space. */
16592 if (it.current_x < it.last_visible_x)
16593 display_string (NULL, string, Qnil, 0, 0, &it,
16594 SCHARS (string) + 1, 0, 0, -1);
16597 /* Fill out the line with spaces. */
16598 if (it.current_x < it.last_visible_x)
16599 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16601 /* Compute the total height of the lines. */
16602 compute_line_metrics (&it);
16607 /***********************************************************************
16608 Mode Line
16609 ***********************************************************************/
16611 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16612 FORCE is non-zero, redisplay mode lines unconditionally.
16613 Otherwise, redisplay only mode lines that are garbaged. Value is
16614 the number of windows whose mode lines were redisplayed. */
16616 static int
16617 redisplay_mode_lines (window, force)
16618 Lisp_Object window;
16619 int force;
16621 int nwindows = 0;
16623 while (!NILP (window))
16625 struct window *w = XWINDOW (window);
16627 if (WINDOWP (w->hchild))
16628 nwindows += redisplay_mode_lines (w->hchild, force);
16629 else if (WINDOWP (w->vchild))
16630 nwindows += redisplay_mode_lines (w->vchild, force);
16631 else if (force
16632 || FRAME_GARBAGED_P (XFRAME (w->frame))
16633 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16635 struct text_pos lpoint;
16636 struct buffer *old = current_buffer;
16638 /* Set the window's buffer for the mode line display. */
16639 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16640 set_buffer_internal_1 (XBUFFER (w->buffer));
16642 /* Point refers normally to the selected window. For any
16643 other window, set up appropriate value. */
16644 if (!EQ (window, selected_window))
16646 struct text_pos pt;
16648 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16649 if (CHARPOS (pt) < BEGV)
16650 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16651 else if (CHARPOS (pt) > (ZV - 1))
16652 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16653 else
16654 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16657 /* Display mode lines. */
16658 clear_glyph_matrix (w->desired_matrix);
16659 if (display_mode_lines (w))
16661 ++nwindows;
16662 w->must_be_updated_p = 1;
16665 /* Restore old settings. */
16666 set_buffer_internal_1 (old);
16667 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16670 window = w->next;
16673 return nwindows;
16677 /* Display the mode and/or top line of window W. Value is the number
16678 of mode lines displayed. */
16680 static int
16681 display_mode_lines (w)
16682 struct window *w;
16684 Lisp_Object old_selected_window, old_selected_frame;
16685 int n = 0;
16687 old_selected_frame = selected_frame;
16688 selected_frame = w->frame;
16689 old_selected_window = selected_window;
16690 XSETWINDOW (selected_window, w);
16692 /* These will be set while the mode line specs are processed. */
16693 line_number_displayed = 0;
16694 w->column_number_displayed = Qnil;
16696 if (WINDOW_WANTS_MODELINE_P (w))
16698 struct window *sel_w = XWINDOW (old_selected_window);
16700 /* Select mode line face based on the real selected window. */
16701 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16702 current_buffer->mode_line_format);
16703 ++n;
16706 if (WINDOW_WANTS_HEADER_LINE_P (w))
16708 display_mode_line (w, HEADER_LINE_FACE_ID,
16709 current_buffer->header_line_format);
16710 ++n;
16713 selected_frame = old_selected_frame;
16714 selected_window = old_selected_window;
16715 return n;
16719 /* Display mode or top line of window W. FACE_ID specifies which line
16720 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16721 FORMAT is the mode line format to display. Value is the pixel
16722 height of the mode line displayed. */
16724 static int
16725 display_mode_line (w, face_id, format)
16726 struct window *w;
16727 enum face_id face_id;
16728 Lisp_Object format;
16730 struct it it;
16731 struct face *face;
16732 int count = SPECPDL_INDEX ();
16734 init_iterator (&it, w, -1, -1, NULL, face_id);
16735 /* Don't extend on a previously drawn mode-line.
16736 This may happen if called from pos_visible_p. */
16737 it.glyph_row->enabled_p = 0;
16738 prepare_desired_row (it.glyph_row);
16740 it.glyph_row->mode_line_p = 1;
16742 if (! mode_line_inverse_video)
16743 /* Force the mode-line to be displayed in the default face. */
16744 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16746 record_unwind_protect (unwind_format_mode_line,
16747 format_mode_line_unwind_data (NULL, 0));
16749 mode_line_target = MODE_LINE_DISPLAY;
16751 /* Temporarily make frame's keyboard the current kboard so that
16752 kboard-local variables in the mode_line_format will get the right
16753 values. */
16754 push_frame_kboard (it.f);
16755 record_unwind_save_match_data ();
16756 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16757 pop_frame_kboard ();
16759 unbind_to (count, Qnil);
16761 /* Fill up with spaces. */
16762 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16764 compute_line_metrics (&it);
16765 it.glyph_row->full_width_p = 1;
16766 it.glyph_row->continued_p = 0;
16767 it.glyph_row->truncated_on_left_p = 0;
16768 it.glyph_row->truncated_on_right_p = 0;
16770 /* Make a 3D mode-line have a shadow at its right end. */
16771 face = FACE_FROM_ID (it.f, face_id);
16772 extend_face_to_end_of_line (&it);
16773 if (face->box != FACE_NO_BOX)
16775 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16776 + it.glyph_row->used[TEXT_AREA] - 1);
16777 last->right_box_line_p = 1;
16780 return it.glyph_row->height;
16783 /* Move element ELT in LIST to the front of LIST.
16784 Return the updated list. */
16786 static Lisp_Object
16787 move_elt_to_front (elt, list)
16788 Lisp_Object elt, list;
16790 register Lisp_Object tail, prev;
16791 register Lisp_Object tem;
16793 tail = list;
16794 prev = Qnil;
16795 while (CONSP (tail))
16797 tem = XCAR (tail);
16799 if (EQ (elt, tem))
16801 /* Splice out the link TAIL. */
16802 if (NILP (prev))
16803 list = XCDR (tail);
16804 else
16805 Fsetcdr (prev, XCDR (tail));
16807 /* Now make it the first. */
16808 Fsetcdr (tail, list);
16809 return tail;
16811 else
16812 prev = tail;
16813 tail = XCDR (tail);
16814 QUIT;
16817 /* Not found--return unchanged LIST. */
16818 return list;
16821 /* Contribute ELT to the mode line for window IT->w. How it
16822 translates into text depends on its data type.
16824 IT describes the display environment in which we display, as usual.
16826 DEPTH is the depth in recursion. It is used to prevent
16827 infinite recursion here.
16829 FIELD_WIDTH is the number of characters the display of ELT should
16830 occupy in the mode line, and PRECISION is the maximum number of
16831 characters to display from ELT's representation. See
16832 display_string for details.
16834 Returns the hpos of the end of the text generated by ELT.
16836 PROPS is a property list to add to any string we encounter.
16838 If RISKY is nonzero, remove (disregard) any properties in any string
16839 we encounter, and ignore :eval and :propertize.
16841 The global variable `mode_line_target' determines whether the
16842 output is passed to `store_mode_line_noprop',
16843 `store_mode_line_string', or `display_string'. */
16845 static int
16846 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16847 struct it *it;
16848 int depth;
16849 int field_width, precision;
16850 Lisp_Object elt, props;
16851 int risky;
16853 int n = 0, field, prec;
16854 int literal = 0;
16856 tail_recurse:
16857 if (depth > 100)
16858 elt = build_string ("*too-deep*");
16860 depth++;
16862 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16864 case Lisp_String:
16866 /* A string: output it and check for %-constructs within it. */
16867 unsigned char c;
16868 int offset = 0;
16870 if (SCHARS (elt) > 0
16871 && (!NILP (props) || risky))
16873 Lisp_Object oprops, aelt;
16874 oprops = Ftext_properties_at (make_number (0), elt);
16876 /* If the starting string's properties are not what
16877 we want, translate the string. Also, if the string
16878 is risky, do that anyway. */
16880 if (NILP (Fequal (props, oprops)) || risky)
16882 /* If the starting string has properties,
16883 merge the specified ones onto the existing ones. */
16884 if (! NILP (oprops) && !risky)
16886 Lisp_Object tem;
16888 oprops = Fcopy_sequence (oprops);
16889 tem = props;
16890 while (CONSP (tem))
16892 oprops = Fplist_put (oprops, XCAR (tem),
16893 XCAR (XCDR (tem)));
16894 tem = XCDR (XCDR (tem));
16896 props = oprops;
16899 aelt = Fassoc (elt, mode_line_proptrans_alist);
16900 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16902 /* AELT is what we want. Move it to the front
16903 without consing. */
16904 elt = XCAR (aelt);
16905 mode_line_proptrans_alist
16906 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16908 else
16910 Lisp_Object tem;
16912 /* If AELT has the wrong props, it is useless.
16913 so get rid of it. */
16914 if (! NILP (aelt))
16915 mode_line_proptrans_alist
16916 = Fdelq (aelt, mode_line_proptrans_alist);
16918 elt = Fcopy_sequence (elt);
16919 Fset_text_properties (make_number (0), Flength (elt),
16920 props, elt);
16921 /* Add this item to mode_line_proptrans_alist. */
16922 mode_line_proptrans_alist
16923 = Fcons (Fcons (elt, props),
16924 mode_line_proptrans_alist);
16925 /* Truncate mode_line_proptrans_alist
16926 to at most 50 elements. */
16927 tem = Fnthcdr (make_number (50),
16928 mode_line_proptrans_alist);
16929 if (! NILP (tem))
16930 XSETCDR (tem, Qnil);
16935 offset = 0;
16937 if (literal)
16939 prec = precision - n;
16940 switch (mode_line_target)
16942 case MODE_LINE_NOPROP:
16943 case MODE_LINE_TITLE:
16944 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16945 break;
16946 case MODE_LINE_STRING:
16947 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16948 break;
16949 case MODE_LINE_DISPLAY:
16950 n += display_string (NULL, elt, Qnil, 0, 0, it,
16951 0, prec, 0, STRING_MULTIBYTE (elt));
16952 break;
16955 break;
16958 /* Handle the non-literal case. */
16960 while ((precision <= 0 || n < precision)
16961 && SREF (elt, offset) != 0
16962 && (mode_line_target != MODE_LINE_DISPLAY
16963 || it->current_x < it->last_visible_x))
16965 int last_offset = offset;
16967 /* Advance to end of string or next format specifier. */
16968 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16971 if (offset - 1 != last_offset)
16973 int nchars, nbytes;
16975 /* Output to end of string or up to '%'. Field width
16976 is length of string. Don't output more than
16977 PRECISION allows us. */
16978 offset--;
16980 prec = c_string_width (SDATA (elt) + last_offset,
16981 offset - last_offset, precision - n,
16982 &nchars, &nbytes);
16984 switch (mode_line_target)
16986 case MODE_LINE_NOPROP:
16987 case MODE_LINE_TITLE:
16988 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16989 break;
16990 case MODE_LINE_STRING:
16992 int bytepos = last_offset;
16993 int charpos = string_byte_to_char (elt, bytepos);
16994 int endpos = (precision <= 0
16995 ? string_byte_to_char (elt, offset)
16996 : charpos + nchars);
16998 n += store_mode_line_string (NULL,
16999 Fsubstring (elt, make_number (charpos),
17000 make_number (endpos)),
17001 0, 0, 0, Qnil);
17003 break;
17004 case MODE_LINE_DISPLAY:
17006 int bytepos = last_offset;
17007 int charpos = string_byte_to_char (elt, bytepos);
17009 if (precision <= 0)
17010 nchars = string_byte_to_char (elt, offset) - charpos;
17011 n += display_string (NULL, elt, Qnil, 0, charpos,
17012 it, 0, nchars, 0,
17013 STRING_MULTIBYTE (elt));
17015 break;
17018 else /* c == '%' */
17020 int percent_position = offset;
17022 /* Get the specified minimum width. Zero means
17023 don't pad. */
17024 field = 0;
17025 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17026 field = field * 10 + c - '0';
17028 /* Don't pad beyond the total padding allowed. */
17029 if (field_width - n > 0 && field > field_width - n)
17030 field = field_width - n;
17032 /* Note that either PRECISION <= 0 or N < PRECISION. */
17033 prec = precision - n;
17035 if (c == 'M')
17036 n += display_mode_element (it, depth, field, prec,
17037 Vglobal_mode_string, props,
17038 risky);
17039 else if (c != 0)
17041 int multibyte;
17042 int bytepos, charpos;
17043 unsigned char *spec;
17045 bytepos = percent_position;
17046 charpos = (STRING_MULTIBYTE (elt)
17047 ? string_byte_to_char (elt, bytepos)
17048 : bytepos);
17050 spec
17051 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17053 switch (mode_line_target)
17055 case MODE_LINE_NOPROP:
17056 case MODE_LINE_TITLE:
17057 n += store_mode_line_noprop (spec, field, prec);
17058 break;
17059 case MODE_LINE_STRING:
17061 int len = strlen (spec);
17062 Lisp_Object tem = make_string (spec, len);
17063 props = Ftext_properties_at (make_number (charpos), elt);
17064 /* Should only keep face property in props */
17065 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17067 break;
17068 case MODE_LINE_DISPLAY:
17070 int nglyphs_before, nwritten;
17072 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17073 nwritten = display_string (spec, Qnil, elt,
17074 charpos, 0, it,
17075 field, prec, 0,
17076 multibyte);
17078 /* Assign to the glyphs written above the
17079 string where the `%x' came from, position
17080 of the `%'. */
17081 if (nwritten > 0)
17083 struct glyph *glyph
17084 = (it->glyph_row->glyphs[TEXT_AREA]
17085 + nglyphs_before);
17086 int i;
17088 for (i = 0; i < nwritten; ++i)
17090 glyph[i].object = elt;
17091 glyph[i].charpos = charpos;
17094 n += nwritten;
17097 break;
17100 else /* c == 0 */
17101 break;
17105 break;
17107 case Lisp_Symbol:
17108 /* A symbol: process the value of the symbol recursively
17109 as if it appeared here directly. Avoid error if symbol void.
17110 Special case: if value of symbol is a string, output the string
17111 literally. */
17113 register Lisp_Object tem;
17115 /* If the variable is not marked as risky to set
17116 then its contents are risky to use. */
17117 if (NILP (Fget (elt, Qrisky_local_variable)))
17118 risky = 1;
17120 tem = Fboundp (elt);
17121 if (!NILP (tem))
17123 tem = Fsymbol_value (elt);
17124 /* If value is a string, output that string literally:
17125 don't check for % within it. */
17126 if (STRINGP (tem))
17127 literal = 1;
17129 if (!EQ (tem, elt))
17131 /* Give up right away for nil or t. */
17132 elt = tem;
17133 goto tail_recurse;
17137 break;
17139 case Lisp_Cons:
17141 register Lisp_Object car, tem;
17143 /* A cons cell: five distinct cases.
17144 If first element is :eval or :propertize, do something special.
17145 If first element is a string or a cons, process all the elements
17146 and effectively concatenate them.
17147 If first element is a negative number, truncate displaying cdr to
17148 at most that many characters. If positive, pad (with spaces)
17149 to at least that many characters.
17150 If first element is a symbol, process the cadr or caddr recursively
17151 according to whether the symbol's value is non-nil or nil. */
17152 car = XCAR (elt);
17153 if (EQ (car, QCeval))
17155 /* An element of the form (:eval FORM) means evaluate FORM
17156 and use the result as mode line elements. */
17158 if (risky)
17159 break;
17161 if (CONSP (XCDR (elt)))
17163 Lisp_Object spec;
17164 spec = safe_eval (XCAR (XCDR (elt)));
17165 n += display_mode_element (it, depth, field_width - n,
17166 precision - n, spec, props,
17167 risky);
17170 else if (EQ (car, QCpropertize))
17172 /* An element of the form (:propertize ELT PROPS...)
17173 means display ELT but applying properties PROPS. */
17175 if (risky)
17176 break;
17178 if (CONSP (XCDR (elt)))
17179 n += display_mode_element (it, depth, field_width - n,
17180 precision - n, XCAR (XCDR (elt)),
17181 XCDR (XCDR (elt)), risky);
17183 else if (SYMBOLP (car))
17185 tem = Fboundp (car);
17186 elt = XCDR (elt);
17187 if (!CONSP (elt))
17188 goto invalid;
17189 /* elt is now the cdr, and we know it is a cons cell.
17190 Use its car if CAR has a non-nil value. */
17191 if (!NILP (tem))
17193 tem = Fsymbol_value (car);
17194 if (!NILP (tem))
17196 elt = XCAR (elt);
17197 goto tail_recurse;
17200 /* Symbol's value is nil (or symbol is unbound)
17201 Get the cddr of the original list
17202 and if possible find the caddr and use that. */
17203 elt = XCDR (elt);
17204 if (NILP (elt))
17205 break;
17206 else if (!CONSP (elt))
17207 goto invalid;
17208 elt = XCAR (elt);
17209 goto tail_recurse;
17211 else if (INTEGERP (car))
17213 register int lim = XINT (car);
17214 elt = XCDR (elt);
17215 if (lim < 0)
17217 /* Negative int means reduce maximum width. */
17218 if (precision <= 0)
17219 precision = -lim;
17220 else
17221 precision = min (precision, -lim);
17223 else if (lim > 0)
17225 /* Padding specified. Don't let it be more than
17226 current maximum. */
17227 if (precision > 0)
17228 lim = min (precision, lim);
17230 /* If that's more padding than already wanted, queue it.
17231 But don't reduce padding already specified even if
17232 that is beyond the current truncation point. */
17233 field_width = max (lim, field_width);
17235 goto tail_recurse;
17237 else if (STRINGP (car) || CONSP (car))
17239 register int limit = 50;
17240 /* Limit is to protect against circular lists. */
17241 while (CONSP (elt)
17242 && --limit > 0
17243 && (precision <= 0 || n < precision))
17245 n += display_mode_element (it, depth,
17246 /* Do padding only after the last
17247 element in the list. */
17248 (! CONSP (XCDR (elt))
17249 ? field_width - n
17250 : 0),
17251 precision - n, XCAR (elt),
17252 props, risky);
17253 elt = XCDR (elt);
17257 break;
17259 default:
17260 invalid:
17261 elt = build_string ("*invalid*");
17262 goto tail_recurse;
17265 /* Pad to FIELD_WIDTH. */
17266 if (field_width > 0 && n < field_width)
17268 switch (mode_line_target)
17270 case MODE_LINE_NOPROP:
17271 case MODE_LINE_TITLE:
17272 n += store_mode_line_noprop ("", field_width - n, 0);
17273 break;
17274 case MODE_LINE_STRING:
17275 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17276 break;
17277 case MODE_LINE_DISPLAY:
17278 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17279 0, 0, 0);
17280 break;
17284 return n;
17287 /* Store a mode-line string element in mode_line_string_list.
17289 If STRING is non-null, display that C string. Otherwise, the Lisp
17290 string LISP_STRING is displayed.
17292 FIELD_WIDTH is the minimum number of output glyphs to produce.
17293 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17294 with spaces. FIELD_WIDTH <= 0 means don't pad.
17296 PRECISION is the maximum number of characters to output from
17297 STRING. PRECISION <= 0 means don't truncate the string.
17299 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17300 properties to the string.
17302 PROPS are the properties to add to the string.
17303 The mode_line_string_face face property is always added to the string.
17306 static int
17307 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17308 char *string;
17309 Lisp_Object lisp_string;
17310 int copy_string;
17311 int field_width;
17312 int precision;
17313 Lisp_Object props;
17315 int len;
17316 int n = 0;
17318 if (string != NULL)
17320 len = strlen (string);
17321 if (precision > 0 && len > precision)
17322 len = precision;
17323 lisp_string = make_string (string, len);
17324 if (NILP (props))
17325 props = mode_line_string_face_prop;
17326 else if (!NILP (mode_line_string_face))
17328 Lisp_Object face = Fplist_get (props, Qface);
17329 props = Fcopy_sequence (props);
17330 if (NILP (face))
17331 face = mode_line_string_face;
17332 else
17333 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17334 props = Fplist_put (props, Qface, face);
17336 Fadd_text_properties (make_number (0), make_number (len),
17337 props, lisp_string);
17339 else
17341 len = XFASTINT (Flength (lisp_string));
17342 if (precision > 0 && len > precision)
17344 len = precision;
17345 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17346 precision = -1;
17348 if (!NILP (mode_line_string_face))
17350 Lisp_Object face;
17351 if (NILP (props))
17352 props = Ftext_properties_at (make_number (0), lisp_string);
17353 face = Fplist_get (props, Qface);
17354 if (NILP (face))
17355 face = mode_line_string_face;
17356 else
17357 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17358 props = Fcons (Qface, Fcons (face, Qnil));
17359 if (copy_string)
17360 lisp_string = Fcopy_sequence (lisp_string);
17362 if (!NILP (props))
17363 Fadd_text_properties (make_number (0), make_number (len),
17364 props, lisp_string);
17367 if (len > 0)
17369 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17370 n += len;
17373 if (field_width > len)
17375 field_width -= len;
17376 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17377 if (!NILP (props))
17378 Fadd_text_properties (make_number (0), make_number (field_width),
17379 props, lisp_string);
17380 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17381 n += field_width;
17384 return n;
17388 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17389 1, 4, 0,
17390 doc: /* Format a string out of a mode line format specification.
17391 First arg FORMAT specifies the mode line format (see `mode-line-format'
17392 for details) to use.
17394 Optional second arg FACE specifies the face property to put
17395 on all characters for which no face is specified.
17396 The value t means whatever face the window's mode line currently uses
17397 \(either `mode-line' or `mode-line-inactive', depending).
17398 A value of nil means the default is no face property.
17399 If FACE is an integer, the value string has no text properties.
17401 Optional third and fourth args WINDOW and BUFFER specify the window
17402 and buffer to use as the context for the formatting (defaults
17403 are the selected window and the window's buffer). */)
17404 (format, face, window, buffer)
17405 Lisp_Object format, face, window, buffer;
17407 struct it it;
17408 int len;
17409 struct window *w;
17410 struct buffer *old_buffer = NULL;
17411 int face_id = -1;
17412 int no_props = INTEGERP (face);
17413 int count = SPECPDL_INDEX ();
17414 Lisp_Object str;
17415 int string_start = 0;
17417 if (NILP (window))
17418 window = selected_window;
17419 CHECK_WINDOW (window);
17420 w = XWINDOW (window);
17422 if (NILP (buffer))
17423 buffer = w->buffer;
17424 CHECK_BUFFER (buffer);
17426 /* Make formatting the modeline a non-op when noninteractive, otherwise
17427 there will be problems later caused by a partially initialized frame. */
17428 if (NILP (format) || noninteractive)
17429 return build_string ("");
17431 if (no_props)
17432 face = Qnil;
17434 if (!NILP (face))
17436 if (EQ (face, Qt))
17437 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17438 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17441 if (face_id < 0)
17442 face_id = DEFAULT_FACE_ID;
17444 if (XBUFFER (buffer) != current_buffer)
17445 old_buffer = current_buffer;
17447 /* Save things including mode_line_proptrans_alist,
17448 and set that to nil so that we don't alter the outer value. */
17449 record_unwind_protect (unwind_format_mode_line,
17450 format_mode_line_unwind_data (old_buffer, 1));
17451 mode_line_proptrans_alist = Qnil;
17453 if (old_buffer)
17454 set_buffer_internal_1 (XBUFFER (buffer));
17456 init_iterator (&it, w, -1, -1, NULL, face_id);
17458 if (no_props)
17460 mode_line_target = MODE_LINE_NOPROP;
17461 mode_line_string_face_prop = Qnil;
17462 mode_line_string_list = Qnil;
17463 string_start = MODE_LINE_NOPROP_LEN (0);
17465 else
17467 mode_line_target = MODE_LINE_STRING;
17468 mode_line_string_list = Qnil;
17469 mode_line_string_face = face;
17470 mode_line_string_face_prop
17471 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17474 push_frame_kboard (it.f);
17475 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17476 pop_frame_kboard ();
17478 if (no_props)
17480 len = MODE_LINE_NOPROP_LEN (string_start);
17481 str = make_string (mode_line_noprop_buf + string_start, len);
17483 else
17485 mode_line_string_list = Fnreverse (mode_line_string_list);
17486 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17487 make_string ("", 0));
17490 unbind_to (count, Qnil);
17491 return str;
17494 /* Write a null-terminated, right justified decimal representation of
17495 the positive integer D to BUF using a minimal field width WIDTH. */
17497 static void
17498 pint2str (buf, width, d)
17499 register char *buf;
17500 register int width;
17501 register int d;
17503 register char *p = buf;
17505 if (d <= 0)
17506 *p++ = '0';
17507 else
17509 while (d > 0)
17511 *p++ = d % 10 + '0';
17512 d /= 10;
17516 for (width -= (int) (p - buf); width > 0; --width)
17517 *p++ = ' ';
17518 *p-- = '\0';
17519 while (p > buf)
17521 d = *buf;
17522 *buf++ = *p;
17523 *p-- = d;
17527 /* Write a null-terminated, right justified decimal and "human
17528 readable" representation of the nonnegative integer D to BUF using
17529 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17531 static const char power_letter[] =
17533 0, /* not used */
17534 'k', /* kilo */
17535 'M', /* mega */
17536 'G', /* giga */
17537 'T', /* tera */
17538 'P', /* peta */
17539 'E', /* exa */
17540 'Z', /* zetta */
17541 'Y' /* yotta */
17544 static void
17545 pint2hrstr (buf, width, d)
17546 char *buf;
17547 int width;
17548 int d;
17550 /* We aim to represent the nonnegative integer D as
17551 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17552 int quotient = d;
17553 int remainder = 0;
17554 /* -1 means: do not use TENTHS. */
17555 int tenths = -1;
17556 int exponent = 0;
17558 /* Length of QUOTIENT.TENTHS as a string. */
17559 int length;
17561 char * psuffix;
17562 char * p;
17564 if (1000 <= quotient)
17566 /* Scale to the appropriate EXPONENT. */
17569 remainder = quotient % 1000;
17570 quotient /= 1000;
17571 exponent++;
17573 while (1000 <= quotient);
17575 /* Round to nearest and decide whether to use TENTHS or not. */
17576 if (quotient <= 9)
17578 tenths = remainder / 100;
17579 if (50 <= remainder % 100)
17581 if (tenths < 9)
17582 tenths++;
17583 else
17585 quotient++;
17586 if (quotient == 10)
17587 tenths = -1;
17588 else
17589 tenths = 0;
17593 else
17594 if (500 <= remainder)
17596 if (quotient < 999)
17597 quotient++;
17598 else
17600 quotient = 1;
17601 exponent++;
17602 tenths = 0;
17607 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17608 if (tenths == -1 && quotient <= 99)
17609 if (quotient <= 9)
17610 length = 1;
17611 else
17612 length = 2;
17613 else
17614 length = 3;
17615 p = psuffix = buf + max (width, length);
17617 /* Print EXPONENT. */
17618 if (exponent)
17619 *psuffix++ = power_letter[exponent];
17620 *psuffix = '\0';
17622 /* Print TENTHS. */
17623 if (tenths >= 0)
17625 *--p = '0' + tenths;
17626 *--p = '.';
17629 /* Print QUOTIENT. */
17632 int digit = quotient % 10;
17633 *--p = '0' + digit;
17635 while ((quotient /= 10) != 0);
17637 /* Print leading spaces. */
17638 while (buf < p)
17639 *--p = ' ';
17642 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17643 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17644 type of CODING_SYSTEM. Return updated pointer into BUF. */
17646 static unsigned char invalid_eol_type[] = "(*invalid*)";
17648 static char *
17649 decode_mode_spec_coding (coding_system, buf, eol_flag)
17650 Lisp_Object coding_system;
17651 register char *buf;
17652 int eol_flag;
17654 Lisp_Object val;
17655 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17656 const unsigned char *eol_str;
17657 int eol_str_len;
17658 /* The EOL conversion we are using. */
17659 Lisp_Object eoltype;
17661 val = Fget (coding_system, Qcoding_system);
17662 eoltype = Qnil;
17664 if (!VECTORP (val)) /* Not yet decided. */
17666 if (multibyte)
17667 *buf++ = '-';
17668 if (eol_flag)
17669 eoltype = eol_mnemonic_undecided;
17670 /* Don't mention EOL conversion if it isn't decided. */
17672 else
17674 Lisp_Object eolvalue;
17676 eolvalue = Fget (coding_system, Qeol_type);
17678 if (multibyte)
17679 *buf++ = XFASTINT (AREF (val, 1));
17681 if (eol_flag)
17683 /* The EOL conversion that is normal on this system. */
17685 if (NILP (eolvalue)) /* Not yet decided. */
17686 eoltype = eol_mnemonic_undecided;
17687 else if (VECTORP (eolvalue)) /* Not yet decided. */
17688 eoltype = eol_mnemonic_undecided;
17689 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17690 eoltype = (XFASTINT (eolvalue) == 0
17691 ? eol_mnemonic_unix
17692 : (XFASTINT (eolvalue) == 1
17693 ? eol_mnemonic_dos : eol_mnemonic_mac));
17697 if (eol_flag)
17699 /* Mention the EOL conversion if it is not the usual one. */
17700 if (STRINGP (eoltype))
17702 eol_str = SDATA (eoltype);
17703 eol_str_len = SBYTES (eoltype);
17705 else if (INTEGERP (eoltype)
17706 && CHAR_VALID_P (XINT (eoltype), 0))
17708 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17709 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17710 eol_str = tmp;
17712 else
17714 eol_str = invalid_eol_type;
17715 eol_str_len = sizeof (invalid_eol_type) - 1;
17717 bcopy (eol_str, buf, eol_str_len);
17718 buf += eol_str_len;
17721 return buf;
17724 /* Return a string for the output of a mode line %-spec for window W,
17725 generated by character C. PRECISION >= 0 means don't return a
17726 string longer than that value. FIELD_WIDTH > 0 means pad the
17727 string returned with spaces to that value. Return 1 in *MULTIBYTE
17728 if the result is multibyte text.
17730 Note we operate on the current buffer for most purposes,
17731 the exception being w->base_line_pos. */
17733 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17735 static char *
17736 decode_mode_spec (w, c, field_width, precision, multibyte)
17737 struct window *w;
17738 register int c;
17739 int field_width, precision;
17740 int *multibyte;
17742 Lisp_Object obj;
17743 struct frame *f = XFRAME (WINDOW_FRAME (w));
17744 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17745 struct buffer *b = current_buffer;
17747 obj = Qnil;
17748 *multibyte = 0;
17750 switch (c)
17752 case '*':
17753 if (!NILP (b->read_only))
17754 return "%";
17755 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17756 return "*";
17757 return "-";
17759 case '+':
17760 /* This differs from %* only for a modified read-only buffer. */
17761 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17762 return "*";
17763 if (!NILP (b->read_only))
17764 return "%";
17765 return "-";
17767 case '&':
17768 /* This differs from %* in ignoring read-only-ness. */
17769 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17770 return "*";
17771 return "-";
17773 case '%':
17774 return "%";
17776 case '[':
17778 int i;
17779 char *p;
17781 if (command_loop_level > 5)
17782 return "[[[... ";
17783 p = decode_mode_spec_buf;
17784 for (i = 0; i < command_loop_level; i++)
17785 *p++ = '[';
17786 *p = 0;
17787 return decode_mode_spec_buf;
17790 case ']':
17792 int i;
17793 char *p;
17795 if (command_loop_level > 5)
17796 return " ...]]]";
17797 p = decode_mode_spec_buf;
17798 for (i = 0; i < command_loop_level; i++)
17799 *p++ = ']';
17800 *p = 0;
17801 return decode_mode_spec_buf;
17804 case '-':
17806 register int i;
17808 /* Let lots_of_dashes be a string of infinite length. */
17809 if (mode_line_target == MODE_LINE_NOPROP ||
17810 mode_line_target == MODE_LINE_STRING)
17811 return "--";
17812 if (field_width <= 0
17813 || field_width > sizeof (lots_of_dashes))
17815 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17816 decode_mode_spec_buf[i] = '-';
17817 decode_mode_spec_buf[i] = '\0';
17818 return decode_mode_spec_buf;
17820 else
17821 return lots_of_dashes;
17824 case 'b':
17825 obj = b->name;
17826 break;
17828 case 'c':
17829 /* %c and %l are ignored in `frame-title-format'.
17830 (In redisplay_internal, the frame title is drawn _before_ the
17831 windows are updated, so the stuff which depends on actual
17832 window contents (such as %l) may fail to render properly, or
17833 even crash emacs.) */
17834 if (mode_line_target == MODE_LINE_TITLE)
17835 return "";
17836 else
17838 int col = (int) current_column (); /* iftc */
17839 w->column_number_displayed = make_number (col);
17840 pint2str (decode_mode_spec_buf, field_width, col);
17841 return decode_mode_spec_buf;
17844 case 'e':
17845 #ifndef SYSTEM_MALLOC
17847 if (NILP (Vmemory_full))
17848 return "";
17849 else
17850 return "!MEM FULL! ";
17852 #else
17853 return "";
17854 #endif
17856 case 'F':
17857 /* %F displays the frame name. */
17858 if (!NILP (f->title))
17859 return (char *) SDATA (f->title);
17860 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17861 return (char *) SDATA (f->name);
17862 return "Emacs";
17864 case 'f':
17865 obj = b->filename;
17866 break;
17868 case 'i':
17870 int size = ZV - BEGV;
17871 pint2str (decode_mode_spec_buf, field_width, size);
17872 return decode_mode_spec_buf;
17875 case 'I':
17877 int size = ZV - BEGV;
17878 pint2hrstr (decode_mode_spec_buf, field_width, size);
17879 return decode_mode_spec_buf;
17882 case 'l':
17884 int startpos, startpos_byte, line, linepos, linepos_byte;
17885 int topline, nlines, junk, height;
17887 /* %c and %l are ignored in `frame-title-format'. */
17888 if (mode_line_target == MODE_LINE_TITLE)
17889 return "";
17891 startpos = XMARKER (w->start)->charpos;
17892 startpos_byte = marker_byte_position (w->start);
17893 height = WINDOW_TOTAL_LINES (w);
17895 /* If we decided that this buffer isn't suitable for line numbers,
17896 don't forget that too fast. */
17897 if (EQ (w->base_line_pos, w->buffer))
17898 goto no_value;
17899 /* But do forget it, if the window shows a different buffer now. */
17900 else if (BUFFERP (w->base_line_pos))
17901 w->base_line_pos = Qnil;
17903 /* If the buffer is very big, don't waste time. */
17904 if (INTEGERP (Vline_number_display_limit)
17905 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17907 w->base_line_pos = Qnil;
17908 w->base_line_number = Qnil;
17909 goto no_value;
17912 if (!NILP (w->base_line_number)
17913 && !NILP (w->base_line_pos)
17914 && XFASTINT (w->base_line_pos) <= startpos)
17916 line = XFASTINT (w->base_line_number);
17917 linepos = XFASTINT (w->base_line_pos);
17918 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17920 else
17922 line = 1;
17923 linepos = BUF_BEGV (b);
17924 linepos_byte = BUF_BEGV_BYTE (b);
17927 /* Count lines from base line to window start position. */
17928 nlines = display_count_lines (linepos, linepos_byte,
17929 startpos_byte,
17930 startpos, &junk);
17932 topline = nlines + line;
17934 /* Determine a new base line, if the old one is too close
17935 or too far away, or if we did not have one.
17936 "Too close" means it's plausible a scroll-down would
17937 go back past it. */
17938 if (startpos == BUF_BEGV (b))
17940 w->base_line_number = make_number (topline);
17941 w->base_line_pos = make_number (BUF_BEGV (b));
17943 else if (nlines < height + 25 || nlines > height * 3 + 50
17944 || linepos == BUF_BEGV (b))
17946 int limit = BUF_BEGV (b);
17947 int limit_byte = BUF_BEGV_BYTE (b);
17948 int position;
17949 int distance = (height * 2 + 30) * line_number_display_limit_width;
17951 if (startpos - distance > limit)
17953 limit = startpos - distance;
17954 limit_byte = CHAR_TO_BYTE (limit);
17957 nlines = display_count_lines (startpos, startpos_byte,
17958 limit_byte,
17959 - (height * 2 + 30),
17960 &position);
17961 /* If we couldn't find the lines we wanted within
17962 line_number_display_limit_width chars per line,
17963 give up on line numbers for this window. */
17964 if (position == limit_byte && limit == startpos - distance)
17966 w->base_line_pos = w->buffer;
17967 w->base_line_number = Qnil;
17968 goto no_value;
17971 w->base_line_number = make_number (topline - nlines);
17972 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17975 /* Now count lines from the start pos to point. */
17976 nlines = display_count_lines (startpos, startpos_byte,
17977 PT_BYTE, PT, &junk);
17979 /* Record that we did display the line number. */
17980 line_number_displayed = 1;
17982 /* Make the string to show. */
17983 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17984 return decode_mode_spec_buf;
17985 no_value:
17987 char* p = decode_mode_spec_buf;
17988 int pad = field_width - 2;
17989 while (pad-- > 0)
17990 *p++ = ' ';
17991 *p++ = '?';
17992 *p++ = '?';
17993 *p = '\0';
17994 return decode_mode_spec_buf;
17997 break;
17999 case 'm':
18000 obj = b->mode_name;
18001 break;
18003 case 'n':
18004 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18005 return " Narrow";
18006 break;
18008 case 'p':
18010 int pos = marker_position (w->start);
18011 int total = BUF_ZV (b) - BUF_BEGV (b);
18013 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18015 if (pos <= BUF_BEGV (b))
18016 return "All";
18017 else
18018 return "Bottom";
18020 else if (pos <= BUF_BEGV (b))
18021 return "Top";
18022 else
18024 if (total > 1000000)
18025 /* Do it differently for a large value, to avoid overflow. */
18026 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18027 else
18028 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18029 /* We can't normally display a 3-digit number,
18030 so get us a 2-digit number that is close. */
18031 if (total == 100)
18032 total = 99;
18033 sprintf (decode_mode_spec_buf, "%2d%%", total);
18034 return decode_mode_spec_buf;
18038 /* Display percentage of size above the bottom of the screen. */
18039 case 'P':
18041 int toppos = marker_position (w->start);
18042 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18043 int total = BUF_ZV (b) - BUF_BEGV (b);
18045 if (botpos >= BUF_ZV (b))
18047 if (toppos <= BUF_BEGV (b))
18048 return "All";
18049 else
18050 return "Bottom";
18052 else
18054 if (total > 1000000)
18055 /* Do it differently for a large value, to avoid overflow. */
18056 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18057 else
18058 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18059 /* We can't normally display a 3-digit number,
18060 so get us a 2-digit number that is close. */
18061 if (total == 100)
18062 total = 99;
18063 if (toppos <= BUF_BEGV (b))
18064 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18065 else
18066 sprintf (decode_mode_spec_buf, "%2d%%", total);
18067 return decode_mode_spec_buf;
18071 case 's':
18072 /* status of process */
18073 obj = Fget_buffer_process (Fcurrent_buffer ());
18074 if (NILP (obj))
18075 return "no process";
18076 #ifdef subprocesses
18077 obj = Fsymbol_name (Fprocess_status (obj));
18078 #endif
18079 break;
18081 case 't': /* indicate TEXT or BINARY */
18082 #ifdef MODE_LINE_BINARY_TEXT
18083 return MODE_LINE_BINARY_TEXT (b);
18084 #else
18085 return "T";
18086 #endif
18088 case 'z':
18089 /* coding-system (not including end-of-line format) */
18090 case 'Z':
18091 /* coding-system (including end-of-line type) */
18093 int eol_flag = (c == 'Z');
18094 char *p = decode_mode_spec_buf;
18096 if (! FRAME_WINDOW_P (f))
18098 /* No need to mention EOL here--the terminal never needs
18099 to do EOL conversion. */
18100 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
18101 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
18103 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18104 p, eol_flag);
18106 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18107 #ifdef subprocesses
18108 obj = Fget_buffer_process (Fcurrent_buffer ());
18109 if (PROCESSP (obj))
18111 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18112 p, eol_flag);
18113 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18114 p, eol_flag);
18116 #endif /* subprocesses */
18117 #endif /* 0 */
18118 *p = 0;
18119 return decode_mode_spec_buf;
18123 if (STRINGP (obj))
18125 *multibyte = STRING_MULTIBYTE (obj);
18126 return (char *) SDATA (obj);
18128 else
18129 return "";
18133 /* Count up to COUNT lines starting from START / START_BYTE.
18134 But don't go beyond LIMIT_BYTE.
18135 Return the number of lines thus found (always nonnegative).
18137 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18139 static int
18140 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18141 int start, start_byte, limit_byte, count;
18142 int *byte_pos_ptr;
18144 register unsigned char *cursor;
18145 unsigned char *base;
18147 register int ceiling;
18148 register unsigned char *ceiling_addr;
18149 int orig_count = count;
18151 /* If we are not in selective display mode,
18152 check only for newlines. */
18153 int selective_display = (!NILP (current_buffer->selective_display)
18154 && !INTEGERP (current_buffer->selective_display));
18156 if (count > 0)
18158 while (start_byte < limit_byte)
18160 ceiling = BUFFER_CEILING_OF (start_byte);
18161 ceiling = min (limit_byte - 1, ceiling);
18162 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18163 base = (cursor = BYTE_POS_ADDR (start_byte));
18164 while (1)
18166 if (selective_display)
18167 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18169 else
18170 while (*cursor != '\n' && ++cursor != ceiling_addr)
18173 if (cursor != ceiling_addr)
18175 if (--count == 0)
18177 start_byte += cursor - base + 1;
18178 *byte_pos_ptr = start_byte;
18179 return orig_count;
18181 else
18182 if (++cursor == ceiling_addr)
18183 break;
18185 else
18186 break;
18188 start_byte += cursor - base;
18191 else
18193 while (start_byte > limit_byte)
18195 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18196 ceiling = max (limit_byte, ceiling);
18197 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18198 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18199 while (1)
18201 if (selective_display)
18202 while (--cursor != ceiling_addr
18203 && *cursor != '\n' && *cursor != 015)
18205 else
18206 while (--cursor != ceiling_addr && *cursor != '\n')
18209 if (cursor != ceiling_addr)
18211 if (++count == 0)
18213 start_byte += cursor - base + 1;
18214 *byte_pos_ptr = start_byte;
18215 /* When scanning backwards, we should
18216 not count the newline posterior to which we stop. */
18217 return - orig_count - 1;
18220 else
18221 break;
18223 /* Here we add 1 to compensate for the last decrement
18224 of CURSOR, which took it past the valid range. */
18225 start_byte += cursor - base + 1;
18229 *byte_pos_ptr = limit_byte;
18231 if (count < 0)
18232 return - orig_count + count;
18233 return orig_count - count;
18239 /***********************************************************************
18240 Displaying strings
18241 ***********************************************************************/
18243 /* Display a NUL-terminated string, starting with index START.
18245 If STRING is non-null, display that C string. Otherwise, the Lisp
18246 string LISP_STRING is displayed.
18248 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18249 FACE_STRING. Display STRING or LISP_STRING with the face at
18250 FACE_STRING_POS in FACE_STRING:
18252 Display the string in the environment given by IT, but use the
18253 standard display table, temporarily.
18255 FIELD_WIDTH is the minimum number of output glyphs to produce.
18256 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18257 with spaces. If STRING has more characters, more than FIELD_WIDTH
18258 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18260 PRECISION is the maximum number of characters to output from
18261 STRING. PRECISION < 0 means don't truncate the string.
18263 This is roughly equivalent to printf format specifiers:
18265 FIELD_WIDTH PRECISION PRINTF
18266 ----------------------------------------
18267 -1 -1 %s
18268 -1 10 %.10s
18269 10 -1 %10s
18270 20 10 %20.10s
18272 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18273 display them, and < 0 means obey the current buffer's value of
18274 enable_multibyte_characters.
18276 Value is the number of columns displayed. */
18278 static int
18279 display_string (string, lisp_string, face_string, face_string_pos,
18280 start, it, field_width, precision, max_x, multibyte)
18281 unsigned char *string;
18282 Lisp_Object lisp_string;
18283 Lisp_Object face_string;
18284 int face_string_pos;
18285 int start;
18286 struct it *it;
18287 int field_width, precision, max_x;
18288 int multibyte;
18290 int hpos_at_start = it->hpos;
18291 int saved_face_id = it->face_id;
18292 struct glyph_row *row = it->glyph_row;
18294 /* Initialize the iterator IT for iteration over STRING beginning
18295 with index START. */
18296 reseat_to_string (it, string, lisp_string, start,
18297 precision, field_width, multibyte);
18299 /* If displaying STRING, set up the face of the iterator
18300 from LISP_STRING, if that's given. */
18301 if (STRINGP (face_string))
18303 int endptr;
18304 struct face *face;
18306 it->face_id
18307 = face_at_string_position (it->w, face_string, face_string_pos,
18308 0, it->region_beg_charpos,
18309 it->region_end_charpos,
18310 &endptr, it->base_face_id, 0);
18311 face = FACE_FROM_ID (it->f, it->face_id);
18312 it->face_box_p = face->box != FACE_NO_BOX;
18315 /* Set max_x to the maximum allowed X position. Don't let it go
18316 beyond the right edge of the window. */
18317 if (max_x <= 0)
18318 max_x = it->last_visible_x;
18319 else
18320 max_x = min (max_x, it->last_visible_x);
18322 /* Skip over display elements that are not visible. because IT->w is
18323 hscrolled. */
18324 if (it->current_x < it->first_visible_x)
18325 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18326 MOVE_TO_POS | MOVE_TO_X);
18328 row->ascent = it->max_ascent;
18329 row->height = it->max_ascent + it->max_descent;
18330 row->phys_ascent = it->max_phys_ascent;
18331 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18332 row->extra_line_spacing = it->max_extra_line_spacing;
18334 /* This condition is for the case that we are called with current_x
18335 past last_visible_x. */
18336 while (it->current_x < max_x)
18338 int x_before, x, n_glyphs_before, i, nglyphs;
18340 /* Get the next display element. */
18341 if (!get_next_display_element (it))
18342 break;
18344 /* Produce glyphs. */
18345 x_before = it->current_x;
18346 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18347 PRODUCE_GLYPHS (it);
18349 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18350 i = 0;
18351 x = x_before;
18352 while (i < nglyphs)
18354 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18356 if (!it->truncate_lines_p
18357 && x + glyph->pixel_width > max_x)
18359 /* End of continued line or max_x reached. */
18360 if (CHAR_GLYPH_PADDING_P (*glyph))
18362 /* A wide character is unbreakable. */
18363 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18364 it->current_x = x_before;
18366 else
18368 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18369 it->current_x = x;
18371 break;
18373 else if (x + glyph->pixel_width > it->first_visible_x)
18375 /* Glyph is at least partially visible. */
18376 ++it->hpos;
18377 if (x < it->first_visible_x)
18378 it->glyph_row->x = x - it->first_visible_x;
18380 else
18382 /* Glyph is off the left margin of the display area.
18383 Should not happen. */
18384 abort ();
18387 row->ascent = max (row->ascent, it->max_ascent);
18388 row->height = max (row->height, it->max_ascent + it->max_descent);
18389 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18390 row->phys_height = max (row->phys_height,
18391 it->max_phys_ascent + it->max_phys_descent);
18392 row->extra_line_spacing = max (row->extra_line_spacing,
18393 it->max_extra_line_spacing);
18394 x += glyph->pixel_width;
18395 ++i;
18398 /* Stop if max_x reached. */
18399 if (i < nglyphs)
18400 break;
18402 /* Stop at line ends. */
18403 if (ITERATOR_AT_END_OF_LINE_P (it))
18405 it->continuation_lines_width = 0;
18406 break;
18409 set_iterator_to_next (it, 1);
18411 /* Stop if truncating at the right edge. */
18412 if (it->truncate_lines_p
18413 && it->current_x >= it->last_visible_x)
18415 /* Add truncation mark, but don't do it if the line is
18416 truncated at a padding space. */
18417 if (IT_CHARPOS (*it) < it->string_nchars)
18419 if (!FRAME_WINDOW_P (it->f))
18421 int i, n;
18423 if (it->current_x > it->last_visible_x)
18425 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18426 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18427 break;
18428 for (n = row->used[TEXT_AREA]; i < n; ++i)
18430 row->used[TEXT_AREA] = i;
18431 produce_special_glyphs (it, IT_TRUNCATION);
18434 produce_special_glyphs (it, IT_TRUNCATION);
18436 it->glyph_row->truncated_on_right_p = 1;
18438 break;
18442 /* Maybe insert a truncation at the left. */
18443 if (it->first_visible_x
18444 && IT_CHARPOS (*it) > 0)
18446 if (!FRAME_WINDOW_P (it->f))
18447 insert_left_trunc_glyphs (it);
18448 it->glyph_row->truncated_on_left_p = 1;
18451 it->face_id = saved_face_id;
18453 /* Value is number of columns displayed. */
18454 return it->hpos - hpos_at_start;
18459 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18460 appears as an element of LIST or as the car of an element of LIST.
18461 If PROPVAL is a list, compare each element against LIST in that
18462 way, and return 1/2 if any element of PROPVAL is found in LIST.
18463 Otherwise return 0. This function cannot quit.
18464 The return value is 2 if the text is invisible but with an ellipsis
18465 and 1 if it's invisible and without an ellipsis. */
18468 invisible_p (propval, list)
18469 register Lisp_Object propval;
18470 Lisp_Object list;
18472 register Lisp_Object tail, proptail;
18474 for (tail = list; CONSP (tail); tail = XCDR (tail))
18476 register Lisp_Object tem;
18477 tem = XCAR (tail);
18478 if (EQ (propval, tem))
18479 return 1;
18480 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18481 return NILP (XCDR (tem)) ? 1 : 2;
18484 if (CONSP (propval))
18486 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18488 Lisp_Object propelt;
18489 propelt = XCAR (proptail);
18490 for (tail = list; CONSP (tail); tail = XCDR (tail))
18492 register Lisp_Object tem;
18493 tem = XCAR (tail);
18494 if (EQ (propelt, tem))
18495 return 1;
18496 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18497 return NILP (XCDR (tem)) ? 1 : 2;
18502 return 0;
18505 /* Calculate a width or height in pixels from a specification using
18506 the following elements:
18508 SPEC ::=
18509 NUM - a (fractional) multiple of the default font width/height
18510 (NUM) - specifies exactly NUM pixels
18511 UNIT - a fixed number of pixels, see below.
18512 ELEMENT - size of a display element in pixels, see below.
18513 (NUM . SPEC) - equals NUM * SPEC
18514 (+ SPEC SPEC ...) - add pixel values
18515 (- SPEC SPEC ...) - subtract pixel values
18516 (- SPEC) - negate pixel value
18518 NUM ::=
18519 INT or FLOAT - a number constant
18520 SYMBOL - use symbol's (buffer local) variable binding.
18522 UNIT ::=
18523 in - pixels per inch *)
18524 mm - pixels per 1/1000 meter *)
18525 cm - pixels per 1/100 meter *)
18526 width - width of current font in pixels.
18527 height - height of current font in pixels.
18529 *) using the ratio(s) defined in display-pixels-per-inch.
18531 ELEMENT ::=
18533 left-fringe - left fringe width in pixels
18534 right-fringe - right fringe width in pixels
18536 left-margin - left margin width in pixels
18537 right-margin - right margin width in pixels
18539 scroll-bar - scroll-bar area width in pixels
18541 Examples:
18543 Pixels corresponding to 5 inches:
18544 (5 . in)
18546 Total width of non-text areas on left side of window (if scroll-bar is on left):
18547 '(space :width (+ left-fringe left-margin scroll-bar))
18549 Align to first text column (in header line):
18550 '(space :align-to 0)
18552 Align to middle of text area minus half the width of variable `my-image'
18553 containing a loaded image:
18554 '(space :align-to (0.5 . (- text my-image)))
18556 Width of left margin minus width of 1 character in the default font:
18557 '(space :width (- left-margin 1))
18559 Width of left margin minus width of 2 characters in the current font:
18560 '(space :width (- left-margin (2 . width)))
18562 Center 1 character over left-margin (in header line):
18563 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18565 Different ways to express width of left fringe plus left margin minus one pixel:
18566 '(space :width (- (+ left-fringe left-margin) (1)))
18567 '(space :width (+ left-fringe left-margin (- (1))))
18568 '(space :width (+ left-fringe left-margin (-1)))
18572 #define NUMVAL(X) \
18573 ((INTEGERP (X) || FLOATP (X)) \
18574 ? XFLOATINT (X) \
18575 : - 1)
18578 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18579 double *res;
18580 struct it *it;
18581 Lisp_Object prop;
18582 void *font;
18583 int width_p, *align_to;
18585 double pixels;
18587 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18588 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18590 if (NILP (prop))
18591 return OK_PIXELS (0);
18593 if (SYMBOLP (prop))
18595 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18597 char *unit = SDATA (SYMBOL_NAME (prop));
18599 if (unit[0] == 'i' && unit[1] == 'n')
18600 pixels = 1.0;
18601 else if (unit[0] == 'm' && unit[1] == 'm')
18602 pixels = 25.4;
18603 else if (unit[0] == 'c' && unit[1] == 'm')
18604 pixels = 2.54;
18605 else
18606 pixels = 0;
18607 if (pixels > 0)
18609 double ppi;
18610 #ifdef HAVE_WINDOW_SYSTEM
18611 if (FRAME_WINDOW_P (it->f)
18612 && (ppi = (width_p
18613 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18614 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18615 ppi > 0))
18616 return OK_PIXELS (ppi / pixels);
18617 #endif
18619 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18620 || (CONSP (Vdisplay_pixels_per_inch)
18621 && (ppi = (width_p
18622 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18623 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18624 ppi > 0)))
18625 return OK_PIXELS (ppi / pixels);
18627 return 0;
18631 #ifdef HAVE_WINDOW_SYSTEM
18632 if (EQ (prop, Qheight))
18633 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18634 if (EQ (prop, Qwidth))
18635 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18636 #else
18637 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18638 return OK_PIXELS (1);
18639 #endif
18641 if (EQ (prop, Qtext))
18642 return OK_PIXELS (width_p
18643 ? window_box_width (it->w, TEXT_AREA)
18644 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18646 if (align_to && *align_to < 0)
18648 *res = 0;
18649 if (EQ (prop, Qleft))
18650 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18651 if (EQ (prop, Qright))
18652 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18653 if (EQ (prop, Qcenter))
18654 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18655 + window_box_width (it->w, TEXT_AREA) / 2);
18656 if (EQ (prop, Qleft_fringe))
18657 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18658 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18659 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18660 if (EQ (prop, Qright_fringe))
18661 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18662 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18663 : window_box_right_offset (it->w, TEXT_AREA));
18664 if (EQ (prop, Qleft_margin))
18665 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18666 if (EQ (prop, Qright_margin))
18667 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18668 if (EQ (prop, Qscroll_bar))
18669 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18671 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18672 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18673 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18674 : 0)));
18676 else
18678 if (EQ (prop, Qleft_fringe))
18679 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18680 if (EQ (prop, Qright_fringe))
18681 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18682 if (EQ (prop, Qleft_margin))
18683 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18684 if (EQ (prop, Qright_margin))
18685 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18686 if (EQ (prop, Qscroll_bar))
18687 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18690 prop = Fbuffer_local_value (prop, it->w->buffer);
18693 if (INTEGERP (prop) || FLOATP (prop))
18695 int base_unit = (width_p
18696 ? FRAME_COLUMN_WIDTH (it->f)
18697 : FRAME_LINE_HEIGHT (it->f));
18698 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18701 if (CONSP (prop))
18703 Lisp_Object car = XCAR (prop);
18704 Lisp_Object cdr = XCDR (prop);
18706 if (SYMBOLP (car))
18708 #ifdef HAVE_WINDOW_SYSTEM
18709 if (valid_image_p (prop))
18711 int id = lookup_image (it->f, prop);
18712 struct image *img = IMAGE_FROM_ID (it->f, id);
18714 return OK_PIXELS (width_p ? img->width : img->height);
18716 #endif
18717 if (EQ (car, Qplus) || EQ (car, Qminus))
18719 int first = 1;
18720 double px;
18722 pixels = 0;
18723 while (CONSP (cdr))
18725 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18726 font, width_p, align_to))
18727 return 0;
18728 if (first)
18729 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18730 else
18731 pixels += px;
18732 cdr = XCDR (cdr);
18734 if (EQ (car, Qminus))
18735 pixels = -pixels;
18736 return OK_PIXELS (pixels);
18739 car = Fbuffer_local_value (car, it->w->buffer);
18742 if (INTEGERP (car) || FLOATP (car))
18744 double fact;
18745 pixels = XFLOATINT (car);
18746 if (NILP (cdr))
18747 return OK_PIXELS (pixels);
18748 if (calc_pixel_width_or_height (&fact, it, cdr,
18749 font, width_p, align_to))
18750 return OK_PIXELS (pixels * fact);
18751 return 0;
18754 return 0;
18757 return 0;
18761 /***********************************************************************
18762 Glyph Display
18763 ***********************************************************************/
18765 #ifdef HAVE_WINDOW_SYSTEM
18767 #if GLYPH_DEBUG
18769 void
18770 dump_glyph_string (s)
18771 struct glyph_string *s;
18773 fprintf (stderr, "glyph string\n");
18774 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18775 s->x, s->y, s->width, s->height);
18776 fprintf (stderr, " ybase = %d\n", s->ybase);
18777 fprintf (stderr, " hl = %d\n", s->hl);
18778 fprintf (stderr, " left overhang = %d, right = %d\n",
18779 s->left_overhang, s->right_overhang);
18780 fprintf (stderr, " nchars = %d\n", s->nchars);
18781 fprintf (stderr, " extends to end of line = %d\n",
18782 s->extends_to_end_of_line_p);
18783 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18784 fprintf (stderr, " bg width = %d\n", s->background_width);
18787 #endif /* GLYPH_DEBUG */
18789 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18790 of XChar2b structures for S; it can't be allocated in
18791 init_glyph_string because it must be allocated via `alloca'. W
18792 is the window on which S is drawn. ROW and AREA are the glyph row
18793 and area within the row from which S is constructed. START is the
18794 index of the first glyph structure covered by S. HL is a
18795 face-override for drawing S. */
18797 #ifdef HAVE_NTGUI
18798 #define OPTIONAL_HDC(hdc) hdc,
18799 #define DECLARE_HDC(hdc) HDC hdc;
18800 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18801 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18802 #endif
18804 #ifndef OPTIONAL_HDC
18805 #define OPTIONAL_HDC(hdc)
18806 #define DECLARE_HDC(hdc)
18807 #define ALLOCATE_HDC(hdc, f)
18808 #define RELEASE_HDC(hdc, f)
18809 #endif
18811 static void
18812 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18813 struct glyph_string *s;
18814 DECLARE_HDC (hdc)
18815 XChar2b *char2b;
18816 struct window *w;
18817 struct glyph_row *row;
18818 enum glyph_row_area area;
18819 int start;
18820 enum draw_glyphs_face hl;
18822 bzero (s, sizeof *s);
18823 s->w = w;
18824 s->f = XFRAME (w->frame);
18825 #ifdef HAVE_NTGUI
18826 s->hdc = hdc;
18827 #endif
18828 s->display = FRAME_X_DISPLAY (s->f);
18829 s->window = FRAME_X_WINDOW (s->f);
18830 s->char2b = char2b;
18831 s->hl = hl;
18832 s->row = row;
18833 s->area = area;
18834 s->first_glyph = row->glyphs[area] + start;
18835 s->height = row->height;
18836 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18838 /* Display the internal border below the tool-bar window. */
18839 if (WINDOWP (s->f->tool_bar_window)
18840 && s->w == XWINDOW (s->f->tool_bar_window))
18841 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18843 s->ybase = s->y + row->ascent;
18847 /* Append the list of glyph strings with head H and tail T to the list
18848 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18850 static INLINE void
18851 append_glyph_string_lists (head, tail, h, t)
18852 struct glyph_string **head, **tail;
18853 struct glyph_string *h, *t;
18855 if (h)
18857 if (*head)
18858 (*tail)->next = h;
18859 else
18860 *head = h;
18861 h->prev = *tail;
18862 *tail = t;
18867 /* Prepend the list of glyph strings with head H and tail T to the
18868 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18869 result. */
18871 static INLINE void
18872 prepend_glyph_string_lists (head, tail, h, t)
18873 struct glyph_string **head, **tail;
18874 struct glyph_string *h, *t;
18876 if (h)
18878 if (*head)
18879 (*head)->prev = t;
18880 else
18881 *tail = t;
18882 t->next = *head;
18883 *head = h;
18888 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18889 Set *HEAD and *TAIL to the resulting list. */
18891 static INLINE void
18892 append_glyph_string (head, tail, s)
18893 struct glyph_string **head, **tail;
18894 struct glyph_string *s;
18896 s->next = s->prev = NULL;
18897 append_glyph_string_lists (head, tail, s, s);
18901 /* Get face and two-byte form of character glyph GLYPH on frame F.
18902 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18903 a pointer to a realized face that is ready for display. */
18905 static INLINE struct face *
18906 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18907 struct frame *f;
18908 struct glyph *glyph;
18909 XChar2b *char2b;
18910 int *two_byte_p;
18912 struct face *face;
18914 xassert (glyph->type == CHAR_GLYPH);
18915 face = FACE_FROM_ID (f, glyph->face_id);
18917 if (two_byte_p)
18918 *two_byte_p = 0;
18920 if (!glyph->multibyte_p)
18922 /* Unibyte case. We don't have to encode, but we have to make
18923 sure to use a face suitable for unibyte. */
18924 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18926 else if (glyph->u.ch < 128)
18928 /* Case of ASCII in a face known to fit ASCII. */
18929 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18931 else
18933 int c1, c2, charset;
18935 /* Split characters into bytes. If c2 is -1 afterwards, C is
18936 really a one-byte character so that byte1 is zero. */
18937 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18938 if (c2 > 0)
18939 STORE_XCHAR2B (char2b, c1, c2);
18940 else
18941 STORE_XCHAR2B (char2b, 0, c1);
18943 /* Maybe encode the character in *CHAR2B. */
18944 if (charset != CHARSET_ASCII)
18946 struct font_info *font_info
18947 = FONT_INFO_FROM_ID (f, face->font_info_id);
18948 if (font_info)
18949 glyph->font_type
18950 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18954 /* Make sure X resources of the face are allocated. */
18955 xassert (face != NULL);
18956 PREPARE_FACE_FOR_DISPLAY (f, face);
18957 return face;
18961 /* Fill glyph string S with composition components specified by S->cmp.
18963 FACES is an array of faces for all components of this composition.
18964 S->gidx is the index of the first component for S.
18966 OVERLAPS non-zero means S should draw the foreground only, and use
18967 its physical height for clipping. See also draw_glyphs.
18969 Value is the index of a component not in S. */
18971 static int
18972 fill_composite_glyph_string (s, faces, overlaps)
18973 struct glyph_string *s;
18974 struct face **faces;
18975 int overlaps;
18977 int i;
18979 xassert (s);
18981 s->for_overlaps = overlaps;
18983 s->face = faces[s->gidx];
18984 s->font = s->face->font;
18985 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18987 /* For all glyphs of this composition, starting at the offset
18988 S->gidx, until we reach the end of the definition or encounter a
18989 glyph that requires the different face, add it to S. */
18990 ++s->nchars;
18991 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18992 ++s->nchars;
18994 /* All glyph strings for the same composition has the same width,
18995 i.e. the width set for the first component of the composition. */
18997 s->width = s->first_glyph->pixel_width;
18999 /* If the specified font could not be loaded, use the frame's
19000 default font, but record the fact that we couldn't load it in
19001 the glyph string so that we can draw rectangles for the
19002 characters of the glyph string. */
19003 if (s->font == NULL)
19005 s->font_not_found_p = 1;
19006 s->font = FRAME_FONT (s->f);
19009 /* Adjust base line for subscript/superscript text. */
19010 s->ybase += s->first_glyph->voffset;
19012 xassert (s->face && s->face->gc);
19014 /* This glyph string must always be drawn with 16-bit functions. */
19015 s->two_byte_p = 1;
19017 return s->gidx + s->nchars;
19021 /* Fill glyph string S from a sequence of character glyphs.
19023 FACE_ID is the face id of the string. START is the index of the
19024 first glyph to consider, END is the index of the last + 1.
19025 OVERLAPS non-zero means S should draw the foreground only, and use
19026 its physical height for clipping. See also draw_glyphs.
19028 Value is the index of the first glyph not in S. */
19030 static int
19031 fill_glyph_string (s, face_id, start, end, overlaps)
19032 struct glyph_string *s;
19033 int face_id;
19034 int start, end, overlaps;
19036 struct glyph *glyph, *last;
19037 int voffset;
19038 int glyph_not_available_p;
19040 xassert (s->f == XFRAME (s->w->frame));
19041 xassert (s->nchars == 0);
19042 xassert (start >= 0 && end > start);
19044 s->for_overlaps = overlaps,
19045 glyph = s->row->glyphs[s->area] + start;
19046 last = s->row->glyphs[s->area] + end;
19047 voffset = glyph->voffset;
19049 glyph_not_available_p = glyph->glyph_not_available_p;
19051 while (glyph < last
19052 && glyph->type == CHAR_GLYPH
19053 && glyph->voffset == voffset
19054 /* Same face id implies same font, nowadays. */
19055 && glyph->face_id == face_id
19056 && glyph->glyph_not_available_p == glyph_not_available_p)
19058 int two_byte_p;
19060 s->face = get_glyph_face_and_encoding (s->f, glyph,
19061 s->char2b + s->nchars,
19062 &two_byte_p);
19063 s->two_byte_p = two_byte_p;
19064 ++s->nchars;
19065 xassert (s->nchars <= end - start);
19066 s->width += glyph->pixel_width;
19067 ++glyph;
19070 s->font = s->face->font;
19071 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19073 /* If the specified font could not be loaded, use the frame's font,
19074 but record the fact that we couldn't load it in
19075 S->font_not_found_p so that we can draw rectangles for the
19076 characters of the glyph string. */
19077 if (s->font == NULL || glyph_not_available_p)
19079 s->font_not_found_p = 1;
19080 s->font = FRAME_FONT (s->f);
19083 /* Adjust base line for subscript/superscript text. */
19084 s->ybase += voffset;
19086 xassert (s->face && s->face->gc);
19087 return glyph - s->row->glyphs[s->area];
19091 /* Fill glyph string S from image glyph S->first_glyph. */
19093 static void
19094 fill_image_glyph_string (s)
19095 struct glyph_string *s;
19097 xassert (s->first_glyph->type == IMAGE_GLYPH);
19098 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19099 xassert (s->img);
19100 s->slice = s->first_glyph->slice;
19101 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19102 s->font = s->face->font;
19103 s->width = s->first_glyph->pixel_width;
19105 /* Adjust base line for subscript/superscript text. */
19106 s->ybase += s->first_glyph->voffset;
19110 /* Fill glyph string S from a sequence of stretch glyphs.
19112 ROW is the glyph row in which the glyphs are found, AREA is the
19113 area within the row. START is the index of the first glyph to
19114 consider, END is the index of the last + 1.
19116 Value is the index of the first glyph not in S. */
19118 static int
19119 fill_stretch_glyph_string (s, row, area, start, end)
19120 struct glyph_string *s;
19121 struct glyph_row *row;
19122 enum glyph_row_area area;
19123 int start, end;
19125 struct glyph *glyph, *last;
19126 int voffset, face_id;
19128 xassert (s->first_glyph->type == STRETCH_GLYPH);
19130 glyph = s->row->glyphs[s->area] + start;
19131 last = s->row->glyphs[s->area] + end;
19132 face_id = glyph->face_id;
19133 s->face = FACE_FROM_ID (s->f, face_id);
19134 s->font = s->face->font;
19135 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19136 s->width = glyph->pixel_width;
19137 s->nchars = 1;
19138 voffset = glyph->voffset;
19140 for (++glyph;
19141 (glyph < last
19142 && glyph->type == STRETCH_GLYPH
19143 && glyph->voffset == voffset
19144 && glyph->face_id == face_id);
19145 ++glyph)
19146 s->width += glyph->pixel_width;
19148 /* Adjust base line for subscript/superscript text. */
19149 s->ybase += voffset;
19151 /* The case that face->gc == 0 is handled when drawing the glyph
19152 string by calling PREPARE_FACE_FOR_DISPLAY. */
19153 xassert (s->face);
19154 return glyph - s->row->glyphs[s->area];
19158 /* EXPORT for RIF:
19159 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19160 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19161 assumed to be zero. */
19163 void
19164 x_get_glyph_overhangs (glyph, f, left, right)
19165 struct glyph *glyph;
19166 struct frame *f;
19167 int *left, *right;
19169 *left = *right = 0;
19171 if (glyph->type == CHAR_GLYPH)
19173 XFontStruct *font;
19174 struct face *face;
19175 struct font_info *font_info;
19176 XChar2b char2b;
19177 XCharStruct *pcm;
19179 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19180 font = face->font;
19181 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19182 if (font /* ++KFS: Should this be font_info ? */
19183 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19185 if (pcm->rbearing > pcm->width)
19186 *right = pcm->rbearing - pcm->width;
19187 if (pcm->lbearing < 0)
19188 *left = -pcm->lbearing;
19194 /* Return the index of the first glyph preceding glyph string S that
19195 is overwritten by S because of S's left overhang. Value is -1
19196 if no glyphs are overwritten. */
19198 static int
19199 left_overwritten (s)
19200 struct glyph_string *s;
19202 int k;
19204 if (s->left_overhang)
19206 int x = 0, i;
19207 struct glyph *glyphs = s->row->glyphs[s->area];
19208 int first = s->first_glyph - glyphs;
19210 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19211 x -= glyphs[i].pixel_width;
19213 k = i + 1;
19215 else
19216 k = -1;
19218 return k;
19222 /* Return the index of the first glyph preceding glyph string S that
19223 is overwriting S because of its right overhang. Value is -1 if no
19224 glyph in front of S overwrites S. */
19226 static int
19227 left_overwriting (s)
19228 struct glyph_string *s;
19230 int i, k, x;
19231 struct glyph *glyphs = s->row->glyphs[s->area];
19232 int first = s->first_glyph - glyphs;
19234 k = -1;
19235 x = 0;
19236 for (i = first - 1; i >= 0; --i)
19238 int left, right;
19239 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19240 if (x + right > 0)
19241 k = i;
19242 x -= glyphs[i].pixel_width;
19245 return k;
19249 /* Return the index of the last glyph following glyph string S that is
19250 not overwritten by S because of S's right overhang. Value is -1 if
19251 no such glyph is found. */
19253 static int
19254 right_overwritten (s)
19255 struct glyph_string *s;
19257 int k = -1;
19259 if (s->right_overhang)
19261 int x = 0, i;
19262 struct glyph *glyphs = s->row->glyphs[s->area];
19263 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19264 int end = s->row->used[s->area];
19266 for (i = first; i < end && s->right_overhang > x; ++i)
19267 x += glyphs[i].pixel_width;
19269 k = i;
19272 return k;
19276 /* Return the index of the last glyph following glyph string S that
19277 overwrites S because of its left overhang. Value is negative
19278 if no such glyph is found. */
19280 static int
19281 right_overwriting (s)
19282 struct glyph_string *s;
19284 int i, k, x;
19285 int end = s->row->used[s->area];
19286 struct glyph *glyphs = s->row->glyphs[s->area];
19287 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19289 k = -1;
19290 x = 0;
19291 for (i = first; i < end; ++i)
19293 int left, right;
19294 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19295 if (x - left < 0)
19296 k = i;
19297 x += glyphs[i].pixel_width;
19300 return k;
19304 /* Get face and two-byte form of character C in face FACE_ID on frame
19305 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19306 means we want to display multibyte text. DISPLAY_P non-zero means
19307 make sure that X resources for the face returned are allocated.
19308 Value is a pointer to a realized face that is ready for display if
19309 DISPLAY_P is non-zero. */
19311 static INLINE struct face *
19312 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19313 struct frame *f;
19314 int c, face_id;
19315 XChar2b *char2b;
19316 int multibyte_p, display_p;
19318 struct face *face = FACE_FROM_ID (f, face_id);
19320 if (!multibyte_p)
19322 /* Unibyte case. We don't have to encode, but we have to make
19323 sure to use a face suitable for unibyte. */
19324 STORE_XCHAR2B (char2b, 0, c);
19325 face_id = FACE_FOR_CHAR (f, face, c);
19326 face = FACE_FROM_ID (f, face_id);
19328 else if (c < 128)
19330 /* Case of ASCII in a face known to fit ASCII. */
19331 STORE_XCHAR2B (char2b, 0, c);
19333 else
19335 int c1, c2, charset;
19337 /* Split characters into bytes. If c2 is -1 afterwards, C is
19338 really a one-byte character so that byte1 is zero. */
19339 SPLIT_CHAR (c, charset, c1, c2);
19340 if (c2 > 0)
19341 STORE_XCHAR2B (char2b, c1, c2);
19342 else
19343 STORE_XCHAR2B (char2b, 0, c1);
19345 /* Maybe encode the character in *CHAR2B. */
19346 if (face->font != NULL)
19348 struct font_info *font_info
19349 = FONT_INFO_FROM_ID (f, face->font_info_id);
19350 if (font_info)
19351 rif->encode_char (c, char2b, font_info, 0);
19355 /* Make sure X resources of the face are allocated. */
19356 #ifdef HAVE_X_WINDOWS
19357 if (display_p)
19358 #endif
19360 xassert (face != NULL);
19361 PREPARE_FACE_FOR_DISPLAY (f, face);
19364 return face;
19368 /* Set background width of glyph string S. START is the index of the
19369 first glyph following S. LAST_X is the right-most x-position + 1
19370 in the drawing area. */
19372 static INLINE void
19373 set_glyph_string_background_width (s, start, last_x)
19374 struct glyph_string *s;
19375 int start;
19376 int last_x;
19378 /* If the face of this glyph string has to be drawn to the end of
19379 the drawing area, set S->extends_to_end_of_line_p. */
19381 if (start == s->row->used[s->area]
19382 && s->area == TEXT_AREA
19383 && ((s->row->fill_line_p
19384 && (s->hl == DRAW_NORMAL_TEXT
19385 || s->hl == DRAW_IMAGE_RAISED
19386 || s->hl == DRAW_IMAGE_SUNKEN))
19387 || s->hl == DRAW_MOUSE_FACE))
19388 s->extends_to_end_of_line_p = 1;
19390 /* If S extends its face to the end of the line, set its
19391 background_width to the distance to the right edge of the drawing
19392 area. */
19393 if (s->extends_to_end_of_line_p)
19394 s->background_width = last_x - s->x + 1;
19395 else
19396 s->background_width = s->width;
19400 /* Compute overhangs and x-positions for glyph string S and its
19401 predecessors, or successors. X is the starting x-position for S.
19402 BACKWARD_P non-zero means process predecessors. */
19404 static void
19405 compute_overhangs_and_x (s, x, backward_p)
19406 struct glyph_string *s;
19407 int x;
19408 int backward_p;
19410 if (backward_p)
19412 while (s)
19414 if (rif->compute_glyph_string_overhangs)
19415 rif->compute_glyph_string_overhangs (s);
19416 x -= s->width;
19417 s->x = x;
19418 s = s->prev;
19421 else
19423 while (s)
19425 if (rif->compute_glyph_string_overhangs)
19426 rif->compute_glyph_string_overhangs (s);
19427 s->x = x;
19428 x += s->width;
19429 s = s->next;
19436 /* The following macros are only called from draw_glyphs below.
19437 They reference the following parameters of that function directly:
19438 `w', `row', `area', and `overlap_p'
19439 as well as the following local variables:
19440 `s', `f', and `hdc' (in W32) */
19442 #ifdef HAVE_NTGUI
19443 /* On W32, silently add local `hdc' variable to argument list of
19444 init_glyph_string. */
19445 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19446 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19447 #else
19448 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19449 init_glyph_string (s, char2b, w, row, area, start, hl)
19450 #endif
19452 /* Add a glyph string for a stretch glyph to the list of strings
19453 between HEAD and TAIL. START is the index of the stretch glyph in
19454 row area AREA of glyph row ROW. END is the index of the last glyph
19455 in that glyph row area. X is the current output position assigned
19456 to the new glyph string constructed. HL overrides that face of the
19457 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19458 is the right-most x-position of the drawing area. */
19460 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19461 and below -- keep them on one line. */
19462 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19463 do \
19465 s = (struct glyph_string *) alloca (sizeof *s); \
19466 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19467 START = fill_stretch_glyph_string (s, row, area, START, END); \
19468 append_glyph_string (&HEAD, &TAIL, s); \
19469 s->x = (X); \
19471 while (0)
19474 /* Add a glyph string for an image glyph to the list of strings
19475 between HEAD and TAIL. START is the index of the image glyph in
19476 row area AREA of glyph row ROW. END is the index of the last glyph
19477 in that glyph row area. X is the current output position assigned
19478 to the new glyph string constructed. HL overrides that face of the
19479 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19480 is the right-most x-position of the drawing area. */
19482 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19483 do \
19485 s = (struct glyph_string *) alloca (sizeof *s); \
19486 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19487 fill_image_glyph_string (s); \
19488 append_glyph_string (&HEAD, &TAIL, s); \
19489 ++START; \
19490 s->x = (X); \
19492 while (0)
19495 /* Add a glyph string for a sequence of character glyphs to the list
19496 of strings between HEAD and TAIL. START is the index of the first
19497 glyph in row area AREA of glyph row ROW that is part of the new
19498 glyph string. END is the index of the last glyph in that glyph row
19499 area. X is the current output position assigned to the new glyph
19500 string constructed. HL overrides that face of the glyph; e.g. it
19501 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19502 right-most x-position of the drawing area. */
19504 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19505 do \
19507 int c, face_id; \
19508 XChar2b *char2b; \
19510 c = (row)->glyphs[area][START].u.ch; \
19511 face_id = (row)->glyphs[area][START].face_id; \
19513 s = (struct glyph_string *) alloca (sizeof *s); \
19514 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19515 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19516 append_glyph_string (&HEAD, &TAIL, s); \
19517 s->x = (X); \
19518 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19520 while (0)
19523 /* Add a glyph string for a composite sequence to the list of strings
19524 between HEAD and TAIL. START is the index of the first glyph in
19525 row area AREA of glyph row ROW that is part of the new glyph
19526 string. END is the index of the last glyph in that glyph row area.
19527 X is the current output position assigned to the new glyph string
19528 constructed. HL overrides that face of the glyph; e.g. it is
19529 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19530 x-position of the drawing area. */
19532 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19533 do { \
19534 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19535 int face_id = (row)->glyphs[area][START].face_id; \
19536 struct face *base_face = FACE_FROM_ID (f, face_id); \
19537 struct composition *cmp = composition_table[cmp_id]; \
19538 int glyph_len = cmp->glyph_len; \
19539 XChar2b *char2b; \
19540 struct face **faces; \
19541 struct glyph_string *first_s = NULL; \
19542 int n; \
19544 base_face = base_face->ascii_face; \
19545 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19546 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19547 /* At first, fill in `char2b' and `faces'. */ \
19548 for (n = 0; n < glyph_len; n++) \
19550 int c = COMPOSITION_GLYPH (cmp, n); \
19551 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19552 faces[n] = FACE_FROM_ID (f, this_face_id); \
19553 get_char_face_and_encoding (f, c, this_face_id, \
19554 char2b + n, 1, 1); \
19557 /* Make glyph_strings for each glyph sequence that is drawable by \
19558 the same face, and append them to HEAD/TAIL. */ \
19559 for (n = 0; n < cmp->glyph_len;) \
19561 s = (struct glyph_string *) alloca (sizeof *s); \
19562 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19563 append_glyph_string (&(HEAD), &(TAIL), s); \
19564 s->cmp = cmp; \
19565 s->gidx = n; \
19566 s->x = (X); \
19568 if (n == 0) \
19569 first_s = s; \
19571 n = fill_composite_glyph_string (s, faces, overlaps); \
19574 ++START; \
19575 s = first_s; \
19576 } while (0)
19579 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19580 of AREA of glyph row ROW on window W between indices START and END.
19581 HL overrides the face for drawing glyph strings, e.g. it is
19582 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19583 x-positions of the drawing area.
19585 This is an ugly monster macro construct because we must use alloca
19586 to allocate glyph strings (because draw_glyphs can be called
19587 asynchronously). */
19589 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19590 do \
19592 HEAD = TAIL = NULL; \
19593 while (START < END) \
19595 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19596 switch (first_glyph->type) \
19598 case CHAR_GLYPH: \
19599 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19600 HL, X, LAST_X); \
19601 break; \
19603 case COMPOSITE_GLYPH: \
19604 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19605 HL, X, LAST_X); \
19606 break; \
19608 case STRETCH_GLYPH: \
19609 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19610 HL, X, LAST_X); \
19611 break; \
19613 case IMAGE_GLYPH: \
19614 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19615 HL, X, LAST_X); \
19616 break; \
19618 default: \
19619 abort (); \
19622 set_glyph_string_background_width (s, START, LAST_X); \
19623 (X) += s->width; \
19626 while (0)
19629 /* Draw glyphs between START and END in AREA of ROW on window W,
19630 starting at x-position X. X is relative to AREA in W. HL is a
19631 face-override with the following meaning:
19633 DRAW_NORMAL_TEXT draw normally
19634 DRAW_CURSOR draw in cursor face
19635 DRAW_MOUSE_FACE draw in mouse face.
19636 DRAW_INVERSE_VIDEO draw in mode line face
19637 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19638 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19640 If OVERLAPS is non-zero, draw only the foreground of characters and
19641 clip to the physical height of ROW. Non-zero value also defines
19642 the overlapping part to be drawn:
19644 OVERLAPS_PRED overlap with preceding rows
19645 OVERLAPS_SUCC overlap with succeeding rows
19646 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19647 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19649 Value is the x-position reached, relative to AREA of W. */
19651 static int
19652 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19653 struct window *w;
19654 int x;
19655 struct glyph_row *row;
19656 enum glyph_row_area area;
19657 int start, end;
19658 enum draw_glyphs_face hl;
19659 int overlaps;
19661 struct glyph_string *head, *tail;
19662 struct glyph_string *s;
19663 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19664 int last_x, area_width;
19665 int x_reached;
19666 int i, j;
19667 struct frame *f = XFRAME (WINDOW_FRAME (w));
19668 DECLARE_HDC (hdc);
19670 ALLOCATE_HDC (hdc, f);
19672 /* Let's rather be paranoid than getting a SEGV. */
19673 end = min (end, row->used[area]);
19674 start = max (0, start);
19675 start = min (end, start);
19677 /* Translate X to frame coordinates. Set last_x to the right
19678 end of the drawing area. */
19679 if (row->full_width_p)
19681 /* X is relative to the left edge of W, without scroll bars
19682 or fringes. */
19683 x += WINDOW_LEFT_EDGE_X (w);
19684 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19686 else
19688 int area_left = window_box_left (w, area);
19689 x += area_left;
19690 area_width = window_box_width (w, area);
19691 last_x = area_left + area_width;
19694 /* Build a doubly-linked list of glyph_string structures between
19695 head and tail from what we have to draw. Note that the macro
19696 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19697 the reason we use a separate variable `i'. */
19698 i = start;
19699 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19700 if (tail)
19701 x_reached = tail->x + tail->background_width;
19702 else
19703 x_reached = x;
19705 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19706 the row, redraw some glyphs in front or following the glyph
19707 strings built above. */
19708 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19710 int dummy_x = 0;
19711 struct glyph_string *h, *t;
19713 /* Compute overhangs for all glyph strings. */
19714 if (rif->compute_glyph_string_overhangs)
19715 for (s = head; s; s = s->next)
19716 rif->compute_glyph_string_overhangs (s);
19718 /* Prepend glyph strings for glyphs in front of the first glyph
19719 string that are overwritten because of the first glyph
19720 string's left overhang. The background of all strings
19721 prepended must be drawn because the first glyph string
19722 draws over it. */
19723 i = left_overwritten (head);
19724 if (i >= 0)
19726 j = i;
19727 BUILD_GLYPH_STRINGS (j, start, h, t,
19728 DRAW_NORMAL_TEXT, dummy_x, last_x);
19729 start = i;
19730 compute_overhangs_and_x (t, head->x, 1);
19731 prepend_glyph_string_lists (&head, &tail, h, t);
19732 clip_head = head;
19735 /* Prepend glyph strings for glyphs in front of the first glyph
19736 string that overwrite that glyph string because of their
19737 right overhang. For these strings, only the foreground must
19738 be drawn, because it draws over the glyph string at `head'.
19739 The background must not be drawn because this would overwrite
19740 right overhangs of preceding glyphs for which no glyph
19741 strings exist. */
19742 i = left_overwriting (head);
19743 if (i >= 0)
19745 clip_head = head;
19746 BUILD_GLYPH_STRINGS (i, start, h, t,
19747 DRAW_NORMAL_TEXT, dummy_x, last_x);
19748 for (s = h; s; s = s->next)
19749 s->background_filled_p = 1;
19750 compute_overhangs_and_x (t, head->x, 1);
19751 prepend_glyph_string_lists (&head, &tail, h, t);
19754 /* Append glyphs strings for glyphs following the last glyph
19755 string tail that are overwritten by tail. The background of
19756 these strings has to be drawn because tail's foreground draws
19757 over it. */
19758 i = right_overwritten (tail);
19759 if (i >= 0)
19761 BUILD_GLYPH_STRINGS (end, i, h, t,
19762 DRAW_NORMAL_TEXT, x, last_x);
19763 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19764 append_glyph_string_lists (&head, &tail, h, t);
19765 clip_tail = tail;
19768 /* Append glyph strings for glyphs following the last glyph
19769 string tail that overwrite tail. The foreground of such
19770 glyphs has to be drawn because it writes into the background
19771 of tail. The background must not be drawn because it could
19772 paint over the foreground of following glyphs. */
19773 i = right_overwriting (tail);
19774 if (i >= 0)
19776 clip_tail = tail;
19777 BUILD_GLYPH_STRINGS (end, i, h, t,
19778 DRAW_NORMAL_TEXT, x, last_x);
19779 for (s = h; s; s = s->next)
19780 s->background_filled_p = 1;
19781 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19782 append_glyph_string_lists (&head, &tail, h, t);
19784 if (clip_head || clip_tail)
19785 for (s = head; s; s = s->next)
19787 s->clip_head = clip_head;
19788 s->clip_tail = clip_tail;
19792 /* Draw all strings. */
19793 for (s = head; s; s = s->next)
19794 rif->draw_glyph_string (s);
19796 if (area == TEXT_AREA
19797 && !row->full_width_p
19798 /* When drawing overlapping rows, only the glyph strings'
19799 foreground is drawn, which doesn't erase a cursor
19800 completely. */
19801 && !overlaps)
19803 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19804 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19805 : (tail ? tail->x + tail->background_width : x));
19807 int text_left = window_box_left (w, TEXT_AREA);
19808 x0 -= text_left;
19809 x1 -= text_left;
19811 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19812 row->y, MATRIX_ROW_BOTTOM_Y (row));
19815 /* Value is the x-position up to which drawn, relative to AREA of W.
19816 This doesn't include parts drawn because of overhangs. */
19817 if (row->full_width_p)
19818 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19819 else
19820 x_reached -= window_box_left (w, area);
19822 RELEASE_HDC (hdc, f);
19824 return x_reached;
19827 /* Expand row matrix if too narrow. Don't expand if area
19828 is not present. */
19830 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19832 if (!fonts_changed_p \
19833 && (it->glyph_row->glyphs[area] \
19834 < it->glyph_row->glyphs[area + 1])) \
19836 it->w->ncols_scale_factor++; \
19837 fonts_changed_p = 1; \
19841 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19842 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19844 static INLINE void
19845 append_glyph (it)
19846 struct it *it;
19848 struct glyph *glyph;
19849 enum glyph_row_area area = it->area;
19851 xassert (it->glyph_row);
19852 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19854 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19855 if (glyph < it->glyph_row->glyphs[area + 1])
19857 glyph->charpos = CHARPOS (it->position);
19858 glyph->object = it->object;
19859 glyph->pixel_width = it->pixel_width;
19860 glyph->ascent = it->ascent;
19861 glyph->descent = it->descent;
19862 glyph->voffset = it->voffset;
19863 glyph->type = CHAR_GLYPH;
19864 glyph->multibyte_p = it->multibyte_p;
19865 glyph->left_box_line_p = it->start_of_box_run_p;
19866 glyph->right_box_line_p = it->end_of_box_run_p;
19867 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19868 || it->phys_descent > it->descent);
19869 glyph->padding_p = 0;
19870 glyph->glyph_not_available_p = it->glyph_not_available_p;
19871 glyph->face_id = it->face_id;
19872 glyph->u.ch = it->char_to_display;
19873 glyph->slice = null_glyph_slice;
19874 glyph->font_type = FONT_TYPE_UNKNOWN;
19875 ++it->glyph_row->used[area];
19877 else
19878 IT_EXPAND_MATRIX_WIDTH (it, area);
19881 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19882 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19884 static INLINE void
19885 append_composite_glyph (it)
19886 struct it *it;
19888 struct glyph *glyph;
19889 enum glyph_row_area area = it->area;
19891 xassert (it->glyph_row);
19893 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19894 if (glyph < it->glyph_row->glyphs[area + 1])
19896 glyph->charpos = CHARPOS (it->position);
19897 glyph->object = it->object;
19898 glyph->pixel_width = it->pixel_width;
19899 glyph->ascent = it->ascent;
19900 glyph->descent = it->descent;
19901 glyph->voffset = it->voffset;
19902 glyph->type = COMPOSITE_GLYPH;
19903 glyph->multibyte_p = it->multibyte_p;
19904 glyph->left_box_line_p = it->start_of_box_run_p;
19905 glyph->right_box_line_p = it->end_of_box_run_p;
19906 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19907 || it->phys_descent > it->descent);
19908 glyph->padding_p = 0;
19909 glyph->glyph_not_available_p = 0;
19910 glyph->face_id = it->face_id;
19911 glyph->u.cmp_id = it->cmp_id;
19912 glyph->slice = null_glyph_slice;
19913 glyph->font_type = FONT_TYPE_UNKNOWN;
19914 ++it->glyph_row->used[area];
19916 else
19917 IT_EXPAND_MATRIX_WIDTH (it, area);
19921 /* Change IT->ascent and IT->height according to the setting of
19922 IT->voffset. */
19924 static INLINE void
19925 take_vertical_position_into_account (it)
19926 struct it *it;
19928 if (it->voffset)
19930 if (it->voffset < 0)
19931 /* Increase the ascent so that we can display the text higher
19932 in the line. */
19933 it->ascent -= it->voffset;
19934 else
19935 /* Increase the descent so that we can display the text lower
19936 in the line. */
19937 it->descent += it->voffset;
19942 /* Produce glyphs/get display metrics for the image IT is loaded with.
19943 See the description of struct display_iterator in dispextern.h for
19944 an overview of struct display_iterator. */
19946 static void
19947 produce_image_glyph (it)
19948 struct it *it;
19950 struct image *img;
19951 struct face *face;
19952 int glyph_ascent, crop;
19953 struct glyph_slice slice;
19955 xassert (it->what == IT_IMAGE);
19957 face = FACE_FROM_ID (it->f, it->face_id);
19958 xassert (face);
19959 /* Make sure X resources of the face is loaded. */
19960 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19962 if (it->image_id < 0)
19964 /* Fringe bitmap. */
19965 it->ascent = it->phys_ascent = 0;
19966 it->descent = it->phys_descent = 0;
19967 it->pixel_width = 0;
19968 it->nglyphs = 0;
19969 return;
19972 img = IMAGE_FROM_ID (it->f, it->image_id);
19973 xassert (img);
19974 /* Make sure X resources of the image is loaded. */
19975 prepare_image_for_display (it->f, img);
19977 slice.x = slice.y = 0;
19978 slice.width = img->width;
19979 slice.height = img->height;
19981 if (INTEGERP (it->slice.x))
19982 slice.x = XINT (it->slice.x);
19983 else if (FLOATP (it->slice.x))
19984 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19986 if (INTEGERP (it->slice.y))
19987 slice.y = XINT (it->slice.y);
19988 else if (FLOATP (it->slice.y))
19989 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19991 if (INTEGERP (it->slice.width))
19992 slice.width = XINT (it->slice.width);
19993 else if (FLOATP (it->slice.width))
19994 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19996 if (INTEGERP (it->slice.height))
19997 slice.height = XINT (it->slice.height);
19998 else if (FLOATP (it->slice.height))
19999 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20001 if (slice.x >= img->width)
20002 slice.x = img->width;
20003 if (slice.y >= img->height)
20004 slice.y = img->height;
20005 if (slice.x + slice.width >= img->width)
20006 slice.width = img->width - slice.x;
20007 if (slice.y + slice.height > img->height)
20008 slice.height = img->height - slice.y;
20010 if (slice.width == 0 || slice.height == 0)
20011 return;
20013 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20015 it->descent = slice.height - glyph_ascent;
20016 if (slice.y == 0)
20017 it->descent += img->vmargin;
20018 if (slice.y + slice.height == img->height)
20019 it->descent += img->vmargin;
20020 it->phys_descent = it->descent;
20022 it->pixel_width = slice.width;
20023 if (slice.x == 0)
20024 it->pixel_width += img->hmargin;
20025 if (slice.x + slice.width == img->width)
20026 it->pixel_width += img->hmargin;
20028 /* It's quite possible for images to have an ascent greater than
20029 their height, so don't get confused in that case. */
20030 if (it->descent < 0)
20031 it->descent = 0;
20033 #if 0 /* this breaks image tiling */
20034 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20035 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20036 if (face_ascent > it->ascent)
20037 it->ascent = it->phys_ascent = face_ascent;
20038 #endif
20040 it->nglyphs = 1;
20042 if (face->box != FACE_NO_BOX)
20044 if (face->box_line_width > 0)
20046 if (slice.y == 0)
20047 it->ascent += face->box_line_width;
20048 if (slice.y + slice.height == img->height)
20049 it->descent += face->box_line_width;
20052 if (it->start_of_box_run_p && slice.x == 0)
20053 it->pixel_width += abs (face->box_line_width);
20054 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20055 it->pixel_width += abs (face->box_line_width);
20058 take_vertical_position_into_account (it);
20060 /* Automatically crop wide image glyphs at right edge so we can
20061 draw the cursor on same display row. */
20062 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20063 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20065 it->pixel_width -= crop;
20066 slice.width -= crop;
20069 if (it->glyph_row)
20071 struct glyph *glyph;
20072 enum glyph_row_area area = it->area;
20074 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20075 if (glyph < it->glyph_row->glyphs[area + 1])
20077 glyph->charpos = CHARPOS (it->position);
20078 glyph->object = it->object;
20079 glyph->pixel_width = it->pixel_width;
20080 glyph->ascent = glyph_ascent;
20081 glyph->descent = it->descent;
20082 glyph->voffset = it->voffset;
20083 glyph->type = IMAGE_GLYPH;
20084 glyph->multibyte_p = it->multibyte_p;
20085 glyph->left_box_line_p = it->start_of_box_run_p;
20086 glyph->right_box_line_p = it->end_of_box_run_p;
20087 glyph->overlaps_vertically_p = 0;
20088 glyph->padding_p = 0;
20089 glyph->glyph_not_available_p = 0;
20090 glyph->face_id = it->face_id;
20091 glyph->u.img_id = img->id;
20092 glyph->slice = slice;
20093 glyph->font_type = FONT_TYPE_UNKNOWN;
20094 ++it->glyph_row->used[area];
20096 else
20097 IT_EXPAND_MATRIX_WIDTH (it, area);
20102 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20103 of the glyph, WIDTH and HEIGHT are the width and height of the
20104 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20106 static void
20107 append_stretch_glyph (it, object, width, height, ascent)
20108 struct it *it;
20109 Lisp_Object object;
20110 int width, height;
20111 int ascent;
20113 struct glyph *glyph;
20114 enum glyph_row_area area = it->area;
20116 xassert (ascent >= 0 && ascent <= height);
20118 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20119 if (glyph < it->glyph_row->glyphs[area + 1])
20121 glyph->charpos = CHARPOS (it->position);
20122 glyph->object = object;
20123 glyph->pixel_width = width;
20124 glyph->ascent = ascent;
20125 glyph->descent = height - ascent;
20126 glyph->voffset = it->voffset;
20127 glyph->type = STRETCH_GLYPH;
20128 glyph->multibyte_p = it->multibyte_p;
20129 glyph->left_box_line_p = it->start_of_box_run_p;
20130 glyph->right_box_line_p = it->end_of_box_run_p;
20131 glyph->overlaps_vertically_p = 0;
20132 glyph->padding_p = 0;
20133 glyph->glyph_not_available_p = 0;
20134 glyph->face_id = it->face_id;
20135 glyph->u.stretch.ascent = ascent;
20136 glyph->u.stretch.height = height;
20137 glyph->slice = null_glyph_slice;
20138 glyph->font_type = FONT_TYPE_UNKNOWN;
20139 ++it->glyph_row->used[area];
20141 else
20142 IT_EXPAND_MATRIX_WIDTH (it, area);
20146 /* Produce a stretch glyph for iterator IT. IT->object is the value
20147 of the glyph property displayed. The value must be a list
20148 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20149 being recognized:
20151 1. `:width WIDTH' specifies that the space should be WIDTH *
20152 canonical char width wide. WIDTH may be an integer or floating
20153 point number.
20155 2. `:relative-width FACTOR' specifies that the width of the stretch
20156 should be computed from the width of the first character having the
20157 `glyph' property, and should be FACTOR times that width.
20159 3. `:align-to HPOS' specifies that the space should be wide enough
20160 to reach HPOS, a value in canonical character units.
20162 Exactly one of the above pairs must be present.
20164 4. `:height HEIGHT' specifies that the height of the stretch produced
20165 should be HEIGHT, measured in canonical character units.
20167 5. `:relative-height FACTOR' specifies that the height of the
20168 stretch should be FACTOR times the height of the characters having
20169 the glyph property.
20171 Either none or exactly one of 4 or 5 must be present.
20173 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20174 of the stretch should be used for the ascent of the stretch.
20175 ASCENT must be in the range 0 <= ASCENT <= 100. */
20177 static void
20178 produce_stretch_glyph (it)
20179 struct it *it;
20181 /* (space :width WIDTH :height HEIGHT ...) */
20182 Lisp_Object prop, plist;
20183 int width = 0, height = 0, align_to = -1;
20184 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20185 int ascent = 0;
20186 double tem;
20187 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20188 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20190 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20192 /* List should start with `space'. */
20193 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20194 plist = XCDR (it->object);
20196 /* Compute the width of the stretch. */
20197 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20198 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20200 /* Absolute width `:width WIDTH' specified and valid. */
20201 zero_width_ok_p = 1;
20202 width = (int)tem;
20204 else if (prop = Fplist_get (plist, QCrelative_width),
20205 NUMVAL (prop) > 0)
20207 /* Relative width `:relative-width FACTOR' specified and valid.
20208 Compute the width of the characters having the `glyph'
20209 property. */
20210 struct it it2;
20211 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20213 it2 = *it;
20214 if (it->multibyte_p)
20216 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20217 - IT_BYTEPOS (*it));
20218 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20220 else
20221 it2.c = *p, it2.len = 1;
20223 it2.glyph_row = NULL;
20224 it2.what = IT_CHARACTER;
20225 x_produce_glyphs (&it2);
20226 width = NUMVAL (prop) * it2.pixel_width;
20228 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20229 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20231 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20232 align_to = (align_to < 0
20234 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20235 else if (align_to < 0)
20236 align_to = window_box_left_offset (it->w, TEXT_AREA);
20237 width = max (0, (int)tem + align_to - it->current_x);
20238 zero_width_ok_p = 1;
20240 else
20241 /* Nothing specified -> width defaults to canonical char width. */
20242 width = FRAME_COLUMN_WIDTH (it->f);
20244 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20245 width = 1;
20247 /* Compute height. */
20248 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20249 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20251 height = (int)tem;
20252 zero_height_ok_p = 1;
20254 else if (prop = Fplist_get (plist, QCrelative_height),
20255 NUMVAL (prop) > 0)
20256 height = FONT_HEIGHT (font) * NUMVAL (prop);
20257 else
20258 height = FONT_HEIGHT (font);
20260 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20261 height = 1;
20263 /* Compute percentage of height used for ascent. If
20264 `:ascent ASCENT' is present and valid, use that. Otherwise,
20265 derive the ascent from the font in use. */
20266 if (prop = Fplist_get (plist, QCascent),
20267 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20268 ascent = height * NUMVAL (prop) / 100.0;
20269 else if (!NILP (prop)
20270 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20271 ascent = min (max (0, (int)tem), height);
20272 else
20273 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20275 if (width > 0 && !it->truncate_lines_p
20276 && it->current_x + width > it->last_visible_x)
20277 width = it->last_visible_x - it->current_x - 1;
20279 if (width > 0 && height > 0 && it->glyph_row)
20281 Lisp_Object object = it->stack[it->sp - 1].string;
20282 if (!STRINGP (object))
20283 object = it->w->buffer;
20284 append_stretch_glyph (it, object, width, height, ascent);
20287 it->pixel_width = width;
20288 it->ascent = it->phys_ascent = ascent;
20289 it->descent = it->phys_descent = height - it->ascent;
20290 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20292 take_vertical_position_into_account (it);
20295 /* Get line-height and line-spacing property at point.
20296 If line-height has format (HEIGHT TOTAL), return TOTAL
20297 in TOTAL_HEIGHT. */
20299 static Lisp_Object
20300 get_line_height_property (it, prop)
20301 struct it *it;
20302 Lisp_Object prop;
20304 Lisp_Object position;
20306 if (STRINGP (it->object))
20307 position = make_number (IT_STRING_CHARPOS (*it));
20308 else if (BUFFERP (it->object))
20309 position = make_number (IT_CHARPOS (*it));
20310 else
20311 return Qnil;
20313 return Fget_char_property (position, prop, it->object);
20316 /* Calculate line-height and line-spacing properties.
20317 An integer value specifies explicit pixel value.
20318 A float value specifies relative value to current face height.
20319 A cons (float . face-name) specifies relative value to
20320 height of specified face font.
20322 Returns height in pixels, or nil. */
20325 static Lisp_Object
20326 calc_line_height_property (it, val, font, boff, override)
20327 struct it *it;
20328 Lisp_Object val;
20329 XFontStruct *font;
20330 int boff, override;
20332 Lisp_Object face_name = Qnil;
20333 int ascent, descent, height;
20335 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20336 return val;
20338 if (CONSP (val))
20340 face_name = XCAR (val);
20341 val = XCDR (val);
20342 if (!NUMBERP (val))
20343 val = make_number (1);
20344 if (NILP (face_name))
20346 height = it->ascent + it->descent;
20347 goto scale;
20351 if (NILP (face_name))
20353 font = FRAME_FONT (it->f);
20354 boff = FRAME_BASELINE_OFFSET (it->f);
20356 else if (EQ (face_name, Qt))
20358 override = 0;
20360 else
20362 int face_id;
20363 struct face *face;
20364 struct font_info *font_info;
20366 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20367 if (face_id < 0)
20368 return make_number (-1);
20370 face = FACE_FROM_ID (it->f, face_id);
20371 font = face->font;
20372 if (font == NULL)
20373 return make_number (-1);
20375 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20376 boff = font_info->baseline_offset;
20377 if (font_info->vertical_centering)
20378 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20381 ascent = FONT_BASE (font) + boff;
20382 descent = FONT_DESCENT (font) - boff;
20384 if (override)
20386 it->override_ascent = ascent;
20387 it->override_descent = descent;
20388 it->override_boff = boff;
20391 height = ascent + descent;
20393 scale:
20394 if (FLOATP (val))
20395 height = (int)(XFLOAT_DATA (val) * height);
20396 else if (INTEGERP (val))
20397 height *= XINT (val);
20399 return make_number (height);
20403 /* RIF:
20404 Produce glyphs/get display metrics for the display element IT is
20405 loaded with. See the description of struct it in dispextern.h
20406 for an overview of struct it. */
20408 void
20409 x_produce_glyphs (it)
20410 struct it *it;
20412 int extra_line_spacing = it->extra_line_spacing;
20414 it->glyph_not_available_p = 0;
20416 if (it->what == IT_CHARACTER)
20418 XChar2b char2b;
20419 XFontStruct *font;
20420 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20421 XCharStruct *pcm;
20422 int font_not_found_p;
20423 struct font_info *font_info;
20424 int boff; /* baseline offset */
20425 /* We may change it->multibyte_p upon unibyte<->multibyte
20426 conversion. So, save the current value now and restore it
20427 later.
20429 Note: It seems that we don't have to record multibyte_p in
20430 struct glyph because the character code itself tells if or
20431 not the character is multibyte. Thus, in the future, we must
20432 consider eliminating the field `multibyte_p' in the struct
20433 glyph. */
20434 int saved_multibyte_p = it->multibyte_p;
20436 /* Maybe translate single-byte characters to multibyte, or the
20437 other way. */
20438 it->char_to_display = it->c;
20439 if (!ASCII_BYTE_P (it->c))
20441 if (unibyte_display_via_language_environment
20442 && SINGLE_BYTE_CHAR_P (it->c)
20443 && (it->c >= 0240
20444 || !NILP (Vnonascii_translation_table)))
20446 it->char_to_display = unibyte_char_to_multibyte (it->c);
20447 it->multibyte_p = 1;
20448 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20449 face = FACE_FROM_ID (it->f, it->face_id);
20451 else if (!SINGLE_BYTE_CHAR_P (it->c)
20452 && !it->multibyte_p)
20454 it->multibyte_p = 1;
20455 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20456 face = FACE_FROM_ID (it->f, it->face_id);
20460 /* Get font to use. Encode IT->char_to_display. */
20461 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20462 &char2b, it->multibyte_p, 0);
20463 font = face->font;
20465 /* When no suitable font found, use the default font. */
20466 font_not_found_p = font == NULL;
20467 if (font_not_found_p)
20469 font = FRAME_FONT (it->f);
20470 boff = FRAME_BASELINE_OFFSET (it->f);
20471 font_info = NULL;
20473 else
20475 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20476 boff = font_info->baseline_offset;
20477 if (font_info->vertical_centering)
20478 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20481 if (it->char_to_display >= ' '
20482 && (!it->multibyte_p || it->char_to_display < 128))
20484 /* Either unibyte or ASCII. */
20485 int stretched_p;
20487 it->nglyphs = 1;
20489 pcm = rif->per_char_metric (font, &char2b,
20490 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20492 if (it->override_ascent >= 0)
20494 it->ascent = it->override_ascent;
20495 it->descent = it->override_descent;
20496 boff = it->override_boff;
20498 else
20500 it->ascent = FONT_BASE (font) + boff;
20501 it->descent = FONT_DESCENT (font) - boff;
20504 if (pcm)
20506 it->phys_ascent = pcm->ascent + boff;
20507 it->phys_descent = pcm->descent - boff;
20508 it->pixel_width = pcm->width;
20510 else
20512 it->glyph_not_available_p = 1;
20513 it->phys_ascent = it->ascent;
20514 it->phys_descent = it->descent;
20515 it->pixel_width = FONT_WIDTH (font);
20518 if (it->constrain_row_ascent_descent_p)
20520 if (it->descent > it->max_descent)
20522 it->ascent += it->descent - it->max_descent;
20523 it->descent = it->max_descent;
20525 if (it->ascent > it->max_ascent)
20527 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20528 it->ascent = it->max_ascent;
20530 it->phys_ascent = min (it->phys_ascent, it->ascent);
20531 it->phys_descent = min (it->phys_descent, it->descent);
20532 extra_line_spacing = 0;
20535 /* If this is a space inside a region of text with
20536 `space-width' property, change its width. */
20537 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20538 if (stretched_p)
20539 it->pixel_width *= XFLOATINT (it->space_width);
20541 /* If face has a box, add the box thickness to the character
20542 height. If character has a box line to the left and/or
20543 right, add the box line width to the character's width. */
20544 if (face->box != FACE_NO_BOX)
20546 int thick = face->box_line_width;
20548 if (thick > 0)
20550 it->ascent += thick;
20551 it->descent += thick;
20553 else
20554 thick = -thick;
20556 if (it->start_of_box_run_p)
20557 it->pixel_width += thick;
20558 if (it->end_of_box_run_p)
20559 it->pixel_width += thick;
20562 /* If face has an overline, add the height of the overline
20563 (1 pixel) and a 1 pixel margin to the character height. */
20564 if (face->overline_p)
20565 it->ascent += overline_margin;
20567 if (it->constrain_row_ascent_descent_p)
20569 if (it->ascent > it->max_ascent)
20570 it->ascent = it->max_ascent;
20571 if (it->descent > it->max_descent)
20572 it->descent = it->max_descent;
20575 take_vertical_position_into_account (it);
20577 /* If we have to actually produce glyphs, do it. */
20578 if (it->glyph_row)
20580 if (stretched_p)
20582 /* Translate a space with a `space-width' property
20583 into a stretch glyph. */
20584 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20585 / FONT_HEIGHT (font));
20586 append_stretch_glyph (it, it->object, it->pixel_width,
20587 it->ascent + it->descent, ascent);
20589 else
20590 append_glyph (it);
20592 /* If characters with lbearing or rbearing are displayed
20593 in this line, record that fact in a flag of the
20594 glyph row. This is used to optimize X output code. */
20595 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20596 it->glyph_row->contains_overlapping_glyphs_p = 1;
20599 else if (it->char_to_display == '\n')
20601 /* A newline has no width but we need the height of the line.
20602 But if previous part of the line set a height, don't
20603 increase that height */
20605 Lisp_Object height;
20606 Lisp_Object total_height = Qnil;
20608 it->override_ascent = -1;
20609 it->pixel_width = 0;
20610 it->nglyphs = 0;
20612 height = get_line_height_property(it, Qline_height);
20613 /* Split (line-height total-height) list */
20614 if (CONSP (height)
20615 && CONSP (XCDR (height))
20616 && NILP (XCDR (XCDR (height))))
20618 total_height = XCAR (XCDR (height));
20619 height = XCAR (height);
20621 height = calc_line_height_property(it, height, font, boff, 1);
20623 if (it->override_ascent >= 0)
20625 it->ascent = it->override_ascent;
20626 it->descent = it->override_descent;
20627 boff = it->override_boff;
20629 else
20631 it->ascent = FONT_BASE (font) + boff;
20632 it->descent = FONT_DESCENT (font) - boff;
20635 if (EQ (height, Qt))
20637 if (it->descent > it->max_descent)
20639 it->ascent += it->descent - it->max_descent;
20640 it->descent = it->max_descent;
20642 if (it->ascent > it->max_ascent)
20644 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20645 it->ascent = it->max_ascent;
20647 it->phys_ascent = min (it->phys_ascent, it->ascent);
20648 it->phys_descent = min (it->phys_descent, it->descent);
20649 it->constrain_row_ascent_descent_p = 1;
20650 extra_line_spacing = 0;
20652 else
20654 Lisp_Object spacing;
20656 it->phys_ascent = it->ascent;
20657 it->phys_descent = it->descent;
20659 if ((it->max_ascent > 0 || it->max_descent > 0)
20660 && face->box != FACE_NO_BOX
20661 && face->box_line_width > 0)
20663 it->ascent += face->box_line_width;
20664 it->descent += face->box_line_width;
20666 if (!NILP (height)
20667 && XINT (height) > it->ascent + it->descent)
20668 it->ascent = XINT (height) - it->descent;
20670 if (!NILP (total_height))
20671 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20672 else
20674 spacing = get_line_height_property(it, Qline_spacing);
20675 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20677 if (INTEGERP (spacing))
20679 extra_line_spacing = XINT (spacing);
20680 if (!NILP (total_height))
20681 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20685 else if (it->char_to_display == '\t')
20687 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20688 int x = it->current_x + it->continuation_lines_width;
20689 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20691 /* If the distance from the current position to the next tab
20692 stop is less than a space character width, use the
20693 tab stop after that. */
20694 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20695 next_tab_x += tab_width;
20697 it->pixel_width = next_tab_x - x;
20698 it->nglyphs = 1;
20699 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20700 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20702 if (it->glyph_row)
20704 append_stretch_glyph (it, it->object, it->pixel_width,
20705 it->ascent + it->descent, it->ascent);
20708 else
20710 /* A multi-byte character. Assume that the display width of the
20711 character is the width of the character multiplied by the
20712 width of the font. */
20714 /* If we found a font, this font should give us the right
20715 metrics. If we didn't find a font, use the frame's
20716 default font and calculate the width of the character
20717 from the charset width; this is what old redisplay code
20718 did. */
20720 pcm = rif->per_char_metric (font, &char2b,
20721 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20723 if (font_not_found_p || !pcm)
20725 int charset = CHAR_CHARSET (it->char_to_display);
20727 it->glyph_not_available_p = 1;
20728 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20729 * CHARSET_WIDTH (charset));
20730 it->phys_ascent = FONT_BASE (font) + boff;
20731 it->phys_descent = FONT_DESCENT (font) - boff;
20733 else
20735 it->pixel_width = pcm->width;
20736 it->phys_ascent = pcm->ascent + boff;
20737 it->phys_descent = pcm->descent - boff;
20738 if (it->glyph_row
20739 && (pcm->lbearing < 0
20740 || pcm->rbearing > pcm->width))
20741 it->glyph_row->contains_overlapping_glyphs_p = 1;
20743 it->nglyphs = 1;
20744 it->ascent = FONT_BASE (font) + boff;
20745 it->descent = FONT_DESCENT (font) - boff;
20746 if (face->box != FACE_NO_BOX)
20748 int thick = face->box_line_width;
20750 if (thick > 0)
20752 it->ascent += thick;
20753 it->descent += thick;
20755 else
20756 thick = - thick;
20758 if (it->start_of_box_run_p)
20759 it->pixel_width += thick;
20760 if (it->end_of_box_run_p)
20761 it->pixel_width += thick;
20764 /* If face has an overline, add the height of the overline
20765 (1 pixel) and a 1 pixel margin to the character height. */
20766 if (face->overline_p)
20767 it->ascent += overline_margin;
20769 take_vertical_position_into_account (it);
20771 if (it->glyph_row)
20772 append_glyph (it);
20774 it->multibyte_p = saved_multibyte_p;
20776 else if (it->what == IT_COMPOSITION)
20778 /* Note: A composition is represented as one glyph in the
20779 glyph matrix. There are no padding glyphs. */
20780 XChar2b char2b;
20781 XFontStruct *font;
20782 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20783 XCharStruct *pcm;
20784 int font_not_found_p;
20785 struct font_info *font_info;
20786 int boff; /* baseline offset */
20787 struct composition *cmp = composition_table[it->cmp_id];
20789 /* Maybe translate single-byte characters to multibyte. */
20790 it->char_to_display = it->c;
20791 if (unibyte_display_via_language_environment
20792 && SINGLE_BYTE_CHAR_P (it->c)
20793 && (it->c >= 0240
20794 || (it->c >= 0200
20795 && !NILP (Vnonascii_translation_table))))
20797 it->char_to_display = unibyte_char_to_multibyte (it->c);
20800 /* Get face and font to use. Encode IT->char_to_display. */
20801 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20802 face = FACE_FROM_ID (it->f, it->face_id);
20803 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20804 &char2b, it->multibyte_p, 0);
20805 font = face->font;
20807 /* When no suitable font found, use the default font. */
20808 font_not_found_p = font == NULL;
20809 if (font_not_found_p)
20811 font = FRAME_FONT (it->f);
20812 boff = FRAME_BASELINE_OFFSET (it->f);
20813 font_info = NULL;
20815 else
20817 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20818 boff = font_info->baseline_offset;
20819 if (font_info->vertical_centering)
20820 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20823 /* There are no padding glyphs, so there is only one glyph to
20824 produce for the composition. Important is that pixel_width,
20825 ascent and descent are the values of what is drawn by
20826 draw_glyphs (i.e. the values of the overall glyphs composed). */
20827 it->nglyphs = 1;
20829 /* If we have not yet calculated pixel size data of glyphs of
20830 the composition for the current face font, calculate them
20831 now. Theoretically, we have to check all fonts for the
20832 glyphs, but that requires much time and memory space. So,
20833 here we check only the font of the first glyph. This leads
20834 to incorrect display very rarely, and C-l (recenter) can
20835 correct the display anyway. */
20836 if (cmp->font != (void *) font)
20838 /* Ascent and descent of the font of the first character of
20839 this composition (adjusted by baseline offset). Ascent
20840 and descent of overall glyphs should not be less than
20841 them respectively. */
20842 int font_ascent = FONT_BASE (font) + boff;
20843 int font_descent = FONT_DESCENT (font) - boff;
20844 /* Bounding box of the overall glyphs. */
20845 int leftmost, rightmost, lowest, highest;
20846 int i, width, ascent, descent;
20848 cmp->font = (void *) font;
20850 /* Initialize the bounding box. */
20851 if (font_info
20852 && (pcm = rif->per_char_metric (font, &char2b,
20853 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20855 width = pcm->width;
20856 ascent = pcm->ascent;
20857 descent = pcm->descent;
20859 else
20861 width = FONT_WIDTH (font);
20862 ascent = FONT_BASE (font);
20863 descent = FONT_DESCENT (font);
20866 rightmost = width;
20867 lowest = - descent + boff;
20868 highest = ascent + boff;
20869 leftmost = 0;
20871 if (font_info
20872 && font_info->default_ascent
20873 && CHAR_TABLE_P (Vuse_default_ascent)
20874 && !NILP (Faref (Vuse_default_ascent,
20875 make_number (it->char_to_display))))
20876 highest = font_info->default_ascent + boff;
20878 /* Draw the first glyph at the normal position. It may be
20879 shifted to right later if some other glyphs are drawn at
20880 the left. */
20881 cmp->offsets[0] = 0;
20882 cmp->offsets[1] = boff;
20884 /* Set cmp->offsets for the remaining glyphs. */
20885 for (i = 1; i < cmp->glyph_len; i++)
20887 int left, right, btm, top;
20888 int ch = COMPOSITION_GLYPH (cmp, i);
20889 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20891 face = FACE_FROM_ID (it->f, face_id);
20892 get_char_face_and_encoding (it->f, ch, face->id,
20893 &char2b, it->multibyte_p, 0);
20894 font = face->font;
20895 if (font == NULL)
20897 font = FRAME_FONT (it->f);
20898 boff = FRAME_BASELINE_OFFSET (it->f);
20899 font_info = NULL;
20901 else
20903 font_info
20904 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20905 boff = font_info->baseline_offset;
20906 if (font_info->vertical_centering)
20907 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20910 if (font_info
20911 && (pcm = rif->per_char_metric (font, &char2b,
20912 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20914 width = pcm->width;
20915 ascent = pcm->ascent;
20916 descent = pcm->descent;
20918 else
20920 width = FONT_WIDTH (font);
20921 ascent = 1;
20922 descent = 0;
20925 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20927 /* Relative composition with or without
20928 alternate chars. */
20929 left = (leftmost + rightmost - width) / 2;
20930 btm = - descent + boff;
20931 if (font_info && font_info->relative_compose
20932 && (! CHAR_TABLE_P (Vignore_relative_composition)
20933 || NILP (Faref (Vignore_relative_composition,
20934 make_number (ch)))))
20937 if (- descent >= font_info->relative_compose)
20938 /* One extra pixel between two glyphs. */
20939 btm = highest + 1;
20940 else if (ascent <= 0)
20941 /* One extra pixel between two glyphs. */
20942 btm = lowest - 1 - ascent - descent;
20945 else
20947 /* A composition rule is specified by an integer
20948 value that encodes global and new reference
20949 points (GREF and NREF). GREF and NREF are
20950 specified by numbers as below:
20952 0---1---2 -- ascent
20956 9--10--11 -- center
20958 ---3---4---5--- baseline
20960 6---7---8 -- descent
20962 int rule = COMPOSITION_RULE (cmp, i);
20963 int gref, nref, grefx, grefy, nrefx, nrefy;
20965 COMPOSITION_DECODE_RULE (rule, gref, nref);
20966 grefx = gref % 3, nrefx = nref % 3;
20967 grefy = gref / 3, nrefy = nref / 3;
20969 left = (leftmost
20970 + grefx * (rightmost - leftmost) / 2
20971 - nrefx * width / 2);
20972 btm = ((grefy == 0 ? highest
20973 : grefy == 1 ? 0
20974 : grefy == 2 ? lowest
20975 : (highest + lowest) / 2)
20976 - (nrefy == 0 ? ascent + descent
20977 : nrefy == 1 ? descent - boff
20978 : nrefy == 2 ? 0
20979 : (ascent + descent) / 2));
20982 cmp->offsets[i * 2] = left;
20983 cmp->offsets[i * 2 + 1] = btm + descent;
20985 /* Update the bounding box of the overall glyphs. */
20986 right = left + width;
20987 top = btm + descent + ascent;
20988 if (left < leftmost)
20989 leftmost = left;
20990 if (right > rightmost)
20991 rightmost = right;
20992 if (top > highest)
20993 highest = top;
20994 if (btm < lowest)
20995 lowest = btm;
20998 /* If there are glyphs whose x-offsets are negative,
20999 shift all glyphs to the right and make all x-offsets
21000 non-negative. */
21001 if (leftmost < 0)
21003 for (i = 0; i < cmp->glyph_len; i++)
21004 cmp->offsets[i * 2] -= leftmost;
21005 rightmost -= leftmost;
21008 cmp->pixel_width = rightmost;
21009 cmp->ascent = highest;
21010 cmp->descent = - lowest;
21011 if (cmp->ascent < font_ascent)
21012 cmp->ascent = font_ascent;
21013 if (cmp->descent < font_descent)
21014 cmp->descent = font_descent;
21017 it->pixel_width = cmp->pixel_width;
21018 it->ascent = it->phys_ascent = cmp->ascent;
21019 it->descent = it->phys_descent = cmp->descent;
21021 if (face->box != FACE_NO_BOX)
21023 int thick = face->box_line_width;
21025 if (thick > 0)
21027 it->ascent += thick;
21028 it->descent += thick;
21030 else
21031 thick = - thick;
21033 if (it->start_of_box_run_p)
21034 it->pixel_width += thick;
21035 if (it->end_of_box_run_p)
21036 it->pixel_width += thick;
21039 /* If face has an overline, add the height of the overline
21040 (1 pixel) and a 1 pixel margin to the character height. */
21041 if (face->overline_p)
21042 it->ascent += overline_margin;
21044 take_vertical_position_into_account (it);
21046 if (it->glyph_row)
21047 append_composite_glyph (it);
21049 else if (it->what == IT_IMAGE)
21050 produce_image_glyph (it);
21051 else if (it->what == IT_STRETCH)
21052 produce_stretch_glyph (it);
21054 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21055 because this isn't true for images with `:ascent 100'. */
21056 xassert (it->ascent >= 0 && it->descent >= 0);
21057 if (it->area == TEXT_AREA)
21058 it->current_x += it->pixel_width;
21060 if (extra_line_spacing > 0)
21062 it->descent += extra_line_spacing;
21063 if (extra_line_spacing > it->max_extra_line_spacing)
21064 it->max_extra_line_spacing = extra_line_spacing;
21067 it->max_ascent = max (it->max_ascent, it->ascent);
21068 it->max_descent = max (it->max_descent, it->descent);
21069 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21070 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21073 /* EXPORT for RIF:
21074 Output LEN glyphs starting at START at the nominal cursor position.
21075 Advance the nominal cursor over the text. The global variable
21076 updated_window contains the window being updated, updated_row is
21077 the glyph row being updated, and updated_area is the area of that
21078 row being updated. */
21080 void
21081 x_write_glyphs (start, len)
21082 struct glyph *start;
21083 int len;
21085 int x, hpos;
21087 xassert (updated_window && updated_row);
21088 BLOCK_INPUT;
21090 /* Write glyphs. */
21092 hpos = start - updated_row->glyphs[updated_area];
21093 x = draw_glyphs (updated_window, output_cursor.x,
21094 updated_row, updated_area,
21095 hpos, hpos + len,
21096 DRAW_NORMAL_TEXT, 0);
21098 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21099 if (updated_area == TEXT_AREA
21100 && updated_window->phys_cursor_on_p
21101 && updated_window->phys_cursor.vpos == output_cursor.vpos
21102 && updated_window->phys_cursor.hpos >= hpos
21103 && updated_window->phys_cursor.hpos < hpos + len)
21104 updated_window->phys_cursor_on_p = 0;
21106 UNBLOCK_INPUT;
21108 /* Advance the output cursor. */
21109 output_cursor.hpos += len;
21110 output_cursor.x = x;
21114 /* EXPORT for RIF:
21115 Insert LEN glyphs from START at the nominal cursor position. */
21117 void
21118 x_insert_glyphs (start, len)
21119 struct glyph *start;
21120 int len;
21122 struct frame *f;
21123 struct window *w;
21124 int line_height, shift_by_width, shifted_region_width;
21125 struct glyph_row *row;
21126 struct glyph *glyph;
21127 int frame_x, frame_y, hpos;
21129 xassert (updated_window && updated_row);
21130 BLOCK_INPUT;
21131 w = updated_window;
21132 f = XFRAME (WINDOW_FRAME (w));
21134 /* Get the height of the line we are in. */
21135 row = updated_row;
21136 line_height = row->height;
21138 /* Get the width of the glyphs to insert. */
21139 shift_by_width = 0;
21140 for (glyph = start; glyph < start + len; ++glyph)
21141 shift_by_width += glyph->pixel_width;
21143 /* Get the width of the region to shift right. */
21144 shifted_region_width = (window_box_width (w, updated_area)
21145 - output_cursor.x
21146 - shift_by_width);
21148 /* Shift right. */
21149 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21150 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21152 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21153 line_height, shift_by_width);
21155 /* Write the glyphs. */
21156 hpos = start - row->glyphs[updated_area];
21157 draw_glyphs (w, output_cursor.x, row, updated_area,
21158 hpos, hpos + len,
21159 DRAW_NORMAL_TEXT, 0);
21161 /* Advance the output cursor. */
21162 output_cursor.hpos += len;
21163 output_cursor.x += shift_by_width;
21164 UNBLOCK_INPUT;
21168 /* EXPORT for RIF:
21169 Erase the current text line from the nominal cursor position
21170 (inclusive) to pixel column TO_X (exclusive). The idea is that
21171 everything from TO_X onward is already erased.
21173 TO_X is a pixel position relative to updated_area of
21174 updated_window. TO_X == -1 means clear to the end of this area. */
21176 void
21177 x_clear_end_of_line (to_x)
21178 int to_x;
21180 struct frame *f;
21181 struct window *w = updated_window;
21182 int max_x, min_y, max_y;
21183 int from_x, from_y, to_y;
21185 xassert (updated_window && updated_row);
21186 f = XFRAME (w->frame);
21188 if (updated_row->full_width_p)
21189 max_x = WINDOW_TOTAL_WIDTH (w);
21190 else
21191 max_x = window_box_width (w, updated_area);
21192 max_y = window_text_bottom_y (w);
21194 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21195 of window. For TO_X > 0, truncate to end of drawing area. */
21196 if (to_x == 0)
21197 return;
21198 else if (to_x < 0)
21199 to_x = max_x;
21200 else
21201 to_x = min (to_x, max_x);
21203 to_y = min (max_y, output_cursor.y + updated_row->height);
21205 /* Notice if the cursor will be cleared by this operation. */
21206 if (!updated_row->full_width_p)
21207 notice_overwritten_cursor (w, updated_area,
21208 output_cursor.x, -1,
21209 updated_row->y,
21210 MATRIX_ROW_BOTTOM_Y (updated_row));
21212 from_x = output_cursor.x;
21214 /* Translate to frame coordinates. */
21215 if (updated_row->full_width_p)
21217 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21218 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21220 else
21222 int area_left = window_box_left (w, updated_area);
21223 from_x += area_left;
21224 to_x += area_left;
21227 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21228 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21229 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21231 /* Prevent inadvertently clearing to end of the X window. */
21232 if (to_x > from_x && to_y > from_y)
21234 BLOCK_INPUT;
21235 rif->clear_frame_area (f, from_x, from_y,
21236 to_x - from_x, to_y - from_y);
21237 UNBLOCK_INPUT;
21241 #endif /* HAVE_WINDOW_SYSTEM */
21245 /***********************************************************************
21246 Cursor types
21247 ***********************************************************************/
21249 /* Value is the internal representation of the specified cursor type
21250 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21251 of the bar cursor. */
21253 static enum text_cursor_kinds
21254 get_specified_cursor_type (arg, width)
21255 Lisp_Object arg;
21256 int *width;
21258 enum text_cursor_kinds type;
21260 if (NILP (arg))
21261 return NO_CURSOR;
21263 if (EQ (arg, Qbox))
21264 return FILLED_BOX_CURSOR;
21266 if (EQ (arg, Qhollow))
21267 return HOLLOW_BOX_CURSOR;
21269 if (EQ (arg, Qbar))
21271 *width = 2;
21272 return BAR_CURSOR;
21275 if (CONSP (arg)
21276 && EQ (XCAR (arg), Qbar)
21277 && INTEGERP (XCDR (arg))
21278 && XINT (XCDR (arg)) >= 0)
21280 *width = XINT (XCDR (arg));
21281 return BAR_CURSOR;
21284 if (EQ (arg, Qhbar))
21286 *width = 2;
21287 return HBAR_CURSOR;
21290 if (CONSP (arg)
21291 && EQ (XCAR (arg), Qhbar)
21292 && INTEGERP (XCDR (arg))
21293 && XINT (XCDR (arg)) >= 0)
21295 *width = XINT (XCDR (arg));
21296 return HBAR_CURSOR;
21299 /* Treat anything unknown as "hollow box cursor".
21300 It was bad to signal an error; people have trouble fixing
21301 .Xdefaults with Emacs, when it has something bad in it. */
21302 type = HOLLOW_BOX_CURSOR;
21304 return type;
21307 /* Set the default cursor types for specified frame. */
21308 void
21309 set_frame_cursor_types (f, arg)
21310 struct frame *f;
21311 Lisp_Object arg;
21313 int width;
21314 Lisp_Object tem;
21316 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21317 FRAME_CURSOR_WIDTH (f) = width;
21319 /* By default, set up the blink-off state depending on the on-state. */
21321 tem = Fassoc (arg, Vblink_cursor_alist);
21322 if (!NILP (tem))
21324 FRAME_BLINK_OFF_CURSOR (f)
21325 = get_specified_cursor_type (XCDR (tem), &width);
21326 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21328 else
21329 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21333 /* Return the cursor we want to be displayed in window W. Return
21334 width of bar/hbar cursor through WIDTH arg. Return with
21335 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21336 (i.e. if the `system caret' should track this cursor).
21338 In a mini-buffer window, we want the cursor only to appear if we
21339 are reading input from this window. For the selected window, we
21340 want the cursor type given by the frame parameter or buffer local
21341 setting of cursor-type. If explicitly marked off, draw no cursor.
21342 In all other cases, we want a hollow box cursor. */
21344 static enum text_cursor_kinds
21345 get_window_cursor_type (w, glyph, width, active_cursor)
21346 struct window *w;
21347 struct glyph *glyph;
21348 int *width;
21349 int *active_cursor;
21351 struct frame *f = XFRAME (w->frame);
21352 struct buffer *b = XBUFFER (w->buffer);
21353 int cursor_type = DEFAULT_CURSOR;
21354 Lisp_Object alt_cursor;
21355 int non_selected = 0;
21357 *active_cursor = 1;
21359 /* Echo area */
21360 if (cursor_in_echo_area
21361 && FRAME_HAS_MINIBUF_P (f)
21362 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21364 if (w == XWINDOW (echo_area_window))
21366 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21368 *width = FRAME_CURSOR_WIDTH (f);
21369 return FRAME_DESIRED_CURSOR (f);
21371 else
21372 return get_specified_cursor_type (b->cursor_type, width);
21375 *active_cursor = 0;
21376 non_selected = 1;
21379 /* Detect a nonselected window or nonselected frame. */
21380 else if (w != XWINDOW (f->selected_window)
21381 #ifdef HAVE_WINDOW_SYSTEM
21382 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21383 #endif
21386 *active_cursor = 0;
21388 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21389 return NO_CURSOR;
21391 non_selected = 1;
21394 /* Never display a cursor in a window in which cursor-type is nil. */
21395 if (NILP (b->cursor_type))
21396 return NO_CURSOR;
21398 /* Get the normal cursor type for this window. */
21399 if (EQ (b->cursor_type, Qt))
21401 cursor_type = FRAME_DESIRED_CURSOR (f);
21402 *width = FRAME_CURSOR_WIDTH (f);
21404 else
21405 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21407 /* Use cursor-in-non-selected-windows instead
21408 for non-selected window or frame. */
21409 if (non_selected)
21411 alt_cursor = b->cursor_in_non_selected_windows;
21412 if (!EQ (Qt, alt_cursor))
21413 return get_specified_cursor_type (alt_cursor, width);
21414 /* t means modify the normal cursor type. */
21415 if (cursor_type == FILLED_BOX_CURSOR)
21416 cursor_type = HOLLOW_BOX_CURSOR;
21417 else if (cursor_type == BAR_CURSOR && *width > 1)
21418 --*width;
21419 return cursor_type;
21422 /* Use normal cursor if not blinked off. */
21423 if (!w->cursor_off_p)
21425 #ifdef HAVE_WINDOW_SYSTEM
21426 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21428 if (cursor_type == FILLED_BOX_CURSOR)
21430 /* Using a block cursor on large images can be very annoying.
21431 So use a hollow cursor for "large" images.
21432 If image is not transparent (no mask), also use hollow cursor. */
21433 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21434 if (img != NULL && IMAGEP (img->spec))
21436 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21437 where N = size of default frame font size.
21438 This should cover most of the "tiny" icons people may use. */
21439 if (!img->mask
21440 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21441 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21442 cursor_type = HOLLOW_BOX_CURSOR;
21445 else if (cursor_type != NO_CURSOR)
21447 /* Display current only supports BOX and HOLLOW cursors for images.
21448 So for now, unconditionally use a HOLLOW cursor when cursor is
21449 not a solid box cursor. */
21450 cursor_type = HOLLOW_BOX_CURSOR;
21453 #endif
21454 return cursor_type;
21457 /* Cursor is blinked off, so determine how to "toggle" it. */
21459 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21460 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21461 return get_specified_cursor_type (XCDR (alt_cursor), width);
21463 /* Then see if frame has specified a specific blink off cursor type. */
21464 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21466 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21467 return FRAME_BLINK_OFF_CURSOR (f);
21470 #if 0
21471 /* Some people liked having a permanently visible blinking cursor,
21472 while others had very strong opinions against it. So it was
21473 decided to remove it. KFS 2003-09-03 */
21475 /* Finally perform built-in cursor blinking:
21476 filled box <-> hollow box
21477 wide [h]bar <-> narrow [h]bar
21478 narrow [h]bar <-> no cursor
21479 other type <-> no cursor */
21481 if (cursor_type == FILLED_BOX_CURSOR)
21482 return HOLLOW_BOX_CURSOR;
21484 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21486 *width = 1;
21487 return cursor_type;
21489 #endif
21491 return NO_CURSOR;
21495 #ifdef HAVE_WINDOW_SYSTEM
21497 /* Notice when the text cursor of window W has been completely
21498 overwritten by a drawing operation that outputs glyphs in AREA
21499 starting at X0 and ending at X1 in the line starting at Y0 and
21500 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21501 the rest of the line after X0 has been written. Y coordinates
21502 are window-relative. */
21504 static void
21505 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21506 struct window *w;
21507 enum glyph_row_area area;
21508 int x0, y0, x1, y1;
21510 int cx0, cx1, cy0, cy1;
21511 struct glyph_row *row;
21513 if (!w->phys_cursor_on_p)
21514 return;
21515 if (area != TEXT_AREA)
21516 return;
21518 if (w->phys_cursor.vpos < 0
21519 || w->phys_cursor.vpos >= w->current_matrix->nrows
21520 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21521 !(row->enabled_p && row->displays_text_p)))
21522 return;
21524 if (row->cursor_in_fringe_p)
21526 row->cursor_in_fringe_p = 0;
21527 draw_fringe_bitmap (w, row, 0);
21528 w->phys_cursor_on_p = 0;
21529 return;
21532 cx0 = w->phys_cursor.x;
21533 cx1 = cx0 + w->phys_cursor_width;
21534 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21535 return;
21537 /* The cursor image will be completely removed from the
21538 screen if the output area intersects the cursor area in
21539 y-direction. When we draw in [y0 y1[, and some part of
21540 the cursor is at y < y0, that part must have been drawn
21541 before. When scrolling, the cursor is erased before
21542 actually scrolling, so we don't come here. When not
21543 scrolling, the rows above the old cursor row must have
21544 changed, and in this case these rows must have written
21545 over the cursor image.
21547 Likewise if part of the cursor is below y1, with the
21548 exception of the cursor being in the first blank row at
21549 the buffer and window end because update_text_area
21550 doesn't draw that row. (Except when it does, but
21551 that's handled in update_text_area.) */
21553 cy0 = w->phys_cursor.y;
21554 cy1 = cy0 + w->phys_cursor_height;
21555 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21556 return;
21558 w->phys_cursor_on_p = 0;
21561 #endif /* HAVE_WINDOW_SYSTEM */
21564 /************************************************************************
21565 Mouse Face
21566 ************************************************************************/
21568 #ifdef HAVE_WINDOW_SYSTEM
21570 /* EXPORT for RIF:
21571 Fix the display of area AREA of overlapping row ROW in window W
21572 with respect to the overlapping part OVERLAPS. */
21574 void
21575 x_fix_overlapping_area (w, row, area, overlaps)
21576 struct window *w;
21577 struct glyph_row *row;
21578 enum glyph_row_area area;
21579 int overlaps;
21581 int i, x;
21583 BLOCK_INPUT;
21585 x = 0;
21586 for (i = 0; i < row->used[area];)
21588 if (row->glyphs[area][i].overlaps_vertically_p)
21590 int start = i, start_x = x;
21594 x += row->glyphs[area][i].pixel_width;
21595 ++i;
21597 while (i < row->used[area]
21598 && row->glyphs[area][i].overlaps_vertically_p);
21600 draw_glyphs (w, start_x, row, area,
21601 start, i,
21602 DRAW_NORMAL_TEXT, overlaps);
21604 else
21606 x += row->glyphs[area][i].pixel_width;
21607 ++i;
21611 UNBLOCK_INPUT;
21615 /* EXPORT:
21616 Draw the cursor glyph of window W in glyph row ROW. See the
21617 comment of draw_glyphs for the meaning of HL. */
21619 void
21620 draw_phys_cursor_glyph (w, row, hl)
21621 struct window *w;
21622 struct glyph_row *row;
21623 enum draw_glyphs_face hl;
21625 /* If cursor hpos is out of bounds, don't draw garbage. This can
21626 happen in mini-buffer windows when switching between echo area
21627 glyphs and mini-buffer. */
21628 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21630 int on_p = w->phys_cursor_on_p;
21631 int x1;
21632 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21633 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21634 hl, 0);
21635 w->phys_cursor_on_p = on_p;
21637 if (hl == DRAW_CURSOR)
21638 w->phys_cursor_width = x1 - w->phys_cursor.x;
21639 /* When we erase the cursor, and ROW is overlapped by other
21640 rows, make sure that these overlapping parts of other rows
21641 are redrawn. */
21642 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21644 w->phys_cursor_width = x1 - w->phys_cursor.x;
21646 if (row > w->current_matrix->rows
21647 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21648 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21649 OVERLAPS_ERASED_CURSOR);
21651 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21652 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21653 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21654 OVERLAPS_ERASED_CURSOR);
21660 /* EXPORT:
21661 Erase the image of a cursor of window W from the screen. */
21663 void
21664 erase_phys_cursor (w)
21665 struct window *w;
21667 struct frame *f = XFRAME (w->frame);
21668 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21669 int hpos = w->phys_cursor.hpos;
21670 int vpos = w->phys_cursor.vpos;
21671 int mouse_face_here_p = 0;
21672 struct glyph_matrix *active_glyphs = w->current_matrix;
21673 struct glyph_row *cursor_row;
21674 struct glyph *cursor_glyph;
21675 enum draw_glyphs_face hl;
21677 /* No cursor displayed or row invalidated => nothing to do on the
21678 screen. */
21679 if (w->phys_cursor_type == NO_CURSOR)
21680 goto mark_cursor_off;
21682 /* VPOS >= active_glyphs->nrows means that window has been resized.
21683 Don't bother to erase the cursor. */
21684 if (vpos >= active_glyphs->nrows)
21685 goto mark_cursor_off;
21687 /* If row containing cursor is marked invalid, there is nothing we
21688 can do. */
21689 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21690 if (!cursor_row->enabled_p)
21691 goto mark_cursor_off;
21693 /* If line spacing is > 0, old cursor may only be partially visible in
21694 window after split-window. So adjust visible height. */
21695 cursor_row->visible_height = min (cursor_row->visible_height,
21696 window_text_bottom_y (w) - cursor_row->y);
21698 /* If row is completely invisible, don't attempt to delete a cursor which
21699 isn't there. This can happen if cursor is at top of a window, and
21700 we switch to a buffer with a header line in that window. */
21701 if (cursor_row->visible_height <= 0)
21702 goto mark_cursor_off;
21704 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21705 if (cursor_row->cursor_in_fringe_p)
21707 cursor_row->cursor_in_fringe_p = 0;
21708 draw_fringe_bitmap (w, cursor_row, 0);
21709 goto mark_cursor_off;
21712 /* This can happen when the new row is shorter than the old one.
21713 In this case, either draw_glyphs or clear_end_of_line
21714 should have cleared the cursor. Note that we wouldn't be
21715 able to erase the cursor in this case because we don't have a
21716 cursor glyph at hand. */
21717 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21718 goto mark_cursor_off;
21720 /* If the cursor is in the mouse face area, redisplay that when
21721 we clear the cursor. */
21722 if (! NILP (dpyinfo->mouse_face_window)
21723 && w == XWINDOW (dpyinfo->mouse_face_window)
21724 && (vpos > dpyinfo->mouse_face_beg_row
21725 || (vpos == dpyinfo->mouse_face_beg_row
21726 && hpos >= dpyinfo->mouse_face_beg_col))
21727 && (vpos < dpyinfo->mouse_face_end_row
21728 || (vpos == dpyinfo->mouse_face_end_row
21729 && hpos < dpyinfo->mouse_face_end_col))
21730 /* Don't redraw the cursor's spot in mouse face if it is at the
21731 end of a line (on a newline). The cursor appears there, but
21732 mouse highlighting does not. */
21733 && cursor_row->used[TEXT_AREA] > hpos)
21734 mouse_face_here_p = 1;
21736 /* Maybe clear the display under the cursor. */
21737 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21739 int x, y, left_x;
21740 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21741 int width;
21743 cursor_glyph = get_phys_cursor_glyph (w);
21744 if (cursor_glyph == NULL)
21745 goto mark_cursor_off;
21747 width = cursor_glyph->pixel_width;
21748 left_x = window_box_left_offset (w, TEXT_AREA);
21749 x = w->phys_cursor.x;
21750 if (x < left_x)
21751 width -= left_x - x;
21752 width = min (width, window_box_width (w, TEXT_AREA) - x);
21753 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21754 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21756 if (width > 0)
21757 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21760 /* Erase the cursor by redrawing the character underneath it. */
21761 if (mouse_face_here_p)
21762 hl = DRAW_MOUSE_FACE;
21763 else
21764 hl = DRAW_NORMAL_TEXT;
21765 draw_phys_cursor_glyph (w, cursor_row, hl);
21767 mark_cursor_off:
21768 w->phys_cursor_on_p = 0;
21769 w->phys_cursor_type = NO_CURSOR;
21773 /* EXPORT:
21774 Display or clear cursor of window W. If ON is zero, clear the
21775 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21776 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21778 void
21779 display_and_set_cursor (w, on, hpos, vpos, x, y)
21780 struct window *w;
21781 int on, hpos, vpos, x, y;
21783 struct frame *f = XFRAME (w->frame);
21784 int new_cursor_type;
21785 int new_cursor_width;
21786 int active_cursor;
21787 struct glyph_row *glyph_row;
21788 struct glyph *glyph;
21790 /* This is pointless on invisible frames, and dangerous on garbaged
21791 windows and frames; in the latter case, the frame or window may
21792 be in the midst of changing its size, and x and y may be off the
21793 window. */
21794 if (! FRAME_VISIBLE_P (f)
21795 || FRAME_GARBAGED_P (f)
21796 || vpos >= w->current_matrix->nrows
21797 || hpos >= w->current_matrix->matrix_w)
21798 return;
21800 /* If cursor is off and we want it off, return quickly. */
21801 if (!on && !w->phys_cursor_on_p)
21802 return;
21804 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21805 /* If cursor row is not enabled, we don't really know where to
21806 display the cursor. */
21807 if (!glyph_row->enabled_p)
21809 w->phys_cursor_on_p = 0;
21810 return;
21813 glyph = NULL;
21814 if (!glyph_row->exact_window_width_line_p
21815 || hpos < glyph_row->used[TEXT_AREA])
21816 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21818 xassert (interrupt_input_blocked);
21820 /* Set new_cursor_type to the cursor we want to be displayed. */
21821 new_cursor_type = get_window_cursor_type (w, glyph,
21822 &new_cursor_width, &active_cursor);
21824 /* If cursor is currently being shown and we don't want it to be or
21825 it is in the wrong place, or the cursor type is not what we want,
21826 erase it. */
21827 if (w->phys_cursor_on_p
21828 && (!on
21829 || w->phys_cursor.x != x
21830 || w->phys_cursor.y != y
21831 || new_cursor_type != w->phys_cursor_type
21832 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21833 && new_cursor_width != w->phys_cursor_width)))
21834 erase_phys_cursor (w);
21836 /* Don't check phys_cursor_on_p here because that flag is only set
21837 to zero in some cases where we know that the cursor has been
21838 completely erased, to avoid the extra work of erasing the cursor
21839 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21840 still not be visible, or it has only been partly erased. */
21841 if (on)
21843 w->phys_cursor_ascent = glyph_row->ascent;
21844 w->phys_cursor_height = glyph_row->height;
21846 /* Set phys_cursor_.* before x_draw_.* is called because some
21847 of them may need the information. */
21848 w->phys_cursor.x = x;
21849 w->phys_cursor.y = glyph_row->y;
21850 w->phys_cursor.hpos = hpos;
21851 w->phys_cursor.vpos = vpos;
21854 rif->draw_window_cursor (w, glyph_row, x, y,
21855 new_cursor_type, new_cursor_width,
21856 on, active_cursor);
21860 /* Switch the display of W's cursor on or off, according to the value
21861 of ON. */
21863 static void
21864 update_window_cursor (w, on)
21865 struct window *w;
21866 int on;
21868 /* Don't update cursor in windows whose frame is in the process
21869 of being deleted. */
21870 if (w->current_matrix)
21872 BLOCK_INPUT;
21873 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21874 w->phys_cursor.x, w->phys_cursor.y);
21875 UNBLOCK_INPUT;
21880 /* Call update_window_cursor with parameter ON_P on all leaf windows
21881 in the window tree rooted at W. */
21883 static void
21884 update_cursor_in_window_tree (w, on_p)
21885 struct window *w;
21886 int on_p;
21888 while (w)
21890 if (!NILP (w->hchild))
21891 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21892 else if (!NILP (w->vchild))
21893 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21894 else
21895 update_window_cursor (w, on_p);
21897 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21902 /* EXPORT:
21903 Display the cursor on window W, or clear it, according to ON_P.
21904 Don't change the cursor's position. */
21906 void
21907 x_update_cursor (f, on_p)
21908 struct frame *f;
21909 int on_p;
21911 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21915 /* EXPORT:
21916 Clear the cursor of window W to background color, and mark the
21917 cursor as not shown. This is used when the text where the cursor
21918 is is about to be rewritten. */
21920 void
21921 x_clear_cursor (w)
21922 struct window *w;
21924 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21925 update_window_cursor (w, 0);
21929 /* EXPORT:
21930 Display the active region described by mouse_face_* according to DRAW. */
21932 void
21933 show_mouse_face (dpyinfo, draw)
21934 Display_Info *dpyinfo;
21935 enum draw_glyphs_face draw;
21937 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21938 struct frame *f = XFRAME (WINDOW_FRAME (w));
21940 if (/* If window is in the process of being destroyed, don't bother
21941 to do anything. */
21942 w->current_matrix != NULL
21943 /* Don't update mouse highlight if hidden */
21944 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21945 /* Recognize when we are called to operate on rows that don't exist
21946 anymore. This can happen when a window is split. */
21947 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21949 int phys_cursor_on_p = w->phys_cursor_on_p;
21950 struct glyph_row *row, *first, *last;
21952 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21953 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21955 for (row = first; row <= last && row->enabled_p; ++row)
21957 int start_hpos, end_hpos, start_x;
21959 /* For all but the first row, the highlight starts at column 0. */
21960 if (row == first)
21962 start_hpos = dpyinfo->mouse_face_beg_col;
21963 start_x = dpyinfo->mouse_face_beg_x;
21965 else
21967 start_hpos = 0;
21968 start_x = 0;
21971 if (row == last)
21972 end_hpos = dpyinfo->mouse_face_end_col;
21973 else
21975 end_hpos = row->used[TEXT_AREA];
21976 if (draw == DRAW_NORMAL_TEXT)
21977 row->fill_line_p = 1; /* Clear to end of line */
21980 if (end_hpos > start_hpos)
21982 draw_glyphs (w, start_x, row, TEXT_AREA,
21983 start_hpos, end_hpos,
21984 draw, 0);
21986 row->mouse_face_p
21987 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21991 /* When we've written over the cursor, arrange for it to
21992 be displayed again. */
21993 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21995 BLOCK_INPUT;
21996 display_and_set_cursor (w, 1,
21997 w->phys_cursor.hpos, w->phys_cursor.vpos,
21998 w->phys_cursor.x, w->phys_cursor.y);
21999 UNBLOCK_INPUT;
22003 /* Change the mouse cursor. */
22004 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22005 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22006 else if (draw == DRAW_MOUSE_FACE)
22007 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22008 else
22009 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22012 /* EXPORT:
22013 Clear out the mouse-highlighted active region.
22014 Redraw it un-highlighted first. Value is non-zero if mouse
22015 face was actually drawn unhighlighted. */
22018 clear_mouse_face (dpyinfo)
22019 Display_Info *dpyinfo;
22021 int cleared = 0;
22023 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22025 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22026 cleared = 1;
22029 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22030 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22031 dpyinfo->mouse_face_window = Qnil;
22032 dpyinfo->mouse_face_overlay = Qnil;
22033 return cleared;
22037 /* EXPORT:
22038 Non-zero if physical cursor of window W is within mouse face. */
22041 cursor_in_mouse_face_p (w)
22042 struct window *w;
22044 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22045 int in_mouse_face = 0;
22047 if (WINDOWP (dpyinfo->mouse_face_window)
22048 && XWINDOW (dpyinfo->mouse_face_window) == w)
22050 int hpos = w->phys_cursor.hpos;
22051 int vpos = w->phys_cursor.vpos;
22053 if (vpos >= dpyinfo->mouse_face_beg_row
22054 && vpos <= dpyinfo->mouse_face_end_row
22055 && (vpos > dpyinfo->mouse_face_beg_row
22056 || hpos >= dpyinfo->mouse_face_beg_col)
22057 && (vpos < dpyinfo->mouse_face_end_row
22058 || hpos < dpyinfo->mouse_face_end_col
22059 || dpyinfo->mouse_face_past_end))
22060 in_mouse_face = 1;
22063 return in_mouse_face;
22069 /* Find the glyph matrix position of buffer position CHARPOS in window
22070 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22071 current glyphs must be up to date. If CHARPOS is above window
22072 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22073 of last line in W. In the row containing CHARPOS, stop before glyphs
22074 having STOP as object. */
22076 #if 1 /* This is a version of fast_find_position that's more correct
22077 in the presence of hscrolling, for example. I didn't install
22078 it right away because the problem fixed is minor, it failed
22079 in 20.x as well, and I think it's too risky to install
22080 so near the release of 21.1. 2001-09-25 gerd. */
22082 static int
22083 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22084 struct window *w;
22085 int charpos;
22086 int *hpos, *vpos, *x, *y;
22087 Lisp_Object stop;
22089 struct glyph_row *row, *first;
22090 struct glyph *glyph, *end;
22091 int past_end = 0;
22093 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22094 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22096 *x = first->x;
22097 *y = first->y;
22098 *hpos = 0;
22099 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22100 return 1;
22103 row = row_containing_pos (w, charpos, first, NULL, 0);
22104 if (row == NULL)
22106 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22107 past_end = 1;
22110 /* If whole rows or last part of a row came from a display overlay,
22111 row_containing_pos will skip over such rows because their end pos
22112 equals the start pos of the overlay or interval.
22114 Move back if we have a STOP object and previous row's
22115 end glyph came from STOP. */
22116 if (!NILP (stop))
22118 struct glyph_row *prev;
22119 while ((prev = row - 1, prev >= first)
22120 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22121 && prev->used[TEXT_AREA] > 0)
22123 struct glyph *beg = prev->glyphs[TEXT_AREA];
22124 glyph = beg + prev->used[TEXT_AREA];
22125 while (--glyph >= beg
22126 && INTEGERP (glyph->object));
22127 if (glyph < beg
22128 || !EQ (stop, glyph->object))
22129 break;
22130 row = prev;
22134 *x = row->x;
22135 *y = row->y;
22136 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22138 glyph = row->glyphs[TEXT_AREA];
22139 end = glyph + row->used[TEXT_AREA];
22141 /* Skip over glyphs not having an object at the start of the row.
22142 These are special glyphs like truncation marks on terminal
22143 frames. */
22144 if (row->displays_text_p)
22145 while (glyph < end
22146 && INTEGERP (glyph->object)
22147 && !EQ (stop, glyph->object)
22148 && glyph->charpos < 0)
22150 *x += glyph->pixel_width;
22151 ++glyph;
22154 while (glyph < end
22155 && !INTEGERP (glyph->object)
22156 && !EQ (stop, glyph->object)
22157 && (!BUFFERP (glyph->object)
22158 || glyph->charpos < charpos))
22160 *x += glyph->pixel_width;
22161 ++glyph;
22164 *hpos = glyph - row->glyphs[TEXT_AREA];
22165 return !past_end;
22168 #else /* not 1 */
22170 static int
22171 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22172 struct window *w;
22173 int pos;
22174 int *hpos, *vpos, *x, *y;
22175 Lisp_Object stop;
22177 int i;
22178 int lastcol;
22179 int maybe_next_line_p = 0;
22180 int line_start_position;
22181 int yb = window_text_bottom_y (w);
22182 struct glyph_row *row, *best_row;
22183 int row_vpos, best_row_vpos;
22184 int current_x;
22186 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22187 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22189 while (row->y < yb)
22191 if (row->used[TEXT_AREA])
22192 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22193 else
22194 line_start_position = 0;
22196 if (line_start_position > pos)
22197 break;
22198 /* If the position sought is the end of the buffer,
22199 don't include the blank lines at the bottom of the window. */
22200 else if (line_start_position == pos
22201 && pos == BUF_ZV (XBUFFER (w->buffer)))
22203 maybe_next_line_p = 1;
22204 break;
22206 else if (line_start_position > 0)
22208 best_row = row;
22209 best_row_vpos = row_vpos;
22212 if (row->y + row->height >= yb)
22213 break;
22215 ++row;
22216 ++row_vpos;
22219 /* Find the right column within BEST_ROW. */
22220 lastcol = 0;
22221 current_x = best_row->x;
22222 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22224 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22225 int charpos = glyph->charpos;
22227 if (BUFFERP (glyph->object))
22229 if (charpos == pos)
22231 *hpos = i;
22232 *vpos = best_row_vpos;
22233 *x = current_x;
22234 *y = best_row->y;
22235 return 1;
22237 else if (charpos > pos)
22238 break;
22240 else if (EQ (glyph->object, stop))
22241 break;
22243 if (charpos > 0)
22244 lastcol = i;
22245 current_x += glyph->pixel_width;
22248 /* If we're looking for the end of the buffer,
22249 and we didn't find it in the line we scanned,
22250 use the start of the following line. */
22251 if (maybe_next_line_p)
22253 ++best_row;
22254 ++best_row_vpos;
22255 lastcol = 0;
22256 current_x = best_row->x;
22259 *vpos = best_row_vpos;
22260 *hpos = lastcol + 1;
22261 *x = current_x;
22262 *y = best_row->y;
22263 return 0;
22266 #endif /* not 1 */
22269 /* Find the position of the glyph for position POS in OBJECT in
22270 window W's current matrix, and return in *X, *Y the pixel
22271 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22273 RIGHT_P non-zero means return the position of the right edge of the
22274 glyph, RIGHT_P zero means return the left edge position.
22276 If no glyph for POS exists in the matrix, return the position of
22277 the glyph with the next smaller position that is in the matrix, if
22278 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22279 exists in the matrix, return the position of the glyph with the
22280 next larger position in OBJECT.
22282 Value is non-zero if a glyph was found. */
22284 static int
22285 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22286 struct window *w;
22287 int pos;
22288 Lisp_Object object;
22289 int *hpos, *vpos, *x, *y;
22290 int right_p;
22292 int yb = window_text_bottom_y (w);
22293 struct glyph_row *r;
22294 struct glyph *best_glyph = NULL;
22295 struct glyph_row *best_row = NULL;
22296 int best_x = 0;
22298 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22299 r->enabled_p && r->y < yb;
22300 ++r)
22302 struct glyph *g = r->glyphs[TEXT_AREA];
22303 struct glyph *e = g + r->used[TEXT_AREA];
22304 int gx;
22306 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22307 if (EQ (g->object, object))
22309 if (g->charpos == pos)
22311 best_glyph = g;
22312 best_x = gx;
22313 best_row = r;
22314 goto found;
22316 else if (best_glyph == NULL
22317 || ((abs (g->charpos - pos)
22318 < abs (best_glyph->charpos - pos))
22319 && (right_p
22320 ? g->charpos < pos
22321 : g->charpos > pos)))
22323 best_glyph = g;
22324 best_x = gx;
22325 best_row = r;
22330 found:
22332 if (best_glyph)
22334 *x = best_x;
22335 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22337 if (right_p)
22339 *x += best_glyph->pixel_width;
22340 ++*hpos;
22343 *y = best_row->y;
22344 *vpos = best_row - w->current_matrix->rows;
22347 return best_glyph != NULL;
22351 /* See if position X, Y is within a hot-spot of an image. */
22353 static int
22354 on_hot_spot_p (hot_spot, x, y)
22355 Lisp_Object hot_spot;
22356 int x, y;
22358 if (!CONSP (hot_spot))
22359 return 0;
22361 if (EQ (XCAR (hot_spot), Qrect))
22363 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22364 Lisp_Object rect = XCDR (hot_spot);
22365 Lisp_Object tem;
22366 if (!CONSP (rect))
22367 return 0;
22368 if (!CONSP (XCAR (rect)))
22369 return 0;
22370 if (!CONSP (XCDR (rect)))
22371 return 0;
22372 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22373 return 0;
22374 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22375 return 0;
22376 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22377 return 0;
22378 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22379 return 0;
22380 return 1;
22382 else if (EQ (XCAR (hot_spot), Qcircle))
22384 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22385 Lisp_Object circ = XCDR (hot_spot);
22386 Lisp_Object lr, lx0, ly0;
22387 if (CONSP (circ)
22388 && CONSP (XCAR (circ))
22389 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22390 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22391 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22393 double r = XFLOATINT (lr);
22394 double dx = XINT (lx0) - x;
22395 double dy = XINT (ly0) - y;
22396 return (dx * dx + dy * dy <= r * r);
22399 else if (EQ (XCAR (hot_spot), Qpoly))
22401 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22402 if (VECTORP (XCDR (hot_spot)))
22404 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22405 Lisp_Object *poly = v->contents;
22406 int n = v->size;
22407 int i;
22408 int inside = 0;
22409 Lisp_Object lx, ly;
22410 int x0, y0;
22412 /* Need an even number of coordinates, and at least 3 edges. */
22413 if (n < 6 || n & 1)
22414 return 0;
22416 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22417 If count is odd, we are inside polygon. Pixels on edges
22418 may or may not be included depending on actual geometry of the
22419 polygon. */
22420 if ((lx = poly[n-2], !INTEGERP (lx))
22421 || (ly = poly[n-1], !INTEGERP (lx)))
22422 return 0;
22423 x0 = XINT (lx), y0 = XINT (ly);
22424 for (i = 0; i < n; i += 2)
22426 int x1 = x0, y1 = y0;
22427 if ((lx = poly[i], !INTEGERP (lx))
22428 || (ly = poly[i+1], !INTEGERP (ly)))
22429 return 0;
22430 x0 = XINT (lx), y0 = XINT (ly);
22432 /* Does this segment cross the X line? */
22433 if (x0 >= x)
22435 if (x1 >= x)
22436 continue;
22438 else if (x1 < x)
22439 continue;
22440 if (y > y0 && y > y1)
22441 continue;
22442 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22443 inside = !inside;
22445 return inside;
22448 /* If we don't understand the format, pretend we're not in the hot-spot. */
22449 return 0;
22452 Lisp_Object
22453 find_hot_spot (map, x, y)
22454 Lisp_Object map;
22455 int x, y;
22457 while (CONSP (map))
22459 if (CONSP (XCAR (map))
22460 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22461 return XCAR (map);
22462 map = XCDR (map);
22465 return Qnil;
22468 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22469 3, 3, 0,
22470 doc: /* Lookup in image map MAP coordinates X and Y.
22471 An image map is an alist where each element has the format (AREA ID PLIST).
22472 An AREA is specified as either a rectangle, a circle, or a polygon:
22473 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22474 pixel coordinates of the upper left and bottom right corners.
22475 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22476 and the radius of the circle; r may be a float or integer.
22477 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22478 vector describes one corner in the polygon.
22479 Returns the alist element for the first matching AREA in MAP. */)
22480 (map, x, y)
22481 Lisp_Object map;
22482 Lisp_Object x, y;
22484 if (NILP (map))
22485 return Qnil;
22487 CHECK_NUMBER (x);
22488 CHECK_NUMBER (y);
22490 return find_hot_spot (map, XINT (x), XINT (y));
22494 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22495 static void
22496 define_frame_cursor1 (f, cursor, pointer)
22497 struct frame *f;
22498 Cursor cursor;
22499 Lisp_Object pointer;
22501 /* Do not change cursor shape while dragging mouse. */
22502 if (!NILP (do_mouse_tracking))
22503 return;
22505 if (!NILP (pointer))
22507 if (EQ (pointer, Qarrow))
22508 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22509 else if (EQ (pointer, Qhand))
22510 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22511 else if (EQ (pointer, Qtext))
22512 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22513 else if (EQ (pointer, intern ("hdrag")))
22514 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22515 #ifdef HAVE_X_WINDOWS
22516 else if (EQ (pointer, intern ("vdrag")))
22517 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22518 #endif
22519 else if (EQ (pointer, intern ("hourglass")))
22520 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22521 else if (EQ (pointer, Qmodeline))
22522 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22523 else
22524 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22527 if (cursor != No_Cursor)
22528 rif->define_frame_cursor (f, cursor);
22531 /* Take proper action when mouse has moved to the mode or header line
22532 or marginal area AREA of window W, x-position X and y-position Y.
22533 X is relative to the start of the text display area of W, so the
22534 width of bitmap areas and scroll bars must be subtracted to get a
22535 position relative to the start of the mode line. */
22537 static void
22538 note_mode_line_or_margin_highlight (window, x, y, area)
22539 Lisp_Object window;
22540 int x, y;
22541 enum window_part area;
22543 struct window *w = XWINDOW (window);
22544 struct frame *f = XFRAME (w->frame);
22545 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22546 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22547 Lisp_Object pointer = Qnil;
22548 int charpos, dx, dy, width, height;
22549 Lisp_Object string, object = Qnil;
22550 Lisp_Object pos, help;
22552 Lisp_Object mouse_face;
22553 int original_x_pixel = x;
22554 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22555 struct glyph_row *row;
22557 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22559 int x0;
22560 struct glyph *end;
22562 string = mode_line_string (w, area, &x, &y, &charpos,
22563 &object, &dx, &dy, &width, &height);
22565 row = (area == ON_MODE_LINE
22566 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22567 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22569 /* Find glyph */
22570 if (row->mode_line_p && row->enabled_p)
22572 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22573 end = glyph + row->used[TEXT_AREA];
22575 for (x0 = original_x_pixel;
22576 glyph < end && x0 >= glyph->pixel_width;
22577 ++glyph)
22578 x0 -= glyph->pixel_width;
22580 if (glyph >= end)
22581 glyph = NULL;
22584 else
22586 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22587 string = marginal_area_string (w, area, &x, &y, &charpos,
22588 &object, &dx, &dy, &width, &height);
22591 help = Qnil;
22593 if (IMAGEP (object))
22595 Lisp_Object image_map, hotspot;
22596 if ((image_map = Fplist_get (XCDR (object), QCmap),
22597 !NILP (image_map))
22598 && (hotspot = find_hot_spot (image_map, dx, dy),
22599 CONSP (hotspot))
22600 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22602 Lisp_Object area_id, plist;
22604 area_id = XCAR (hotspot);
22605 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22606 If so, we could look for mouse-enter, mouse-leave
22607 properties in PLIST (and do something...). */
22608 hotspot = XCDR (hotspot);
22609 if (CONSP (hotspot)
22610 && (plist = XCAR (hotspot), CONSP (plist)))
22612 pointer = Fplist_get (plist, Qpointer);
22613 if (NILP (pointer))
22614 pointer = Qhand;
22615 help = Fplist_get (plist, Qhelp_echo);
22616 if (!NILP (help))
22618 help_echo_string = help;
22619 /* Is this correct? ++kfs */
22620 XSETWINDOW (help_echo_window, w);
22621 help_echo_object = w->buffer;
22622 help_echo_pos = charpos;
22626 if (NILP (pointer))
22627 pointer = Fplist_get (XCDR (object), QCpointer);
22630 if (STRINGP (string))
22632 pos = make_number (charpos);
22633 /* If we're on a string with `help-echo' text property, arrange
22634 for the help to be displayed. This is done by setting the
22635 global variable help_echo_string to the help string. */
22636 if (NILP (help))
22638 help = Fget_text_property (pos, Qhelp_echo, string);
22639 if (!NILP (help))
22641 help_echo_string = help;
22642 XSETWINDOW (help_echo_window, w);
22643 help_echo_object = string;
22644 help_echo_pos = charpos;
22648 if (NILP (pointer))
22649 pointer = Fget_text_property (pos, Qpointer, string);
22651 /* Change the mouse pointer according to what is under X/Y. */
22652 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22654 Lisp_Object map;
22655 map = Fget_text_property (pos, Qlocal_map, string);
22656 if (!KEYMAPP (map))
22657 map = Fget_text_property (pos, Qkeymap, string);
22658 if (!KEYMAPP (map))
22659 cursor = dpyinfo->vertical_scroll_bar_cursor;
22662 /* Change the mouse face according to what is under X/Y. */
22663 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22664 if (!NILP (mouse_face)
22665 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22666 && glyph)
22668 Lisp_Object b, e;
22670 struct glyph * tmp_glyph;
22672 int gpos;
22673 int gseq_length;
22674 int total_pixel_width;
22675 int ignore;
22677 int vpos, hpos;
22679 b = Fprevious_single_property_change (make_number (charpos + 1),
22680 Qmouse_face, string, Qnil);
22681 if (NILP (b))
22682 b = make_number (0);
22684 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22685 if (NILP (e))
22686 e = make_number (SCHARS (string));
22688 /* Calculate the position(glyph position: GPOS) of GLYPH in
22689 displayed string. GPOS is different from CHARPOS.
22691 CHARPOS is the position of glyph in internal string
22692 object. A mode line string format has structures which
22693 is converted to a flatten by emacs lisp interpreter.
22694 The internal string is an element of the structures.
22695 The displayed string is the flatten string. */
22696 gpos = 0;
22697 if (glyph > row_start_glyph)
22699 tmp_glyph = glyph - 1;
22700 while (tmp_glyph >= row_start_glyph
22701 && tmp_glyph->charpos >= XINT (b)
22702 && EQ (tmp_glyph->object, glyph->object))
22704 tmp_glyph--;
22705 gpos++;
22709 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22710 displayed string holding GLYPH.
22712 GSEQ_LENGTH is different from SCHARS (STRING).
22713 SCHARS (STRING) returns the length of the internal string. */
22714 for (tmp_glyph = glyph, gseq_length = gpos;
22715 tmp_glyph->charpos < XINT (e);
22716 tmp_glyph++, gseq_length++)
22718 if (!EQ (tmp_glyph->object, glyph->object))
22719 break;
22722 total_pixel_width = 0;
22723 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22724 total_pixel_width += tmp_glyph->pixel_width;
22726 /* Pre calculation of re-rendering position */
22727 vpos = (x - gpos);
22728 hpos = (area == ON_MODE_LINE
22729 ? (w->current_matrix)->nrows - 1
22730 : 0);
22732 /* If the re-rendering position is included in the last
22733 re-rendering area, we should do nothing. */
22734 if ( EQ (window, dpyinfo->mouse_face_window)
22735 && dpyinfo->mouse_face_beg_col <= vpos
22736 && vpos < dpyinfo->mouse_face_end_col
22737 && dpyinfo->mouse_face_beg_row == hpos )
22738 return;
22740 if (clear_mouse_face (dpyinfo))
22741 cursor = No_Cursor;
22743 dpyinfo->mouse_face_beg_col = vpos;
22744 dpyinfo->mouse_face_beg_row = hpos;
22746 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22747 dpyinfo->mouse_face_beg_y = 0;
22749 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22750 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22752 dpyinfo->mouse_face_end_x = 0;
22753 dpyinfo->mouse_face_end_y = 0;
22755 dpyinfo->mouse_face_past_end = 0;
22756 dpyinfo->mouse_face_window = window;
22758 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22759 charpos,
22760 0, 0, 0, &ignore,
22761 glyph->face_id, 1);
22762 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22764 if (NILP (pointer))
22765 pointer = Qhand;
22767 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22768 clear_mouse_face (dpyinfo);
22770 define_frame_cursor1 (f, cursor, pointer);
22774 /* EXPORT:
22775 Take proper action when the mouse has moved to position X, Y on
22776 frame F as regards highlighting characters that have mouse-face
22777 properties. Also de-highlighting chars where the mouse was before.
22778 X and Y can be negative or out of range. */
22780 void
22781 note_mouse_highlight (f, x, y)
22782 struct frame *f;
22783 int x, y;
22785 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22786 enum window_part part;
22787 Lisp_Object window;
22788 struct window *w;
22789 Cursor cursor = No_Cursor;
22790 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22791 struct buffer *b;
22793 /* When a menu is active, don't highlight because this looks odd. */
22794 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22795 if (popup_activated ())
22796 return;
22797 #endif
22799 if (NILP (Vmouse_highlight)
22800 || !f->glyphs_initialized_p)
22801 return;
22803 dpyinfo->mouse_face_mouse_x = x;
22804 dpyinfo->mouse_face_mouse_y = y;
22805 dpyinfo->mouse_face_mouse_frame = f;
22807 if (dpyinfo->mouse_face_defer)
22808 return;
22810 if (gc_in_progress)
22812 dpyinfo->mouse_face_deferred_gc = 1;
22813 return;
22816 /* Which window is that in? */
22817 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22819 /* If we were displaying active text in another window, clear that.
22820 Also clear if we move out of text area in same window. */
22821 if (! EQ (window, dpyinfo->mouse_face_window)
22822 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22823 && !NILP (dpyinfo->mouse_face_window)))
22824 clear_mouse_face (dpyinfo);
22826 /* Not on a window -> return. */
22827 if (!WINDOWP (window))
22828 return;
22830 /* Reset help_echo_string. It will get recomputed below. */
22831 help_echo_string = Qnil;
22833 /* Convert to window-relative pixel coordinates. */
22834 w = XWINDOW (window);
22835 frame_to_window_pixel_xy (w, &x, &y);
22837 /* Handle tool-bar window differently since it doesn't display a
22838 buffer. */
22839 if (EQ (window, f->tool_bar_window))
22841 note_tool_bar_highlight (f, x, y);
22842 return;
22845 /* Mouse is on the mode, header line or margin? */
22846 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22847 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22849 note_mode_line_or_margin_highlight (window, x, y, part);
22850 return;
22853 if (part == ON_VERTICAL_BORDER)
22855 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22856 help_echo_string = build_string ("drag-mouse-1: resize");
22858 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22859 || part == ON_SCROLL_BAR)
22860 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22861 else
22862 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22864 /* Are we in a window whose display is up to date?
22865 And verify the buffer's text has not changed. */
22866 b = XBUFFER (w->buffer);
22867 if (part == ON_TEXT
22868 && EQ (w->window_end_valid, w->buffer)
22869 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22870 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22872 int hpos, vpos, pos, i, dx, dy, area;
22873 struct glyph *glyph;
22874 Lisp_Object object;
22875 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22876 Lisp_Object *overlay_vec = NULL;
22877 int noverlays;
22878 struct buffer *obuf;
22879 int obegv, ozv, same_region;
22881 /* Find the glyph under X/Y. */
22882 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22884 /* Look for :pointer property on image. */
22885 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22887 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22888 if (img != NULL && IMAGEP (img->spec))
22890 Lisp_Object image_map, hotspot;
22891 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22892 !NILP (image_map))
22893 && (hotspot = find_hot_spot (image_map,
22894 glyph->slice.x + dx,
22895 glyph->slice.y + dy),
22896 CONSP (hotspot))
22897 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22899 Lisp_Object area_id, plist;
22901 area_id = XCAR (hotspot);
22902 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22903 If so, we could look for mouse-enter, mouse-leave
22904 properties in PLIST (and do something...). */
22905 hotspot = XCDR (hotspot);
22906 if (CONSP (hotspot)
22907 && (plist = XCAR (hotspot), CONSP (plist)))
22909 pointer = Fplist_get (plist, Qpointer);
22910 if (NILP (pointer))
22911 pointer = Qhand;
22912 help_echo_string = Fplist_get (plist, Qhelp_echo);
22913 if (!NILP (help_echo_string))
22915 help_echo_window = window;
22916 help_echo_object = glyph->object;
22917 help_echo_pos = glyph->charpos;
22921 if (NILP (pointer))
22922 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22926 /* Clear mouse face if X/Y not over text. */
22927 if (glyph == NULL
22928 || area != TEXT_AREA
22929 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22931 if (clear_mouse_face (dpyinfo))
22932 cursor = No_Cursor;
22933 if (NILP (pointer))
22935 if (area != TEXT_AREA)
22936 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22937 else
22938 pointer = Vvoid_text_area_pointer;
22940 goto set_cursor;
22943 pos = glyph->charpos;
22944 object = glyph->object;
22945 if (!STRINGP (object) && !BUFFERP (object))
22946 goto set_cursor;
22948 /* If we get an out-of-range value, return now; avoid an error. */
22949 if (BUFFERP (object) && pos > BUF_Z (b))
22950 goto set_cursor;
22952 /* Make the window's buffer temporarily current for
22953 overlays_at and compute_char_face. */
22954 obuf = current_buffer;
22955 current_buffer = b;
22956 obegv = BEGV;
22957 ozv = ZV;
22958 BEGV = BEG;
22959 ZV = Z;
22961 /* Is this char mouse-active or does it have help-echo? */
22962 position = make_number (pos);
22964 if (BUFFERP (object))
22966 /* Put all the overlays we want in a vector in overlay_vec. */
22967 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22968 /* Sort overlays into increasing priority order. */
22969 noverlays = sort_overlays (overlay_vec, noverlays, w);
22971 else
22972 noverlays = 0;
22974 same_region = (EQ (window, dpyinfo->mouse_face_window)
22975 && vpos >= dpyinfo->mouse_face_beg_row
22976 && vpos <= dpyinfo->mouse_face_end_row
22977 && (vpos > dpyinfo->mouse_face_beg_row
22978 || hpos >= dpyinfo->mouse_face_beg_col)
22979 && (vpos < dpyinfo->mouse_face_end_row
22980 || hpos < dpyinfo->mouse_face_end_col
22981 || dpyinfo->mouse_face_past_end));
22983 if (same_region)
22984 cursor = No_Cursor;
22986 /* Check mouse-face highlighting. */
22987 if (! same_region
22988 /* If there exists an overlay with mouse-face overlapping
22989 the one we are currently highlighting, we have to
22990 check if we enter the overlapping overlay, and then
22991 highlight only that. */
22992 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22993 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22995 /* Find the highest priority overlay that has a mouse-face
22996 property. */
22997 overlay = Qnil;
22998 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23000 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23001 if (!NILP (mouse_face))
23002 overlay = overlay_vec[i];
23005 /* If we're actually highlighting the same overlay as
23006 before, there's no need to do that again. */
23007 if (!NILP (overlay)
23008 && EQ (overlay, dpyinfo->mouse_face_overlay))
23009 goto check_help_echo;
23011 dpyinfo->mouse_face_overlay = overlay;
23013 /* Clear the display of the old active region, if any. */
23014 if (clear_mouse_face (dpyinfo))
23015 cursor = No_Cursor;
23017 /* If no overlay applies, get a text property. */
23018 if (NILP (overlay))
23019 mouse_face = Fget_text_property (position, Qmouse_face, object);
23021 /* Handle the overlay case. */
23022 if (!NILP (overlay))
23024 /* Find the range of text around this char that
23025 should be active. */
23026 Lisp_Object before, after;
23027 int ignore;
23029 before = Foverlay_start (overlay);
23030 after = Foverlay_end (overlay);
23031 /* Record this as the current active region. */
23032 fast_find_position (w, XFASTINT (before),
23033 &dpyinfo->mouse_face_beg_col,
23034 &dpyinfo->mouse_face_beg_row,
23035 &dpyinfo->mouse_face_beg_x,
23036 &dpyinfo->mouse_face_beg_y, Qnil);
23038 dpyinfo->mouse_face_past_end
23039 = !fast_find_position (w, XFASTINT (after),
23040 &dpyinfo->mouse_face_end_col,
23041 &dpyinfo->mouse_face_end_row,
23042 &dpyinfo->mouse_face_end_x,
23043 &dpyinfo->mouse_face_end_y, Qnil);
23044 dpyinfo->mouse_face_window = window;
23046 dpyinfo->mouse_face_face_id
23047 = face_at_buffer_position (w, pos, 0, 0,
23048 &ignore, pos + 1,
23049 !dpyinfo->mouse_face_hidden);
23051 /* Display it as active. */
23052 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23053 cursor = No_Cursor;
23055 /* Handle the text property case. */
23056 else if (!NILP (mouse_face) && BUFFERP (object))
23058 /* Find the range of text around this char that
23059 should be active. */
23060 Lisp_Object before, after, beginning, end;
23061 int ignore;
23063 beginning = Fmarker_position (w->start);
23064 end = make_number (BUF_Z (XBUFFER (object))
23065 - XFASTINT (w->window_end_pos));
23066 before
23067 = Fprevious_single_property_change (make_number (pos + 1),
23068 Qmouse_face,
23069 object, beginning);
23070 after
23071 = Fnext_single_property_change (position, Qmouse_face,
23072 object, end);
23074 /* Record this as the current active region. */
23075 fast_find_position (w, XFASTINT (before),
23076 &dpyinfo->mouse_face_beg_col,
23077 &dpyinfo->mouse_face_beg_row,
23078 &dpyinfo->mouse_face_beg_x,
23079 &dpyinfo->mouse_face_beg_y, Qnil);
23080 dpyinfo->mouse_face_past_end
23081 = !fast_find_position (w, XFASTINT (after),
23082 &dpyinfo->mouse_face_end_col,
23083 &dpyinfo->mouse_face_end_row,
23084 &dpyinfo->mouse_face_end_x,
23085 &dpyinfo->mouse_face_end_y, Qnil);
23086 dpyinfo->mouse_face_window = window;
23088 if (BUFFERP (object))
23089 dpyinfo->mouse_face_face_id
23090 = face_at_buffer_position (w, pos, 0, 0,
23091 &ignore, pos + 1,
23092 !dpyinfo->mouse_face_hidden);
23094 /* Display it as active. */
23095 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23096 cursor = No_Cursor;
23098 else if (!NILP (mouse_face) && STRINGP (object))
23100 Lisp_Object b, e;
23101 int ignore;
23103 b = Fprevious_single_property_change (make_number (pos + 1),
23104 Qmouse_face,
23105 object, Qnil);
23106 e = Fnext_single_property_change (position, Qmouse_face,
23107 object, Qnil);
23108 if (NILP (b))
23109 b = make_number (0);
23110 if (NILP (e))
23111 e = make_number (SCHARS (object) - 1);
23113 fast_find_string_pos (w, XINT (b), object,
23114 &dpyinfo->mouse_face_beg_col,
23115 &dpyinfo->mouse_face_beg_row,
23116 &dpyinfo->mouse_face_beg_x,
23117 &dpyinfo->mouse_face_beg_y, 0);
23118 fast_find_string_pos (w, XINT (e), object,
23119 &dpyinfo->mouse_face_end_col,
23120 &dpyinfo->mouse_face_end_row,
23121 &dpyinfo->mouse_face_end_x,
23122 &dpyinfo->mouse_face_end_y, 1);
23123 dpyinfo->mouse_face_past_end = 0;
23124 dpyinfo->mouse_face_window = window;
23125 dpyinfo->mouse_face_face_id
23126 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23127 glyph->face_id, 1);
23128 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23129 cursor = No_Cursor;
23131 else if (STRINGP (object) && NILP (mouse_face))
23133 /* A string which doesn't have mouse-face, but
23134 the text ``under'' it might have. */
23135 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23136 int start = MATRIX_ROW_START_CHARPOS (r);
23138 pos = string_buffer_position (w, object, start);
23139 if (pos > 0)
23140 mouse_face = get_char_property_and_overlay (make_number (pos),
23141 Qmouse_face,
23142 w->buffer,
23143 &overlay);
23144 if (!NILP (mouse_face) && !NILP (overlay))
23146 Lisp_Object before = Foverlay_start (overlay);
23147 Lisp_Object after = Foverlay_end (overlay);
23148 int ignore;
23150 /* Note that we might not be able to find position
23151 BEFORE in the glyph matrix if the overlay is
23152 entirely covered by a `display' property. In
23153 this case, we overshoot. So let's stop in
23154 the glyph matrix before glyphs for OBJECT. */
23155 fast_find_position (w, XFASTINT (before),
23156 &dpyinfo->mouse_face_beg_col,
23157 &dpyinfo->mouse_face_beg_row,
23158 &dpyinfo->mouse_face_beg_x,
23159 &dpyinfo->mouse_face_beg_y,
23160 object);
23162 dpyinfo->mouse_face_past_end
23163 = !fast_find_position (w, XFASTINT (after),
23164 &dpyinfo->mouse_face_end_col,
23165 &dpyinfo->mouse_face_end_row,
23166 &dpyinfo->mouse_face_end_x,
23167 &dpyinfo->mouse_face_end_y,
23168 Qnil);
23169 dpyinfo->mouse_face_window = window;
23170 dpyinfo->mouse_face_face_id
23171 = face_at_buffer_position (w, pos, 0, 0,
23172 &ignore, pos + 1,
23173 !dpyinfo->mouse_face_hidden);
23175 /* Display it as active. */
23176 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23177 cursor = No_Cursor;
23182 check_help_echo:
23184 /* Look for a `help-echo' property. */
23185 if (NILP (help_echo_string)) {
23186 Lisp_Object help, overlay;
23188 /* Check overlays first. */
23189 help = overlay = Qnil;
23190 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23192 overlay = overlay_vec[i];
23193 help = Foverlay_get (overlay, Qhelp_echo);
23196 if (!NILP (help))
23198 help_echo_string = help;
23199 help_echo_window = window;
23200 help_echo_object = overlay;
23201 help_echo_pos = pos;
23203 else
23205 Lisp_Object object = glyph->object;
23206 int charpos = glyph->charpos;
23208 /* Try text properties. */
23209 if (STRINGP (object)
23210 && charpos >= 0
23211 && charpos < SCHARS (object))
23213 help = Fget_text_property (make_number (charpos),
23214 Qhelp_echo, object);
23215 if (NILP (help))
23217 /* If the string itself doesn't specify a help-echo,
23218 see if the buffer text ``under'' it does. */
23219 struct glyph_row *r
23220 = MATRIX_ROW (w->current_matrix, vpos);
23221 int start = MATRIX_ROW_START_CHARPOS (r);
23222 int pos = string_buffer_position (w, object, start);
23223 if (pos > 0)
23225 help = Fget_char_property (make_number (pos),
23226 Qhelp_echo, w->buffer);
23227 if (!NILP (help))
23229 charpos = pos;
23230 object = w->buffer;
23235 else if (BUFFERP (object)
23236 && charpos >= BEGV
23237 && charpos < ZV)
23238 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23239 object);
23241 if (!NILP (help))
23243 help_echo_string = help;
23244 help_echo_window = window;
23245 help_echo_object = object;
23246 help_echo_pos = charpos;
23251 /* Look for a `pointer' property. */
23252 if (NILP (pointer))
23254 /* Check overlays first. */
23255 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23256 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23258 if (NILP (pointer))
23260 Lisp_Object object = glyph->object;
23261 int charpos = glyph->charpos;
23263 /* Try text properties. */
23264 if (STRINGP (object)
23265 && charpos >= 0
23266 && charpos < SCHARS (object))
23268 pointer = Fget_text_property (make_number (charpos),
23269 Qpointer, object);
23270 if (NILP (pointer))
23272 /* If the string itself doesn't specify a pointer,
23273 see if the buffer text ``under'' it does. */
23274 struct glyph_row *r
23275 = MATRIX_ROW (w->current_matrix, vpos);
23276 int start = MATRIX_ROW_START_CHARPOS (r);
23277 int pos = string_buffer_position (w, object, start);
23278 if (pos > 0)
23279 pointer = Fget_char_property (make_number (pos),
23280 Qpointer, w->buffer);
23283 else if (BUFFERP (object)
23284 && charpos >= BEGV
23285 && charpos < ZV)
23286 pointer = Fget_text_property (make_number (charpos),
23287 Qpointer, object);
23291 BEGV = obegv;
23292 ZV = ozv;
23293 current_buffer = obuf;
23296 set_cursor:
23298 define_frame_cursor1 (f, cursor, pointer);
23302 /* EXPORT for RIF:
23303 Clear any mouse-face on window W. This function is part of the
23304 redisplay interface, and is called from try_window_id and similar
23305 functions to ensure the mouse-highlight is off. */
23307 void
23308 x_clear_window_mouse_face (w)
23309 struct window *w;
23311 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23312 Lisp_Object window;
23314 BLOCK_INPUT;
23315 XSETWINDOW (window, w);
23316 if (EQ (window, dpyinfo->mouse_face_window))
23317 clear_mouse_face (dpyinfo);
23318 UNBLOCK_INPUT;
23322 /* EXPORT:
23323 Just discard the mouse face information for frame F, if any.
23324 This is used when the size of F is changed. */
23326 void
23327 cancel_mouse_face (f)
23328 struct frame *f;
23330 Lisp_Object window;
23331 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23333 window = dpyinfo->mouse_face_window;
23334 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23336 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23337 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23338 dpyinfo->mouse_face_window = Qnil;
23343 #endif /* HAVE_WINDOW_SYSTEM */
23346 /***********************************************************************
23347 Exposure Events
23348 ***********************************************************************/
23350 #ifdef HAVE_WINDOW_SYSTEM
23352 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23353 which intersects rectangle R. R is in window-relative coordinates. */
23355 static void
23356 expose_area (w, row, r, area)
23357 struct window *w;
23358 struct glyph_row *row;
23359 XRectangle *r;
23360 enum glyph_row_area area;
23362 struct glyph *first = row->glyphs[area];
23363 struct glyph *end = row->glyphs[area] + row->used[area];
23364 struct glyph *last;
23365 int first_x, start_x, x;
23367 if (area == TEXT_AREA && row->fill_line_p)
23368 /* If row extends face to end of line write the whole line. */
23369 draw_glyphs (w, 0, row, area,
23370 0, row->used[area],
23371 DRAW_NORMAL_TEXT, 0);
23372 else
23374 /* Set START_X to the window-relative start position for drawing glyphs of
23375 AREA. The first glyph of the text area can be partially visible.
23376 The first glyphs of other areas cannot. */
23377 start_x = window_box_left_offset (w, area);
23378 x = start_x;
23379 if (area == TEXT_AREA)
23380 x += row->x;
23382 /* Find the first glyph that must be redrawn. */
23383 while (first < end
23384 && x + first->pixel_width < r->x)
23386 x += first->pixel_width;
23387 ++first;
23390 /* Find the last one. */
23391 last = first;
23392 first_x = x;
23393 while (last < end
23394 && x < r->x + r->width)
23396 x += last->pixel_width;
23397 ++last;
23400 /* Repaint. */
23401 if (last > first)
23402 draw_glyphs (w, first_x - start_x, row, area,
23403 first - row->glyphs[area], last - row->glyphs[area],
23404 DRAW_NORMAL_TEXT, 0);
23409 /* Redraw the parts of the glyph row ROW on window W intersecting
23410 rectangle R. R is in window-relative coordinates. Value is
23411 non-zero if mouse-face was overwritten. */
23413 static int
23414 expose_line (w, row, r)
23415 struct window *w;
23416 struct glyph_row *row;
23417 XRectangle *r;
23419 xassert (row->enabled_p);
23421 if (row->mode_line_p || w->pseudo_window_p)
23422 draw_glyphs (w, 0, row, TEXT_AREA,
23423 0, row->used[TEXT_AREA],
23424 DRAW_NORMAL_TEXT, 0);
23425 else
23427 if (row->used[LEFT_MARGIN_AREA])
23428 expose_area (w, row, r, LEFT_MARGIN_AREA);
23429 if (row->used[TEXT_AREA])
23430 expose_area (w, row, r, TEXT_AREA);
23431 if (row->used[RIGHT_MARGIN_AREA])
23432 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23433 draw_row_fringe_bitmaps (w, row);
23436 return row->mouse_face_p;
23440 /* Redraw those parts of glyphs rows during expose event handling that
23441 overlap other rows. Redrawing of an exposed line writes over parts
23442 of lines overlapping that exposed line; this function fixes that.
23444 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23445 row in W's current matrix that is exposed and overlaps other rows.
23446 LAST_OVERLAPPING_ROW is the last such row. */
23448 static void
23449 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23450 struct window *w;
23451 struct glyph_row *first_overlapping_row;
23452 struct glyph_row *last_overlapping_row;
23454 struct glyph_row *row;
23456 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23457 if (row->overlapping_p)
23459 xassert (row->enabled_p && !row->mode_line_p);
23461 if (row->used[LEFT_MARGIN_AREA])
23462 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23464 if (row->used[TEXT_AREA])
23465 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23467 if (row->used[RIGHT_MARGIN_AREA])
23468 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23473 /* Return non-zero if W's cursor intersects rectangle R. */
23475 static int
23476 phys_cursor_in_rect_p (w, r)
23477 struct window *w;
23478 XRectangle *r;
23480 XRectangle cr, result;
23481 struct glyph *cursor_glyph;
23482 struct glyph_row *row;
23484 if (w->phys_cursor.vpos >= 0
23485 && w->phys_cursor.vpos < w->current_matrix->nrows
23486 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23487 row->enabled_p)
23488 && row->cursor_in_fringe_p)
23490 /* Cursor is in the fringe. */
23491 cr.x = window_box_right_offset (w,
23492 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23493 ? RIGHT_MARGIN_AREA
23494 : TEXT_AREA));
23495 cr.y = row->y;
23496 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23497 cr.height = row->height;
23498 return x_intersect_rectangles (&cr, r, &result);
23501 cursor_glyph = get_phys_cursor_glyph (w);
23502 if (cursor_glyph)
23504 /* r is relative to W's box, but w->phys_cursor.x is relative
23505 to left edge of W's TEXT area. Adjust it. */
23506 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23507 cr.y = w->phys_cursor.y;
23508 cr.width = cursor_glyph->pixel_width;
23509 cr.height = w->phys_cursor_height;
23510 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23511 I assume the effect is the same -- and this is portable. */
23512 return x_intersect_rectangles (&cr, r, &result);
23514 else
23515 return 0;
23519 /* EXPORT:
23520 Draw a vertical window border to the right of window W if W doesn't
23521 have vertical scroll bars. */
23523 void
23524 x_draw_vertical_border (w)
23525 struct window *w;
23527 /* We could do better, if we knew what type of scroll-bar the adjacent
23528 windows (on either side) have... But we don't :-(
23529 However, I think this works ok. ++KFS 2003-04-25 */
23531 /* Redraw borders between horizontally adjacent windows. Don't
23532 do it for frames with vertical scroll bars because either the
23533 right scroll bar of a window, or the left scroll bar of its
23534 neighbor will suffice as a border. */
23535 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23536 return;
23538 if (!WINDOW_RIGHTMOST_P (w)
23539 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23541 int x0, x1, y0, y1;
23543 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23544 y1 -= 1;
23546 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23547 x1 -= 1;
23549 rif->draw_vertical_window_border (w, x1, y0, y1);
23551 else if (!WINDOW_LEFTMOST_P (w)
23552 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23554 int x0, x1, y0, y1;
23556 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23557 y1 -= 1;
23559 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23560 x0 -= 1;
23562 rif->draw_vertical_window_border (w, x0, y0, y1);
23567 /* Redraw the part of window W intersection rectangle FR. Pixel
23568 coordinates in FR are frame-relative. Call this function with
23569 input blocked. Value is non-zero if the exposure overwrites
23570 mouse-face. */
23572 static int
23573 expose_window (w, fr)
23574 struct window *w;
23575 XRectangle *fr;
23577 struct frame *f = XFRAME (w->frame);
23578 XRectangle wr, r;
23579 int mouse_face_overwritten_p = 0;
23581 /* If window is not yet fully initialized, do nothing. This can
23582 happen when toolkit scroll bars are used and a window is split.
23583 Reconfiguring the scroll bar will generate an expose for a newly
23584 created window. */
23585 if (w->current_matrix == NULL)
23586 return 0;
23588 /* When we're currently updating the window, display and current
23589 matrix usually don't agree. Arrange for a thorough display
23590 later. */
23591 if (w == updated_window)
23593 SET_FRAME_GARBAGED (f);
23594 return 0;
23597 /* Frame-relative pixel rectangle of W. */
23598 wr.x = WINDOW_LEFT_EDGE_X (w);
23599 wr.y = WINDOW_TOP_EDGE_Y (w);
23600 wr.width = WINDOW_TOTAL_WIDTH (w);
23601 wr.height = WINDOW_TOTAL_HEIGHT (w);
23603 if (x_intersect_rectangles (fr, &wr, &r))
23605 int yb = window_text_bottom_y (w);
23606 struct glyph_row *row;
23607 int cursor_cleared_p;
23608 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23610 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23611 r.x, r.y, r.width, r.height));
23613 /* Convert to window coordinates. */
23614 r.x -= WINDOW_LEFT_EDGE_X (w);
23615 r.y -= WINDOW_TOP_EDGE_Y (w);
23617 /* Turn off the cursor. */
23618 if (!w->pseudo_window_p
23619 && phys_cursor_in_rect_p (w, &r))
23621 x_clear_cursor (w);
23622 cursor_cleared_p = 1;
23624 else
23625 cursor_cleared_p = 0;
23627 /* Update lines intersecting rectangle R. */
23628 first_overlapping_row = last_overlapping_row = NULL;
23629 for (row = w->current_matrix->rows;
23630 row->enabled_p;
23631 ++row)
23633 int y0 = row->y;
23634 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23636 if ((y0 >= r.y && y0 < r.y + r.height)
23637 || (y1 > r.y && y1 < r.y + r.height)
23638 || (r.y >= y0 && r.y < y1)
23639 || (r.y + r.height > y0 && r.y + r.height < y1))
23641 /* A header line may be overlapping, but there is no need
23642 to fix overlapping areas for them. KFS 2005-02-12 */
23643 if (row->overlapping_p && !row->mode_line_p)
23645 if (first_overlapping_row == NULL)
23646 first_overlapping_row = row;
23647 last_overlapping_row = row;
23650 if (expose_line (w, row, &r))
23651 mouse_face_overwritten_p = 1;
23654 if (y1 >= yb)
23655 break;
23658 /* Display the mode line if there is one. */
23659 if (WINDOW_WANTS_MODELINE_P (w)
23660 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23661 row->enabled_p)
23662 && row->y < r.y + r.height)
23664 if (expose_line (w, row, &r))
23665 mouse_face_overwritten_p = 1;
23668 if (!w->pseudo_window_p)
23670 /* Fix the display of overlapping rows. */
23671 if (first_overlapping_row)
23672 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23674 /* Draw border between windows. */
23675 x_draw_vertical_border (w);
23677 /* Turn the cursor on again. */
23678 if (cursor_cleared_p)
23679 update_window_cursor (w, 1);
23683 return mouse_face_overwritten_p;
23688 /* Redraw (parts) of all windows in the window tree rooted at W that
23689 intersect R. R contains frame pixel coordinates. Value is
23690 non-zero if the exposure overwrites mouse-face. */
23692 static int
23693 expose_window_tree (w, r)
23694 struct window *w;
23695 XRectangle *r;
23697 struct frame *f = XFRAME (w->frame);
23698 int mouse_face_overwritten_p = 0;
23700 while (w && !FRAME_GARBAGED_P (f))
23702 if (!NILP (w->hchild))
23703 mouse_face_overwritten_p
23704 |= expose_window_tree (XWINDOW (w->hchild), r);
23705 else if (!NILP (w->vchild))
23706 mouse_face_overwritten_p
23707 |= expose_window_tree (XWINDOW (w->vchild), r);
23708 else
23709 mouse_face_overwritten_p |= expose_window (w, r);
23711 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23714 return mouse_face_overwritten_p;
23718 /* EXPORT:
23719 Redisplay an exposed area of frame F. X and Y are the upper-left
23720 corner of the exposed rectangle. W and H are width and height of
23721 the exposed area. All are pixel values. W or H zero means redraw
23722 the entire frame. */
23724 void
23725 expose_frame (f, x, y, w, h)
23726 struct frame *f;
23727 int x, y, w, h;
23729 XRectangle r;
23730 int mouse_face_overwritten_p = 0;
23732 TRACE ((stderr, "expose_frame "));
23734 /* No need to redraw if frame will be redrawn soon. */
23735 if (FRAME_GARBAGED_P (f))
23737 TRACE ((stderr, " garbaged\n"));
23738 return;
23741 /* If basic faces haven't been realized yet, there is no point in
23742 trying to redraw anything. This can happen when we get an expose
23743 event while Emacs is starting, e.g. by moving another window. */
23744 if (FRAME_FACE_CACHE (f) == NULL
23745 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23747 TRACE ((stderr, " no faces\n"));
23748 return;
23751 if (w == 0 || h == 0)
23753 r.x = r.y = 0;
23754 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23755 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23757 else
23759 r.x = x;
23760 r.y = y;
23761 r.width = w;
23762 r.height = h;
23765 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23766 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23768 if (WINDOWP (f->tool_bar_window))
23769 mouse_face_overwritten_p
23770 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23772 #ifdef HAVE_X_WINDOWS
23773 #ifndef MSDOS
23774 #ifndef USE_X_TOOLKIT
23775 if (WINDOWP (f->menu_bar_window))
23776 mouse_face_overwritten_p
23777 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23778 #endif /* not USE_X_TOOLKIT */
23779 #endif
23780 #endif
23782 /* Some window managers support a focus-follows-mouse style with
23783 delayed raising of frames. Imagine a partially obscured frame,
23784 and moving the mouse into partially obscured mouse-face on that
23785 frame. The visible part of the mouse-face will be highlighted,
23786 then the WM raises the obscured frame. With at least one WM, KDE
23787 2.1, Emacs is not getting any event for the raising of the frame
23788 (even tried with SubstructureRedirectMask), only Expose events.
23789 These expose events will draw text normally, i.e. not
23790 highlighted. Which means we must redo the highlight here.
23791 Subsume it under ``we love X''. --gerd 2001-08-15 */
23792 /* Included in Windows version because Windows most likely does not
23793 do the right thing if any third party tool offers
23794 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23795 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23797 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23798 if (f == dpyinfo->mouse_face_mouse_frame)
23800 int x = dpyinfo->mouse_face_mouse_x;
23801 int y = dpyinfo->mouse_face_mouse_y;
23802 clear_mouse_face (dpyinfo);
23803 note_mouse_highlight (f, x, y);
23809 /* EXPORT:
23810 Determine the intersection of two rectangles R1 and R2. Return
23811 the intersection in *RESULT. Value is non-zero if RESULT is not
23812 empty. */
23815 x_intersect_rectangles (r1, r2, result)
23816 XRectangle *r1, *r2, *result;
23818 XRectangle *left, *right;
23819 XRectangle *upper, *lower;
23820 int intersection_p = 0;
23822 /* Rearrange so that R1 is the left-most rectangle. */
23823 if (r1->x < r2->x)
23824 left = r1, right = r2;
23825 else
23826 left = r2, right = r1;
23828 /* X0 of the intersection is right.x0, if this is inside R1,
23829 otherwise there is no intersection. */
23830 if (right->x <= left->x + left->width)
23832 result->x = right->x;
23834 /* The right end of the intersection is the minimum of the
23835 the right ends of left and right. */
23836 result->width = (min (left->x + left->width, right->x + right->width)
23837 - result->x);
23839 /* Same game for Y. */
23840 if (r1->y < r2->y)
23841 upper = r1, lower = r2;
23842 else
23843 upper = r2, lower = r1;
23845 /* The upper end of the intersection is lower.y0, if this is inside
23846 of upper. Otherwise, there is no intersection. */
23847 if (lower->y <= upper->y + upper->height)
23849 result->y = lower->y;
23851 /* The lower end of the intersection is the minimum of the lower
23852 ends of upper and lower. */
23853 result->height = (min (lower->y + lower->height,
23854 upper->y + upper->height)
23855 - result->y);
23856 intersection_p = 1;
23860 return intersection_p;
23863 #endif /* HAVE_WINDOW_SYSTEM */
23866 /***********************************************************************
23867 Initialization
23868 ***********************************************************************/
23870 void
23871 syms_of_xdisp ()
23873 Vwith_echo_area_save_vector = Qnil;
23874 staticpro (&Vwith_echo_area_save_vector);
23876 Vmessage_stack = Qnil;
23877 staticpro (&Vmessage_stack);
23879 Qinhibit_redisplay = intern ("inhibit-redisplay");
23880 staticpro (&Qinhibit_redisplay);
23882 message_dolog_marker1 = Fmake_marker ();
23883 staticpro (&message_dolog_marker1);
23884 message_dolog_marker2 = Fmake_marker ();
23885 staticpro (&message_dolog_marker2);
23886 message_dolog_marker3 = Fmake_marker ();
23887 staticpro (&message_dolog_marker3);
23889 #if GLYPH_DEBUG
23890 defsubr (&Sdump_frame_glyph_matrix);
23891 defsubr (&Sdump_glyph_matrix);
23892 defsubr (&Sdump_glyph_row);
23893 defsubr (&Sdump_tool_bar_row);
23894 defsubr (&Strace_redisplay);
23895 defsubr (&Strace_to_stderr);
23896 #endif
23897 #ifdef HAVE_WINDOW_SYSTEM
23898 defsubr (&Stool_bar_lines_needed);
23899 defsubr (&Slookup_image_map);
23900 #endif
23901 defsubr (&Sformat_mode_line);
23903 staticpro (&Qmenu_bar_update_hook);
23904 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23906 staticpro (&Qoverriding_terminal_local_map);
23907 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23909 staticpro (&Qoverriding_local_map);
23910 Qoverriding_local_map = intern ("overriding-local-map");
23912 staticpro (&Qwindow_scroll_functions);
23913 Qwindow_scroll_functions = intern ("window-scroll-functions");
23915 staticpro (&Qredisplay_end_trigger_functions);
23916 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23918 staticpro (&Qinhibit_point_motion_hooks);
23919 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23921 QCdata = intern (":data");
23922 staticpro (&QCdata);
23923 Qdisplay = intern ("display");
23924 staticpro (&Qdisplay);
23925 Qspace_width = intern ("space-width");
23926 staticpro (&Qspace_width);
23927 Qraise = intern ("raise");
23928 staticpro (&Qraise);
23929 Qslice = intern ("slice");
23930 staticpro (&Qslice);
23931 Qspace = intern ("space");
23932 staticpro (&Qspace);
23933 Qmargin = intern ("margin");
23934 staticpro (&Qmargin);
23935 Qpointer = intern ("pointer");
23936 staticpro (&Qpointer);
23937 Qleft_margin = intern ("left-margin");
23938 staticpro (&Qleft_margin);
23939 Qright_margin = intern ("right-margin");
23940 staticpro (&Qright_margin);
23941 Qcenter = intern ("center");
23942 staticpro (&Qcenter);
23943 Qline_height = intern ("line-height");
23944 staticpro (&Qline_height);
23945 QCalign_to = intern (":align-to");
23946 staticpro (&QCalign_to);
23947 QCrelative_width = intern (":relative-width");
23948 staticpro (&QCrelative_width);
23949 QCrelative_height = intern (":relative-height");
23950 staticpro (&QCrelative_height);
23951 QCeval = intern (":eval");
23952 staticpro (&QCeval);
23953 QCpropertize = intern (":propertize");
23954 staticpro (&QCpropertize);
23955 QCfile = intern (":file");
23956 staticpro (&QCfile);
23957 Qfontified = intern ("fontified");
23958 staticpro (&Qfontified);
23959 Qfontification_functions = intern ("fontification-functions");
23960 staticpro (&Qfontification_functions);
23961 Qtrailing_whitespace = intern ("trailing-whitespace");
23962 staticpro (&Qtrailing_whitespace);
23963 Qescape_glyph = intern ("escape-glyph");
23964 staticpro (&Qescape_glyph);
23965 Qnobreak_space = intern ("nobreak-space");
23966 staticpro (&Qnobreak_space);
23967 Qimage = intern ("image");
23968 staticpro (&Qimage);
23969 QCmap = intern (":map");
23970 staticpro (&QCmap);
23971 QCpointer = intern (":pointer");
23972 staticpro (&QCpointer);
23973 Qrect = intern ("rect");
23974 staticpro (&Qrect);
23975 Qcircle = intern ("circle");
23976 staticpro (&Qcircle);
23977 Qpoly = intern ("poly");
23978 staticpro (&Qpoly);
23979 Qmessage_truncate_lines = intern ("message-truncate-lines");
23980 staticpro (&Qmessage_truncate_lines);
23981 Qgrow_only = intern ("grow-only");
23982 staticpro (&Qgrow_only);
23983 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23984 staticpro (&Qinhibit_menubar_update);
23985 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23986 staticpro (&Qinhibit_eval_during_redisplay);
23987 Qposition = intern ("position");
23988 staticpro (&Qposition);
23989 Qbuffer_position = intern ("buffer-position");
23990 staticpro (&Qbuffer_position);
23991 Qobject = intern ("object");
23992 staticpro (&Qobject);
23993 Qbar = intern ("bar");
23994 staticpro (&Qbar);
23995 Qhbar = intern ("hbar");
23996 staticpro (&Qhbar);
23997 Qbox = intern ("box");
23998 staticpro (&Qbox);
23999 Qhollow = intern ("hollow");
24000 staticpro (&Qhollow);
24001 Qhand = intern ("hand");
24002 staticpro (&Qhand);
24003 Qarrow = intern ("arrow");
24004 staticpro (&Qarrow);
24005 Qtext = intern ("text");
24006 staticpro (&Qtext);
24007 Qrisky_local_variable = intern ("risky-local-variable");
24008 staticpro (&Qrisky_local_variable);
24009 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24010 staticpro (&Qinhibit_free_realized_faces);
24012 list_of_error = Fcons (Fcons (intern ("error"),
24013 Fcons (intern ("void-variable"), Qnil)),
24014 Qnil);
24015 staticpro (&list_of_error);
24017 Qlast_arrow_position = intern ("last-arrow-position");
24018 staticpro (&Qlast_arrow_position);
24019 Qlast_arrow_string = intern ("last-arrow-string");
24020 staticpro (&Qlast_arrow_string);
24022 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24023 staticpro (&Qoverlay_arrow_string);
24024 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24025 staticpro (&Qoverlay_arrow_bitmap);
24027 echo_buffer[0] = echo_buffer[1] = Qnil;
24028 staticpro (&echo_buffer[0]);
24029 staticpro (&echo_buffer[1]);
24031 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24032 staticpro (&echo_area_buffer[0]);
24033 staticpro (&echo_area_buffer[1]);
24035 Vmessages_buffer_name = build_string ("*Messages*");
24036 staticpro (&Vmessages_buffer_name);
24038 mode_line_proptrans_alist = Qnil;
24039 staticpro (&mode_line_proptrans_alist);
24040 mode_line_string_list = Qnil;
24041 staticpro (&mode_line_string_list);
24042 mode_line_string_face = Qnil;
24043 staticpro (&mode_line_string_face);
24044 mode_line_string_face_prop = Qnil;
24045 staticpro (&mode_line_string_face_prop);
24046 Vmode_line_unwind_vector = Qnil;
24047 staticpro (&Vmode_line_unwind_vector);
24049 help_echo_string = Qnil;
24050 staticpro (&help_echo_string);
24051 help_echo_object = Qnil;
24052 staticpro (&help_echo_object);
24053 help_echo_window = Qnil;
24054 staticpro (&help_echo_window);
24055 previous_help_echo_string = Qnil;
24056 staticpro (&previous_help_echo_string);
24057 help_echo_pos = -1;
24059 #ifdef HAVE_WINDOW_SYSTEM
24060 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24061 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24062 For example, if a block cursor is over a tab, it will be drawn as
24063 wide as that tab on the display. */);
24064 x_stretch_cursor_p = 0;
24065 #endif
24067 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24068 doc: /* *Non-nil means highlight trailing whitespace.
24069 The face used for trailing whitespace is `trailing-whitespace'. */);
24070 Vshow_trailing_whitespace = Qnil;
24072 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24073 doc: /* *Control highlighting of nobreak space and soft hyphen.
24074 A value of t means highlight the character itself (for nobreak space,
24075 use face `nobreak-space').
24076 A value of nil means no highlighting.
24077 Other values mean display the escape glyph followed by an ordinary
24078 space or ordinary hyphen. */);
24079 Vnobreak_char_display = Qt;
24081 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24082 doc: /* *The pointer shape to show in void text areas.
24083 A value of nil means to show the text pointer. Other options are `arrow',
24084 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24085 Vvoid_text_area_pointer = Qarrow;
24087 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24088 doc: /* Non-nil means don't actually do any redisplay.
24089 This is used for internal purposes. */);
24090 Vinhibit_redisplay = Qnil;
24092 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24093 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24094 Vglobal_mode_string = Qnil;
24096 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24097 doc: /* Marker for where to display an arrow on top of the buffer text.
24098 This must be the beginning of a line in order to work.
24099 See also `overlay-arrow-string'. */);
24100 Voverlay_arrow_position = Qnil;
24102 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24103 doc: /* String to display as an arrow in non-window frames.
24104 See also `overlay-arrow-position'. */);
24105 Voverlay_arrow_string = build_string ("=>");
24107 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24108 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24109 The symbols on this list are examined during redisplay to determine
24110 where to display overlay arrows. */);
24111 Voverlay_arrow_variable_list
24112 = Fcons (intern ("overlay-arrow-position"), Qnil);
24114 DEFVAR_INT ("scroll-step", &scroll_step,
24115 doc: /* *The number of lines to try scrolling a window by when point moves out.
24116 If that fails to bring point back on frame, point is centered instead.
24117 If this is zero, point is always centered after it moves off frame.
24118 If you want scrolling to always be a line at a time, you should set
24119 `scroll-conservatively' to a large value rather than set this to 1. */);
24121 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24122 doc: /* *Scroll up to this many lines, to bring point back on screen.
24123 If point moves off-screen, redisplay will scroll by up to
24124 `scroll-conservatively' lines in order to bring point just barely
24125 onto the screen again. If that cannot be done, then redisplay
24126 recenters point as usual.
24128 A value of zero means always recenter point if it moves off screen. */);
24129 scroll_conservatively = 0;
24131 DEFVAR_INT ("scroll-margin", &scroll_margin,
24132 doc: /* *Number of lines of margin at the top and bottom of a window.
24133 Recenter the window whenever point gets within this many lines
24134 of the top or bottom of the window. */);
24135 scroll_margin = 0;
24137 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24138 doc: /* Pixels per inch value for non-window system displays.
24139 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24140 Vdisplay_pixels_per_inch = make_float (72.0);
24142 #if GLYPH_DEBUG
24143 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24144 #endif
24146 DEFVAR_BOOL ("truncate-partial-width-windows",
24147 &truncate_partial_width_windows,
24148 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24149 truncate_partial_width_windows = 1;
24151 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24152 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24153 Any other value means to use the appropriate face, `mode-line',
24154 `header-line', or `menu' respectively. */);
24155 mode_line_inverse_video = 1;
24157 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24158 doc: /* *Maximum buffer size for which line number should be displayed.
24159 If the buffer is bigger than this, the line number does not appear
24160 in the mode line. A value of nil means no limit. */);
24161 Vline_number_display_limit = Qnil;
24163 DEFVAR_INT ("line-number-display-limit-width",
24164 &line_number_display_limit_width,
24165 doc: /* *Maximum line width (in characters) for line number display.
24166 If the average length of the lines near point is bigger than this, then the
24167 line number may be omitted from the mode line. */);
24168 line_number_display_limit_width = 200;
24170 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24171 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24172 highlight_nonselected_windows = 0;
24174 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24175 doc: /* Non-nil if more than one frame is visible on this display.
24176 Minibuffer-only frames don't count, but iconified frames do.
24177 This variable is not guaranteed to be accurate except while processing
24178 `frame-title-format' and `icon-title-format'. */);
24180 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24181 doc: /* Template for displaying the title bar of visible frames.
24182 \(Assuming the window manager supports this feature.)
24184 This variable has the same structure as `mode-line-format', except that
24185 the %c and %l constructs are ignored. It is used only on frames for
24186 which no explicit name has been set \(see `modify-frame-parameters'). */);
24188 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24189 doc: /* Template for displaying the title bar of an iconified frame.
24190 \(Assuming the window manager supports this feature.)
24191 This variable has the same structure as `mode-line-format' (which see),
24192 and is used only on frames for which no explicit name has been set
24193 \(see `modify-frame-parameters'). */);
24194 Vicon_title_format
24195 = Vframe_title_format
24196 = Fcons (intern ("multiple-frames"),
24197 Fcons (build_string ("%b"),
24198 Fcons (Fcons (empty_string,
24199 Fcons (intern ("invocation-name"),
24200 Fcons (build_string ("@"),
24201 Fcons (intern ("system-name"),
24202 Qnil)))),
24203 Qnil)));
24205 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24206 doc: /* Maximum number of lines to keep in the message log buffer.
24207 If nil, disable message logging. If t, log messages but don't truncate
24208 the buffer when it becomes large. */);
24209 Vmessage_log_max = make_number (100);
24211 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24212 doc: /* Functions called before redisplay, if window sizes have changed.
24213 The value should be a list of functions that take one argument.
24214 Just before redisplay, for each frame, if any of its windows have changed
24215 size since the last redisplay, or have been split or deleted,
24216 all the functions in the list are called, with the frame as argument. */);
24217 Vwindow_size_change_functions = Qnil;
24219 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24220 doc: /* List of functions to call before redisplaying a window with scrolling.
24221 Each function is called with two arguments, the window
24222 and its new display-start position. Note that the value of `window-end'
24223 is not valid when these functions are called. */);
24224 Vwindow_scroll_functions = Qnil;
24226 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24227 doc: /* Functions called when redisplay of a window reaches the end trigger.
24228 Each function is called with two arguments, the window and the end trigger value.
24229 See `set-window-redisplay-end-trigger'. */);
24230 Vredisplay_end_trigger_functions = Qnil;
24232 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24233 doc: /* *Non-nil means autoselect window with mouse pointer.
24234 If nil, do not autoselect windows.
24235 A positive number means delay autoselection by that many seconds: a
24236 window is autoselected only after the mouse has remained in that
24237 window for the duration of the delay.
24238 A negative number has a similar effect, but causes windows to be
24239 autoselected only after the mouse has stopped moving. \(Because of
24240 the way Emacs compares mouse events, you will occasionally wait twice
24241 that time before the window gets selected.\)
24242 Any other value means to autoselect window instantaneously when the
24243 mouse pointer enters it.
24245 Autoselection selects the minibuffer only if it is active, and never
24246 unselects the minibuffer if it is active.
24248 When customizing this variable make sure that the actual value of
24249 `focus-follows-mouse' matches the behavior of your window manager. */);
24250 Vmouse_autoselect_window = Qnil;
24252 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24253 doc: /* *Non-nil means automatically resize tool-bars.
24254 This dynamically changes the tool-bar's height to the minimum height
24255 that is needed to make all tool-bar items visible.
24256 If value is `grow-only', the tool-bar's height is only increased
24257 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24258 Vauto_resize_tool_bars = Qt;
24260 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24261 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24262 auto_raise_tool_bar_buttons_p = 1;
24264 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24265 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24266 make_cursor_line_fully_visible_p = 1;
24268 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24269 doc: /* *Border below tool-bar in pixels.
24270 If an integer, use it as the height of the border.
24271 If it is one of `internal-border-width' or `border-width', use the
24272 value of the corresponding frame parameter.
24273 Otherwise, no border is added below the tool-bar. */);
24274 Vtool_bar_border = Qinternal_border_width;
24276 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24277 doc: /* *Margin around tool-bar buttons in pixels.
24278 If an integer, use that for both horizontal and vertical margins.
24279 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24280 HORZ specifying the horizontal margin, and VERT specifying the
24281 vertical margin. */);
24282 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24284 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24285 doc: /* *Relief thickness of tool-bar buttons. */);
24286 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24288 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24289 doc: /* List of functions to call to fontify regions of text.
24290 Each function is called with one argument POS. Functions must
24291 fontify a region starting at POS in the current buffer, and give
24292 fontified regions the property `fontified'. */);
24293 Vfontification_functions = Qnil;
24294 Fmake_variable_buffer_local (Qfontification_functions);
24296 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24297 &unibyte_display_via_language_environment,
24298 doc: /* *Non-nil means display unibyte text according to language environment.
24299 Specifically this means that unibyte non-ASCII characters
24300 are displayed by converting them to the equivalent multibyte characters
24301 according to the current language environment. As a result, they are
24302 displayed according to the current fontset. */);
24303 unibyte_display_via_language_environment = 0;
24305 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24306 doc: /* *Maximum height for resizing mini-windows.
24307 If a float, it specifies a fraction of the mini-window frame's height.
24308 If an integer, it specifies a number of lines. */);
24309 Vmax_mini_window_height = make_float (0.25);
24311 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24312 doc: /* *How to resize mini-windows.
24313 A value of nil means don't automatically resize mini-windows.
24314 A value of t means resize them to fit the text displayed in them.
24315 A value of `grow-only', the default, means let mini-windows grow
24316 only, until their display becomes empty, at which point the windows
24317 go back to their normal size. */);
24318 Vresize_mini_windows = Qgrow_only;
24320 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24321 doc: /* Alist specifying how to blink the cursor off.
24322 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24323 `cursor-type' frame-parameter or variable equals ON-STATE,
24324 comparing using `equal', Emacs uses OFF-STATE to specify
24325 how to blink it off. ON-STATE and OFF-STATE are values for
24326 the `cursor-type' frame parameter.
24328 If a frame's ON-STATE has no entry in this list,
24329 the frame's other specifications determine how to blink the cursor off. */);
24330 Vblink_cursor_alist = Qnil;
24332 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24333 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24334 automatic_hscrolling_p = 1;
24336 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24337 doc: /* *How many columns away from the window edge point is allowed to get
24338 before automatic hscrolling will horizontally scroll the window. */);
24339 hscroll_margin = 5;
24341 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24342 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24343 When point is less than `hscroll-margin' columns from the window
24344 edge, automatic hscrolling will scroll the window by the amount of columns
24345 determined by this variable. If its value is a positive integer, scroll that
24346 many columns. If it's a positive floating-point number, it specifies the
24347 fraction of the window's width to scroll. If it's nil or zero, point will be
24348 centered horizontally after the scroll. Any other value, including negative
24349 numbers, are treated as if the value were zero.
24351 Automatic hscrolling always moves point outside the scroll margin, so if
24352 point was more than scroll step columns inside the margin, the window will
24353 scroll more than the value given by the scroll step.
24355 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24356 and `scroll-right' overrides this variable's effect. */);
24357 Vhscroll_step = make_number (0);
24359 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24360 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24361 Bind this around calls to `message' to let it take effect. */);
24362 message_truncate_lines = 0;
24364 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24365 doc: /* Normal hook run to update the menu bar definitions.
24366 Redisplay runs this hook before it redisplays the menu bar.
24367 This is used to update submenus such as Buffers,
24368 whose contents depend on various data. */);
24369 Vmenu_bar_update_hook = Qnil;
24371 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24372 doc: /* Frame for which we are updating a menu.
24373 The enable predicate for a menu binding should check this variable. */);
24374 Vmenu_updating_frame = Qnil;
24376 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24377 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24378 inhibit_menubar_update = 0;
24380 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24381 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24382 inhibit_eval_during_redisplay = 0;
24384 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24385 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24386 inhibit_free_realized_faces = 0;
24388 #if GLYPH_DEBUG
24389 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24390 doc: /* Inhibit try_window_id display optimization. */);
24391 inhibit_try_window_id = 0;
24393 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24394 doc: /* Inhibit try_window_reusing display optimization. */);
24395 inhibit_try_window_reusing = 0;
24397 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24398 doc: /* Inhibit try_cursor_movement display optimization. */);
24399 inhibit_try_cursor_movement = 0;
24400 #endif /* GLYPH_DEBUG */
24402 DEFVAR_INT ("overline-margin", &overline_margin,
24403 doc: /* *Space between overline and text, in pixels.
24404 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24405 margin to the caracter height. */);
24406 overline_margin = 2;
24410 /* Initialize this module when Emacs starts. */
24412 void
24413 init_xdisp ()
24415 Lisp_Object root_window;
24416 struct window *mini_w;
24418 current_header_line_height = current_mode_line_height = -1;
24420 CHARPOS (this_line_start_pos) = 0;
24422 mini_w = XWINDOW (minibuf_window);
24423 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24425 if (!noninteractive)
24427 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24428 int i;
24430 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24431 set_window_height (root_window,
24432 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24434 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24435 set_window_height (minibuf_window, 1, 0);
24437 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24438 mini_w->total_cols = make_number (FRAME_COLS (f));
24440 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24441 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24442 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24444 /* The default ellipsis glyphs `...'. */
24445 for (i = 0; i < 3; ++i)
24446 default_invis_vector[i] = make_number ('.');
24450 /* Allocate the buffer for frame titles.
24451 Also used for `format-mode-line'. */
24452 int size = 100;
24453 mode_line_noprop_buf = (char *) xmalloc (size);
24454 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24455 mode_line_noprop_ptr = mode_line_noprop_buf;
24456 mode_line_target = MODE_LINE_DISPLAY;
24459 help_echo_showing_p = 0;
24463 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24464 (do not change this comment) */