(icomplete-get-keys): Look up KEYS using all maps in proper buffer.
[emacs.git] / src / xdisp.c
blobfd72005fc7877ebe07afddd6d26e5b26622a34ef
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 it->current.pos = start_pos;
4306 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4308 it->method = GET_FROM_STRETCH;
4309 it->object = value;
4310 it->position = start_pos;
4311 if (BUFFERP (object))
4312 it->current.pos = start_pos;
4314 #ifdef HAVE_WINDOW_SYSTEM
4315 else
4317 it->what = IT_IMAGE;
4318 it->image_id = lookup_image (it->f, value);
4319 it->position = start_pos;
4320 it->object = NILP (object) ? it->w->buffer : object;
4321 it->method = GET_FROM_IMAGE;
4323 /* Say that we haven't consumed the characters with
4324 `display' property yet. The call to pop_it in
4325 set_iterator_to_next will clean this up. */
4326 if (BUFFERP (object))
4327 it->current.pos = start_pos;
4329 #endif /* HAVE_WINDOW_SYSTEM */
4331 return 1;
4334 /* Invalid property or property not supported. Restore
4335 POSITION to what it was before. */
4336 *position = start_pos;
4337 return 0;
4341 /* Check if SPEC is a display specification value whose text should be
4342 treated as intangible. */
4344 static int
4345 single_display_spec_intangible_p (prop)
4346 Lisp_Object prop;
4348 /* Skip over `when FORM'. */
4349 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4351 prop = XCDR (prop);
4352 if (!CONSP (prop))
4353 return 0;
4354 prop = XCDR (prop);
4357 if (STRINGP (prop))
4358 return 1;
4360 if (!CONSP (prop))
4361 return 0;
4363 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4364 we don't need to treat text as intangible. */
4365 if (EQ (XCAR (prop), Qmargin))
4367 prop = XCDR (prop);
4368 if (!CONSP (prop))
4369 return 0;
4371 prop = XCDR (prop);
4372 if (!CONSP (prop)
4373 || EQ (XCAR (prop), Qleft_margin)
4374 || EQ (XCAR (prop), Qright_margin))
4375 return 0;
4378 return (CONSP (prop)
4379 && (EQ (XCAR (prop), Qimage)
4380 || EQ (XCAR (prop), Qspace)));
4384 /* Check if PROP is a display property value whose text should be
4385 treated as intangible. */
4388 display_prop_intangible_p (prop)
4389 Lisp_Object prop;
4391 if (CONSP (prop)
4392 && CONSP (XCAR (prop))
4393 && !EQ (Qmargin, XCAR (XCAR (prop))))
4395 /* A list of sub-properties. */
4396 while (CONSP (prop))
4398 if (single_display_spec_intangible_p (XCAR (prop)))
4399 return 1;
4400 prop = XCDR (prop);
4403 else if (VECTORP (prop))
4405 /* A vector of sub-properties. */
4406 int i;
4407 for (i = 0; i < ASIZE (prop); ++i)
4408 if (single_display_spec_intangible_p (AREF (prop, i)))
4409 return 1;
4411 else
4412 return single_display_spec_intangible_p (prop);
4414 return 0;
4418 /* Return 1 if PROP is a display sub-property value containing STRING. */
4420 static int
4421 single_display_spec_string_p (prop, string)
4422 Lisp_Object prop, string;
4424 if (EQ (string, prop))
4425 return 1;
4427 /* Skip over `when FORM'. */
4428 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4430 prop = XCDR (prop);
4431 if (!CONSP (prop))
4432 return 0;
4433 prop = XCDR (prop);
4436 if (CONSP (prop))
4437 /* Skip over `margin LOCATION'. */
4438 if (EQ (XCAR (prop), Qmargin))
4440 prop = XCDR (prop);
4441 if (!CONSP (prop))
4442 return 0;
4444 prop = XCDR (prop);
4445 if (!CONSP (prop))
4446 return 0;
4449 return CONSP (prop) && EQ (XCAR (prop), string);
4453 /* Return 1 if STRING appears in the `display' property PROP. */
4455 static int
4456 display_prop_string_p (prop, string)
4457 Lisp_Object prop, string;
4459 if (CONSP (prop)
4460 && CONSP (XCAR (prop))
4461 && !EQ (Qmargin, XCAR (XCAR (prop))))
4463 /* A list of sub-properties. */
4464 while (CONSP (prop))
4466 if (single_display_spec_string_p (XCAR (prop), string))
4467 return 1;
4468 prop = XCDR (prop);
4471 else if (VECTORP (prop))
4473 /* A vector of sub-properties. */
4474 int i;
4475 for (i = 0; i < ASIZE (prop); ++i)
4476 if (single_display_spec_string_p (AREF (prop, i), string))
4477 return 1;
4479 else
4480 return single_display_spec_string_p (prop, string);
4482 return 0;
4486 /* Determine from which buffer position in W's buffer STRING comes
4487 from. AROUND_CHARPOS is an approximate position where it could
4488 be from. Value is the buffer position or 0 if it couldn't be
4489 determined.
4491 W's buffer must be current.
4493 This function is necessary because we don't record buffer positions
4494 in glyphs generated from strings (to keep struct glyph small).
4495 This function may only use code that doesn't eval because it is
4496 called asynchronously from note_mouse_highlight. */
4499 string_buffer_position (w, string, around_charpos)
4500 struct window *w;
4501 Lisp_Object string;
4502 int around_charpos;
4504 Lisp_Object limit, prop, pos;
4505 const int MAX_DISTANCE = 1000;
4506 int found = 0;
4508 pos = make_number (around_charpos);
4509 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4510 while (!found && !EQ (pos, limit))
4512 prop = Fget_char_property (pos, Qdisplay, Qnil);
4513 if (!NILP (prop) && display_prop_string_p (prop, string))
4514 found = 1;
4515 else
4516 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4519 if (!found)
4521 pos = make_number (around_charpos);
4522 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4523 while (!found && !EQ (pos, limit))
4525 prop = Fget_char_property (pos, Qdisplay, Qnil);
4526 if (!NILP (prop) && display_prop_string_p (prop, string))
4527 found = 1;
4528 else
4529 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4530 limit);
4534 return found ? XINT (pos) : 0;
4539 /***********************************************************************
4540 `composition' property
4541 ***********************************************************************/
4543 /* Set up iterator IT from `composition' property at its current
4544 position. Called from handle_stop. */
4546 static enum prop_handled
4547 handle_composition_prop (it)
4548 struct it *it;
4550 Lisp_Object prop, string;
4551 int pos, pos_byte, end;
4552 enum prop_handled handled = HANDLED_NORMALLY;
4554 if (STRINGP (it->string))
4556 pos = IT_STRING_CHARPOS (*it);
4557 pos_byte = IT_STRING_BYTEPOS (*it);
4558 string = it->string;
4560 else
4562 pos = IT_CHARPOS (*it);
4563 pos_byte = IT_BYTEPOS (*it);
4564 string = Qnil;
4567 /* If there's a valid composition and point is not inside of the
4568 composition (in the case that the composition is from the current
4569 buffer), draw a glyph composed from the composition components. */
4570 if (find_composition (pos, -1, &pos, &end, &prop, string)
4571 && COMPOSITION_VALID_P (pos, end, prop)
4572 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4574 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4576 if (id >= 0)
4578 struct composition *cmp = composition_table[id];
4580 if (cmp->glyph_len == 0)
4582 /* No glyph. */
4583 if (STRINGP (it->string))
4585 IT_STRING_CHARPOS (*it) = end;
4586 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4587 end);
4589 else
4591 IT_CHARPOS (*it) = end;
4592 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4594 return HANDLED_RECOMPUTE_PROPS;
4597 it->stop_charpos = end;
4598 push_it (it);
4600 it->method = GET_FROM_COMPOSITION;
4601 it->cmp_id = id;
4602 it->cmp_len = COMPOSITION_LENGTH (prop);
4603 /* For a terminal, draw only the first character of the
4604 components. */
4605 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4606 it->len = (STRINGP (it->string)
4607 ? string_char_to_byte (it->string, end)
4608 : CHAR_TO_BYTE (end)) - pos_byte;
4609 handled = HANDLED_RETURN;
4613 return handled;
4618 /***********************************************************************
4619 Overlay strings
4620 ***********************************************************************/
4622 /* The following structure is used to record overlay strings for
4623 later sorting in load_overlay_strings. */
4625 struct overlay_entry
4627 Lisp_Object overlay;
4628 Lisp_Object string;
4629 int priority;
4630 int after_string_p;
4634 /* Set up iterator IT from overlay strings at its current position.
4635 Called from handle_stop. */
4637 static enum prop_handled
4638 handle_overlay_change (it)
4639 struct it *it;
4641 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4642 return HANDLED_RECOMPUTE_PROPS;
4643 else
4644 return HANDLED_NORMALLY;
4648 /* Set up the next overlay string for delivery by IT, if there is an
4649 overlay string to deliver. Called by set_iterator_to_next when the
4650 end of the current overlay string is reached. If there are more
4651 overlay strings to display, IT->string and
4652 IT->current.overlay_string_index are set appropriately here.
4653 Otherwise IT->string is set to nil. */
4655 static void
4656 next_overlay_string (it)
4657 struct it *it;
4659 ++it->current.overlay_string_index;
4660 if (it->current.overlay_string_index == it->n_overlay_strings)
4662 /* No more overlay strings. Restore IT's settings to what
4663 they were before overlay strings were processed, and
4664 continue to deliver from current_buffer. */
4665 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4667 pop_it (it);
4668 xassert (it->sp > 0
4669 || it->method == GET_FROM_COMPOSITION
4670 || (NILP (it->string)
4671 && it->method == GET_FROM_BUFFER
4672 && it->stop_charpos >= BEGV
4673 && it->stop_charpos <= it->end_charpos));
4674 it->current.overlay_string_index = -1;
4675 it->n_overlay_strings = 0;
4677 /* If we're at the end of the buffer, record that we have
4678 processed the overlay strings there already, so that
4679 next_element_from_buffer doesn't try it again. */
4680 if (IT_CHARPOS (*it) >= it->end_charpos)
4681 it->overlay_strings_at_end_processed_p = 1;
4683 /* If we have to display `...' for invisible text, set
4684 the iterator up for that. */
4685 if (display_ellipsis_p)
4686 setup_for_ellipsis (it, 0);
4688 else
4690 /* There are more overlay strings to process. If
4691 IT->current.overlay_string_index has advanced to a position
4692 where we must load IT->overlay_strings with more strings, do
4693 it. */
4694 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4696 if (it->current.overlay_string_index && i == 0)
4697 load_overlay_strings (it, 0);
4699 /* Initialize IT to deliver display elements from the overlay
4700 string. */
4701 it->string = it->overlay_strings[i];
4702 it->multibyte_p = STRING_MULTIBYTE (it->string);
4703 SET_TEXT_POS (it->current.string_pos, 0, 0);
4704 it->method = GET_FROM_STRING;
4705 it->stop_charpos = 0;
4708 CHECK_IT (it);
4712 /* Compare two overlay_entry structures E1 and E2. Used as a
4713 comparison function for qsort in load_overlay_strings. Overlay
4714 strings for the same position are sorted so that
4716 1. All after-strings come in front of before-strings, except
4717 when they come from the same overlay.
4719 2. Within after-strings, strings are sorted so that overlay strings
4720 from overlays with higher priorities come first.
4722 2. Within before-strings, strings are sorted so that overlay
4723 strings from overlays with higher priorities come last.
4725 Value is analogous to strcmp. */
4728 static int
4729 compare_overlay_entries (e1, e2)
4730 void *e1, *e2;
4732 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4733 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4734 int result;
4736 if (entry1->after_string_p != entry2->after_string_p)
4738 /* Let after-strings appear in front of before-strings if
4739 they come from different overlays. */
4740 if (EQ (entry1->overlay, entry2->overlay))
4741 result = entry1->after_string_p ? 1 : -1;
4742 else
4743 result = entry1->after_string_p ? -1 : 1;
4745 else if (entry1->after_string_p)
4746 /* After-strings sorted in order of decreasing priority. */
4747 result = entry2->priority - entry1->priority;
4748 else
4749 /* Before-strings sorted in order of increasing priority. */
4750 result = entry1->priority - entry2->priority;
4752 return result;
4756 /* Load the vector IT->overlay_strings with overlay strings from IT's
4757 current buffer position, or from CHARPOS if that is > 0. Set
4758 IT->n_overlays to the total number of overlay strings found.
4760 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4761 a time. On entry into load_overlay_strings,
4762 IT->current.overlay_string_index gives the number of overlay
4763 strings that have already been loaded by previous calls to this
4764 function.
4766 IT->add_overlay_start contains an additional overlay start
4767 position to consider for taking overlay strings from, if non-zero.
4768 This position comes into play when the overlay has an `invisible'
4769 property, and both before and after-strings. When we've skipped to
4770 the end of the overlay, because of its `invisible' property, we
4771 nevertheless want its before-string to appear.
4772 IT->add_overlay_start will contain the overlay start position
4773 in this case.
4775 Overlay strings are sorted so that after-string strings come in
4776 front of before-string strings. Within before and after-strings,
4777 strings are sorted by overlay priority. See also function
4778 compare_overlay_entries. */
4780 static void
4781 load_overlay_strings (it, charpos)
4782 struct it *it;
4783 int charpos;
4785 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4786 Lisp_Object overlay, window, str, invisible;
4787 struct Lisp_Overlay *ov;
4788 int start, end;
4789 int size = 20;
4790 int n = 0, i, j, invis_p;
4791 struct overlay_entry *entries
4792 = (struct overlay_entry *) alloca (size * sizeof *entries);
4794 if (charpos <= 0)
4795 charpos = IT_CHARPOS (*it);
4797 /* Append the overlay string STRING of overlay OVERLAY to vector
4798 `entries' which has size `size' and currently contains `n'
4799 elements. AFTER_P non-zero means STRING is an after-string of
4800 OVERLAY. */
4801 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4802 do \
4804 Lisp_Object priority; \
4806 if (n == size) \
4808 int new_size = 2 * size; \
4809 struct overlay_entry *old = entries; \
4810 entries = \
4811 (struct overlay_entry *) alloca (new_size \
4812 * sizeof *entries); \
4813 bcopy (old, entries, size * sizeof *entries); \
4814 size = new_size; \
4817 entries[n].string = (STRING); \
4818 entries[n].overlay = (OVERLAY); \
4819 priority = Foverlay_get ((OVERLAY), Qpriority); \
4820 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4821 entries[n].after_string_p = (AFTER_P); \
4822 ++n; \
4824 while (0)
4826 /* Process overlay before the overlay center. */
4827 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4829 XSETMISC (overlay, ov);
4830 xassert (OVERLAYP (overlay));
4831 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4832 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4834 if (end < charpos)
4835 break;
4837 /* Skip this overlay if it doesn't start or end at IT's current
4838 position. */
4839 if (end != charpos && start != charpos)
4840 continue;
4842 /* Skip this overlay if it doesn't apply to IT->w. */
4843 window = Foverlay_get (overlay, Qwindow);
4844 if (WINDOWP (window) && XWINDOW (window) != it->w)
4845 continue;
4847 /* If the text ``under'' the overlay is invisible, both before-
4848 and after-strings from this overlay are visible; start and
4849 end position are indistinguishable. */
4850 invisible = Foverlay_get (overlay, Qinvisible);
4851 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4853 /* If overlay has a non-empty before-string, record it. */
4854 if ((start == charpos || (end == charpos && invis_p))
4855 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4856 && SCHARS (str))
4857 RECORD_OVERLAY_STRING (overlay, str, 0);
4859 /* If overlay has a non-empty after-string, record it. */
4860 if ((end == charpos || (start == charpos && invis_p))
4861 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4862 && SCHARS (str))
4863 RECORD_OVERLAY_STRING (overlay, str, 1);
4866 /* Process overlays after the overlay center. */
4867 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4869 XSETMISC (overlay, ov);
4870 xassert (OVERLAYP (overlay));
4871 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4872 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4874 if (start > charpos)
4875 break;
4877 /* Skip this overlay if it doesn't start or end at IT's current
4878 position. */
4879 if (end != charpos && start != charpos)
4880 continue;
4882 /* Skip this overlay if it doesn't apply to IT->w. */
4883 window = Foverlay_get (overlay, Qwindow);
4884 if (WINDOWP (window) && XWINDOW (window) != it->w)
4885 continue;
4887 /* If the text ``under'' the overlay is invisible, it has a zero
4888 dimension, and both before- and after-strings apply. */
4889 invisible = Foverlay_get (overlay, Qinvisible);
4890 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4892 /* If overlay has a non-empty before-string, record it. */
4893 if ((start == charpos || (end == charpos && invis_p))
4894 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4895 && SCHARS (str))
4896 RECORD_OVERLAY_STRING (overlay, str, 0);
4898 /* If overlay has a non-empty after-string, record it. */
4899 if ((end == charpos || (start == charpos && invis_p))
4900 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4901 && SCHARS (str))
4902 RECORD_OVERLAY_STRING (overlay, str, 1);
4905 #undef RECORD_OVERLAY_STRING
4907 /* Sort entries. */
4908 if (n > 1)
4909 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4911 /* Record the total number of strings to process. */
4912 it->n_overlay_strings = n;
4914 /* IT->current.overlay_string_index is the number of overlay strings
4915 that have already been consumed by IT. Copy some of the
4916 remaining overlay strings to IT->overlay_strings. */
4917 i = 0;
4918 j = it->current.overlay_string_index;
4919 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4921 it->overlay_strings[i] = entries[j].string;
4922 it->string_overlays[i++] = entries[j++].overlay;
4925 CHECK_IT (it);
4929 /* Get the first chunk of overlay strings at IT's current buffer
4930 position, or at CHARPOS if that is > 0. Value is non-zero if at
4931 least one overlay string was found. */
4933 static int
4934 get_overlay_strings_1 (it, charpos, compute_stop_p)
4935 struct it *it;
4936 int charpos;
4938 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4939 process. This fills IT->overlay_strings with strings, and sets
4940 IT->n_overlay_strings to the total number of strings to process.
4941 IT->pos.overlay_string_index has to be set temporarily to zero
4942 because load_overlay_strings needs this; it must be set to -1
4943 when no overlay strings are found because a zero value would
4944 indicate a position in the first overlay string. */
4945 it->current.overlay_string_index = 0;
4946 load_overlay_strings (it, charpos);
4948 /* If we found overlay strings, set up IT to deliver display
4949 elements from the first one. Otherwise set up IT to deliver
4950 from current_buffer. */
4951 if (it->n_overlay_strings)
4953 /* Make sure we know settings in current_buffer, so that we can
4954 restore meaningful values when we're done with the overlay
4955 strings. */
4956 if (compute_stop_p)
4957 compute_stop_pos (it);
4958 xassert (it->face_id >= 0);
4960 /* Save IT's settings. They are restored after all overlay
4961 strings have been processed. */
4962 xassert (!compute_stop_p || it->sp == 0);
4963 push_it (it);
4965 /* Set up IT to deliver display elements from the first overlay
4966 string. */
4967 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4968 it->string = it->overlay_strings[0];
4969 it->from_overlay = Qnil;
4970 it->stop_charpos = 0;
4971 xassert (STRINGP (it->string));
4972 it->end_charpos = SCHARS (it->string);
4973 it->multibyte_p = STRING_MULTIBYTE (it->string);
4974 it->method = GET_FROM_STRING;
4975 return 1;
4978 it->current.overlay_string_index = -1;
4979 return 0;
4982 static int
4983 get_overlay_strings (it, charpos)
4984 struct it *it;
4985 int charpos;
4987 it->string = Qnil;
4988 it->method = GET_FROM_BUFFER;
4990 (void) get_overlay_strings_1 (it, charpos, 1);
4992 CHECK_IT (it);
4994 /* Value is non-zero if we found at least one overlay string. */
4995 return STRINGP (it->string);
5000 /***********************************************************************
5001 Saving and restoring state
5002 ***********************************************************************/
5004 /* Save current settings of IT on IT->stack. Called, for example,
5005 before setting up IT for an overlay string, to be able to restore
5006 IT's settings to what they were after the overlay string has been
5007 processed. */
5009 static void
5010 push_it (it)
5011 struct it *it;
5013 struct iterator_stack_entry *p;
5015 xassert (it->sp < IT_STACK_SIZE);
5016 p = it->stack + it->sp;
5018 p->stop_charpos = it->stop_charpos;
5019 xassert (it->face_id >= 0);
5020 p->face_id = it->face_id;
5021 p->string = it->string;
5022 p->method = it->method;
5023 p->from_overlay = it->from_overlay;
5024 switch (p->method)
5026 case GET_FROM_IMAGE:
5027 p->u.image.object = it->object;
5028 p->u.image.image_id = it->image_id;
5029 p->u.image.slice = it->slice;
5030 break;
5031 case GET_FROM_COMPOSITION:
5032 p->u.comp.object = it->object;
5033 p->u.comp.c = it->c;
5034 p->u.comp.len = it->len;
5035 p->u.comp.cmp_id = it->cmp_id;
5036 p->u.comp.cmp_len = it->cmp_len;
5037 break;
5038 case GET_FROM_STRETCH:
5039 p->u.stretch.object = it->object;
5040 break;
5042 p->position = it->position;
5043 p->current = it->current;
5044 p->end_charpos = it->end_charpos;
5045 p->string_nchars = it->string_nchars;
5046 p->area = it->area;
5047 p->multibyte_p = it->multibyte_p;
5048 p->space_width = it->space_width;
5049 p->font_height = it->font_height;
5050 p->voffset = it->voffset;
5051 p->string_from_display_prop_p = it->string_from_display_prop_p;
5052 p->display_ellipsis_p = 0;
5053 ++it->sp;
5057 /* Restore IT's settings from IT->stack. Called, for example, when no
5058 more overlay strings must be processed, and we return to delivering
5059 display elements from a buffer, or when the end of a string from a
5060 `display' property is reached and we return to delivering display
5061 elements from an overlay string, or from a buffer. */
5063 static void
5064 pop_it (it)
5065 struct it *it;
5067 struct iterator_stack_entry *p;
5069 xassert (it->sp > 0);
5070 --it->sp;
5071 p = it->stack + it->sp;
5072 it->stop_charpos = p->stop_charpos;
5073 it->face_id = p->face_id;
5074 it->current = p->current;
5075 it->position = p->position;
5076 it->string = p->string;
5077 it->from_overlay = p->from_overlay;
5078 if (NILP (it->string))
5079 SET_TEXT_POS (it->current.string_pos, -1, -1);
5080 it->method = p->method;
5081 switch (it->method)
5083 case GET_FROM_IMAGE:
5084 it->image_id = p->u.image.image_id;
5085 it->object = p->u.image.object;
5086 it->slice = p->u.image.slice;
5087 break;
5088 case GET_FROM_COMPOSITION:
5089 it->object = p->u.comp.object;
5090 it->c = p->u.comp.c;
5091 it->len = p->u.comp.len;
5092 it->cmp_id = p->u.comp.cmp_id;
5093 it->cmp_len = p->u.comp.cmp_len;
5094 break;
5095 case GET_FROM_STRETCH:
5096 it->object = p->u.comp.object;
5097 break;
5098 case GET_FROM_BUFFER:
5099 it->object = it->w->buffer;
5100 break;
5101 case GET_FROM_STRING:
5102 it->object = it->string;
5103 break;
5105 it->end_charpos = p->end_charpos;
5106 it->string_nchars = p->string_nchars;
5107 it->area = p->area;
5108 it->multibyte_p = p->multibyte_p;
5109 it->space_width = p->space_width;
5110 it->font_height = p->font_height;
5111 it->voffset = p->voffset;
5112 it->string_from_display_prop_p = p->string_from_display_prop_p;
5117 /***********************************************************************
5118 Moving over lines
5119 ***********************************************************************/
5121 /* Set IT's current position to the previous line start. */
5123 static void
5124 back_to_previous_line_start (it)
5125 struct it *it;
5127 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5128 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5132 /* Move IT to the next line start.
5134 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5135 we skipped over part of the text (as opposed to moving the iterator
5136 continuously over the text). Otherwise, don't change the value
5137 of *SKIPPED_P.
5139 Newlines may come from buffer text, overlay strings, or strings
5140 displayed via the `display' property. That's the reason we can't
5141 simply use find_next_newline_no_quit.
5143 Note that this function may not skip over invisible text that is so
5144 because of text properties and immediately follows a newline. If
5145 it would, function reseat_at_next_visible_line_start, when called
5146 from set_iterator_to_next, would effectively make invisible
5147 characters following a newline part of the wrong glyph row, which
5148 leads to wrong cursor motion. */
5150 static int
5151 forward_to_next_line_start (it, skipped_p)
5152 struct it *it;
5153 int *skipped_p;
5155 int old_selective, newline_found_p, n;
5156 const int MAX_NEWLINE_DISTANCE = 500;
5158 /* If already on a newline, just consume it to avoid unintended
5159 skipping over invisible text below. */
5160 if (it->what == IT_CHARACTER
5161 && it->c == '\n'
5162 && CHARPOS (it->position) == IT_CHARPOS (*it))
5164 set_iterator_to_next (it, 0);
5165 it->c = 0;
5166 return 1;
5169 /* Don't handle selective display in the following. It's (a)
5170 unnecessary because it's done by the caller, and (b) leads to an
5171 infinite recursion because next_element_from_ellipsis indirectly
5172 calls this function. */
5173 old_selective = it->selective;
5174 it->selective = 0;
5176 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5177 from buffer text. */
5178 for (n = newline_found_p = 0;
5179 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5180 n += STRINGP (it->string) ? 0 : 1)
5182 if (!get_next_display_element (it))
5183 return 0;
5184 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5185 set_iterator_to_next (it, 0);
5188 /* If we didn't find a newline near enough, see if we can use a
5189 short-cut. */
5190 if (!newline_found_p)
5192 int start = IT_CHARPOS (*it);
5193 int limit = find_next_newline_no_quit (start, 1);
5194 Lisp_Object pos;
5196 xassert (!STRINGP (it->string));
5198 /* If there isn't any `display' property in sight, and no
5199 overlays, we can just use the position of the newline in
5200 buffer text. */
5201 if (it->stop_charpos >= limit
5202 || ((pos = Fnext_single_property_change (make_number (start),
5203 Qdisplay,
5204 Qnil, make_number (limit)),
5205 NILP (pos))
5206 && next_overlay_change (start) == ZV))
5208 IT_CHARPOS (*it) = limit;
5209 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5210 *skipped_p = newline_found_p = 1;
5212 else
5214 while (get_next_display_element (it)
5215 && !newline_found_p)
5217 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5218 set_iterator_to_next (it, 0);
5223 it->selective = old_selective;
5224 return newline_found_p;
5228 /* Set IT's current position to the previous visible line start. Skip
5229 invisible text that is so either due to text properties or due to
5230 selective display. Caution: this does not change IT->current_x and
5231 IT->hpos. */
5233 static void
5234 back_to_previous_visible_line_start (it)
5235 struct it *it;
5237 while (IT_CHARPOS (*it) > BEGV)
5239 back_to_previous_line_start (it);
5241 if (IT_CHARPOS (*it) <= BEGV)
5242 break;
5244 /* If selective > 0, then lines indented more than that values
5245 are invisible. */
5246 if (it->selective > 0
5247 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5248 (double) it->selective)) /* iftc */
5249 continue;
5251 /* Check the newline before point for invisibility. */
5253 Lisp_Object prop;
5254 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5255 Qinvisible, it->window);
5256 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5257 continue;
5260 if (IT_CHARPOS (*it) <= BEGV)
5261 break;
5264 struct it it2;
5265 int pos;
5266 int beg, end;
5267 Lisp_Object val, overlay;
5269 /* If newline is part of a composition, continue from start of composition */
5270 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5271 && beg < IT_CHARPOS (*it))
5272 goto replaced;
5274 /* If newline is replaced by a display property, find start of overlay
5275 or interval and continue search from that point. */
5276 it2 = *it;
5277 pos = --IT_CHARPOS (it2);
5278 --IT_BYTEPOS (it2);
5279 it2.sp = 0;
5280 if (handle_display_prop (&it2) == HANDLED_RETURN
5281 && !NILP (val = get_char_property_and_overlay
5282 (make_number (pos), Qdisplay, Qnil, &overlay))
5283 && (OVERLAYP (overlay)
5284 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5285 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5286 goto replaced;
5288 /* Newline is not replaced by anything -- so we are done. */
5289 break;
5291 replaced:
5292 if (beg < BEGV)
5293 beg = BEGV;
5294 IT_CHARPOS (*it) = beg;
5295 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5299 it->continuation_lines_width = 0;
5301 xassert (IT_CHARPOS (*it) >= BEGV);
5302 xassert (IT_CHARPOS (*it) == BEGV
5303 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5304 CHECK_IT (it);
5308 /* Reseat iterator IT at the previous visible line start. Skip
5309 invisible text that is so either due to text properties or due to
5310 selective display. At the end, update IT's overlay information,
5311 face information etc. */
5313 void
5314 reseat_at_previous_visible_line_start (it)
5315 struct it *it;
5317 back_to_previous_visible_line_start (it);
5318 reseat (it, it->current.pos, 1);
5319 CHECK_IT (it);
5323 /* Reseat iterator IT on the next visible line start in the current
5324 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5325 preceding the line start. Skip over invisible text that is so
5326 because of selective display. Compute faces, overlays etc at the
5327 new position. Note that this function does not skip over text that
5328 is invisible because of text properties. */
5330 static void
5331 reseat_at_next_visible_line_start (it, on_newline_p)
5332 struct it *it;
5333 int on_newline_p;
5335 int newline_found_p, skipped_p = 0;
5337 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5339 /* Skip over lines that are invisible because they are indented
5340 more than the value of IT->selective. */
5341 if (it->selective > 0)
5342 while (IT_CHARPOS (*it) < ZV
5343 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5344 (double) it->selective)) /* iftc */
5346 xassert (IT_BYTEPOS (*it) == BEGV
5347 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5348 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5351 /* Position on the newline if that's what's requested. */
5352 if (on_newline_p && newline_found_p)
5354 if (STRINGP (it->string))
5356 if (IT_STRING_CHARPOS (*it) > 0)
5358 --IT_STRING_CHARPOS (*it);
5359 --IT_STRING_BYTEPOS (*it);
5362 else if (IT_CHARPOS (*it) > BEGV)
5364 --IT_CHARPOS (*it);
5365 --IT_BYTEPOS (*it);
5366 reseat (it, it->current.pos, 0);
5369 else if (skipped_p)
5370 reseat (it, it->current.pos, 0);
5372 CHECK_IT (it);
5377 /***********************************************************************
5378 Changing an iterator's position
5379 ***********************************************************************/
5381 /* Change IT's current position to POS in current_buffer. If FORCE_P
5382 is non-zero, always check for text properties at the new position.
5383 Otherwise, text properties are only looked up if POS >=
5384 IT->check_charpos of a property. */
5386 static void
5387 reseat (it, pos, force_p)
5388 struct it *it;
5389 struct text_pos pos;
5390 int force_p;
5392 int original_pos = IT_CHARPOS (*it);
5394 reseat_1 (it, pos, 0);
5396 /* Determine where to check text properties. Avoid doing it
5397 where possible because text property lookup is very expensive. */
5398 if (force_p
5399 || CHARPOS (pos) > it->stop_charpos
5400 || CHARPOS (pos) < original_pos)
5401 handle_stop (it);
5403 CHECK_IT (it);
5407 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5408 IT->stop_pos to POS, also. */
5410 static void
5411 reseat_1 (it, pos, set_stop_p)
5412 struct it *it;
5413 struct text_pos pos;
5414 int set_stop_p;
5416 /* Don't call this function when scanning a C string. */
5417 xassert (it->s == NULL);
5419 /* POS must be a reasonable value. */
5420 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5422 it->current.pos = it->position = pos;
5423 it->end_charpos = ZV;
5424 it->dpvec = NULL;
5425 it->current.dpvec_index = -1;
5426 it->current.overlay_string_index = -1;
5427 IT_STRING_CHARPOS (*it) = -1;
5428 IT_STRING_BYTEPOS (*it) = -1;
5429 it->string = Qnil;
5430 it->method = GET_FROM_BUFFER;
5431 it->object = it->w->buffer;
5432 it->area = TEXT_AREA;
5433 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5434 it->sp = 0;
5435 it->string_from_display_prop_p = 0;
5436 it->face_before_selective_p = 0;
5438 if (set_stop_p)
5439 it->stop_charpos = CHARPOS (pos);
5443 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5444 If S is non-null, it is a C string to iterate over. Otherwise,
5445 STRING gives a Lisp string to iterate over.
5447 If PRECISION > 0, don't return more then PRECISION number of
5448 characters from the string.
5450 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5451 characters have been returned. FIELD_WIDTH < 0 means an infinite
5452 field width.
5454 MULTIBYTE = 0 means disable processing of multibyte characters,
5455 MULTIBYTE > 0 means enable it,
5456 MULTIBYTE < 0 means use IT->multibyte_p.
5458 IT must be initialized via a prior call to init_iterator before
5459 calling this function. */
5461 static void
5462 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5463 struct it *it;
5464 unsigned char *s;
5465 Lisp_Object string;
5466 int charpos;
5467 int precision, field_width, multibyte;
5469 /* No region in strings. */
5470 it->region_beg_charpos = it->region_end_charpos = -1;
5472 /* No text property checks performed by default, but see below. */
5473 it->stop_charpos = -1;
5475 /* Set iterator position and end position. */
5476 bzero (&it->current, sizeof it->current);
5477 it->current.overlay_string_index = -1;
5478 it->current.dpvec_index = -1;
5479 xassert (charpos >= 0);
5481 /* If STRING is specified, use its multibyteness, otherwise use the
5482 setting of MULTIBYTE, if specified. */
5483 if (multibyte >= 0)
5484 it->multibyte_p = multibyte > 0;
5486 if (s == NULL)
5488 xassert (STRINGP (string));
5489 it->string = string;
5490 it->s = NULL;
5491 it->end_charpos = it->string_nchars = SCHARS (string);
5492 it->method = GET_FROM_STRING;
5493 it->current.string_pos = string_pos (charpos, string);
5495 else
5497 it->s = s;
5498 it->string = Qnil;
5500 /* Note that we use IT->current.pos, not it->current.string_pos,
5501 for displaying C strings. */
5502 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5503 if (it->multibyte_p)
5505 it->current.pos = c_string_pos (charpos, s, 1);
5506 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5508 else
5510 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5511 it->end_charpos = it->string_nchars = strlen (s);
5514 it->method = GET_FROM_C_STRING;
5517 /* PRECISION > 0 means don't return more than PRECISION characters
5518 from the string. */
5519 if (precision > 0 && it->end_charpos - charpos > precision)
5520 it->end_charpos = it->string_nchars = charpos + precision;
5522 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5523 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5524 FIELD_WIDTH < 0 means infinite field width. This is useful for
5525 padding with `-' at the end of a mode line. */
5526 if (field_width < 0)
5527 field_width = INFINITY;
5528 if (field_width > it->end_charpos - charpos)
5529 it->end_charpos = charpos + field_width;
5531 /* Use the standard display table for displaying strings. */
5532 if (DISP_TABLE_P (Vstandard_display_table))
5533 it->dp = XCHAR_TABLE (Vstandard_display_table);
5535 it->stop_charpos = charpos;
5536 CHECK_IT (it);
5541 /***********************************************************************
5542 Iteration
5543 ***********************************************************************/
5545 /* Map enum it_method value to corresponding next_element_from_* function. */
5547 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5549 next_element_from_buffer,
5550 next_element_from_display_vector,
5551 next_element_from_composition,
5552 next_element_from_string,
5553 next_element_from_c_string,
5554 next_element_from_image,
5555 next_element_from_stretch
5559 /* Load IT's display element fields with information about the next
5560 display element from the current position of IT. Value is zero if
5561 end of buffer (or C string) is reached. */
5563 static struct frame *last_escape_glyph_frame = NULL;
5564 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5565 static int last_escape_glyph_merged_face_id = 0;
5568 get_next_display_element (it)
5569 struct it *it;
5571 /* Non-zero means that we found a display element. Zero means that
5572 we hit the end of what we iterate over. Performance note: the
5573 function pointer `method' used here turns out to be faster than
5574 using a sequence of if-statements. */
5575 int success_p;
5577 get_next:
5578 success_p = (*get_next_element[it->method]) (it);
5580 if (it->what == IT_CHARACTER)
5582 /* Map via display table or translate control characters.
5583 IT->c, IT->len etc. have been set to the next character by
5584 the function call above. If we have a display table, and it
5585 contains an entry for IT->c, translate it. Don't do this if
5586 IT->c itself comes from a display table, otherwise we could
5587 end up in an infinite recursion. (An alternative could be to
5588 count the recursion depth of this function and signal an
5589 error when a certain maximum depth is reached.) Is it worth
5590 it? */
5591 if (success_p && it->dpvec == NULL)
5593 Lisp_Object dv;
5595 if (it->dp
5596 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5597 VECTORP (dv)))
5599 struct Lisp_Vector *v = XVECTOR (dv);
5601 /* Return the first character from the display table
5602 entry, if not empty. If empty, don't display the
5603 current character. */
5604 if (v->size)
5606 it->dpvec_char_len = it->len;
5607 it->dpvec = v->contents;
5608 it->dpend = v->contents + v->size;
5609 it->current.dpvec_index = 0;
5610 it->dpvec_face_id = -1;
5611 it->saved_face_id = it->face_id;
5612 it->method = GET_FROM_DISPLAY_VECTOR;
5613 it->ellipsis_p = 0;
5615 else
5617 set_iterator_to_next (it, 0);
5619 goto get_next;
5622 /* Translate control characters into `\003' or `^C' form.
5623 Control characters coming from a display table entry are
5624 currently not translated because we use IT->dpvec to hold
5625 the translation. This could easily be changed but I
5626 don't believe that it is worth doing.
5628 If it->multibyte_p is nonzero, eight-bit characters and
5629 non-printable multibyte characters are also translated to
5630 octal form.
5632 If it->multibyte_p is zero, eight-bit characters that
5633 don't have corresponding multibyte char code are also
5634 translated to octal form. */
5635 else if ((it->c < ' '
5636 && (it->area != TEXT_AREA
5637 /* In mode line, treat \n like other crl chars. */
5638 || (it->c != '\t'
5639 && it->glyph_row && it->glyph_row->mode_line_p)
5640 || (it->c != '\n' && it->c != '\t')))
5641 || (it->multibyte_p
5642 ? ((it->c >= 127
5643 && it->len == 1)
5644 || !CHAR_PRINTABLE_P (it->c)
5645 || (!NILP (Vnobreak_char_display)
5646 && (it->c == 0x8a0 || it->c == 0x8ad
5647 || it->c == 0x920 || it->c == 0x92d
5648 || it->c == 0xe20 || it->c == 0xe2d
5649 || it->c == 0xf20 || it->c == 0xf2d)))
5650 : (it->c >= 127
5651 && (!unibyte_display_via_language_environment
5652 || it->c == unibyte_char_to_multibyte (it->c)))))
5654 /* IT->c is a control character which must be displayed
5655 either as '\003' or as `^C' where the '\\' and '^'
5656 can be defined in the display table. Fill
5657 IT->ctl_chars with glyphs for what we have to
5658 display. Then, set IT->dpvec to these glyphs. */
5659 GLYPH g;
5660 int ctl_len;
5661 int face_id, lface_id = 0 ;
5662 GLYPH escape_glyph;
5664 /* Handle control characters with ^. */
5666 if (it->c < 128 && it->ctl_arrow_p)
5668 g = '^'; /* default glyph for Control */
5669 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5670 if (it->dp
5671 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5672 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5674 g = XINT (DISP_CTRL_GLYPH (it->dp));
5675 lface_id = FAST_GLYPH_FACE (g);
5677 if (lface_id)
5679 g = FAST_GLYPH_CHAR (g);
5680 face_id = merge_faces (it->f, Qt, lface_id,
5681 it->face_id);
5683 else if (it->f == last_escape_glyph_frame
5684 && it->face_id == last_escape_glyph_face_id)
5686 face_id = last_escape_glyph_merged_face_id;
5688 else
5690 /* Merge the escape-glyph face into the current face. */
5691 face_id = merge_faces (it->f, Qescape_glyph, 0,
5692 it->face_id);
5693 last_escape_glyph_frame = it->f;
5694 last_escape_glyph_face_id = it->face_id;
5695 last_escape_glyph_merged_face_id = face_id;
5698 XSETINT (it->ctl_chars[0], g);
5699 g = it->c ^ 0100;
5700 XSETINT (it->ctl_chars[1], g);
5701 ctl_len = 2;
5702 goto display_control;
5705 /* Handle non-break space in the mode where it only gets
5706 highlighting. */
5708 if (EQ (Vnobreak_char_display, Qt)
5709 && (it->c == 0x8a0 || it->c == 0x920
5710 || it->c == 0xe20 || it->c == 0xf20))
5712 /* Merge the no-break-space face into the current face. */
5713 face_id = merge_faces (it->f, Qnobreak_space, 0,
5714 it->face_id);
5716 g = it->c = ' ';
5717 XSETINT (it->ctl_chars[0], g);
5718 ctl_len = 1;
5719 goto display_control;
5722 /* Handle sequences that start with the "escape glyph". */
5724 /* the default escape glyph is \. */
5725 escape_glyph = '\\';
5727 if (it->dp
5728 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5729 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5731 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5732 lface_id = FAST_GLYPH_FACE (escape_glyph);
5734 if (lface_id)
5736 /* The display table specified a face.
5737 Merge it into face_id and also into escape_glyph. */
5738 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5739 face_id = merge_faces (it->f, Qt, lface_id,
5740 it->face_id);
5742 else if (it->f == last_escape_glyph_frame
5743 && it->face_id == last_escape_glyph_face_id)
5745 face_id = last_escape_glyph_merged_face_id;
5747 else
5749 /* Merge the escape-glyph face into the current face. */
5750 face_id = merge_faces (it->f, Qescape_glyph, 0,
5751 it->face_id);
5752 last_escape_glyph_frame = it->f;
5753 last_escape_glyph_face_id = it->face_id;
5754 last_escape_glyph_merged_face_id = face_id;
5757 /* Handle soft hyphens in the mode where they only get
5758 highlighting. */
5760 if (EQ (Vnobreak_char_display, Qt)
5761 && (it->c == 0x8ad || it->c == 0x92d
5762 || it->c == 0xe2d || it->c == 0xf2d))
5764 g = it->c = '-';
5765 XSETINT (it->ctl_chars[0], g);
5766 ctl_len = 1;
5767 goto display_control;
5770 /* Handle non-break space and soft hyphen
5771 with the escape glyph. */
5773 if (it->c == 0x8a0 || it->c == 0x8ad
5774 || it->c == 0x920 || it->c == 0x92d
5775 || it->c == 0xe20 || it->c == 0xe2d
5776 || it->c == 0xf20 || it->c == 0xf2d)
5778 XSETINT (it->ctl_chars[0], escape_glyph);
5779 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5780 XSETINT (it->ctl_chars[1], g);
5781 ctl_len = 2;
5782 goto display_control;
5786 unsigned char str[MAX_MULTIBYTE_LENGTH];
5787 int len;
5788 int i;
5790 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5791 if (SINGLE_BYTE_CHAR_P (it->c))
5792 str[0] = it->c, len = 1;
5793 else
5795 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5796 if (len < 0)
5798 /* It's an invalid character, which shouldn't
5799 happen actually, but due to bugs it may
5800 happen. Let's print the char as is, there's
5801 not much meaningful we can do with it. */
5802 str[0] = it->c;
5803 str[1] = it->c >> 8;
5804 str[2] = it->c >> 16;
5805 str[3] = it->c >> 24;
5806 len = 4;
5810 for (i = 0; i < len; i++)
5812 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5813 /* Insert three more glyphs into IT->ctl_chars for
5814 the octal display of the character. */
5815 g = ((str[i] >> 6) & 7) + '0';
5816 XSETINT (it->ctl_chars[i * 4 + 1], g);
5817 g = ((str[i] >> 3) & 7) + '0';
5818 XSETINT (it->ctl_chars[i * 4 + 2], g);
5819 g = (str[i] & 7) + '0';
5820 XSETINT (it->ctl_chars[i * 4 + 3], g);
5822 ctl_len = len * 4;
5825 display_control:
5826 /* Set up IT->dpvec and return first character from it. */
5827 it->dpvec_char_len = it->len;
5828 it->dpvec = it->ctl_chars;
5829 it->dpend = it->dpvec + ctl_len;
5830 it->current.dpvec_index = 0;
5831 it->dpvec_face_id = face_id;
5832 it->saved_face_id = it->face_id;
5833 it->method = GET_FROM_DISPLAY_VECTOR;
5834 it->ellipsis_p = 0;
5835 goto get_next;
5839 /* Adjust face id for a multibyte character. There are no
5840 multibyte character in unibyte text. */
5841 if (it->multibyte_p
5842 && success_p
5843 && FRAME_WINDOW_P (it->f))
5845 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5846 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5850 /* Is this character the last one of a run of characters with
5851 box? If yes, set IT->end_of_box_run_p to 1. */
5852 if (it->face_box_p
5853 && it->s == NULL)
5855 int face_id;
5856 struct face *face;
5858 it->end_of_box_run_p
5859 = ((face_id = face_after_it_pos (it),
5860 face_id != it->face_id)
5861 && (face = FACE_FROM_ID (it->f, face_id),
5862 face->box == FACE_NO_BOX));
5865 /* Value is 0 if end of buffer or string reached. */
5866 return success_p;
5870 /* Move IT to the next display element.
5872 RESEAT_P non-zero means if called on a newline in buffer text,
5873 skip to the next visible line start.
5875 Functions get_next_display_element and set_iterator_to_next are
5876 separate because I find this arrangement easier to handle than a
5877 get_next_display_element function that also increments IT's
5878 position. The way it is we can first look at an iterator's current
5879 display element, decide whether it fits on a line, and if it does,
5880 increment the iterator position. The other way around we probably
5881 would either need a flag indicating whether the iterator has to be
5882 incremented the next time, or we would have to implement a
5883 decrement position function which would not be easy to write. */
5885 void
5886 set_iterator_to_next (it, reseat_p)
5887 struct it *it;
5888 int reseat_p;
5890 /* Reset flags indicating start and end of a sequence of characters
5891 with box. Reset them at the start of this function because
5892 moving the iterator to a new position might set them. */
5893 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5895 switch (it->method)
5897 case GET_FROM_BUFFER:
5898 /* The current display element of IT is a character from
5899 current_buffer. Advance in the buffer, and maybe skip over
5900 invisible lines that are so because of selective display. */
5901 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5902 reseat_at_next_visible_line_start (it, 0);
5903 else
5905 xassert (it->len != 0);
5906 IT_BYTEPOS (*it) += it->len;
5907 IT_CHARPOS (*it) += 1;
5908 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5910 break;
5912 case GET_FROM_COMPOSITION:
5913 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5914 xassert (it->sp > 0);
5915 pop_it (it);
5916 if (it->method == GET_FROM_STRING)
5918 IT_STRING_BYTEPOS (*it) += it->len;
5919 IT_STRING_CHARPOS (*it) += it->cmp_len;
5920 goto consider_string_end;
5922 else if (it->method == GET_FROM_BUFFER)
5924 IT_BYTEPOS (*it) += it->len;
5925 IT_CHARPOS (*it) += it->cmp_len;
5927 break;
5929 case GET_FROM_C_STRING:
5930 /* Current display element of IT is from a C string. */
5931 IT_BYTEPOS (*it) += it->len;
5932 IT_CHARPOS (*it) += 1;
5933 break;
5935 case GET_FROM_DISPLAY_VECTOR:
5936 /* Current display element of IT is from a display table entry.
5937 Advance in the display table definition. Reset it to null if
5938 end reached, and continue with characters from buffers/
5939 strings. */
5940 ++it->current.dpvec_index;
5942 /* Restore face of the iterator to what they were before the
5943 display vector entry (these entries may contain faces). */
5944 it->face_id = it->saved_face_id;
5946 if (it->dpvec + it->current.dpvec_index == it->dpend)
5948 int recheck_faces = it->ellipsis_p;
5950 if (it->s)
5951 it->method = GET_FROM_C_STRING;
5952 else if (STRINGP (it->string))
5953 it->method = GET_FROM_STRING;
5954 else
5956 it->method = GET_FROM_BUFFER;
5957 it->object = it->w->buffer;
5960 it->dpvec = NULL;
5961 it->current.dpvec_index = -1;
5963 /* Skip over characters which were displayed via IT->dpvec. */
5964 if (it->dpvec_char_len < 0)
5965 reseat_at_next_visible_line_start (it, 1);
5966 else if (it->dpvec_char_len > 0)
5968 if (it->method == GET_FROM_STRING
5969 && it->n_overlay_strings > 0)
5970 it->ignore_overlay_strings_at_pos_p = 1;
5971 it->len = it->dpvec_char_len;
5972 set_iterator_to_next (it, reseat_p);
5975 /* Maybe recheck faces after display vector */
5976 if (recheck_faces)
5977 it->stop_charpos = IT_CHARPOS (*it);
5979 break;
5981 case GET_FROM_STRING:
5982 /* Current display element is a character from a Lisp string. */
5983 xassert (it->s == NULL && STRINGP (it->string));
5984 IT_STRING_BYTEPOS (*it) += it->len;
5985 IT_STRING_CHARPOS (*it) += 1;
5987 consider_string_end:
5989 if (it->current.overlay_string_index >= 0)
5991 /* IT->string is an overlay string. Advance to the
5992 next, if there is one. */
5993 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5994 next_overlay_string (it);
5996 else
5998 /* IT->string is not an overlay string. If we reached
5999 its end, and there is something on IT->stack, proceed
6000 with what is on the stack. This can be either another
6001 string, this time an overlay string, or a buffer. */
6002 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6003 && it->sp > 0)
6005 pop_it (it);
6006 if (it->method == GET_FROM_STRING)
6007 goto consider_string_end;
6010 break;
6012 case GET_FROM_IMAGE:
6013 case GET_FROM_STRETCH:
6014 /* The position etc with which we have to proceed are on
6015 the stack. The position may be at the end of a string,
6016 if the `display' property takes up the whole string. */
6017 xassert (it->sp > 0);
6018 pop_it (it);
6019 if (it->method == GET_FROM_STRING)
6020 goto consider_string_end;
6021 break;
6023 default:
6024 /* There are no other methods defined, so this should be a bug. */
6025 abort ();
6028 xassert (it->method != GET_FROM_STRING
6029 || (STRINGP (it->string)
6030 && IT_STRING_CHARPOS (*it) >= 0));
6033 /* Load IT's display element fields with information about the next
6034 display element which comes from a display table entry or from the
6035 result of translating a control character to one of the forms `^C'
6036 or `\003'.
6038 IT->dpvec holds the glyphs to return as characters.
6039 IT->saved_face_id holds the face id before the display vector--
6040 it is restored into IT->face_idin set_iterator_to_next. */
6042 static int
6043 next_element_from_display_vector (it)
6044 struct it *it;
6046 /* Precondition. */
6047 xassert (it->dpvec && it->current.dpvec_index >= 0);
6049 it->face_id = it->saved_face_id;
6051 if (INTEGERP (*it->dpvec)
6052 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6054 GLYPH g;
6056 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6057 it->c = FAST_GLYPH_CHAR (g);
6058 it->len = CHAR_BYTES (it->c);
6060 /* The entry may contain a face id to use. Such a face id is
6061 the id of a Lisp face, not a realized face. A face id of
6062 zero means no face is specified. */
6063 if (it->dpvec_face_id >= 0)
6064 it->face_id = it->dpvec_face_id;
6065 else
6067 int lface_id = FAST_GLYPH_FACE (g);
6068 if (lface_id > 0)
6069 it->face_id = merge_faces (it->f, Qt, lface_id,
6070 it->saved_face_id);
6073 else
6074 /* Display table entry is invalid. Return a space. */
6075 it->c = ' ', it->len = 1;
6077 /* Don't change position and object of the iterator here. They are
6078 still the values of the character that had this display table
6079 entry or was translated, and that's what we want. */
6080 it->what = IT_CHARACTER;
6081 return 1;
6085 /* Load IT with the next display element from Lisp string IT->string.
6086 IT->current.string_pos is the current position within the string.
6087 If IT->current.overlay_string_index >= 0, the Lisp string is an
6088 overlay string. */
6090 static int
6091 next_element_from_string (it)
6092 struct it *it;
6094 struct text_pos position;
6096 xassert (STRINGP (it->string));
6097 xassert (IT_STRING_CHARPOS (*it) >= 0);
6098 position = it->current.string_pos;
6100 /* Time to check for invisible text? */
6101 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6102 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6104 handle_stop (it);
6106 /* Since a handler may have changed IT->method, we must
6107 recurse here. */
6108 return get_next_display_element (it);
6111 if (it->current.overlay_string_index >= 0)
6113 /* Get the next character from an overlay string. In overlay
6114 strings, There is no field width or padding with spaces to
6115 do. */
6116 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6118 it->what = IT_EOB;
6119 return 0;
6121 else if (STRING_MULTIBYTE (it->string))
6123 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6124 const unsigned char *s = (SDATA (it->string)
6125 + IT_STRING_BYTEPOS (*it));
6126 it->c = string_char_and_length (s, remaining, &it->len);
6128 else
6130 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6131 it->len = 1;
6134 else
6136 /* Get the next character from a Lisp string that is not an
6137 overlay string. Such strings come from the mode line, for
6138 example. We may have to pad with spaces, or truncate the
6139 string. See also next_element_from_c_string. */
6140 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6142 it->what = IT_EOB;
6143 return 0;
6145 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6147 /* Pad with spaces. */
6148 it->c = ' ', it->len = 1;
6149 CHARPOS (position) = BYTEPOS (position) = -1;
6151 else if (STRING_MULTIBYTE (it->string))
6153 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6154 const unsigned char *s = (SDATA (it->string)
6155 + IT_STRING_BYTEPOS (*it));
6156 it->c = string_char_and_length (s, maxlen, &it->len);
6158 else
6160 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6161 it->len = 1;
6165 /* Record what we have and where it came from. */
6166 it->what = IT_CHARACTER;
6167 it->object = it->string;
6168 it->position = position;
6169 return 1;
6173 /* Load IT with next display element from C string IT->s.
6174 IT->string_nchars is the maximum number of characters to return
6175 from the string. IT->end_charpos may be greater than
6176 IT->string_nchars when this function is called, in which case we
6177 may have to return padding spaces. Value is zero if end of string
6178 reached, including padding spaces. */
6180 static int
6181 next_element_from_c_string (it)
6182 struct it *it;
6184 int success_p = 1;
6186 xassert (it->s);
6187 it->what = IT_CHARACTER;
6188 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6189 it->object = Qnil;
6191 /* IT's position can be greater IT->string_nchars in case a field
6192 width or precision has been specified when the iterator was
6193 initialized. */
6194 if (IT_CHARPOS (*it) >= it->end_charpos)
6196 /* End of the game. */
6197 it->what = IT_EOB;
6198 success_p = 0;
6200 else if (IT_CHARPOS (*it) >= it->string_nchars)
6202 /* Pad with spaces. */
6203 it->c = ' ', it->len = 1;
6204 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6206 else if (it->multibyte_p)
6208 /* Implementation note: The calls to strlen apparently aren't a
6209 performance problem because there is no noticeable performance
6210 difference between Emacs running in unibyte or multibyte mode. */
6211 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6212 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6213 maxlen, &it->len);
6215 else
6216 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6218 return success_p;
6222 /* Set up IT to return characters from an ellipsis, if appropriate.
6223 The definition of the ellipsis glyphs may come from a display table
6224 entry. This function Fills IT with the first glyph from the
6225 ellipsis if an ellipsis is to be displayed. */
6227 static int
6228 next_element_from_ellipsis (it)
6229 struct it *it;
6231 if (it->selective_display_ellipsis_p)
6232 setup_for_ellipsis (it, it->len);
6233 else
6235 /* The face at the current position may be different from the
6236 face we find after the invisible text. Remember what it
6237 was in IT->saved_face_id, and signal that it's there by
6238 setting face_before_selective_p. */
6239 it->saved_face_id = it->face_id;
6240 it->method = GET_FROM_BUFFER;
6241 it->object = it->w->buffer;
6242 reseat_at_next_visible_line_start (it, 1);
6243 it->face_before_selective_p = 1;
6246 return get_next_display_element (it);
6250 /* Deliver an image display element. The iterator IT is already
6251 filled with image information (done in handle_display_prop). Value
6252 is always 1. */
6255 static int
6256 next_element_from_image (it)
6257 struct it *it;
6259 it->what = IT_IMAGE;
6260 return 1;
6264 /* Fill iterator IT with next display element from a stretch glyph
6265 property. IT->object is the value of the text property. Value is
6266 always 1. */
6268 static int
6269 next_element_from_stretch (it)
6270 struct it *it;
6272 it->what = IT_STRETCH;
6273 return 1;
6277 /* Load IT with the next display element from current_buffer. Value
6278 is zero if end of buffer reached. IT->stop_charpos is the next
6279 position at which to stop and check for text properties or buffer
6280 end. */
6282 static int
6283 next_element_from_buffer (it)
6284 struct it *it;
6286 int success_p = 1;
6288 /* Check this assumption, otherwise, we would never enter the
6289 if-statement, below. */
6290 xassert (IT_CHARPOS (*it) >= BEGV
6291 && IT_CHARPOS (*it) <= it->stop_charpos);
6293 if (IT_CHARPOS (*it) >= it->stop_charpos)
6295 if (IT_CHARPOS (*it) >= it->end_charpos)
6297 int overlay_strings_follow_p;
6299 /* End of the game, except when overlay strings follow that
6300 haven't been returned yet. */
6301 if (it->overlay_strings_at_end_processed_p)
6302 overlay_strings_follow_p = 0;
6303 else
6305 it->overlay_strings_at_end_processed_p = 1;
6306 overlay_strings_follow_p = get_overlay_strings (it, 0);
6309 if (overlay_strings_follow_p)
6310 success_p = get_next_display_element (it);
6311 else
6313 it->what = IT_EOB;
6314 it->position = it->current.pos;
6315 success_p = 0;
6318 else
6320 handle_stop (it);
6321 return get_next_display_element (it);
6324 else
6326 /* No face changes, overlays etc. in sight, so just return a
6327 character from current_buffer. */
6328 unsigned char *p;
6330 /* Maybe run the redisplay end trigger hook. Performance note:
6331 This doesn't seem to cost measurable time. */
6332 if (it->redisplay_end_trigger_charpos
6333 && it->glyph_row
6334 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6335 run_redisplay_end_trigger_hook (it);
6337 /* Get the next character, maybe multibyte. */
6338 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6339 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6341 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6342 - IT_BYTEPOS (*it));
6343 it->c = string_char_and_length (p, maxlen, &it->len);
6345 else
6346 it->c = *p, it->len = 1;
6348 /* Record what we have and where it came from. */
6349 it->what = IT_CHARACTER;
6350 it->object = it->w->buffer;
6351 it->position = it->current.pos;
6353 /* Normally we return the character found above, except when we
6354 really want to return an ellipsis for selective display. */
6355 if (it->selective)
6357 if (it->c == '\n')
6359 /* A value of selective > 0 means hide lines indented more
6360 than that number of columns. */
6361 if (it->selective > 0
6362 && IT_CHARPOS (*it) + 1 < ZV
6363 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6364 IT_BYTEPOS (*it) + 1,
6365 (double) it->selective)) /* iftc */
6367 success_p = next_element_from_ellipsis (it);
6368 it->dpvec_char_len = -1;
6371 else if (it->c == '\r' && it->selective == -1)
6373 /* A value of selective == -1 means that everything from the
6374 CR to the end of the line is invisible, with maybe an
6375 ellipsis displayed for it. */
6376 success_p = next_element_from_ellipsis (it);
6377 it->dpvec_char_len = -1;
6382 /* Value is zero if end of buffer reached. */
6383 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6384 return success_p;
6388 /* Run the redisplay end trigger hook for IT. */
6390 static void
6391 run_redisplay_end_trigger_hook (it)
6392 struct it *it;
6394 Lisp_Object args[3];
6396 /* IT->glyph_row should be non-null, i.e. we should be actually
6397 displaying something, or otherwise we should not run the hook. */
6398 xassert (it->glyph_row);
6400 /* Set up hook arguments. */
6401 args[0] = Qredisplay_end_trigger_functions;
6402 args[1] = it->window;
6403 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6404 it->redisplay_end_trigger_charpos = 0;
6406 /* Since we are *trying* to run these functions, don't try to run
6407 them again, even if they get an error. */
6408 it->w->redisplay_end_trigger = Qnil;
6409 Frun_hook_with_args (3, args);
6411 /* Notice if it changed the face of the character we are on. */
6412 handle_face_prop (it);
6416 /* Deliver a composition display element. The iterator IT is already
6417 filled with composition information (done in
6418 handle_composition_prop). Value is always 1. */
6420 static int
6421 next_element_from_composition (it)
6422 struct it *it;
6424 it->what = IT_COMPOSITION;
6425 it->position = (STRINGP (it->string)
6426 ? it->current.string_pos
6427 : it->current.pos);
6428 if (STRINGP (it->string))
6429 it->object = it->string;
6430 else
6431 it->object = it->w->buffer;
6432 return 1;
6437 /***********************************************************************
6438 Moving an iterator without producing glyphs
6439 ***********************************************************************/
6441 /* Check if iterator is at a position corresponding to a valid buffer
6442 position after some move_it_ call. */
6444 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6445 ((it)->method == GET_FROM_STRING \
6446 ? IT_STRING_CHARPOS (*it) == 0 \
6447 : 1)
6450 /* Move iterator IT to a specified buffer or X position within one
6451 line on the display without producing glyphs.
6453 OP should be a bit mask including some or all of these bits:
6454 MOVE_TO_X: Stop on reaching x-position TO_X.
6455 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6456 Regardless of OP's value, stop in reaching the end of the display line.
6458 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6459 This means, in particular, that TO_X includes window's horizontal
6460 scroll amount.
6462 The return value has several possible values that
6463 say what condition caused the scan to stop:
6465 MOVE_POS_MATCH_OR_ZV
6466 - when TO_POS or ZV was reached.
6468 MOVE_X_REACHED
6469 -when TO_X was reached before TO_POS or ZV were reached.
6471 MOVE_LINE_CONTINUED
6472 - when we reached the end of the display area and the line must
6473 be continued.
6475 MOVE_LINE_TRUNCATED
6476 - when we reached the end of the display area and the line is
6477 truncated.
6479 MOVE_NEWLINE_OR_CR
6480 - when we stopped at a line end, i.e. a newline or a CR and selective
6481 display is on. */
6483 static enum move_it_result
6484 move_it_in_display_line_to (it, to_charpos, to_x, op)
6485 struct it *it;
6486 int to_charpos, to_x, op;
6488 enum move_it_result result = MOVE_UNDEFINED;
6489 struct glyph_row *saved_glyph_row;
6491 /* Don't produce glyphs in produce_glyphs. */
6492 saved_glyph_row = it->glyph_row;
6493 it->glyph_row = NULL;
6495 #define BUFFER_POS_REACHED_P() \
6496 ((op & MOVE_TO_POS) != 0 \
6497 && BUFFERP (it->object) \
6498 && IT_CHARPOS (*it) >= to_charpos \
6499 && (it->method == GET_FROM_BUFFER \
6500 || (it->method == GET_FROM_DISPLAY_VECTOR \
6501 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6504 while (1)
6506 int x, i, ascent = 0, descent = 0;
6508 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6509 if ((op & MOVE_TO_POS) != 0
6510 && BUFFERP (it->object)
6511 && it->method == GET_FROM_BUFFER
6512 && IT_CHARPOS (*it) > to_charpos)
6514 result = MOVE_POS_MATCH_OR_ZV;
6515 break;
6518 /* Stop when ZV reached.
6519 We used to stop here when TO_CHARPOS reached as well, but that is
6520 too soon if this glyph does not fit on this line. So we handle it
6521 explicitly below. */
6522 if (!get_next_display_element (it)
6523 || (it->truncate_lines_p
6524 && BUFFER_POS_REACHED_P ()))
6526 result = MOVE_POS_MATCH_OR_ZV;
6527 break;
6530 /* The call to produce_glyphs will get the metrics of the
6531 display element IT is loaded with. We record in x the
6532 x-position before this display element in case it does not
6533 fit on the line. */
6534 x = it->current_x;
6536 /* Remember the line height so far in case the next element doesn't
6537 fit on the line. */
6538 if (!it->truncate_lines_p)
6540 ascent = it->max_ascent;
6541 descent = it->max_descent;
6544 PRODUCE_GLYPHS (it);
6546 if (it->area != TEXT_AREA)
6548 set_iterator_to_next (it, 1);
6549 continue;
6552 /* The number of glyphs we get back in IT->nglyphs will normally
6553 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6554 character on a terminal frame, or (iii) a line end. For the
6555 second case, IT->nglyphs - 1 padding glyphs will be present
6556 (on X frames, there is only one glyph produced for a
6557 composite character.
6559 The behavior implemented below means, for continuation lines,
6560 that as many spaces of a TAB as fit on the current line are
6561 displayed there. For terminal frames, as many glyphs of a
6562 multi-glyph character are displayed in the current line, too.
6563 This is what the old redisplay code did, and we keep it that
6564 way. Under X, the whole shape of a complex character must
6565 fit on the line or it will be completely displayed in the
6566 next line.
6568 Note that both for tabs and padding glyphs, all glyphs have
6569 the same width. */
6570 if (it->nglyphs)
6572 /* More than one glyph or glyph doesn't fit on line. All
6573 glyphs have the same width. */
6574 int single_glyph_width = it->pixel_width / it->nglyphs;
6575 int new_x;
6576 int x_before_this_char = x;
6577 int hpos_before_this_char = it->hpos;
6579 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6581 new_x = x + single_glyph_width;
6583 /* We want to leave anything reaching TO_X to the caller. */
6584 if ((op & MOVE_TO_X) && new_x > to_x)
6586 if (BUFFER_POS_REACHED_P ())
6587 goto buffer_pos_reached;
6588 it->current_x = x;
6589 result = MOVE_X_REACHED;
6590 break;
6592 else if (/* Lines are continued. */
6593 !it->truncate_lines_p
6594 && (/* And glyph doesn't fit on the line. */
6595 new_x > it->last_visible_x
6596 /* Or it fits exactly and we're on a window
6597 system frame. */
6598 || (new_x == it->last_visible_x
6599 && FRAME_WINDOW_P (it->f))))
6601 if (/* IT->hpos == 0 means the very first glyph
6602 doesn't fit on the line, e.g. a wide image. */
6603 it->hpos == 0
6604 || (new_x == it->last_visible_x
6605 && FRAME_WINDOW_P (it->f)))
6607 ++it->hpos;
6608 it->current_x = new_x;
6610 /* The character's last glyph just barely fits
6611 in this row. */
6612 if (i == it->nglyphs - 1)
6614 /* If this is the destination position,
6615 return a position *before* it in this row,
6616 now that we know it fits in this row. */
6617 if (BUFFER_POS_REACHED_P ())
6619 it->hpos = hpos_before_this_char;
6620 it->current_x = x_before_this_char;
6621 result = MOVE_POS_MATCH_OR_ZV;
6622 break;
6625 set_iterator_to_next (it, 1);
6626 #ifdef HAVE_WINDOW_SYSTEM
6627 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6629 if (!get_next_display_element (it))
6631 result = MOVE_POS_MATCH_OR_ZV;
6632 break;
6634 if (BUFFER_POS_REACHED_P ())
6636 if (ITERATOR_AT_END_OF_LINE_P (it))
6637 result = MOVE_POS_MATCH_OR_ZV;
6638 else
6639 result = MOVE_LINE_CONTINUED;
6640 break;
6642 if (ITERATOR_AT_END_OF_LINE_P (it))
6644 result = MOVE_NEWLINE_OR_CR;
6645 break;
6648 #endif /* HAVE_WINDOW_SYSTEM */
6651 else
6653 it->current_x = x;
6654 it->max_ascent = ascent;
6655 it->max_descent = descent;
6658 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6659 IT_CHARPOS (*it)));
6660 result = MOVE_LINE_CONTINUED;
6661 break;
6663 else if (BUFFER_POS_REACHED_P ())
6664 goto buffer_pos_reached;
6665 else if (new_x > it->first_visible_x)
6667 /* Glyph is visible. Increment number of glyphs that
6668 would be displayed. */
6669 ++it->hpos;
6671 else
6673 /* Glyph is completely off the left margin of the display
6674 area. Nothing to do. */
6678 if (result != MOVE_UNDEFINED)
6679 break;
6681 else if (BUFFER_POS_REACHED_P ())
6683 buffer_pos_reached:
6684 it->current_x = x;
6685 it->max_ascent = ascent;
6686 it->max_descent = descent;
6687 result = MOVE_POS_MATCH_OR_ZV;
6688 break;
6690 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6692 /* Stop when TO_X specified and reached. This check is
6693 necessary here because of lines consisting of a line end,
6694 only. The line end will not produce any glyphs and we
6695 would never get MOVE_X_REACHED. */
6696 xassert (it->nglyphs == 0);
6697 result = MOVE_X_REACHED;
6698 break;
6701 /* Is this a line end? If yes, we're done. */
6702 if (ITERATOR_AT_END_OF_LINE_P (it))
6704 result = MOVE_NEWLINE_OR_CR;
6705 break;
6708 /* The current display element has been consumed. Advance
6709 to the next. */
6710 set_iterator_to_next (it, 1);
6712 /* Stop if lines are truncated and IT's current x-position is
6713 past the right edge of the window now. */
6714 if (it->truncate_lines_p
6715 && it->current_x >= it->last_visible_x)
6717 #ifdef HAVE_WINDOW_SYSTEM
6718 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6720 if (!get_next_display_element (it)
6721 || BUFFER_POS_REACHED_P ())
6723 result = MOVE_POS_MATCH_OR_ZV;
6724 break;
6726 if (ITERATOR_AT_END_OF_LINE_P (it))
6728 result = MOVE_NEWLINE_OR_CR;
6729 break;
6732 #endif /* HAVE_WINDOW_SYSTEM */
6733 result = MOVE_LINE_TRUNCATED;
6734 break;
6738 #undef BUFFER_POS_REACHED_P
6740 /* Restore the iterator settings altered at the beginning of this
6741 function. */
6742 it->glyph_row = saved_glyph_row;
6743 return result;
6747 /* Move IT forward until it satisfies one or more of the criteria in
6748 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6750 OP is a bit-mask that specifies where to stop, and in particular,
6751 which of those four position arguments makes a difference. See the
6752 description of enum move_operation_enum.
6754 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6755 screen line, this function will set IT to the next position >
6756 TO_CHARPOS. */
6758 void
6759 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6760 struct it *it;
6761 int to_charpos, to_x, to_y, to_vpos;
6762 int op;
6764 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6765 int line_height;
6766 int reached = 0;
6768 for (;;)
6770 if (op & MOVE_TO_VPOS)
6772 /* If no TO_CHARPOS and no TO_X specified, stop at the
6773 start of the line TO_VPOS. */
6774 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6776 if (it->vpos == to_vpos)
6778 reached = 1;
6779 break;
6781 else
6782 skip = move_it_in_display_line_to (it, -1, -1, 0);
6784 else
6786 /* TO_VPOS >= 0 means stop at TO_X in the line at
6787 TO_VPOS, or at TO_POS, whichever comes first. */
6788 if (it->vpos == to_vpos)
6790 reached = 2;
6791 break;
6794 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6796 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6798 reached = 3;
6799 break;
6801 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6803 /* We have reached TO_X but not in the line we want. */
6804 skip = move_it_in_display_line_to (it, to_charpos,
6805 -1, MOVE_TO_POS);
6806 if (skip == MOVE_POS_MATCH_OR_ZV)
6808 reached = 4;
6809 break;
6814 else if (op & MOVE_TO_Y)
6816 struct it it_backup;
6818 /* TO_Y specified means stop at TO_X in the line containing
6819 TO_Y---or at TO_CHARPOS if this is reached first. The
6820 problem is that we can't really tell whether the line
6821 contains TO_Y before we have completely scanned it, and
6822 this may skip past TO_X. What we do is to first scan to
6823 TO_X.
6825 If TO_X is not specified, use a TO_X of zero. The reason
6826 is to make the outcome of this function more predictable.
6827 If we didn't use TO_X == 0, we would stop at the end of
6828 the line which is probably not what a caller would expect
6829 to happen. */
6830 skip = move_it_in_display_line_to (it, to_charpos,
6831 ((op & MOVE_TO_X)
6832 ? to_x : 0),
6833 (MOVE_TO_X
6834 | (op & MOVE_TO_POS)));
6836 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6837 if (skip == MOVE_POS_MATCH_OR_ZV)
6839 reached = 5;
6840 break;
6843 /* If TO_X was reached, we would like to know whether TO_Y
6844 is in the line. This can only be said if we know the
6845 total line height which requires us to scan the rest of
6846 the line. */
6847 if (skip == MOVE_X_REACHED)
6849 it_backup = *it;
6850 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6851 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6852 op & MOVE_TO_POS);
6853 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6856 /* Now, decide whether TO_Y is in this line. */
6857 line_height = it->max_ascent + it->max_descent;
6858 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6860 if (to_y >= it->current_y
6861 && to_y < it->current_y + line_height)
6863 if (skip == MOVE_X_REACHED)
6864 /* If TO_Y is in this line and TO_X was reached above,
6865 we scanned too far. We have to restore IT's settings
6866 to the ones before skipping. */
6867 *it = it_backup;
6868 reached = 6;
6870 else if (skip == MOVE_X_REACHED)
6872 skip = skip2;
6873 if (skip == MOVE_POS_MATCH_OR_ZV)
6874 reached = 7;
6877 if (reached)
6878 break;
6880 else if (BUFFERP (it->object)
6881 && it->method == GET_FROM_BUFFER
6882 && IT_CHARPOS (*it) >= to_charpos)
6883 skip = MOVE_POS_MATCH_OR_ZV;
6884 else
6885 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6887 switch (skip)
6889 case MOVE_POS_MATCH_OR_ZV:
6890 reached = 8;
6891 goto out;
6893 case MOVE_NEWLINE_OR_CR:
6894 set_iterator_to_next (it, 1);
6895 it->continuation_lines_width = 0;
6896 break;
6898 case MOVE_LINE_TRUNCATED:
6899 it->continuation_lines_width = 0;
6900 reseat_at_next_visible_line_start (it, 0);
6901 if ((op & MOVE_TO_POS) != 0
6902 && IT_CHARPOS (*it) > to_charpos)
6904 reached = 9;
6905 goto out;
6907 break;
6909 case MOVE_LINE_CONTINUED:
6910 /* For continued lines ending in a tab, some of the glyphs
6911 associated with the tab are displayed on the current
6912 line. Since it->current_x does not include these glyphs,
6913 we use it->last_visible_x instead. */
6914 it->continuation_lines_width +=
6915 (it->c == '\t') ? it->last_visible_x : it->current_x;
6916 break;
6918 default:
6919 abort ();
6922 /* Reset/increment for the next run. */
6923 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6924 it->current_x = it->hpos = 0;
6925 it->current_y += it->max_ascent + it->max_descent;
6926 ++it->vpos;
6927 last_height = it->max_ascent + it->max_descent;
6928 last_max_ascent = it->max_ascent;
6929 it->max_ascent = it->max_descent = 0;
6932 out:
6934 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6938 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6940 If DY > 0, move IT backward at least that many pixels. DY = 0
6941 means move IT backward to the preceding line start or BEGV. This
6942 function may move over more than DY pixels if IT->current_y - DY
6943 ends up in the middle of a line; in this case IT->current_y will be
6944 set to the top of the line moved to. */
6946 void
6947 move_it_vertically_backward (it, dy)
6948 struct it *it;
6949 int dy;
6951 int nlines, h;
6952 struct it it2, it3;
6953 int start_pos;
6955 move_further_back:
6956 xassert (dy >= 0);
6958 start_pos = IT_CHARPOS (*it);
6960 /* Estimate how many newlines we must move back. */
6961 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6963 /* Set the iterator's position that many lines back. */
6964 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6965 back_to_previous_visible_line_start (it);
6967 /* Reseat the iterator here. When moving backward, we don't want
6968 reseat to skip forward over invisible text, set up the iterator
6969 to deliver from overlay strings at the new position etc. So,
6970 use reseat_1 here. */
6971 reseat_1 (it, it->current.pos, 1);
6973 /* We are now surely at a line start. */
6974 it->current_x = it->hpos = 0;
6975 it->continuation_lines_width = 0;
6977 /* Move forward and see what y-distance we moved. First move to the
6978 start of the next line so that we get its height. We need this
6979 height to be able to tell whether we reached the specified
6980 y-distance. */
6981 it2 = *it;
6982 it2.max_ascent = it2.max_descent = 0;
6985 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6986 MOVE_TO_POS | MOVE_TO_VPOS);
6988 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6989 xassert (IT_CHARPOS (*it) >= BEGV);
6990 it3 = it2;
6992 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6993 xassert (IT_CHARPOS (*it) >= BEGV);
6994 /* H is the actual vertical distance from the position in *IT
6995 and the starting position. */
6996 h = it2.current_y - it->current_y;
6997 /* NLINES is the distance in number of lines. */
6998 nlines = it2.vpos - it->vpos;
7000 /* Correct IT's y and vpos position
7001 so that they are relative to the starting point. */
7002 it->vpos -= nlines;
7003 it->current_y -= h;
7005 if (dy == 0)
7007 /* DY == 0 means move to the start of the screen line. The
7008 value of nlines is > 0 if continuation lines were involved. */
7009 if (nlines > 0)
7010 move_it_by_lines (it, nlines, 1);
7011 #if 0
7012 /* I think this assert is bogus if buffer contains
7013 invisible text or images. KFS. */
7014 xassert (IT_CHARPOS (*it) <= start_pos);
7015 #endif
7017 else
7019 /* The y-position we try to reach, relative to *IT.
7020 Note that H has been subtracted in front of the if-statement. */
7021 int target_y = it->current_y + h - dy;
7022 int y0 = it3.current_y;
7023 int y1 = line_bottom_y (&it3);
7024 int line_height = y1 - y0;
7026 /* If we did not reach target_y, try to move further backward if
7027 we can. If we moved too far backward, try to move forward. */
7028 if (target_y < it->current_y
7029 /* This is heuristic. In a window that's 3 lines high, with
7030 a line height of 13 pixels each, recentering with point
7031 on the bottom line will try to move -39/2 = 19 pixels
7032 backward. Try to avoid moving into the first line. */
7033 && (it->current_y - target_y
7034 > min (window_box_height (it->w), line_height * 2 / 3))
7035 && IT_CHARPOS (*it) > BEGV)
7037 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7038 target_y - it->current_y));
7039 dy = it->current_y - target_y;
7040 goto move_further_back;
7042 else if (target_y >= it->current_y + line_height
7043 && IT_CHARPOS (*it) < ZV)
7045 /* Should move forward by at least one line, maybe more.
7047 Note: Calling move_it_by_lines can be expensive on
7048 terminal frames, where compute_motion is used (via
7049 vmotion) to do the job, when there are very long lines
7050 and truncate-lines is nil. That's the reason for
7051 treating terminal frames specially here. */
7053 if (!FRAME_WINDOW_P (it->f))
7054 move_it_vertically (it, target_y - (it->current_y + line_height));
7055 else
7059 move_it_by_lines (it, 1, 1);
7061 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7064 #if 0
7065 /* I think this assert is bogus if buffer contains
7066 invisible text or images. KFS. */
7067 xassert (IT_CHARPOS (*it) >= BEGV);
7068 #endif
7074 /* Move IT by a specified amount of pixel lines DY. DY negative means
7075 move backwards. DY = 0 means move to start of screen line. At the
7076 end, IT will be on the start of a screen line. */
7078 void
7079 move_it_vertically (it, dy)
7080 struct it *it;
7081 int dy;
7083 if (dy <= 0)
7084 move_it_vertically_backward (it, -dy);
7085 else
7087 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7088 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7089 MOVE_TO_POS | MOVE_TO_Y);
7090 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7092 /* If buffer ends in ZV without a newline, move to the start of
7093 the line to satisfy the post-condition. */
7094 if (IT_CHARPOS (*it) == ZV
7095 && ZV > BEGV
7096 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7097 move_it_by_lines (it, 0, 0);
7102 /* Move iterator IT past the end of the text line it is in. */
7104 void
7105 move_it_past_eol (it)
7106 struct it *it;
7108 enum move_it_result rc;
7110 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7111 if (rc == MOVE_NEWLINE_OR_CR)
7112 set_iterator_to_next (it, 0);
7116 #if 0 /* Currently not used. */
7118 /* Return non-zero if some text between buffer positions START_CHARPOS
7119 and END_CHARPOS is invisible. IT->window is the window for text
7120 property lookup. */
7122 static int
7123 invisible_text_between_p (it, start_charpos, end_charpos)
7124 struct it *it;
7125 int start_charpos, end_charpos;
7127 Lisp_Object prop, limit;
7128 int invisible_found_p;
7130 xassert (it != NULL && start_charpos <= end_charpos);
7132 /* Is text at START invisible? */
7133 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7134 it->window);
7135 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7136 invisible_found_p = 1;
7137 else
7139 limit = Fnext_single_char_property_change (make_number (start_charpos),
7140 Qinvisible, Qnil,
7141 make_number (end_charpos));
7142 invisible_found_p = XFASTINT (limit) < end_charpos;
7145 return invisible_found_p;
7148 #endif /* 0 */
7151 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7152 negative means move up. DVPOS == 0 means move to the start of the
7153 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7154 NEED_Y_P is zero, IT->current_y will be left unchanged.
7156 Further optimization ideas: If we would know that IT->f doesn't use
7157 a face with proportional font, we could be faster for
7158 truncate-lines nil. */
7160 void
7161 move_it_by_lines (it, dvpos, need_y_p)
7162 struct it *it;
7163 int dvpos, need_y_p;
7165 struct position pos;
7167 /* The commented-out optimization uses vmotion on terminals. This
7168 gives bad results, because elements like it->what, on which
7169 callers such as pos_visible_p rely, aren't updated. */
7170 /* if (!FRAME_WINDOW_P (it->f))
7172 struct text_pos textpos;
7174 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7175 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7176 reseat (it, textpos, 1);
7177 it->vpos += pos.vpos;
7178 it->current_y += pos.vpos;
7180 else */
7182 if (dvpos == 0)
7184 /* DVPOS == 0 means move to the start of the screen line. */
7185 move_it_vertically_backward (it, 0);
7186 xassert (it->current_x == 0 && it->hpos == 0);
7187 /* Let next call to line_bottom_y calculate real line height */
7188 last_height = 0;
7190 else if (dvpos > 0)
7192 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7193 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7194 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7196 else
7198 struct it it2;
7199 int start_charpos, i;
7201 /* Start at the beginning of the screen line containing IT's
7202 position. This may actually move vertically backwards,
7203 in case of overlays, so adjust dvpos accordingly. */
7204 dvpos += it->vpos;
7205 move_it_vertically_backward (it, 0);
7206 dvpos -= it->vpos;
7208 /* Go back -DVPOS visible lines and reseat the iterator there. */
7209 start_charpos = IT_CHARPOS (*it);
7210 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7211 back_to_previous_visible_line_start (it);
7212 reseat (it, it->current.pos, 1);
7214 /* Move further back if we end up in a string or an image. */
7215 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7217 /* First try to move to start of display line. */
7218 dvpos += it->vpos;
7219 move_it_vertically_backward (it, 0);
7220 dvpos -= it->vpos;
7221 if (IT_POS_VALID_AFTER_MOVE_P (it))
7222 break;
7223 /* If start of line is still in string or image,
7224 move further back. */
7225 back_to_previous_visible_line_start (it);
7226 reseat (it, it->current.pos, 1);
7227 dvpos--;
7230 it->current_x = it->hpos = 0;
7232 /* Above call may have moved too far if continuation lines
7233 are involved. Scan forward and see if it did. */
7234 it2 = *it;
7235 it2.vpos = it2.current_y = 0;
7236 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7237 it->vpos -= it2.vpos;
7238 it->current_y -= it2.current_y;
7239 it->current_x = it->hpos = 0;
7241 /* If we moved too far back, move IT some lines forward. */
7242 if (it2.vpos > -dvpos)
7244 int delta = it2.vpos + dvpos;
7245 it2 = *it;
7246 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7247 /* Move back again if we got too far ahead. */
7248 if (IT_CHARPOS (*it) >= start_charpos)
7249 *it = it2;
7254 /* Return 1 if IT points into the middle of a display vector. */
7257 in_display_vector_p (it)
7258 struct it *it;
7260 return (it->method == GET_FROM_DISPLAY_VECTOR
7261 && it->current.dpvec_index > 0
7262 && it->dpvec + it->current.dpvec_index != it->dpend);
7266 /***********************************************************************
7267 Messages
7268 ***********************************************************************/
7271 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7272 to *Messages*. */
7274 void
7275 add_to_log (format, arg1, arg2)
7276 char *format;
7277 Lisp_Object arg1, arg2;
7279 Lisp_Object args[3];
7280 Lisp_Object msg, fmt;
7281 char *buffer;
7282 int len;
7283 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7284 USE_SAFE_ALLOCA;
7286 /* Do nothing if called asynchronously. Inserting text into
7287 a buffer may call after-change-functions and alike and
7288 that would means running Lisp asynchronously. */
7289 if (handling_signal)
7290 return;
7292 fmt = msg = Qnil;
7293 GCPRO4 (fmt, msg, arg1, arg2);
7295 args[0] = fmt = build_string (format);
7296 args[1] = arg1;
7297 args[2] = arg2;
7298 msg = Fformat (3, args);
7300 len = SBYTES (msg) + 1;
7301 SAFE_ALLOCA (buffer, char *, len);
7302 bcopy (SDATA (msg), buffer, len);
7304 message_dolog (buffer, len - 1, 1, 0);
7305 SAFE_FREE ();
7307 UNGCPRO;
7311 /* Output a newline in the *Messages* buffer if "needs" one. */
7313 void
7314 message_log_maybe_newline ()
7316 if (message_log_need_newline)
7317 message_dolog ("", 0, 1, 0);
7321 /* Add a string M of length NBYTES to the message log, optionally
7322 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7323 nonzero, means interpret the contents of M as multibyte. This
7324 function calls low-level routines in order to bypass text property
7325 hooks, etc. which might not be safe to run.
7327 This may GC (insert may run before/after change hooks),
7328 so the buffer M must NOT point to a Lisp string. */
7330 void
7331 message_dolog (m, nbytes, nlflag, multibyte)
7332 const char *m;
7333 int nbytes, nlflag, multibyte;
7335 if (!NILP (Vmemory_full))
7336 return;
7338 if (!NILP (Vmessage_log_max))
7340 struct buffer *oldbuf;
7341 Lisp_Object oldpoint, oldbegv, oldzv;
7342 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7343 int point_at_end = 0;
7344 int zv_at_end = 0;
7345 Lisp_Object old_deactivate_mark, tem;
7346 struct gcpro gcpro1;
7348 old_deactivate_mark = Vdeactivate_mark;
7349 oldbuf = current_buffer;
7350 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7351 current_buffer->undo_list = Qt;
7353 oldpoint = message_dolog_marker1;
7354 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7355 oldbegv = message_dolog_marker2;
7356 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7357 oldzv = message_dolog_marker3;
7358 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7359 GCPRO1 (old_deactivate_mark);
7361 if (PT == Z)
7362 point_at_end = 1;
7363 if (ZV == Z)
7364 zv_at_end = 1;
7366 BEGV = BEG;
7367 BEGV_BYTE = BEG_BYTE;
7368 ZV = Z;
7369 ZV_BYTE = Z_BYTE;
7370 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7372 /* Insert the string--maybe converting multibyte to single byte
7373 or vice versa, so that all the text fits the buffer. */
7374 if (multibyte
7375 && NILP (current_buffer->enable_multibyte_characters))
7377 int i, c, char_bytes;
7378 unsigned char work[1];
7380 /* Convert a multibyte string to single-byte
7381 for the *Message* buffer. */
7382 for (i = 0; i < nbytes; i += char_bytes)
7384 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7385 work[0] = (SINGLE_BYTE_CHAR_P (c)
7387 : multibyte_char_to_unibyte (c, Qnil));
7388 insert_1_both (work, 1, 1, 1, 0, 0);
7391 else if (! multibyte
7392 && ! NILP (current_buffer->enable_multibyte_characters))
7394 int i, c, char_bytes;
7395 unsigned char *msg = (unsigned char *) m;
7396 unsigned char str[MAX_MULTIBYTE_LENGTH];
7397 /* Convert a single-byte string to multibyte
7398 for the *Message* buffer. */
7399 for (i = 0; i < nbytes; i++)
7401 c = unibyte_char_to_multibyte (msg[i]);
7402 char_bytes = CHAR_STRING (c, str);
7403 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7406 else if (nbytes)
7407 insert_1 (m, nbytes, 1, 0, 0);
7409 if (nlflag)
7411 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7412 insert_1 ("\n", 1, 1, 0, 0);
7414 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7415 this_bol = PT;
7416 this_bol_byte = PT_BYTE;
7418 /* See if this line duplicates the previous one.
7419 If so, combine duplicates. */
7420 if (this_bol > BEG)
7422 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7423 prev_bol = PT;
7424 prev_bol_byte = PT_BYTE;
7426 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7427 this_bol, this_bol_byte);
7428 if (dup)
7430 del_range_both (prev_bol, prev_bol_byte,
7431 this_bol, this_bol_byte, 0);
7432 if (dup > 1)
7434 char dupstr[40];
7435 int duplen;
7437 /* If you change this format, don't forget to also
7438 change message_log_check_duplicate. */
7439 sprintf (dupstr, " [%d times]", dup);
7440 duplen = strlen (dupstr);
7441 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7442 insert_1 (dupstr, duplen, 1, 0, 1);
7447 /* If we have more than the desired maximum number of lines
7448 in the *Messages* buffer now, delete the oldest ones.
7449 This is safe because we don't have undo in this buffer. */
7451 if (NATNUMP (Vmessage_log_max))
7453 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7454 -XFASTINT (Vmessage_log_max) - 1, 0);
7455 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7458 BEGV = XMARKER (oldbegv)->charpos;
7459 BEGV_BYTE = marker_byte_position (oldbegv);
7461 if (zv_at_end)
7463 ZV = Z;
7464 ZV_BYTE = Z_BYTE;
7466 else
7468 ZV = XMARKER (oldzv)->charpos;
7469 ZV_BYTE = marker_byte_position (oldzv);
7472 if (point_at_end)
7473 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7474 else
7475 /* We can't do Fgoto_char (oldpoint) because it will run some
7476 Lisp code. */
7477 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7478 XMARKER (oldpoint)->bytepos);
7480 UNGCPRO;
7481 unchain_marker (XMARKER (oldpoint));
7482 unchain_marker (XMARKER (oldbegv));
7483 unchain_marker (XMARKER (oldzv));
7485 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7486 set_buffer_internal (oldbuf);
7487 if (NILP (tem))
7488 windows_or_buffers_changed = old_windows_or_buffers_changed;
7489 message_log_need_newline = !nlflag;
7490 Vdeactivate_mark = old_deactivate_mark;
7495 /* We are at the end of the buffer after just having inserted a newline.
7496 (Note: We depend on the fact we won't be crossing the gap.)
7497 Check to see if the most recent message looks a lot like the previous one.
7498 Return 0 if different, 1 if the new one should just replace it, or a
7499 value N > 1 if we should also append " [N times]". */
7501 static int
7502 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7503 int prev_bol, this_bol;
7504 int prev_bol_byte, this_bol_byte;
7506 int i;
7507 int len = Z_BYTE - 1 - this_bol_byte;
7508 int seen_dots = 0;
7509 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7510 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7512 for (i = 0; i < len; i++)
7514 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7515 seen_dots = 1;
7516 if (p1[i] != p2[i])
7517 return seen_dots;
7519 p1 += len;
7520 if (*p1 == '\n')
7521 return 2;
7522 if (*p1++ == ' ' && *p1++ == '[')
7524 int n = 0;
7525 while (*p1 >= '0' && *p1 <= '9')
7526 n = n * 10 + *p1++ - '0';
7527 if (strncmp (p1, " times]\n", 8) == 0)
7528 return n+1;
7530 return 0;
7534 /* Display an echo area message M with a specified length of NBYTES
7535 bytes. The string may include null characters. If M is 0, clear
7536 out any existing message, and let the mini-buffer text show
7537 through.
7539 This may GC, so the buffer M must NOT point to a Lisp string. */
7541 void
7542 message2 (m, nbytes, multibyte)
7543 const char *m;
7544 int nbytes;
7545 int multibyte;
7547 /* First flush out any partial line written with print. */
7548 message_log_maybe_newline ();
7549 if (m)
7550 message_dolog (m, nbytes, 1, multibyte);
7551 message2_nolog (m, nbytes, multibyte);
7555 /* The non-logging counterpart of message2. */
7557 void
7558 message2_nolog (m, nbytes, multibyte)
7559 const char *m;
7560 int nbytes, multibyte;
7562 struct frame *sf = SELECTED_FRAME ();
7563 message_enable_multibyte = multibyte;
7565 if (noninteractive)
7567 if (noninteractive_need_newline)
7568 putc ('\n', stderr);
7569 noninteractive_need_newline = 0;
7570 if (m)
7571 fwrite (m, nbytes, 1, stderr);
7572 if (cursor_in_echo_area == 0)
7573 fprintf (stderr, "\n");
7574 fflush (stderr);
7576 /* A null message buffer means that the frame hasn't really been
7577 initialized yet. Error messages get reported properly by
7578 cmd_error, so this must be just an informative message; toss it. */
7579 else if (INTERACTIVE
7580 && sf->glyphs_initialized_p
7581 && FRAME_MESSAGE_BUF (sf))
7583 Lisp_Object mini_window;
7584 struct frame *f;
7586 /* Get the frame containing the mini-buffer
7587 that the selected frame is using. */
7588 mini_window = FRAME_MINIBUF_WINDOW (sf);
7589 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7591 FRAME_SAMPLE_VISIBILITY (f);
7592 if (FRAME_VISIBLE_P (sf)
7593 && ! FRAME_VISIBLE_P (f))
7594 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7596 if (m)
7598 set_message (m, Qnil, nbytes, multibyte);
7599 if (minibuffer_auto_raise)
7600 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7602 else
7603 clear_message (1, 1);
7605 do_pending_window_change (0);
7606 echo_area_display (1);
7607 do_pending_window_change (0);
7608 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7609 (*frame_up_to_date_hook) (f);
7614 /* Display an echo area message M with a specified length of NBYTES
7615 bytes. The string may include null characters. If M is not a
7616 string, clear out any existing message, and let the mini-buffer
7617 text show through.
7619 This function cancels echoing. */
7621 void
7622 message3 (m, nbytes, multibyte)
7623 Lisp_Object m;
7624 int nbytes;
7625 int multibyte;
7627 struct gcpro gcpro1;
7629 GCPRO1 (m);
7630 clear_message (1,1);
7631 cancel_echoing ();
7633 /* First flush out any partial line written with print. */
7634 message_log_maybe_newline ();
7635 if (STRINGP (m))
7637 char *buffer;
7638 USE_SAFE_ALLOCA;
7640 SAFE_ALLOCA (buffer, char *, nbytes);
7641 bcopy (SDATA (m), buffer, nbytes);
7642 message_dolog (buffer, nbytes, 1, multibyte);
7643 SAFE_FREE ();
7645 message3_nolog (m, nbytes, multibyte);
7647 UNGCPRO;
7651 /* The non-logging version of message3.
7652 This does not cancel echoing, because it is used for echoing.
7653 Perhaps we need to make a separate function for echoing
7654 and make this cancel echoing. */
7656 void
7657 message3_nolog (m, nbytes, multibyte)
7658 Lisp_Object m;
7659 int nbytes, multibyte;
7661 struct frame *sf = SELECTED_FRAME ();
7662 message_enable_multibyte = multibyte;
7664 if (noninteractive)
7666 if (noninteractive_need_newline)
7667 putc ('\n', stderr);
7668 noninteractive_need_newline = 0;
7669 if (STRINGP (m))
7670 fwrite (SDATA (m), nbytes, 1, stderr);
7671 if (cursor_in_echo_area == 0)
7672 fprintf (stderr, "\n");
7673 fflush (stderr);
7675 /* A null message buffer means that the frame hasn't really been
7676 initialized yet. Error messages get reported properly by
7677 cmd_error, so this must be just an informative message; toss it. */
7678 else if (INTERACTIVE
7679 && sf->glyphs_initialized_p
7680 && FRAME_MESSAGE_BUF (sf))
7682 Lisp_Object mini_window;
7683 Lisp_Object frame;
7684 struct frame *f;
7686 /* Get the frame containing the mini-buffer
7687 that the selected frame is using. */
7688 mini_window = FRAME_MINIBUF_WINDOW (sf);
7689 frame = XWINDOW (mini_window)->frame;
7690 f = XFRAME (frame);
7692 FRAME_SAMPLE_VISIBILITY (f);
7693 if (FRAME_VISIBLE_P (sf)
7694 && !FRAME_VISIBLE_P (f))
7695 Fmake_frame_visible (frame);
7697 if (STRINGP (m) && SCHARS (m) > 0)
7699 set_message (NULL, m, nbytes, multibyte);
7700 if (minibuffer_auto_raise)
7701 Fraise_frame (frame);
7702 /* Assume we are not echoing.
7703 (If we are, echo_now will override this.) */
7704 echo_message_buffer = Qnil;
7706 else
7707 clear_message (1, 1);
7709 do_pending_window_change (0);
7710 echo_area_display (1);
7711 do_pending_window_change (0);
7712 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7713 (*frame_up_to_date_hook) (f);
7718 /* Display a null-terminated echo area message M. If M is 0, clear
7719 out any existing message, and let the mini-buffer text show through.
7721 The buffer M must continue to exist until after the echo area gets
7722 cleared or some other message gets displayed there. Do not pass
7723 text that is stored in a Lisp string. Do not pass text in a buffer
7724 that was alloca'd. */
7726 void
7727 message1 (m)
7728 char *m;
7730 message2 (m, (m ? strlen (m) : 0), 0);
7734 /* The non-logging counterpart of message1. */
7736 void
7737 message1_nolog (m)
7738 char *m;
7740 message2_nolog (m, (m ? strlen (m) : 0), 0);
7743 /* Display a message M which contains a single %s
7744 which gets replaced with STRING. */
7746 void
7747 message_with_string (m, string, log)
7748 char *m;
7749 Lisp_Object string;
7750 int log;
7752 CHECK_STRING (string);
7754 if (noninteractive)
7756 if (m)
7758 if (noninteractive_need_newline)
7759 putc ('\n', stderr);
7760 noninteractive_need_newline = 0;
7761 fprintf (stderr, m, SDATA (string));
7762 if (cursor_in_echo_area == 0)
7763 fprintf (stderr, "\n");
7764 fflush (stderr);
7767 else if (INTERACTIVE)
7769 /* The frame whose minibuffer we're going to display the message on.
7770 It may be larger than the selected frame, so we need
7771 to use its buffer, not the selected frame's buffer. */
7772 Lisp_Object mini_window;
7773 struct frame *f, *sf = SELECTED_FRAME ();
7775 /* Get the frame containing the minibuffer
7776 that the selected frame is using. */
7777 mini_window = FRAME_MINIBUF_WINDOW (sf);
7778 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7780 /* A null message buffer means that the frame hasn't really been
7781 initialized yet. Error messages get reported properly by
7782 cmd_error, so this must be just an informative message; toss it. */
7783 if (FRAME_MESSAGE_BUF (f))
7785 Lisp_Object args[2], message;
7786 struct gcpro gcpro1, gcpro2;
7788 args[0] = build_string (m);
7789 args[1] = message = string;
7790 GCPRO2 (args[0], message);
7791 gcpro1.nvars = 2;
7793 message = Fformat (2, args);
7795 if (log)
7796 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7797 else
7798 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7800 UNGCPRO;
7802 /* Print should start at the beginning of the message
7803 buffer next time. */
7804 message_buf_print = 0;
7810 /* Dump an informative message to the minibuf. If M is 0, clear out
7811 any existing message, and let the mini-buffer text show through. */
7813 /* VARARGS 1 */
7814 void
7815 message (m, a1, a2, a3)
7816 char *m;
7817 EMACS_INT a1, a2, a3;
7819 if (noninteractive)
7821 if (m)
7823 if (noninteractive_need_newline)
7824 putc ('\n', stderr);
7825 noninteractive_need_newline = 0;
7826 fprintf (stderr, m, a1, a2, a3);
7827 if (cursor_in_echo_area == 0)
7828 fprintf (stderr, "\n");
7829 fflush (stderr);
7832 else if (INTERACTIVE)
7834 /* The frame whose mini-buffer we're going to display the message
7835 on. It may be larger than the selected frame, so we need to
7836 use its buffer, not the selected frame's buffer. */
7837 Lisp_Object mini_window;
7838 struct frame *f, *sf = SELECTED_FRAME ();
7840 /* Get the frame containing the mini-buffer
7841 that the selected frame is using. */
7842 mini_window = FRAME_MINIBUF_WINDOW (sf);
7843 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7845 /* A null message buffer means that the frame hasn't really been
7846 initialized yet. Error messages get reported properly by
7847 cmd_error, so this must be just an informative message; toss
7848 it. */
7849 if (FRAME_MESSAGE_BUF (f))
7851 if (m)
7853 int len;
7854 #ifdef NO_ARG_ARRAY
7855 char *a[3];
7856 a[0] = (char *) a1;
7857 a[1] = (char *) a2;
7858 a[2] = (char *) a3;
7860 len = doprnt (FRAME_MESSAGE_BUF (f),
7861 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7862 #else
7863 len = doprnt (FRAME_MESSAGE_BUF (f),
7864 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7865 (char **) &a1);
7866 #endif /* NO_ARG_ARRAY */
7868 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7870 else
7871 message1 (0);
7873 /* Print should start at the beginning of the message
7874 buffer next time. */
7875 message_buf_print = 0;
7881 /* The non-logging version of message. */
7883 void
7884 message_nolog (m, a1, a2, a3)
7885 char *m;
7886 EMACS_INT a1, a2, a3;
7888 Lisp_Object old_log_max;
7889 old_log_max = Vmessage_log_max;
7890 Vmessage_log_max = Qnil;
7891 message (m, a1, a2, a3);
7892 Vmessage_log_max = old_log_max;
7896 /* Display the current message in the current mini-buffer. This is
7897 only called from error handlers in process.c, and is not time
7898 critical. */
7900 void
7901 update_echo_area ()
7903 if (!NILP (echo_area_buffer[0]))
7905 Lisp_Object string;
7906 string = Fcurrent_message ();
7907 message3 (string, SBYTES (string),
7908 !NILP (current_buffer->enable_multibyte_characters));
7913 /* Make sure echo area buffers in `echo_buffers' are live.
7914 If they aren't, make new ones. */
7916 static void
7917 ensure_echo_area_buffers ()
7919 int i;
7921 for (i = 0; i < 2; ++i)
7922 if (!BUFFERP (echo_buffer[i])
7923 || NILP (XBUFFER (echo_buffer[i])->name))
7925 char name[30];
7926 Lisp_Object old_buffer;
7927 int j;
7929 old_buffer = echo_buffer[i];
7930 sprintf (name, " *Echo Area %d*", i);
7931 echo_buffer[i] = Fget_buffer_create (build_string (name));
7932 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7934 for (j = 0; j < 2; ++j)
7935 if (EQ (old_buffer, echo_area_buffer[j]))
7936 echo_area_buffer[j] = echo_buffer[i];
7941 /* Call FN with args A1..A4 with either the current or last displayed
7942 echo_area_buffer as current buffer.
7944 WHICH zero means use the current message buffer
7945 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7946 from echo_buffer[] and clear it.
7948 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7949 suitable buffer from echo_buffer[] and clear it.
7951 Value is what FN returns. */
7953 static int
7954 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7955 struct window *w;
7956 int which;
7957 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7958 EMACS_INT a1;
7959 Lisp_Object a2;
7960 EMACS_INT a3, a4;
7962 Lisp_Object buffer;
7963 int this_one, the_other, clear_buffer_p, rc;
7964 int count = SPECPDL_INDEX ();
7966 /* If buffers aren't live, make new ones. */
7967 ensure_echo_area_buffers ();
7969 clear_buffer_p = 0;
7971 if (which == 0)
7972 this_one = 0, the_other = 1;
7973 else if (which > 0)
7974 this_one = 1, the_other = 0;
7976 /* Choose a suitable buffer from echo_buffer[] is we don't
7977 have one. */
7978 if (NILP (echo_area_buffer[this_one]))
7980 echo_area_buffer[this_one]
7981 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7982 ? echo_buffer[the_other]
7983 : echo_buffer[this_one]);
7984 clear_buffer_p = 1;
7987 buffer = echo_area_buffer[this_one];
7989 /* Don't get confused by reusing the buffer used for echoing
7990 for a different purpose. */
7991 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7992 cancel_echoing ();
7994 record_unwind_protect (unwind_with_echo_area_buffer,
7995 with_echo_area_buffer_unwind_data (w));
7997 /* Make the echo area buffer current. Note that for display
7998 purposes, it is not necessary that the displayed window's buffer
7999 == current_buffer, except for text property lookup. So, let's
8000 only set that buffer temporarily here without doing a full
8001 Fset_window_buffer. We must also change w->pointm, though,
8002 because otherwise an assertions in unshow_buffer fails, and Emacs
8003 aborts. */
8004 set_buffer_internal_1 (XBUFFER (buffer));
8005 if (w)
8007 w->buffer = buffer;
8008 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8011 current_buffer->undo_list = Qt;
8012 current_buffer->read_only = Qnil;
8013 specbind (Qinhibit_read_only, Qt);
8014 specbind (Qinhibit_modification_hooks, Qt);
8016 if (clear_buffer_p && Z > BEG)
8017 del_range (BEG, Z);
8019 xassert (BEGV >= BEG);
8020 xassert (ZV <= Z && ZV >= BEGV);
8022 rc = fn (a1, a2, a3, a4);
8024 xassert (BEGV >= BEG);
8025 xassert (ZV <= Z && ZV >= BEGV);
8027 unbind_to (count, Qnil);
8028 return rc;
8032 /* Save state that should be preserved around the call to the function
8033 FN called in with_echo_area_buffer. */
8035 static Lisp_Object
8036 with_echo_area_buffer_unwind_data (w)
8037 struct window *w;
8039 int i = 0;
8040 Lisp_Object vector;
8042 /* Reduce consing by keeping one vector in
8043 Vwith_echo_area_save_vector. */
8044 vector = Vwith_echo_area_save_vector;
8045 Vwith_echo_area_save_vector = Qnil;
8047 if (NILP (vector))
8048 vector = Fmake_vector (make_number (7), Qnil);
8050 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8051 AREF (vector, i) = Vdeactivate_mark, ++i;
8052 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8054 if (w)
8056 XSETWINDOW (AREF (vector, i), w); ++i;
8057 AREF (vector, i) = w->buffer; ++i;
8058 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8059 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8061 else
8063 int end = i + 4;
8064 for (; i < end; ++i)
8065 AREF (vector, i) = Qnil;
8068 xassert (i == ASIZE (vector));
8069 return vector;
8073 /* Restore global state from VECTOR which was created by
8074 with_echo_area_buffer_unwind_data. */
8076 static Lisp_Object
8077 unwind_with_echo_area_buffer (vector)
8078 Lisp_Object vector;
8080 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8081 Vdeactivate_mark = AREF (vector, 1);
8082 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8084 if (WINDOWP (AREF (vector, 3)))
8086 struct window *w;
8087 Lisp_Object buffer, charpos, bytepos;
8089 w = XWINDOW (AREF (vector, 3));
8090 buffer = AREF (vector, 4);
8091 charpos = AREF (vector, 5);
8092 bytepos = AREF (vector, 6);
8094 w->buffer = buffer;
8095 set_marker_both (w->pointm, buffer,
8096 XFASTINT (charpos), XFASTINT (bytepos));
8099 Vwith_echo_area_save_vector = vector;
8100 return Qnil;
8104 /* Set up the echo area for use by print functions. MULTIBYTE_P
8105 non-zero means we will print multibyte. */
8107 void
8108 setup_echo_area_for_printing (multibyte_p)
8109 int multibyte_p;
8111 /* If we can't find an echo area any more, exit. */
8112 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8113 Fkill_emacs (Qnil);
8115 ensure_echo_area_buffers ();
8117 if (!message_buf_print)
8119 /* A message has been output since the last time we printed.
8120 Choose a fresh echo area buffer. */
8121 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8122 echo_area_buffer[0] = echo_buffer[1];
8123 else
8124 echo_area_buffer[0] = echo_buffer[0];
8126 /* Switch to that buffer and clear it. */
8127 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8128 current_buffer->truncate_lines = Qnil;
8130 if (Z > BEG)
8132 int count = SPECPDL_INDEX ();
8133 specbind (Qinhibit_read_only, Qt);
8134 /* Note that undo recording is always disabled. */
8135 del_range (BEG, Z);
8136 unbind_to (count, Qnil);
8138 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8140 /* Set up the buffer for the multibyteness we need. */
8141 if (multibyte_p
8142 != !NILP (current_buffer->enable_multibyte_characters))
8143 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8145 /* Raise the frame containing the echo area. */
8146 if (minibuffer_auto_raise)
8148 struct frame *sf = SELECTED_FRAME ();
8149 Lisp_Object mini_window;
8150 mini_window = FRAME_MINIBUF_WINDOW (sf);
8151 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8154 message_log_maybe_newline ();
8155 message_buf_print = 1;
8157 else
8159 if (NILP (echo_area_buffer[0]))
8161 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8162 echo_area_buffer[0] = echo_buffer[1];
8163 else
8164 echo_area_buffer[0] = echo_buffer[0];
8167 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8169 /* Someone switched buffers between print requests. */
8170 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8171 current_buffer->truncate_lines = Qnil;
8177 /* Display an echo area message in window W. Value is non-zero if W's
8178 height is changed. If display_last_displayed_message_p is
8179 non-zero, display the message that was last displayed, otherwise
8180 display the current message. */
8182 static int
8183 display_echo_area (w)
8184 struct window *w;
8186 int i, no_message_p, window_height_changed_p, count;
8188 /* Temporarily disable garbage collections while displaying the echo
8189 area. This is done because a GC can print a message itself.
8190 That message would modify the echo area buffer's contents while a
8191 redisplay of the buffer is going on, and seriously confuse
8192 redisplay. */
8193 count = inhibit_garbage_collection ();
8195 /* If there is no message, we must call display_echo_area_1
8196 nevertheless because it resizes the window. But we will have to
8197 reset the echo_area_buffer in question to nil at the end because
8198 with_echo_area_buffer will sets it to an empty buffer. */
8199 i = display_last_displayed_message_p ? 1 : 0;
8200 no_message_p = NILP (echo_area_buffer[i]);
8202 window_height_changed_p
8203 = with_echo_area_buffer (w, display_last_displayed_message_p,
8204 display_echo_area_1,
8205 (EMACS_INT) w, Qnil, 0, 0);
8207 if (no_message_p)
8208 echo_area_buffer[i] = Qnil;
8210 unbind_to (count, Qnil);
8211 return window_height_changed_p;
8215 /* Helper for display_echo_area. Display the current buffer which
8216 contains the current echo area message in window W, a mini-window,
8217 a pointer to which is passed in A1. A2..A4 are currently not used.
8218 Change the height of W so that all of the message is displayed.
8219 Value is non-zero if height of W was changed. */
8221 static int
8222 display_echo_area_1 (a1, a2, a3, a4)
8223 EMACS_INT a1;
8224 Lisp_Object a2;
8225 EMACS_INT a3, a4;
8227 struct window *w = (struct window *) a1;
8228 Lisp_Object window;
8229 struct text_pos start;
8230 int window_height_changed_p = 0;
8232 /* Do this before displaying, so that we have a large enough glyph
8233 matrix for the display. If we can't get enough space for the
8234 whole text, display the last N lines. That works by setting w->start. */
8235 window_height_changed_p = resize_mini_window (w, 0);
8237 /* Use the starting position chosen by resize_mini_window. */
8238 SET_TEXT_POS_FROM_MARKER (start, w->start);
8240 /* Display. */
8241 clear_glyph_matrix (w->desired_matrix);
8242 XSETWINDOW (window, w);
8243 try_window (window, start, 0);
8245 return window_height_changed_p;
8249 /* Resize the echo area window to exactly the size needed for the
8250 currently displayed message, if there is one. If a mini-buffer
8251 is active, don't shrink it. */
8253 void
8254 resize_echo_area_exactly ()
8256 if (BUFFERP (echo_area_buffer[0])
8257 && WINDOWP (echo_area_window))
8259 struct window *w = XWINDOW (echo_area_window);
8260 int resized_p;
8261 Lisp_Object resize_exactly;
8263 if (minibuf_level == 0)
8264 resize_exactly = Qt;
8265 else
8266 resize_exactly = Qnil;
8268 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8269 (EMACS_INT) w, resize_exactly, 0, 0);
8270 if (resized_p)
8272 ++windows_or_buffers_changed;
8273 ++update_mode_lines;
8274 redisplay_internal (0);
8280 /* Callback function for with_echo_area_buffer, when used from
8281 resize_echo_area_exactly. A1 contains a pointer to the window to
8282 resize, EXACTLY non-nil means resize the mini-window exactly to the
8283 size of the text displayed. A3 and A4 are not used. Value is what
8284 resize_mini_window returns. */
8286 static int
8287 resize_mini_window_1 (a1, exactly, a3, a4)
8288 EMACS_INT a1;
8289 Lisp_Object exactly;
8290 EMACS_INT a3, a4;
8292 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8296 /* Resize mini-window W to fit the size of its contents. EXACT:P
8297 means size the window exactly to the size needed. Otherwise, it's
8298 only enlarged until W's buffer is empty.
8300 Set W->start to the right place to begin display. If the whole
8301 contents fit, start at the beginning. Otherwise, start so as
8302 to make the end of the contents appear. This is particularly
8303 important for y-or-n-p, but seems desirable generally.
8305 Value is non-zero if the window height has been changed. */
8308 resize_mini_window (w, exact_p)
8309 struct window *w;
8310 int exact_p;
8312 struct frame *f = XFRAME (w->frame);
8313 int window_height_changed_p = 0;
8315 xassert (MINI_WINDOW_P (w));
8317 /* By default, start display at the beginning. */
8318 set_marker_both (w->start, w->buffer,
8319 BUF_BEGV (XBUFFER (w->buffer)),
8320 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8322 /* Don't resize windows while redisplaying a window; it would
8323 confuse redisplay functions when the size of the window they are
8324 displaying changes from under them. Such a resizing can happen,
8325 for instance, when which-func prints a long message while
8326 we are running fontification-functions. We're running these
8327 functions with safe_call which binds inhibit-redisplay to t. */
8328 if (!NILP (Vinhibit_redisplay))
8329 return 0;
8331 /* Nil means don't try to resize. */
8332 if (NILP (Vresize_mini_windows)
8333 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8334 return 0;
8336 if (!FRAME_MINIBUF_ONLY_P (f))
8338 struct it it;
8339 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8340 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8341 int height, max_height;
8342 int unit = FRAME_LINE_HEIGHT (f);
8343 struct text_pos start;
8344 struct buffer *old_current_buffer = NULL;
8346 if (current_buffer != XBUFFER (w->buffer))
8348 old_current_buffer = current_buffer;
8349 set_buffer_internal (XBUFFER (w->buffer));
8352 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8354 /* Compute the max. number of lines specified by the user. */
8355 if (FLOATP (Vmax_mini_window_height))
8356 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8357 else if (INTEGERP (Vmax_mini_window_height))
8358 max_height = XINT (Vmax_mini_window_height);
8359 else
8360 max_height = total_height / 4;
8362 /* Correct that max. height if it's bogus. */
8363 max_height = max (1, max_height);
8364 max_height = min (total_height, max_height);
8366 /* Find out the height of the text in the window. */
8367 if (it.truncate_lines_p)
8368 height = 1;
8369 else
8371 last_height = 0;
8372 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8373 if (it.max_ascent == 0 && it.max_descent == 0)
8374 height = it.current_y + last_height;
8375 else
8376 height = it.current_y + it.max_ascent + it.max_descent;
8377 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8378 height = (height + unit - 1) / unit;
8381 /* Compute a suitable window start. */
8382 if (height > max_height)
8384 height = max_height;
8385 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8386 move_it_vertically_backward (&it, (height - 1) * unit);
8387 start = it.current.pos;
8389 else
8390 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8391 SET_MARKER_FROM_TEXT_POS (w->start, start);
8393 if (EQ (Vresize_mini_windows, Qgrow_only))
8395 /* Let it grow only, until we display an empty message, in which
8396 case the window shrinks again. */
8397 if (height > WINDOW_TOTAL_LINES (w))
8399 int old_height = WINDOW_TOTAL_LINES (w);
8400 freeze_window_starts (f, 1);
8401 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8402 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8404 else if (height < WINDOW_TOTAL_LINES (w)
8405 && (exact_p || BEGV == ZV))
8407 int old_height = WINDOW_TOTAL_LINES (w);
8408 freeze_window_starts (f, 0);
8409 shrink_mini_window (w);
8410 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8413 else
8415 /* Always resize to exact size needed. */
8416 if (height > WINDOW_TOTAL_LINES (w))
8418 int old_height = WINDOW_TOTAL_LINES (w);
8419 freeze_window_starts (f, 1);
8420 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8421 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8423 else if (height < WINDOW_TOTAL_LINES (w))
8425 int old_height = WINDOW_TOTAL_LINES (w);
8426 freeze_window_starts (f, 0);
8427 shrink_mini_window (w);
8429 if (height)
8431 freeze_window_starts (f, 1);
8432 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8435 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8439 if (old_current_buffer)
8440 set_buffer_internal (old_current_buffer);
8443 return window_height_changed_p;
8447 /* Value is the current message, a string, or nil if there is no
8448 current message. */
8450 Lisp_Object
8451 current_message ()
8453 Lisp_Object msg;
8455 if (NILP (echo_area_buffer[0]))
8456 msg = Qnil;
8457 else
8459 with_echo_area_buffer (0, 0, current_message_1,
8460 (EMACS_INT) &msg, Qnil, 0, 0);
8461 if (NILP (msg))
8462 echo_area_buffer[0] = Qnil;
8465 return msg;
8469 static int
8470 current_message_1 (a1, a2, a3, a4)
8471 EMACS_INT a1;
8472 Lisp_Object a2;
8473 EMACS_INT a3, a4;
8475 Lisp_Object *msg = (Lisp_Object *) a1;
8477 if (Z > BEG)
8478 *msg = make_buffer_string (BEG, Z, 1);
8479 else
8480 *msg = Qnil;
8481 return 0;
8485 /* Push the current message on Vmessage_stack for later restauration
8486 by restore_message. Value is non-zero if the current message isn't
8487 empty. This is a relatively infrequent operation, so it's not
8488 worth optimizing. */
8491 push_message ()
8493 Lisp_Object msg;
8494 msg = current_message ();
8495 Vmessage_stack = Fcons (msg, Vmessage_stack);
8496 return STRINGP (msg);
8500 /* Restore message display from the top of Vmessage_stack. */
8502 void
8503 restore_message ()
8505 Lisp_Object msg;
8507 xassert (CONSP (Vmessage_stack));
8508 msg = XCAR (Vmessage_stack);
8509 if (STRINGP (msg))
8510 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8511 else
8512 message3_nolog (msg, 0, 0);
8516 /* Handler for record_unwind_protect calling pop_message. */
8518 Lisp_Object
8519 pop_message_unwind (dummy)
8520 Lisp_Object dummy;
8522 pop_message ();
8523 return Qnil;
8526 /* Pop the top-most entry off Vmessage_stack. */
8528 void
8529 pop_message ()
8531 xassert (CONSP (Vmessage_stack));
8532 Vmessage_stack = XCDR (Vmessage_stack);
8536 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8537 exits. If the stack is not empty, we have a missing pop_message
8538 somewhere. */
8540 void
8541 check_message_stack ()
8543 if (!NILP (Vmessage_stack))
8544 abort ();
8548 /* Truncate to NCHARS what will be displayed in the echo area the next
8549 time we display it---but don't redisplay it now. */
8551 void
8552 truncate_echo_area (nchars)
8553 int nchars;
8555 if (nchars == 0)
8556 echo_area_buffer[0] = Qnil;
8557 /* A null message buffer means that the frame hasn't really been
8558 initialized yet. Error messages get reported properly by
8559 cmd_error, so this must be just an informative message; toss it. */
8560 else if (!noninteractive
8561 && INTERACTIVE
8562 && !NILP (echo_area_buffer[0]))
8564 struct frame *sf = SELECTED_FRAME ();
8565 if (FRAME_MESSAGE_BUF (sf))
8566 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8571 /* Helper function for truncate_echo_area. Truncate the current
8572 message to at most NCHARS characters. */
8574 static int
8575 truncate_message_1 (nchars, a2, a3, a4)
8576 EMACS_INT nchars;
8577 Lisp_Object a2;
8578 EMACS_INT a3, a4;
8580 if (BEG + nchars < Z)
8581 del_range (BEG + nchars, Z);
8582 if (Z == BEG)
8583 echo_area_buffer[0] = Qnil;
8584 return 0;
8588 /* Set the current message to a substring of S or STRING.
8590 If STRING is a Lisp string, set the message to the first NBYTES
8591 bytes from STRING. NBYTES zero means use the whole string. If
8592 STRING is multibyte, the message will be displayed multibyte.
8594 If S is not null, set the message to the first LEN bytes of S. LEN
8595 zero means use the whole string. MULTIBYTE_P non-zero means S is
8596 multibyte. Display the message multibyte in that case.
8598 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8599 to t before calling set_message_1 (which calls insert).
8602 void
8603 set_message (s, string, nbytes, multibyte_p)
8604 const char *s;
8605 Lisp_Object string;
8606 int nbytes, multibyte_p;
8608 message_enable_multibyte
8609 = ((s && multibyte_p)
8610 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8612 with_echo_area_buffer (0, 0, set_message_1,
8613 (EMACS_INT) s, string, nbytes, multibyte_p);
8614 message_buf_print = 0;
8615 help_echo_showing_p = 0;
8619 /* Helper function for set_message. Arguments have the same meaning
8620 as there, with A1 corresponding to S and A2 corresponding to STRING
8621 This function is called with the echo area buffer being
8622 current. */
8624 static int
8625 set_message_1 (a1, a2, nbytes, multibyte_p)
8626 EMACS_INT a1;
8627 Lisp_Object a2;
8628 EMACS_INT nbytes, multibyte_p;
8630 const char *s = (const char *) a1;
8631 Lisp_Object string = a2;
8633 /* Change multibyteness of the echo buffer appropriately. */
8634 if (message_enable_multibyte
8635 != !NILP (current_buffer->enable_multibyte_characters))
8636 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8638 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8640 /* Insert new message at BEG. */
8641 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8642 Ferase_buffer ();
8644 if (STRINGP (string))
8646 int nchars;
8648 if (nbytes == 0)
8649 nbytes = SBYTES (string);
8650 nchars = string_byte_to_char (string, nbytes);
8652 /* This function takes care of single/multibyte conversion. We
8653 just have to ensure that the echo area buffer has the right
8654 setting of enable_multibyte_characters. */
8655 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8657 else if (s)
8659 if (nbytes == 0)
8660 nbytes = strlen (s);
8662 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8664 /* Convert from multi-byte to single-byte. */
8665 int i, c, n;
8666 unsigned char work[1];
8668 /* Convert a multibyte string to single-byte. */
8669 for (i = 0; i < nbytes; i += n)
8671 c = string_char_and_length (s + i, nbytes - i, &n);
8672 work[0] = (SINGLE_BYTE_CHAR_P (c)
8674 : multibyte_char_to_unibyte (c, Qnil));
8675 insert_1_both (work, 1, 1, 1, 0, 0);
8678 else if (!multibyte_p
8679 && !NILP (current_buffer->enable_multibyte_characters))
8681 /* Convert from single-byte to multi-byte. */
8682 int i, c, n;
8683 const unsigned char *msg = (const unsigned char *) s;
8684 unsigned char str[MAX_MULTIBYTE_LENGTH];
8686 /* Convert a single-byte string to multibyte. */
8687 for (i = 0; i < nbytes; i++)
8689 c = unibyte_char_to_multibyte (msg[i]);
8690 n = CHAR_STRING (c, str);
8691 insert_1_both (str, 1, n, 1, 0, 0);
8694 else
8695 insert_1 (s, nbytes, 1, 0, 0);
8698 return 0;
8702 /* Clear messages. CURRENT_P non-zero means clear the current
8703 message. LAST_DISPLAYED_P non-zero means clear the message
8704 last displayed. */
8706 void
8707 clear_message (current_p, last_displayed_p)
8708 int current_p, last_displayed_p;
8710 if (current_p)
8712 echo_area_buffer[0] = Qnil;
8713 message_cleared_p = 1;
8716 if (last_displayed_p)
8717 echo_area_buffer[1] = Qnil;
8719 message_buf_print = 0;
8722 /* Clear garbaged frames.
8724 This function is used where the old redisplay called
8725 redraw_garbaged_frames which in turn called redraw_frame which in
8726 turn called clear_frame. The call to clear_frame was a source of
8727 flickering. I believe a clear_frame is not necessary. It should
8728 suffice in the new redisplay to invalidate all current matrices,
8729 and ensure a complete redisplay of all windows. */
8731 static void
8732 clear_garbaged_frames ()
8734 if (frame_garbaged)
8736 Lisp_Object tail, frame;
8737 int changed_count = 0;
8739 FOR_EACH_FRAME (tail, frame)
8741 struct frame *f = XFRAME (frame);
8743 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8745 if (f->resized_p)
8747 Fredraw_frame (frame);
8748 f->force_flush_display_p = 1;
8750 clear_current_matrices (f);
8751 changed_count++;
8752 f->garbaged = 0;
8753 f->resized_p = 0;
8757 frame_garbaged = 0;
8758 if (changed_count)
8759 ++windows_or_buffers_changed;
8764 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8765 is non-zero update selected_frame. Value is non-zero if the
8766 mini-windows height has been changed. */
8768 static int
8769 echo_area_display (update_frame_p)
8770 int update_frame_p;
8772 Lisp_Object mini_window;
8773 struct window *w;
8774 struct frame *f;
8775 int window_height_changed_p = 0;
8776 struct frame *sf = SELECTED_FRAME ();
8778 mini_window = FRAME_MINIBUF_WINDOW (sf);
8779 w = XWINDOW (mini_window);
8780 f = XFRAME (WINDOW_FRAME (w));
8782 /* Don't display if frame is invisible or not yet initialized. */
8783 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8784 return 0;
8786 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8787 #ifndef MAC_OS8
8788 #ifdef HAVE_WINDOW_SYSTEM
8789 /* When Emacs starts, selected_frame may be a visible terminal
8790 frame, even if we run under a window system. If we let this
8791 through, a message would be displayed on the terminal. */
8792 if (EQ (selected_frame, Vterminal_frame)
8793 && !NILP (Vwindow_system))
8794 return 0;
8795 #endif /* HAVE_WINDOW_SYSTEM */
8796 #endif
8798 /* Redraw garbaged frames. */
8799 if (frame_garbaged)
8800 clear_garbaged_frames ();
8802 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8804 echo_area_window = mini_window;
8805 window_height_changed_p = display_echo_area (w);
8806 w->must_be_updated_p = 1;
8808 /* Update the display, unless called from redisplay_internal.
8809 Also don't update the screen during redisplay itself. The
8810 update will happen at the end of redisplay, and an update
8811 here could cause confusion. */
8812 if (update_frame_p && !redisplaying_p)
8814 int n = 0;
8816 /* If the display update has been interrupted by pending
8817 input, update mode lines in the frame. Due to the
8818 pending input, it might have been that redisplay hasn't
8819 been called, so that mode lines above the echo area are
8820 garbaged. This looks odd, so we prevent it here. */
8821 if (!display_completed)
8822 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8824 if (window_height_changed_p
8825 /* Don't do this if Emacs is shutting down. Redisplay
8826 needs to run hooks. */
8827 && !NILP (Vrun_hooks))
8829 /* Must update other windows. Likewise as in other
8830 cases, don't let this update be interrupted by
8831 pending input. */
8832 int count = SPECPDL_INDEX ();
8833 specbind (Qredisplay_dont_pause, Qt);
8834 windows_or_buffers_changed = 1;
8835 redisplay_internal (0);
8836 unbind_to (count, Qnil);
8838 else if (FRAME_WINDOW_P (f) && n == 0)
8840 /* Window configuration is the same as before.
8841 Can do with a display update of the echo area,
8842 unless we displayed some mode lines. */
8843 update_single_window (w, 1);
8844 rif->flush_display (f);
8846 else
8847 update_frame (f, 1, 1);
8849 /* If cursor is in the echo area, make sure that the next
8850 redisplay displays the minibuffer, so that the cursor will
8851 be replaced with what the minibuffer wants. */
8852 if (cursor_in_echo_area)
8853 ++windows_or_buffers_changed;
8856 else if (!EQ (mini_window, selected_window))
8857 windows_or_buffers_changed++;
8859 /* The current message is now also the last one displayed. */
8860 echo_area_buffer[1] = echo_area_buffer[0];
8862 /* Prevent redisplay optimization in redisplay_internal by resetting
8863 this_line_start_pos. This is done because the mini-buffer now
8864 displays the message instead of its buffer text. */
8865 if (EQ (mini_window, selected_window))
8866 CHARPOS (this_line_start_pos) = 0;
8868 return window_height_changed_p;
8873 /***********************************************************************
8874 Mode Lines and Frame Titles
8875 ***********************************************************************/
8877 /* A buffer for constructing non-propertized mode-line strings and
8878 frame titles in it; allocated from the heap in init_xdisp and
8879 resized as needed in store_mode_line_noprop_char. */
8881 static char *mode_line_noprop_buf;
8883 /* The buffer's end, and a current output position in it. */
8885 static char *mode_line_noprop_buf_end;
8886 static char *mode_line_noprop_ptr;
8888 #define MODE_LINE_NOPROP_LEN(start) \
8889 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8891 static enum {
8892 MODE_LINE_DISPLAY = 0,
8893 MODE_LINE_TITLE,
8894 MODE_LINE_NOPROP,
8895 MODE_LINE_STRING
8896 } mode_line_target;
8898 /* Alist that caches the results of :propertize.
8899 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8900 static Lisp_Object mode_line_proptrans_alist;
8902 /* List of strings making up the mode-line. */
8903 static Lisp_Object mode_line_string_list;
8905 /* Base face property when building propertized mode line string. */
8906 static Lisp_Object mode_line_string_face;
8907 static Lisp_Object mode_line_string_face_prop;
8910 /* Unwind data for mode line strings */
8912 static Lisp_Object Vmode_line_unwind_vector;
8914 static Lisp_Object
8915 format_mode_line_unwind_data (obuf, save_proptrans)
8916 struct buffer *obuf;
8918 Lisp_Object vector;
8920 /* Reduce consing by keeping one vector in
8921 Vwith_echo_area_save_vector. */
8922 vector = Vmode_line_unwind_vector;
8923 Vmode_line_unwind_vector = Qnil;
8925 if (NILP (vector))
8926 vector = Fmake_vector (make_number (7), Qnil);
8928 AREF (vector, 0) = make_number (mode_line_target);
8929 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8930 AREF (vector, 2) = mode_line_string_list;
8931 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8932 AREF (vector, 4) = mode_line_string_face;
8933 AREF (vector, 5) = mode_line_string_face_prop;
8935 if (obuf)
8936 XSETBUFFER (AREF (vector, 6), obuf);
8937 else
8938 AREF (vector, 6) = Qnil;
8940 return vector;
8943 static Lisp_Object
8944 unwind_format_mode_line (vector)
8945 Lisp_Object vector;
8947 mode_line_target = XINT (AREF (vector, 0));
8948 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8949 mode_line_string_list = AREF (vector, 2);
8950 if (! EQ (AREF (vector, 3), Qt))
8951 mode_line_proptrans_alist = AREF (vector, 3);
8952 mode_line_string_face = AREF (vector, 4);
8953 mode_line_string_face_prop = AREF (vector, 5);
8955 if (!NILP (AREF (vector, 6)))
8957 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8958 AREF (vector, 6) = Qnil;
8961 Vmode_line_unwind_vector = vector;
8962 return Qnil;
8966 /* Store a single character C for the frame title in mode_line_noprop_buf.
8967 Re-allocate mode_line_noprop_buf if necessary. */
8969 static void
8970 #ifdef PROTOTYPES
8971 store_mode_line_noprop_char (char c)
8972 #else
8973 store_mode_line_noprop_char (c)
8974 char c;
8975 #endif
8977 /* If output position has reached the end of the allocated buffer,
8978 double the buffer's size. */
8979 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8981 int len = MODE_LINE_NOPROP_LEN (0);
8982 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8983 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8984 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8985 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8988 *mode_line_noprop_ptr++ = c;
8992 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8993 mode_line_noprop_ptr. STR is the string to store. Do not copy
8994 characters that yield more columns than PRECISION; PRECISION <= 0
8995 means copy the whole string. Pad with spaces until FIELD_WIDTH
8996 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8997 pad. Called from display_mode_element when it is used to build a
8998 frame title. */
9000 static int
9001 store_mode_line_noprop (str, field_width, precision)
9002 const unsigned char *str;
9003 int field_width, precision;
9005 int n = 0;
9006 int dummy, nbytes;
9008 /* Copy at most PRECISION chars from STR. */
9009 nbytes = strlen (str);
9010 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9011 while (nbytes--)
9012 store_mode_line_noprop_char (*str++);
9014 /* Fill up with spaces until FIELD_WIDTH reached. */
9015 while (field_width > 0
9016 && n < field_width)
9018 store_mode_line_noprop_char (' ');
9019 ++n;
9022 return n;
9025 /***********************************************************************
9026 Frame Titles
9027 ***********************************************************************/
9029 #ifdef HAVE_WINDOW_SYSTEM
9031 /* Set the title of FRAME, if it has changed. The title format is
9032 Vicon_title_format if FRAME is iconified, otherwise it is
9033 frame_title_format. */
9035 static void
9036 x_consider_frame_title (frame)
9037 Lisp_Object frame;
9039 struct frame *f = XFRAME (frame);
9041 if (FRAME_WINDOW_P (f)
9042 || FRAME_MINIBUF_ONLY_P (f)
9043 || f->explicit_name)
9045 /* Do we have more than one visible frame on this X display? */
9046 Lisp_Object tail;
9047 Lisp_Object fmt;
9048 int title_start;
9049 char *title;
9050 int len;
9051 struct it it;
9052 int count = SPECPDL_INDEX ();
9054 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9056 Lisp_Object other_frame = XCAR (tail);
9057 struct frame *tf = XFRAME (other_frame);
9059 if (tf != f
9060 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9061 && !FRAME_MINIBUF_ONLY_P (tf)
9062 && !EQ (other_frame, tip_frame)
9063 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9064 break;
9067 /* Set global variable indicating that multiple frames exist. */
9068 multiple_frames = CONSP (tail);
9070 /* Switch to the buffer of selected window of the frame. Set up
9071 mode_line_target so that display_mode_element will output into
9072 mode_line_noprop_buf; then display the title. */
9073 record_unwind_protect (unwind_format_mode_line,
9074 format_mode_line_unwind_data (current_buffer, 0));
9076 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9077 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9079 mode_line_target = MODE_LINE_TITLE;
9080 title_start = MODE_LINE_NOPROP_LEN (0);
9081 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9082 NULL, DEFAULT_FACE_ID);
9083 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9084 len = MODE_LINE_NOPROP_LEN (title_start);
9085 title = mode_line_noprop_buf + title_start;
9086 unbind_to (count, Qnil);
9088 /* Set the title only if it's changed. This avoids consing in
9089 the common case where it hasn't. (If it turns out that we've
9090 already wasted too much time by walking through the list with
9091 display_mode_element, then we might need to optimize at a
9092 higher level than this.) */
9093 if (! STRINGP (f->name)
9094 || SBYTES (f->name) != len
9095 || bcmp (title, SDATA (f->name), len) != 0)
9096 x_implicitly_set_name (f, make_string (title, len), Qnil);
9100 #endif /* not HAVE_WINDOW_SYSTEM */
9105 /***********************************************************************
9106 Menu Bars
9107 ***********************************************************************/
9110 /* Prepare for redisplay by updating menu-bar item lists when
9111 appropriate. This can call eval. */
9113 void
9114 prepare_menu_bars ()
9116 int all_windows;
9117 struct gcpro gcpro1, gcpro2;
9118 struct frame *f;
9119 Lisp_Object tooltip_frame;
9121 #ifdef HAVE_WINDOW_SYSTEM
9122 tooltip_frame = tip_frame;
9123 #else
9124 tooltip_frame = Qnil;
9125 #endif
9127 /* Update all frame titles based on their buffer names, etc. We do
9128 this before the menu bars so that the buffer-menu will show the
9129 up-to-date frame titles. */
9130 #ifdef HAVE_WINDOW_SYSTEM
9131 if (windows_or_buffers_changed || update_mode_lines)
9133 Lisp_Object tail, frame;
9135 FOR_EACH_FRAME (tail, frame)
9137 f = XFRAME (frame);
9138 if (!EQ (frame, tooltip_frame)
9139 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9140 x_consider_frame_title (frame);
9143 #endif /* HAVE_WINDOW_SYSTEM */
9145 /* Update the menu bar item lists, if appropriate. This has to be
9146 done before any actual redisplay or generation of display lines. */
9147 all_windows = (update_mode_lines
9148 || buffer_shared > 1
9149 || windows_or_buffers_changed);
9150 if (all_windows)
9152 Lisp_Object tail, frame;
9153 int count = SPECPDL_INDEX ();
9154 /* 1 means that update_menu_bar has run its hooks
9155 so any further calls to update_menu_bar shouldn't do so again. */
9156 int menu_bar_hooks_run = 0;
9158 record_unwind_save_match_data ();
9160 FOR_EACH_FRAME (tail, frame)
9162 f = XFRAME (frame);
9164 /* Ignore tooltip frame. */
9165 if (EQ (frame, tooltip_frame))
9166 continue;
9168 /* If a window on this frame changed size, report that to
9169 the user and clear the size-change flag. */
9170 if (FRAME_WINDOW_SIZES_CHANGED (f))
9172 Lisp_Object functions;
9174 /* Clear flag first in case we get an error below. */
9175 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9176 functions = Vwindow_size_change_functions;
9177 GCPRO2 (tail, functions);
9179 while (CONSP (functions))
9181 call1 (XCAR (functions), frame);
9182 functions = XCDR (functions);
9184 UNGCPRO;
9187 GCPRO1 (tail);
9188 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9189 #ifdef HAVE_WINDOW_SYSTEM
9190 update_tool_bar (f, 0);
9191 #ifdef MAC_OS
9192 mac_update_title_bar (f, 0);
9193 #endif
9194 #endif
9195 UNGCPRO;
9198 unbind_to (count, Qnil);
9200 else
9202 struct frame *sf = SELECTED_FRAME ();
9203 update_menu_bar (sf, 1, 0);
9204 #ifdef HAVE_WINDOW_SYSTEM
9205 update_tool_bar (sf, 1);
9206 #ifdef MAC_OS
9207 mac_update_title_bar (sf, 1);
9208 #endif
9209 #endif
9212 /* Motif needs this. See comment in xmenu.c. Turn it off when
9213 pending_menu_activation is not defined. */
9214 #ifdef USE_X_TOOLKIT
9215 pending_menu_activation = 0;
9216 #endif
9220 /* Update the menu bar item list for frame F. This has to be done
9221 before we start to fill in any display lines, because it can call
9222 eval.
9224 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9226 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9227 already ran the menu bar hooks for this redisplay, so there
9228 is no need to run them again. The return value is the
9229 updated value of this flag, to pass to the next call. */
9231 static int
9232 update_menu_bar (f, save_match_data, hooks_run)
9233 struct frame *f;
9234 int save_match_data;
9235 int hooks_run;
9237 Lisp_Object window;
9238 register struct window *w;
9240 /* If called recursively during a menu update, do nothing. This can
9241 happen when, for instance, an activate-menubar-hook causes a
9242 redisplay. */
9243 if (inhibit_menubar_update)
9244 return hooks_run;
9246 window = FRAME_SELECTED_WINDOW (f);
9247 w = XWINDOW (window);
9249 #if 0 /* The if statement below this if statement used to include the
9250 condition !NILP (w->update_mode_line), rather than using
9251 update_mode_lines directly, and this if statement may have
9252 been added to make that condition work. Now the if
9253 statement below matches its comment, this isn't needed. */
9254 if (update_mode_lines)
9255 w->update_mode_line = Qt;
9256 #endif
9258 if (FRAME_WINDOW_P (f)
9260 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9261 || defined (USE_GTK)
9262 FRAME_EXTERNAL_MENU_BAR (f)
9263 #else
9264 FRAME_MENU_BAR_LINES (f) > 0
9265 #endif
9266 : FRAME_MENU_BAR_LINES (f) > 0)
9268 /* If the user has switched buffers or windows, we need to
9269 recompute to reflect the new bindings. But we'll
9270 recompute when update_mode_lines is set too; that means
9271 that people can use force-mode-line-update to request
9272 that the menu bar be recomputed. The adverse effect on
9273 the rest of the redisplay algorithm is about the same as
9274 windows_or_buffers_changed anyway. */
9275 if (windows_or_buffers_changed
9276 /* This used to test w->update_mode_line, but we believe
9277 there is no need to recompute the menu in that case. */
9278 || update_mode_lines
9279 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9280 < BUF_MODIFF (XBUFFER (w->buffer)))
9281 != !NILP (w->last_had_star))
9282 || ((!NILP (Vtransient_mark_mode)
9283 && !NILP (XBUFFER (w->buffer)->mark_active))
9284 != !NILP (w->region_showing)))
9286 struct buffer *prev = current_buffer;
9287 int count = SPECPDL_INDEX ();
9289 specbind (Qinhibit_menubar_update, Qt);
9291 set_buffer_internal_1 (XBUFFER (w->buffer));
9292 if (save_match_data)
9293 record_unwind_save_match_data ();
9294 if (NILP (Voverriding_local_map_menu_flag))
9296 specbind (Qoverriding_terminal_local_map, Qnil);
9297 specbind (Qoverriding_local_map, Qnil);
9300 if (!hooks_run)
9302 /* Run the Lucid hook. */
9303 safe_run_hooks (Qactivate_menubar_hook);
9305 /* If it has changed current-menubar from previous value,
9306 really recompute the menu-bar from the value. */
9307 if (! NILP (Vlucid_menu_bar_dirty_flag))
9308 call0 (Qrecompute_lucid_menubar);
9310 safe_run_hooks (Qmenu_bar_update_hook);
9312 hooks_run = 1;
9315 XSETFRAME (Vmenu_updating_frame, f);
9316 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9318 /* Redisplay the menu bar in case we changed it. */
9319 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9320 || defined (USE_GTK)
9321 if (FRAME_WINDOW_P (f))
9323 #ifdef MAC_OS
9324 /* All frames on Mac OS share the same menubar. So only
9325 the selected frame should be allowed to set it. */
9326 if (f == SELECTED_FRAME ())
9327 #endif
9328 set_frame_menubar (f, 0, 0);
9330 else
9331 /* On a terminal screen, the menu bar is an ordinary screen
9332 line, and this makes it get updated. */
9333 w->update_mode_line = Qt;
9334 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9335 /* In the non-toolkit version, the menu bar is an ordinary screen
9336 line, and this makes it get updated. */
9337 w->update_mode_line = Qt;
9338 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9340 unbind_to (count, Qnil);
9341 set_buffer_internal_1 (prev);
9345 return hooks_run;
9350 /***********************************************************************
9351 Output Cursor
9352 ***********************************************************************/
9354 #ifdef HAVE_WINDOW_SYSTEM
9356 /* EXPORT:
9357 Nominal cursor position -- where to draw output.
9358 HPOS and VPOS are window relative glyph matrix coordinates.
9359 X and Y are window relative pixel coordinates. */
9361 struct cursor_pos output_cursor;
9364 /* EXPORT:
9365 Set the global variable output_cursor to CURSOR. All cursor
9366 positions are relative to updated_window. */
9368 void
9369 set_output_cursor (cursor)
9370 struct cursor_pos *cursor;
9372 output_cursor.hpos = cursor->hpos;
9373 output_cursor.vpos = cursor->vpos;
9374 output_cursor.x = cursor->x;
9375 output_cursor.y = cursor->y;
9379 /* EXPORT for RIF:
9380 Set a nominal cursor position.
9382 HPOS and VPOS are column/row positions in a window glyph matrix. X
9383 and Y are window text area relative pixel positions.
9385 If this is done during an update, updated_window will contain the
9386 window that is being updated and the position is the future output
9387 cursor position for that window. If updated_window is null, use
9388 selected_window and display the cursor at the given position. */
9390 void
9391 x_cursor_to (vpos, hpos, y, x)
9392 int vpos, hpos, y, x;
9394 struct window *w;
9396 /* If updated_window is not set, work on selected_window. */
9397 if (updated_window)
9398 w = updated_window;
9399 else
9400 w = XWINDOW (selected_window);
9402 /* Set the output cursor. */
9403 output_cursor.hpos = hpos;
9404 output_cursor.vpos = vpos;
9405 output_cursor.x = x;
9406 output_cursor.y = y;
9408 /* If not called as part of an update, really display the cursor.
9409 This will also set the cursor position of W. */
9410 if (updated_window == NULL)
9412 BLOCK_INPUT;
9413 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9414 if (rif->flush_display_optional)
9415 rif->flush_display_optional (SELECTED_FRAME ());
9416 UNBLOCK_INPUT;
9420 #endif /* HAVE_WINDOW_SYSTEM */
9423 /***********************************************************************
9424 Tool-bars
9425 ***********************************************************************/
9427 #ifdef HAVE_WINDOW_SYSTEM
9429 /* Where the mouse was last time we reported a mouse event. */
9431 FRAME_PTR last_mouse_frame;
9433 /* Tool-bar item index of the item on which a mouse button was pressed
9434 or -1. */
9436 int last_tool_bar_item;
9439 /* Update the tool-bar item list for frame F. This has to be done
9440 before we start to fill in any display lines. Called from
9441 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9442 and restore it here. */
9444 static void
9445 update_tool_bar (f, save_match_data)
9446 struct frame *f;
9447 int save_match_data;
9449 #ifdef USE_GTK
9450 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9451 #else
9452 int do_update = WINDOWP (f->tool_bar_window)
9453 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9454 #endif
9456 if (do_update)
9458 Lisp_Object window;
9459 struct window *w;
9461 window = FRAME_SELECTED_WINDOW (f);
9462 w = XWINDOW (window);
9464 /* If the user has switched buffers or windows, we need to
9465 recompute to reflect the new bindings. But we'll
9466 recompute when update_mode_lines is set too; that means
9467 that people can use force-mode-line-update to request
9468 that the menu bar be recomputed. The adverse effect on
9469 the rest of the redisplay algorithm is about the same as
9470 windows_or_buffers_changed anyway. */
9471 if (windows_or_buffers_changed
9472 || !NILP (w->update_mode_line)
9473 || update_mode_lines
9474 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9475 < BUF_MODIFF (XBUFFER (w->buffer)))
9476 != !NILP (w->last_had_star))
9477 || ((!NILP (Vtransient_mark_mode)
9478 && !NILP (XBUFFER (w->buffer)->mark_active))
9479 != !NILP (w->region_showing)))
9481 struct buffer *prev = current_buffer;
9482 int count = SPECPDL_INDEX ();
9483 Lisp_Object new_tool_bar;
9484 int new_n_tool_bar;
9485 struct gcpro gcpro1;
9487 /* Set current_buffer to the buffer of the selected
9488 window of the frame, so that we get the right local
9489 keymaps. */
9490 set_buffer_internal_1 (XBUFFER (w->buffer));
9492 /* Save match data, if we must. */
9493 if (save_match_data)
9494 record_unwind_save_match_data ();
9496 /* Make sure that we don't accidentally use bogus keymaps. */
9497 if (NILP (Voverriding_local_map_menu_flag))
9499 specbind (Qoverriding_terminal_local_map, Qnil);
9500 specbind (Qoverriding_local_map, Qnil);
9503 GCPRO1 (new_tool_bar);
9505 /* Build desired tool-bar items from keymaps. */
9506 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9507 &new_n_tool_bar);
9509 /* Redisplay the tool-bar if we changed it. */
9510 if (new_n_tool_bar != f->n_tool_bar_items
9511 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9513 /* Redisplay that happens asynchronously due to an expose event
9514 may access f->tool_bar_items. Make sure we update both
9515 variables within BLOCK_INPUT so no such event interrupts. */
9516 BLOCK_INPUT;
9517 f->tool_bar_items = new_tool_bar;
9518 f->n_tool_bar_items = new_n_tool_bar;
9519 w->update_mode_line = Qt;
9520 UNBLOCK_INPUT;
9523 UNGCPRO;
9525 unbind_to (count, Qnil);
9526 set_buffer_internal_1 (prev);
9532 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9533 F's desired tool-bar contents. F->tool_bar_items must have
9534 been set up previously by calling prepare_menu_bars. */
9536 static void
9537 build_desired_tool_bar_string (f)
9538 struct frame *f;
9540 int i, size, size_needed;
9541 struct gcpro gcpro1, gcpro2, gcpro3;
9542 Lisp_Object image, plist, props;
9544 image = plist = props = Qnil;
9545 GCPRO3 (image, plist, props);
9547 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9548 Otherwise, make a new string. */
9550 /* The size of the string we might be able to reuse. */
9551 size = (STRINGP (f->desired_tool_bar_string)
9552 ? SCHARS (f->desired_tool_bar_string)
9553 : 0);
9555 /* We need one space in the string for each image. */
9556 size_needed = f->n_tool_bar_items;
9558 /* Reuse f->desired_tool_bar_string, if possible. */
9559 if (size < size_needed || NILP (f->desired_tool_bar_string))
9560 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9561 make_number (' '));
9562 else
9564 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9565 Fremove_text_properties (make_number (0), make_number (size),
9566 props, f->desired_tool_bar_string);
9569 /* Put a `display' property on the string for the images to display,
9570 put a `menu_item' property on tool-bar items with a value that
9571 is the index of the item in F's tool-bar item vector. */
9572 for (i = 0; i < f->n_tool_bar_items; ++i)
9574 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9576 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9577 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9578 int hmargin, vmargin, relief, idx, end;
9579 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9581 /* If image is a vector, choose the image according to the
9582 button state. */
9583 image = PROP (TOOL_BAR_ITEM_IMAGES);
9584 if (VECTORP (image))
9586 if (enabled_p)
9587 idx = (selected_p
9588 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9589 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9590 else
9591 idx = (selected_p
9592 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9593 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9595 xassert (ASIZE (image) >= idx);
9596 image = AREF (image, idx);
9598 else
9599 idx = -1;
9601 /* Ignore invalid image specifications. */
9602 if (!valid_image_p (image))
9603 continue;
9605 /* Display the tool-bar button pressed, or depressed. */
9606 plist = Fcopy_sequence (XCDR (image));
9608 /* Compute margin and relief to draw. */
9609 relief = (tool_bar_button_relief >= 0
9610 ? tool_bar_button_relief
9611 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9612 hmargin = vmargin = relief;
9614 if (INTEGERP (Vtool_bar_button_margin)
9615 && XINT (Vtool_bar_button_margin) > 0)
9617 hmargin += XFASTINT (Vtool_bar_button_margin);
9618 vmargin += XFASTINT (Vtool_bar_button_margin);
9620 else if (CONSP (Vtool_bar_button_margin))
9622 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9623 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9624 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9626 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9627 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9628 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9631 if (auto_raise_tool_bar_buttons_p)
9633 /* Add a `:relief' property to the image spec if the item is
9634 selected. */
9635 if (selected_p)
9637 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9638 hmargin -= relief;
9639 vmargin -= relief;
9642 else
9644 /* If image is selected, display it pressed, i.e. with a
9645 negative relief. If it's not selected, display it with a
9646 raised relief. */
9647 plist = Fplist_put (plist, QCrelief,
9648 (selected_p
9649 ? make_number (-relief)
9650 : make_number (relief)));
9651 hmargin -= relief;
9652 vmargin -= relief;
9655 /* Put a margin around the image. */
9656 if (hmargin || vmargin)
9658 if (hmargin == vmargin)
9659 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9660 else
9661 plist = Fplist_put (plist, QCmargin,
9662 Fcons (make_number (hmargin),
9663 make_number (vmargin)));
9666 /* If button is not enabled, and we don't have special images
9667 for the disabled state, make the image appear disabled by
9668 applying an appropriate algorithm to it. */
9669 if (!enabled_p && idx < 0)
9670 plist = Fplist_put (plist, QCconversion, Qdisabled);
9672 /* Put a `display' text property on the string for the image to
9673 display. Put a `menu-item' property on the string that gives
9674 the start of this item's properties in the tool-bar items
9675 vector. */
9676 image = Fcons (Qimage, plist);
9677 props = list4 (Qdisplay, image,
9678 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9680 /* Let the last image hide all remaining spaces in the tool bar
9681 string. The string can be longer than needed when we reuse a
9682 previous string. */
9683 if (i + 1 == f->n_tool_bar_items)
9684 end = SCHARS (f->desired_tool_bar_string);
9685 else
9686 end = i + 1;
9687 Fadd_text_properties (make_number (i), make_number (end),
9688 props, f->desired_tool_bar_string);
9689 #undef PROP
9692 UNGCPRO;
9696 /* Display one line of the tool-bar of frame IT->f.
9698 HEIGHT specifies the desired height of the tool-bar line.
9699 If the actual height of the glyph row is less than HEIGHT, the
9700 row's height is increased to HEIGHT, and the icons are centered
9701 vertically in the new height.
9703 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9704 count a final empty row in case the tool-bar width exactly matches
9705 the window width.
9708 static void
9709 display_tool_bar_line (it, height)
9710 struct it *it;
9711 int height;
9713 struct glyph_row *row = it->glyph_row;
9714 int max_x = it->last_visible_x;
9715 struct glyph *last;
9717 prepare_desired_row (row);
9718 row->y = it->current_y;
9720 /* Note that this isn't made use of if the face hasn't a box,
9721 so there's no need to check the face here. */
9722 it->start_of_box_run_p = 1;
9724 while (it->current_x < max_x)
9726 int x, n_glyphs_before, i, nglyphs;
9727 struct it it_before;
9729 /* Get the next display element. */
9730 if (!get_next_display_element (it))
9732 /* Don't count empty row if we are counting needed tool-bar lines. */
9733 if (height < 0 && !it->hpos)
9734 return;
9735 break;
9738 /* Produce glyphs. */
9739 n_glyphs_before = row->used[TEXT_AREA];
9740 it_before = *it;
9742 PRODUCE_GLYPHS (it);
9744 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9745 i = 0;
9746 x = it_before.current_x;
9747 while (i < nglyphs)
9749 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9751 if (x + glyph->pixel_width > max_x)
9753 /* Glyph doesn't fit on line. Backtrack. */
9754 row->used[TEXT_AREA] = n_glyphs_before;
9755 *it = it_before;
9756 /* If this is the only glyph on this line, it will never fit on the
9757 toolbar, so skip it. But ensure there is at least one glyph,
9758 so we don't accidentally disable the tool-bar. */
9759 if (n_glyphs_before == 0
9760 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9761 break;
9762 goto out;
9765 ++it->hpos;
9766 x += glyph->pixel_width;
9767 ++i;
9770 /* Stop at line ends. */
9771 if (ITERATOR_AT_END_OF_LINE_P (it))
9772 break;
9774 set_iterator_to_next (it, 1);
9777 out:;
9779 row->displays_text_p = row->used[TEXT_AREA] != 0;
9781 /* Use default face for the border below the tool bar.
9783 FIXME: When auto-resize-tool-bars is grow-only, there is
9784 no additional border below the possibly empty tool-bar lines.
9785 So to make the extra empty lines look "normal", we have to
9786 use the tool-bar face for the border too. */
9787 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9788 it->face_id = DEFAULT_FACE_ID;
9790 extend_face_to_end_of_line (it);
9791 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9792 last->right_box_line_p = 1;
9793 if (last == row->glyphs[TEXT_AREA])
9794 last->left_box_line_p = 1;
9796 /* Make line the desired height and center it vertically. */
9797 if ((height -= it->max_ascent + it->max_descent) > 0)
9799 /* Don't add more than one line height. */
9800 height %= FRAME_LINE_HEIGHT (it->f);
9801 it->max_ascent += height / 2;
9802 it->max_descent += (height + 1) / 2;
9805 compute_line_metrics (it);
9807 /* If line is empty, make it occupy the rest of the tool-bar. */
9808 if (!row->displays_text_p)
9810 row->height = row->phys_height = it->last_visible_y - row->y;
9811 row->visible_height = row->height;
9812 row->ascent = row->phys_ascent = 0;
9813 row->extra_line_spacing = 0;
9816 row->full_width_p = 1;
9817 row->continued_p = 0;
9818 row->truncated_on_left_p = 0;
9819 row->truncated_on_right_p = 0;
9821 it->current_x = it->hpos = 0;
9822 it->current_y += row->height;
9823 ++it->vpos;
9824 ++it->glyph_row;
9828 /* Max tool-bar height. */
9830 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9831 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9833 /* Value is the number of screen lines needed to make all tool-bar
9834 items of frame F visible. The number of actual rows needed is
9835 returned in *N_ROWS if non-NULL. */
9837 static int
9838 tool_bar_lines_needed (f, n_rows)
9839 struct frame *f;
9840 int *n_rows;
9842 struct window *w = XWINDOW (f->tool_bar_window);
9843 struct it it;
9844 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9845 the desired matrix, so use (unused) mode-line row as temporary row to
9846 avoid destroying the first tool-bar row. */
9847 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9849 /* Initialize an iterator for iteration over
9850 F->desired_tool_bar_string in the tool-bar window of frame F. */
9851 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9852 it.first_visible_x = 0;
9853 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9854 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9856 while (!ITERATOR_AT_END_P (&it))
9858 clear_glyph_row (temp_row);
9859 it.glyph_row = temp_row;
9860 display_tool_bar_line (&it, -1);
9862 clear_glyph_row (temp_row);
9864 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9865 if (n_rows)
9866 *n_rows = it.vpos > 0 ? it.vpos : -1;
9868 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9872 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9873 0, 1, 0,
9874 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9875 (frame)
9876 Lisp_Object frame;
9878 struct frame *f;
9879 struct window *w;
9880 int nlines = 0;
9882 if (NILP (frame))
9883 frame = selected_frame;
9884 else
9885 CHECK_FRAME (frame);
9886 f = XFRAME (frame);
9888 if (WINDOWP (f->tool_bar_window)
9889 || (w = XWINDOW (f->tool_bar_window),
9890 WINDOW_TOTAL_LINES (w) > 0))
9892 update_tool_bar (f, 1);
9893 if (f->n_tool_bar_items)
9895 build_desired_tool_bar_string (f);
9896 nlines = tool_bar_lines_needed (f, NULL);
9900 return make_number (nlines);
9904 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9905 height should be changed. */
9907 static int
9908 redisplay_tool_bar (f)
9909 struct frame *f;
9911 struct window *w;
9912 struct it it;
9913 struct glyph_row *row;
9915 #ifdef USE_GTK
9916 if (FRAME_EXTERNAL_TOOL_BAR (f))
9917 update_frame_tool_bar (f);
9918 return 0;
9919 #endif
9921 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9922 do anything. This means you must start with tool-bar-lines
9923 non-zero to get the auto-sizing effect. Or in other words, you
9924 can turn off tool-bars by specifying tool-bar-lines zero. */
9925 if (!WINDOWP (f->tool_bar_window)
9926 || (w = XWINDOW (f->tool_bar_window),
9927 WINDOW_TOTAL_LINES (w) == 0))
9928 return 0;
9930 /* Set up an iterator for the tool-bar window. */
9931 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9932 it.first_visible_x = 0;
9933 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9934 row = it.glyph_row;
9936 /* Build a string that represents the contents of the tool-bar. */
9937 build_desired_tool_bar_string (f);
9938 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9940 if (f->n_tool_bar_rows == 0)
9942 int nlines;
9944 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9945 nlines != WINDOW_TOTAL_LINES (w)))
9947 extern Lisp_Object Qtool_bar_lines;
9948 Lisp_Object frame;
9949 int old_height = WINDOW_TOTAL_LINES (w);
9951 XSETFRAME (frame, f);
9952 Fmodify_frame_parameters (frame,
9953 Fcons (Fcons (Qtool_bar_lines,
9954 make_number (nlines)),
9955 Qnil));
9956 if (WINDOW_TOTAL_LINES (w) != old_height)
9958 clear_glyph_matrix (w->desired_matrix);
9959 fonts_changed_p = 1;
9960 return 1;
9965 /* Display as many lines as needed to display all tool-bar items. */
9967 if (f->n_tool_bar_rows > 0)
9969 int border, rows, height, extra;
9971 if (INTEGERP (Vtool_bar_border))
9972 border = XINT (Vtool_bar_border);
9973 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9974 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9975 else if (EQ (Vtool_bar_border, Qborder_width))
9976 border = f->border_width;
9977 else
9978 border = 0;
9979 if (border < 0)
9980 border = 0;
9982 rows = f->n_tool_bar_rows;
9983 height = max (1, (it.last_visible_y - border) / rows);
9984 extra = it.last_visible_y - border - height * rows;
9986 while (it.current_y < it.last_visible_y)
9988 int h = 0;
9989 if (extra > 0 && rows-- > 0)
9991 h = (extra + rows - 1) / rows;
9992 extra -= h;
9994 display_tool_bar_line (&it, height + h);
9997 else
9999 while (it.current_y < it.last_visible_y)
10000 display_tool_bar_line (&it, 0);
10003 /* It doesn't make much sense to try scrolling in the tool-bar
10004 window, so don't do it. */
10005 w->desired_matrix->no_scrolling_p = 1;
10006 w->must_be_updated_p = 1;
10008 if (!NILP (Vauto_resize_tool_bars))
10010 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10011 int change_height_p = 0;
10013 /* If we couldn't display everything, change the tool-bar's
10014 height if there is room for more. */
10015 if (IT_STRING_CHARPOS (it) < it.end_charpos
10016 && it.current_y < max_tool_bar_height)
10017 change_height_p = 1;
10019 row = it.glyph_row - 1;
10021 /* If there are blank lines at the end, except for a partially
10022 visible blank line at the end that is smaller than
10023 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10024 if (!row->displays_text_p
10025 && row->height >= FRAME_LINE_HEIGHT (f))
10026 change_height_p = 1;
10028 /* If row displays tool-bar items, but is partially visible,
10029 change the tool-bar's height. */
10030 if (row->displays_text_p
10031 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10032 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10033 change_height_p = 1;
10035 /* Resize windows as needed by changing the `tool-bar-lines'
10036 frame parameter. */
10037 if (change_height_p)
10039 extern Lisp_Object Qtool_bar_lines;
10040 Lisp_Object frame;
10041 int old_height = WINDOW_TOTAL_LINES (w);
10042 int nrows;
10043 int nlines = tool_bar_lines_needed (f, &nrows);
10045 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10046 && !f->minimize_tool_bar_window_p)
10047 ? (nlines > old_height)
10048 : (nlines != old_height));
10049 f->minimize_tool_bar_window_p = 0;
10051 if (change_height_p)
10053 XSETFRAME (frame, f);
10054 Fmodify_frame_parameters (frame,
10055 Fcons (Fcons (Qtool_bar_lines,
10056 make_number (nlines)),
10057 Qnil));
10058 if (WINDOW_TOTAL_LINES (w) != old_height)
10060 clear_glyph_matrix (w->desired_matrix);
10061 f->n_tool_bar_rows = nrows;
10062 fonts_changed_p = 1;
10063 return 1;
10069 f->minimize_tool_bar_window_p = 0;
10070 return 0;
10074 /* Get information about the tool-bar item which is displayed in GLYPH
10075 on frame F. Return in *PROP_IDX the index where tool-bar item
10076 properties start in F->tool_bar_items. Value is zero if
10077 GLYPH doesn't display a tool-bar item. */
10079 static int
10080 tool_bar_item_info (f, glyph, prop_idx)
10081 struct frame *f;
10082 struct glyph *glyph;
10083 int *prop_idx;
10085 Lisp_Object prop;
10086 int success_p;
10087 int charpos;
10089 /* This function can be called asynchronously, which means we must
10090 exclude any possibility that Fget_text_property signals an
10091 error. */
10092 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10093 charpos = max (0, charpos);
10095 /* Get the text property `menu-item' at pos. The value of that
10096 property is the start index of this item's properties in
10097 F->tool_bar_items. */
10098 prop = Fget_text_property (make_number (charpos),
10099 Qmenu_item, f->current_tool_bar_string);
10100 if (INTEGERP (prop))
10102 *prop_idx = XINT (prop);
10103 success_p = 1;
10105 else
10106 success_p = 0;
10108 return success_p;
10112 /* Get information about the tool-bar item at position X/Y on frame F.
10113 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10114 the current matrix of the tool-bar window of F, or NULL if not
10115 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10116 item in F->tool_bar_items. Value is
10118 -1 if X/Y is not on a tool-bar item
10119 0 if X/Y is on the same item that was highlighted before.
10120 1 otherwise. */
10122 static int
10123 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10124 struct frame *f;
10125 int x, y;
10126 struct glyph **glyph;
10127 int *hpos, *vpos, *prop_idx;
10129 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10130 struct window *w = XWINDOW (f->tool_bar_window);
10131 int area;
10133 /* Find the glyph under X/Y. */
10134 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10135 if (*glyph == NULL)
10136 return -1;
10138 /* Get the start of this tool-bar item's properties in
10139 f->tool_bar_items. */
10140 if (!tool_bar_item_info (f, *glyph, prop_idx))
10141 return -1;
10143 /* Is mouse on the highlighted item? */
10144 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10145 && *vpos >= dpyinfo->mouse_face_beg_row
10146 && *vpos <= dpyinfo->mouse_face_end_row
10147 && (*vpos > dpyinfo->mouse_face_beg_row
10148 || *hpos >= dpyinfo->mouse_face_beg_col)
10149 && (*vpos < dpyinfo->mouse_face_end_row
10150 || *hpos < dpyinfo->mouse_face_end_col
10151 || dpyinfo->mouse_face_past_end))
10152 return 0;
10154 return 1;
10158 /* EXPORT:
10159 Handle mouse button event on the tool-bar of frame F, at
10160 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10161 0 for button release. MODIFIERS is event modifiers for button
10162 release. */
10164 void
10165 handle_tool_bar_click (f, x, y, down_p, modifiers)
10166 struct frame *f;
10167 int x, y, down_p;
10168 unsigned int modifiers;
10170 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10171 struct window *w = XWINDOW (f->tool_bar_window);
10172 int hpos, vpos, prop_idx;
10173 struct glyph *glyph;
10174 Lisp_Object enabled_p;
10176 /* If not on the highlighted tool-bar item, return. */
10177 frame_to_window_pixel_xy (w, &x, &y);
10178 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10179 return;
10181 /* If item is disabled, do nothing. */
10182 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10183 if (NILP (enabled_p))
10184 return;
10186 if (down_p)
10188 /* Show item in pressed state. */
10189 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10190 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10191 last_tool_bar_item = prop_idx;
10193 else
10195 Lisp_Object key, frame;
10196 struct input_event event;
10197 EVENT_INIT (event);
10199 /* Show item in released state. */
10200 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10201 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10203 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10205 XSETFRAME (frame, f);
10206 event.kind = TOOL_BAR_EVENT;
10207 event.frame_or_window = frame;
10208 event.arg = frame;
10209 kbd_buffer_store_event (&event);
10211 event.kind = TOOL_BAR_EVENT;
10212 event.frame_or_window = frame;
10213 event.arg = key;
10214 event.modifiers = modifiers;
10215 kbd_buffer_store_event (&event);
10216 last_tool_bar_item = -1;
10221 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10222 tool-bar window-relative coordinates X/Y. Called from
10223 note_mouse_highlight. */
10225 static void
10226 note_tool_bar_highlight (f, x, y)
10227 struct frame *f;
10228 int x, y;
10230 Lisp_Object window = f->tool_bar_window;
10231 struct window *w = XWINDOW (window);
10232 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10233 int hpos, vpos;
10234 struct glyph *glyph;
10235 struct glyph_row *row;
10236 int i;
10237 Lisp_Object enabled_p;
10238 int prop_idx;
10239 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10240 int mouse_down_p, rc;
10242 /* Function note_mouse_highlight is called with negative x(y
10243 values when mouse moves outside of the frame. */
10244 if (x <= 0 || y <= 0)
10246 clear_mouse_face (dpyinfo);
10247 return;
10250 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10251 if (rc < 0)
10253 /* Not on tool-bar item. */
10254 clear_mouse_face (dpyinfo);
10255 return;
10257 else if (rc == 0)
10258 /* On same tool-bar item as before. */
10259 goto set_help_echo;
10261 clear_mouse_face (dpyinfo);
10263 /* Mouse is down, but on different tool-bar item? */
10264 mouse_down_p = (dpyinfo->grabbed
10265 && f == last_mouse_frame
10266 && FRAME_LIVE_P (f));
10267 if (mouse_down_p
10268 && last_tool_bar_item != prop_idx)
10269 return;
10271 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10272 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10274 /* If tool-bar item is not enabled, don't highlight it. */
10275 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10276 if (!NILP (enabled_p))
10278 /* Compute the x-position of the glyph. In front and past the
10279 image is a space. We include this in the highlighted area. */
10280 row = MATRIX_ROW (w->current_matrix, vpos);
10281 for (i = x = 0; i < hpos; ++i)
10282 x += row->glyphs[TEXT_AREA][i].pixel_width;
10284 /* Record this as the current active region. */
10285 dpyinfo->mouse_face_beg_col = hpos;
10286 dpyinfo->mouse_face_beg_row = vpos;
10287 dpyinfo->mouse_face_beg_x = x;
10288 dpyinfo->mouse_face_beg_y = row->y;
10289 dpyinfo->mouse_face_past_end = 0;
10291 dpyinfo->mouse_face_end_col = hpos + 1;
10292 dpyinfo->mouse_face_end_row = vpos;
10293 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10294 dpyinfo->mouse_face_end_y = row->y;
10295 dpyinfo->mouse_face_window = window;
10296 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10298 /* Display it as active. */
10299 show_mouse_face (dpyinfo, draw);
10300 dpyinfo->mouse_face_image_state = draw;
10303 set_help_echo:
10305 /* Set help_echo_string to a help string to display for this tool-bar item.
10306 XTread_socket does the rest. */
10307 help_echo_object = help_echo_window = Qnil;
10308 help_echo_pos = -1;
10309 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10310 if (NILP (help_echo_string))
10311 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10314 #endif /* HAVE_WINDOW_SYSTEM */
10318 /************************************************************************
10319 Horizontal scrolling
10320 ************************************************************************/
10322 static int hscroll_window_tree P_ ((Lisp_Object));
10323 static int hscroll_windows P_ ((Lisp_Object));
10325 /* For all leaf windows in the window tree rooted at WINDOW, set their
10326 hscroll value so that PT is (i) visible in the window, and (ii) so
10327 that it is not within a certain margin at the window's left and
10328 right border. Value is non-zero if any window's hscroll has been
10329 changed. */
10331 static int
10332 hscroll_window_tree (window)
10333 Lisp_Object window;
10335 int hscrolled_p = 0;
10336 int hscroll_relative_p = FLOATP (Vhscroll_step);
10337 int hscroll_step_abs = 0;
10338 double hscroll_step_rel = 0;
10340 if (hscroll_relative_p)
10342 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10343 if (hscroll_step_rel < 0)
10345 hscroll_relative_p = 0;
10346 hscroll_step_abs = 0;
10349 else if (INTEGERP (Vhscroll_step))
10351 hscroll_step_abs = XINT (Vhscroll_step);
10352 if (hscroll_step_abs < 0)
10353 hscroll_step_abs = 0;
10355 else
10356 hscroll_step_abs = 0;
10358 while (WINDOWP (window))
10360 struct window *w = XWINDOW (window);
10362 if (WINDOWP (w->hchild))
10363 hscrolled_p |= hscroll_window_tree (w->hchild);
10364 else if (WINDOWP (w->vchild))
10365 hscrolled_p |= hscroll_window_tree (w->vchild);
10366 else if (w->cursor.vpos >= 0)
10368 int h_margin;
10369 int text_area_width;
10370 struct glyph_row *current_cursor_row
10371 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10372 struct glyph_row *desired_cursor_row
10373 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10374 struct glyph_row *cursor_row
10375 = (desired_cursor_row->enabled_p
10376 ? desired_cursor_row
10377 : current_cursor_row);
10379 text_area_width = window_box_width (w, TEXT_AREA);
10381 /* Scroll when cursor is inside this scroll margin. */
10382 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10384 if ((XFASTINT (w->hscroll)
10385 && w->cursor.x <= h_margin)
10386 || (cursor_row->enabled_p
10387 && cursor_row->truncated_on_right_p
10388 && (w->cursor.x >= text_area_width - h_margin)))
10390 struct it it;
10391 int hscroll;
10392 struct buffer *saved_current_buffer;
10393 int pt;
10394 int wanted_x;
10396 /* Find point in a display of infinite width. */
10397 saved_current_buffer = current_buffer;
10398 current_buffer = XBUFFER (w->buffer);
10400 if (w == XWINDOW (selected_window))
10401 pt = BUF_PT (current_buffer);
10402 else
10404 pt = marker_position (w->pointm);
10405 pt = max (BEGV, pt);
10406 pt = min (ZV, pt);
10409 /* Move iterator to pt starting at cursor_row->start in
10410 a line with infinite width. */
10411 init_to_row_start (&it, w, cursor_row);
10412 it.last_visible_x = INFINITY;
10413 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10414 current_buffer = saved_current_buffer;
10416 /* Position cursor in window. */
10417 if (!hscroll_relative_p && hscroll_step_abs == 0)
10418 hscroll = max (0, (it.current_x
10419 - (ITERATOR_AT_END_OF_LINE_P (&it)
10420 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10421 : (text_area_width / 2))))
10422 / FRAME_COLUMN_WIDTH (it.f);
10423 else if (w->cursor.x >= text_area_width - h_margin)
10425 if (hscroll_relative_p)
10426 wanted_x = text_area_width * (1 - hscroll_step_rel)
10427 - h_margin;
10428 else
10429 wanted_x = text_area_width
10430 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10431 - h_margin;
10432 hscroll
10433 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10435 else
10437 if (hscroll_relative_p)
10438 wanted_x = text_area_width * hscroll_step_rel
10439 + h_margin;
10440 else
10441 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10442 + h_margin;
10443 hscroll
10444 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10446 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10448 /* Don't call Fset_window_hscroll if value hasn't
10449 changed because it will prevent redisplay
10450 optimizations. */
10451 if (XFASTINT (w->hscroll) != hscroll)
10453 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10454 w->hscroll = make_number (hscroll);
10455 hscrolled_p = 1;
10460 window = w->next;
10463 /* Value is non-zero if hscroll of any leaf window has been changed. */
10464 return hscrolled_p;
10468 /* Set hscroll so that cursor is visible and not inside horizontal
10469 scroll margins for all windows in the tree rooted at WINDOW. See
10470 also hscroll_window_tree above. Value is non-zero if any window's
10471 hscroll has been changed. If it has, desired matrices on the frame
10472 of WINDOW are cleared. */
10474 static int
10475 hscroll_windows (window)
10476 Lisp_Object window;
10478 int hscrolled_p;
10480 if (automatic_hscrolling_p)
10482 hscrolled_p = hscroll_window_tree (window);
10483 if (hscrolled_p)
10484 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10486 else
10487 hscrolled_p = 0;
10488 return hscrolled_p;
10493 /************************************************************************
10494 Redisplay
10495 ************************************************************************/
10497 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10498 to a non-zero value. This is sometimes handy to have in a debugger
10499 session. */
10501 #if GLYPH_DEBUG
10503 /* First and last unchanged row for try_window_id. */
10505 int debug_first_unchanged_at_end_vpos;
10506 int debug_last_unchanged_at_beg_vpos;
10508 /* Delta vpos and y. */
10510 int debug_dvpos, debug_dy;
10512 /* Delta in characters and bytes for try_window_id. */
10514 int debug_delta, debug_delta_bytes;
10516 /* Values of window_end_pos and window_end_vpos at the end of
10517 try_window_id. */
10519 EMACS_INT debug_end_pos, debug_end_vpos;
10521 /* Append a string to W->desired_matrix->method. FMT is a printf
10522 format string. A1...A9 are a supplement for a variable-length
10523 argument list. If trace_redisplay_p is non-zero also printf the
10524 resulting string to stderr. */
10526 static void
10527 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10528 struct window *w;
10529 char *fmt;
10530 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10532 char buffer[512];
10533 char *method = w->desired_matrix->method;
10534 int len = strlen (method);
10535 int size = sizeof w->desired_matrix->method;
10536 int remaining = size - len - 1;
10538 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10539 if (len && remaining)
10541 method[len] = '|';
10542 --remaining, ++len;
10545 strncpy (method + len, buffer, remaining);
10547 if (trace_redisplay_p)
10548 fprintf (stderr, "%p (%s): %s\n",
10550 ((BUFFERP (w->buffer)
10551 && STRINGP (XBUFFER (w->buffer)->name))
10552 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10553 : "no buffer"),
10554 buffer);
10557 #endif /* GLYPH_DEBUG */
10560 /* Value is non-zero if all changes in window W, which displays
10561 current_buffer, are in the text between START and END. START is a
10562 buffer position, END is given as a distance from Z. Used in
10563 redisplay_internal for display optimization. */
10565 static INLINE int
10566 text_outside_line_unchanged_p (w, start, end)
10567 struct window *w;
10568 int start, end;
10570 int unchanged_p = 1;
10572 /* If text or overlays have changed, see where. */
10573 if (XFASTINT (w->last_modified) < MODIFF
10574 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10576 /* Gap in the line? */
10577 if (GPT < start || Z - GPT < end)
10578 unchanged_p = 0;
10580 /* Changes start in front of the line, or end after it? */
10581 if (unchanged_p
10582 && (BEG_UNCHANGED < start - 1
10583 || END_UNCHANGED < end))
10584 unchanged_p = 0;
10586 /* If selective display, can't optimize if changes start at the
10587 beginning of the line. */
10588 if (unchanged_p
10589 && INTEGERP (current_buffer->selective_display)
10590 && XINT (current_buffer->selective_display) > 0
10591 && (BEG_UNCHANGED < start || GPT <= start))
10592 unchanged_p = 0;
10594 /* If there are overlays at the start or end of the line, these
10595 may have overlay strings with newlines in them. A change at
10596 START, for instance, may actually concern the display of such
10597 overlay strings as well, and they are displayed on different
10598 lines. So, quickly rule out this case. (For the future, it
10599 might be desirable to implement something more telling than
10600 just BEG/END_UNCHANGED.) */
10601 if (unchanged_p)
10603 if (BEG + BEG_UNCHANGED == start
10604 && overlay_touches_p (start))
10605 unchanged_p = 0;
10606 if (END_UNCHANGED == end
10607 && overlay_touches_p (Z - end))
10608 unchanged_p = 0;
10612 return unchanged_p;
10616 /* Do a frame update, taking possible shortcuts into account. This is
10617 the main external entry point for redisplay.
10619 If the last redisplay displayed an echo area message and that message
10620 is no longer requested, we clear the echo area or bring back the
10621 mini-buffer if that is in use. */
10623 void
10624 redisplay ()
10626 redisplay_internal (0);
10630 static Lisp_Object
10631 overlay_arrow_string_or_property (var)
10632 Lisp_Object var;
10634 Lisp_Object val;
10636 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10637 return val;
10639 return Voverlay_arrow_string;
10642 /* Return 1 if there are any overlay-arrows in current_buffer. */
10643 static int
10644 overlay_arrow_in_current_buffer_p ()
10646 Lisp_Object vlist;
10648 for (vlist = Voverlay_arrow_variable_list;
10649 CONSP (vlist);
10650 vlist = XCDR (vlist))
10652 Lisp_Object var = XCAR (vlist);
10653 Lisp_Object val;
10655 if (!SYMBOLP (var))
10656 continue;
10657 val = find_symbol_value (var);
10658 if (MARKERP (val)
10659 && current_buffer == XMARKER (val)->buffer)
10660 return 1;
10662 return 0;
10666 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10667 has changed. */
10669 static int
10670 overlay_arrows_changed_p ()
10672 Lisp_Object vlist;
10674 for (vlist = Voverlay_arrow_variable_list;
10675 CONSP (vlist);
10676 vlist = XCDR (vlist))
10678 Lisp_Object var = XCAR (vlist);
10679 Lisp_Object val, pstr;
10681 if (!SYMBOLP (var))
10682 continue;
10683 val = find_symbol_value (var);
10684 if (!MARKERP (val))
10685 continue;
10686 if (! EQ (COERCE_MARKER (val),
10687 Fget (var, Qlast_arrow_position))
10688 || ! (pstr = overlay_arrow_string_or_property (var),
10689 EQ (pstr, Fget (var, Qlast_arrow_string))))
10690 return 1;
10692 return 0;
10695 /* Mark overlay arrows to be updated on next redisplay. */
10697 static void
10698 update_overlay_arrows (up_to_date)
10699 int up_to_date;
10701 Lisp_Object vlist;
10703 for (vlist = Voverlay_arrow_variable_list;
10704 CONSP (vlist);
10705 vlist = XCDR (vlist))
10707 Lisp_Object var = XCAR (vlist);
10709 if (!SYMBOLP (var))
10710 continue;
10712 if (up_to_date > 0)
10714 Lisp_Object val = find_symbol_value (var);
10715 Fput (var, Qlast_arrow_position,
10716 COERCE_MARKER (val));
10717 Fput (var, Qlast_arrow_string,
10718 overlay_arrow_string_or_property (var));
10720 else if (up_to_date < 0
10721 || !NILP (Fget (var, Qlast_arrow_position)))
10723 Fput (var, Qlast_arrow_position, Qt);
10724 Fput (var, Qlast_arrow_string, Qt);
10730 /* Return overlay arrow string to display at row.
10731 Return integer (bitmap number) for arrow bitmap in left fringe.
10732 Return nil if no overlay arrow. */
10734 static Lisp_Object
10735 overlay_arrow_at_row (it, row)
10736 struct it *it;
10737 struct glyph_row *row;
10739 Lisp_Object vlist;
10741 for (vlist = Voverlay_arrow_variable_list;
10742 CONSP (vlist);
10743 vlist = XCDR (vlist))
10745 Lisp_Object var = XCAR (vlist);
10746 Lisp_Object val;
10748 if (!SYMBOLP (var))
10749 continue;
10751 val = find_symbol_value (var);
10753 if (MARKERP (val)
10754 && current_buffer == XMARKER (val)->buffer
10755 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10757 if (FRAME_WINDOW_P (it->f)
10758 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10760 #ifdef HAVE_WINDOW_SYSTEM
10761 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10763 int fringe_bitmap;
10764 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10765 return make_number (fringe_bitmap);
10767 #endif
10768 return make_number (-1); /* Use default arrow bitmap */
10770 return overlay_arrow_string_or_property (var);
10774 return Qnil;
10777 /* Return 1 if point moved out of or into a composition. Otherwise
10778 return 0. PREV_BUF and PREV_PT are the last point buffer and
10779 position. BUF and PT are the current point buffer and position. */
10782 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10783 struct buffer *prev_buf, *buf;
10784 int prev_pt, pt;
10786 int start, end;
10787 Lisp_Object prop;
10788 Lisp_Object buffer;
10790 XSETBUFFER (buffer, buf);
10791 /* Check a composition at the last point if point moved within the
10792 same buffer. */
10793 if (prev_buf == buf)
10795 if (prev_pt == pt)
10796 /* Point didn't move. */
10797 return 0;
10799 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10800 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10801 && COMPOSITION_VALID_P (start, end, prop)
10802 && start < prev_pt && end > prev_pt)
10803 /* The last point was within the composition. Return 1 iff
10804 point moved out of the composition. */
10805 return (pt <= start || pt >= end);
10808 /* Check a composition at the current point. */
10809 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10810 && find_composition (pt, -1, &start, &end, &prop, buffer)
10811 && COMPOSITION_VALID_P (start, end, prop)
10812 && start < pt && end > pt);
10816 /* Reconsider the setting of B->clip_changed which is displayed
10817 in window W. */
10819 static INLINE void
10820 reconsider_clip_changes (w, b)
10821 struct window *w;
10822 struct buffer *b;
10824 if (b->clip_changed
10825 && !NILP (w->window_end_valid)
10826 && w->current_matrix->buffer == b
10827 && w->current_matrix->zv == BUF_ZV (b)
10828 && w->current_matrix->begv == BUF_BEGV (b))
10829 b->clip_changed = 0;
10831 /* If display wasn't paused, and W is not a tool bar window, see if
10832 point has been moved into or out of a composition. In that case,
10833 we set b->clip_changed to 1 to force updating the screen. If
10834 b->clip_changed has already been set to 1, we can skip this
10835 check. */
10836 if (!b->clip_changed
10837 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10839 int pt;
10841 if (w == XWINDOW (selected_window))
10842 pt = BUF_PT (current_buffer);
10843 else
10844 pt = marker_position (w->pointm);
10846 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10847 || pt != XINT (w->last_point))
10848 && check_point_in_composition (w->current_matrix->buffer,
10849 XINT (w->last_point),
10850 XBUFFER (w->buffer), pt))
10851 b->clip_changed = 1;
10856 /* Select FRAME to forward the values of frame-local variables into C
10857 variables so that the redisplay routines can access those values
10858 directly. */
10860 static void
10861 select_frame_for_redisplay (frame)
10862 Lisp_Object frame;
10864 Lisp_Object tail, sym, val;
10865 Lisp_Object old = selected_frame;
10867 selected_frame = frame;
10869 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10870 if (CONSP (XCAR (tail))
10871 && (sym = XCAR (XCAR (tail)),
10872 SYMBOLP (sym))
10873 && (sym = indirect_variable (sym),
10874 val = SYMBOL_VALUE (sym),
10875 (BUFFER_LOCAL_VALUEP (val)
10876 || SOME_BUFFER_LOCAL_VALUEP (val)))
10877 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10878 /* Use find_symbol_value rather than Fsymbol_value
10879 to avoid an error if it is void. */
10880 find_symbol_value (sym);
10882 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10883 if (CONSP (XCAR (tail))
10884 && (sym = XCAR (XCAR (tail)),
10885 SYMBOLP (sym))
10886 && (sym = indirect_variable (sym),
10887 val = SYMBOL_VALUE (sym),
10888 (BUFFER_LOCAL_VALUEP (val)
10889 || SOME_BUFFER_LOCAL_VALUEP (val)))
10890 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10891 find_symbol_value (sym);
10895 #define STOP_POLLING \
10896 do { if (! polling_stopped_here) stop_polling (); \
10897 polling_stopped_here = 1; } while (0)
10899 #define RESUME_POLLING \
10900 do { if (polling_stopped_here) start_polling (); \
10901 polling_stopped_here = 0; } while (0)
10904 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10905 response to any user action; therefore, we should preserve the echo
10906 area. (Actually, our caller does that job.) Perhaps in the future
10907 avoid recentering windows if it is not necessary; currently that
10908 causes some problems. */
10910 static void
10911 redisplay_internal (preserve_echo_area)
10912 int preserve_echo_area;
10914 struct window *w = XWINDOW (selected_window);
10915 struct frame *f;
10916 int pause;
10917 int must_finish = 0;
10918 struct text_pos tlbufpos, tlendpos;
10919 int number_of_visible_frames;
10920 int count, count1;
10921 struct frame *sf;
10922 int polling_stopped_here = 0;
10924 /* Non-zero means redisplay has to consider all windows on all
10925 frames. Zero means, only selected_window is considered. */
10926 int consider_all_windows_p;
10928 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10930 /* No redisplay if running in batch mode or frame is not yet fully
10931 initialized, or redisplay is explicitly turned off by setting
10932 Vinhibit_redisplay. */
10933 if (noninteractive
10934 || !NILP (Vinhibit_redisplay))
10935 return;
10937 /* Don't examine these until after testing Vinhibit_redisplay.
10938 When Emacs is shutting down, perhaps because its connection to
10939 X has dropped, we should not look at them at all. */
10940 f = XFRAME (w->frame);
10941 sf = SELECTED_FRAME ();
10943 if (!f->glyphs_initialized_p)
10944 return;
10946 /* The flag redisplay_performed_directly_p is set by
10947 direct_output_for_insert when it already did the whole screen
10948 update necessary. */
10949 if (redisplay_performed_directly_p)
10951 redisplay_performed_directly_p = 0;
10952 if (!hscroll_windows (selected_window))
10953 return;
10956 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10957 if (popup_activated ())
10958 return;
10959 #endif
10961 /* I don't think this happens but let's be paranoid. */
10962 if (redisplaying_p)
10963 return;
10965 /* Record a function that resets redisplaying_p to its old value
10966 when we leave this function. */
10967 count = SPECPDL_INDEX ();
10968 record_unwind_protect (unwind_redisplay,
10969 Fcons (make_number (redisplaying_p), selected_frame));
10970 ++redisplaying_p;
10971 specbind (Qinhibit_free_realized_faces, Qnil);
10974 Lisp_Object tail, frame;
10976 FOR_EACH_FRAME (tail, frame)
10978 struct frame *f = XFRAME (frame);
10979 f->already_hscrolled_p = 0;
10983 retry:
10984 pause = 0;
10985 reconsider_clip_changes (w, current_buffer);
10986 last_escape_glyph_frame = NULL;
10987 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10989 /* If new fonts have been loaded that make a glyph matrix adjustment
10990 necessary, do it. */
10991 if (fonts_changed_p)
10993 adjust_glyphs (NULL);
10994 ++windows_or_buffers_changed;
10995 fonts_changed_p = 0;
10998 /* If face_change_count is non-zero, init_iterator will free all
10999 realized faces, which includes the faces referenced from current
11000 matrices. So, we can't reuse current matrices in this case. */
11001 if (face_change_count)
11002 ++windows_or_buffers_changed;
11004 if (! FRAME_WINDOW_P (sf)
11005 && previous_terminal_frame != sf)
11007 /* Since frames on an ASCII terminal share the same display
11008 area, displaying a different frame means redisplay the whole
11009 thing. */
11010 windows_or_buffers_changed++;
11011 SET_FRAME_GARBAGED (sf);
11012 XSETFRAME (Vterminal_frame, sf);
11014 previous_terminal_frame = sf;
11016 /* Set the visible flags for all frames. Do this before checking
11017 for resized or garbaged frames; they want to know if their frames
11018 are visible. See the comment in frame.h for
11019 FRAME_SAMPLE_VISIBILITY. */
11021 Lisp_Object tail, frame;
11023 number_of_visible_frames = 0;
11025 FOR_EACH_FRAME (tail, frame)
11027 struct frame *f = XFRAME (frame);
11029 FRAME_SAMPLE_VISIBILITY (f);
11030 if (FRAME_VISIBLE_P (f))
11031 ++number_of_visible_frames;
11032 clear_desired_matrices (f);
11036 /* Notice any pending interrupt request to change frame size. */
11037 do_pending_window_change (1);
11039 /* Clear frames marked as garbaged. */
11040 if (frame_garbaged)
11041 clear_garbaged_frames ();
11043 /* Build menubar and tool-bar items. */
11044 if (NILP (Vmemory_full))
11045 prepare_menu_bars ();
11047 if (windows_or_buffers_changed)
11048 update_mode_lines++;
11050 /* Detect case that we need to write or remove a star in the mode line. */
11051 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11053 w->update_mode_line = Qt;
11054 if (buffer_shared > 1)
11055 update_mode_lines++;
11058 /* Avoid invocation of point motion hooks by `current_column' below. */
11059 count1 = SPECPDL_INDEX ();
11060 specbind (Qinhibit_point_motion_hooks, Qt);
11062 /* If %c is in the mode line, update it if needed. */
11063 if (!NILP (w->column_number_displayed)
11064 /* This alternative quickly identifies a common case
11065 where no change is needed. */
11066 && !(PT == XFASTINT (w->last_point)
11067 && XFASTINT (w->last_modified) >= MODIFF
11068 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11069 && (XFASTINT (w->column_number_displayed)
11070 != (int) current_column ())) /* iftc */
11071 w->update_mode_line = Qt;
11073 unbind_to (count1, Qnil);
11075 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11077 /* The variable buffer_shared is set in redisplay_window and
11078 indicates that we redisplay a buffer in different windows. See
11079 there. */
11080 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11081 || cursor_type_changed);
11083 /* If specs for an arrow have changed, do thorough redisplay
11084 to ensure we remove any arrow that should no longer exist. */
11085 if (overlay_arrows_changed_p ())
11086 consider_all_windows_p = windows_or_buffers_changed = 1;
11088 /* Normally the message* functions will have already displayed and
11089 updated the echo area, but the frame may have been trashed, or
11090 the update may have been preempted, so display the echo area
11091 again here. Checking message_cleared_p captures the case that
11092 the echo area should be cleared. */
11093 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11094 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11095 || (message_cleared_p
11096 && minibuf_level == 0
11097 /* If the mini-window is currently selected, this means the
11098 echo-area doesn't show through. */
11099 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11101 int window_height_changed_p = echo_area_display (0);
11102 must_finish = 1;
11104 /* If we don't display the current message, don't clear the
11105 message_cleared_p flag, because, if we did, we wouldn't clear
11106 the echo area in the next redisplay which doesn't preserve
11107 the echo area. */
11108 if (!display_last_displayed_message_p)
11109 message_cleared_p = 0;
11111 if (fonts_changed_p)
11112 goto retry;
11113 else if (window_height_changed_p)
11115 consider_all_windows_p = 1;
11116 ++update_mode_lines;
11117 ++windows_or_buffers_changed;
11119 /* If window configuration was changed, frames may have been
11120 marked garbaged. Clear them or we will experience
11121 surprises wrt scrolling. */
11122 if (frame_garbaged)
11123 clear_garbaged_frames ();
11126 else if (EQ (selected_window, minibuf_window)
11127 && (current_buffer->clip_changed
11128 || XFASTINT (w->last_modified) < MODIFF
11129 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11130 && resize_mini_window (w, 0))
11132 /* Resized active mini-window to fit the size of what it is
11133 showing if its contents might have changed. */
11134 must_finish = 1;
11135 consider_all_windows_p = 1;
11136 ++windows_or_buffers_changed;
11137 ++update_mode_lines;
11139 /* If window configuration was changed, frames may have been
11140 marked garbaged. Clear them or we will experience
11141 surprises wrt scrolling. */
11142 if (frame_garbaged)
11143 clear_garbaged_frames ();
11147 /* If showing the region, and mark has changed, we must redisplay
11148 the whole window. The assignment to this_line_start_pos prevents
11149 the optimization directly below this if-statement. */
11150 if (((!NILP (Vtransient_mark_mode)
11151 && !NILP (XBUFFER (w->buffer)->mark_active))
11152 != !NILP (w->region_showing))
11153 || (!NILP (w->region_showing)
11154 && !EQ (w->region_showing,
11155 Fmarker_position (XBUFFER (w->buffer)->mark))))
11156 CHARPOS (this_line_start_pos) = 0;
11158 /* Optimize the case that only the line containing the cursor in the
11159 selected window has changed. Variables starting with this_ are
11160 set in display_line and record information about the line
11161 containing the cursor. */
11162 tlbufpos = this_line_start_pos;
11163 tlendpos = this_line_end_pos;
11164 if (!consider_all_windows_p
11165 && CHARPOS (tlbufpos) > 0
11166 && NILP (w->update_mode_line)
11167 && !current_buffer->clip_changed
11168 && !current_buffer->prevent_redisplay_optimizations_p
11169 && FRAME_VISIBLE_P (XFRAME (w->frame))
11170 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11171 /* Make sure recorded data applies to current buffer, etc. */
11172 && this_line_buffer == current_buffer
11173 && current_buffer == XBUFFER (w->buffer)
11174 && NILP (w->force_start)
11175 && NILP (w->optional_new_start)
11176 /* Point must be on the line that we have info recorded about. */
11177 && PT >= CHARPOS (tlbufpos)
11178 && PT <= Z - CHARPOS (tlendpos)
11179 /* All text outside that line, including its final newline,
11180 must be unchanged */
11181 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11182 CHARPOS (tlendpos)))
11184 if (CHARPOS (tlbufpos) > BEGV
11185 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11186 && (CHARPOS (tlbufpos) == ZV
11187 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11188 /* Former continuation line has disappeared by becoming empty */
11189 goto cancel;
11190 else if (XFASTINT (w->last_modified) < MODIFF
11191 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11192 || MINI_WINDOW_P (w))
11194 /* We have to handle the case of continuation around a
11195 wide-column character (See the comment in indent.c around
11196 line 885).
11198 For instance, in the following case:
11200 -------- Insert --------
11201 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11202 J_I_ ==> J_I_ `^^' are cursors.
11203 ^^ ^^
11204 -------- --------
11206 As we have to redraw the line above, we should goto cancel. */
11208 struct it it;
11209 int line_height_before = this_line_pixel_height;
11211 /* Note that start_display will handle the case that the
11212 line starting at tlbufpos is a continuation lines. */
11213 start_display (&it, w, tlbufpos);
11215 /* Implementation note: It this still necessary? */
11216 if (it.current_x != this_line_start_x)
11217 goto cancel;
11219 TRACE ((stderr, "trying display optimization 1\n"));
11220 w->cursor.vpos = -1;
11221 overlay_arrow_seen = 0;
11222 it.vpos = this_line_vpos;
11223 it.current_y = this_line_y;
11224 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11225 display_line (&it);
11227 /* If line contains point, is not continued,
11228 and ends at same distance from eob as before, we win */
11229 if (w->cursor.vpos >= 0
11230 /* Line is not continued, otherwise this_line_start_pos
11231 would have been set to 0 in display_line. */
11232 && CHARPOS (this_line_start_pos)
11233 /* Line ends as before. */
11234 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11235 /* Line has same height as before. Otherwise other lines
11236 would have to be shifted up or down. */
11237 && this_line_pixel_height == line_height_before)
11239 /* If this is not the window's last line, we must adjust
11240 the charstarts of the lines below. */
11241 if (it.current_y < it.last_visible_y)
11243 struct glyph_row *row
11244 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11245 int delta, delta_bytes;
11247 if (Z - CHARPOS (tlendpos) == ZV)
11249 /* This line ends at end of (accessible part of)
11250 buffer. There is no newline to count. */
11251 delta = (Z
11252 - CHARPOS (tlendpos)
11253 - MATRIX_ROW_START_CHARPOS (row));
11254 delta_bytes = (Z_BYTE
11255 - BYTEPOS (tlendpos)
11256 - MATRIX_ROW_START_BYTEPOS (row));
11258 else
11260 /* This line ends in a newline. Must take
11261 account of the newline and the rest of the
11262 text that follows. */
11263 delta = (Z
11264 - CHARPOS (tlendpos)
11265 - MATRIX_ROW_START_CHARPOS (row));
11266 delta_bytes = (Z_BYTE
11267 - BYTEPOS (tlendpos)
11268 - MATRIX_ROW_START_BYTEPOS (row));
11271 increment_matrix_positions (w->current_matrix,
11272 this_line_vpos + 1,
11273 w->current_matrix->nrows,
11274 delta, delta_bytes);
11277 /* If this row displays text now but previously didn't,
11278 or vice versa, w->window_end_vpos may have to be
11279 adjusted. */
11280 if ((it.glyph_row - 1)->displays_text_p)
11282 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11283 XSETINT (w->window_end_vpos, this_line_vpos);
11285 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11286 && this_line_vpos > 0)
11287 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11288 w->window_end_valid = Qnil;
11290 /* Update hint: No need to try to scroll in update_window. */
11291 w->desired_matrix->no_scrolling_p = 1;
11293 #if GLYPH_DEBUG
11294 *w->desired_matrix->method = 0;
11295 debug_method_add (w, "optimization 1");
11296 #endif
11297 #ifdef HAVE_WINDOW_SYSTEM
11298 update_window_fringes (w, 0);
11299 #endif
11300 goto update;
11302 else
11303 goto cancel;
11305 else if (/* Cursor position hasn't changed. */
11306 PT == XFASTINT (w->last_point)
11307 /* Make sure the cursor was last displayed
11308 in this window. Otherwise we have to reposition it. */
11309 && 0 <= w->cursor.vpos
11310 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11312 if (!must_finish)
11314 do_pending_window_change (1);
11316 /* We used to always goto end_of_redisplay here, but this
11317 isn't enough if we have a blinking cursor. */
11318 if (w->cursor_off_p == w->last_cursor_off_p)
11319 goto end_of_redisplay;
11321 goto update;
11323 /* If highlighting the region, or if the cursor is in the echo area,
11324 then we can't just move the cursor. */
11325 else if (! (!NILP (Vtransient_mark_mode)
11326 && !NILP (current_buffer->mark_active))
11327 && (EQ (selected_window, current_buffer->last_selected_window)
11328 || highlight_nonselected_windows)
11329 && NILP (w->region_showing)
11330 && NILP (Vshow_trailing_whitespace)
11331 && !cursor_in_echo_area)
11333 struct it it;
11334 struct glyph_row *row;
11336 /* Skip from tlbufpos to PT and see where it is. Note that
11337 PT may be in invisible text. If so, we will end at the
11338 next visible position. */
11339 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11340 NULL, DEFAULT_FACE_ID);
11341 it.current_x = this_line_start_x;
11342 it.current_y = this_line_y;
11343 it.vpos = this_line_vpos;
11345 /* The call to move_it_to stops in front of PT, but
11346 moves over before-strings. */
11347 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11349 if (it.vpos == this_line_vpos
11350 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11351 row->enabled_p))
11353 xassert (this_line_vpos == it.vpos);
11354 xassert (this_line_y == it.current_y);
11355 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11356 #if GLYPH_DEBUG
11357 *w->desired_matrix->method = 0;
11358 debug_method_add (w, "optimization 3");
11359 #endif
11360 goto update;
11362 else
11363 goto cancel;
11366 cancel:
11367 /* Text changed drastically or point moved off of line. */
11368 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11371 CHARPOS (this_line_start_pos) = 0;
11372 consider_all_windows_p |= buffer_shared > 1;
11373 ++clear_face_cache_count;
11374 #ifdef HAVE_WINDOW_SYSTEM
11375 ++clear_image_cache_count;
11376 #endif
11378 /* Build desired matrices, and update the display. If
11379 consider_all_windows_p is non-zero, do it for all windows on all
11380 frames. Otherwise do it for selected_window, only. */
11382 if (consider_all_windows_p)
11384 Lisp_Object tail, frame;
11386 FOR_EACH_FRAME (tail, frame)
11387 XFRAME (frame)->updated_p = 0;
11389 /* Recompute # windows showing selected buffer. This will be
11390 incremented each time such a window is displayed. */
11391 buffer_shared = 0;
11393 FOR_EACH_FRAME (tail, frame)
11395 struct frame *f = XFRAME (frame);
11397 if (FRAME_WINDOW_P (f) || f == sf)
11399 if (! EQ (frame, selected_frame))
11400 /* Select the frame, for the sake of frame-local
11401 variables. */
11402 select_frame_for_redisplay (frame);
11404 /* Mark all the scroll bars to be removed; we'll redeem
11405 the ones we want when we redisplay their windows. */
11406 if (condemn_scroll_bars_hook)
11407 condemn_scroll_bars_hook (f);
11409 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11410 redisplay_windows (FRAME_ROOT_WINDOW (f));
11412 /* Any scroll bars which redisplay_windows should have
11413 nuked should now go away. */
11414 if (judge_scroll_bars_hook)
11415 judge_scroll_bars_hook (f);
11417 /* If fonts changed, display again. */
11418 /* ??? rms: I suspect it is a mistake to jump all the way
11419 back to retry here. It should just retry this frame. */
11420 if (fonts_changed_p)
11421 goto retry;
11423 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11425 /* See if we have to hscroll. */
11426 if (!f->already_hscrolled_p)
11428 f->already_hscrolled_p = 1;
11429 if (hscroll_windows (f->root_window))
11430 goto retry;
11433 /* Prevent various kinds of signals during display
11434 update. stdio is not robust about handling
11435 signals, which can cause an apparent I/O
11436 error. */
11437 if (interrupt_input)
11438 unrequest_sigio ();
11439 STOP_POLLING;
11441 /* Update the display. */
11442 set_window_update_flags (XWINDOW (f->root_window), 1);
11443 pause |= update_frame (f, 0, 0);
11444 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11445 if (pause)
11446 break;
11447 #endif
11449 f->updated_p = 1;
11454 if (!pause)
11456 /* Do the mark_window_display_accurate after all windows have
11457 been redisplayed because this call resets flags in buffers
11458 which are needed for proper redisplay. */
11459 FOR_EACH_FRAME (tail, frame)
11461 struct frame *f = XFRAME (frame);
11462 if (f->updated_p)
11464 mark_window_display_accurate (f->root_window, 1);
11465 if (frame_up_to_date_hook)
11466 frame_up_to_date_hook (f);
11471 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11473 Lisp_Object mini_window;
11474 struct frame *mini_frame;
11476 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11477 /* Use list_of_error, not Qerror, so that
11478 we catch only errors and don't run the debugger. */
11479 internal_condition_case_1 (redisplay_window_1, selected_window,
11480 list_of_error,
11481 redisplay_window_error);
11483 /* Compare desired and current matrices, perform output. */
11485 update:
11486 /* If fonts changed, display again. */
11487 if (fonts_changed_p)
11488 goto retry;
11490 /* Prevent various kinds of signals during display update.
11491 stdio is not robust about handling signals,
11492 which can cause an apparent I/O error. */
11493 if (interrupt_input)
11494 unrequest_sigio ();
11495 STOP_POLLING;
11497 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11499 if (hscroll_windows (selected_window))
11500 goto retry;
11502 XWINDOW (selected_window)->must_be_updated_p = 1;
11503 pause = update_frame (sf, 0, 0);
11506 /* We may have called echo_area_display at the top of this
11507 function. If the echo area is on another frame, that may
11508 have put text on a frame other than the selected one, so the
11509 above call to update_frame would not have caught it. Catch
11510 it here. */
11511 mini_window = FRAME_MINIBUF_WINDOW (sf);
11512 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11514 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11516 XWINDOW (mini_window)->must_be_updated_p = 1;
11517 pause |= update_frame (mini_frame, 0, 0);
11518 if (!pause && hscroll_windows (mini_window))
11519 goto retry;
11523 /* If display was paused because of pending input, make sure we do a
11524 thorough update the next time. */
11525 if (pause)
11527 /* Prevent the optimization at the beginning of
11528 redisplay_internal that tries a single-line update of the
11529 line containing the cursor in the selected window. */
11530 CHARPOS (this_line_start_pos) = 0;
11532 /* Let the overlay arrow be updated the next time. */
11533 update_overlay_arrows (0);
11535 /* If we pause after scrolling, some rows in the current
11536 matrices of some windows are not valid. */
11537 if (!WINDOW_FULL_WIDTH_P (w)
11538 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11539 update_mode_lines = 1;
11541 else
11543 if (!consider_all_windows_p)
11545 /* This has already been done above if
11546 consider_all_windows_p is set. */
11547 mark_window_display_accurate_1 (w, 1);
11549 /* Say overlay arrows are up to date. */
11550 update_overlay_arrows (1);
11552 if (frame_up_to_date_hook != 0)
11553 frame_up_to_date_hook (sf);
11556 update_mode_lines = 0;
11557 windows_or_buffers_changed = 0;
11558 cursor_type_changed = 0;
11561 /* Start SIGIO interrupts coming again. Having them off during the
11562 code above makes it less likely one will discard output, but not
11563 impossible, since there might be stuff in the system buffer here.
11564 But it is much hairier to try to do anything about that. */
11565 if (interrupt_input)
11566 request_sigio ();
11567 RESUME_POLLING;
11569 /* If a frame has become visible which was not before, redisplay
11570 again, so that we display it. Expose events for such a frame
11571 (which it gets when becoming visible) don't call the parts of
11572 redisplay constructing glyphs, so simply exposing a frame won't
11573 display anything in this case. So, we have to display these
11574 frames here explicitly. */
11575 if (!pause)
11577 Lisp_Object tail, frame;
11578 int new_count = 0;
11580 FOR_EACH_FRAME (tail, frame)
11582 int this_is_visible = 0;
11584 if (XFRAME (frame)->visible)
11585 this_is_visible = 1;
11586 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11587 if (XFRAME (frame)->visible)
11588 this_is_visible = 1;
11590 if (this_is_visible)
11591 new_count++;
11594 if (new_count != number_of_visible_frames)
11595 windows_or_buffers_changed++;
11598 /* Change frame size now if a change is pending. */
11599 do_pending_window_change (1);
11601 /* If we just did a pending size change, or have additional
11602 visible frames, redisplay again. */
11603 if (windows_or_buffers_changed && !pause)
11604 goto retry;
11606 /* Clear the face cache eventually. */
11607 if (consider_all_windows_p)
11609 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11611 clear_face_cache (0);
11612 clear_face_cache_count = 0;
11614 #ifdef HAVE_WINDOW_SYSTEM
11615 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11617 Lisp_Object tail, frame;
11618 FOR_EACH_FRAME (tail, frame)
11620 struct frame *f = XFRAME (frame);
11621 if (FRAME_WINDOW_P (f))
11622 clear_image_cache (f, 0);
11624 clear_image_cache_count = 0;
11626 #endif /* HAVE_WINDOW_SYSTEM */
11629 end_of_redisplay:
11630 unbind_to (count, Qnil);
11631 RESUME_POLLING;
11635 /* Redisplay, but leave alone any recent echo area message unless
11636 another message has been requested in its place.
11638 This is useful in situations where you need to redisplay but no
11639 user action has occurred, making it inappropriate for the message
11640 area to be cleared. See tracking_off and
11641 wait_reading_process_output for examples of these situations.
11643 FROM_WHERE is an integer saying from where this function was
11644 called. This is useful for debugging. */
11646 void
11647 redisplay_preserve_echo_area (from_where)
11648 int from_where;
11650 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11652 if (!NILP (echo_area_buffer[1]))
11654 /* We have a previously displayed message, but no current
11655 message. Redisplay the previous message. */
11656 display_last_displayed_message_p = 1;
11657 redisplay_internal (1);
11658 display_last_displayed_message_p = 0;
11660 else
11661 redisplay_internal (1);
11663 if (rif != NULL && rif->flush_display_optional)
11664 rif->flush_display_optional (NULL);
11668 /* Function registered with record_unwind_protect in
11669 redisplay_internal. Reset redisplaying_p to the value it had
11670 before redisplay_internal was called, and clear
11671 prevent_freeing_realized_faces_p. It also selects the previously
11672 selected frame. */
11674 static Lisp_Object
11675 unwind_redisplay (val)
11676 Lisp_Object val;
11678 Lisp_Object old_redisplaying_p, old_frame;
11680 old_redisplaying_p = XCAR (val);
11681 redisplaying_p = XFASTINT (old_redisplaying_p);
11682 old_frame = XCDR (val);
11683 if (! EQ (old_frame, selected_frame))
11684 select_frame_for_redisplay (old_frame);
11685 return Qnil;
11689 /* Mark the display of window W as accurate or inaccurate. If
11690 ACCURATE_P is non-zero mark display of W as accurate. If
11691 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11692 redisplay_internal is called. */
11694 static void
11695 mark_window_display_accurate_1 (w, accurate_p)
11696 struct window *w;
11697 int accurate_p;
11699 if (BUFFERP (w->buffer))
11701 struct buffer *b = XBUFFER (w->buffer);
11703 w->last_modified
11704 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11705 w->last_overlay_modified
11706 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11707 w->last_had_star
11708 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11710 if (accurate_p)
11712 b->clip_changed = 0;
11713 b->prevent_redisplay_optimizations_p = 0;
11715 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11716 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11717 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11718 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11720 w->current_matrix->buffer = b;
11721 w->current_matrix->begv = BUF_BEGV (b);
11722 w->current_matrix->zv = BUF_ZV (b);
11724 w->last_cursor = w->cursor;
11725 w->last_cursor_off_p = w->cursor_off_p;
11727 if (w == XWINDOW (selected_window))
11728 w->last_point = make_number (BUF_PT (b));
11729 else
11730 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11734 if (accurate_p)
11736 w->window_end_valid = w->buffer;
11737 #if 0 /* This is incorrect with variable-height lines. */
11738 xassert (XINT (w->window_end_vpos)
11739 < (WINDOW_TOTAL_LINES (w)
11740 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11741 #endif
11742 w->update_mode_line = Qnil;
11747 /* Mark the display of windows in the window tree rooted at WINDOW as
11748 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11749 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11750 be redisplayed the next time redisplay_internal is called. */
11752 void
11753 mark_window_display_accurate (window, accurate_p)
11754 Lisp_Object window;
11755 int accurate_p;
11757 struct window *w;
11759 for (; !NILP (window); window = w->next)
11761 w = XWINDOW (window);
11762 mark_window_display_accurate_1 (w, accurate_p);
11764 if (!NILP (w->vchild))
11765 mark_window_display_accurate (w->vchild, accurate_p);
11766 if (!NILP (w->hchild))
11767 mark_window_display_accurate (w->hchild, accurate_p);
11770 if (accurate_p)
11772 update_overlay_arrows (1);
11774 else
11776 /* Force a thorough redisplay the next time by setting
11777 last_arrow_position and last_arrow_string to t, which is
11778 unequal to any useful value of Voverlay_arrow_... */
11779 update_overlay_arrows (-1);
11784 /* Return value in display table DP (Lisp_Char_Table *) for character
11785 C. Since a display table doesn't have any parent, we don't have to
11786 follow parent. Do not call this function directly but use the
11787 macro DISP_CHAR_VECTOR. */
11789 Lisp_Object
11790 disp_char_vector (dp, c)
11791 struct Lisp_Char_Table *dp;
11792 int c;
11794 int code[4], i;
11795 Lisp_Object val;
11797 if (SINGLE_BYTE_CHAR_P (c))
11798 return (dp->contents[c]);
11800 SPLIT_CHAR (c, code[0], code[1], code[2]);
11801 if (code[1] < 32)
11802 code[1] = -1;
11803 else if (code[2] < 32)
11804 code[2] = -1;
11806 /* Here, the possible range of code[0] (== charset ID) is
11807 128..max_charset. Since the top level char table contains data
11808 for multibyte characters after 256th element, we must increment
11809 code[0] by 128 to get a correct index. */
11810 code[0] += 128;
11811 code[3] = -1; /* anchor */
11813 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11815 val = dp->contents[code[i]];
11816 if (!SUB_CHAR_TABLE_P (val))
11817 return (NILP (val) ? dp->defalt : val);
11820 /* Here, val is a sub char table. We return the default value of
11821 it. */
11822 return (dp->defalt);
11827 /***********************************************************************
11828 Window Redisplay
11829 ***********************************************************************/
11831 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11833 static void
11834 redisplay_windows (window)
11835 Lisp_Object window;
11837 while (!NILP (window))
11839 struct window *w = XWINDOW (window);
11841 if (!NILP (w->hchild))
11842 redisplay_windows (w->hchild);
11843 else if (!NILP (w->vchild))
11844 redisplay_windows (w->vchild);
11845 else
11847 displayed_buffer = XBUFFER (w->buffer);
11848 /* Use list_of_error, not Qerror, so that
11849 we catch only errors and don't run the debugger. */
11850 internal_condition_case_1 (redisplay_window_0, window,
11851 list_of_error,
11852 redisplay_window_error);
11855 window = w->next;
11859 static Lisp_Object
11860 redisplay_window_error ()
11862 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11863 return Qnil;
11866 static Lisp_Object
11867 redisplay_window_0 (window)
11868 Lisp_Object window;
11870 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11871 redisplay_window (window, 0);
11872 return Qnil;
11875 static Lisp_Object
11876 redisplay_window_1 (window)
11877 Lisp_Object window;
11879 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11880 redisplay_window (window, 1);
11881 return Qnil;
11885 /* Increment GLYPH until it reaches END or CONDITION fails while
11886 adding (GLYPH)->pixel_width to X. */
11888 #define SKIP_GLYPHS(glyph, end, x, condition) \
11889 do \
11891 (x) += (glyph)->pixel_width; \
11892 ++(glyph); \
11894 while ((glyph) < (end) && (condition))
11897 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11898 DELTA is the number of bytes by which positions recorded in ROW
11899 differ from current buffer positions.
11901 Return 0 if cursor is not on this row. 1 otherwise. */
11904 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11905 struct window *w;
11906 struct glyph_row *row;
11907 struct glyph_matrix *matrix;
11908 int delta, delta_bytes, dy, dvpos;
11910 struct glyph *glyph = row->glyphs[TEXT_AREA];
11911 struct glyph *end = glyph + row->used[TEXT_AREA];
11912 struct glyph *cursor = NULL;
11913 /* The first glyph that starts a sequence of glyphs from string. */
11914 struct glyph *string_start;
11915 /* The X coordinate of string_start. */
11916 int string_start_x;
11917 /* The last known character position. */
11918 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11919 /* The last known character position before string_start. */
11920 int string_before_pos;
11921 int x = row->x;
11922 int cursor_x = x;
11923 int cursor_from_overlay_pos = 0;
11924 int pt_old = PT - delta;
11926 /* Skip over glyphs not having an object at the start of the row.
11927 These are special glyphs like truncation marks on terminal
11928 frames. */
11929 if (row->displays_text_p)
11930 while (glyph < end
11931 && INTEGERP (glyph->object)
11932 && glyph->charpos < 0)
11934 x += glyph->pixel_width;
11935 ++glyph;
11938 string_start = NULL;
11939 while (glyph < end
11940 && !INTEGERP (glyph->object)
11941 && (!BUFFERP (glyph->object)
11942 || (last_pos = glyph->charpos) < pt_old))
11944 if (! STRINGP (glyph->object))
11946 string_start = NULL;
11947 x += glyph->pixel_width;
11948 ++glyph;
11949 if (cursor_from_overlay_pos
11950 && last_pos >= cursor_from_overlay_pos)
11952 cursor_from_overlay_pos = 0;
11953 cursor = 0;
11956 else
11958 if (string_start == NULL)
11960 string_before_pos = last_pos;
11961 string_start = glyph;
11962 string_start_x = x;
11964 /* Skip all glyphs from string. */
11967 Lisp_Object cprop;
11968 int pos;
11969 if ((cursor == NULL || glyph > cursor)
11970 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11971 Qcursor, (glyph)->object),
11972 !NILP (cprop))
11973 && (pos = string_buffer_position (w, glyph->object,
11974 string_before_pos),
11975 (pos == 0 /* From overlay */
11976 || pos == pt_old)))
11978 /* Estimate overlay buffer position from the buffer
11979 positions of the glyphs before and after the overlay.
11980 Add 1 to last_pos so that if point corresponds to the
11981 glyph right after the overlay, we still use a 'cursor'
11982 property found in that overlay. */
11983 cursor_from_overlay_pos = (pos ? 0 : last_pos
11984 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11985 cursor = glyph;
11986 cursor_x = x;
11988 x += glyph->pixel_width;
11989 ++glyph;
11991 while (glyph < end && EQ (glyph->object, string_start->object));
11995 if (cursor != NULL)
11997 glyph = cursor;
11998 x = cursor_x;
12000 else if (row->ends_in_ellipsis_p && glyph == end)
12002 /* Scan back over the ellipsis glyphs, decrementing positions. */
12003 while (glyph > row->glyphs[TEXT_AREA]
12004 && (glyph - 1)->charpos == last_pos)
12005 glyph--, x -= glyph->pixel_width;
12006 /* That loop always goes one position too far,
12007 including the glyph before the ellipsis.
12008 So scan forward over that one. */
12009 x += glyph->pixel_width;
12010 glyph++;
12012 else if (string_start
12013 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12015 /* We may have skipped over point because the previous glyphs
12016 are from string. As there's no easy way to know the
12017 character position of the current glyph, find the correct
12018 glyph on point by scanning from string_start again. */
12019 Lisp_Object limit;
12020 Lisp_Object string;
12021 struct glyph *stop = glyph;
12022 int pos;
12024 limit = make_number (pt_old + 1);
12025 glyph = string_start;
12026 x = string_start_x;
12027 string = glyph->object;
12028 pos = string_buffer_position (w, string, string_before_pos);
12029 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12030 because we always put cursor after overlay strings. */
12031 while (pos == 0 && glyph < stop)
12033 string = glyph->object;
12034 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12035 if (glyph < stop)
12036 pos = string_buffer_position (w, glyph->object, string_before_pos);
12039 while (glyph < stop)
12041 pos = XINT (Fnext_single_char_property_change
12042 (make_number (pos), Qdisplay, Qnil, limit));
12043 if (pos > pt_old)
12044 break;
12045 /* Skip glyphs from the same string. */
12046 string = glyph->object;
12047 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12048 /* Skip glyphs from an overlay. */
12049 while (glyph < stop
12050 && ! string_buffer_position (w, glyph->object, pos))
12052 string = glyph->object;
12053 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12057 /* If we reached the end of the line, and end was from a string,
12058 cursor is not on this line. */
12059 if (glyph == end && row->continued_p)
12060 return 0;
12063 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12064 w->cursor.x = x;
12065 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12066 w->cursor.y = row->y + dy;
12068 if (w == XWINDOW (selected_window))
12070 if (!row->continued_p
12071 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12072 && row->x == 0)
12074 this_line_buffer = XBUFFER (w->buffer);
12076 CHARPOS (this_line_start_pos)
12077 = MATRIX_ROW_START_CHARPOS (row) + delta;
12078 BYTEPOS (this_line_start_pos)
12079 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12081 CHARPOS (this_line_end_pos)
12082 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12083 BYTEPOS (this_line_end_pos)
12084 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12086 this_line_y = w->cursor.y;
12087 this_line_pixel_height = row->height;
12088 this_line_vpos = w->cursor.vpos;
12089 this_line_start_x = row->x;
12091 else
12092 CHARPOS (this_line_start_pos) = 0;
12095 return 1;
12099 /* Run window scroll functions, if any, for WINDOW with new window
12100 start STARTP. Sets the window start of WINDOW to that position.
12102 We assume that the window's buffer is really current. */
12104 static INLINE struct text_pos
12105 run_window_scroll_functions (window, startp)
12106 Lisp_Object window;
12107 struct text_pos startp;
12109 struct window *w = XWINDOW (window);
12110 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12112 if (current_buffer != XBUFFER (w->buffer))
12113 abort ();
12115 if (!NILP (Vwindow_scroll_functions))
12117 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12118 make_number (CHARPOS (startp)));
12119 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12120 /* In case the hook functions switch buffers. */
12121 if (current_buffer != XBUFFER (w->buffer))
12122 set_buffer_internal_1 (XBUFFER (w->buffer));
12125 return startp;
12129 /* Make sure the line containing the cursor is fully visible.
12130 A value of 1 means there is nothing to be done.
12131 (Either the line is fully visible, or it cannot be made so,
12132 or we cannot tell.)
12134 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12135 is higher than window.
12137 A value of 0 means the caller should do scrolling
12138 as if point had gone off the screen. */
12140 static int
12141 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12142 struct window *w;
12143 int force_p;
12144 int current_matrix_p;
12146 struct glyph_matrix *matrix;
12147 struct glyph_row *row;
12148 int window_height;
12150 if (!make_cursor_line_fully_visible_p)
12151 return 1;
12153 /* It's not always possible to find the cursor, e.g, when a window
12154 is full of overlay strings. Don't do anything in that case. */
12155 if (w->cursor.vpos < 0)
12156 return 1;
12158 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12159 row = MATRIX_ROW (matrix, w->cursor.vpos);
12161 /* If the cursor row is not partially visible, there's nothing to do. */
12162 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12163 return 1;
12165 /* If the row the cursor is in is taller than the window's height,
12166 it's not clear what to do, so do nothing. */
12167 window_height = window_box_height (w);
12168 if (row->height >= window_height)
12170 if (!force_p || MINI_WINDOW_P (w)
12171 || w->vscroll || w->cursor.vpos == 0)
12172 return 1;
12174 return 0;
12176 #if 0
12177 /* This code used to try to scroll the window just enough to make
12178 the line visible. It returned 0 to say that the caller should
12179 allocate larger glyph matrices. */
12181 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12183 int dy = row->height - row->visible_height;
12184 w->vscroll = 0;
12185 w->cursor.y += dy;
12186 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12188 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12190 int dy = - (row->height - row->visible_height);
12191 w->vscroll = dy;
12192 w->cursor.y += dy;
12193 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12196 /* When we change the cursor y-position of the selected window,
12197 change this_line_y as well so that the display optimization for
12198 the cursor line of the selected window in redisplay_internal uses
12199 the correct y-position. */
12200 if (w == XWINDOW (selected_window))
12201 this_line_y = w->cursor.y;
12203 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12204 redisplay with larger matrices. */
12205 if (matrix->nrows < required_matrix_height (w))
12207 fonts_changed_p = 1;
12208 return 0;
12211 return 1;
12212 #endif /* 0 */
12216 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12217 non-zero means only WINDOW is redisplayed in redisplay_internal.
12218 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12219 in redisplay_window to bring a partially visible line into view in
12220 the case that only the cursor has moved.
12222 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12223 last screen line's vertical height extends past the end of the screen.
12225 Value is
12227 1 if scrolling succeeded
12229 0 if scrolling didn't find point.
12231 -1 if new fonts have been loaded so that we must interrupt
12232 redisplay, adjust glyph matrices, and try again. */
12234 enum
12236 SCROLLING_SUCCESS,
12237 SCROLLING_FAILED,
12238 SCROLLING_NEED_LARGER_MATRICES
12241 static int
12242 try_scrolling (window, just_this_one_p, scroll_conservatively,
12243 scroll_step, temp_scroll_step, last_line_misfit)
12244 Lisp_Object window;
12245 int just_this_one_p;
12246 EMACS_INT scroll_conservatively, scroll_step;
12247 int temp_scroll_step;
12248 int last_line_misfit;
12250 struct window *w = XWINDOW (window);
12251 struct frame *f = XFRAME (w->frame);
12252 struct text_pos scroll_margin_pos;
12253 struct text_pos pos;
12254 struct text_pos startp;
12255 struct it it;
12256 Lisp_Object window_end;
12257 int this_scroll_margin;
12258 int dy = 0;
12259 int scroll_max;
12260 int rc;
12261 int amount_to_scroll = 0;
12262 Lisp_Object aggressive;
12263 int height;
12264 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12266 #if GLYPH_DEBUG
12267 debug_method_add (w, "try_scrolling");
12268 #endif
12270 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12272 /* Compute scroll margin height in pixels. We scroll when point is
12273 within this distance from the top or bottom of the window. */
12274 if (scroll_margin > 0)
12276 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12277 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12279 else
12280 this_scroll_margin = 0;
12282 /* Force scroll_conservatively to have a reasonable value so it doesn't
12283 cause an overflow while computing how much to scroll. */
12284 if (scroll_conservatively)
12285 scroll_conservatively = min (scroll_conservatively,
12286 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12288 /* Compute how much we should try to scroll maximally to bring point
12289 into view. */
12290 if (scroll_step || scroll_conservatively || temp_scroll_step)
12291 scroll_max = max (scroll_step,
12292 max (scroll_conservatively, temp_scroll_step));
12293 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12294 || NUMBERP (current_buffer->scroll_up_aggressively))
12295 /* We're trying to scroll because of aggressive scrolling
12296 but no scroll_step is set. Choose an arbitrary one. Maybe
12297 there should be a variable for this. */
12298 scroll_max = 10;
12299 else
12300 scroll_max = 0;
12301 scroll_max *= FRAME_LINE_HEIGHT (f);
12303 /* Decide whether we have to scroll down. Start at the window end
12304 and move this_scroll_margin up to find the position of the scroll
12305 margin. */
12306 window_end = Fwindow_end (window, Qt);
12308 too_near_end:
12310 CHARPOS (scroll_margin_pos) = XINT (window_end);
12311 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12313 if (this_scroll_margin || extra_scroll_margin_lines)
12315 start_display (&it, w, scroll_margin_pos);
12316 if (this_scroll_margin)
12317 move_it_vertically_backward (&it, this_scroll_margin);
12318 if (extra_scroll_margin_lines)
12319 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12320 scroll_margin_pos = it.current.pos;
12323 if (PT >= CHARPOS (scroll_margin_pos))
12325 int y0;
12327 /* Point is in the scroll margin at the bottom of the window, or
12328 below. Compute a new window start that makes point visible. */
12330 /* Compute the distance from the scroll margin to PT.
12331 Give up if the distance is greater than scroll_max. */
12332 start_display (&it, w, scroll_margin_pos);
12333 y0 = it.current_y;
12334 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12335 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12337 /* To make point visible, we have to move the window start
12338 down so that the line the cursor is in is visible, which
12339 means we have to add in the height of the cursor line. */
12340 dy = line_bottom_y (&it) - y0;
12342 if (dy > scroll_max)
12343 return SCROLLING_FAILED;
12345 /* Move the window start down. If scrolling conservatively,
12346 move it just enough down to make point visible. If
12347 scroll_step is set, move it down by scroll_step. */
12348 start_display (&it, w, startp);
12350 if (scroll_conservatively)
12351 /* Set AMOUNT_TO_SCROLL to at least one line,
12352 and at most scroll_conservatively lines. */
12353 amount_to_scroll
12354 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12355 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12356 else if (scroll_step || temp_scroll_step)
12357 amount_to_scroll = scroll_max;
12358 else
12360 aggressive = current_buffer->scroll_up_aggressively;
12361 height = WINDOW_BOX_TEXT_HEIGHT (w);
12362 if (NUMBERP (aggressive))
12364 double float_amount = XFLOATINT (aggressive) * height;
12365 amount_to_scroll = float_amount;
12366 if (amount_to_scroll == 0 && float_amount > 0)
12367 amount_to_scroll = 1;
12371 if (amount_to_scroll <= 0)
12372 return SCROLLING_FAILED;
12374 /* If moving by amount_to_scroll leaves STARTP unchanged,
12375 move it down one screen line. */
12377 move_it_vertically (&it, amount_to_scroll);
12378 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12379 move_it_by_lines (&it, 1, 1);
12380 startp = it.current.pos;
12382 else
12384 /* See if point is inside the scroll margin at the top of the
12385 window. */
12386 scroll_margin_pos = startp;
12387 if (this_scroll_margin)
12389 start_display (&it, w, startp);
12390 move_it_vertically (&it, this_scroll_margin);
12391 scroll_margin_pos = it.current.pos;
12394 if (PT < CHARPOS (scroll_margin_pos))
12396 /* Point is in the scroll margin at the top of the window or
12397 above what is displayed in the window. */
12398 int y0;
12400 /* Compute the vertical distance from PT to the scroll
12401 margin position. Give up if distance is greater than
12402 scroll_max. */
12403 SET_TEXT_POS (pos, PT, PT_BYTE);
12404 start_display (&it, w, pos);
12405 y0 = it.current_y;
12406 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12407 it.last_visible_y, -1,
12408 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12409 dy = it.current_y - y0;
12410 if (dy > scroll_max)
12411 return SCROLLING_FAILED;
12413 /* Compute new window start. */
12414 start_display (&it, w, startp);
12416 if (scroll_conservatively)
12417 amount_to_scroll
12418 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12419 else if (scroll_step || temp_scroll_step)
12420 amount_to_scroll = scroll_max;
12421 else
12423 aggressive = current_buffer->scroll_down_aggressively;
12424 height = WINDOW_BOX_TEXT_HEIGHT (w);
12425 if (NUMBERP (aggressive))
12427 double float_amount = XFLOATINT (aggressive) * height;
12428 amount_to_scroll = float_amount;
12429 if (amount_to_scroll == 0 && float_amount > 0)
12430 amount_to_scroll = 1;
12434 if (amount_to_scroll <= 0)
12435 return SCROLLING_FAILED;
12437 move_it_vertically_backward (&it, amount_to_scroll);
12438 startp = it.current.pos;
12442 /* Run window scroll functions. */
12443 startp = run_window_scroll_functions (window, startp);
12445 /* Display the window. Give up if new fonts are loaded, or if point
12446 doesn't appear. */
12447 if (!try_window (window, startp, 0))
12448 rc = SCROLLING_NEED_LARGER_MATRICES;
12449 else if (w->cursor.vpos < 0)
12451 clear_glyph_matrix (w->desired_matrix);
12452 rc = SCROLLING_FAILED;
12454 else
12456 /* Maybe forget recorded base line for line number display. */
12457 if (!just_this_one_p
12458 || current_buffer->clip_changed
12459 || BEG_UNCHANGED < CHARPOS (startp))
12460 w->base_line_number = Qnil;
12462 /* If cursor ends up on a partially visible line,
12463 treat that as being off the bottom of the screen. */
12464 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12466 clear_glyph_matrix (w->desired_matrix);
12467 ++extra_scroll_margin_lines;
12468 goto too_near_end;
12470 rc = SCROLLING_SUCCESS;
12473 return rc;
12477 /* Compute a suitable window start for window W if display of W starts
12478 on a continuation line. Value is non-zero if a new window start
12479 was computed.
12481 The new window start will be computed, based on W's width, starting
12482 from the start of the continued line. It is the start of the
12483 screen line with the minimum distance from the old start W->start. */
12485 static int
12486 compute_window_start_on_continuation_line (w)
12487 struct window *w;
12489 struct text_pos pos, start_pos;
12490 int window_start_changed_p = 0;
12492 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12494 /* If window start is on a continuation line... Window start may be
12495 < BEGV in case there's invisible text at the start of the
12496 buffer (M-x rmail, for example). */
12497 if (CHARPOS (start_pos) > BEGV
12498 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12500 struct it it;
12501 struct glyph_row *row;
12503 /* Handle the case that the window start is out of range. */
12504 if (CHARPOS (start_pos) < BEGV)
12505 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12506 else if (CHARPOS (start_pos) > ZV)
12507 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12509 /* Find the start of the continued line. This should be fast
12510 because scan_buffer is fast (newline cache). */
12511 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12512 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12513 row, DEFAULT_FACE_ID);
12514 reseat_at_previous_visible_line_start (&it);
12516 /* If the line start is "too far" away from the window start,
12517 say it takes too much time to compute a new window start. */
12518 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12519 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12521 int min_distance, distance;
12523 /* Move forward by display lines to find the new window
12524 start. If window width was enlarged, the new start can
12525 be expected to be > the old start. If window width was
12526 decreased, the new window start will be < the old start.
12527 So, we're looking for the display line start with the
12528 minimum distance from the old window start. */
12529 pos = it.current.pos;
12530 min_distance = INFINITY;
12531 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12532 distance < min_distance)
12534 min_distance = distance;
12535 pos = it.current.pos;
12536 move_it_by_lines (&it, 1, 0);
12539 /* Set the window start there. */
12540 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12541 window_start_changed_p = 1;
12545 return window_start_changed_p;
12549 /* Try cursor movement in case text has not changed in window WINDOW,
12550 with window start STARTP. Value is
12552 CURSOR_MOVEMENT_SUCCESS if successful
12554 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12556 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12557 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12558 we want to scroll as if scroll-step were set to 1. See the code.
12560 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12561 which case we have to abort this redisplay, and adjust matrices
12562 first. */
12564 enum
12566 CURSOR_MOVEMENT_SUCCESS,
12567 CURSOR_MOVEMENT_CANNOT_BE_USED,
12568 CURSOR_MOVEMENT_MUST_SCROLL,
12569 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12572 static int
12573 try_cursor_movement (window, startp, scroll_step)
12574 Lisp_Object window;
12575 struct text_pos startp;
12576 int *scroll_step;
12578 struct window *w = XWINDOW (window);
12579 struct frame *f = XFRAME (w->frame);
12580 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12582 #if GLYPH_DEBUG
12583 if (inhibit_try_cursor_movement)
12584 return rc;
12585 #endif
12587 /* Handle case where text has not changed, only point, and it has
12588 not moved off the frame. */
12589 if (/* Point may be in this window. */
12590 PT >= CHARPOS (startp)
12591 /* Selective display hasn't changed. */
12592 && !current_buffer->clip_changed
12593 /* Function force-mode-line-update is used to force a thorough
12594 redisplay. It sets either windows_or_buffers_changed or
12595 update_mode_lines. So don't take a shortcut here for these
12596 cases. */
12597 && !update_mode_lines
12598 && !windows_or_buffers_changed
12599 && !cursor_type_changed
12600 /* Can't use this case if highlighting a region. When a
12601 region exists, cursor movement has to do more than just
12602 set the cursor. */
12603 && !(!NILP (Vtransient_mark_mode)
12604 && !NILP (current_buffer->mark_active))
12605 && NILP (w->region_showing)
12606 && NILP (Vshow_trailing_whitespace)
12607 /* Right after splitting windows, last_point may be nil. */
12608 && INTEGERP (w->last_point)
12609 /* This code is not used for mini-buffer for the sake of the case
12610 of redisplaying to replace an echo area message; since in
12611 that case the mini-buffer contents per se are usually
12612 unchanged. This code is of no real use in the mini-buffer
12613 since the handling of this_line_start_pos, etc., in redisplay
12614 handles the same cases. */
12615 && !EQ (window, minibuf_window)
12616 /* When splitting windows or for new windows, it happens that
12617 redisplay is called with a nil window_end_vpos or one being
12618 larger than the window. This should really be fixed in
12619 window.c. I don't have this on my list, now, so we do
12620 approximately the same as the old redisplay code. --gerd. */
12621 && INTEGERP (w->window_end_vpos)
12622 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12623 && (FRAME_WINDOW_P (f)
12624 || !overlay_arrow_in_current_buffer_p ()))
12626 int this_scroll_margin, top_scroll_margin;
12627 struct glyph_row *row = NULL;
12629 #if GLYPH_DEBUG
12630 debug_method_add (w, "cursor movement");
12631 #endif
12633 /* Scroll if point within this distance from the top or bottom
12634 of the window. This is a pixel value. */
12635 this_scroll_margin = max (0, scroll_margin);
12636 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12637 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12639 top_scroll_margin = this_scroll_margin;
12640 if (WINDOW_WANTS_HEADER_LINE_P (w))
12641 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12643 /* Start with the row the cursor was displayed during the last
12644 not paused redisplay. Give up if that row is not valid. */
12645 if (w->last_cursor.vpos < 0
12646 || w->last_cursor.vpos >= w->current_matrix->nrows)
12647 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12648 else
12650 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12651 if (row->mode_line_p)
12652 ++row;
12653 if (!row->enabled_p)
12654 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12657 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12659 int scroll_p = 0;
12660 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12662 if (PT > XFASTINT (w->last_point))
12664 /* Point has moved forward. */
12665 while (MATRIX_ROW_END_CHARPOS (row) < PT
12666 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12668 xassert (row->enabled_p);
12669 ++row;
12672 /* The end position of a row equals the start position
12673 of the next row. If PT is there, we would rather
12674 display it in the next line. */
12675 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12676 && MATRIX_ROW_END_CHARPOS (row) == PT
12677 && !cursor_row_p (w, row))
12678 ++row;
12680 /* If within the scroll margin, scroll. Note that
12681 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12682 the next line would be drawn, and that
12683 this_scroll_margin can be zero. */
12684 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12685 || PT > MATRIX_ROW_END_CHARPOS (row)
12686 /* Line is completely visible last line in window
12687 and PT is to be set in the next line. */
12688 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12689 && PT == MATRIX_ROW_END_CHARPOS (row)
12690 && !row->ends_at_zv_p
12691 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12692 scroll_p = 1;
12694 else if (PT < XFASTINT (w->last_point))
12696 /* Cursor has to be moved backward. Note that PT >=
12697 CHARPOS (startp) because of the outer if-statement. */
12698 while (!row->mode_line_p
12699 && (MATRIX_ROW_START_CHARPOS (row) > PT
12700 || (MATRIX_ROW_START_CHARPOS (row) == PT
12701 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12702 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12703 row > w->current_matrix->rows
12704 && (row-1)->ends_in_newline_from_string_p))))
12705 && (row->y > top_scroll_margin
12706 || CHARPOS (startp) == BEGV))
12708 xassert (row->enabled_p);
12709 --row;
12712 /* Consider the following case: Window starts at BEGV,
12713 there is invisible, intangible text at BEGV, so that
12714 display starts at some point START > BEGV. It can
12715 happen that we are called with PT somewhere between
12716 BEGV and START. Try to handle that case. */
12717 if (row < w->current_matrix->rows
12718 || row->mode_line_p)
12720 row = w->current_matrix->rows;
12721 if (row->mode_line_p)
12722 ++row;
12725 /* Due to newlines in overlay strings, we may have to
12726 skip forward over overlay strings. */
12727 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12728 && MATRIX_ROW_END_CHARPOS (row) == PT
12729 && !cursor_row_p (w, row))
12730 ++row;
12732 /* If within the scroll margin, scroll. */
12733 if (row->y < top_scroll_margin
12734 && CHARPOS (startp) != BEGV)
12735 scroll_p = 1;
12737 else
12739 /* Cursor did not move. So don't scroll even if cursor line
12740 is partially visible, as it was so before. */
12741 rc = CURSOR_MOVEMENT_SUCCESS;
12744 if (PT < MATRIX_ROW_START_CHARPOS (row)
12745 || PT > MATRIX_ROW_END_CHARPOS (row))
12747 /* if PT is not in the glyph row, give up. */
12748 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12750 else if (rc != CURSOR_MOVEMENT_SUCCESS
12751 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12752 && make_cursor_line_fully_visible_p)
12754 if (PT == MATRIX_ROW_END_CHARPOS (row)
12755 && !row->ends_at_zv_p
12756 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12757 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12758 else if (row->height > window_box_height (w))
12760 /* If we end up in a partially visible line, let's
12761 make it fully visible, except when it's taller
12762 than the window, in which case we can't do much
12763 about it. */
12764 *scroll_step = 1;
12765 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12767 else
12769 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12770 if (!cursor_row_fully_visible_p (w, 0, 1))
12771 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12772 else
12773 rc = CURSOR_MOVEMENT_SUCCESS;
12776 else if (scroll_p)
12777 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12778 else
12782 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12784 rc = CURSOR_MOVEMENT_SUCCESS;
12785 break;
12787 ++row;
12789 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12790 && MATRIX_ROW_START_CHARPOS (row) == PT
12791 && cursor_row_p (w, row));
12796 return rc;
12799 void
12800 set_vertical_scroll_bar (w)
12801 struct window *w;
12803 int start, end, whole;
12805 /* Calculate the start and end positions for the current window.
12806 At some point, it would be nice to choose between scrollbars
12807 which reflect the whole buffer size, with special markers
12808 indicating narrowing, and scrollbars which reflect only the
12809 visible region.
12811 Note that mini-buffers sometimes aren't displaying any text. */
12812 if (!MINI_WINDOW_P (w)
12813 || (w == XWINDOW (minibuf_window)
12814 && NILP (echo_area_buffer[0])))
12816 struct buffer *buf = XBUFFER (w->buffer);
12817 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12818 start = marker_position (w->start) - BUF_BEGV (buf);
12819 /* I don't think this is guaranteed to be right. For the
12820 moment, we'll pretend it is. */
12821 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12823 if (end < start)
12824 end = start;
12825 if (whole < (end - start))
12826 whole = end - start;
12828 else
12829 start = end = whole = 0;
12831 /* Indicate what this scroll bar ought to be displaying now. */
12832 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12836 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12837 selected_window is redisplayed.
12839 We can return without actually redisplaying the window if
12840 fonts_changed_p is nonzero. In that case, redisplay_internal will
12841 retry. */
12843 static void
12844 redisplay_window (window, just_this_one_p)
12845 Lisp_Object window;
12846 int just_this_one_p;
12848 struct window *w = XWINDOW (window);
12849 struct frame *f = XFRAME (w->frame);
12850 struct buffer *buffer = XBUFFER (w->buffer);
12851 struct buffer *old = current_buffer;
12852 struct text_pos lpoint, opoint, startp;
12853 int update_mode_line;
12854 int tem;
12855 struct it it;
12856 /* Record it now because it's overwritten. */
12857 int current_matrix_up_to_date_p = 0;
12858 int used_current_matrix_p = 0;
12859 /* This is less strict than current_matrix_up_to_date_p.
12860 It indictes that the buffer contents and narrowing are unchanged. */
12861 int buffer_unchanged_p = 0;
12862 int temp_scroll_step = 0;
12863 int count = SPECPDL_INDEX ();
12864 int rc;
12865 int centering_position = -1;
12866 int last_line_misfit = 0;
12867 int save_beg_unchanged, save_end_unchanged;
12869 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12870 opoint = lpoint;
12872 /* W must be a leaf window here. */
12873 xassert (!NILP (w->buffer));
12874 #if GLYPH_DEBUG
12875 *w->desired_matrix->method = 0;
12876 #endif
12878 specbind (Qinhibit_point_motion_hooks, Qt);
12880 reconsider_clip_changes (w, buffer);
12882 /* Has the mode line to be updated? */
12883 update_mode_line = (!NILP (w->update_mode_line)
12884 || update_mode_lines
12885 || buffer->clip_changed
12886 || buffer->prevent_redisplay_optimizations_p);
12888 if (MINI_WINDOW_P (w))
12890 if (w == XWINDOW (echo_area_window)
12891 && !NILP (echo_area_buffer[0]))
12893 if (update_mode_line)
12894 /* We may have to update a tty frame's menu bar or a
12895 tool-bar. Example `M-x C-h C-h C-g'. */
12896 goto finish_menu_bars;
12897 else
12898 /* We've already displayed the echo area glyphs in this window. */
12899 goto finish_scroll_bars;
12901 else if ((w != XWINDOW (minibuf_window)
12902 || minibuf_level == 0)
12903 /* When buffer is nonempty, redisplay window normally. */
12904 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12905 /* Quail displays non-mini buffers in minibuffer window.
12906 In that case, redisplay the window normally. */
12907 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12909 /* W is a mini-buffer window, but it's not active, so clear
12910 it. */
12911 int yb = window_text_bottom_y (w);
12912 struct glyph_row *row;
12913 int y;
12915 for (y = 0, row = w->desired_matrix->rows;
12916 y < yb;
12917 y += row->height, ++row)
12918 blank_row (w, row, y);
12919 goto finish_scroll_bars;
12922 clear_glyph_matrix (w->desired_matrix);
12925 /* Otherwise set up data on this window; select its buffer and point
12926 value. */
12927 /* Really select the buffer, for the sake of buffer-local
12928 variables. */
12929 set_buffer_internal_1 (XBUFFER (w->buffer));
12930 SET_TEXT_POS (opoint, PT, PT_BYTE);
12932 save_beg_unchanged = BEG_UNCHANGED;
12933 save_end_unchanged = END_UNCHANGED;
12935 current_matrix_up_to_date_p
12936 = (!NILP (w->window_end_valid)
12937 && !current_buffer->clip_changed
12938 && !current_buffer->prevent_redisplay_optimizations_p
12939 && XFASTINT (w->last_modified) >= MODIFF
12940 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12942 buffer_unchanged_p
12943 = (!NILP (w->window_end_valid)
12944 && !current_buffer->clip_changed
12945 && XFASTINT (w->last_modified) >= MODIFF
12946 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12948 /* When windows_or_buffers_changed is non-zero, we can't rely on
12949 the window end being valid, so set it to nil there. */
12950 if (windows_or_buffers_changed)
12952 /* If window starts on a continuation line, maybe adjust the
12953 window start in case the window's width changed. */
12954 if (XMARKER (w->start)->buffer == current_buffer)
12955 compute_window_start_on_continuation_line (w);
12957 w->window_end_valid = Qnil;
12960 /* Some sanity checks. */
12961 CHECK_WINDOW_END (w);
12962 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12963 abort ();
12964 if (BYTEPOS (opoint) < CHARPOS (opoint))
12965 abort ();
12967 /* If %c is in mode line, update it if needed. */
12968 if (!NILP (w->column_number_displayed)
12969 /* This alternative quickly identifies a common case
12970 where no change is needed. */
12971 && !(PT == XFASTINT (w->last_point)
12972 && XFASTINT (w->last_modified) >= MODIFF
12973 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12974 && (XFASTINT (w->column_number_displayed)
12975 != (int) current_column ())) /* iftc */
12976 update_mode_line = 1;
12978 /* Count number of windows showing the selected buffer. An indirect
12979 buffer counts as its base buffer. */
12980 if (!just_this_one_p)
12982 struct buffer *current_base, *window_base;
12983 current_base = current_buffer;
12984 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12985 if (current_base->base_buffer)
12986 current_base = current_base->base_buffer;
12987 if (window_base->base_buffer)
12988 window_base = window_base->base_buffer;
12989 if (current_base == window_base)
12990 buffer_shared++;
12993 /* Point refers normally to the selected window. For any other
12994 window, set up appropriate value. */
12995 if (!EQ (window, selected_window))
12997 int new_pt = XMARKER (w->pointm)->charpos;
12998 int new_pt_byte = marker_byte_position (w->pointm);
12999 if (new_pt < BEGV)
13001 new_pt = BEGV;
13002 new_pt_byte = BEGV_BYTE;
13003 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13005 else if (new_pt > (ZV - 1))
13007 new_pt = ZV;
13008 new_pt_byte = ZV_BYTE;
13009 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13012 /* We don't use SET_PT so that the point-motion hooks don't run. */
13013 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13016 /* If any of the character widths specified in the display table
13017 have changed, invalidate the width run cache. It's true that
13018 this may be a bit late to catch such changes, but the rest of
13019 redisplay goes (non-fatally) haywire when the display table is
13020 changed, so why should we worry about doing any better? */
13021 if (current_buffer->width_run_cache)
13023 struct Lisp_Char_Table *disptab = buffer_display_table ();
13025 if (! disptab_matches_widthtab (disptab,
13026 XVECTOR (current_buffer->width_table)))
13028 invalidate_region_cache (current_buffer,
13029 current_buffer->width_run_cache,
13030 BEG, Z);
13031 recompute_width_table (current_buffer, disptab);
13035 /* If window-start is screwed up, choose a new one. */
13036 if (XMARKER (w->start)->buffer != current_buffer)
13037 goto recenter;
13039 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13041 /* If someone specified a new starting point but did not insist,
13042 check whether it can be used. */
13043 if (!NILP (w->optional_new_start)
13044 && CHARPOS (startp) >= BEGV
13045 && CHARPOS (startp) <= ZV)
13047 w->optional_new_start = Qnil;
13048 start_display (&it, w, startp);
13049 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13050 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13051 if (IT_CHARPOS (it) == PT)
13052 w->force_start = Qt;
13053 /* IT may overshoot PT if text at PT is invisible. */
13054 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13055 w->force_start = Qt;
13058 force_start:
13060 /* Handle case where place to start displaying has been specified,
13061 unless the specified location is outside the accessible range. */
13062 if (!NILP (w->force_start)
13063 || w->frozen_window_start_p)
13065 /* We set this later on if we have to adjust point. */
13066 int new_vpos = -1;
13067 int val;
13069 w->force_start = Qnil;
13070 w->vscroll = 0;
13071 w->window_end_valid = Qnil;
13073 /* Forget any recorded base line for line number display. */
13074 if (!buffer_unchanged_p)
13075 w->base_line_number = Qnil;
13077 /* Redisplay the mode line. Select the buffer properly for that.
13078 Also, run the hook window-scroll-functions
13079 because we have scrolled. */
13080 /* Note, we do this after clearing force_start because
13081 if there's an error, it is better to forget about force_start
13082 than to get into an infinite loop calling the hook functions
13083 and having them get more errors. */
13084 if (!update_mode_line
13085 || ! NILP (Vwindow_scroll_functions))
13087 update_mode_line = 1;
13088 w->update_mode_line = Qt;
13089 startp = run_window_scroll_functions (window, startp);
13092 w->last_modified = make_number (0);
13093 w->last_overlay_modified = make_number (0);
13094 if (CHARPOS (startp) < BEGV)
13095 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13096 else if (CHARPOS (startp) > ZV)
13097 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13099 /* Redisplay, then check if cursor has been set during the
13100 redisplay. Give up if new fonts were loaded. */
13101 val = try_window (window, startp, 1);
13102 if (!val)
13104 w->force_start = Qt;
13105 clear_glyph_matrix (w->desired_matrix);
13106 goto need_larger_matrices;
13108 /* Point was outside the scroll margins. */
13109 if (val < 0)
13110 new_vpos = window_box_height (w) / 2;
13112 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13114 /* If point does not appear, try to move point so it does
13115 appear. The desired matrix has been built above, so we
13116 can use it here. */
13117 new_vpos = window_box_height (w) / 2;
13120 if (!cursor_row_fully_visible_p (w, 0, 0))
13122 /* Point does appear, but on a line partly visible at end of window.
13123 Move it back to a fully-visible line. */
13124 new_vpos = window_box_height (w);
13127 /* If we need to move point for either of the above reasons,
13128 now actually do it. */
13129 if (new_vpos >= 0)
13131 struct glyph_row *row;
13133 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13134 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13135 ++row;
13137 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13138 MATRIX_ROW_START_BYTEPOS (row));
13140 if (w != XWINDOW (selected_window))
13141 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13142 else if (current_buffer == old)
13143 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13145 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13147 /* If we are highlighting the region, then we just changed
13148 the region, so redisplay to show it. */
13149 if (!NILP (Vtransient_mark_mode)
13150 && !NILP (current_buffer->mark_active))
13152 clear_glyph_matrix (w->desired_matrix);
13153 if (!try_window (window, startp, 0))
13154 goto need_larger_matrices;
13158 #if GLYPH_DEBUG
13159 debug_method_add (w, "forced window start");
13160 #endif
13161 goto done;
13164 /* Handle case where text has not changed, only point, and it has
13165 not moved off the frame, and we are not retrying after hscroll.
13166 (current_matrix_up_to_date_p is nonzero when retrying.) */
13167 if (current_matrix_up_to_date_p
13168 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13169 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13171 switch (rc)
13173 case CURSOR_MOVEMENT_SUCCESS:
13174 used_current_matrix_p = 1;
13175 goto done;
13177 #if 0 /* try_cursor_movement never returns this value. */
13178 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13179 goto need_larger_matrices;
13180 #endif
13182 case CURSOR_MOVEMENT_MUST_SCROLL:
13183 goto try_to_scroll;
13185 default:
13186 abort ();
13189 /* If current starting point was originally the beginning of a line
13190 but no longer is, find a new starting point. */
13191 else if (!NILP (w->start_at_line_beg)
13192 && !(CHARPOS (startp) <= BEGV
13193 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13195 #if GLYPH_DEBUG
13196 debug_method_add (w, "recenter 1");
13197 #endif
13198 goto recenter;
13201 /* Try scrolling with try_window_id. Value is > 0 if update has
13202 been done, it is -1 if we know that the same window start will
13203 not work. It is 0 if unsuccessful for some other reason. */
13204 else if ((tem = try_window_id (w)) != 0)
13206 #if GLYPH_DEBUG
13207 debug_method_add (w, "try_window_id %d", tem);
13208 #endif
13210 if (fonts_changed_p)
13211 goto need_larger_matrices;
13212 if (tem > 0)
13213 goto done;
13215 /* Otherwise try_window_id has returned -1 which means that we
13216 don't want the alternative below this comment to execute. */
13218 else if (CHARPOS (startp) >= BEGV
13219 && CHARPOS (startp) <= ZV
13220 && PT >= CHARPOS (startp)
13221 && (CHARPOS (startp) < ZV
13222 /* Avoid starting at end of buffer. */
13223 || CHARPOS (startp) == BEGV
13224 || (XFASTINT (w->last_modified) >= MODIFF
13225 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13228 /* If first window line is a continuation line, and window start
13229 is inside the modified region, but the first change is before
13230 current window start, we must select a new window start.
13232 However, if this is the result of a down-mouse event (e.g. by
13233 extending the mouse-drag-overlay), we don't want to select a
13234 new window start, since that would change the position under
13235 the mouse, resulting in an unwanted mouse-movement rather
13236 than a simple mouse-click. */
13237 if (NILP (w->start_at_line_beg)
13238 && NILP (do_mouse_tracking)
13239 && CHARPOS (startp) > BEGV
13240 && CHARPOS (startp) > BEG + save_beg_unchanged
13241 && CHARPOS (startp) <= Z - save_end_unchanged)
13243 w->force_start = Qt;
13244 if (XMARKER (w->start)->buffer == current_buffer)
13245 compute_window_start_on_continuation_line (w);
13246 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13247 goto force_start;
13250 #if GLYPH_DEBUG
13251 debug_method_add (w, "same window start");
13252 #endif
13254 /* Try to redisplay starting at same place as before.
13255 If point has not moved off frame, accept the results. */
13256 if (!current_matrix_up_to_date_p
13257 /* Don't use try_window_reusing_current_matrix in this case
13258 because a window scroll function can have changed the
13259 buffer. */
13260 || !NILP (Vwindow_scroll_functions)
13261 || MINI_WINDOW_P (w)
13262 || !(used_current_matrix_p
13263 = try_window_reusing_current_matrix (w)))
13265 IF_DEBUG (debug_method_add (w, "1"));
13266 if (try_window (window, startp, 1) < 0)
13267 /* -1 means we need to scroll.
13268 0 means we need new matrices, but fonts_changed_p
13269 is set in that case, so we will detect it below. */
13270 goto try_to_scroll;
13273 if (fonts_changed_p)
13274 goto need_larger_matrices;
13276 if (w->cursor.vpos >= 0)
13278 if (!just_this_one_p
13279 || current_buffer->clip_changed
13280 || BEG_UNCHANGED < CHARPOS (startp))
13281 /* Forget any recorded base line for line number display. */
13282 w->base_line_number = Qnil;
13284 if (!cursor_row_fully_visible_p (w, 1, 0))
13286 clear_glyph_matrix (w->desired_matrix);
13287 last_line_misfit = 1;
13289 /* Drop through and scroll. */
13290 else
13291 goto done;
13293 else
13294 clear_glyph_matrix (w->desired_matrix);
13297 try_to_scroll:
13299 w->last_modified = make_number (0);
13300 w->last_overlay_modified = make_number (0);
13302 /* Redisplay the mode line. Select the buffer properly for that. */
13303 if (!update_mode_line)
13305 update_mode_line = 1;
13306 w->update_mode_line = Qt;
13309 /* Try to scroll by specified few lines. */
13310 if ((scroll_conservatively
13311 || scroll_step
13312 || temp_scroll_step
13313 || NUMBERP (current_buffer->scroll_up_aggressively)
13314 || NUMBERP (current_buffer->scroll_down_aggressively))
13315 && !current_buffer->clip_changed
13316 && CHARPOS (startp) >= BEGV
13317 && CHARPOS (startp) <= ZV)
13319 /* The function returns -1 if new fonts were loaded, 1 if
13320 successful, 0 if not successful. */
13321 int rc = try_scrolling (window, just_this_one_p,
13322 scroll_conservatively,
13323 scroll_step,
13324 temp_scroll_step, last_line_misfit);
13325 switch (rc)
13327 case SCROLLING_SUCCESS:
13328 goto done;
13330 case SCROLLING_NEED_LARGER_MATRICES:
13331 goto need_larger_matrices;
13333 case SCROLLING_FAILED:
13334 break;
13336 default:
13337 abort ();
13341 /* Finally, just choose place to start which centers point */
13343 recenter:
13344 if (centering_position < 0)
13345 centering_position = window_box_height (w) / 2;
13347 #if GLYPH_DEBUG
13348 debug_method_add (w, "recenter");
13349 #endif
13351 /* w->vscroll = 0; */
13353 /* Forget any previously recorded base line for line number display. */
13354 if (!buffer_unchanged_p)
13355 w->base_line_number = Qnil;
13357 /* Move backward half the height of the window. */
13358 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13359 it.current_y = it.last_visible_y;
13360 move_it_vertically_backward (&it, centering_position);
13361 xassert (IT_CHARPOS (it) >= BEGV);
13363 /* The function move_it_vertically_backward may move over more
13364 than the specified y-distance. If it->w is small, e.g. a
13365 mini-buffer window, we may end up in front of the window's
13366 display area. Start displaying at the start of the line
13367 containing PT in this case. */
13368 if (it.current_y <= 0)
13370 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13371 move_it_vertically_backward (&it, 0);
13372 #if 0
13373 /* I think this assert is bogus if buffer contains
13374 invisible text or images. KFS. */
13375 xassert (IT_CHARPOS (it) <= PT);
13376 #endif
13377 it.current_y = 0;
13380 it.current_x = it.hpos = 0;
13382 /* Set startp here explicitly in case that helps avoid an infinite loop
13383 in case the window-scroll-functions functions get errors. */
13384 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13386 /* Run scroll hooks. */
13387 startp = run_window_scroll_functions (window, it.current.pos);
13389 /* Redisplay the window. */
13390 if (!current_matrix_up_to_date_p
13391 || windows_or_buffers_changed
13392 || cursor_type_changed
13393 /* Don't use try_window_reusing_current_matrix in this case
13394 because it can have changed the buffer. */
13395 || !NILP (Vwindow_scroll_functions)
13396 || !just_this_one_p
13397 || MINI_WINDOW_P (w)
13398 || !(used_current_matrix_p
13399 = try_window_reusing_current_matrix (w)))
13400 try_window (window, startp, 0);
13402 /* If new fonts have been loaded (due to fontsets), give up. We
13403 have to start a new redisplay since we need to re-adjust glyph
13404 matrices. */
13405 if (fonts_changed_p)
13406 goto need_larger_matrices;
13408 /* If cursor did not appear assume that the middle of the window is
13409 in the first line of the window. Do it again with the next line.
13410 (Imagine a window of height 100, displaying two lines of height
13411 60. Moving back 50 from it->last_visible_y will end in the first
13412 line.) */
13413 if (w->cursor.vpos < 0)
13415 if (!NILP (w->window_end_valid)
13416 && PT >= Z - XFASTINT (w->window_end_pos))
13418 clear_glyph_matrix (w->desired_matrix);
13419 move_it_by_lines (&it, 1, 0);
13420 try_window (window, it.current.pos, 0);
13422 else if (PT < IT_CHARPOS (it))
13424 clear_glyph_matrix (w->desired_matrix);
13425 move_it_by_lines (&it, -1, 0);
13426 try_window (window, it.current.pos, 0);
13428 else
13430 /* Not much we can do about it. */
13434 /* Consider the following case: Window starts at BEGV, there is
13435 invisible, intangible text at BEGV, so that display starts at
13436 some point START > BEGV. It can happen that we are called with
13437 PT somewhere between BEGV and START. Try to handle that case. */
13438 if (w->cursor.vpos < 0)
13440 struct glyph_row *row = w->current_matrix->rows;
13441 if (row->mode_line_p)
13442 ++row;
13443 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13446 if (!cursor_row_fully_visible_p (w, 0, 0))
13448 /* If vscroll is enabled, disable it and try again. */
13449 if (w->vscroll)
13451 w->vscroll = 0;
13452 clear_glyph_matrix (w->desired_matrix);
13453 goto recenter;
13456 /* If centering point failed to make the whole line visible,
13457 put point at the top instead. That has to make the whole line
13458 visible, if it can be done. */
13459 if (centering_position == 0)
13460 goto done;
13462 clear_glyph_matrix (w->desired_matrix);
13463 centering_position = 0;
13464 goto recenter;
13467 done:
13469 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13470 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13471 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13472 ? Qt : Qnil);
13474 /* Display the mode line, if we must. */
13475 if ((update_mode_line
13476 /* If window not full width, must redo its mode line
13477 if (a) the window to its side is being redone and
13478 (b) we do a frame-based redisplay. This is a consequence
13479 of how inverted lines are drawn in frame-based redisplay. */
13480 || (!just_this_one_p
13481 && !FRAME_WINDOW_P (f)
13482 && !WINDOW_FULL_WIDTH_P (w))
13483 /* Line number to display. */
13484 || INTEGERP (w->base_line_pos)
13485 /* Column number is displayed and different from the one displayed. */
13486 || (!NILP (w->column_number_displayed)
13487 && (XFASTINT (w->column_number_displayed)
13488 != (int) current_column ()))) /* iftc */
13489 /* This means that the window has a mode line. */
13490 && (WINDOW_WANTS_MODELINE_P (w)
13491 || WINDOW_WANTS_HEADER_LINE_P (w)))
13493 display_mode_lines (w);
13495 /* If mode line height has changed, arrange for a thorough
13496 immediate redisplay using the correct mode line height. */
13497 if (WINDOW_WANTS_MODELINE_P (w)
13498 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13500 fonts_changed_p = 1;
13501 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13502 = DESIRED_MODE_LINE_HEIGHT (w);
13505 /* If top line height has changed, arrange for a thorough
13506 immediate redisplay using the correct mode line height. */
13507 if (WINDOW_WANTS_HEADER_LINE_P (w)
13508 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13510 fonts_changed_p = 1;
13511 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13512 = DESIRED_HEADER_LINE_HEIGHT (w);
13515 if (fonts_changed_p)
13516 goto need_larger_matrices;
13519 if (!line_number_displayed
13520 && !BUFFERP (w->base_line_pos))
13522 w->base_line_pos = Qnil;
13523 w->base_line_number = Qnil;
13526 finish_menu_bars:
13528 /* When we reach a frame's selected window, redo the frame's menu bar. */
13529 if (update_mode_line
13530 && EQ (FRAME_SELECTED_WINDOW (f), window))
13532 int redisplay_menu_p = 0;
13533 int redisplay_tool_bar_p = 0;
13535 if (FRAME_WINDOW_P (f))
13537 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13538 || defined (USE_GTK)
13539 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13540 #else
13541 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13542 #endif
13544 else
13545 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13547 if (redisplay_menu_p)
13548 display_menu_bar (w);
13550 #ifdef HAVE_WINDOW_SYSTEM
13551 #ifdef USE_GTK
13552 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13553 #else
13554 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13555 && (FRAME_TOOL_BAR_LINES (f) > 0
13556 || !NILP (Vauto_resize_tool_bars));
13558 #endif
13560 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13562 extern int ignore_mouse_drag_p;
13563 ignore_mouse_drag_p = 1;
13565 #endif
13568 #ifdef HAVE_WINDOW_SYSTEM
13569 if (FRAME_WINDOW_P (f)
13570 && update_window_fringes (w, (just_this_one_p
13571 || (!used_current_matrix_p && !overlay_arrow_seen)
13572 || w->pseudo_window_p)))
13574 update_begin (f);
13575 BLOCK_INPUT;
13576 if (draw_window_fringes (w, 1))
13577 x_draw_vertical_border (w);
13578 UNBLOCK_INPUT;
13579 update_end (f);
13581 #endif /* HAVE_WINDOW_SYSTEM */
13583 /* We go to this label, with fonts_changed_p nonzero,
13584 if it is necessary to try again using larger glyph matrices.
13585 We have to redeem the scroll bar even in this case,
13586 because the loop in redisplay_internal expects that. */
13587 need_larger_matrices:
13589 finish_scroll_bars:
13591 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13593 /* Set the thumb's position and size. */
13594 set_vertical_scroll_bar (w);
13596 /* Note that we actually used the scroll bar attached to this
13597 window, so it shouldn't be deleted at the end of redisplay. */
13598 redeem_scroll_bar_hook (w);
13601 /* Restore current_buffer and value of point in it. */
13602 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13603 set_buffer_internal_1 (old);
13604 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13605 shorter. This can be caused by log truncation in *Messages*. */
13606 if (CHARPOS (lpoint) <= ZV)
13607 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13609 unbind_to (count, Qnil);
13613 /* Build the complete desired matrix of WINDOW with a window start
13614 buffer position POS.
13616 Value is 1 if successful. It is zero if fonts were loaded during
13617 redisplay which makes re-adjusting glyph matrices necessary, and -1
13618 if point would appear in the scroll margins.
13619 (We check that only if CHECK_MARGINS is nonzero. */
13622 try_window (window, pos, check_margins)
13623 Lisp_Object window;
13624 struct text_pos pos;
13625 int check_margins;
13627 struct window *w = XWINDOW (window);
13628 struct it it;
13629 struct glyph_row *last_text_row = NULL;
13630 struct frame *f = XFRAME (w->frame);
13632 /* Make POS the new window start. */
13633 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13635 /* Mark cursor position as unknown. No overlay arrow seen. */
13636 w->cursor.vpos = -1;
13637 overlay_arrow_seen = 0;
13639 /* Initialize iterator and info to start at POS. */
13640 start_display (&it, w, pos);
13642 /* Display all lines of W. */
13643 while (it.current_y < it.last_visible_y)
13645 if (display_line (&it))
13646 last_text_row = it.glyph_row - 1;
13647 if (fonts_changed_p)
13648 return 0;
13651 /* Don't let the cursor end in the scroll margins. */
13652 if (check_margins
13653 && !MINI_WINDOW_P (w))
13655 int this_scroll_margin;
13657 this_scroll_margin = max (0, scroll_margin);
13658 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13659 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13661 if ((w->cursor.y >= 0 /* not vscrolled */
13662 && w->cursor.y < this_scroll_margin
13663 && CHARPOS (pos) > BEGV
13664 && IT_CHARPOS (it) < ZV)
13665 /* rms: considering make_cursor_line_fully_visible_p here
13666 seems to give wrong results. We don't want to recenter
13667 when the last line is partly visible, we want to allow
13668 that case to be handled in the usual way. */
13669 || (w->cursor.y + 1) > it.last_visible_y)
13671 w->cursor.vpos = -1;
13672 clear_glyph_matrix (w->desired_matrix);
13673 return -1;
13677 /* If bottom moved off end of frame, change mode line percentage. */
13678 if (XFASTINT (w->window_end_pos) <= 0
13679 && Z != IT_CHARPOS (it))
13680 w->update_mode_line = Qt;
13682 /* Set window_end_pos to the offset of the last character displayed
13683 on the window from the end of current_buffer. Set
13684 window_end_vpos to its row number. */
13685 if (last_text_row)
13687 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13688 w->window_end_bytepos
13689 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13690 w->window_end_pos
13691 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13692 w->window_end_vpos
13693 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13694 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13695 ->displays_text_p);
13697 else
13699 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13700 w->window_end_pos = make_number (Z - ZV);
13701 w->window_end_vpos = make_number (0);
13704 /* But that is not valid info until redisplay finishes. */
13705 w->window_end_valid = Qnil;
13706 return 1;
13711 /************************************************************************
13712 Window redisplay reusing current matrix when buffer has not changed
13713 ************************************************************************/
13715 /* Try redisplay of window W showing an unchanged buffer with a
13716 different window start than the last time it was displayed by
13717 reusing its current matrix. Value is non-zero if successful.
13718 W->start is the new window start. */
13720 static int
13721 try_window_reusing_current_matrix (w)
13722 struct window *w;
13724 struct frame *f = XFRAME (w->frame);
13725 struct glyph_row *row, *bottom_row;
13726 struct it it;
13727 struct run run;
13728 struct text_pos start, new_start;
13729 int nrows_scrolled, i;
13730 struct glyph_row *last_text_row;
13731 struct glyph_row *last_reused_text_row;
13732 struct glyph_row *start_row;
13733 int start_vpos, min_y, max_y;
13735 #if GLYPH_DEBUG
13736 if (inhibit_try_window_reusing)
13737 return 0;
13738 #endif
13740 if (/* This function doesn't handle terminal frames. */
13741 !FRAME_WINDOW_P (f)
13742 /* Don't try to reuse the display if windows have been split
13743 or such. */
13744 || windows_or_buffers_changed
13745 || cursor_type_changed)
13746 return 0;
13748 /* Can't do this if region may have changed. */
13749 if ((!NILP (Vtransient_mark_mode)
13750 && !NILP (current_buffer->mark_active))
13751 || !NILP (w->region_showing)
13752 || !NILP (Vshow_trailing_whitespace))
13753 return 0;
13755 /* If top-line visibility has changed, give up. */
13756 if (WINDOW_WANTS_HEADER_LINE_P (w)
13757 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13758 return 0;
13760 /* Give up if old or new display is scrolled vertically. We could
13761 make this function handle this, but right now it doesn't. */
13762 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13763 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13764 return 0;
13766 /* The variable new_start now holds the new window start. The old
13767 start `start' can be determined from the current matrix. */
13768 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13769 start = start_row->start.pos;
13770 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13772 /* Clear the desired matrix for the display below. */
13773 clear_glyph_matrix (w->desired_matrix);
13775 if (CHARPOS (new_start) <= CHARPOS (start))
13777 int first_row_y;
13779 /* Don't use this method if the display starts with an ellipsis
13780 displayed for invisible text. It's not easy to handle that case
13781 below, and it's certainly not worth the effort since this is
13782 not a frequent case. */
13783 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13784 return 0;
13786 IF_DEBUG (debug_method_add (w, "twu1"));
13788 /* Display up to a row that can be reused. The variable
13789 last_text_row is set to the last row displayed that displays
13790 text. Note that it.vpos == 0 if or if not there is a
13791 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13792 start_display (&it, w, new_start);
13793 first_row_y = it.current_y;
13794 w->cursor.vpos = -1;
13795 last_text_row = last_reused_text_row = NULL;
13797 while (it.current_y < it.last_visible_y
13798 && !fonts_changed_p)
13800 /* If we have reached into the characters in the START row,
13801 that means the line boundaries have changed. So we
13802 can't start copying with the row START. Maybe it will
13803 work to start copying with the following row. */
13804 while (IT_CHARPOS (it) > CHARPOS (start))
13806 /* Advance to the next row as the "start". */
13807 start_row++;
13808 start = start_row->start.pos;
13809 /* If there are no more rows to try, or just one, give up. */
13810 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13811 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13812 || CHARPOS (start) == ZV)
13814 clear_glyph_matrix (w->desired_matrix);
13815 return 0;
13818 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13820 /* If we have reached alignment,
13821 we can copy the rest of the rows. */
13822 if (IT_CHARPOS (it) == CHARPOS (start))
13823 break;
13825 if (display_line (&it))
13826 last_text_row = it.glyph_row - 1;
13829 /* A value of current_y < last_visible_y means that we stopped
13830 at the previous window start, which in turn means that we
13831 have at least one reusable row. */
13832 if (it.current_y < it.last_visible_y)
13834 /* IT.vpos always starts from 0; it counts text lines. */
13835 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13837 /* Find PT if not already found in the lines displayed. */
13838 if (w->cursor.vpos < 0)
13840 int dy = it.current_y - start_row->y;
13842 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13843 row = row_containing_pos (w, PT, row, NULL, dy);
13844 if (row)
13845 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13846 dy, nrows_scrolled);
13847 else
13849 clear_glyph_matrix (w->desired_matrix);
13850 return 0;
13854 /* Scroll the display. Do it before the current matrix is
13855 changed. The problem here is that update has not yet
13856 run, i.e. part of the current matrix is not up to date.
13857 scroll_run_hook will clear the cursor, and use the
13858 current matrix to get the height of the row the cursor is
13859 in. */
13860 run.current_y = start_row->y;
13861 run.desired_y = it.current_y;
13862 run.height = it.last_visible_y - it.current_y;
13864 if (run.height > 0 && run.current_y != run.desired_y)
13866 update_begin (f);
13867 rif->update_window_begin_hook (w);
13868 rif->clear_window_mouse_face (w);
13869 rif->scroll_run_hook (w, &run);
13870 rif->update_window_end_hook (w, 0, 0);
13871 update_end (f);
13874 /* Shift current matrix down by nrows_scrolled lines. */
13875 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13876 rotate_matrix (w->current_matrix,
13877 start_vpos,
13878 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13879 nrows_scrolled);
13881 /* Disable lines that must be updated. */
13882 for (i = 0; i < nrows_scrolled; ++i)
13883 (start_row + i)->enabled_p = 0;
13885 /* Re-compute Y positions. */
13886 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13887 max_y = it.last_visible_y;
13888 for (row = start_row + nrows_scrolled;
13889 row < bottom_row;
13890 ++row)
13892 row->y = it.current_y;
13893 row->visible_height = row->height;
13895 if (row->y < min_y)
13896 row->visible_height -= min_y - row->y;
13897 if (row->y + row->height > max_y)
13898 row->visible_height -= row->y + row->height - max_y;
13899 row->redraw_fringe_bitmaps_p = 1;
13901 it.current_y += row->height;
13903 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13904 last_reused_text_row = row;
13905 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13906 break;
13909 /* Disable lines in the current matrix which are now
13910 below the window. */
13911 for (++row; row < bottom_row; ++row)
13912 row->enabled_p = row->mode_line_p = 0;
13915 /* Update window_end_pos etc.; last_reused_text_row is the last
13916 reused row from the current matrix containing text, if any.
13917 The value of last_text_row is the last displayed line
13918 containing text. */
13919 if (last_reused_text_row)
13921 w->window_end_bytepos
13922 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13923 w->window_end_pos
13924 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13925 w->window_end_vpos
13926 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13927 w->current_matrix));
13929 else if (last_text_row)
13931 w->window_end_bytepos
13932 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13933 w->window_end_pos
13934 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13935 w->window_end_vpos
13936 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13938 else
13940 /* This window must be completely empty. */
13941 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13942 w->window_end_pos = make_number (Z - ZV);
13943 w->window_end_vpos = make_number (0);
13945 w->window_end_valid = Qnil;
13947 /* Update hint: don't try scrolling again in update_window. */
13948 w->desired_matrix->no_scrolling_p = 1;
13950 #if GLYPH_DEBUG
13951 debug_method_add (w, "try_window_reusing_current_matrix 1");
13952 #endif
13953 return 1;
13955 else if (CHARPOS (new_start) > CHARPOS (start))
13957 struct glyph_row *pt_row, *row;
13958 struct glyph_row *first_reusable_row;
13959 struct glyph_row *first_row_to_display;
13960 int dy;
13961 int yb = window_text_bottom_y (w);
13963 /* Find the row starting at new_start, if there is one. Don't
13964 reuse a partially visible line at the end. */
13965 first_reusable_row = start_row;
13966 while (first_reusable_row->enabled_p
13967 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13968 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13969 < CHARPOS (new_start)))
13970 ++first_reusable_row;
13972 /* Give up if there is no row to reuse. */
13973 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13974 || !first_reusable_row->enabled_p
13975 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13976 != CHARPOS (new_start)))
13977 return 0;
13979 /* We can reuse fully visible rows beginning with
13980 first_reusable_row to the end of the window. Set
13981 first_row_to_display to the first row that cannot be reused.
13982 Set pt_row to the row containing point, if there is any. */
13983 pt_row = NULL;
13984 for (first_row_to_display = first_reusable_row;
13985 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13986 ++first_row_to_display)
13988 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13989 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13990 pt_row = first_row_to_display;
13993 /* Start displaying at the start of first_row_to_display. */
13994 xassert (first_row_to_display->y < yb);
13995 init_to_row_start (&it, w, first_row_to_display);
13997 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13998 - start_vpos);
13999 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14000 - nrows_scrolled);
14001 it.current_y = (first_row_to_display->y - first_reusable_row->y
14002 + WINDOW_HEADER_LINE_HEIGHT (w));
14004 /* Display lines beginning with first_row_to_display in the
14005 desired matrix. Set last_text_row to the last row displayed
14006 that displays text. */
14007 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14008 if (pt_row == NULL)
14009 w->cursor.vpos = -1;
14010 last_text_row = NULL;
14011 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14012 if (display_line (&it))
14013 last_text_row = it.glyph_row - 1;
14015 /* Give up If point isn't in a row displayed or reused. */
14016 if (w->cursor.vpos < 0)
14018 clear_glyph_matrix (w->desired_matrix);
14019 return 0;
14022 /* If point is in a reused row, adjust y and vpos of the cursor
14023 position. */
14024 if (pt_row)
14026 w->cursor.vpos -= nrows_scrolled;
14027 w->cursor.y -= first_reusable_row->y - start_row->y;
14030 /* Scroll the display. */
14031 run.current_y = first_reusable_row->y;
14032 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14033 run.height = it.last_visible_y - run.current_y;
14034 dy = run.current_y - run.desired_y;
14036 if (run.height)
14038 update_begin (f);
14039 rif->update_window_begin_hook (w);
14040 rif->clear_window_mouse_face (w);
14041 rif->scroll_run_hook (w, &run);
14042 rif->update_window_end_hook (w, 0, 0);
14043 update_end (f);
14046 /* Adjust Y positions of reused rows. */
14047 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14048 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14049 max_y = it.last_visible_y;
14050 for (row = first_reusable_row; row < first_row_to_display; ++row)
14052 row->y -= dy;
14053 row->visible_height = row->height;
14054 if (row->y < min_y)
14055 row->visible_height -= min_y - row->y;
14056 if (row->y + row->height > max_y)
14057 row->visible_height -= row->y + row->height - max_y;
14058 row->redraw_fringe_bitmaps_p = 1;
14061 /* Scroll the current matrix. */
14062 xassert (nrows_scrolled > 0);
14063 rotate_matrix (w->current_matrix,
14064 start_vpos,
14065 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14066 -nrows_scrolled);
14068 /* Disable rows not reused. */
14069 for (row -= nrows_scrolled; row < bottom_row; ++row)
14070 row->enabled_p = 0;
14072 /* Point may have moved to a different line, so we cannot assume that
14073 the previous cursor position is valid; locate the correct row. */
14074 if (pt_row)
14076 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14077 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14078 row++)
14080 w->cursor.vpos++;
14081 w->cursor.y = row->y;
14083 if (row < bottom_row)
14085 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14086 while (glyph->charpos < PT)
14088 w->cursor.hpos++;
14089 w->cursor.x += glyph->pixel_width;
14090 glyph++;
14095 /* Adjust window end. A null value of last_text_row means that
14096 the window end is in reused rows which in turn means that
14097 only its vpos can have changed. */
14098 if (last_text_row)
14100 w->window_end_bytepos
14101 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14102 w->window_end_pos
14103 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14104 w->window_end_vpos
14105 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14107 else
14109 w->window_end_vpos
14110 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14113 w->window_end_valid = Qnil;
14114 w->desired_matrix->no_scrolling_p = 1;
14116 #if GLYPH_DEBUG
14117 debug_method_add (w, "try_window_reusing_current_matrix 2");
14118 #endif
14119 return 1;
14122 return 0;
14127 /************************************************************************
14128 Window redisplay reusing current matrix when buffer has changed
14129 ************************************************************************/
14131 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14132 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14133 int *, int *));
14134 static struct glyph_row *
14135 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14136 struct glyph_row *));
14139 /* Return the last row in MATRIX displaying text. If row START is
14140 non-null, start searching with that row. IT gives the dimensions
14141 of the display. Value is null if matrix is empty; otherwise it is
14142 a pointer to the row found. */
14144 static struct glyph_row *
14145 find_last_row_displaying_text (matrix, it, start)
14146 struct glyph_matrix *matrix;
14147 struct it *it;
14148 struct glyph_row *start;
14150 struct glyph_row *row, *row_found;
14152 /* Set row_found to the last row in IT->w's current matrix
14153 displaying text. The loop looks funny but think of partially
14154 visible lines. */
14155 row_found = NULL;
14156 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14157 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14159 xassert (row->enabled_p);
14160 row_found = row;
14161 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14162 break;
14163 ++row;
14166 return row_found;
14170 /* Return the last row in the current matrix of W that is not affected
14171 by changes at the start of current_buffer that occurred since W's
14172 current matrix was built. Value is null if no such row exists.
14174 BEG_UNCHANGED us the number of characters unchanged at the start of
14175 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14176 first changed character in current_buffer. Characters at positions <
14177 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14178 when the current matrix was built. */
14180 static struct glyph_row *
14181 find_last_unchanged_at_beg_row (w)
14182 struct window *w;
14184 int first_changed_pos = BEG + BEG_UNCHANGED;
14185 struct glyph_row *row;
14186 struct glyph_row *row_found = NULL;
14187 int yb = window_text_bottom_y (w);
14189 /* Find the last row displaying unchanged text. */
14190 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14191 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14192 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14194 if (/* If row ends before first_changed_pos, it is unchanged,
14195 except in some case. */
14196 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14197 /* When row ends in ZV and we write at ZV it is not
14198 unchanged. */
14199 && !row->ends_at_zv_p
14200 /* When first_changed_pos is the end of a continued line,
14201 row is not unchanged because it may be no longer
14202 continued. */
14203 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14204 && (row->continued_p
14205 || row->exact_window_width_line_p)))
14206 row_found = row;
14208 /* Stop if last visible row. */
14209 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14210 break;
14212 ++row;
14215 return row_found;
14219 /* Find the first glyph row in the current matrix of W that is not
14220 affected by changes at the end of current_buffer since the
14221 time W's current matrix was built.
14223 Return in *DELTA the number of chars by which buffer positions in
14224 unchanged text at the end of current_buffer must be adjusted.
14226 Return in *DELTA_BYTES the corresponding number of bytes.
14228 Value is null if no such row exists, i.e. all rows are affected by
14229 changes. */
14231 static struct glyph_row *
14232 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14233 struct window *w;
14234 int *delta, *delta_bytes;
14236 struct glyph_row *row;
14237 struct glyph_row *row_found = NULL;
14239 *delta = *delta_bytes = 0;
14241 /* Display must not have been paused, otherwise the current matrix
14242 is not up to date. */
14243 if (NILP (w->window_end_valid))
14244 abort ();
14246 /* A value of window_end_pos >= END_UNCHANGED means that the window
14247 end is in the range of changed text. If so, there is no
14248 unchanged row at the end of W's current matrix. */
14249 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14250 return NULL;
14252 /* Set row to the last row in W's current matrix displaying text. */
14253 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14255 /* If matrix is entirely empty, no unchanged row exists. */
14256 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14258 /* The value of row is the last glyph row in the matrix having a
14259 meaningful buffer position in it. The end position of row
14260 corresponds to window_end_pos. This allows us to translate
14261 buffer positions in the current matrix to current buffer
14262 positions for characters not in changed text. */
14263 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14264 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14265 int last_unchanged_pos, last_unchanged_pos_old;
14266 struct glyph_row *first_text_row
14267 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14269 *delta = Z - Z_old;
14270 *delta_bytes = Z_BYTE - Z_BYTE_old;
14272 /* Set last_unchanged_pos to the buffer position of the last
14273 character in the buffer that has not been changed. Z is the
14274 index + 1 of the last character in current_buffer, i.e. by
14275 subtracting END_UNCHANGED we get the index of the last
14276 unchanged character, and we have to add BEG to get its buffer
14277 position. */
14278 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14279 last_unchanged_pos_old = last_unchanged_pos - *delta;
14281 /* Search backward from ROW for a row displaying a line that
14282 starts at a minimum position >= last_unchanged_pos_old. */
14283 for (; row > first_text_row; --row)
14285 /* This used to abort, but it can happen.
14286 It is ok to just stop the search instead here. KFS. */
14287 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14288 break;
14290 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14291 row_found = row;
14295 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14296 abort ();
14298 return row_found;
14302 /* Make sure that glyph rows in the current matrix of window W
14303 reference the same glyph memory as corresponding rows in the
14304 frame's frame matrix. This function is called after scrolling W's
14305 current matrix on a terminal frame in try_window_id and
14306 try_window_reusing_current_matrix. */
14308 static void
14309 sync_frame_with_window_matrix_rows (w)
14310 struct window *w;
14312 struct frame *f = XFRAME (w->frame);
14313 struct glyph_row *window_row, *window_row_end, *frame_row;
14315 /* Preconditions: W must be a leaf window and full-width. Its frame
14316 must have a frame matrix. */
14317 xassert (NILP (w->hchild) && NILP (w->vchild));
14318 xassert (WINDOW_FULL_WIDTH_P (w));
14319 xassert (!FRAME_WINDOW_P (f));
14321 /* If W is a full-width window, glyph pointers in W's current matrix
14322 have, by definition, to be the same as glyph pointers in the
14323 corresponding frame matrix. Note that frame matrices have no
14324 marginal areas (see build_frame_matrix). */
14325 window_row = w->current_matrix->rows;
14326 window_row_end = window_row + w->current_matrix->nrows;
14327 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14328 while (window_row < window_row_end)
14330 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14331 struct glyph *end = window_row->glyphs[LAST_AREA];
14333 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14334 frame_row->glyphs[TEXT_AREA] = start;
14335 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14336 frame_row->glyphs[LAST_AREA] = end;
14338 /* Disable frame rows whose corresponding window rows have
14339 been disabled in try_window_id. */
14340 if (!window_row->enabled_p)
14341 frame_row->enabled_p = 0;
14343 ++window_row, ++frame_row;
14348 /* Find the glyph row in window W containing CHARPOS. Consider all
14349 rows between START and END (not inclusive). END null means search
14350 all rows to the end of the display area of W. Value is the row
14351 containing CHARPOS or null. */
14353 struct glyph_row *
14354 row_containing_pos (w, charpos, start, end, dy)
14355 struct window *w;
14356 int charpos;
14357 struct glyph_row *start, *end;
14358 int dy;
14360 struct glyph_row *row = start;
14361 int last_y;
14363 /* If we happen to start on a header-line, skip that. */
14364 if (row->mode_line_p)
14365 ++row;
14367 if ((end && row >= end) || !row->enabled_p)
14368 return NULL;
14370 last_y = window_text_bottom_y (w) - dy;
14372 while (1)
14374 /* Give up if we have gone too far. */
14375 if (end && row >= end)
14376 return NULL;
14377 /* This formerly returned if they were equal.
14378 I think that both quantities are of a "last plus one" type;
14379 if so, when they are equal, the row is within the screen. -- rms. */
14380 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14381 return NULL;
14383 /* If it is in this row, return this row. */
14384 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14385 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14386 /* The end position of a row equals the start
14387 position of the next row. If CHARPOS is there, we
14388 would rather display it in the next line, except
14389 when this line ends in ZV. */
14390 && !row->ends_at_zv_p
14391 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14392 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14393 return row;
14394 ++row;
14399 /* Try to redisplay window W by reusing its existing display. W's
14400 current matrix must be up to date when this function is called,
14401 i.e. window_end_valid must not be nil.
14403 Value is
14405 1 if display has been updated
14406 0 if otherwise unsuccessful
14407 -1 if redisplay with same window start is known not to succeed
14409 The following steps are performed:
14411 1. Find the last row in the current matrix of W that is not
14412 affected by changes at the start of current_buffer. If no such row
14413 is found, give up.
14415 2. Find the first row in W's current matrix that is not affected by
14416 changes at the end of current_buffer. Maybe there is no such row.
14418 3. Display lines beginning with the row + 1 found in step 1 to the
14419 row found in step 2 or, if step 2 didn't find a row, to the end of
14420 the window.
14422 4. If cursor is not known to appear on the window, give up.
14424 5. If display stopped at the row found in step 2, scroll the
14425 display and current matrix as needed.
14427 6. Maybe display some lines at the end of W, if we must. This can
14428 happen under various circumstances, like a partially visible line
14429 becoming fully visible, or because newly displayed lines are displayed
14430 in smaller font sizes.
14432 7. Update W's window end information. */
14434 static int
14435 try_window_id (w)
14436 struct window *w;
14438 struct frame *f = XFRAME (w->frame);
14439 struct glyph_matrix *current_matrix = w->current_matrix;
14440 struct glyph_matrix *desired_matrix = w->desired_matrix;
14441 struct glyph_row *last_unchanged_at_beg_row;
14442 struct glyph_row *first_unchanged_at_end_row;
14443 struct glyph_row *row;
14444 struct glyph_row *bottom_row;
14445 int bottom_vpos;
14446 struct it it;
14447 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14448 struct text_pos start_pos;
14449 struct run run;
14450 int first_unchanged_at_end_vpos = 0;
14451 struct glyph_row *last_text_row, *last_text_row_at_end;
14452 struct text_pos start;
14453 int first_changed_charpos, last_changed_charpos;
14455 #if GLYPH_DEBUG
14456 if (inhibit_try_window_id)
14457 return 0;
14458 #endif
14460 /* This is handy for debugging. */
14461 #if 0
14462 #define GIVE_UP(X) \
14463 do { \
14464 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14465 return 0; \
14466 } while (0)
14467 #else
14468 #define GIVE_UP(X) return 0
14469 #endif
14471 SET_TEXT_POS_FROM_MARKER (start, w->start);
14473 /* Don't use this for mini-windows because these can show
14474 messages and mini-buffers, and we don't handle that here. */
14475 if (MINI_WINDOW_P (w))
14476 GIVE_UP (1);
14478 /* This flag is used to prevent redisplay optimizations. */
14479 if (windows_or_buffers_changed || cursor_type_changed)
14480 GIVE_UP (2);
14482 /* Verify that narrowing has not changed.
14483 Also verify that we were not told to prevent redisplay optimizations.
14484 It would be nice to further
14485 reduce the number of cases where this prevents try_window_id. */
14486 if (current_buffer->clip_changed
14487 || current_buffer->prevent_redisplay_optimizations_p)
14488 GIVE_UP (3);
14490 /* Window must either use window-based redisplay or be full width. */
14491 if (!FRAME_WINDOW_P (f)
14492 && (!line_ins_del_ok
14493 || !WINDOW_FULL_WIDTH_P (w)))
14494 GIVE_UP (4);
14496 /* Give up if point is not known NOT to appear in W. */
14497 if (PT < CHARPOS (start))
14498 GIVE_UP (5);
14500 /* Another way to prevent redisplay optimizations. */
14501 if (XFASTINT (w->last_modified) == 0)
14502 GIVE_UP (6);
14504 /* Verify that window is not hscrolled. */
14505 if (XFASTINT (w->hscroll) != 0)
14506 GIVE_UP (7);
14508 /* Verify that display wasn't paused. */
14509 if (NILP (w->window_end_valid))
14510 GIVE_UP (8);
14512 /* Can't use this if highlighting a region because a cursor movement
14513 will do more than just set the cursor. */
14514 if (!NILP (Vtransient_mark_mode)
14515 && !NILP (current_buffer->mark_active))
14516 GIVE_UP (9);
14518 /* Likewise if highlighting trailing whitespace. */
14519 if (!NILP (Vshow_trailing_whitespace))
14520 GIVE_UP (11);
14522 /* Likewise if showing a region. */
14523 if (!NILP (w->region_showing))
14524 GIVE_UP (10);
14526 /* Can use this if overlay arrow position and or string have changed. */
14527 if (overlay_arrows_changed_p ())
14528 GIVE_UP (12);
14531 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14532 only if buffer has really changed. The reason is that the gap is
14533 initially at Z for freshly visited files. The code below would
14534 set end_unchanged to 0 in that case. */
14535 if (MODIFF > SAVE_MODIFF
14536 /* This seems to happen sometimes after saving a buffer. */
14537 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14539 if (GPT - BEG < BEG_UNCHANGED)
14540 BEG_UNCHANGED = GPT - BEG;
14541 if (Z - GPT < END_UNCHANGED)
14542 END_UNCHANGED = Z - GPT;
14545 /* The position of the first and last character that has been changed. */
14546 first_changed_charpos = BEG + BEG_UNCHANGED;
14547 last_changed_charpos = Z - END_UNCHANGED;
14549 /* If window starts after a line end, and the last change is in
14550 front of that newline, then changes don't affect the display.
14551 This case happens with stealth-fontification. Note that although
14552 the display is unchanged, glyph positions in the matrix have to
14553 be adjusted, of course. */
14554 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14555 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14556 && ((last_changed_charpos < CHARPOS (start)
14557 && CHARPOS (start) == BEGV)
14558 || (last_changed_charpos < CHARPOS (start) - 1
14559 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14561 int Z_old, delta, Z_BYTE_old, delta_bytes;
14562 struct glyph_row *r0;
14564 /* Compute how many chars/bytes have been added to or removed
14565 from the buffer. */
14566 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14567 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14568 delta = Z - Z_old;
14569 delta_bytes = Z_BYTE - Z_BYTE_old;
14571 /* Give up if PT is not in the window. Note that it already has
14572 been checked at the start of try_window_id that PT is not in
14573 front of the window start. */
14574 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14575 GIVE_UP (13);
14577 /* If window start is unchanged, we can reuse the whole matrix
14578 as is, after adjusting glyph positions. No need to compute
14579 the window end again, since its offset from Z hasn't changed. */
14580 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14581 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14582 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14583 /* PT must not be in a partially visible line. */
14584 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14585 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14587 /* Adjust positions in the glyph matrix. */
14588 if (delta || delta_bytes)
14590 struct glyph_row *r1
14591 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14592 increment_matrix_positions (w->current_matrix,
14593 MATRIX_ROW_VPOS (r0, current_matrix),
14594 MATRIX_ROW_VPOS (r1, current_matrix),
14595 delta, delta_bytes);
14598 /* Set the cursor. */
14599 row = row_containing_pos (w, PT, r0, NULL, 0);
14600 if (row)
14601 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14602 else
14603 abort ();
14604 return 1;
14608 /* Handle the case that changes are all below what is displayed in
14609 the window, and that PT is in the window. This shortcut cannot
14610 be taken if ZV is visible in the window, and text has been added
14611 there that is visible in the window. */
14612 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14613 /* ZV is not visible in the window, or there are no
14614 changes at ZV, actually. */
14615 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14616 || first_changed_charpos == last_changed_charpos))
14618 struct glyph_row *r0;
14620 /* Give up if PT is not in the window. Note that it already has
14621 been checked at the start of try_window_id that PT is not in
14622 front of the window start. */
14623 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14624 GIVE_UP (14);
14626 /* If window start is unchanged, we can reuse the whole matrix
14627 as is, without changing glyph positions since no text has
14628 been added/removed in front of the window end. */
14629 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14630 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14631 /* PT must not be in a partially visible line. */
14632 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14633 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14635 /* We have to compute the window end anew since text
14636 can have been added/removed after it. */
14637 w->window_end_pos
14638 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14639 w->window_end_bytepos
14640 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14642 /* Set the cursor. */
14643 row = row_containing_pos (w, PT, r0, NULL, 0);
14644 if (row)
14645 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14646 else
14647 abort ();
14648 return 2;
14652 /* Give up if window start is in the changed area.
14654 The condition used to read
14656 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14658 but why that was tested escapes me at the moment. */
14659 if (CHARPOS (start) >= first_changed_charpos
14660 && CHARPOS (start) <= last_changed_charpos)
14661 GIVE_UP (15);
14663 /* Check that window start agrees with the start of the first glyph
14664 row in its current matrix. Check this after we know the window
14665 start is not in changed text, otherwise positions would not be
14666 comparable. */
14667 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14668 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14669 GIVE_UP (16);
14671 /* Give up if the window ends in strings. Overlay strings
14672 at the end are difficult to handle, so don't try. */
14673 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14674 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14675 GIVE_UP (20);
14677 /* Compute the position at which we have to start displaying new
14678 lines. Some of the lines at the top of the window might be
14679 reusable because they are not displaying changed text. Find the
14680 last row in W's current matrix not affected by changes at the
14681 start of current_buffer. Value is null if changes start in the
14682 first line of window. */
14683 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14684 if (last_unchanged_at_beg_row)
14686 /* Avoid starting to display in the moddle of a character, a TAB
14687 for instance. This is easier than to set up the iterator
14688 exactly, and it's not a frequent case, so the additional
14689 effort wouldn't really pay off. */
14690 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14691 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14692 && last_unchanged_at_beg_row > w->current_matrix->rows)
14693 --last_unchanged_at_beg_row;
14695 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14696 GIVE_UP (17);
14698 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14699 GIVE_UP (18);
14700 start_pos = it.current.pos;
14702 /* Start displaying new lines in the desired matrix at the same
14703 vpos we would use in the current matrix, i.e. below
14704 last_unchanged_at_beg_row. */
14705 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14706 current_matrix);
14707 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14708 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14710 xassert (it.hpos == 0 && it.current_x == 0);
14712 else
14714 /* There are no reusable lines at the start of the window.
14715 Start displaying in the first text line. */
14716 start_display (&it, w, start);
14717 it.vpos = it.first_vpos;
14718 start_pos = it.current.pos;
14721 /* Find the first row that is not affected by changes at the end of
14722 the buffer. Value will be null if there is no unchanged row, in
14723 which case we must redisplay to the end of the window. delta
14724 will be set to the value by which buffer positions beginning with
14725 first_unchanged_at_end_row have to be adjusted due to text
14726 changes. */
14727 first_unchanged_at_end_row
14728 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14729 IF_DEBUG (debug_delta = delta);
14730 IF_DEBUG (debug_delta_bytes = delta_bytes);
14732 /* Set stop_pos to the buffer position up to which we will have to
14733 display new lines. If first_unchanged_at_end_row != NULL, this
14734 is the buffer position of the start of the line displayed in that
14735 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14736 that we don't stop at a buffer position. */
14737 stop_pos = 0;
14738 if (first_unchanged_at_end_row)
14740 xassert (last_unchanged_at_beg_row == NULL
14741 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14743 /* If this is a continuation line, move forward to the next one
14744 that isn't. Changes in lines above affect this line.
14745 Caution: this may move first_unchanged_at_end_row to a row
14746 not displaying text. */
14747 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14748 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14749 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14750 < it.last_visible_y))
14751 ++first_unchanged_at_end_row;
14753 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14754 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14755 >= it.last_visible_y))
14756 first_unchanged_at_end_row = NULL;
14757 else
14759 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14760 + delta);
14761 first_unchanged_at_end_vpos
14762 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14763 xassert (stop_pos >= Z - END_UNCHANGED);
14766 else if (last_unchanged_at_beg_row == NULL)
14767 GIVE_UP (19);
14770 #if GLYPH_DEBUG
14772 /* Either there is no unchanged row at the end, or the one we have
14773 now displays text. This is a necessary condition for the window
14774 end pos calculation at the end of this function. */
14775 xassert (first_unchanged_at_end_row == NULL
14776 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14778 debug_last_unchanged_at_beg_vpos
14779 = (last_unchanged_at_beg_row
14780 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14781 : -1);
14782 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14784 #endif /* GLYPH_DEBUG != 0 */
14787 /* Display new lines. Set last_text_row to the last new line
14788 displayed which has text on it, i.e. might end up as being the
14789 line where the window_end_vpos is. */
14790 w->cursor.vpos = -1;
14791 last_text_row = NULL;
14792 overlay_arrow_seen = 0;
14793 while (it.current_y < it.last_visible_y
14794 && !fonts_changed_p
14795 && (first_unchanged_at_end_row == NULL
14796 || IT_CHARPOS (it) < stop_pos))
14798 if (display_line (&it))
14799 last_text_row = it.glyph_row - 1;
14802 if (fonts_changed_p)
14803 return -1;
14806 /* Compute differences in buffer positions, y-positions etc. for
14807 lines reused at the bottom of the window. Compute what we can
14808 scroll. */
14809 if (first_unchanged_at_end_row
14810 /* No lines reused because we displayed everything up to the
14811 bottom of the window. */
14812 && it.current_y < it.last_visible_y)
14814 dvpos = (it.vpos
14815 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14816 current_matrix));
14817 dy = it.current_y - first_unchanged_at_end_row->y;
14818 run.current_y = first_unchanged_at_end_row->y;
14819 run.desired_y = run.current_y + dy;
14820 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14822 else
14824 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14825 first_unchanged_at_end_row = NULL;
14827 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14830 /* Find the cursor if not already found. We have to decide whether
14831 PT will appear on this window (it sometimes doesn't, but this is
14832 not a very frequent case.) This decision has to be made before
14833 the current matrix is altered. A value of cursor.vpos < 0 means
14834 that PT is either in one of the lines beginning at
14835 first_unchanged_at_end_row or below the window. Don't care for
14836 lines that might be displayed later at the window end; as
14837 mentioned, this is not a frequent case. */
14838 if (w->cursor.vpos < 0)
14840 /* Cursor in unchanged rows at the top? */
14841 if (PT < CHARPOS (start_pos)
14842 && last_unchanged_at_beg_row)
14844 row = row_containing_pos (w, PT,
14845 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14846 last_unchanged_at_beg_row + 1, 0);
14847 if (row)
14848 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14851 /* Start from first_unchanged_at_end_row looking for PT. */
14852 else if (first_unchanged_at_end_row)
14854 row = row_containing_pos (w, PT - delta,
14855 first_unchanged_at_end_row, NULL, 0);
14856 if (row)
14857 set_cursor_from_row (w, row, w->current_matrix, delta,
14858 delta_bytes, dy, dvpos);
14861 /* Give up if cursor was not found. */
14862 if (w->cursor.vpos < 0)
14864 clear_glyph_matrix (w->desired_matrix);
14865 return -1;
14869 /* Don't let the cursor end in the scroll margins. */
14871 int this_scroll_margin, cursor_height;
14873 this_scroll_margin = max (0, scroll_margin);
14874 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14875 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14876 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14878 if ((w->cursor.y < this_scroll_margin
14879 && CHARPOS (start) > BEGV)
14880 /* Old redisplay didn't take scroll margin into account at the bottom,
14881 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14882 || (w->cursor.y + (make_cursor_line_fully_visible_p
14883 ? cursor_height + this_scroll_margin
14884 : 1)) > it.last_visible_y)
14886 w->cursor.vpos = -1;
14887 clear_glyph_matrix (w->desired_matrix);
14888 return -1;
14892 /* Scroll the display. Do it before changing the current matrix so
14893 that xterm.c doesn't get confused about where the cursor glyph is
14894 found. */
14895 if (dy && run.height)
14897 update_begin (f);
14899 if (FRAME_WINDOW_P (f))
14901 rif->update_window_begin_hook (w);
14902 rif->clear_window_mouse_face (w);
14903 rif->scroll_run_hook (w, &run);
14904 rif->update_window_end_hook (w, 0, 0);
14906 else
14908 /* Terminal frame. In this case, dvpos gives the number of
14909 lines to scroll by; dvpos < 0 means scroll up. */
14910 int first_unchanged_at_end_vpos
14911 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14912 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14913 int end = (WINDOW_TOP_EDGE_LINE (w)
14914 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14915 + window_internal_height (w));
14917 /* Perform the operation on the screen. */
14918 if (dvpos > 0)
14920 /* Scroll last_unchanged_at_beg_row to the end of the
14921 window down dvpos lines. */
14922 set_terminal_window (end);
14924 /* On dumb terminals delete dvpos lines at the end
14925 before inserting dvpos empty lines. */
14926 if (!scroll_region_ok)
14927 ins_del_lines (end - dvpos, -dvpos);
14929 /* Insert dvpos empty lines in front of
14930 last_unchanged_at_beg_row. */
14931 ins_del_lines (from, dvpos);
14933 else if (dvpos < 0)
14935 /* Scroll up last_unchanged_at_beg_vpos to the end of
14936 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14937 set_terminal_window (end);
14939 /* Delete dvpos lines in front of
14940 last_unchanged_at_beg_vpos. ins_del_lines will set
14941 the cursor to the given vpos and emit |dvpos| delete
14942 line sequences. */
14943 ins_del_lines (from + dvpos, dvpos);
14945 /* On a dumb terminal insert dvpos empty lines at the
14946 end. */
14947 if (!scroll_region_ok)
14948 ins_del_lines (end + dvpos, -dvpos);
14951 set_terminal_window (0);
14954 update_end (f);
14957 /* Shift reused rows of the current matrix to the right position.
14958 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14959 text. */
14960 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14961 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14962 if (dvpos < 0)
14964 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14965 bottom_vpos, dvpos);
14966 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14967 bottom_vpos, 0);
14969 else if (dvpos > 0)
14971 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14972 bottom_vpos, dvpos);
14973 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14974 first_unchanged_at_end_vpos + dvpos, 0);
14977 /* For frame-based redisplay, make sure that current frame and window
14978 matrix are in sync with respect to glyph memory. */
14979 if (!FRAME_WINDOW_P (f))
14980 sync_frame_with_window_matrix_rows (w);
14982 /* Adjust buffer positions in reused rows. */
14983 if (delta || delta_bytes)
14984 increment_matrix_positions (current_matrix,
14985 first_unchanged_at_end_vpos + dvpos,
14986 bottom_vpos, delta, delta_bytes);
14988 /* Adjust Y positions. */
14989 if (dy)
14990 shift_glyph_matrix (w, current_matrix,
14991 first_unchanged_at_end_vpos + dvpos,
14992 bottom_vpos, dy);
14994 if (first_unchanged_at_end_row)
14996 first_unchanged_at_end_row += dvpos;
14997 if (first_unchanged_at_end_row->y >= it.last_visible_y
14998 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14999 first_unchanged_at_end_row = NULL;
15002 /* If scrolling up, there may be some lines to display at the end of
15003 the window. */
15004 last_text_row_at_end = NULL;
15005 if (dy < 0)
15007 /* Scrolling up can leave for example a partially visible line
15008 at the end of the window to be redisplayed. */
15009 /* Set last_row to the glyph row in the current matrix where the
15010 window end line is found. It has been moved up or down in
15011 the matrix by dvpos. */
15012 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15013 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15015 /* If last_row is the window end line, it should display text. */
15016 xassert (last_row->displays_text_p);
15018 /* If window end line was partially visible before, begin
15019 displaying at that line. Otherwise begin displaying with the
15020 line following it. */
15021 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15023 init_to_row_start (&it, w, last_row);
15024 it.vpos = last_vpos;
15025 it.current_y = last_row->y;
15027 else
15029 init_to_row_end (&it, w, last_row);
15030 it.vpos = 1 + last_vpos;
15031 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15032 ++last_row;
15035 /* We may start in a continuation line. If so, we have to
15036 get the right continuation_lines_width and current_x. */
15037 it.continuation_lines_width = last_row->continuation_lines_width;
15038 it.hpos = it.current_x = 0;
15040 /* Display the rest of the lines at the window end. */
15041 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15042 while (it.current_y < it.last_visible_y
15043 && !fonts_changed_p)
15045 /* Is it always sure that the display agrees with lines in
15046 the current matrix? I don't think so, so we mark rows
15047 displayed invalid in the current matrix by setting their
15048 enabled_p flag to zero. */
15049 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15050 if (display_line (&it))
15051 last_text_row_at_end = it.glyph_row - 1;
15055 /* Update window_end_pos and window_end_vpos. */
15056 if (first_unchanged_at_end_row
15057 && !last_text_row_at_end)
15059 /* Window end line if one of the preserved rows from the current
15060 matrix. Set row to the last row displaying text in current
15061 matrix starting at first_unchanged_at_end_row, after
15062 scrolling. */
15063 xassert (first_unchanged_at_end_row->displays_text_p);
15064 row = find_last_row_displaying_text (w->current_matrix, &it,
15065 first_unchanged_at_end_row);
15066 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15068 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15069 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15070 w->window_end_vpos
15071 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15072 xassert (w->window_end_bytepos >= 0);
15073 IF_DEBUG (debug_method_add (w, "A"));
15075 else if (last_text_row_at_end)
15077 w->window_end_pos
15078 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15079 w->window_end_bytepos
15080 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15081 w->window_end_vpos
15082 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15083 xassert (w->window_end_bytepos >= 0);
15084 IF_DEBUG (debug_method_add (w, "B"));
15086 else if (last_text_row)
15088 /* We have displayed either to the end of the window or at the
15089 end of the window, i.e. the last row with text is to be found
15090 in the desired matrix. */
15091 w->window_end_pos
15092 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15093 w->window_end_bytepos
15094 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15095 w->window_end_vpos
15096 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15097 xassert (w->window_end_bytepos >= 0);
15099 else if (first_unchanged_at_end_row == NULL
15100 && last_text_row == NULL
15101 && last_text_row_at_end == NULL)
15103 /* Displayed to end of window, but no line containing text was
15104 displayed. Lines were deleted at the end of the window. */
15105 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15106 int vpos = XFASTINT (w->window_end_vpos);
15107 struct glyph_row *current_row = current_matrix->rows + vpos;
15108 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15110 for (row = NULL;
15111 row == NULL && vpos >= first_vpos;
15112 --vpos, --current_row, --desired_row)
15114 if (desired_row->enabled_p)
15116 if (desired_row->displays_text_p)
15117 row = desired_row;
15119 else if (current_row->displays_text_p)
15120 row = current_row;
15123 xassert (row != NULL);
15124 w->window_end_vpos = make_number (vpos + 1);
15125 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15126 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15127 xassert (w->window_end_bytepos >= 0);
15128 IF_DEBUG (debug_method_add (w, "C"));
15130 else
15131 abort ();
15133 #if 0 /* This leads to problems, for instance when the cursor is
15134 at ZV, and the cursor line displays no text. */
15135 /* Disable rows below what's displayed in the window. This makes
15136 debugging easier. */
15137 enable_glyph_matrix_rows (current_matrix,
15138 XFASTINT (w->window_end_vpos) + 1,
15139 bottom_vpos, 0);
15140 #endif
15142 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15143 debug_end_vpos = XFASTINT (w->window_end_vpos));
15145 /* Record that display has not been completed. */
15146 w->window_end_valid = Qnil;
15147 w->desired_matrix->no_scrolling_p = 1;
15148 return 3;
15150 #undef GIVE_UP
15155 /***********************************************************************
15156 More debugging support
15157 ***********************************************************************/
15159 #if GLYPH_DEBUG
15161 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15162 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15163 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15166 /* Dump the contents of glyph matrix MATRIX on stderr.
15168 GLYPHS 0 means don't show glyph contents.
15169 GLYPHS 1 means show glyphs in short form
15170 GLYPHS > 1 means show glyphs in long form. */
15172 void
15173 dump_glyph_matrix (matrix, glyphs)
15174 struct glyph_matrix *matrix;
15175 int glyphs;
15177 int i;
15178 for (i = 0; i < matrix->nrows; ++i)
15179 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15183 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15184 the glyph row and area where the glyph comes from. */
15186 void
15187 dump_glyph (row, glyph, area)
15188 struct glyph_row *row;
15189 struct glyph *glyph;
15190 int area;
15192 if (glyph->type == CHAR_GLYPH)
15194 fprintf (stderr,
15195 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15196 glyph - row->glyphs[TEXT_AREA],
15197 'C',
15198 glyph->charpos,
15199 (BUFFERP (glyph->object)
15200 ? 'B'
15201 : (STRINGP (glyph->object)
15202 ? 'S'
15203 : '-')),
15204 glyph->pixel_width,
15205 glyph->u.ch,
15206 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15207 ? glyph->u.ch
15208 : '.'),
15209 glyph->face_id,
15210 glyph->left_box_line_p,
15211 glyph->right_box_line_p);
15213 else if (glyph->type == STRETCH_GLYPH)
15215 fprintf (stderr,
15216 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15217 glyph - row->glyphs[TEXT_AREA],
15218 'S',
15219 glyph->charpos,
15220 (BUFFERP (glyph->object)
15221 ? 'B'
15222 : (STRINGP (glyph->object)
15223 ? 'S'
15224 : '-')),
15225 glyph->pixel_width,
15227 '.',
15228 glyph->face_id,
15229 glyph->left_box_line_p,
15230 glyph->right_box_line_p);
15232 else if (glyph->type == IMAGE_GLYPH)
15234 fprintf (stderr,
15235 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15236 glyph - row->glyphs[TEXT_AREA],
15237 'I',
15238 glyph->charpos,
15239 (BUFFERP (glyph->object)
15240 ? 'B'
15241 : (STRINGP (glyph->object)
15242 ? 'S'
15243 : '-')),
15244 glyph->pixel_width,
15245 glyph->u.img_id,
15246 '.',
15247 glyph->face_id,
15248 glyph->left_box_line_p,
15249 glyph->right_box_line_p);
15251 else if (glyph->type == COMPOSITE_GLYPH)
15253 fprintf (stderr,
15254 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15255 glyph - row->glyphs[TEXT_AREA],
15256 '+',
15257 glyph->charpos,
15258 (BUFFERP (glyph->object)
15259 ? 'B'
15260 : (STRINGP (glyph->object)
15261 ? 'S'
15262 : '-')),
15263 glyph->pixel_width,
15264 glyph->u.cmp_id,
15265 '.',
15266 glyph->face_id,
15267 glyph->left_box_line_p,
15268 glyph->right_box_line_p);
15273 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15274 GLYPHS 0 means don't show glyph contents.
15275 GLYPHS 1 means show glyphs in short form
15276 GLYPHS > 1 means show glyphs in long form. */
15278 void
15279 dump_glyph_row (row, vpos, glyphs)
15280 struct glyph_row *row;
15281 int vpos, glyphs;
15283 if (glyphs != 1)
15285 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15286 fprintf (stderr, "======================================================================\n");
15288 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15289 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15290 vpos,
15291 MATRIX_ROW_START_CHARPOS (row),
15292 MATRIX_ROW_END_CHARPOS (row),
15293 row->used[TEXT_AREA],
15294 row->contains_overlapping_glyphs_p,
15295 row->enabled_p,
15296 row->truncated_on_left_p,
15297 row->truncated_on_right_p,
15298 row->continued_p,
15299 MATRIX_ROW_CONTINUATION_LINE_P (row),
15300 row->displays_text_p,
15301 row->ends_at_zv_p,
15302 row->fill_line_p,
15303 row->ends_in_middle_of_char_p,
15304 row->starts_in_middle_of_char_p,
15305 row->mouse_face_p,
15306 row->x,
15307 row->y,
15308 row->pixel_width,
15309 row->height,
15310 row->visible_height,
15311 row->ascent,
15312 row->phys_ascent);
15313 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15314 row->end.overlay_string_index,
15315 row->continuation_lines_width);
15316 fprintf (stderr, "%9d %5d\n",
15317 CHARPOS (row->start.string_pos),
15318 CHARPOS (row->end.string_pos));
15319 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15320 row->end.dpvec_index);
15323 if (glyphs > 1)
15325 int area;
15327 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15329 struct glyph *glyph = row->glyphs[area];
15330 struct glyph *glyph_end = glyph + row->used[area];
15332 /* Glyph for a line end in text. */
15333 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15334 ++glyph_end;
15336 if (glyph < glyph_end)
15337 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15339 for (; glyph < glyph_end; ++glyph)
15340 dump_glyph (row, glyph, area);
15343 else if (glyphs == 1)
15345 int area;
15347 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15349 char *s = (char *) alloca (row->used[area] + 1);
15350 int i;
15352 for (i = 0; i < row->used[area]; ++i)
15354 struct glyph *glyph = row->glyphs[area] + i;
15355 if (glyph->type == CHAR_GLYPH
15356 && glyph->u.ch < 0x80
15357 && glyph->u.ch >= ' ')
15358 s[i] = glyph->u.ch;
15359 else
15360 s[i] = '.';
15363 s[i] = '\0';
15364 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15370 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15371 Sdump_glyph_matrix, 0, 1, "p",
15372 doc: /* Dump the current matrix of the selected window to stderr.
15373 Shows contents of glyph row structures. With non-nil
15374 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15375 glyphs in short form, otherwise show glyphs in long form. */)
15376 (glyphs)
15377 Lisp_Object glyphs;
15379 struct window *w = XWINDOW (selected_window);
15380 struct buffer *buffer = XBUFFER (w->buffer);
15382 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15383 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15384 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15385 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15386 fprintf (stderr, "=============================================\n");
15387 dump_glyph_matrix (w->current_matrix,
15388 NILP (glyphs) ? 0 : XINT (glyphs));
15389 return Qnil;
15393 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15394 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15397 struct frame *f = XFRAME (selected_frame);
15398 dump_glyph_matrix (f->current_matrix, 1);
15399 return Qnil;
15403 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15404 doc: /* Dump glyph row ROW to stderr.
15405 GLYPH 0 means don't dump glyphs.
15406 GLYPH 1 means dump glyphs in short form.
15407 GLYPH > 1 or omitted means dump glyphs in long form. */)
15408 (row, glyphs)
15409 Lisp_Object row, glyphs;
15411 struct glyph_matrix *matrix;
15412 int vpos;
15414 CHECK_NUMBER (row);
15415 matrix = XWINDOW (selected_window)->current_matrix;
15416 vpos = XINT (row);
15417 if (vpos >= 0 && vpos < matrix->nrows)
15418 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15419 vpos,
15420 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15421 return Qnil;
15425 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15426 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15427 GLYPH 0 means don't dump glyphs.
15428 GLYPH 1 means dump glyphs in short form.
15429 GLYPH > 1 or omitted means dump glyphs in long form. */)
15430 (row, glyphs)
15431 Lisp_Object row, glyphs;
15433 struct frame *sf = SELECTED_FRAME ();
15434 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15435 int vpos;
15437 CHECK_NUMBER (row);
15438 vpos = XINT (row);
15439 if (vpos >= 0 && vpos < m->nrows)
15440 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15441 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15442 return Qnil;
15446 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15447 doc: /* Toggle tracing of redisplay.
15448 With ARG, turn tracing on if and only if ARG is positive. */)
15449 (arg)
15450 Lisp_Object arg;
15452 if (NILP (arg))
15453 trace_redisplay_p = !trace_redisplay_p;
15454 else
15456 arg = Fprefix_numeric_value (arg);
15457 trace_redisplay_p = XINT (arg) > 0;
15460 return Qnil;
15464 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15465 doc: /* Like `format', but print result to stderr.
15466 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15467 (nargs, args)
15468 int nargs;
15469 Lisp_Object *args;
15471 Lisp_Object s = Fformat (nargs, args);
15472 fprintf (stderr, "%s", SDATA (s));
15473 return Qnil;
15476 #endif /* GLYPH_DEBUG */
15480 /***********************************************************************
15481 Building Desired Matrix Rows
15482 ***********************************************************************/
15484 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15485 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15487 static struct glyph_row *
15488 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15489 struct window *w;
15490 Lisp_Object overlay_arrow_string;
15492 struct frame *f = XFRAME (WINDOW_FRAME (w));
15493 struct buffer *buffer = XBUFFER (w->buffer);
15494 struct buffer *old = current_buffer;
15495 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15496 int arrow_len = SCHARS (overlay_arrow_string);
15497 const unsigned char *arrow_end = arrow_string + arrow_len;
15498 const unsigned char *p;
15499 struct it it;
15500 int multibyte_p;
15501 int n_glyphs_before;
15503 set_buffer_temp (buffer);
15504 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15505 it.glyph_row->used[TEXT_AREA] = 0;
15506 SET_TEXT_POS (it.position, 0, 0);
15508 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15509 p = arrow_string;
15510 while (p < arrow_end)
15512 Lisp_Object face, ilisp;
15514 /* Get the next character. */
15515 if (multibyte_p)
15516 it.c = string_char_and_length (p, arrow_len, &it.len);
15517 else
15518 it.c = *p, it.len = 1;
15519 p += it.len;
15521 /* Get its face. */
15522 ilisp = make_number (p - arrow_string);
15523 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15524 it.face_id = compute_char_face (f, it.c, face);
15526 /* Compute its width, get its glyphs. */
15527 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15528 SET_TEXT_POS (it.position, -1, -1);
15529 PRODUCE_GLYPHS (&it);
15531 /* If this character doesn't fit any more in the line, we have
15532 to remove some glyphs. */
15533 if (it.current_x > it.last_visible_x)
15535 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15536 break;
15540 set_buffer_temp (old);
15541 return it.glyph_row;
15545 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15546 glyphs are only inserted for terminal frames since we can't really
15547 win with truncation glyphs when partially visible glyphs are
15548 involved. Which glyphs to insert is determined by
15549 produce_special_glyphs. */
15551 static void
15552 insert_left_trunc_glyphs (it)
15553 struct it *it;
15555 struct it truncate_it;
15556 struct glyph *from, *end, *to, *toend;
15558 xassert (!FRAME_WINDOW_P (it->f));
15560 /* Get the truncation glyphs. */
15561 truncate_it = *it;
15562 truncate_it.current_x = 0;
15563 truncate_it.face_id = DEFAULT_FACE_ID;
15564 truncate_it.glyph_row = &scratch_glyph_row;
15565 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15566 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15567 truncate_it.object = make_number (0);
15568 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15570 /* Overwrite glyphs from IT with truncation glyphs. */
15571 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15572 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15573 to = it->glyph_row->glyphs[TEXT_AREA];
15574 toend = to + it->glyph_row->used[TEXT_AREA];
15576 while (from < end)
15577 *to++ = *from++;
15579 /* There may be padding glyphs left over. Overwrite them too. */
15580 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15582 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15583 while (from < end)
15584 *to++ = *from++;
15587 if (to > toend)
15588 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15592 /* Compute the pixel height and width of IT->glyph_row.
15594 Most of the time, ascent and height of a display line will be equal
15595 to the max_ascent and max_height values of the display iterator
15596 structure. This is not the case if
15598 1. We hit ZV without displaying anything. In this case, max_ascent
15599 and max_height will be zero.
15601 2. We have some glyphs that don't contribute to the line height.
15602 (The glyph row flag contributes_to_line_height_p is for future
15603 pixmap extensions).
15605 The first case is easily covered by using default values because in
15606 these cases, the line height does not really matter, except that it
15607 must not be zero. */
15609 static void
15610 compute_line_metrics (it)
15611 struct it *it;
15613 struct glyph_row *row = it->glyph_row;
15614 int area, i;
15616 if (FRAME_WINDOW_P (it->f))
15618 int i, min_y, max_y;
15620 /* The line may consist of one space only, that was added to
15621 place the cursor on it. If so, the row's height hasn't been
15622 computed yet. */
15623 if (row->height == 0)
15625 if (it->max_ascent + it->max_descent == 0)
15626 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15627 row->ascent = it->max_ascent;
15628 row->height = it->max_ascent + it->max_descent;
15629 row->phys_ascent = it->max_phys_ascent;
15630 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15631 row->extra_line_spacing = it->max_extra_line_spacing;
15634 /* Compute the width of this line. */
15635 row->pixel_width = row->x;
15636 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15637 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15639 xassert (row->pixel_width >= 0);
15640 xassert (row->ascent >= 0 && row->height > 0);
15642 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15643 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15645 /* If first line's physical ascent is larger than its logical
15646 ascent, use the physical ascent, and make the row taller.
15647 This makes accented characters fully visible. */
15648 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15649 && row->phys_ascent > row->ascent)
15651 row->height += row->phys_ascent - row->ascent;
15652 row->ascent = row->phys_ascent;
15655 /* Compute how much of the line is visible. */
15656 row->visible_height = row->height;
15658 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15659 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15661 if (row->y < min_y)
15662 row->visible_height -= min_y - row->y;
15663 if (row->y + row->height > max_y)
15664 row->visible_height -= row->y + row->height - max_y;
15666 else
15668 row->pixel_width = row->used[TEXT_AREA];
15669 if (row->continued_p)
15670 row->pixel_width -= it->continuation_pixel_width;
15671 else if (row->truncated_on_right_p)
15672 row->pixel_width -= it->truncation_pixel_width;
15673 row->ascent = row->phys_ascent = 0;
15674 row->height = row->phys_height = row->visible_height = 1;
15675 row->extra_line_spacing = 0;
15678 /* Compute a hash code for this row. */
15679 row->hash = 0;
15680 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15681 for (i = 0; i < row->used[area]; ++i)
15682 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15683 + row->glyphs[area][i].u.val
15684 + row->glyphs[area][i].face_id
15685 + row->glyphs[area][i].padding_p
15686 + (row->glyphs[area][i].type << 2));
15688 it->max_ascent = it->max_descent = 0;
15689 it->max_phys_ascent = it->max_phys_descent = 0;
15693 /* Append one space to the glyph row of iterator IT if doing a
15694 window-based redisplay. The space has the same face as
15695 IT->face_id. Value is non-zero if a space was added.
15697 This function is called to make sure that there is always one glyph
15698 at the end of a glyph row that the cursor can be set on under
15699 window-systems. (If there weren't such a glyph we would not know
15700 how wide and tall a box cursor should be displayed).
15702 At the same time this space let's a nicely handle clearing to the
15703 end of the line if the row ends in italic text. */
15705 static int
15706 append_space_for_newline (it, default_face_p)
15707 struct it *it;
15708 int default_face_p;
15710 if (FRAME_WINDOW_P (it->f))
15712 int n = it->glyph_row->used[TEXT_AREA];
15714 if (it->glyph_row->glyphs[TEXT_AREA] + n
15715 < it->glyph_row->glyphs[1 + TEXT_AREA])
15717 /* Save some values that must not be changed.
15718 Must save IT->c and IT->len because otherwise
15719 ITERATOR_AT_END_P wouldn't work anymore after
15720 append_space_for_newline has been called. */
15721 enum display_element_type saved_what = it->what;
15722 int saved_c = it->c, saved_len = it->len;
15723 int saved_x = it->current_x;
15724 int saved_face_id = it->face_id;
15725 struct text_pos saved_pos;
15726 Lisp_Object saved_object;
15727 struct face *face;
15729 saved_object = it->object;
15730 saved_pos = it->position;
15732 it->what = IT_CHARACTER;
15733 bzero (&it->position, sizeof it->position);
15734 it->object = make_number (0);
15735 it->c = ' ';
15736 it->len = 1;
15738 if (default_face_p)
15739 it->face_id = DEFAULT_FACE_ID;
15740 else if (it->face_before_selective_p)
15741 it->face_id = it->saved_face_id;
15742 face = FACE_FROM_ID (it->f, it->face_id);
15743 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15745 PRODUCE_GLYPHS (it);
15747 it->override_ascent = -1;
15748 it->constrain_row_ascent_descent_p = 0;
15749 it->current_x = saved_x;
15750 it->object = saved_object;
15751 it->position = saved_pos;
15752 it->what = saved_what;
15753 it->face_id = saved_face_id;
15754 it->len = saved_len;
15755 it->c = saved_c;
15756 return 1;
15760 return 0;
15764 /* Extend the face of the last glyph in the text area of IT->glyph_row
15765 to the end of the display line. Called from display_line.
15766 If the glyph row is empty, add a space glyph to it so that we
15767 know the face to draw. Set the glyph row flag fill_line_p. */
15769 static void
15770 extend_face_to_end_of_line (it)
15771 struct it *it;
15773 struct face *face;
15774 struct frame *f = it->f;
15776 /* If line is already filled, do nothing. */
15777 if (it->current_x >= it->last_visible_x)
15778 return;
15780 /* Face extension extends the background and box of IT->face_id
15781 to the end of the line. If the background equals the background
15782 of the frame, we don't have to do anything. */
15783 if (it->face_before_selective_p)
15784 face = FACE_FROM_ID (it->f, it->saved_face_id);
15785 else
15786 face = FACE_FROM_ID (f, it->face_id);
15788 if (FRAME_WINDOW_P (f)
15789 && it->glyph_row->displays_text_p
15790 && face->box == FACE_NO_BOX
15791 && face->background == FRAME_BACKGROUND_PIXEL (f)
15792 && !face->stipple)
15793 return;
15795 /* Set the glyph row flag indicating that the face of the last glyph
15796 in the text area has to be drawn to the end of the text area. */
15797 it->glyph_row->fill_line_p = 1;
15799 /* If current character of IT is not ASCII, make sure we have the
15800 ASCII face. This will be automatically undone the next time
15801 get_next_display_element returns a multibyte character. Note
15802 that the character will always be single byte in unibyte text. */
15803 if (!SINGLE_BYTE_CHAR_P (it->c))
15805 it->face_id = FACE_FOR_CHAR (f, face, 0);
15808 if (FRAME_WINDOW_P (f))
15810 /* If the row is empty, add a space with the current face of IT,
15811 so that we know which face to draw. */
15812 if (it->glyph_row->used[TEXT_AREA] == 0)
15814 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15815 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15816 it->glyph_row->used[TEXT_AREA] = 1;
15819 else
15821 /* Save some values that must not be changed. */
15822 int saved_x = it->current_x;
15823 struct text_pos saved_pos;
15824 Lisp_Object saved_object;
15825 enum display_element_type saved_what = it->what;
15826 int saved_face_id = it->face_id;
15828 saved_object = it->object;
15829 saved_pos = it->position;
15831 it->what = IT_CHARACTER;
15832 bzero (&it->position, sizeof it->position);
15833 it->object = make_number (0);
15834 it->c = ' ';
15835 it->len = 1;
15836 it->face_id = face->id;
15838 PRODUCE_GLYPHS (it);
15840 while (it->current_x <= it->last_visible_x)
15841 PRODUCE_GLYPHS (it);
15843 /* Don't count these blanks really. It would let us insert a left
15844 truncation glyph below and make us set the cursor on them, maybe. */
15845 it->current_x = saved_x;
15846 it->object = saved_object;
15847 it->position = saved_pos;
15848 it->what = saved_what;
15849 it->face_id = saved_face_id;
15854 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15855 trailing whitespace. */
15857 static int
15858 trailing_whitespace_p (charpos)
15859 int charpos;
15861 int bytepos = CHAR_TO_BYTE (charpos);
15862 int c = 0;
15864 while (bytepos < ZV_BYTE
15865 && (c = FETCH_CHAR (bytepos),
15866 c == ' ' || c == '\t'))
15867 ++bytepos;
15869 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15871 if (bytepos != PT_BYTE)
15872 return 1;
15874 return 0;
15878 /* Highlight trailing whitespace, if any, in ROW. */
15880 void
15881 highlight_trailing_whitespace (f, row)
15882 struct frame *f;
15883 struct glyph_row *row;
15885 int used = row->used[TEXT_AREA];
15887 if (used)
15889 struct glyph *start = row->glyphs[TEXT_AREA];
15890 struct glyph *glyph = start + used - 1;
15892 /* Skip over glyphs inserted to display the cursor at the
15893 end of a line, for extending the face of the last glyph
15894 to the end of the line on terminals, and for truncation
15895 and continuation glyphs. */
15896 while (glyph >= start
15897 && glyph->type == CHAR_GLYPH
15898 && INTEGERP (glyph->object))
15899 --glyph;
15901 /* If last glyph is a space or stretch, and it's trailing
15902 whitespace, set the face of all trailing whitespace glyphs in
15903 IT->glyph_row to `trailing-whitespace'. */
15904 if (glyph >= start
15905 && BUFFERP (glyph->object)
15906 && (glyph->type == STRETCH_GLYPH
15907 || (glyph->type == CHAR_GLYPH
15908 && glyph->u.ch == ' '))
15909 && trailing_whitespace_p (glyph->charpos))
15911 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15912 if (face_id < 0)
15913 return;
15915 while (glyph >= start
15916 && BUFFERP (glyph->object)
15917 && (glyph->type == STRETCH_GLYPH
15918 || (glyph->type == CHAR_GLYPH
15919 && glyph->u.ch == ' ')))
15920 (glyph--)->face_id = face_id;
15926 /* Value is non-zero if glyph row ROW in window W should be
15927 used to hold the cursor. */
15929 static int
15930 cursor_row_p (w, row)
15931 struct window *w;
15932 struct glyph_row *row;
15934 int cursor_row_p = 1;
15936 if (PT == MATRIX_ROW_END_CHARPOS (row))
15938 /* Suppose the row ends on a string.
15939 Unless the row is continued, that means it ends on a newline
15940 in the string. If it's anything other than a display string
15941 (e.g. a before-string from an overlay), we don't want the
15942 cursor there. (This heuristic seems to give the optimal
15943 behavior for the various types of multi-line strings.) */
15944 if (CHARPOS (row->end.string_pos) >= 0)
15946 if (row->continued_p)
15947 cursor_row_p = 1;
15948 else
15950 /* Check for `display' property. */
15951 struct glyph *beg = row->glyphs[TEXT_AREA];
15952 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15953 struct glyph *glyph;
15955 cursor_row_p = 0;
15956 for (glyph = end; glyph >= beg; --glyph)
15957 if (STRINGP (glyph->object))
15959 Lisp_Object prop
15960 = Fget_char_property (make_number (PT),
15961 Qdisplay, Qnil);
15962 cursor_row_p =
15963 (!NILP (prop)
15964 && display_prop_string_p (prop, glyph->object));
15965 break;
15969 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15971 /* If the row ends in middle of a real character,
15972 and the line is continued, we want the cursor here.
15973 That's because MATRIX_ROW_END_CHARPOS would equal
15974 PT if PT is before the character. */
15975 if (!row->ends_in_ellipsis_p)
15976 cursor_row_p = row->continued_p;
15977 else
15978 /* If the row ends in an ellipsis, then
15979 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15980 We want that position to be displayed after the ellipsis. */
15981 cursor_row_p = 0;
15983 /* If the row ends at ZV, display the cursor at the end of that
15984 row instead of at the start of the row below. */
15985 else if (row->ends_at_zv_p)
15986 cursor_row_p = 1;
15987 else
15988 cursor_row_p = 0;
15991 return cursor_row_p;
15995 /* Construct the glyph row IT->glyph_row in the desired matrix of
15996 IT->w from text at the current position of IT. See dispextern.h
15997 for an overview of struct it. Value is non-zero if
15998 IT->glyph_row displays text, as opposed to a line displaying ZV
15999 only. */
16001 static int
16002 display_line (it)
16003 struct it *it;
16005 struct glyph_row *row = it->glyph_row;
16006 Lisp_Object overlay_arrow_string;
16008 /* We always start displaying at hpos zero even if hscrolled. */
16009 xassert (it->hpos == 0 && it->current_x == 0);
16011 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16012 >= it->w->desired_matrix->nrows)
16014 it->w->nrows_scale_factor++;
16015 fonts_changed_p = 1;
16016 return 0;
16019 /* Is IT->w showing the region? */
16020 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16022 /* Clear the result glyph row and enable it. */
16023 prepare_desired_row (row);
16025 row->y = it->current_y;
16026 row->start = it->start;
16027 row->continuation_lines_width = it->continuation_lines_width;
16028 row->displays_text_p = 1;
16029 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16030 it->starts_in_middle_of_char_p = 0;
16032 /* Arrange the overlays nicely for our purposes. Usually, we call
16033 display_line on only one line at a time, in which case this
16034 can't really hurt too much, or we call it on lines which appear
16035 one after another in the buffer, in which case all calls to
16036 recenter_overlay_lists but the first will be pretty cheap. */
16037 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16039 /* Move over display elements that are not visible because we are
16040 hscrolled. This may stop at an x-position < IT->first_visible_x
16041 if the first glyph is partially visible or if we hit a line end. */
16042 if (it->current_x < it->first_visible_x)
16044 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16045 MOVE_TO_POS | MOVE_TO_X);
16048 /* Get the initial row height. This is either the height of the
16049 text hscrolled, if there is any, or zero. */
16050 row->ascent = it->max_ascent;
16051 row->height = it->max_ascent + it->max_descent;
16052 row->phys_ascent = it->max_phys_ascent;
16053 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16054 row->extra_line_spacing = it->max_extra_line_spacing;
16056 /* Loop generating characters. The loop is left with IT on the next
16057 character to display. */
16058 while (1)
16060 int n_glyphs_before, hpos_before, x_before;
16061 int x, i, nglyphs;
16062 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16064 /* Retrieve the next thing to display. Value is zero if end of
16065 buffer reached. */
16066 if (!get_next_display_element (it))
16068 /* Maybe add a space at the end of this line that is used to
16069 display the cursor there under X. Set the charpos of the
16070 first glyph of blank lines not corresponding to any text
16071 to -1. */
16072 #ifdef HAVE_WINDOW_SYSTEM
16073 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16074 row->exact_window_width_line_p = 1;
16075 else
16076 #endif /* HAVE_WINDOW_SYSTEM */
16077 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16078 || row->used[TEXT_AREA] == 0)
16080 row->glyphs[TEXT_AREA]->charpos = -1;
16081 row->displays_text_p = 0;
16083 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16084 && (!MINI_WINDOW_P (it->w)
16085 || (minibuf_level && EQ (it->window, minibuf_window))))
16086 row->indicate_empty_line_p = 1;
16089 it->continuation_lines_width = 0;
16090 row->ends_at_zv_p = 1;
16091 break;
16094 /* Now, get the metrics of what we want to display. This also
16095 generates glyphs in `row' (which is IT->glyph_row). */
16096 n_glyphs_before = row->used[TEXT_AREA];
16097 x = it->current_x;
16099 /* Remember the line height so far in case the next element doesn't
16100 fit on the line. */
16101 if (!it->truncate_lines_p)
16103 ascent = it->max_ascent;
16104 descent = it->max_descent;
16105 phys_ascent = it->max_phys_ascent;
16106 phys_descent = it->max_phys_descent;
16109 PRODUCE_GLYPHS (it);
16111 /* If this display element was in marginal areas, continue with
16112 the next one. */
16113 if (it->area != TEXT_AREA)
16115 row->ascent = max (row->ascent, it->max_ascent);
16116 row->height = max (row->height, it->max_ascent + it->max_descent);
16117 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16118 row->phys_height = max (row->phys_height,
16119 it->max_phys_ascent + it->max_phys_descent);
16120 row->extra_line_spacing = max (row->extra_line_spacing,
16121 it->max_extra_line_spacing);
16122 set_iterator_to_next (it, 1);
16123 continue;
16126 /* Does the display element fit on the line? If we truncate
16127 lines, we should draw past the right edge of the window. If
16128 we don't truncate, we want to stop so that we can display the
16129 continuation glyph before the right margin. If lines are
16130 continued, there are two possible strategies for characters
16131 resulting in more than 1 glyph (e.g. tabs): Display as many
16132 glyphs as possible in this line and leave the rest for the
16133 continuation line, or display the whole element in the next
16134 line. Original redisplay did the former, so we do it also. */
16135 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16136 hpos_before = it->hpos;
16137 x_before = x;
16139 if (/* Not a newline. */
16140 nglyphs > 0
16141 /* Glyphs produced fit entirely in the line. */
16142 && it->current_x < it->last_visible_x)
16144 it->hpos += nglyphs;
16145 row->ascent = max (row->ascent, it->max_ascent);
16146 row->height = max (row->height, it->max_ascent + it->max_descent);
16147 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16148 row->phys_height = max (row->phys_height,
16149 it->max_phys_ascent + it->max_phys_descent);
16150 row->extra_line_spacing = max (row->extra_line_spacing,
16151 it->max_extra_line_spacing);
16152 if (it->current_x - it->pixel_width < it->first_visible_x)
16153 row->x = x - it->first_visible_x;
16155 else
16157 int new_x;
16158 struct glyph *glyph;
16160 for (i = 0; i < nglyphs; ++i, x = new_x)
16162 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16163 new_x = x + glyph->pixel_width;
16165 if (/* Lines are continued. */
16166 !it->truncate_lines_p
16167 && (/* Glyph doesn't fit on the line. */
16168 new_x > it->last_visible_x
16169 /* Or it fits exactly on a window system frame. */
16170 || (new_x == it->last_visible_x
16171 && FRAME_WINDOW_P (it->f))))
16173 /* End of a continued line. */
16175 if (it->hpos == 0
16176 || (new_x == it->last_visible_x
16177 && FRAME_WINDOW_P (it->f)))
16179 /* Current glyph is the only one on the line or
16180 fits exactly on the line. We must continue
16181 the line because we can't draw the cursor
16182 after the glyph. */
16183 row->continued_p = 1;
16184 it->current_x = new_x;
16185 it->continuation_lines_width += new_x;
16186 ++it->hpos;
16187 if (i == nglyphs - 1)
16189 set_iterator_to_next (it, 1);
16190 #ifdef HAVE_WINDOW_SYSTEM
16191 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16193 if (!get_next_display_element (it))
16195 row->exact_window_width_line_p = 1;
16196 it->continuation_lines_width = 0;
16197 row->continued_p = 0;
16198 row->ends_at_zv_p = 1;
16200 else if (ITERATOR_AT_END_OF_LINE_P (it))
16202 row->continued_p = 0;
16203 row->exact_window_width_line_p = 1;
16206 #endif /* HAVE_WINDOW_SYSTEM */
16209 else if (CHAR_GLYPH_PADDING_P (*glyph)
16210 && !FRAME_WINDOW_P (it->f))
16212 /* A padding glyph that doesn't fit on this line.
16213 This means the whole character doesn't fit
16214 on the line. */
16215 row->used[TEXT_AREA] = n_glyphs_before;
16217 /* Fill the rest of the row with continuation
16218 glyphs like in 20.x. */
16219 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16220 < row->glyphs[1 + TEXT_AREA])
16221 produce_special_glyphs (it, IT_CONTINUATION);
16223 row->continued_p = 1;
16224 it->current_x = x_before;
16225 it->continuation_lines_width += x_before;
16227 /* Restore the height to what it was before the
16228 element not fitting on the line. */
16229 it->max_ascent = ascent;
16230 it->max_descent = descent;
16231 it->max_phys_ascent = phys_ascent;
16232 it->max_phys_descent = phys_descent;
16234 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16236 /* A TAB that extends past the right edge of the
16237 window. This produces a single glyph on
16238 window system frames. We leave the glyph in
16239 this row and let it fill the row, but don't
16240 consume the TAB. */
16241 it->continuation_lines_width += it->last_visible_x;
16242 row->ends_in_middle_of_char_p = 1;
16243 row->continued_p = 1;
16244 glyph->pixel_width = it->last_visible_x - x;
16245 it->starts_in_middle_of_char_p = 1;
16247 else
16249 /* Something other than a TAB that draws past
16250 the right edge of the window. Restore
16251 positions to values before the element. */
16252 row->used[TEXT_AREA] = n_glyphs_before + i;
16254 /* Display continuation glyphs. */
16255 if (!FRAME_WINDOW_P (it->f))
16256 produce_special_glyphs (it, IT_CONTINUATION);
16257 row->continued_p = 1;
16259 it->current_x = x_before;
16260 it->continuation_lines_width += x;
16261 extend_face_to_end_of_line (it);
16263 if (nglyphs > 1 && i > 0)
16265 row->ends_in_middle_of_char_p = 1;
16266 it->starts_in_middle_of_char_p = 1;
16269 /* Restore the height to what it was before the
16270 element not fitting on the line. */
16271 it->max_ascent = ascent;
16272 it->max_descent = descent;
16273 it->max_phys_ascent = phys_ascent;
16274 it->max_phys_descent = phys_descent;
16277 break;
16279 else if (new_x > it->first_visible_x)
16281 /* Increment number of glyphs actually displayed. */
16282 ++it->hpos;
16284 if (x < it->first_visible_x)
16285 /* Glyph is partially visible, i.e. row starts at
16286 negative X position. */
16287 row->x = x - it->first_visible_x;
16289 else
16291 /* Glyph is completely off the left margin of the
16292 window. This should not happen because of the
16293 move_it_in_display_line at the start of this
16294 function, unless the text display area of the
16295 window is empty. */
16296 xassert (it->first_visible_x <= it->last_visible_x);
16300 row->ascent = max (row->ascent, it->max_ascent);
16301 row->height = max (row->height, it->max_ascent + it->max_descent);
16302 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16303 row->phys_height = max (row->phys_height,
16304 it->max_phys_ascent + it->max_phys_descent);
16305 row->extra_line_spacing = max (row->extra_line_spacing,
16306 it->max_extra_line_spacing);
16308 /* End of this display line if row is continued. */
16309 if (row->continued_p || row->ends_at_zv_p)
16310 break;
16313 at_end_of_line:
16314 /* Is this a line end? If yes, we're also done, after making
16315 sure that a non-default face is extended up to the right
16316 margin of the window. */
16317 if (ITERATOR_AT_END_OF_LINE_P (it))
16319 int used_before = row->used[TEXT_AREA];
16321 row->ends_in_newline_from_string_p = STRINGP (it->object);
16323 #ifdef HAVE_WINDOW_SYSTEM
16324 /* Add a space at the end of the line that is used to
16325 display the cursor there. */
16326 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16327 append_space_for_newline (it, 0);
16328 #endif /* HAVE_WINDOW_SYSTEM */
16330 /* Extend the face to the end of the line. */
16331 extend_face_to_end_of_line (it);
16333 /* Make sure we have the position. */
16334 if (used_before == 0)
16335 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16337 /* Consume the line end. This skips over invisible lines. */
16338 set_iterator_to_next (it, 1);
16339 it->continuation_lines_width = 0;
16340 break;
16343 /* Proceed with next display element. Note that this skips
16344 over lines invisible because of selective display. */
16345 set_iterator_to_next (it, 1);
16347 /* If we truncate lines, we are done when the last displayed
16348 glyphs reach past the right margin of the window. */
16349 if (it->truncate_lines_p
16350 && (FRAME_WINDOW_P (it->f)
16351 ? (it->current_x >= it->last_visible_x)
16352 : (it->current_x > it->last_visible_x)))
16354 /* Maybe add truncation glyphs. */
16355 if (!FRAME_WINDOW_P (it->f))
16357 int i, n;
16359 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16360 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16361 break;
16363 for (n = row->used[TEXT_AREA]; i < n; ++i)
16365 row->used[TEXT_AREA] = i;
16366 produce_special_glyphs (it, IT_TRUNCATION);
16369 #ifdef HAVE_WINDOW_SYSTEM
16370 else
16372 /* Don't truncate if we can overflow newline into fringe. */
16373 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16375 if (!get_next_display_element (it))
16377 it->continuation_lines_width = 0;
16378 row->ends_at_zv_p = 1;
16379 row->exact_window_width_line_p = 1;
16380 break;
16382 if (ITERATOR_AT_END_OF_LINE_P (it))
16384 row->exact_window_width_line_p = 1;
16385 goto at_end_of_line;
16389 #endif /* HAVE_WINDOW_SYSTEM */
16391 row->truncated_on_right_p = 1;
16392 it->continuation_lines_width = 0;
16393 reseat_at_next_visible_line_start (it, 0);
16394 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16395 it->hpos = hpos_before;
16396 it->current_x = x_before;
16397 break;
16401 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16402 at the left window margin. */
16403 if (it->first_visible_x
16404 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16406 if (!FRAME_WINDOW_P (it->f))
16407 insert_left_trunc_glyphs (it);
16408 row->truncated_on_left_p = 1;
16411 /* If the start of this line is the overlay arrow-position, then
16412 mark this glyph row as the one containing the overlay arrow.
16413 This is clearly a mess with variable size fonts. It would be
16414 better to let it be displayed like cursors under X. */
16415 if ((row->displays_text_p || !overlay_arrow_seen)
16416 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16417 !NILP (overlay_arrow_string)))
16419 /* Overlay arrow in window redisplay is a fringe bitmap. */
16420 if (STRINGP (overlay_arrow_string))
16422 struct glyph_row *arrow_row
16423 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16424 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16425 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16426 struct glyph *p = row->glyphs[TEXT_AREA];
16427 struct glyph *p2, *end;
16429 /* Copy the arrow glyphs. */
16430 while (glyph < arrow_end)
16431 *p++ = *glyph++;
16433 /* Throw away padding glyphs. */
16434 p2 = p;
16435 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16436 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16437 ++p2;
16438 if (p2 > p)
16440 while (p2 < end)
16441 *p++ = *p2++;
16442 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16445 else
16447 xassert (INTEGERP (overlay_arrow_string));
16448 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16450 overlay_arrow_seen = 1;
16453 /* Compute pixel dimensions of this line. */
16454 compute_line_metrics (it);
16456 /* Remember the position at which this line ends. */
16457 row->end = it->current;
16459 /* Record whether this row ends inside an ellipsis. */
16460 row->ends_in_ellipsis_p
16461 = (it->method == GET_FROM_DISPLAY_VECTOR
16462 && it->ellipsis_p);
16464 /* Save fringe bitmaps in this row. */
16465 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16466 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16467 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16468 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16470 it->left_user_fringe_bitmap = 0;
16471 it->left_user_fringe_face_id = 0;
16472 it->right_user_fringe_bitmap = 0;
16473 it->right_user_fringe_face_id = 0;
16475 /* Maybe set the cursor. */
16476 if (it->w->cursor.vpos < 0
16477 && PT >= MATRIX_ROW_START_CHARPOS (row)
16478 && PT <= MATRIX_ROW_END_CHARPOS (row)
16479 && cursor_row_p (it->w, row))
16480 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16482 /* Highlight trailing whitespace. */
16483 if (!NILP (Vshow_trailing_whitespace))
16484 highlight_trailing_whitespace (it->f, it->glyph_row);
16486 /* Prepare for the next line. This line starts horizontally at (X
16487 HPOS) = (0 0). Vertical positions are incremented. As a
16488 convenience for the caller, IT->glyph_row is set to the next
16489 row to be used. */
16490 it->current_x = it->hpos = 0;
16491 it->current_y += row->height;
16492 ++it->vpos;
16493 ++it->glyph_row;
16494 it->start = it->current;
16495 return row->displays_text_p;
16500 /***********************************************************************
16501 Menu Bar
16502 ***********************************************************************/
16504 /* Redisplay the menu bar in the frame for window W.
16506 The menu bar of X frames that don't have X toolkit support is
16507 displayed in a special window W->frame->menu_bar_window.
16509 The menu bar of terminal frames is treated specially as far as
16510 glyph matrices are concerned. Menu bar lines are not part of
16511 windows, so the update is done directly on the frame matrix rows
16512 for the menu bar. */
16514 static void
16515 display_menu_bar (w)
16516 struct window *w;
16518 struct frame *f = XFRAME (WINDOW_FRAME (w));
16519 struct it it;
16520 Lisp_Object items;
16521 int i;
16523 /* Don't do all this for graphical frames. */
16524 #ifdef HAVE_NTGUI
16525 if (!NILP (Vwindow_system))
16526 return;
16527 #endif
16528 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16529 if (FRAME_X_P (f))
16530 return;
16531 #endif
16532 #ifdef MAC_OS
16533 if (FRAME_MAC_P (f))
16534 return;
16535 #endif
16537 #ifdef USE_X_TOOLKIT
16538 xassert (!FRAME_WINDOW_P (f));
16539 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16540 it.first_visible_x = 0;
16541 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16542 #else /* not USE_X_TOOLKIT */
16543 if (FRAME_WINDOW_P (f))
16545 /* Menu bar lines are displayed in the desired matrix of the
16546 dummy window menu_bar_window. */
16547 struct window *menu_w;
16548 xassert (WINDOWP (f->menu_bar_window));
16549 menu_w = XWINDOW (f->menu_bar_window);
16550 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16551 MENU_FACE_ID);
16552 it.first_visible_x = 0;
16553 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16555 else
16557 /* This is a TTY frame, i.e. character hpos/vpos are used as
16558 pixel x/y. */
16559 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16560 MENU_FACE_ID);
16561 it.first_visible_x = 0;
16562 it.last_visible_x = FRAME_COLS (f);
16564 #endif /* not USE_X_TOOLKIT */
16566 if (! mode_line_inverse_video)
16567 /* Force the menu-bar to be displayed in the default face. */
16568 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16570 /* Clear all rows of the menu bar. */
16571 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16573 struct glyph_row *row = it.glyph_row + i;
16574 clear_glyph_row (row);
16575 row->enabled_p = 1;
16576 row->full_width_p = 1;
16579 /* Display all items of the menu bar. */
16580 items = FRAME_MENU_BAR_ITEMS (it.f);
16581 for (i = 0; i < XVECTOR (items)->size; i += 4)
16583 Lisp_Object string;
16585 /* Stop at nil string. */
16586 string = AREF (items, i + 1);
16587 if (NILP (string))
16588 break;
16590 /* Remember where item was displayed. */
16591 AREF (items, i + 3) = make_number (it.hpos);
16593 /* Display the item, pad with one space. */
16594 if (it.current_x < it.last_visible_x)
16595 display_string (NULL, string, Qnil, 0, 0, &it,
16596 SCHARS (string) + 1, 0, 0, -1);
16599 /* Fill out the line with spaces. */
16600 if (it.current_x < it.last_visible_x)
16601 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16603 /* Compute the total height of the lines. */
16604 compute_line_metrics (&it);
16609 /***********************************************************************
16610 Mode Line
16611 ***********************************************************************/
16613 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16614 FORCE is non-zero, redisplay mode lines unconditionally.
16615 Otherwise, redisplay only mode lines that are garbaged. Value is
16616 the number of windows whose mode lines were redisplayed. */
16618 static int
16619 redisplay_mode_lines (window, force)
16620 Lisp_Object window;
16621 int force;
16623 int nwindows = 0;
16625 while (!NILP (window))
16627 struct window *w = XWINDOW (window);
16629 if (WINDOWP (w->hchild))
16630 nwindows += redisplay_mode_lines (w->hchild, force);
16631 else if (WINDOWP (w->vchild))
16632 nwindows += redisplay_mode_lines (w->vchild, force);
16633 else if (force
16634 || FRAME_GARBAGED_P (XFRAME (w->frame))
16635 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16637 struct text_pos lpoint;
16638 struct buffer *old = current_buffer;
16640 /* Set the window's buffer for the mode line display. */
16641 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16642 set_buffer_internal_1 (XBUFFER (w->buffer));
16644 /* Point refers normally to the selected window. For any
16645 other window, set up appropriate value. */
16646 if (!EQ (window, selected_window))
16648 struct text_pos pt;
16650 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16651 if (CHARPOS (pt) < BEGV)
16652 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16653 else if (CHARPOS (pt) > (ZV - 1))
16654 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16655 else
16656 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16659 /* Display mode lines. */
16660 clear_glyph_matrix (w->desired_matrix);
16661 if (display_mode_lines (w))
16663 ++nwindows;
16664 w->must_be_updated_p = 1;
16667 /* Restore old settings. */
16668 set_buffer_internal_1 (old);
16669 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16672 window = w->next;
16675 return nwindows;
16679 /* Display the mode and/or top line of window W. Value is the number
16680 of mode lines displayed. */
16682 static int
16683 display_mode_lines (w)
16684 struct window *w;
16686 Lisp_Object old_selected_window, old_selected_frame;
16687 int n = 0;
16689 old_selected_frame = selected_frame;
16690 selected_frame = w->frame;
16691 old_selected_window = selected_window;
16692 XSETWINDOW (selected_window, w);
16694 /* These will be set while the mode line specs are processed. */
16695 line_number_displayed = 0;
16696 w->column_number_displayed = Qnil;
16698 if (WINDOW_WANTS_MODELINE_P (w))
16700 struct window *sel_w = XWINDOW (old_selected_window);
16702 /* Select mode line face based on the real selected window. */
16703 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16704 current_buffer->mode_line_format);
16705 ++n;
16708 if (WINDOW_WANTS_HEADER_LINE_P (w))
16710 display_mode_line (w, HEADER_LINE_FACE_ID,
16711 current_buffer->header_line_format);
16712 ++n;
16715 selected_frame = old_selected_frame;
16716 selected_window = old_selected_window;
16717 return n;
16721 /* Display mode or top line of window W. FACE_ID specifies which line
16722 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16723 FORMAT is the mode line format to display. Value is the pixel
16724 height of the mode line displayed. */
16726 static int
16727 display_mode_line (w, face_id, format)
16728 struct window *w;
16729 enum face_id face_id;
16730 Lisp_Object format;
16732 struct it it;
16733 struct face *face;
16734 int count = SPECPDL_INDEX ();
16736 init_iterator (&it, w, -1, -1, NULL, face_id);
16737 /* Don't extend on a previously drawn mode-line.
16738 This may happen if called from pos_visible_p. */
16739 it.glyph_row->enabled_p = 0;
16740 prepare_desired_row (it.glyph_row);
16742 it.glyph_row->mode_line_p = 1;
16744 if (! mode_line_inverse_video)
16745 /* Force the mode-line to be displayed in the default face. */
16746 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16748 record_unwind_protect (unwind_format_mode_line,
16749 format_mode_line_unwind_data (NULL, 0));
16751 mode_line_target = MODE_LINE_DISPLAY;
16753 /* Temporarily make frame's keyboard the current kboard so that
16754 kboard-local variables in the mode_line_format will get the right
16755 values. */
16756 push_frame_kboard (it.f);
16757 record_unwind_save_match_data ();
16758 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16759 pop_frame_kboard ();
16761 unbind_to (count, Qnil);
16763 /* Fill up with spaces. */
16764 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16766 compute_line_metrics (&it);
16767 it.glyph_row->full_width_p = 1;
16768 it.glyph_row->continued_p = 0;
16769 it.glyph_row->truncated_on_left_p = 0;
16770 it.glyph_row->truncated_on_right_p = 0;
16772 /* Make a 3D mode-line have a shadow at its right end. */
16773 face = FACE_FROM_ID (it.f, face_id);
16774 extend_face_to_end_of_line (&it);
16775 if (face->box != FACE_NO_BOX)
16777 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16778 + it.glyph_row->used[TEXT_AREA] - 1);
16779 last->right_box_line_p = 1;
16782 return it.glyph_row->height;
16785 /* Move element ELT in LIST to the front of LIST.
16786 Return the updated list. */
16788 static Lisp_Object
16789 move_elt_to_front (elt, list)
16790 Lisp_Object elt, list;
16792 register Lisp_Object tail, prev;
16793 register Lisp_Object tem;
16795 tail = list;
16796 prev = Qnil;
16797 while (CONSP (tail))
16799 tem = XCAR (tail);
16801 if (EQ (elt, tem))
16803 /* Splice out the link TAIL. */
16804 if (NILP (prev))
16805 list = XCDR (tail);
16806 else
16807 Fsetcdr (prev, XCDR (tail));
16809 /* Now make it the first. */
16810 Fsetcdr (tail, list);
16811 return tail;
16813 else
16814 prev = tail;
16815 tail = XCDR (tail);
16816 QUIT;
16819 /* Not found--return unchanged LIST. */
16820 return list;
16823 /* Contribute ELT to the mode line for window IT->w. How it
16824 translates into text depends on its data type.
16826 IT describes the display environment in which we display, as usual.
16828 DEPTH is the depth in recursion. It is used to prevent
16829 infinite recursion here.
16831 FIELD_WIDTH is the number of characters the display of ELT should
16832 occupy in the mode line, and PRECISION is the maximum number of
16833 characters to display from ELT's representation. See
16834 display_string for details.
16836 Returns the hpos of the end of the text generated by ELT.
16838 PROPS is a property list to add to any string we encounter.
16840 If RISKY is nonzero, remove (disregard) any properties in any string
16841 we encounter, and ignore :eval and :propertize.
16843 The global variable `mode_line_target' determines whether the
16844 output is passed to `store_mode_line_noprop',
16845 `store_mode_line_string', or `display_string'. */
16847 static int
16848 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16849 struct it *it;
16850 int depth;
16851 int field_width, precision;
16852 Lisp_Object elt, props;
16853 int risky;
16855 int n = 0, field, prec;
16856 int literal = 0;
16858 tail_recurse:
16859 if (depth > 100)
16860 elt = build_string ("*too-deep*");
16862 depth++;
16864 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16866 case Lisp_String:
16868 /* A string: output it and check for %-constructs within it. */
16869 unsigned char c;
16870 int offset = 0;
16872 if (SCHARS (elt) > 0
16873 && (!NILP (props) || risky))
16875 Lisp_Object oprops, aelt;
16876 oprops = Ftext_properties_at (make_number (0), elt);
16878 /* If the starting string's properties are not what
16879 we want, translate the string. Also, if the string
16880 is risky, do that anyway. */
16882 if (NILP (Fequal (props, oprops)) || risky)
16884 /* If the starting string has properties,
16885 merge the specified ones onto the existing ones. */
16886 if (! NILP (oprops) && !risky)
16888 Lisp_Object tem;
16890 oprops = Fcopy_sequence (oprops);
16891 tem = props;
16892 while (CONSP (tem))
16894 oprops = Fplist_put (oprops, XCAR (tem),
16895 XCAR (XCDR (tem)));
16896 tem = XCDR (XCDR (tem));
16898 props = oprops;
16901 aelt = Fassoc (elt, mode_line_proptrans_alist);
16902 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16904 /* AELT is what we want. Move it to the front
16905 without consing. */
16906 elt = XCAR (aelt);
16907 mode_line_proptrans_alist
16908 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16910 else
16912 Lisp_Object tem;
16914 /* If AELT has the wrong props, it is useless.
16915 so get rid of it. */
16916 if (! NILP (aelt))
16917 mode_line_proptrans_alist
16918 = Fdelq (aelt, mode_line_proptrans_alist);
16920 elt = Fcopy_sequence (elt);
16921 Fset_text_properties (make_number (0), Flength (elt),
16922 props, elt);
16923 /* Add this item to mode_line_proptrans_alist. */
16924 mode_line_proptrans_alist
16925 = Fcons (Fcons (elt, props),
16926 mode_line_proptrans_alist);
16927 /* Truncate mode_line_proptrans_alist
16928 to at most 50 elements. */
16929 tem = Fnthcdr (make_number (50),
16930 mode_line_proptrans_alist);
16931 if (! NILP (tem))
16932 XSETCDR (tem, Qnil);
16937 offset = 0;
16939 if (literal)
16941 prec = precision - n;
16942 switch (mode_line_target)
16944 case MODE_LINE_NOPROP:
16945 case MODE_LINE_TITLE:
16946 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16947 break;
16948 case MODE_LINE_STRING:
16949 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16950 break;
16951 case MODE_LINE_DISPLAY:
16952 n += display_string (NULL, elt, Qnil, 0, 0, it,
16953 0, prec, 0, STRING_MULTIBYTE (elt));
16954 break;
16957 break;
16960 /* Handle the non-literal case. */
16962 while ((precision <= 0 || n < precision)
16963 && SREF (elt, offset) != 0
16964 && (mode_line_target != MODE_LINE_DISPLAY
16965 || it->current_x < it->last_visible_x))
16967 int last_offset = offset;
16969 /* Advance to end of string or next format specifier. */
16970 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16973 if (offset - 1 != last_offset)
16975 int nchars, nbytes;
16977 /* Output to end of string or up to '%'. Field width
16978 is length of string. Don't output more than
16979 PRECISION allows us. */
16980 offset--;
16982 prec = c_string_width (SDATA (elt) + last_offset,
16983 offset - last_offset, precision - n,
16984 &nchars, &nbytes);
16986 switch (mode_line_target)
16988 case MODE_LINE_NOPROP:
16989 case MODE_LINE_TITLE:
16990 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16991 break;
16992 case MODE_LINE_STRING:
16994 int bytepos = last_offset;
16995 int charpos = string_byte_to_char (elt, bytepos);
16996 int endpos = (precision <= 0
16997 ? string_byte_to_char (elt, offset)
16998 : charpos + nchars);
17000 n += store_mode_line_string (NULL,
17001 Fsubstring (elt, make_number (charpos),
17002 make_number (endpos)),
17003 0, 0, 0, Qnil);
17005 break;
17006 case MODE_LINE_DISPLAY:
17008 int bytepos = last_offset;
17009 int charpos = string_byte_to_char (elt, bytepos);
17011 if (precision <= 0)
17012 nchars = string_byte_to_char (elt, offset) - charpos;
17013 n += display_string (NULL, elt, Qnil, 0, charpos,
17014 it, 0, nchars, 0,
17015 STRING_MULTIBYTE (elt));
17017 break;
17020 else /* c == '%' */
17022 int percent_position = offset;
17024 /* Get the specified minimum width. Zero means
17025 don't pad. */
17026 field = 0;
17027 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17028 field = field * 10 + c - '0';
17030 /* Don't pad beyond the total padding allowed. */
17031 if (field_width - n > 0 && field > field_width - n)
17032 field = field_width - n;
17034 /* Note that either PRECISION <= 0 or N < PRECISION. */
17035 prec = precision - n;
17037 if (c == 'M')
17038 n += display_mode_element (it, depth, field, prec,
17039 Vglobal_mode_string, props,
17040 risky);
17041 else if (c != 0)
17043 int multibyte;
17044 int bytepos, charpos;
17045 unsigned char *spec;
17047 bytepos = percent_position;
17048 charpos = (STRING_MULTIBYTE (elt)
17049 ? string_byte_to_char (elt, bytepos)
17050 : bytepos);
17052 spec
17053 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17055 switch (mode_line_target)
17057 case MODE_LINE_NOPROP:
17058 case MODE_LINE_TITLE:
17059 n += store_mode_line_noprop (spec, field, prec);
17060 break;
17061 case MODE_LINE_STRING:
17063 int len = strlen (spec);
17064 Lisp_Object tem = make_string (spec, len);
17065 props = Ftext_properties_at (make_number (charpos), elt);
17066 /* Should only keep face property in props */
17067 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17069 break;
17070 case MODE_LINE_DISPLAY:
17072 int nglyphs_before, nwritten;
17074 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17075 nwritten = display_string (spec, Qnil, elt,
17076 charpos, 0, it,
17077 field, prec, 0,
17078 multibyte);
17080 /* Assign to the glyphs written above the
17081 string where the `%x' came from, position
17082 of the `%'. */
17083 if (nwritten > 0)
17085 struct glyph *glyph
17086 = (it->glyph_row->glyphs[TEXT_AREA]
17087 + nglyphs_before);
17088 int i;
17090 for (i = 0; i < nwritten; ++i)
17092 glyph[i].object = elt;
17093 glyph[i].charpos = charpos;
17096 n += nwritten;
17099 break;
17102 else /* c == 0 */
17103 break;
17107 break;
17109 case Lisp_Symbol:
17110 /* A symbol: process the value of the symbol recursively
17111 as if it appeared here directly. Avoid error if symbol void.
17112 Special case: if value of symbol is a string, output the string
17113 literally. */
17115 register Lisp_Object tem;
17117 /* If the variable is not marked as risky to set
17118 then its contents are risky to use. */
17119 if (NILP (Fget (elt, Qrisky_local_variable)))
17120 risky = 1;
17122 tem = Fboundp (elt);
17123 if (!NILP (tem))
17125 tem = Fsymbol_value (elt);
17126 /* If value is a string, output that string literally:
17127 don't check for % within it. */
17128 if (STRINGP (tem))
17129 literal = 1;
17131 if (!EQ (tem, elt))
17133 /* Give up right away for nil or t. */
17134 elt = tem;
17135 goto tail_recurse;
17139 break;
17141 case Lisp_Cons:
17143 register Lisp_Object car, tem;
17145 /* A cons cell: five distinct cases.
17146 If first element is :eval or :propertize, do something special.
17147 If first element is a string or a cons, process all the elements
17148 and effectively concatenate them.
17149 If first element is a negative number, truncate displaying cdr to
17150 at most that many characters. If positive, pad (with spaces)
17151 to at least that many characters.
17152 If first element is a symbol, process the cadr or caddr recursively
17153 according to whether the symbol's value is non-nil or nil. */
17154 car = XCAR (elt);
17155 if (EQ (car, QCeval))
17157 /* An element of the form (:eval FORM) means evaluate FORM
17158 and use the result as mode line elements. */
17160 if (risky)
17161 break;
17163 if (CONSP (XCDR (elt)))
17165 Lisp_Object spec;
17166 spec = safe_eval (XCAR (XCDR (elt)));
17167 n += display_mode_element (it, depth, field_width - n,
17168 precision - n, spec, props,
17169 risky);
17172 else if (EQ (car, QCpropertize))
17174 /* An element of the form (:propertize ELT PROPS...)
17175 means display ELT but applying properties PROPS. */
17177 if (risky)
17178 break;
17180 if (CONSP (XCDR (elt)))
17181 n += display_mode_element (it, depth, field_width - n,
17182 precision - n, XCAR (XCDR (elt)),
17183 XCDR (XCDR (elt)), risky);
17185 else if (SYMBOLP (car))
17187 tem = Fboundp (car);
17188 elt = XCDR (elt);
17189 if (!CONSP (elt))
17190 goto invalid;
17191 /* elt is now the cdr, and we know it is a cons cell.
17192 Use its car if CAR has a non-nil value. */
17193 if (!NILP (tem))
17195 tem = Fsymbol_value (car);
17196 if (!NILP (tem))
17198 elt = XCAR (elt);
17199 goto tail_recurse;
17202 /* Symbol's value is nil (or symbol is unbound)
17203 Get the cddr of the original list
17204 and if possible find the caddr and use that. */
17205 elt = XCDR (elt);
17206 if (NILP (elt))
17207 break;
17208 else if (!CONSP (elt))
17209 goto invalid;
17210 elt = XCAR (elt);
17211 goto tail_recurse;
17213 else if (INTEGERP (car))
17215 register int lim = XINT (car);
17216 elt = XCDR (elt);
17217 if (lim < 0)
17219 /* Negative int means reduce maximum width. */
17220 if (precision <= 0)
17221 precision = -lim;
17222 else
17223 precision = min (precision, -lim);
17225 else if (lim > 0)
17227 /* Padding specified. Don't let it be more than
17228 current maximum. */
17229 if (precision > 0)
17230 lim = min (precision, lim);
17232 /* If that's more padding than already wanted, queue it.
17233 But don't reduce padding already specified even if
17234 that is beyond the current truncation point. */
17235 field_width = max (lim, field_width);
17237 goto tail_recurse;
17239 else if (STRINGP (car) || CONSP (car))
17241 register int limit = 50;
17242 /* Limit is to protect against circular lists. */
17243 while (CONSP (elt)
17244 && --limit > 0
17245 && (precision <= 0 || n < precision))
17247 n += display_mode_element (it, depth,
17248 /* Do padding only after the last
17249 element in the list. */
17250 (! CONSP (XCDR (elt))
17251 ? field_width - n
17252 : 0),
17253 precision - n, XCAR (elt),
17254 props, risky);
17255 elt = XCDR (elt);
17259 break;
17261 default:
17262 invalid:
17263 elt = build_string ("*invalid*");
17264 goto tail_recurse;
17267 /* Pad to FIELD_WIDTH. */
17268 if (field_width > 0 && n < field_width)
17270 switch (mode_line_target)
17272 case MODE_LINE_NOPROP:
17273 case MODE_LINE_TITLE:
17274 n += store_mode_line_noprop ("", field_width - n, 0);
17275 break;
17276 case MODE_LINE_STRING:
17277 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17278 break;
17279 case MODE_LINE_DISPLAY:
17280 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17281 0, 0, 0);
17282 break;
17286 return n;
17289 /* Store a mode-line string element in mode_line_string_list.
17291 If STRING is non-null, display that C string. Otherwise, the Lisp
17292 string LISP_STRING is displayed.
17294 FIELD_WIDTH is the minimum number of output glyphs to produce.
17295 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17296 with spaces. FIELD_WIDTH <= 0 means don't pad.
17298 PRECISION is the maximum number of characters to output from
17299 STRING. PRECISION <= 0 means don't truncate the string.
17301 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17302 properties to the string.
17304 PROPS are the properties to add to the string.
17305 The mode_line_string_face face property is always added to the string.
17308 static int
17309 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17310 char *string;
17311 Lisp_Object lisp_string;
17312 int copy_string;
17313 int field_width;
17314 int precision;
17315 Lisp_Object props;
17317 int len;
17318 int n = 0;
17320 if (string != NULL)
17322 len = strlen (string);
17323 if (precision > 0 && len > precision)
17324 len = precision;
17325 lisp_string = make_string (string, len);
17326 if (NILP (props))
17327 props = mode_line_string_face_prop;
17328 else if (!NILP (mode_line_string_face))
17330 Lisp_Object face = Fplist_get (props, Qface);
17331 props = Fcopy_sequence (props);
17332 if (NILP (face))
17333 face = mode_line_string_face;
17334 else
17335 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17336 props = Fplist_put (props, Qface, face);
17338 Fadd_text_properties (make_number (0), make_number (len),
17339 props, lisp_string);
17341 else
17343 len = XFASTINT (Flength (lisp_string));
17344 if (precision > 0 && len > precision)
17346 len = precision;
17347 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17348 precision = -1;
17350 if (!NILP (mode_line_string_face))
17352 Lisp_Object face;
17353 if (NILP (props))
17354 props = Ftext_properties_at (make_number (0), lisp_string);
17355 face = Fplist_get (props, Qface);
17356 if (NILP (face))
17357 face = mode_line_string_face;
17358 else
17359 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17360 props = Fcons (Qface, Fcons (face, Qnil));
17361 if (copy_string)
17362 lisp_string = Fcopy_sequence (lisp_string);
17364 if (!NILP (props))
17365 Fadd_text_properties (make_number (0), make_number (len),
17366 props, lisp_string);
17369 if (len > 0)
17371 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17372 n += len;
17375 if (field_width > len)
17377 field_width -= len;
17378 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17379 if (!NILP (props))
17380 Fadd_text_properties (make_number (0), make_number (field_width),
17381 props, lisp_string);
17382 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17383 n += field_width;
17386 return n;
17390 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17391 1, 4, 0,
17392 doc: /* Format a string out of a mode line format specification.
17393 First arg FORMAT specifies the mode line format (see `mode-line-format'
17394 for details) to use.
17396 Optional second arg FACE specifies the face property to put
17397 on all characters for which no face is specified.
17398 The value t means whatever face the window's mode line currently uses
17399 \(either `mode-line' or `mode-line-inactive', depending).
17400 A value of nil means the default is no face property.
17401 If FACE is an integer, the value string has no text properties.
17403 Optional third and fourth args WINDOW and BUFFER specify the window
17404 and buffer to use as the context for the formatting (defaults
17405 are the selected window and the window's buffer). */)
17406 (format, face, window, buffer)
17407 Lisp_Object format, face, window, buffer;
17409 struct it it;
17410 int len;
17411 struct window *w;
17412 struct buffer *old_buffer = NULL;
17413 int face_id = -1;
17414 int no_props = INTEGERP (face);
17415 int count = SPECPDL_INDEX ();
17416 Lisp_Object str;
17417 int string_start = 0;
17419 if (NILP (window))
17420 window = selected_window;
17421 CHECK_WINDOW (window);
17422 w = XWINDOW (window);
17424 if (NILP (buffer))
17425 buffer = w->buffer;
17426 CHECK_BUFFER (buffer);
17428 /* Make formatting the modeline a non-op when noninteractive, otherwise
17429 there will be problems later caused by a partially initialized frame. */
17430 if (NILP (format) || noninteractive)
17431 return build_string ("");
17433 if (no_props)
17434 face = Qnil;
17436 if (!NILP (face))
17438 if (EQ (face, Qt))
17439 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17440 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17443 if (face_id < 0)
17444 face_id = DEFAULT_FACE_ID;
17446 if (XBUFFER (buffer) != current_buffer)
17447 old_buffer = current_buffer;
17449 /* Save things including mode_line_proptrans_alist,
17450 and set that to nil so that we don't alter the outer value. */
17451 record_unwind_protect (unwind_format_mode_line,
17452 format_mode_line_unwind_data (old_buffer, 1));
17453 mode_line_proptrans_alist = Qnil;
17455 if (old_buffer)
17456 set_buffer_internal_1 (XBUFFER (buffer));
17458 init_iterator (&it, w, -1, -1, NULL, face_id);
17460 if (no_props)
17462 mode_line_target = MODE_LINE_NOPROP;
17463 mode_line_string_face_prop = Qnil;
17464 mode_line_string_list = Qnil;
17465 string_start = MODE_LINE_NOPROP_LEN (0);
17467 else
17469 mode_line_target = MODE_LINE_STRING;
17470 mode_line_string_list = Qnil;
17471 mode_line_string_face = face;
17472 mode_line_string_face_prop
17473 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17476 push_frame_kboard (it.f);
17477 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17478 pop_frame_kboard ();
17480 if (no_props)
17482 len = MODE_LINE_NOPROP_LEN (string_start);
17483 str = make_string (mode_line_noprop_buf + string_start, len);
17485 else
17487 mode_line_string_list = Fnreverse (mode_line_string_list);
17488 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17489 make_string ("", 0));
17492 unbind_to (count, Qnil);
17493 return str;
17496 /* Write a null-terminated, right justified decimal representation of
17497 the positive integer D to BUF using a minimal field width WIDTH. */
17499 static void
17500 pint2str (buf, width, d)
17501 register char *buf;
17502 register int width;
17503 register int d;
17505 register char *p = buf;
17507 if (d <= 0)
17508 *p++ = '0';
17509 else
17511 while (d > 0)
17513 *p++ = d % 10 + '0';
17514 d /= 10;
17518 for (width -= (int) (p - buf); width > 0; --width)
17519 *p++ = ' ';
17520 *p-- = '\0';
17521 while (p > buf)
17523 d = *buf;
17524 *buf++ = *p;
17525 *p-- = d;
17529 /* Write a null-terminated, right justified decimal and "human
17530 readable" representation of the nonnegative integer D to BUF using
17531 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17533 static const char power_letter[] =
17535 0, /* not used */
17536 'k', /* kilo */
17537 'M', /* mega */
17538 'G', /* giga */
17539 'T', /* tera */
17540 'P', /* peta */
17541 'E', /* exa */
17542 'Z', /* zetta */
17543 'Y' /* yotta */
17546 static void
17547 pint2hrstr (buf, width, d)
17548 char *buf;
17549 int width;
17550 int d;
17552 /* We aim to represent the nonnegative integer D as
17553 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17554 int quotient = d;
17555 int remainder = 0;
17556 /* -1 means: do not use TENTHS. */
17557 int tenths = -1;
17558 int exponent = 0;
17560 /* Length of QUOTIENT.TENTHS as a string. */
17561 int length;
17563 char * psuffix;
17564 char * p;
17566 if (1000 <= quotient)
17568 /* Scale to the appropriate EXPONENT. */
17571 remainder = quotient % 1000;
17572 quotient /= 1000;
17573 exponent++;
17575 while (1000 <= quotient);
17577 /* Round to nearest and decide whether to use TENTHS or not. */
17578 if (quotient <= 9)
17580 tenths = remainder / 100;
17581 if (50 <= remainder % 100)
17583 if (tenths < 9)
17584 tenths++;
17585 else
17587 quotient++;
17588 if (quotient == 10)
17589 tenths = -1;
17590 else
17591 tenths = 0;
17595 else
17596 if (500 <= remainder)
17598 if (quotient < 999)
17599 quotient++;
17600 else
17602 quotient = 1;
17603 exponent++;
17604 tenths = 0;
17609 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17610 if (tenths == -1 && quotient <= 99)
17611 if (quotient <= 9)
17612 length = 1;
17613 else
17614 length = 2;
17615 else
17616 length = 3;
17617 p = psuffix = buf + max (width, length);
17619 /* Print EXPONENT. */
17620 if (exponent)
17621 *psuffix++ = power_letter[exponent];
17622 *psuffix = '\0';
17624 /* Print TENTHS. */
17625 if (tenths >= 0)
17627 *--p = '0' + tenths;
17628 *--p = '.';
17631 /* Print QUOTIENT. */
17634 int digit = quotient % 10;
17635 *--p = '0' + digit;
17637 while ((quotient /= 10) != 0);
17639 /* Print leading spaces. */
17640 while (buf < p)
17641 *--p = ' ';
17644 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17645 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17646 type of CODING_SYSTEM. Return updated pointer into BUF. */
17648 static unsigned char invalid_eol_type[] = "(*invalid*)";
17650 static char *
17651 decode_mode_spec_coding (coding_system, buf, eol_flag)
17652 Lisp_Object coding_system;
17653 register char *buf;
17654 int eol_flag;
17656 Lisp_Object val;
17657 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17658 const unsigned char *eol_str;
17659 int eol_str_len;
17660 /* The EOL conversion we are using. */
17661 Lisp_Object eoltype;
17663 val = Fget (coding_system, Qcoding_system);
17664 eoltype = Qnil;
17666 if (!VECTORP (val)) /* Not yet decided. */
17668 if (multibyte)
17669 *buf++ = '-';
17670 if (eol_flag)
17671 eoltype = eol_mnemonic_undecided;
17672 /* Don't mention EOL conversion if it isn't decided. */
17674 else
17676 Lisp_Object eolvalue;
17678 eolvalue = Fget (coding_system, Qeol_type);
17680 if (multibyte)
17681 *buf++ = XFASTINT (AREF (val, 1));
17683 if (eol_flag)
17685 /* The EOL conversion that is normal on this system. */
17687 if (NILP (eolvalue)) /* Not yet decided. */
17688 eoltype = eol_mnemonic_undecided;
17689 else if (VECTORP (eolvalue)) /* Not yet decided. */
17690 eoltype = eol_mnemonic_undecided;
17691 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17692 eoltype = (XFASTINT (eolvalue) == 0
17693 ? eol_mnemonic_unix
17694 : (XFASTINT (eolvalue) == 1
17695 ? eol_mnemonic_dos : eol_mnemonic_mac));
17699 if (eol_flag)
17701 /* Mention the EOL conversion if it is not the usual one. */
17702 if (STRINGP (eoltype))
17704 eol_str = SDATA (eoltype);
17705 eol_str_len = SBYTES (eoltype);
17707 else if (INTEGERP (eoltype)
17708 && CHAR_VALID_P (XINT (eoltype), 0))
17710 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17711 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17712 eol_str = tmp;
17714 else
17716 eol_str = invalid_eol_type;
17717 eol_str_len = sizeof (invalid_eol_type) - 1;
17719 bcopy (eol_str, buf, eol_str_len);
17720 buf += eol_str_len;
17723 return buf;
17726 /* Return a string for the output of a mode line %-spec for window W,
17727 generated by character C. PRECISION >= 0 means don't return a
17728 string longer than that value. FIELD_WIDTH > 0 means pad the
17729 string returned with spaces to that value. Return 1 in *MULTIBYTE
17730 if the result is multibyte text.
17732 Note we operate on the current buffer for most purposes,
17733 the exception being w->base_line_pos. */
17735 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17737 static char *
17738 decode_mode_spec (w, c, field_width, precision, multibyte)
17739 struct window *w;
17740 register int c;
17741 int field_width, precision;
17742 int *multibyte;
17744 Lisp_Object obj;
17745 struct frame *f = XFRAME (WINDOW_FRAME (w));
17746 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17747 struct buffer *b = current_buffer;
17749 obj = Qnil;
17750 *multibyte = 0;
17752 switch (c)
17754 case '*':
17755 if (!NILP (b->read_only))
17756 return "%";
17757 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17758 return "*";
17759 return "-";
17761 case '+':
17762 /* This differs from %* only for a modified read-only buffer. */
17763 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17764 return "*";
17765 if (!NILP (b->read_only))
17766 return "%";
17767 return "-";
17769 case '&':
17770 /* This differs from %* in ignoring read-only-ness. */
17771 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17772 return "*";
17773 return "-";
17775 case '%':
17776 return "%";
17778 case '[':
17780 int i;
17781 char *p;
17783 if (command_loop_level > 5)
17784 return "[[[... ";
17785 p = decode_mode_spec_buf;
17786 for (i = 0; i < command_loop_level; i++)
17787 *p++ = '[';
17788 *p = 0;
17789 return decode_mode_spec_buf;
17792 case ']':
17794 int i;
17795 char *p;
17797 if (command_loop_level > 5)
17798 return " ...]]]";
17799 p = decode_mode_spec_buf;
17800 for (i = 0; i < command_loop_level; i++)
17801 *p++ = ']';
17802 *p = 0;
17803 return decode_mode_spec_buf;
17806 case '-':
17808 register int i;
17810 /* Let lots_of_dashes be a string of infinite length. */
17811 if (mode_line_target == MODE_LINE_NOPROP ||
17812 mode_line_target == MODE_LINE_STRING)
17813 return "--";
17814 if (field_width <= 0
17815 || field_width > sizeof (lots_of_dashes))
17817 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17818 decode_mode_spec_buf[i] = '-';
17819 decode_mode_spec_buf[i] = '\0';
17820 return decode_mode_spec_buf;
17822 else
17823 return lots_of_dashes;
17826 case 'b':
17827 obj = b->name;
17828 break;
17830 case 'c':
17831 /* %c and %l are ignored in `frame-title-format'.
17832 (In redisplay_internal, the frame title is drawn _before_ the
17833 windows are updated, so the stuff which depends on actual
17834 window contents (such as %l) may fail to render properly, or
17835 even crash emacs.) */
17836 if (mode_line_target == MODE_LINE_TITLE)
17837 return "";
17838 else
17840 int col = (int) current_column (); /* iftc */
17841 w->column_number_displayed = make_number (col);
17842 pint2str (decode_mode_spec_buf, field_width, col);
17843 return decode_mode_spec_buf;
17846 case 'e':
17847 #ifndef SYSTEM_MALLOC
17849 if (NILP (Vmemory_full))
17850 return "";
17851 else
17852 return "!MEM FULL! ";
17854 #else
17855 return "";
17856 #endif
17858 case 'F':
17859 /* %F displays the frame name. */
17860 if (!NILP (f->title))
17861 return (char *) SDATA (f->title);
17862 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17863 return (char *) SDATA (f->name);
17864 return "Emacs";
17866 case 'f':
17867 obj = b->filename;
17868 break;
17870 case 'i':
17872 int size = ZV - BEGV;
17873 pint2str (decode_mode_spec_buf, field_width, size);
17874 return decode_mode_spec_buf;
17877 case 'I':
17879 int size = ZV - BEGV;
17880 pint2hrstr (decode_mode_spec_buf, field_width, size);
17881 return decode_mode_spec_buf;
17884 case 'l':
17886 int startpos, startpos_byte, line, linepos, linepos_byte;
17887 int topline, nlines, junk, height;
17889 /* %c and %l are ignored in `frame-title-format'. */
17890 if (mode_line_target == MODE_LINE_TITLE)
17891 return "";
17893 startpos = XMARKER (w->start)->charpos;
17894 startpos_byte = marker_byte_position (w->start);
17895 height = WINDOW_TOTAL_LINES (w);
17897 /* If we decided that this buffer isn't suitable for line numbers,
17898 don't forget that too fast. */
17899 if (EQ (w->base_line_pos, w->buffer))
17900 goto no_value;
17901 /* But do forget it, if the window shows a different buffer now. */
17902 else if (BUFFERP (w->base_line_pos))
17903 w->base_line_pos = Qnil;
17905 /* If the buffer is very big, don't waste time. */
17906 if (INTEGERP (Vline_number_display_limit)
17907 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17909 w->base_line_pos = Qnil;
17910 w->base_line_number = Qnil;
17911 goto no_value;
17914 if (!NILP (w->base_line_number)
17915 && !NILP (w->base_line_pos)
17916 && XFASTINT (w->base_line_pos) <= startpos)
17918 line = XFASTINT (w->base_line_number);
17919 linepos = XFASTINT (w->base_line_pos);
17920 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17922 else
17924 line = 1;
17925 linepos = BUF_BEGV (b);
17926 linepos_byte = BUF_BEGV_BYTE (b);
17929 /* Count lines from base line to window start position. */
17930 nlines = display_count_lines (linepos, linepos_byte,
17931 startpos_byte,
17932 startpos, &junk);
17934 topline = nlines + line;
17936 /* Determine a new base line, if the old one is too close
17937 or too far away, or if we did not have one.
17938 "Too close" means it's plausible a scroll-down would
17939 go back past it. */
17940 if (startpos == BUF_BEGV (b))
17942 w->base_line_number = make_number (topline);
17943 w->base_line_pos = make_number (BUF_BEGV (b));
17945 else if (nlines < height + 25 || nlines > height * 3 + 50
17946 || linepos == BUF_BEGV (b))
17948 int limit = BUF_BEGV (b);
17949 int limit_byte = BUF_BEGV_BYTE (b);
17950 int position;
17951 int distance = (height * 2 + 30) * line_number_display_limit_width;
17953 if (startpos - distance > limit)
17955 limit = startpos - distance;
17956 limit_byte = CHAR_TO_BYTE (limit);
17959 nlines = display_count_lines (startpos, startpos_byte,
17960 limit_byte,
17961 - (height * 2 + 30),
17962 &position);
17963 /* If we couldn't find the lines we wanted within
17964 line_number_display_limit_width chars per line,
17965 give up on line numbers for this window. */
17966 if (position == limit_byte && limit == startpos - distance)
17968 w->base_line_pos = w->buffer;
17969 w->base_line_number = Qnil;
17970 goto no_value;
17973 w->base_line_number = make_number (topline - nlines);
17974 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17977 /* Now count lines from the start pos to point. */
17978 nlines = display_count_lines (startpos, startpos_byte,
17979 PT_BYTE, PT, &junk);
17981 /* Record that we did display the line number. */
17982 line_number_displayed = 1;
17984 /* Make the string to show. */
17985 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17986 return decode_mode_spec_buf;
17987 no_value:
17989 char* p = decode_mode_spec_buf;
17990 int pad = field_width - 2;
17991 while (pad-- > 0)
17992 *p++ = ' ';
17993 *p++ = '?';
17994 *p++ = '?';
17995 *p = '\0';
17996 return decode_mode_spec_buf;
17999 break;
18001 case 'm':
18002 obj = b->mode_name;
18003 break;
18005 case 'n':
18006 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18007 return " Narrow";
18008 break;
18010 case 'p':
18012 int pos = marker_position (w->start);
18013 int total = BUF_ZV (b) - BUF_BEGV (b);
18015 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18017 if (pos <= BUF_BEGV (b))
18018 return "All";
18019 else
18020 return "Bottom";
18022 else if (pos <= BUF_BEGV (b))
18023 return "Top";
18024 else
18026 if (total > 1000000)
18027 /* Do it differently for a large value, to avoid overflow. */
18028 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18029 else
18030 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18031 /* We can't normally display a 3-digit number,
18032 so get us a 2-digit number that is close. */
18033 if (total == 100)
18034 total = 99;
18035 sprintf (decode_mode_spec_buf, "%2d%%", total);
18036 return decode_mode_spec_buf;
18040 /* Display percentage of size above the bottom of the screen. */
18041 case 'P':
18043 int toppos = marker_position (w->start);
18044 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18045 int total = BUF_ZV (b) - BUF_BEGV (b);
18047 if (botpos >= BUF_ZV (b))
18049 if (toppos <= BUF_BEGV (b))
18050 return "All";
18051 else
18052 return "Bottom";
18054 else
18056 if (total > 1000000)
18057 /* Do it differently for a large value, to avoid overflow. */
18058 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18059 else
18060 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18061 /* We can't normally display a 3-digit number,
18062 so get us a 2-digit number that is close. */
18063 if (total == 100)
18064 total = 99;
18065 if (toppos <= BUF_BEGV (b))
18066 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18067 else
18068 sprintf (decode_mode_spec_buf, "%2d%%", total);
18069 return decode_mode_spec_buf;
18073 case 's':
18074 /* status of process */
18075 obj = Fget_buffer_process (Fcurrent_buffer ());
18076 if (NILP (obj))
18077 return "no process";
18078 #ifdef subprocesses
18079 obj = Fsymbol_name (Fprocess_status (obj));
18080 #endif
18081 break;
18083 case 't': /* indicate TEXT or BINARY */
18084 #ifdef MODE_LINE_BINARY_TEXT
18085 return MODE_LINE_BINARY_TEXT (b);
18086 #else
18087 return "T";
18088 #endif
18090 case 'z':
18091 /* coding-system (not including end-of-line format) */
18092 case 'Z':
18093 /* coding-system (including end-of-line type) */
18095 int eol_flag = (c == 'Z');
18096 char *p = decode_mode_spec_buf;
18098 if (! FRAME_WINDOW_P (f))
18100 /* No need to mention EOL here--the terminal never needs
18101 to do EOL conversion. */
18102 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
18103 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
18105 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18106 p, eol_flag);
18108 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18109 #ifdef subprocesses
18110 obj = Fget_buffer_process (Fcurrent_buffer ());
18111 if (PROCESSP (obj))
18113 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18114 p, eol_flag);
18115 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18116 p, eol_flag);
18118 #endif /* subprocesses */
18119 #endif /* 0 */
18120 *p = 0;
18121 return decode_mode_spec_buf;
18125 if (STRINGP (obj))
18127 *multibyte = STRING_MULTIBYTE (obj);
18128 return (char *) SDATA (obj);
18130 else
18131 return "";
18135 /* Count up to COUNT lines starting from START / START_BYTE.
18136 But don't go beyond LIMIT_BYTE.
18137 Return the number of lines thus found (always nonnegative).
18139 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18141 static int
18142 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18143 int start, start_byte, limit_byte, count;
18144 int *byte_pos_ptr;
18146 register unsigned char *cursor;
18147 unsigned char *base;
18149 register int ceiling;
18150 register unsigned char *ceiling_addr;
18151 int orig_count = count;
18153 /* If we are not in selective display mode,
18154 check only for newlines. */
18155 int selective_display = (!NILP (current_buffer->selective_display)
18156 && !INTEGERP (current_buffer->selective_display));
18158 if (count > 0)
18160 while (start_byte < limit_byte)
18162 ceiling = BUFFER_CEILING_OF (start_byte);
18163 ceiling = min (limit_byte - 1, ceiling);
18164 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18165 base = (cursor = BYTE_POS_ADDR (start_byte));
18166 while (1)
18168 if (selective_display)
18169 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18171 else
18172 while (*cursor != '\n' && ++cursor != ceiling_addr)
18175 if (cursor != ceiling_addr)
18177 if (--count == 0)
18179 start_byte += cursor - base + 1;
18180 *byte_pos_ptr = start_byte;
18181 return orig_count;
18183 else
18184 if (++cursor == ceiling_addr)
18185 break;
18187 else
18188 break;
18190 start_byte += cursor - base;
18193 else
18195 while (start_byte > limit_byte)
18197 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18198 ceiling = max (limit_byte, ceiling);
18199 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18200 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18201 while (1)
18203 if (selective_display)
18204 while (--cursor != ceiling_addr
18205 && *cursor != '\n' && *cursor != 015)
18207 else
18208 while (--cursor != ceiling_addr && *cursor != '\n')
18211 if (cursor != ceiling_addr)
18213 if (++count == 0)
18215 start_byte += cursor - base + 1;
18216 *byte_pos_ptr = start_byte;
18217 /* When scanning backwards, we should
18218 not count the newline posterior to which we stop. */
18219 return - orig_count - 1;
18222 else
18223 break;
18225 /* Here we add 1 to compensate for the last decrement
18226 of CURSOR, which took it past the valid range. */
18227 start_byte += cursor - base + 1;
18231 *byte_pos_ptr = limit_byte;
18233 if (count < 0)
18234 return - orig_count + count;
18235 return orig_count - count;
18241 /***********************************************************************
18242 Displaying strings
18243 ***********************************************************************/
18245 /* Display a NUL-terminated string, starting with index START.
18247 If STRING is non-null, display that C string. Otherwise, the Lisp
18248 string LISP_STRING is displayed.
18250 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18251 FACE_STRING. Display STRING or LISP_STRING with the face at
18252 FACE_STRING_POS in FACE_STRING:
18254 Display the string in the environment given by IT, but use the
18255 standard display table, temporarily.
18257 FIELD_WIDTH is the minimum number of output glyphs to produce.
18258 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18259 with spaces. If STRING has more characters, more than FIELD_WIDTH
18260 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18262 PRECISION is the maximum number of characters to output from
18263 STRING. PRECISION < 0 means don't truncate the string.
18265 This is roughly equivalent to printf format specifiers:
18267 FIELD_WIDTH PRECISION PRINTF
18268 ----------------------------------------
18269 -1 -1 %s
18270 -1 10 %.10s
18271 10 -1 %10s
18272 20 10 %20.10s
18274 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18275 display them, and < 0 means obey the current buffer's value of
18276 enable_multibyte_characters.
18278 Value is the number of columns displayed. */
18280 static int
18281 display_string (string, lisp_string, face_string, face_string_pos,
18282 start, it, field_width, precision, max_x, multibyte)
18283 unsigned char *string;
18284 Lisp_Object lisp_string;
18285 Lisp_Object face_string;
18286 int face_string_pos;
18287 int start;
18288 struct it *it;
18289 int field_width, precision, max_x;
18290 int multibyte;
18292 int hpos_at_start = it->hpos;
18293 int saved_face_id = it->face_id;
18294 struct glyph_row *row = it->glyph_row;
18296 /* Initialize the iterator IT for iteration over STRING beginning
18297 with index START. */
18298 reseat_to_string (it, string, lisp_string, start,
18299 precision, field_width, multibyte);
18301 /* If displaying STRING, set up the face of the iterator
18302 from LISP_STRING, if that's given. */
18303 if (STRINGP (face_string))
18305 int endptr;
18306 struct face *face;
18308 it->face_id
18309 = face_at_string_position (it->w, face_string, face_string_pos,
18310 0, it->region_beg_charpos,
18311 it->region_end_charpos,
18312 &endptr, it->base_face_id, 0);
18313 face = FACE_FROM_ID (it->f, it->face_id);
18314 it->face_box_p = face->box != FACE_NO_BOX;
18317 /* Set max_x to the maximum allowed X position. Don't let it go
18318 beyond the right edge of the window. */
18319 if (max_x <= 0)
18320 max_x = it->last_visible_x;
18321 else
18322 max_x = min (max_x, it->last_visible_x);
18324 /* Skip over display elements that are not visible. because IT->w is
18325 hscrolled. */
18326 if (it->current_x < it->first_visible_x)
18327 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18328 MOVE_TO_POS | MOVE_TO_X);
18330 row->ascent = it->max_ascent;
18331 row->height = it->max_ascent + it->max_descent;
18332 row->phys_ascent = it->max_phys_ascent;
18333 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18334 row->extra_line_spacing = it->max_extra_line_spacing;
18336 /* This condition is for the case that we are called with current_x
18337 past last_visible_x. */
18338 while (it->current_x < max_x)
18340 int x_before, x, n_glyphs_before, i, nglyphs;
18342 /* Get the next display element. */
18343 if (!get_next_display_element (it))
18344 break;
18346 /* Produce glyphs. */
18347 x_before = it->current_x;
18348 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18349 PRODUCE_GLYPHS (it);
18351 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18352 i = 0;
18353 x = x_before;
18354 while (i < nglyphs)
18356 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18358 if (!it->truncate_lines_p
18359 && x + glyph->pixel_width > max_x)
18361 /* End of continued line or max_x reached. */
18362 if (CHAR_GLYPH_PADDING_P (*glyph))
18364 /* A wide character is unbreakable. */
18365 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18366 it->current_x = x_before;
18368 else
18370 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18371 it->current_x = x;
18373 break;
18375 else if (x + glyph->pixel_width > it->first_visible_x)
18377 /* Glyph is at least partially visible. */
18378 ++it->hpos;
18379 if (x < it->first_visible_x)
18380 it->glyph_row->x = x - it->first_visible_x;
18382 else
18384 /* Glyph is off the left margin of the display area.
18385 Should not happen. */
18386 abort ();
18389 row->ascent = max (row->ascent, it->max_ascent);
18390 row->height = max (row->height, it->max_ascent + it->max_descent);
18391 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18392 row->phys_height = max (row->phys_height,
18393 it->max_phys_ascent + it->max_phys_descent);
18394 row->extra_line_spacing = max (row->extra_line_spacing,
18395 it->max_extra_line_spacing);
18396 x += glyph->pixel_width;
18397 ++i;
18400 /* Stop if max_x reached. */
18401 if (i < nglyphs)
18402 break;
18404 /* Stop at line ends. */
18405 if (ITERATOR_AT_END_OF_LINE_P (it))
18407 it->continuation_lines_width = 0;
18408 break;
18411 set_iterator_to_next (it, 1);
18413 /* Stop if truncating at the right edge. */
18414 if (it->truncate_lines_p
18415 && it->current_x >= it->last_visible_x)
18417 /* Add truncation mark, but don't do it if the line is
18418 truncated at a padding space. */
18419 if (IT_CHARPOS (*it) < it->string_nchars)
18421 if (!FRAME_WINDOW_P (it->f))
18423 int i, n;
18425 if (it->current_x > it->last_visible_x)
18427 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18428 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18429 break;
18430 for (n = row->used[TEXT_AREA]; i < n; ++i)
18432 row->used[TEXT_AREA] = i;
18433 produce_special_glyphs (it, IT_TRUNCATION);
18436 produce_special_glyphs (it, IT_TRUNCATION);
18438 it->glyph_row->truncated_on_right_p = 1;
18440 break;
18444 /* Maybe insert a truncation at the left. */
18445 if (it->first_visible_x
18446 && IT_CHARPOS (*it) > 0)
18448 if (!FRAME_WINDOW_P (it->f))
18449 insert_left_trunc_glyphs (it);
18450 it->glyph_row->truncated_on_left_p = 1;
18453 it->face_id = saved_face_id;
18455 /* Value is number of columns displayed. */
18456 return it->hpos - hpos_at_start;
18461 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18462 appears as an element of LIST or as the car of an element of LIST.
18463 If PROPVAL is a list, compare each element against LIST in that
18464 way, and return 1/2 if any element of PROPVAL is found in LIST.
18465 Otherwise return 0. This function cannot quit.
18466 The return value is 2 if the text is invisible but with an ellipsis
18467 and 1 if it's invisible and without an ellipsis. */
18470 invisible_p (propval, list)
18471 register Lisp_Object propval;
18472 Lisp_Object list;
18474 register Lisp_Object tail, proptail;
18476 for (tail = list; CONSP (tail); tail = XCDR (tail))
18478 register Lisp_Object tem;
18479 tem = XCAR (tail);
18480 if (EQ (propval, tem))
18481 return 1;
18482 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18483 return NILP (XCDR (tem)) ? 1 : 2;
18486 if (CONSP (propval))
18488 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18490 Lisp_Object propelt;
18491 propelt = XCAR (proptail);
18492 for (tail = list; CONSP (tail); tail = XCDR (tail))
18494 register Lisp_Object tem;
18495 tem = XCAR (tail);
18496 if (EQ (propelt, tem))
18497 return 1;
18498 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18499 return NILP (XCDR (tem)) ? 1 : 2;
18504 return 0;
18507 /* Calculate a width or height in pixels from a specification using
18508 the following elements:
18510 SPEC ::=
18511 NUM - a (fractional) multiple of the default font width/height
18512 (NUM) - specifies exactly NUM pixels
18513 UNIT - a fixed number of pixels, see below.
18514 ELEMENT - size of a display element in pixels, see below.
18515 (NUM . SPEC) - equals NUM * SPEC
18516 (+ SPEC SPEC ...) - add pixel values
18517 (- SPEC SPEC ...) - subtract pixel values
18518 (- SPEC) - negate pixel value
18520 NUM ::=
18521 INT or FLOAT - a number constant
18522 SYMBOL - use symbol's (buffer local) variable binding.
18524 UNIT ::=
18525 in - pixels per inch *)
18526 mm - pixels per 1/1000 meter *)
18527 cm - pixels per 1/100 meter *)
18528 width - width of current font in pixels.
18529 height - height of current font in pixels.
18531 *) using the ratio(s) defined in display-pixels-per-inch.
18533 ELEMENT ::=
18535 left-fringe - left fringe width in pixels
18536 right-fringe - right fringe width in pixels
18538 left-margin - left margin width in pixels
18539 right-margin - right margin width in pixels
18541 scroll-bar - scroll-bar area width in pixels
18543 Examples:
18545 Pixels corresponding to 5 inches:
18546 (5 . in)
18548 Total width of non-text areas on left side of window (if scroll-bar is on left):
18549 '(space :width (+ left-fringe left-margin scroll-bar))
18551 Align to first text column (in header line):
18552 '(space :align-to 0)
18554 Align to middle of text area minus half the width of variable `my-image'
18555 containing a loaded image:
18556 '(space :align-to (0.5 . (- text my-image)))
18558 Width of left margin minus width of 1 character in the default font:
18559 '(space :width (- left-margin 1))
18561 Width of left margin minus width of 2 characters in the current font:
18562 '(space :width (- left-margin (2 . width)))
18564 Center 1 character over left-margin (in header line):
18565 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18567 Different ways to express width of left fringe plus left margin minus one pixel:
18568 '(space :width (- (+ left-fringe left-margin) (1)))
18569 '(space :width (+ left-fringe left-margin (- (1))))
18570 '(space :width (+ left-fringe left-margin (-1)))
18574 #define NUMVAL(X) \
18575 ((INTEGERP (X) || FLOATP (X)) \
18576 ? XFLOATINT (X) \
18577 : - 1)
18580 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18581 double *res;
18582 struct it *it;
18583 Lisp_Object prop;
18584 void *font;
18585 int width_p, *align_to;
18587 double pixels;
18589 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18590 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18592 if (NILP (prop))
18593 return OK_PIXELS (0);
18595 if (SYMBOLP (prop))
18597 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18599 char *unit = SDATA (SYMBOL_NAME (prop));
18601 if (unit[0] == 'i' && unit[1] == 'n')
18602 pixels = 1.0;
18603 else if (unit[0] == 'm' && unit[1] == 'm')
18604 pixels = 25.4;
18605 else if (unit[0] == 'c' && unit[1] == 'm')
18606 pixels = 2.54;
18607 else
18608 pixels = 0;
18609 if (pixels > 0)
18611 double ppi;
18612 #ifdef HAVE_WINDOW_SYSTEM
18613 if (FRAME_WINDOW_P (it->f)
18614 && (ppi = (width_p
18615 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18616 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18617 ppi > 0))
18618 return OK_PIXELS (ppi / pixels);
18619 #endif
18621 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18622 || (CONSP (Vdisplay_pixels_per_inch)
18623 && (ppi = (width_p
18624 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18625 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18626 ppi > 0)))
18627 return OK_PIXELS (ppi / pixels);
18629 return 0;
18633 #ifdef HAVE_WINDOW_SYSTEM
18634 if (EQ (prop, Qheight))
18635 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18636 if (EQ (prop, Qwidth))
18637 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18638 #else
18639 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18640 return OK_PIXELS (1);
18641 #endif
18643 if (EQ (prop, Qtext))
18644 return OK_PIXELS (width_p
18645 ? window_box_width (it->w, TEXT_AREA)
18646 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18648 if (align_to && *align_to < 0)
18650 *res = 0;
18651 if (EQ (prop, Qleft))
18652 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18653 if (EQ (prop, Qright))
18654 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18655 if (EQ (prop, Qcenter))
18656 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18657 + window_box_width (it->w, TEXT_AREA) / 2);
18658 if (EQ (prop, Qleft_fringe))
18659 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18660 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18661 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18662 if (EQ (prop, Qright_fringe))
18663 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18664 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18665 : window_box_right_offset (it->w, TEXT_AREA));
18666 if (EQ (prop, Qleft_margin))
18667 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18668 if (EQ (prop, Qright_margin))
18669 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18670 if (EQ (prop, Qscroll_bar))
18671 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18673 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18674 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18675 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18676 : 0)));
18678 else
18680 if (EQ (prop, Qleft_fringe))
18681 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18682 if (EQ (prop, Qright_fringe))
18683 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18684 if (EQ (prop, Qleft_margin))
18685 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18686 if (EQ (prop, Qright_margin))
18687 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18688 if (EQ (prop, Qscroll_bar))
18689 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18692 prop = Fbuffer_local_value (prop, it->w->buffer);
18695 if (INTEGERP (prop) || FLOATP (prop))
18697 int base_unit = (width_p
18698 ? FRAME_COLUMN_WIDTH (it->f)
18699 : FRAME_LINE_HEIGHT (it->f));
18700 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18703 if (CONSP (prop))
18705 Lisp_Object car = XCAR (prop);
18706 Lisp_Object cdr = XCDR (prop);
18708 if (SYMBOLP (car))
18710 #ifdef HAVE_WINDOW_SYSTEM
18711 if (valid_image_p (prop))
18713 int id = lookup_image (it->f, prop);
18714 struct image *img = IMAGE_FROM_ID (it->f, id);
18716 return OK_PIXELS (width_p ? img->width : img->height);
18718 #endif
18719 if (EQ (car, Qplus) || EQ (car, Qminus))
18721 int first = 1;
18722 double px;
18724 pixels = 0;
18725 while (CONSP (cdr))
18727 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18728 font, width_p, align_to))
18729 return 0;
18730 if (first)
18731 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18732 else
18733 pixels += px;
18734 cdr = XCDR (cdr);
18736 if (EQ (car, Qminus))
18737 pixels = -pixels;
18738 return OK_PIXELS (pixels);
18741 car = Fbuffer_local_value (car, it->w->buffer);
18744 if (INTEGERP (car) || FLOATP (car))
18746 double fact;
18747 pixels = XFLOATINT (car);
18748 if (NILP (cdr))
18749 return OK_PIXELS (pixels);
18750 if (calc_pixel_width_or_height (&fact, it, cdr,
18751 font, width_p, align_to))
18752 return OK_PIXELS (pixels * fact);
18753 return 0;
18756 return 0;
18759 return 0;
18763 /***********************************************************************
18764 Glyph Display
18765 ***********************************************************************/
18767 #ifdef HAVE_WINDOW_SYSTEM
18769 #if GLYPH_DEBUG
18771 void
18772 dump_glyph_string (s)
18773 struct glyph_string *s;
18775 fprintf (stderr, "glyph string\n");
18776 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18777 s->x, s->y, s->width, s->height);
18778 fprintf (stderr, " ybase = %d\n", s->ybase);
18779 fprintf (stderr, " hl = %d\n", s->hl);
18780 fprintf (stderr, " left overhang = %d, right = %d\n",
18781 s->left_overhang, s->right_overhang);
18782 fprintf (stderr, " nchars = %d\n", s->nchars);
18783 fprintf (stderr, " extends to end of line = %d\n",
18784 s->extends_to_end_of_line_p);
18785 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18786 fprintf (stderr, " bg width = %d\n", s->background_width);
18789 #endif /* GLYPH_DEBUG */
18791 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18792 of XChar2b structures for S; it can't be allocated in
18793 init_glyph_string because it must be allocated via `alloca'. W
18794 is the window on which S is drawn. ROW and AREA are the glyph row
18795 and area within the row from which S is constructed. START is the
18796 index of the first glyph structure covered by S. HL is a
18797 face-override for drawing S. */
18799 #ifdef HAVE_NTGUI
18800 #define OPTIONAL_HDC(hdc) hdc,
18801 #define DECLARE_HDC(hdc) HDC hdc;
18802 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18803 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18804 #endif
18806 #ifndef OPTIONAL_HDC
18807 #define OPTIONAL_HDC(hdc)
18808 #define DECLARE_HDC(hdc)
18809 #define ALLOCATE_HDC(hdc, f)
18810 #define RELEASE_HDC(hdc, f)
18811 #endif
18813 static void
18814 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18815 struct glyph_string *s;
18816 DECLARE_HDC (hdc)
18817 XChar2b *char2b;
18818 struct window *w;
18819 struct glyph_row *row;
18820 enum glyph_row_area area;
18821 int start;
18822 enum draw_glyphs_face hl;
18824 bzero (s, sizeof *s);
18825 s->w = w;
18826 s->f = XFRAME (w->frame);
18827 #ifdef HAVE_NTGUI
18828 s->hdc = hdc;
18829 #endif
18830 s->display = FRAME_X_DISPLAY (s->f);
18831 s->window = FRAME_X_WINDOW (s->f);
18832 s->char2b = char2b;
18833 s->hl = hl;
18834 s->row = row;
18835 s->area = area;
18836 s->first_glyph = row->glyphs[area] + start;
18837 s->height = row->height;
18838 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18840 /* Display the internal border below the tool-bar window. */
18841 if (WINDOWP (s->f->tool_bar_window)
18842 && s->w == XWINDOW (s->f->tool_bar_window))
18843 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18845 s->ybase = s->y + row->ascent;
18849 /* Append the list of glyph strings with head H and tail T to the list
18850 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18852 static INLINE void
18853 append_glyph_string_lists (head, tail, h, t)
18854 struct glyph_string **head, **tail;
18855 struct glyph_string *h, *t;
18857 if (h)
18859 if (*head)
18860 (*tail)->next = h;
18861 else
18862 *head = h;
18863 h->prev = *tail;
18864 *tail = t;
18869 /* Prepend the list of glyph strings with head H and tail T to the
18870 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18871 result. */
18873 static INLINE void
18874 prepend_glyph_string_lists (head, tail, h, t)
18875 struct glyph_string **head, **tail;
18876 struct glyph_string *h, *t;
18878 if (h)
18880 if (*head)
18881 (*head)->prev = t;
18882 else
18883 *tail = t;
18884 t->next = *head;
18885 *head = h;
18890 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18891 Set *HEAD and *TAIL to the resulting list. */
18893 static INLINE void
18894 append_glyph_string (head, tail, s)
18895 struct glyph_string **head, **tail;
18896 struct glyph_string *s;
18898 s->next = s->prev = NULL;
18899 append_glyph_string_lists (head, tail, s, s);
18903 /* Get face and two-byte form of character glyph GLYPH on frame F.
18904 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18905 a pointer to a realized face that is ready for display. */
18907 static INLINE struct face *
18908 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18909 struct frame *f;
18910 struct glyph *glyph;
18911 XChar2b *char2b;
18912 int *two_byte_p;
18914 struct face *face;
18916 xassert (glyph->type == CHAR_GLYPH);
18917 face = FACE_FROM_ID (f, glyph->face_id);
18919 if (two_byte_p)
18920 *two_byte_p = 0;
18922 if (!glyph->multibyte_p)
18924 /* Unibyte case. We don't have to encode, but we have to make
18925 sure to use a face suitable for unibyte. */
18926 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18928 else if (glyph->u.ch < 128)
18930 /* Case of ASCII in a face known to fit ASCII. */
18931 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18933 else
18935 int c1, c2, charset;
18937 /* Split characters into bytes. If c2 is -1 afterwards, C is
18938 really a one-byte character so that byte1 is zero. */
18939 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18940 if (c2 > 0)
18941 STORE_XCHAR2B (char2b, c1, c2);
18942 else
18943 STORE_XCHAR2B (char2b, 0, c1);
18945 /* Maybe encode the character in *CHAR2B. */
18946 if (charset != CHARSET_ASCII)
18948 struct font_info *font_info
18949 = FONT_INFO_FROM_ID (f, face->font_info_id);
18950 if (font_info)
18951 glyph->font_type
18952 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18956 /* Make sure X resources of the face are allocated. */
18957 xassert (face != NULL);
18958 PREPARE_FACE_FOR_DISPLAY (f, face);
18959 return face;
18963 /* Fill glyph string S with composition components specified by S->cmp.
18965 FACES is an array of faces for all components of this composition.
18966 S->gidx is the index of the first component for S.
18968 OVERLAPS non-zero means S should draw the foreground only, and use
18969 its physical height for clipping. See also draw_glyphs.
18971 Value is the index of a component not in S. */
18973 static int
18974 fill_composite_glyph_string (s, faces, overlaps)
18975 struct glyph_string *s;
18976 struct face **faces;
18977 int overlaps;
18979 int i;
18981 xassert (s);
18983 s->for_overlaps = overlaps;
18985 s->face = faces[s->gidx];
18986 s->font = s->face->font;
18987 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18989 /* For all glyphs of this composition, starting at the offset
18990 S->gidx, until we reach the end of the definition or encounter a
18991 glyph that requires the different face, add it to S. */
18992 ++s->nchars;
18993 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18994 ++s->nchars;
18996 /* All glyph strings for the same composition has the same width,
18997 i.e. the width set for the first component of the composition. */
18999 s->width = s->first_glyph->pixel_width;
19001 /* If the specified font could not be loaded, use the frame's
19002 default font, but record the fact that we couldn't load it in
19003 the glyph string so that we can draw rectangles for the
19004 characters of the glyph string. */
19005 if (s->font == NULL)
19007 s->font_not_found_p = 1;
19008 s->font = FRAME_FONT (s->f);
19011 /* Adjust base line for subscript/superscript text. */
19012 s->ybase += s->first_glyph->voffset;
19014 xassert (s->face && s->face->gc);
19016 /* This glyph string must always be drawn with 16-bit functions. */
19017 s->two_byte_p = 1;
19019 return s->gidx + s->nchars;
19023 /* Fill glyph string S from a sequence of character glyphs.
19025 FACE_ID is the face id of the string. START is the index of the
19026 first glyph to consider, END is the index of the last + 1.
19027 OVERLAPS non-zero means S should draw the foreground only, and use
19028 its physical height for clipping. See also draw_glyphs.
19030 Value is the index of the first glyph not in S. */
19032 static int
19033 fill_glyph_string (s, face_id, start, end, overlaps)
19034 struct glyph_string *s;
19035 int face_id;
19036 int start, end, overlaps;
19038 struct glyph *glyph, *last;
19039 int voffset;
19040 int glyph_not_available_p;
19042 xassert (s->f == XFRAME (s->w->frame));
19043 xassert (s->nchars == 0);
19044 xassert (start >= 0 && end > start);
19046 s->for_overlaps = overlaps,
19047 glyph = s->row->glyphs[s->area] + start;
19048 last = s->row->glyphs[s->area] + end;
19049 voffset = glyph->voffset;
19051 glyph_not_available_p = glyph->glyph_not_available_p;
19053 while (glyph < last
19054 && glyph->type == CHAR_GLYPH
19055 && glyph->voffset == voffset
19056 /* Same face id implies same font, nowadays. */
19057 && glyph->face_id == face_id
19058 && glyph->glyph_not_available_p == glyph_not_available_p)
19060 int two_byte_p;
19062 s->face = get_glyph_face_and_encoding (s->f, glyph,
19063 s->char2b + s->nchars,
19064 &two_byte_p);
19065 s->two_byte_p = two_byte_p;
19066 ++s->nchars;
19067 xassert (s->nchars <= end - start);
19068 s->width += glyph->pixel_width;
19069 ++glyph;
19072 s->font = s->face->font;
19073 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19075 /* If the specified font could not be loaded, use the frame's font,
19076 but record the fact that we couldn't load it in
19077 S->font_not_found_p so that we can draw rectangles for the
19078 characters of the glyph string. */
19079 if (s->font == NULL || glyph_not_available_p)
19081 s->font_not_found_p = 1;
19082 s->font = FRAME_FONT (s->f);
19085 /* Adjust base line for subscript/superscript text. */
19086 s->ybase += voffset;
19088 xassert (s->face && s->face->gc);
19089 return glyph - s->row->glyphs[s->area];
19093 /* Fill glyph string S from image glyph S->first_glyph. */
19095 static void
19096 fill_image_glyph_string (s)
19097 struct glyph_string *s;
19099 xassert (s->first_glyph->type == IMAGE_GLYPH);
19100 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19101 xassert (s->img);
19102 s->slice = s->first_glyph->slice;
19103 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19104 s->font = s->face->font;
19105 s->width = s->first_glyph->pixel_width;
19107 /* Adjust base line for subscript/superscript text. */
19108 s->ybase += s->first_glyph->voffset;
19112 /* Fill glyph string S from a sequence of stretch glyphs.
19114 ROW is the glyph row in which the glyphs are found, AREA is the
19115 area within the row. START is the index of the first glyph to
19116 consider, END is the index of the last + 1.
19118 Value is the index of the first glyph not in S. */
19120 static int
19121 fill_stretch_glyph_string (s, row, area, start, end)
19122 struct glyph_string *s;
19123 struct glyph_row *row;
19124 enum glyph_row_area area;
19125 int start, end;
19127 struct glyph *glyph, *last;
19128 int voffset, face_id;
19130 xassert (s->first_glyph->type == STRETCH_GLYPH);
19132 glyph = s->row->glyphs[s->area] + start;
19133 last = s->row->glyphs[s->area] + end;
19134 face_id = glyph->face_id;
19135 s->face = FACE_FROM_ID (s->f, face_id);
19136 s->font = s->face->font;
19137 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19138 s->width = glyph->pixel_width;
19139 s->nchars = 1;
19140 voffset = glyph->voffset;
19142 for (++glyph;
19143 (glyph < last
19144 && glyph->type == STRETCH_GLYPH
19145 && glyph->voffset == voffset
19146 && glyph->face_id == face_id);
19147 ++glyph)
19148 s->width += glyph->pixel_width;
19150 /* Adjust base line for subscript/superscript text. */
19151 s->ybase += voffset;
19153 /* The case that face->gc == 0 is handled when drawing the glyph
19154 string by calling PREPARE_FACE_FOR_DISPLAY. */
19155 xassert (s->face);
19156 return glyph - s->row->glyphs[s->area];
19160 /* EXPORT for RIF:
19161 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19162 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19163 assumed to be zero. */
19165 void
19166 x_get_glyph_overhangs (glyph, f, left, right)
19167 struct glyph *glyph;
19168 struct frame *f;
19169 int *left, *right;
19171 *left = *right = 0;
19173 if (glyph->type == CHAR_GLYPH)
19175 XFontStruct *font;
19176 struct face *face;
19177 struct font_info *font_info;
19178 XChar2b char2b;
19179 XCharStruct *pcm;
19181 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19182 font = face->font;
19183 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19184 if (font /* ++KFS: Should this be font_info ? */
19185 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19187 if (pcm->rbearing > pcm->width)
19188 *right = pcm->rbearing - pcm->width;
19189 if (pcm->lbearing < 0)
19190 *left = -pcm->lbearing;
19196 /* Return the index of the first glyph preceding glyph string S that
19197 is overwritten by S because of S's left overhang. Value is -1
19198 if no glyphs are overwritten. */
19200 static int
19201 left_overwritten (s)
19202 struct glyph_string *s;
19204 int k;
19206 if (s->left_overhang)
19208 int x = 0, i;
19209 struct glyph *glyphs = s->row->glyphs[s->area];
19210 int first = s->first_glyph - glyphs;
19212 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19213 x -= glyphs[i].pixel_width;
19215 k = i + 1;
19217 else
19218 k = -1;
19220 return k;
19224 /* Return the index of the first glyph preceding glyph string S that
19225 is overwriting S because of its right overhang. Value is -1 if no
19226 glyph in front of S overwrites S. */
19228 static int
19229 left_overwriting (s)
19230 struct glyph_string *s;
19232 int i, k, x;
19233 struct glyph *glyphs = s->row->glyphs[s->area];
19234 int first = s->first_glyph - glyphs;
19236 k = -1;
19237 x = 0;
19238 for (i = first - 1; i >= 0; --i)
19240 int left, right;
19241 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19242 if (x + right > 0)
19243 k = i;
19244 x -= glyphs[i].pixel_width;
19247 return k;
19251 /* Return the index of the last glyph following glyph string S that is
19252 not overwritten by S because of S's right overhang. Value is -1 if
19253 no such glyph is found. */
19255 static int
19256 right_overwritten (s)
19257 struct glyph_string *s;
19259 int k = -1;
19261 if (s->right_overhang)
19263 int x = 0, i;
19264 struct glyph *glyphs = s->row->glyphs[s->area];
19265 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19266 int end = s->row->used[s->area];
19268 for (i = first; i < end && s->right_overhang > x; ++i)
19269 x += glyphs[i].pixel_width;
19271 k = i;
19274 return k;
19278 /* Return the index of the last glyph following glyph string S that
19279 overwrites S because of its left overhang. Value is negative
19280 if no such glyph is found. */
19282 static int
19283 right_overwriting (s)
19284 struct glyph_string *s;
19286 int i, k, x;
19287 int end = s->row->used[s->area];
19288 struct glyph *glyphs = s->row->glyphs[s->area];
19289 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19291 k = -1;
19292 x = 0;
19293 for (i = first; i < end; ++i)
19295 int left, right;
19296 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19297 if (x - left < 0)
19298 k = i;
19299 x += glyphs[i].pixel_width;
19302 return k;
19306 /* Get face and two-byte form of character C in face FACE_ID on frame
19307 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19308 means we want to display multibyte text. DISPLAY_P non-zero means
19309 make sure that X resources for the face returned are allocated.
19310 Value is a pointer to a realized face that is ready for display if
19311 DISPLAY_P is non-zero. */
19313 static INLINE struct face *
19314 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19315 struct frame *f;
19316 int c, face_id;
19317 XChar2b *char2b;
19318 int multibyte_p, display_p;
19320 struct face *face = FACE_FROM_ID (f, face_id);
19322 if (!multibyte_p)
19324 /* Unibyte case. We don't have to encode, but we have to make
19325 sure to use a face suitable for unibyte. */
19326 STORE_XCHAR2B (char2b, 0, c);
19327 face_id = FACE_FOR_CHAR (f, face, c);
19328 face = FACE_FROM_ID (f, face_id);
19330 else if (c < 128)
19332 /* Case of ASCII in a face known to fit ASCII. */
19333 STORE_XCHAR2B (char2b, 0, c);
19335 else
19337 int c1, c2, charset;
19339 /* Split characters into bytes. If c2 is -1 afterwards, C is
19340 really a one-byte character so that byte1 is zero. */
19341 SPLIT_CHAR (c, charset, c1, c2);
19342 if (c2 > 0)
19343 STORE_XCHAR2B (char2b, c1, c2);
19344 else
19345 STORE_XCHAR2B (char2b, 0, c1);
19347 /* Maybe encode the character in *CHAR2B. */
19348 if (face->font != NULL)
19350 struct font_info *font_info
19351 = FONT_INFO_FROM_ID (f, face->font_info_id);
19352 if (font_info)
19353 rif->encode_char (c, char2b, font_info, 0);
19357 /* Make sure X resources of the face are allocated. */
19358 #ifdef HAVE_X_WINDOWS
19359 if (display_p)
19360 #endif
19362 xassert (face != NULL);
19363 PREPARE_FACE_FOR_DISPLAY (f, face);
19366 return face;
19370 /* Set background width of glyph string S. START is the index of the
19371 first glyph following S. LAST_X is the right-most x-position + 1
19372 in the drawing area. */
19374 static INLINE void
19375 set_glyph_string_background_width (s, start, last_x)
19376 struct glyph_string *s;
19377 int start;
19378 int last_x;
19380 /* If the face of this glyph string has to be drawn to the end of
19381 the drawing area, set S->extends_to_end_of_line_p. */
19383 if (start == s->row->used[s->area]
19384 && s->area == TEXT_AREA
19385 && ((s->row->fill_line_p
19386 && (s->hl == DRAW_NORMAL_TEXT
19387 || s->hl == DRAW_IMAGE_RAISED
19388 || s->hl == DRAW_IMAGE_SUNKEN))
19389 || s->hl == DRAW_MOUSE_FACE))
19390 s->extends_to_end_of_line_p = 1;
19392 /* If S extends its face to the end of the line, set its
19393 background_width to the distance to the right edge of the drawing
19394 area. */
19395 if (s->extends_to_end_of_line_p)
19396 s->background_width = last_x - s->x + 1;
19397 else
19398 s->background_width = s->width;
19402 /* Compute overhangs and x-positions for glyph string S and its
19403 predecessors, or successors. X is the starting x-position for S.
19404 BACKWARD_P non-zero means process predecessors. */
19406 static void
19407 compute_overhangs_and_x (s, x, backward_p)
19408 struct glyph_string *s;
19409 int x;
19410 int backward_p;
19412 if (backward_p)
19414 while (s)
19416 if (rif->compute_glyph_string_overhangs)
19417 rif->compute_glyph_string_overhangs (s);
19418 x -= s->width;
19419 s->x = x;
19420 s = s->prev;
19423 else
19425 while (s)
19427 if (rif->compute_glyph_string_overhangs)
19428 rif->compute_glyph_string_overhangs (s);
19429 s->x = x;
19430 x += s->width;
19431 s = s->next;
19438 /* The following macros are only called from draw_glyphs below.
19439 They reference the following parameters of that function directly:
19440 `w', `row', `area', and `overlap_p'
19441 as well as the following local variables:
19442 `s', `f', and `hdc' (in W32) */
19444 #ifdef HAVE_NTGUI
19445 /* On W32, silently add local `hdc' variable to argument list of
19446 init_glyph_string. */
19447 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19448 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19449 #else
19450 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19451 init_glyph_string (s, char2b, w, row, area, start, hl)
19452 #endif
19454 /* Add a glyph string for a stretch glyph to the list of strings
19455 between HEAD and TAIL. START is the index of the stretch glyph in
19456 row area AREA of glyph row ROW. END is the index of the last glyph
19457 in that glyph row area. X is the current output position assigned
19458 to the new glyph string constructed. HL overrides that face of the
19459 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19460 is the right-most x-position of the drawing area. */
19462 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19463 and below -- keep them on one line. */
19464 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19465 do \
19467 s = (struct glyph_string *) alloca (sizeof *s); \
19468 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19469 START = fill_stretch_glyph_string (s, row, area, START, END); \
19470 append_glyph_string (&HEAD, &TAIL, s); \
19471 s->x = (X); \
19473 while (0)
19476 /* Add a glyph string for an image glyph to the list of strings
19477 between HEAD and TAIL. START is the index of the image glyph in
19478 row area AREA of glyph row ROW. END is the index of the last glyph
19479 in that glyph row area. X is the current output position assigned
19480 to the new glyph string constructed. HL overrides that face of the
19481 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19482 is the right-most x-position of the drawing area. */
19484 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19485 do \
19487 s = (struct glyph_string *) alloca (sizeof *s); \
19488 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19489 fill_image_glyph_string (s); \
19490 append_glyph_string (&HEAD, &TAIL, s); \
19491 ++START; \
19492 s->x = (X); \
19494 while (0)
19497 /* Add a glyph string for a sequence of character glyphs to the list
19498 of strings between HEAD and TAIL. START is the index of the first
19499 glyph in row area AREA of glyph row ROW that is part of the new
19500 glyph string. END is the index of the last glyph in that glyph row
19501 area. X is the current output position assigned to the new glyph
19502 string constructed. HL overrides that face of the glyph; e.g. it
19503 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19504 right-most x-position of the drawing area. */
19506 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19507 do \
19509 int c, face_id; \
19510 XChar2b *char2b; \
19512 c = (row)->glyphs[area][START].u.ch; \
19513 face_id = (row)->glyphs[area][START].face_id; \
19515 s = (struct glyph_string *) alloca (sizeof *s); \
19516 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19517 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19518 append_glyph_string (&HEAD, &TAIL, s); \
19519 s->x = (X); \
19520 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19522 while (0)
19525 /* Add a glyph string for a composite sequence to the list of strings
19526 between HEAD and TAIL. START is the index of the first glyph in
19527 row area AREA of glyph row ROW that is part of the new glyph
19528 string. END is the index of the last glyph in that glyph row area.
19529 X is the current output position assigned to the new glyph string
19530 constructed. HL overrides that face of the glyph; e.g. it is
19531 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19532 x-position of the drawing area. */
19534 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19535 do { \
19536 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19537 int face_id = (row)->glyphs[area][START].face_id; \
19538 struct face *base_face = FACE_FROM_ID (f, face_id); \
19539 struct composition *cmp = composition_table[cmp_id]; \
19540 int glyph_len = cmp->glyph_len; \
19541 XChar2b *char2b; \
19542 struct face **faces; \
19543 struct glyph_string *first_s = NULL; \
19544 int n; \
19546 base_face = base_face->ascii_face; \
19547 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19548 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19549 /* At first, fill in `char2b' and `faces'. */ \
19550 for (n = 0; n < glyph_len; n++) \
19552 int c = COMPOSITION_GLYPH (cmp, n); \
19553 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19554 faces[n] = FACE_FROM_ID (f, this_face_id); \
19555 get_char_face_and_encoding (f, c, this_face_id, \
19556 char2b + n, 1, 1); \
19559 /* Make glyph_strings for each glyph sequence that is drawable by \
19560 the same face, and append them to HEAD/TAIL. */ \
19561 for (n = 0; n < cmp->glyph_len;) \
19563 s = (struct glyph_string *) alloca (sizeof *s); \
19564 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19565 append_glyph_string (&(HEAD), &(TAIL), s); \
19566 s->cmp = cmp; \
19567 s->gidx = n; \
19568 s->x = (X); \
19570 if (n == 0) \
19571 first_s = s; \
19573 n = fill_composite_glyph_string (s, faces, overlaps); \
19576 ++START; \
19577 s = first_s; \
19578 } while (0)
19581 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19582 of AREA of glyph row ROW on window W between indices START and END.
19583 HL overrides the face for drawing glyph strings, e.g. it is
19584 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19585 x-positions of the drawing area.
19587 This is an ugly monster macro construct because we must use alloca
19588 to allocate glyph strings (because draw_glyphs can be called
19589 asynchronously). */
19591 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19592 do \
19594 HEAD = TAIL = NULL; \
19595 while (START < END) \
19597 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19598 switch (first_glyph->type) \
19600 case CHAR_GLYPH: \
19601 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19602 HL, X, LAST_X); \
19603 break; \
19605 case COMPOSITE_GLYPH: \
19606 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19607 HL, X, LAST_X); \
19608 break; \
19610 case STRETCH_GLYPH: \
19611 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19612 HL, X, LAST_X); \
19613 break; \
19615 case IMAGE_GLYPH: \
19616 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19617 HL, X, LAST_X); \
19618 break; \
19620 default: \
19621 abort (); \
19624 set_glyph_string_background_width (s, START, LAST_X); \
19625 (X) += s->width; \
19628 while (0)
19631 /* Draw glyphs between START and END in AREA of ROW on window W,
19632 starting at x-position X. X is relative to AREA in W. HL is a
19633 face-override with the following meaning:
19635 DRAW_NORMAL_TEXT draw normally
19636 DRAW_CURSOR draw in cursor face
19637 DRAW_MOUSE_FACE draw in mouse face.
19638 DRAW_INVERSE_VIDEO draw in mode line face
19639 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19640 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19642 If OVERLAPS is non-zero, draw only the foreground of characters and
19643 clip to the physical height of ROW. Non-zero value also defines
19644 the overlapping part to be drawn:
19646 OVERLAPS_PRED overlap with preceding rows
19647 OVERLAPS_SUCC overlap with succeeding rows
19648 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19649 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19651 Value is the x-position reached, relative to AREA of W. */
19653 static int
19654 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19655 struct window *w;
19656 int x;
19657 struct glyph_row *row;
19658 enum glyph_row_area area;
19659 int start, end;
19660 enum draw_glyphs_face hl;
19661 int overlaps;
19663 struct glyph_string *head, *tail;
19664 struct glyph_string *s;
19665 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19666 int last_x, area_width;
19667 int x_reached;
19668 int i, j;
19669 struct frame *f = XFRAME (WINDOW_FRAME (w));
19670 DECLARE_HDC (hdc);
19672 ALLOCATE_HDC (hdc, f);
19674 /* Let's rather be paranoid than getting a SEGV. */
19675 end = min (end, row->used[area]);
19676 start = max (0, start);
19677 start = min (end, start);
19679 /* Translate X to frame coordinates. Set last_x to the right
19680 end of the drawing area. */
19681 if (row->full_width_p)
19683 /* X is relative to the left edge of W, without scroll bars
19684 or fringes. */
19685 x += WINDOW_LEFT_EDGE_X (w);
19686 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19688 else
19690 int area_left = window_box_left (w, area);
19691 x += area_left;
19692 area_width = window_box_width (w, area);
19693 last_x = area_left + area_width;
19696 /* Build a doubly-linked list of glyph_string structures between
19697 head and tail from what we have to draw. Note that the macro
19698 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19699 the reason we use a separate variable `i'. */
19700 i = start;
19701 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19702 if (tail)
19703 x_reached = tail->x + tail->background_width;
19704 else
19705 x_reached = x;
19707 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19708 the row, redraw some glyphs in front or following the glyph
19709 strings built above. */
19710 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19712 int dummy_x = 0;
19713 struct glyph_string *h, *t;
19715 /* Compute overhangs for all glyph strings. */
19716 if (rif->compute_glyph_string_overhangs)
19717 for (s = head; s; s = s->next)
19718 rif->compute_glyph_string_overhangs (s);
19720 /* Prepend glyph strings for glyphs in front of the first glyph
19721 string that are overwritten because of the first glyph
19722 string's left overhang. The background of all strings
19723 prepended must be drawn because the first glyph string
19724 draws over it. */
19725 i = left_overwritten (head);
19726 if (i >= 0)
19728 j = i;
19729 BUILD_GLYPH_STRINGS (j, start, h, t,
19730 DRAW_NORMAL_TEXT, dummy_x, last_x);
19731 start = i;
19732 compute_overhangs_and_x (t, head->x, 1);
19733 prepend_glyph_string_lists (&head, &tail, h, t);
19734 clip_head = head;
19737 /* Prepend glyph strings for glyphs in front of the first glyph
19738 string that overwrite that glyph string because of their
19739 right overhang. For these strings, only the foreground must
19740 be drawn, because it draws over the glyph string at `head'.
19741 The background must not be drawn because this would overwrite
19742 right overhangs of preceding glyphs for which no glyph
19743 strings exist. */
19744 i = left_overwriting (head);
19745 if (i >= 0)
19747 clip_head = head;
19748 BUILD_GLYPH_STRINGS (i, start, h, t,
19749 DRAW_NORMAL_TEXT, dummy_x, last_x);
19750 for (s = h; s; s = s->next)
19751 s->background_filled_p = 1;
19752 compute_overhangs_and_x (t, head->x, 1);
19753 prepend_glyph_string_lists (&head, &tail, h, t);
19756 /* Append glyphs strings for glyphs following the last glyph
19757 string tail that are overwritten by tail. The background of
19758 these strings has to be drawn because tail's foreground draws
19759 over it. */
19760 i = right_overwritten (tail);
19761 if (i >= 0)
19763 BUILD_GLYPH_STRINGS (end, i, h, t,
19764 DRAW_NORMAL_TEXT, x, last_x);
19765 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19766 append_glyph_string_lists (&head, &tail, h, t);
19767 clip_tail = tail;
19770 /* Append glyph strings for glyphs following the last glyph
19771 string tail that overwrite tail. The foreground of such
19772 glyphs has to be drawn because it writes into the background
19773 of tail. The background must not be drawn because it could
19774 paint over the foreground of following glyphs. */
19775 i = right_overwriting (tail);
19776 if (i >= 0)
19778 clip_tail = tail;
19779 BUILD_GLYPH_STRINGS (end, i, h, t,
19780 DRAW_NORMAL_TEXT, x, last_x);
19781 for (s = h; s; s = s->next)
19782 s->background_filled_p = 1;
19783 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19784 append_glyph_string_lists (&head, &tail, h, t);
19786 if (clip_head || clip_tail)
19787 for (s = head; s; s = s->next)
19789 s->clip_head = clip_head;
19790 s->clip_tail = clip_tail;
19794 /* Draw all strings. */
19795 for (s = head; s; s = s->next)
19796 rif->draw_glyph_string (s);
19798 if (area == TEXT_AREA
19799 && !row->full_width_p
19800 /* When drawing overlapping rows, only the glyph strings'
19801 foreground is drawn, which doesn't erase a cursor
19802 completely. */
19803 && !overlaps)
19805 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19806 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19807 : (tail ? tail->x + tail->background_width : x));
19809 int text_left = window_box_left (w, TEXT_AREA);
19810 x0 -= text_left;
19811 x1 -= text_left;
19813 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19814 row->y, MATRIX_ROW_BOTTOM_Y (row));
19817 /* Value is the x-position up to which drawn, relative to AREA of W.
19818 This doesn't include parts drawn because of overhangs. */
19819 if (row->full_width_p)
19820 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19821 else
19822 x_reached -= window_box_left (w, area);
19824 RELEASE_HDC (hdc, f);
19826 return x_reached;
19829 /* Expand row matrix if too narrow. Don't expand if area
19830 is not present. */
19832 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19834 if (!fonts_changed_p \
19835 && (it->glyph_row->glyphs[area] \
19836 < it->glyph_row->glyphs[area + 1])) \
19838 it->w->ncols_scale_factor++; \
19839 fonts_changed_p = 1; \
19843 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19844 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19846 static INLINE void
19847 append_glyph (it)
19848 struct it *it;
19850 struct glyph *glyph;
19851 enum glyph_row_area area = it->area;
19853 xassert (it->glyph_row);
19854 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19856 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19857 if (glyph < it->glyph_row->glyphs[area + 1])
19859 glyph->charpos = CHARPOS (it->position);
19860 glyph->object = it->object;
19861 glyph->pixel_width = it->pixel_width;
19862 glyph->ascent = it->ascent;
19863 glyph->descent = it->descent;
19864 glyph->voffset = it->voffset;
19865 glyph->type = CHAR_GLYPH;
19866 glyph->multibyte_p = it->multibyte_p;
19867 glyph->left_box_line_p = it->start_of_box_run_p;
19868 glyph->right_box_line_p = it->end_of_box_run_p;
19869 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19870 || it->phys_descent > it->descent);
19871 glyph->padding_p = 0;
19872 glyph->glyph_not_available_p = it->glyph_not_available_p;
19873 glyph->face_id = it->face_id;
19874 glyph->u.ch = it->char_to_display;
19875 glyph->slice = null_glyph_slice;
19876 glyph->font_type = FONT_TYPE_UNKNOWN;
19877 ++it->glyph_row->used[area];
19879 else
19880 IT_EXPAND_MATRIX_WIDTH (it, area);
19883 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19884 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19886 static INLINE void
19887 append_composite_glyph (it)
19888 struct it *it;
19890 struct glyph *glyph;
19891 enum glyph_row_area area = it->area;
19893 xassert (it->glyph_row);
19895 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19896 if (glyph < it->glyph_row->glyphs[area + 1])
19898 glyph->charpos = CHARPOS (it->position);
19899 glyph->object = it->object;
19900 glyph->pixel_width = it->pixel_width;
19901 glyph->ascent = it->ascent;
19902 glyph->descent = it->descent;
19903 glyph->voffset = it->voffset;
19904 glyph->type = COMPOSITE_GLYPH;
19905 glyph->multibyte_p = it->multibyte_p;
19906 glyph->left_box_line_p = it->start_of_box_run_p;
19907 glyph->right_box_line_p = it->end_of_box_run_p;
19908 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19909 || it->phys_descent > it->descent);
19910 glyph->padding_p = 0;
19911 glyph->glyph_not_available_p = 0;
19912 glyph->face_id = it->face_id;
19913 glyph->u.cmp_id = it->cmp_id;
19914 glyph->slice = null_glyph_slice;
19915 glyph->font_type = FONT_TYPE_UNKNOWN;
19916 ++it->glyph_row->used[area];
19918 else
19919 IT_EXPAND_MATRIX_WIDTH (it, area);
19923 /* Change IT->ascent and IT->height according to the setting of
19924 IT->voffset. */
19926 static INLINE void
19927 take_vertical_position_into_account (it)
19928 struct it *it;
19930 if (it->voffset)
19932 if (it->voffset < 0)
19933 /* Increase the ascent so that we can display the text higher
19934 in the line. */
19935 it->ascent -= it->voffset;
19936 else
19937 /* Increase the descent so that we can display the text lower
19938 in the line. */
19939 it->descent += it->voffset;
19944 /* Produce glyphs/get display metrics for the image IT is loaded with.
19945 See the description of struct display_iterator in dispextern.h for
19946 an overview of struct display_iterator. */
19948 static void
19949 produce_image_glyph (it)
19950 struct it *it;
19952 struct image *img;
19953 struct face *face;
19954 int glyph_ascent, crop;
19955 struct glyph_slice slice;
19957 xassert (it->what == IT_IMAGE);
19959 face = FACE_FROM_ID (it->f, it->face_id);
19960 xassert (face);
19961 /* Make sure X resources of the face is loaded. */
19962 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19964 if (it->image_id < 0)
19966 /* Fringe bitmap. */
19967 it->ascent = it->phys_ascent = 0;
19968 it->descent = it->phys_descent = 0;
19969 it->pixel_width = 0;
19970 it->nglyphs = 0;
19971 return;
19974 img = IMAGE_FROM_ID (it->f, it->image_id);
19975 xassert (img);
19976 /* Make sure X resources of the image is loaded. */
19977 prepare_image_for_display (it->f, img);
19979 slice.x = slice.y = 0;
19980 slice.width = img->width;
19981 slice.height = img->height;
19983 if (INTEGERP (it->slice.x))
19984 slice.x = XINT (it->slice.x);
19985 else if (FLOATP (it->slice.x))
19986 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19988 if (INTEGERP (it->slice.y))
19989 slice.y = XINT (it->slice.y);
19990 else if (FLOATP (it->slice.y))
19991 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19993 if (INTEGERP (it->slice.width))
19994 slice.width = XINT (it->slice.width);
19995 else if (FLOATP (it->slice.width))
19996 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19998 if (INTEGERP (it->slice.height))
19999 slice.height = XINT (it->slice.height);
20000 else if (FLOATP (it->slice.height))
20001 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20003 if (slice.x >= img->width)
20004 slice.x = img->width;
20005 if (slice.y >= img->height)
20006 slice.y = img->height;
20007 if (slice.x + slice.width >= img->width)
20008 slice.width = img->width - slice.x;
20009 if (slice.y + slice.height > img->height)
20010 slice.height = img->height - slice.y;
20012 if (slice.width == 0 || slice.height == 0)
20013 return;
20015 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20017 it->descent = slice.height - glyph_ascent;
20018 if (slice.y == 0)
20019 it->descent += img->vmargin;
20020 if (slice.y + slice.height == img->height)
20021 it->descent += img->vmargin;
20022 it->phys_descent = it->descent;
20024 it->pixel_width = slice.width;
20025 if (slice.x == 0)
20026 it->pixel_width += img->hmargin;
20027 if (slice.x + slice.width == img->width)
20028 it->pixel_width += img->hmargin;
20030 /* It's quite possible for images to have an ascent greater than
20031 their height, so don't get confused in that case. */
20032 if (it->descent < 0)
20033 it->descent = 0;
20035 #if 0 /* this breaks image tiling */
20036 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20037 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20038 if (face_ascent > it->ascent)
20039 it->ascent = it->phys_ascent = face_ascent;
20040 #endif
20042 it->nglyphs = 1;
20044 if (face->box != FACE_NO_BOX)
20046 if (face->box_line_width > 0)
20048 if (slice.y == 0)
20049 it->ascent += face->box_line_width;
20050 if (slice.y + slice.height == img->height)
20051 it->descent += face->box_line_width;
20054 if (it->start_of_box_run_p && slice.x == 0)
20055 it->pixel_width += abs (face->box_line_width);
20056 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20057 it->pixel_width += abs (face->box_line_width);
20060 take_vertical_position_into_account (it);
20062 /* Automatically crop wide image glyphs at right edge so we can
20063 draw the cursor on same display row. */
20064 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20065 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20067 it->pixel_width -= crop;
20068 slice.width -= crop;
20071 if (it->glyph_row)
20073 struct glyph *glyph;
20074 enum glyph_row_area area = it->area;
20076 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20077 if (glyph < it->glyph_row->glyphs[area + 1])
20079 glyph->charpos = CHARPOS (it->position);
20080 glyph->object = it->object;
20081 glyph->pixel_width = it->pixel_width;
20082 glyph->ascent = glyph_ascent;
20083 glyph->descent = it->descent;
20084 glyph->voffset = it->voffset;
20085 glyph->type = IMAGE_GLYPH;
20086 glyph->multibyte_p = it->multibyte_p;
20087 glyph->left_box_line_p = it->start_of_box_run_p;
20088 glyph->right_box_line_p = it->end_of_box_run_p;
20089 glyph->overlaps_vertically_p = 0;
20090 glyph->padding_p = 0;
20091 glyph->glyph_not_available_p = 0;
20092 glyph->face_id = it->face_id;
20093 glyph->u.img_id = img->id;
20094 glyph->slice = slice;
20095 glyph->font_type = FONT_TYPE_UNKNOWN;
20096 ++it->glyph_row->used[area];
20098 else
20099 IT_EXPAND_MATRIX_WIDTH (it, area);
20104 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20105 of the glyph, WIDTH and HEIGHT are the width and height of the
20106 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20108 static void
20109 append_stretch_glyph (it, object, width, height, ascent)
20110 struct it *it;
20111 Lisp_Object object;
20112 int width, height;
20113 int ascent;
20115 struct glyph *glyph;
20116 enum glyph_row_area area = it->area;
20118 xassert (ascent >= 0 && ascent <= height);
20120 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20121 if (glyph < it->glyph_row->glyphs[area + 1])
20123 glyph->charpos = CHARPOS (it->position);
20124 glyph->object = object;
20125 glyph->pixel_width = width;
20126 glyph->ascent = ascent;
20127 glyph->descent = height - ascent;
20128 glyph->voffset = it->voffset;
20129 glyph->type = STRETCH_GLYPH;
20130 glyph->multibyte_p = it->multibyte_p;
20131 glyph->left_box_line_p = it->start_of_box_run_p;
20132 glyph->right_box_line_p = it->end_of_box_run_p;
20133 glyph->overlaps_vertically_p = 0;
20134 glyph->padding_p = 0;
20135 glyph->glyph_not_available_p = 0;
20136 glyph->face_id = it->face_id;
20137 glyph->u.stretch.ascent = ascent;
20138 glyph->u.stretch.height = height;
20139 glyph->slice = null_glyph_slice;
20140 glyph->font_type = FONT_TYPE_UNKNOWN;
20141 ++it->glyph_row->used[area];
20143 else
20144 IT_EXPAND_MATRIX_WIDTH (it, area);
20148 /* Produce a stretch glyph for iterator IT. IT->object is the value
20149 of the glyph property displayed. The value must be a list
20150 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20151 being recognized:
20153 1. `:width WIDTH' specifies that the space should be WIDTH *
20154 canonical char width wide. WIDTH may be an integer or floating
20155 point number.
20157 2. `:relative-width FACTOR' specifies that the width of the stretch
20158 should be computed from the width of the first character having the
20159 `glyph' property, and should be FACTOR times that width.
20161 3. `:align-to HPOS' specifies that the space should be wide enough
20162 to reach HPOS, a value in canonical character units.
20164 Exactly one of the above pairs must be present.
20166 4. `:height HEIGHT' specifies that the height of the stretch produced
20167 should be HEIGHT, measured in canonical character units.
20169 5. `:relative-height FACTOR' specifies that the height of the
20170 stretch should be FACTOR times the height of the characters having
20171 the glyph property.
20173 Either none or exactly one of 4 or 5 must be present.
20175 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20176 of the stretch should be used for the ascent of the stretch.
20177 ASCENT must be in the range 0 <= ASCENT <= 100. */
20179 static void
20180 produce_stretch_glyph (it)
20181 struct it *it;
20183 /* (space :width WIDTH :height HEIGHT ...) */
20184 Lisp_Object prop, plist;
20185 int width = 0, height = 0, align_to = -1;
20186 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20187 int ascent = 0;
20188 double tem;
20189 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20190 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20192 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20194 /* List should start with `space'. */
20195 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20196 plist = XCDR (it->object);
20198 /* Compute the width of the stretch. */
20199 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20200 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20202 /* Absolute width `:width WIDTH' specified and valid. */
20203 zero_width_ok_p = 1;
20204 width = (int)tem;
20206 else if (prop = Fplist_get (plist, QCrelative_width),
20207 NUMVAL (prop) > 0)
20209 /* Relative width `:relative-width FACTOR' specified and valid.
20210 Compute the width of the characters having the `glyph'
20211 property. */
20212 struct it it2;
20213 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20215 it2 = *it;
20216 if (it->multibyte_p)
20218 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20219 - IT_BYTEPOS (*it));
20220 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20222 else
20223 it2.c = *p, it2.len = 1;
20225 it2.glyph_row = NULL;
20226 it2.what = IT_CHARACTER;
20227 x_produce_glyphs (&it2);
20228 width = NUMVAL (prop) * it2.pixel_width;
20230 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20231 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20233 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20234 align_to = (align_to < 0
20236 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20237 else if (align_to < 0)
20238 align_to = window_box_left_offset (it->w, TEXT_AREA);
20239 width = max (0, (int)tem + align_to - it->current_x);
20240 zero_width_ok_p = 1;
20242 else
20243 /* Nothing specified -> width defaults to canonical char width. */
20244 width = FRAME_COLUMN_WIDTH (it->f);
20246 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20247 width = 1;
20249 /* Compute height. */
20250 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20251 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20253 height = (int)tem;
20254 zero_height_ok_p = 1;
20256 else if (prop = Fplist_get (plist, QCrelative_height),
20257 NUMVAL (prop) > 0)
20258 height = FONT_HEIGHT (font) * NUMVAL (prop);
20259 else
20260 height = FONT_HEIGHT (font);
20262 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20263 height = 1;
20265 /* Compute percentage of height used for ascent. If
20266 `:ascent ASCENT' is present and valid, use that. Otherwise,
20267 derive the ascent from the font in use. */
20268 if (prop = Fplist_get (plist, QCascent),
20269 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20270 ascent = height * NUMVAL (prop) / 100.0;
20271 else if (!NILP (prop)
20272 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20273 ascent = min (max (0, (int)tem), height);
20274 else
20275 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20277 if (width > 0 && !it->truncate_lines_p
20278 && it->current_x + width > it->last_visible_x)
20279 width = it->last_visible_x - it->current_x - 1;
20281 if (width > 0 && height > 0 && it->glyph_row)
20283 Lisp_Object object = it->stack[it->sp - 1].string;
20284 if (!STRINGP (object))
20285 object = it->w->buffer;
20286 append_stretch_glyph (it, object, width, height, ascent);
20289 it->pixel_width = width;
20290 it->ascent = it->phys_ascent = ascent;
20291 it->descent = it->phys_descent = height - it->ascent;
20292 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20294 take_vertical_position_into_account (it);
20297 /* Get line-height and line-spacing property at point.
20298 If line-height has format (HEIGHT TOTAL), return TOTAL
20299 in TOTAL_HEIGHT. */
20301 static Lisp_Object
20302 get_line_height_property (it, prop)
20303 struct it *it;
20304 Lisp_Object prop;
20306 Lisp_Object position;
20308 if (STRINGP (it->object))
20309 position = make_number (IT_STRING_CHARPOS (*it));
20310 else if (BUFFERP (it->object))
20311 position = make_number (IT_CHARPOS (*it));
20312 else
20313 return Qnil;
20315 return Fget_char_property (position, prop, it->object);
20318 /* Calculate line-height and line-spacing properties.
20319 An integer value specifies explicit pixel value.
20320 A float value specifies relative value to current face height.
20321 A cons (float . face-name) specifies relative value to
20322 height of specified face font.
20324 Returns height in pixels, or nil. */
20327 static Lisp_Object
20328 calc_line_height_property (it, val, font, boff, override)
20329 struct it *it;
20330 Lisp_Object val;
20331 XFontStruct *font;
20332 int boff, override;
20334 Lisp_Object face_name = Qnil;
20335 int ascent, descent, height;
20337 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20338 return val;
20340 if (CONSP (val))
20342 face_name = XCAR (val);
20343 val = XCDR (val);
20344 if (!NUMBERP (val))
20345 val = make_number (1);
20346 if (NILP (face_name))
20348 height = it->ascent + it->descent;
20349 goto scale;
20353 if (NILP (face_name))
20355 font = FRAME_FONT (it->f);
20356 boff = FRAME_BASELINE_OFFSET (it->f);
20358 else if (EQ (face_name, Qt))
20360 override = 0;
20362 else
20364 int face_id;
20365 struct face *face;
20366 struct font_info *font_info;
20368 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20369 if (face_id < 0)
20370 return make_number (-1);
20372 face = FACE_FROM_ID (it->f, face_id);
20373 font = face->font;
20374 if (font == NULL)
20375 return make_number (-1);
20377 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20378 boff = font_info->baseline_offset;
20379 if (font_info->vertical_centering)
20380 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20383 ascent = FONT_BASE (font) + boff;
20384 descent = FONT_DESCENT (font) - boff;
20386 if (override)
20388 it->override_ascent = ascent;
20389 it->override_descent = descent;
20390 it->override_boff = boff;
20393 height = ascent + descent;
20395 scale:
20396 if (FLOATP (val))
20397 height = (int)(XFLOAT_DATA (val) * height);
20398 else if (INTEGERP (val))
20399 height *= XINT (val);
20401 return make_number (height);
20405 /* RIF:
20406 Produce glyphs/get display metrics for the display element IT is
20407 loaded with. See the description of struct it in dispextern.h
20408 for an overview of struct it. */
20410 void
20411 x_produce_glyphs (it)
20412 struct it *it;
20414 int extra_line_spacing = it->extra_line_spacing;
20416 it->glyph_not_available_p = 0;
20418 if (it->what == IT_CHARACTER)
20420 XChar2b char2b;
20421 XFontStruct *font;
20422 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20423 XCharStruct *pcm;
20424 int font_not_found_p;
20425 struct font_info *font_info;
20426 int boff; /* baseline offset */
20427 /* We may change it->multibyte_p upon unibyte<->multibyte
20428 conversion. So, save the current value now and restore it
20429 later.
20431 Note: It seems that we don't have to record multibyte_p in
20432 struct glyph because the character code itself tells if or
20433 not the character is multibyte. Thus, in the future, we must
20434 consider eliminating the field `multibyte_p' in the struct
20435 glyph. */
20436 int saved_multibyte_p = it->multibyte_p;
20438 /* Maybe translate single-byte characters to multibyte, or the
20439 other way. */
20440 it->char_to_display = it->c;
20441 if (!ASCII_BYTE_P (it->c))
20443 if (unibyte_display_via_language_environment
20444 && SINGLE_BYTE_CHAR_P (it->c)
20445 && (it->c >= 0240
20446 || !NILP (Vnonascii_translation_table)))
20448 it->char_to_display = unibyte_char_to_multibyte (it->c);
20449 it->multibyte_p = 1;
20450 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20451 face = FACE_FROM_ID (it->f, it->face_id);
20453 else if (!SINGLE_BYTE_CHAR_P (it->c)
20454 && !it->multibyte_p)
20456 it->multibyte_p = 1;
20457 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20458 face = FACE_FROM_ID (it->f, it->face_id);
20462 /* Get font to use. Encode IT->char_to_display. */
20463 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20464 &char2b, it->multibyte_p, 0);
20465 font = face->font;
20467 /* When no suitable font found, use the default font. */
20468 font_not_found_p = font == NULL;
20469 if (font_not_found_p)
20471 font = FRAME_FONT (it->f);
20472 boff = FRAME_BASELINE_OFFSET (it->f);
20473 font_info = NULL;
20475 else
20477 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20478 boff = font_info->baseline_offset;
20479 if (font_info->vertical_centering)
20480 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20483 if (it->char_to_display >= ' '
20484 && (!it->multibyte_p || it->char_to_display < 128))
20486 /* Either unibyte or ASCII. */
20487 int stretched_p;
20489 it->nglyphs = 1;
20491 pcm = rif->per_char_metric (font, &char2b,
20492 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20494 if (it->override_ascent >= 0)
20496 it->ascent = it->override_ascent;
20497 it->descent = it->override_descent;
20498 boff = it->override_boff;
20500 else
20502 it->ascent = FONT_BASE (font) + boff;
20503 it->descent = FONT_DESCENT (font) - boff;
20506 if (pcm)
20508 it->phys_ascent = pcm->ascent + boff;
20509 it->phys_descent = pcm->descent - boff;
20510 it->pixel_width = pcm->width;
20512 else
20514 it->glyph_not_available_p = 1;
20515 it->phys_ascent = it->ascent;
20516 it->phys_descent = it->descent;
20517 it->pixel_width = FONT_WIDTH (font);
20520 if (it->constrain_row_ascent_descent_p)
20522 if (it->descent > it->max_descent)
20524 it->ascent += it->descent - it->max_descent;
20525 it->descent = it->max_descent;
20527 if (it->ascent > it->max_ascent)
20529 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20530 it->ascent = it->max_ascent;
20532 it->phys_ascent = min (it->phys_ascent, it->ascent);
20533 it->phys_descent = min (it->phys_descent, it->descent);
20534 extra_line_spacing = 0;
20537 /* If this is a space inside a region of text with
20538 `space-width' property, change its width. */
20539 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20540 if (stretched_p)
20541 it->pixel_width *= XFLOATINT (it->space_width);
20543 /* If face has a box, add the box thickness to the character
20544 height. If character has a box line to the left and/or
20545 right, add the box line width to the character's width. */
20546 if (face->box != FACE_NO_BOX)
20548 int thick = face->box_line_width;
20550 if (thick > 0)
20552 it->ascent += thick;
20553 it->descent += thick;
20555 else
20556 thick = -thick;
20558 if (it->start_of_box_run_p)
20559 it->pixel_width += thick;
20560 if (it->end_of_box_run_p)
20561 it->pixel_width += thick;
20564 /* If face has an overline, add the height of the overline
20565 (1 pixel) and a 1 pixel margin to the character height. */
20566 if (face->overline_p)
20567 it->ascent += overline_margin;
20569 if (it->constrain_row_ascent_descent_p)
20571 if (it->ascent > it->max_ascent)
20572 it->ascent = it->max_ascent;
20573 if (it->descent > it->max_descent)
20574 it->descent = it->max_descent;
20577 take_vertical_position_into_account (it);
20579 /* If we have to actually produce glyphs, do it. */
20580 if (it->glyph_row)
20582 if (stretched_p)
20584 /* Translate a space with a `space-width' property
20585 into a stretch glyph. */
20586 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20587 / FONT_HEIGHT (font));
20588 append_stretch_glyph (it, it->object, it->pixel_width,
20589 it->ascent + it->descent, ascent);
20591 else
20592 append_glyph (it);
20594 /* If characters with lbearing or rbearing are displayed
20595 in this line, record that fact in a flag of the
20596 glyph row. This is used to optimize X output code. */
20597 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20598 it->glyph_row->contains_overlapping_glyphs_p = 1;
20601 else if (it->char_to_display == '\n')
20603 /* A newline has no width but we need the height of the line.
20604 But if previous part of the line set a height, don't
20605 increase that height */
20607 Lisp_Object height;
20608 Lisp_Object total_height = Qnil;
20610 it->override_ascent = -1;
20611 it->pixel_width = 0;
20612 it->nglyphs = 0;
20614 height = get_line_height_property(it, Qline_height);
20615 /* Split (line-height total-height) list */
20616 if (CONSP (height)
20617 && CONSP (XCDR (height))
20618 && NILP (XCDR (XCDR (height))))
20620 total_height = XCAR (XCDR (height));
20621 height = XCAR (height);
20623 height = calc_line_height_property(it, height, font, boff, 1);
20625 if (it->override_ascent >= 0)
20627 it->ascent = it->override_ascent;
20628 it->descent = it->override_descent;
20629 boff = it->override_boff;
20631 else
20633 it->ascent = FONT_BASE (font) + boff;
20634 it->descent = FONT_DESCENT (font) - boff;
20637 if (EQ (height, Qt))
20639 if (it->descent > it->max_descent)
20641 it->ascent += it->descent - it->max_descent;
20642 it->descent = it->max_descent;
20644 if (it->ascent > it->max_ascent)
20646 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20647 it->ascent = it->max_ascent;
20649 it->phys_ascent = min (it->phys_ascent, it->ascent);
20650 it->phys_descent = min (it->phys_descent, it->descent);
20651 it->constrain_row_ascent_descent_p = 1;
20652 extra_line_spacing = 0;
20654 else
20656 Lisp_Object spacing;
20658 it->phys_ascent = it->ascent;
20659 it->phys_descent = it->descent;
20661 if ((it->max_ascent > 0 || it->max_descent > 0)
20662 && face->box != FACE_NO_BOX
20663 && face->box_line_width > 0)
20665 it->ascent += face->box_line_width;
20666 it->descent += face->box_line_width;
20668 if (!NILP (height)
20669 && XINT (height) > it->ascent + it->descent)
20670 it->ascent = XINT (height) - it->descent;
20672 if (!NILP (total_height))
20673 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20674 else
20676 spacing = get_line_height_property(it, Qline_spacing);
20677 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20679 if (INTEGERP (spacing))
20681 extra_line_spacing = XINT (spacing);
20682 if (!NILP (total_height))
20683 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20687 else if (it->char_to_display == '\t')
20689 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20690 int x = it->current_x + it->continuation_lines_width;
20691 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20693 /* If the distance from the current position to the next tab
20694 stop is less than a space character width, use the
20695 tab stop after that. */
20696 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20697 next_tab_x += tab_width;
20699 it->pixel_width = next_tab_x - x;
20700 it->nglyphs = 1;
20701 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20702 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20704 if (it->glyph_row)
20706 append_stretch_glyph (it, it->object, it->pixel_width,
20707 it->ascent + it->descent, it->ascent);
20710 else
20712 /* A multi-byte character. Assume that the display width of the
20713 character is the width of the character multiplied by the
20714 width of the font. */
20716 /* If we found a font, this font should give us the right
20717 metrics. If we didn't find a font, use the frame's
20718 default font and calculate the width of the character
20719 from the charset width; this is what old redisplay code
20720 did. */
20722 pcm = rif->per_char_metric (font, &char2b,
20723 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20725 if (font_not_found_p || !pcm)
20727 int charset = CHAR_CHARSET (it->char_to_display);
20729 it->glyph_not_available_p = 1;
20730 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20731 * CHARSET_WIDTH (charset));
20732 it->phys_ascent = FONT_BASE (font) + boff;
20733 it->phys_descent = FONT_DESCENT (font) - boff;
20735 else
20737 it->pixel_width = pcm->width;
20738 it->phys_ascent = pcm->ascent + boff;
20739 it->phys_descent = pcm->descent - boff;
20740 if (it->glyph_row
20741 && (pcm->lbearing < 0
20742 || pcm->rbearing > pcm->width))
20743 it->glyph_row->contains_overlapping_glyphs_p = 1;
20745 it->nglyphs = 1;
20746 it->ascent = FONT_BASE (font) + boff;
20747 it->descent = FONT_DESCENT (font) - boff;
20748 if (face->box != FACE_NO_BOX)
20750 int thick = face->box_line_width;
20752 if (thick > 0)
20754 it->ascent += thick;
20755 it->descent += thick;
20757 else
20758 thick = - thick;
20760 if (it->start_of_box_run_p)
20761 it->pixel_width += thick;
20762 if (it->end_of_box_run_p)
20763 it->pixel_width += thick;
20766 /* If face has an overline, add the height of the overline
20767 (1 pixel) and a 1 pixel margin to the character height. */
20768 if (face->overline_p)
20769 it->ascent += overline_margin;
20771 take_vertical_position_into_account (it);
20773 if (it->glyph_row)
20774 append_glyph (it);
20776 it->multibyte_p = saved_multibyte_p;
20778 else if (it->what == IT_COMPOSITION)
20780 /* Note: A composition is represented as one glyph in the
20781 glyph matrix. There are no padding glyphs. */
20782 XChar2b char2b;
20783 XFontStruct *font;
20784 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20785 XCharStruct *pcm;
20786 int font_not_found_p;
20787 struct font_info *font_info;
20788 int boff; /* baseline offset */
20789 struct composition *cmp = composition_table[it->cmp_id];
20791 /* Maybe translate single-byte characters to multibyte. */
20792 it->char_to_display = it->c;
20793 if (unibyte_display_via_language_environment
20794 && SINGLE_BYTE_CHAR_P (it->c)
20795 && (it->c >= 0240
20796 || (it->c >= 0200
20797 && !NILP (Vnonascii_translation_table))))
20799 it->char_to_display = unibyte_char_to_multibyte (it->c);
20802 /* Get face and font to use. Encode IT->char_to_display. */
20803 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20804 face = FACE_FROM_ID (it->f, it->face_id);
20805 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20806 &char2b, it->multibyte_p, 0);
20807 font = face->font;
20809 /* When no suitable font found, use the default font. */
20810 font_not_found_p = font == NULL;
20811 if (font_not_found_p)
20813 font = FRAME_FONT (it->f);
20814 boff = FRAME_BASELINE_OFFSET (it->f);
20815 font_info = NULL;
20817 else
20819 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20820 boff = font_info->baseline_offset;
20821 if (font_info->vertical_centering)
20822 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20825 /* There are no padding glyphs, so there is only one glyph to
20826 produce for the composition. Important is that pixel_width,
20827 ascent and descent are the values of what is drawn by
20828 draw_glyphs (i.e. the values of the overall glyphs composed). */
20829 it->nglyphs = 1;
20831 /* If we have not yet calculated pixel size data of glyphs of
20832 the composition for the current face font, calculate them
20833 now. Theoretically, we have to check all fonts for the
20834 glyphs, but that requires much time and memory space. So,
20835 here we check only the font of the first glyph. This leads
20836 to incorrect display very rarely, and C-l (recenter) can
20837 correct the display anyway. */
20838 if (cmp->font != (void *) font)
20840 /* Ascent and descent of the font of the first character of
20841 this composition (adjusted by baseline offset). Ascent
20842 and descent of overall glyphs should not be less than
20843 them respectively. */
20844 int font_ascent = FONT_BASE (font) + boff;
20845 int font_descent = FONT_DESCENT (font) - boff;
20846 /* Bounding box of the overall glyphs. */
20847 int leftmost, rightmost, lowest, highest;
20848 int i, width, ascent, descent;
20850 cmp->font = (void *) font;
20852 /* Initialize the bounding box. */
20853 if (font_info
20854 && (pcm = rif->per_char_metric (font, &char2b,
20855 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20857 width = pcm->width;
20858 ascent = pcm->ascent;
20859 descent = pcm->descent;
20861 else
20863 width = FONT_WIDTH (font);
20864 ascent = FONT_BASE (font);
20865 descent = FONT_DESCENT (font);
20868 rightmost = width;
20869 lowest = - descent + boff;
20870 highest = ascent + boff;
20871 leftmost = 0;
20873 if (font_info
20874 && font_info->default_ascent
20875 && CHAR_TABLE_P (Vuse_default_ascent)
20876 && !NILP (Faref (Vuse_default_ascent,
20877 make_number (it->char_to_display))))
20878 highest = font_info->default_ascent + boff;
20880 /* Draw the first glyph at the normal position. It may be
20881 shifted to right later if some other glyphs are drawn at
20882 the left. */
20883 cmp->offsets[0] = 0;
20884 cmp->offsets[1] = boff;
20886 /* Set cmp->offsets for the remaining glyphs. */
20887 for (i = 1; i < cmp->glyph_len; i++)
20889 int left, right, btm, top;
20890 int ch = COMPOSITION_GLYPH (cmp, i);
20891 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20893 face = FACE_FROM_ID (it->f, face_id);
20894 get_char_face_and_encoding (it->f, ch, face->id,
20895 &char2b, it->multibyte_p, 0);
20896 font = face->font;
20897 if (font == NULL)
20899 font = FRAME_FONT (it->f);
20900 boff = FRAME_BASELINE_OFFSET (it->f);
20901 font_info = NULL;
20903 else
20905 font_info
20906 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20907 boff = font_info->baseline_offset;
20908 if (font_info->vertical_centering)
20909 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20912 if (font_info
20913 && (pcm = rif->per_char_metric (font, &char2b,
20914 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20916 width = pcm->width;
20917 ascent = pcm->ascent;
20918 descent = pcm->descent;
20920 else
20922 width = FONT_WIDTH (font);
20923 ascent = 1;
20924 descent = 0;
20927 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20929 /* Relative composition with or without
20930 alternate chars. */
20931 left = (leftmost + rightmost - width) / 2;
20932 btm = - descent + boff;
20933 if (font_info && font_info->relative_compose
20934 && (! CHAR_TABLE_P (Vignore_relative_composition)
20935 || NILP (Faref (Vignore_relative_composition,
20936 make_number (ch)))))
20939 if (- descent >= font_info->relative_compose)
20940 /* One extra pixel between two glyphs. */
20941 btm = highest + 1;
20942 else if (ascent <= 0)
20943 /* One extra pixel between two glyphs. */
20944 btm = lowest - 1 - ascent - descent;
20947 else
20949 /* A composition rule is specified by an integer
20950 value that encodes global and new reference
20951 points (GREF and NREF). GREF and NREF are
20952 specified by numbers as below:
20954 0---1---2 -- ascent
20958 9--10--11 -- center
20960 ---3---4---5--- baseline
20962 6---7---8 -- descent
20964 int rule = COMPOSITION_RULE (cmp, i);
20965 int gref, nref, grefx, grefy, nrefx, nrefy;
20967 COMPOSITION_DECODE_RULE (rule, gref, nref);
20968 grefx = gref % 3, nrefx = nref % 3;
20969 grefy = gref / 3, nrefy = nref / 3;
20971 left = (leftmost
20972 + grefx * (rightmost - leftmost) / 2
20973 - nrefx * width / 2);
20974 btm = ((grefy == 0 ? highest
20975 : grefy == 1 ? 0
20976 : grefy == 2 ? lowest
20977 : (highest + lowest) / 2)
20978 - (nrefy == 0 ? ascent + descent
20979 : nrefy == 1 ? descent - boff
20980 : nrefy == 2 ? 0
20981 : (ascent + descent) / 2));
20984 cmp->offsets[i * 2] = left;
20985 cmp->offsets[i * 2 + 1] = btm + descent;
20987 /* Update the bounding box of the overall glyphs. */
20988 right = left + width;
20989 top = btm + descent + ascent;
20990 if (left < leftmost)
20991 leftmost = left;
20992 if (right > rightmost)
20993 rightmost = right;
20994 if (top > highest)
20995 highest = top;
20996 if (btm < lowest)
20997 lowest = btm;
21000 /* If there are glyphs whose x-offsets are negative,
21001 shift all glyphs to the right and make all x-offsets
21002 non-negative. */
21003 if (leftmost < 0)
21005 for (i = 0; i < cmp->glyph_len; i++)
21006 cmp->offsets[i * 2] -= leftmost;
21007 rightmost -= leftmost;
21010 cmp->pixel_width = rightmost;
21011 cmp->ascent = highest;
21012 cmp->descent = - lowest;
21013 if (cmp->ascent < font_ascent)
21014 cmp->ascent = font_ascent;
21015 if (cmp->descent < font_descent)
21016 cmp->descent = font_descent;
21019 it->pixel_width = cmp->pixel_width;
21020 it->ascent = it->phys_ascent = cmp->ascent;
21021 it->descent = it->phys_descent = cmp->descent;
21023 if (face->box != FACE_NO_BOX)
21025 int thick = face->box_line_width;
21027 if (thick > 0)
21029 it->ascent += thick;
21030 it->descent += thick;
21032 else
21033 thick = - thick;
21035 if (it->start_of_box_run_p)
21036 it->pixel_width += thick;
21037 if (it->end_of_box_run_p)
21038 it->pixel_width += thick;
21041 /* If face has an overline, add the height of the overline
21042 (1 pixel) and a 1 pixel margin to the character height. */
21043 if (face->overline_p)
21044 it->ascent += overline_margin;
21046 take_vertical_position_into_account (it);
21048 if (it->glyph_row)
21049 append_composite_glyph (it);
21051 else if (it->what == IT_IMAGE)
21052 produce_image_glyph (it);
21053 else if (it->what == IT_STRETCH)
21054 produce_stretch_glyph (it);
21056 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21057 because this isn't true for images with `:ascent 100'. */
21058 xassert (it->ascent >= 0 && it->descent >= 0);
21059 if (it->area == TEXT_AREA)
21060 it->current_x += it->pixel_width;
21062 if (extra_line_spacing > 0)
21064 it->descent += extra_line_spacing;
21065 if (extra_line_spacing > it->max_extra_line_spacing)
21066 it->max_extra_line_spacing = extra_line_spacing;
21069 it->max_ascent = max (it->max_ascent, it->ascent);
21070 it->max_descent = max (it->max_descent, it->descent);
21071 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21072 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21075 /* EXPORT for RIF:
21076 Output LEN glyphs starting at START at the nominal cursor position.
21077 Advance the nominal cursor over the text. The global variable
21078 updated_window contains the window being updated, updated_row is
21079 the glyph row being updated, and updated_area is the area of that
21080 row being updated. */
21082 void
21083 x_write_glyphs (start, len)
21084 struct glyph *start;
21085 int len;
21087 int x, hpos;
21089 xassert (updated_window && updated_row);
21090 BLOCK_INPUT;
21092 /* Write glyphs. */
21094 hpos = start - updated_row->glyphs[updated_area];
21095 x = draw_glyphs (updated_window, output_cursor.x,
21096 updated_row, updated_area,
21097 hpos, hpos + len,
21098 DRAW_NORMAL_TEXT, 0);
21100 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21101 if (updated_area == TEXT_AREA
21102 && updated_window->phys_cursor_on_p
21103 && updated_window->phys_cursor.vpos == output_cursor.vpos
21104 && updated_window->phys_cursor.hpos >= hpos
21105 && updated_window->phys_cursor.hpos < hpos + len)
21106 updated_window->phys_cursor_on_p = 0;
21108 UNBLOCK_INPUT;
21110 /* Advance the output cursor. */
21111 output_cursor.hpos += len;
21112 output_cursor.x = x;
21116 /* EXPORT for RIF:
21117 Insert LEN glyphs from START at the nominal cursor position. */
21119 void
21120 x_insert_glyphs (start, len)
21121 struct glyph *start;
21122 int len;
21124 struct frame *f;
21125 struct window *w;
21126 int line_height, shift_by_width, shifted_region_width;
21127 struct glyph_row *row;
21128 struct glyph *glyph;
21129 int frame_x, frame_y, hpos;
21131 xassert (updated_window && updated_row);
21132 BLOCK_INPUT;
21133 w = updated_window;
21134 f = XFRAME (WINDOW_FRAME (w));
21136 /* Get the height of the line we are in. */
21137 row = updated_row;
21138 line_height = row->height;
21140 /* Get the width of the glyphs to insert. */
21141 shift_by_width = 0;
21142 for (glyph = start; glyph < start + len; ++glyph)
21143 shift_by_width += glyph->pixel_width;
21145 /* Get the width of the region to shift right. */
21146 shifted_region_width = (window_box_width (w, updated_area)
21147 - output_cursor.x
21148 - shift_by_width);
21150 /* Shift right. */
21151 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21152 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21154 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21155 line_height, shift_by_width);
21157 /* Write the glyphs. */
21158 hpos = start - row->glyphs[updated_area];
21159 draw_glyphs (w, output_cursor.x, row, updated_area,
21160 hpos, hpos + len,
21161 DRAW_NORMAL_TEXT, 0);
21163 /* Advance the output cursor. */
21164 output_cursor.hpos += len;
21165 output_cursor.x += shift_by_width;
21166 UNBLOCK_INPUT;
21170 /* EXPORT for RIF:
21171 Erase the current text line from the nominal cursor position
21172 (inclusive) to pixel column TO_X (exclusive). The idea is that
21173 everything from TO_X onward is already erased.
21175 TO_X is a pixel position relative to updated_area of
21176 updated_window. TO_X == -1 means clear to the end of this area. */
21178 void
21179 x_clear_end_of_line (to_x)
21180 int to_x;
21182 struct frame *f;
21183 struct window *w = updated_window;
21184 int max_x, min_y, max_y;
21185 int from_x, from_y, to_y;
21187 xassert (updated_window && updated_row);
21188 f = XFRAME (w->frame);
21190 if (updated_row->full_width_p)
21191 max_x = WINDOW_TOTAL_WIDTH (w);
21192 else
21193 max_x = window_box_width (w, updated_area);
21194 max_y = window_text_bottom_y (w);
21196 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21197 of window. For TO_X > 0, truncate to end of drawing area. */
21198 if (to_x == 0)
21199 return;
21200 else if (to_x < 0)
21201 to_x = max_x;
21202 else
21203 to_x = min (to_x, max_x);
21205 to_y = min (max_y, output_cursor.y + updated_row->height);
21207 /* Notice if the cursor will be cleared by this operation. */
21208 if (!updated_row->full_width_p)
21209 notice_overwritten_cursor (w, updated_area,
21210 output_cursor.x, -1,
21211 updated_row->y,
21212 MATRIX_ROW_BOTTOM_Y (updated_row));
21214 from_x = output_cursor.x;
21216 /* Translate to frame coordinates. */
21217 if (updated_row->full_width_p)
21219 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21220 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21222 else
21224 int area_left = window_box_left (w, updated_area);
21225 from_x += area_left;
21226 to_x += area_left;
21229 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21230 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21231 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21233 /* Prevent inadvertently clearing to end of the X window. */
21234 if (to_x > from_x && to_y > from_y)
21236 BLOCK_INPUT;
21237 rif->clear_frame_area (f, from_x, from_y,
21238 to_x - from_x, to_y - from_y);
21239 UNBLOCK_INPUT;
21243 #endif /* HAVE_WINDOW_SYSTEM */
21247 /***********************************************************************
21248 Cursor types
21249 ***********************************************************************/
21251 /* Value is the internal representation of the specified cursor type
21252 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21253 of the bar cursor. */
21255 static enum text_cursor_kinds
21256 get_specified_cursor_type (arg, width)
21257 Lisp_Object arg;
21258 int *width;
21260 enum text_cursor_kinds type;
21262 if (NILP (arg))
21263 return NO_CURSOR;
21265 if (EQ (arg, Qbox))
21266 return FILLED_BOX_CURSOR;
21268 if (EQ (arg, Qhollow))
21269 return HOLLOW_BOX_CURSOR;
21271 if (EQ (arg, Qbar))
21273 *width = 2;
21274 return BAR_CURSOR;
21277 if (CONSP (arg)
21278 && EQ (XCAR (arg), Qbar)
21279 && INTEGERP (XCDR (arg))
21280 && XINT (XCDR (arg)) >= 0)
21282 *width = XINT (XCDR (arg));
21283 return BAR_CURSOR;
21286 if (EQ (arg, Qhbar))
21288 *width = 2;
21289 return HBAR_CURSOR;
21292 if (CONSP (arg)
21293 && EQ (XCAR (arg), Qhbar)
21294 && INTEGERP (XCDR (arg))
21295 && XINT (XCDR (arg)) >= 0)
21297 *width = XINT (XCDR (arg));
21298 return HBAR_CURSOR;
21301 /* Treat anything unknown as "hollow box cursor".
21302 It was bad to signal an error; people have trouble fixing
21303 .Xdefaults with Emacs, when it has something bad in it. */
21304 type = HOLLOW_BOX_CURSOR;
21306 return type;
21309 /* Set the default cursor types for specified frame. */
21310 void
21311 set_frame_cursor_types (f, arg)
21312 struct frame *f;
21313 Lisp_Object arg;
21315 int width;
21316 Lisp_Object tem;
21318 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21319 FRAME_CURSOR_WIDTH (f) = width;
21321 /* By default, set up the blink-off state depending on the on-state. */
21323 tem = Fassoc (arg, Vblink_cursor_alist);
21324 if (!NILP (tem))
21326 FRAME_BLINK_OFF_CURSOR (f)
21327 = get_specified_cursor_type (XCDR (tem), &width);
21328 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21330 else
21331 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21335 /* Return the cursor we want to be displayed in window W. Return
21336 width of bar/hbar cursor through WIDTH arg. Return with
21337 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21338 (i.e. if the `system caret' should track this cursor).
21340 In a mini-buffer window, we want the cursor only to appear if we
21341 are reading input from this window. For the selected window, we
21342 want the cursor type given by the frame parameter or buffer local
21343 setting of cursor-type. If explicitly marked off, draw no cursor.
21344 In all other cases, we want a hollow box cursor. */
21346 static enum text_cursor_kinds
21347 get_window_cursor_type (w, glyph, width, active_cursor)
21348 struct window *w;
21349 struct glyph *glyph;
21350 int *width;
21351 int *active_cursor;
21353 struct frame *f = XFRAME (w->frame);
21354 struct buffer *b = XBUFFER (w->buffer);
21355 int cursor_type = DEFAULT_CURSOR;
21356 Lisp_Object alt_cursor;
21357 int non_selected = 0;
21359 *active_cursor = 1;
21361 /* Echo area */
21362 if (cursor_in_echo_area
21363 && FRAME_HAS_MINIBUF_P (f)
21364 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21366 if (w == XWINDOW (echo_area_window))
21368 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21370 *width = FRAME_CURSOR_WIDTH (f);
21371 return FRAME_DESIRED_CURSOR (f);
21373 else
21374 return get_specified_cursor_type (b->cursor_type, width);
21377 *active_cursor = 0;
21378 non_selected = 1;
21381 /* Detect a nonselected window or nonselected frame. */
21382 else if (w != XWINDOW (f->selected_window)
21383 #ifdef HAVE_WINDOW_SYSTEM
21384 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21385 #endif
21388 *active_cursor = 0;
21390 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21391 return NO_CURSOR;
21393 non_selected = 1;
21396 /* Never display a cursor in a window in which cursor-type is nil. */
21397 if (NILP (b->cursor_type))
21398 return NO_CURSOR;
21400 /* Get the normal cursor type for this window. */
21401 if (EQ (b->cursor_type, Qt))
21403 cursor_type = FRAME_DESIRED_CURSOR (f);
21404 *width = FRAME_CURSOR_WIDTH (f);
21406 else
21407 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21409 /* Use cursor-in-non-selected-windows instead
21410 for non-selected window or frame. */
21411 if (non_selected)
21413 alt_cursor = b->cursor_in_non_selected_windows;
21414 if (!EQ (Qt, alt_cursor))
21415 return get_specified_cursor_type (alt_cursor, width);
21416 /* t means modify the normal cursor type. */
21417 if (cursor_type == FILLED_BOX_CURSOR)
21418 cursor_type = HOLLOW_BOX_CURSOR;
21419 else if (cursor_type == BAR_CURSOR && *width > 1)
21420 --*width;
21421 return cursor_type;
21424 /* Use normal cursor if not blinked off. */
21425 if (!w->cursor_off_p)
21427 #ifdef HAVE_WINDOW_SYSTEM
21428 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21430 if (cursor_type == FILLED_BOX_CURSOR)
21432 /* Using a block cursor on large images can be very annoying.
21433 So use a hollow cursor for "large" images.
21434 If image is not transparent (no mask), also use hollow cursor. */
21435 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21436 if (img != NULL && IMAGEP (img->spec))
21438 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21439 where N = size of default frame font size.
21440 This should cover most of the "tiny" icons people may use. */
21441 if (!img->mask
21442 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21443 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21444 cursor_type = HOLLOW_BOX_CURSOR;
21447 else if (cursor_type != NO_CURSOR)
21449 /* Display current only supports BOX and HOLLOW cursors for images.
21450 So for now, unconditionally use a HOLLOW cursor when cursor is
21451 not a solid box cursor. */
21452 cursor_type = HOLLOW_BOX_CURSOR;
21455 #endif
21456 return cursor_type;
21459 /* Cursor is blinked off, so determine how to "toggle" it. */
21461 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21462 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21463 return get_specified_cursor_type (XCDR (alt_cursor), width);
21465 /* Then see if frame has specified a specific blink off cursor type. */
21466 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21468 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21469 return FRAME_BLINK_OFF_CURSOR (f);
21472 #if 0
21473 /* Some people liked having a permanently visible blinking cursor,
21474 while others had very strong opinions against it. So it was
21475 decided to remove it. KFS 2003-09-03 */
21477 /* Finally perform built-in cursor blinking:
21478 filled box <-> hollow box
21479 wide [h]bar <-> narrow [h]bar
21480 narrow [h]bar <-> no cursor
21481 other type <-> no cursor */
21483 if (cursor_type == FILLED_BOX_CURSOR)
21484 return HOLLOW_BOX_CURSOR;
21486 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21488 *width = 1;
21489 return cursor_type;
21491 #endif
21493 return NO_CURSOR;
21497 #ifdef HAVE_WINDOW_SYSTEM
21499 /* Notice when the text cursor of window W has been completely
21500 overwritten by a drawing operation that outputs glyphs in AREA
21501 starting at X0 and ending at X1 in the line starting at Y0 and
21502 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21503 the rest of the line after X0 has been written. Y coordinates
21504 are window-relative. */
21506 static void
21507 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21508 struct window *w;
21509 enum glyph_row_area area;
21510 int x0, y0, x1, y1;
21512 int cx0, cx1, cy0, cy1;
21513 struct glyph_row *row;
21515 if (!w->phys_cursor_on_p)
21516 return;
21517 if (area != TEXT_AREA)
21518 return;
21520 if (w->phys_cursor.vpos < 0
21521 || w->phys_cursor.vpos >= w->current_matrix->nrows
21522 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21523 !(row->enabled_p && row->displays_text_p)))
21524 return;
21526 if (row->cursor_in_fringe_p)
21528 row->cursor_in_fringe_p = 0;
21529 draw_fringe_bitmap (w, row, 0);
21530 w->phys_cursor_on_p = 0;
21531 return;
21534 cx0 = w->phys_cursor.x;
21535 cx1 = cx0 + w->phys_cursor_width;
21536 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21537 return;
21539 /* The cursor image will be completely removed from the
21540 screen if the output area intersects the cursor area in
21541 y-direction. When we draw in [y0 y1[, and some part of
21542 the cursor is at y < y0, that part must have been drawn
21543 before. When scrolling, the cursor is erased before
21544 actually scrolling, so we don't come here. When not
21545 scrolling, the rows above the old cursor row must have
21546 changed, and in this case these rows must have written
21547 over the cursor image.
21549 Likewise if part of the cursor is below y1, with the
21550 exception of the cursor being in the first blank row at
21551 the buffer and window end because update_text_area
21552 doesn't draw that row. (Except when it does, but
21553 that's handled in update_text_area.) */
21555 cy0 = w->phys_cursor.y;
21556 cy1 = cy0 + w->phys_cursor_height;
21557 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21558 return;
21560 w->phys_cursor_on_p = 0;
21563 #endif /* HAVE_WINDOW_SYSTEM */
21566 /************************************************************************
21567 Mouse Face
21568 ************************************************************************/
21570 #ifdef HAVE_WINDOW_SYSTEM
21572 /* EXPORT for RIF:
21573 Fix the display of area AREA of overlapping row ROW in window W
21574 with respect to the overlapping part OVERLAPS. */
21576 void
21577 x_fix_overlapping_area (w, row, area, overlaps)
21578 struct window *w;
21579 struct glyph_row *row;
21580 enum glyph_row_area area;
21581 int overlaps;
21583 int i, x;
21585 BLOCK_INPUT;
21587 x = 0;
21588 for (i = 0; i < row->used[area];)
21590 if (row->glyphs[area][i].overlaps_vertically_p)
21592 int start = i, start_x = x;
21596 x += row->glyphs[area][i].pixel_width;
21597 ++i;
21599 while (i < row->used[area]
21600 && row->glyphs[area][i].overlaps_vertically_p);
21602 draw_glyphs (w, start_x, row, area,
21603 start, i,
21604 DRAW_NORMAL_TEXT, overlaps);
21606 else
21608 x += row->glyphs[area][i].pixel_width;
21609 ++i;
21613 UNBLOCK_INPUT;
21617 /* EXPORT:
21618 Draw the cursor glyph of window W in glyph row ROW. See the
21619 comment of draw_glyphs for the meaning of HL. */
21621 void
21622 draw_phys_cursor_glyph (w, row, hl)
21623 struct window *w;
21624 struct glyph_row *row;
21625 enum draw_glyphs_face hl;
21627 /* If cursor hpos is out of bounds, don't draw garbage. This can
21628 happen in mini-buffer windows when switching between echo area
21629 glyphs and mini-buffer. */
21630 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21632 int on_p = w->phys_cursor_on_p;
21633 int x1;
21634 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21635 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21636 hl, 0);
21637 w->phys_cursor_on_p = on_p;
21639 if (hl == DRAW_CURSOR)
21640 w->phys_cursor_width = x1 - w->phys_cursor.x;
21641 /* When we erase the cursor, and ROW is overlapped by other
21642 rows, make sure that these overlapping parts of other rows
21643 are redrawn. */
21644 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21646 w->phys_cursor_width = x1 - w->phys_cursor.x;
21648 if (row > w->current_matrix->rows
21649 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21650 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21651 OVERLAPS_ERASED_CURSOR);
21653 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21654 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21655 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21656 OVERLAPS_ERASED_CURSOR);
21662 /* EXPORT:
21663 Erase the image of a cursor of window W from the screen. */
21665 void
21666 erase_phys_cursor (w)
21667 struct window *w;
21669 struct frame *f = XFRAME (w->frame);
21670 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21671 int hpos = w->phys_cursor.hpos;
21672 int vpos = w->phys_cursor.vpos;
21673 int mouse_face_here_p = 0;
21674 struct glyph_matrix *active_glyphs = w->current_matrix;
21675 struct glyph_row *cursor_row;
21676 struct glyph *cursor_glyph;
21677 enum draw_glyphs_face hl;
21679 /* No cursor displayed or row invalidated => nothing to do on the
21680 screen. */
21681 if (w->phys_cursor_type == NO_CURSOR)
21682 goto mark_cursor_off;
21684 /* VPOS >= active_glyphs->nrows means that window has been resized.
21685 Don't bother to erase the cursor. */
21686 if (vpos >= active_glyphs->nrows)
21687 goto mark_cursor_off;
21689 /* If row containing cursor is marked invalid, there is nothing we
21690 can do. */
21691 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21692 if (!cursor_row->enabled_p)
21693 goto mark_cursor_off;
21695 /* If line spacing is > 0, old cursor may only be partially visible in
21696 window after split-window. So adjust visible height. */
21697 cursor_row->visible_height = min (cursor_row->visible_height,
21698 window_text_bottom_y (w) - cursor_row->y);
21700 /* If row is completely invisible, don't attempt to delete a cursor which
21701 isn't there. This can happen if cursor is at top of a window, and
21702 we switch to a buffer with a header line in that window. */
21703 if (cursor_row->visible_height <= 0)
21704 goto mark_cursor_off;
21706 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21707 if (cursor_row->cursor_in_fringe_p)
21709 cursor_row->cursor_in_fringe_p = 0;
21710 draw_fringe_bitmap (w, cursor_row, 0);
21711 goto mark_cursor_off;
21714 /* This can happen when the new row is shorter than the old one.
21715 In this case, either draw_glyphs or clear_end_of_line
21716 should have cleared the cursor. Note that we wouldn't be
21717 able to erase the cursor in this case because we don't have a
21718 cursor glyph at hand. */
21719 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21720 goto mark_cursor_off;
21722 /* If the cursor is in the mouse face area, redisplay that when
21723 we clear the cursor. */
21724 if (! NILP (dpyinfo->mouse_face_window)
21725 && w == XWINDOW (dpyinfo->mouse_face_window)
21726 && (vpos > dpyinfo->mouse_face_beg_row
21727 || (vpos == dpyinfo->mouse_face_beg_row
21728 && hpos >= dpyinfo->mouse_face_beg_col))
21729 && (vpos < dpyinfo->mouse_face_end_row
21730 || (vpos == dpyinfo->mouse_face_end_row
21731 && hpos < dpyinfo->mouse_face_end_col))
21732 /* Don't redraw the cursor's spot in mouse face if it is at the
21733 end of a line (on a newline). The cursor appears there, but
21734 mouse highlighting does not. */
21735 && cursor_row->used[TEXT_AREA] > hpos)
21736 mouse_face_here_p = 1;
21738 /* Maybe clear the display under the cursor. */
21739 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21741 int x, y, left_x;
21742 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21743 int width;
21745 cursor_glyph = get_phys_cursor_glyph (w);
21746 if (cursor_glyph == NULL)
21747 goto mark_cursor_off;
21749 width = cursor_glyph->pixel_width;
21750 left_x = window_box_left_offset (w, TEXT_AREA);
21751 x = w->phys_cursor.x;
21752 if (x < left_x)
21753 width -= left_x - x;
21754 width = min (width, window_box_width (w, TEXT_AREA) - x);
21755 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21756 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21758 if (width > 0)
21759 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21762 /* Erase the cursor by redrawing the character underneath it. */
21763 if (mouse_face_here_p)
21764 hl = DRAW_MOUSE_FACE;
21765 else
21766 hl = DRAW_NORMAL_TEXT;
21767 draw_phys_cursor_glyph (w, cursor_row, hl);
21769 mark_cursor_off:
21770 w->phys_cursor_on_p = 0;
21771 w->phys_cursor_type = NO_CURSOR;
21775 /* EXPORT:
21776 Display or clear cursor of window W. If ON is zero, clear the
21777 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21778 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21780 void
21781 display_and_set_cursor (w, on, hpos, vpos, x, y)
21782 struct window *w;
21783 int on, hpos, vpos, x, y;
21785 struct frame *f = XFRAME (w->frame);
21786 int new_cursor_type;
21787 int new_cursor_width;
21788 int active_cursor;
21789 struct glyph_row *glyph_row;
21790 struct glyph *glyph;
21792 /* This is pointless on invisible frames, and dangerous on garbaged
21793 windows and frames; in the latter case, the frame or window may
21794 be in the midst of changing its size, and x and y may be off the
21795 window. */
21796 if (! FRAME_VISIBLE_P (f)
21797 || FRAME_GARBAGED_P (f)
21798 || vpos >= w->current_matrix->nrows
21799 || hpos >= w->current_matrix->matrix_w)
21800 return;
21802 /* If cursor is off and we want it off, return quickly. */
21803 if (!on && !w->phys_cursor_on_p)
21804 return;
21806 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21807 /* If cursor row is not enabled, we don't really know where to
21808 display the cursor. */
21809 if (!glyph_row->enabled_p)
21811 w->phys_cursor_on_p = 0;
21812 return;
21815 glyph = NULL;
21816 if (!glyph_row->exact_window_width_line_p
21817 || hpos < glyph_row->used[TEXT_AREA])
21818 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21820 xassert (interrupt_input_blocked);
21822 /* Set new_cursor_type to the cursor we want to be displayed. */
21823 new_cursor_type = get_window_cursor_type (w, glyph,
21824 &new_cursor_width, &active_cursor);
21826 /* If cursor is currently being shown and we don't want it to be or
21827 it is in the wrong place, or the cursor type is not what we want,
21828 erase it. */
21829 if (w->phys_cursor_on_p
21830 && (!on
21831 || w->phys_cursor.x != x
21832 || w->phys_cursor.y != y
21833 || new_cursor_type != w->phys_cursor_type
21834 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21835 && new_cursor_width != w->phys_cursor_width)))
21836 erase_phys_cursor (w);
21838 /* Don't check phys_cursor_on_p here because that flag is only set
21839 to zero in some cases where we know that the cursor has been
21840 completely erased, to avoid the extra work of erasing the cursor
21841 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21842 still not be visible, or it has only been partly erased. */
21843 if (on)
21845 w->phys_cursor_ascent = glyph_row->ascent;
21846 w->phys_cursor_height = glyph_row->height;
21848 /* Set phys_cursor_.* before x_draw_.* is called because some
21849 of them may need the information. */
21850 w->phys_cursor.x = x;
21851 w->phys_cursor.y = glyph_row->y;
21852 w->phys_cursor.hpos = hpos;
21853 w->phys_cursor.vpos = vpos;
21856 rif->draw_window_cursor (w, glyph_row, x, y,
21857 new_cursor_type, new_cursor_width,
21858 on, active_cursor);
21862 /* Switch the display of W's cursor on or off, according to the value
21863 of ON. */
21865 static void
21866 update_window_cursor (w, on)
21867 struct window *w;
21868 int on;
21870 /* Don't update cursor in windows whose frame is in the process
21871 of being deleted. */
21872 if (w->current_matrix)
21874 BLOCK_INPUT;
21875 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21876 w->phys_cursor.x, w->phys_cursor.y);
21877 UNBLOCK_INPUT;
21882 /* Call update_window_cursor with parameter ON_P on all leaf windows
21883 in the window tree rooted at W. */
21885 static void
21886 update_cursor_in_window_tree (w, on_p)
21887 struct window *w;
21888 int on_p;
21890 while (w)
21892 if (!NILP (w->hchild))
21893 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21894 else if (!NILP (w->vchild))
21895 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21896 else
21897 update_window_cursor (w, on_p);
21899 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21904 /* EXPORT:
21905 Display the cursor on window W, or clear it, according to ON_P.
21906 Don't change the cursor's position. */
21908 void
21909 x_update_cursor (f, on_p)
21910 struct frame *f;
21911 int on_p;
21913 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21917 /* EXPORT:
21918 Clear the cursor of window W to background color, and mark the
21919 cursor as not shown. This is used when the text where the cursor
21920 is is about to be rewritten. */
21922 void
21923 x_clear_cursor (w)
21924 struct window *w;
21926 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21927 update_window_cursor (w, 0);
21931 /* EXPORT:
21932 Display the active region described by mouse_face_* according to DRAW. */
21934 void
21935 show_mouse_face (dpyinfo, draw)
21936 Display_Info *dpyinfo;
21937 enum draw_glyphs_face draw;
21939 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21940 struct frame *f = XFRAME (WINDOW_FRAME (w));
21942 if (/* If window is in the process of being destroyed, don't bother
21943 to do anything. */
21944 w->current_matrix != NULL
21945 /* Don't update mouse highlight if hidden */
21946 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21947 /* Recognize when we are called to operate on rows that don't exist
21948 anymore. This can happen when a window is split. */
21949 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21951 int phys_cursor_on_p = w->phys_cursor_on_p;
21952 struct glyph_row *row, *first, *last;
21954 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21955 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21957 for (row = first; row <= last && row->enabled_p; ++row)
21959 int start_hpos, end_hpos, start_x;
21961 /* For all but the first row, the highlight starts at column 0. */
21962 if (row == first)
21964 start_hpos = dpyinfo->mouse_face_beg_col;
21965 start_x = dpyinfo->mouse_face_beg_x;
21967 else
21969 start_hpos = 0;
21970 start_x = 0;
21973 if (row == last)
21974 end_hpos = dpyinfo->mouse_face_end_col;
21975 else
21977 end_hpos = row->used[TEXT_AREA];
21978 if (draw == DRAW_NORMAL_TEXT)
21979 row->fill_line_p = 1; /* Clear to end of line */
21982 if (end_hpos > start_hpos)
21984 draw_glyphs (w, start_x, row, TEXT_AREA,
21985 start_hpos, end_hpos,
21986 draw, 0);
21988 row->mouse_face_p
21989 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21993 /* When we've written over the cursor, arrange for it to
21994 be displayed again. */
21995 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21997 BLOCK_INPUT;
21998 display_and_set_cursor (w, 1,
21999 w->phys_cursor.hpos, w->phys_cursor.vpos,
22000 w->phys_cursor.x, w->phys_cursor.y);
22001 UNBLOCK_INPUT;
22005 /* Change the mouse cursor. */
22006 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22007 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22008 else if (draw == DRAW_MOUSE_FACE)
22009 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22010 else
22011 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22014 /* EXPORT:
22015 Clear out the mouse-highlighted active region.
22016 Redraw it un-highlighted first. Value is non-zero if mouse
22017 face was actually drawn unhighlighted. */
22020 clear_mouse_face (dpyinfo)
22021 Display_Info *dpyinfo;
22023 int cleared = 0;
22025 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22027 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22028 cleared = 1;
22031 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22032 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22033 dpyinfo->mouse_face_window = Qnil;
22034 dpyinfo->mouse_face_overlay = Qnil;
22035 return cleared;
22039 /* EXPORT:
22040 Non-zero if physical cursor of window W is within mouse face. */
22043 cursor_in_mouse_face_p (w)
22044 struct window *w;
22046 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22047 int in_mouse_face = 0;
22049 if (WINDOWP (dpyinfo->mouse_face_window)
22050 && XWINDOW (dpyinfo->mouse_face_window) == w)
22052 int hpos = w->phys_cursor.hpos;
22053 int vpos = w->phys_cursor.vpos;
22055 if (vpos >= dpyinfo->mouse_face_beg_row
22056 && vpos <= dpyinfo->mouse_face_end_row
22057 && (vpos > dpyinfo->mouse_face_beg_row
22058 || hpos >= dpyinfo->mouse_face_beg_col)
22059 && (vpos < dpyinfo->mouse_face_end_row
22060 || hpos < dpyinfo->mouse_face_end_col
22061 || dpyinfo->mouse_face_past_end))
22062 in_mouse_face = 1;
22065 return in_mouse_face;
22071 /* Find the glyph matrix position of buffer position CHARPOS in window
22072 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22073 current glyphs must be up to date. If CHARPOS is above window
22074 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22075 of last line in W. In the row containing CHARPOS, stop before glyphs
22076 having STOP as object. */
22078 #if 1 /* This is a version of fast_find_position that's more correct
22079 in the presence of hscrolling, for example. I didn't install
22080 it right away because the problem fixed is minor, it failed
22081 in 20.x as well, and I think it's too risky to install
22082 so near the release of 21.1. 2001-09-25 gerd. */
22084 static int
22085 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22086 struct window *w;
22087 int charpos;
22088 int *hpos, *vpos, *x, *y;
22089 Lisp_Object stop;
22091 struct glyph_row *row, *first;
22092 struct glyph *glyph, *end;
22093 int past_end = 0;
22095 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22096 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22098 *x = first->x;
22099 *y = first->y;
22100 *hpos = 0;
22101 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22102 return 1;
22105 row = row_containing_pos (w, charpos, first, NULL, 0);
22106 if (row == NULL)
22108 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22109 past_end = 1;
22112 /* If whole rows or last part of a row came from a display overlay,
22113 row_containing_pos will skip over such rows because their end pos
22114 equals the start pos of the overlay or interval.
22116 Move back if we have a STOP object and previous row's
22117 end glyph came from STOP. */
22118 if (!NILP (stop))
22120 struct glyph_row *prev;
22121 while ((prev = row - 1, prev >= first)
22122 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22123 && prev->used[TEXT_AREA] > 0)
22125 struct glyph *beg = prev->glyphs[TEXT_AREA];
22126 glyph = beg + prev->used[TEXT_AREA];
22127 while (--glyph >= beg
22128 && INTEGERP (glyph->object));
22129 if (glyph < beg
22130 || !EQ (stop, glyph->object))
22131 break;
22132 row = prev;
22136 *x = row->x;
22137 *y = row->y;
22138 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22140 glyph = row->glyphs[TEXT_AREA];
22141 end = glyph + row->used[TEXT_AREA];
22143 /* Skip over glyphs not having an object at the start of the row.
22144 These are special glyphs like truncation marks on terminal
22145 frames. */
22146 if (row->displays_text_p)
22147 while (glyph < end
22148 && INTEGERP (glyph->object)
22149 && !EQ (stop, glyph->object)
22150 && glyph->charpos < 0)
22152 *x += glyph->pixel_width;
22153 ++glyph;
22156 while (glyph < end
22157 && !INTEGERP (glyph->object)
22158 && !EQ (stop, glyph->object)
22159 && (!BUFFERP (glyph->object)
22160 || glyph->charpos < charpos))
22162 *x += glyph->pixel_width;
22163 ++glyph;
22166 *hpos = glyph - row->glyphs[TEXT_AREA];
22167 return !past_end;
22170 #else /* not 1 */
22172 static int
22173 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22174 struct window *w;
22175 int pos;
22176 int *hpos, *vpos, *x, *y;
22177 Lisp_Object stop;
22179 int i;
22180 int lastcol;
22181 int maybe_next_line_p = 0;
22182 int line_start_position;
22183 int yb = window_text_bottom_y (w);
22184 struct glyph_row *row, *best_row;
22185 int row_vpos, best_row_vpos;
22186 int current_x;
22188 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22189 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22191 while (row->y < yb)
22193 if (row->used[TEXT_AREA])
22194 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22195 else
22196 line_start_position = 0;
22198 if (line_start_position > pos)
22199 break;
22200 /* If the position sought is the end of the buffer,
22201 don't include the blank lines at the bottom of the window. */
22202 else if (line_start_position == pos
22203 && pos == BUF_ZV (XBUFFER (w->buffer)))
22205 maybe_next_line_p = 1;
22206 break;
22208 else if (line_start_position > 0)
22210 best_row = row;
22211 best_row_vpos = row_vpos;
22214 if (row->y + row->height >= yb)
22215 break;
22217 ++row;
22218 ++row_vpos;
22221 /* Find the right column within BEST_ROW. */
22222 lastcol = 0;
22223 current_x = best_row->x;
22224 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22226 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22227 int charpos = glyph->charpos;
22229 if (BUFFERP (glyph->object))
22231 if (charpos == pos)
22233 *hpos = i;
22234 *vpos = best_row_vpos;
22235 *x = current_x;
22236 *y = best_row->y;
22237 return 1;
22239 else if (charpos > pos)
22240 break;
22242 else if (EQ (glyph->object, stop))
22243 break;
22245 if (charpos > 0)
22246 lastcol = i;
22247 current_x += glyph->pixel_width;
22250 /* If we're looking for the end of the buffer,
22251 and we didn't find it in the line we scanned,
22252 use the start of the following line. */
22253 if (maybe_next_line_p)
22255 ++best_row;
22256 ++best_row_vpos;
22257 lastcol = 0;
22258 current_x = best_row->x;
22261 *vpos = best_row_vpos;
22262 *hpos = lastcol + 1;
22263 *x = current_x;
22264 *y = best_row->y;
22265 return 0;
22268 #endif /* not 1 */
22271 /* Find the position of the glyph for position POS in OBJECT in
22272 window W's current matrix, and return in *X, *Y the pixel
22273 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22275 RIGHT_P non-zero means return the position of the right edge of the
22276 glyph, RIGHT_P zero means return the left edge position.
22278 If no glyph for POS exists in the matrix, return the position of
22279 the glyph with the next smaller position that is in the matrix, if
22280 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22281 exists in the matrix, return the position of the glyph with the
22282 next larger position in OBJECT.
22284 Value is non-zero if a glyph was found. */
22286 static int
22287 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22288 struct window *w;
22289 int pos;
22290 Lisp_Object object;
22291 int *hpos, *vpos, *x, *y;
22292 int right_p;
22294 int yb = window_text_bottom_y (w);
22295 struct glyph_row *r;
22296 struct glyph *best_glyph = NULL;
22297 struct glyph_row *best_row = NULL;
22298 int best_x = 0;
22300 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22301 r->enabled_p && r->y < yb;
22302 ++r)
22304 struct glyph *g = r->glyphs[TEXT_AREA];
22305 struct glyph *e = g + r->used[TEXT_AREA];
22306 int gx;
22308 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22309 if (EQ (g->object, object))
22311 if (g->charpos == pos)
22313 best_glyph = g;
22314 best_x = gx;
22315 best_row = r;
22316 goto found;
22318 else if (best_glyph == NULL
22319 || ((abs (g->charpos - pos)
22320 < abs (best_glyph->charpos - pos))
22321 && (right_p
22322 ? g->charpos < pos
22323 : g->charpos > pos)))
22325 best_glyph = g;
22326 best_x = gx;
22327 best_row = r;
22332 found:
22334 if (best_glyph)
22336 *x = best_x;
22337 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22339 if (right_p)
22341 *x += best_glyph->pixel_width;
22342 ++*hpos;
22345 *y = best_row->y;
22346 *vpos = best_row - w->current_matrix->rows;
22349 return best_glyph != NULL;
22353 /* See if position X, Y is within a hot-spot of an image. */
22355 static int
22356 on_hot_spot_p (hot_spot, x, y)
22357 Lisp_Object hot_spot;
22358 int x, y;
22360 if (!CONSP (hot_spot))
22361 return 0;
22363 if (EQ (XCAR (hot_spot), Qrect))
22365 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22366 Lisp_Object rect = XCDR (hot_spot);
22367 Lisp_Object tem;
22368 if (!CONSP (rect))
22369 return 0;
22370 if (!CONSP (XCAR (rect)))
22371 return 0;
22372 if (!CONSP (XCDR (rect)))
22373 return 0;
22374 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22375 return 0;
22376 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22377 return 0;
22378 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22379 return 0;
22380 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22381 return 0;
22382 return 1;
22384 else if (EQ (XCAR (hot_spot), Qcircle))
22386 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22387 Lisp_Object circ = XCDR (hot_spot);
22388 Lisp_Object lr, lx0, ly0;
22389 if (CONSP (circ)
22390 && CONSP (XCAR (circ))
22391 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22392 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22393 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22395 double r = XFLOATINT (lr);
22396 double dx = XINT (lx0) - x;
22397 double dy = XINT (ly0) - y;
22398 return (dx * dx + dy * dy <= r * r);
22401 else if (EQ (XCAR (hot_spot), Qpoly))
22403 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22404 if (VECTORP (XCDR (hot_spot)))
22406 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22407 Lisp_Object *poly = v->contents;
22408 int n = v->size;
22409 int i;
22410 int inside = 0;
22411 Lisp_Object lx, ly;
22412 int x0, y0;
22414 /* Need an even number of coordinates, and at least 3 edges. */
22415 if (n < 6 || n & 1)
22416 return 0;
22418 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22419 If count is odd, we are inside polygon. Pixels on edges
22420 may or may not be included depending on actual geometry of the
22421 polygon. */
22422 if ((lx = poly[n-2], !INTEGERP (lx))
22423 || (ly = poly[n-1], !INTEGERP (lx)))
22424 return 0;
22425 x0 = XINT (lx), y0 = XINT (ly);
22426 for (i = 0; i < n; i += 2)
22428 int x1 = x0, y1 = y0;
22429 if ((lx = poly[i], !INTEGERP (lx))
22430 || (ly = poly[i+1], !INTEGERP (ly)))
22431 return 0;
22432 x0 = XINT (lx), y0 = XINT (ly);
22434 /* Does this segment cross the X line? */
22435 if (x0 >= x)
22437 if (x1 >= x)
22438 continue;
22440 else if (x1 < x)
22441 continue;
22442 if (y > y0 && y > y1)
22443 continue;
22444 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22445 inside = !inside;
22447 return inside;
22450 /* If we don't understand the format, pretend we're not in the hot-spot. */
22451 return 0;
22454 Lisp_Object
22455 find_hot_spot (map, x, y)
22456 Lisp_Object map;
22457 int x, y;
22459 while (CONSP (map))
22461 if (CONSP (XCAR (map))
22462 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22463 return XCAR (map);
22464 map = XCDR (map);
22467 return Qnil;
22470 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22471 3, 3, 0,
22472 doc: /* Lookup in image map MAP coordinates X and Y.
22473 An image map is an alist where each element has the format (AREA ID PLIST).
22474 An AREA is specified as either a rectangle, a circle, or a polygon:
22475 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22476 pixel coordinates of the upper left and bottom right corners.
22477 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22478 and the radius of the circle; r may be a float or integer.
22479 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22480 vector describes one corner in the polygon.
22481 Returns the alist element for the first matching AREA in MAP. */)
22482 (map, x, y)
22483 Lisp_Object map;
22484 Lisp_Object x, y;
22486 if (NILP (map))
22487 return Qnil;
22489 CHECK_NUMBER (x);
22490 CHECK_NUMBER (y);
22492 return find_hot_spot (map, XINT (x), XINT (y));
22496 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22497 static void
22498 define_frame_cursor1 (f, cursor, pointer)
22499 struct frame *f;
22500 Cursor cursor;
22501 Lisp_Object pointer;
22503 /* Do not change cursor shape while dragging mouse. */
22504 if (!NILP (do_mouse_tracking))
22505 return;
22507 if (!NILP (pointer))
22509 if (EQ (pointer, Qarrow))
22510 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22511 else if (EQ (pointer, Qhand))
22512 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22513 else if (EQ (pointer, Qtext))
22514 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22515 else if (EQ (pointer, intern ("hdrag")))
22516 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22517 #ifdef HAVE_X_WINDOWS
22518 else if (EQ (pointer, intern ("vdrag")))
22519 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22520 #endif
22521 else if (EQ (pointer, intern ("hourglass")))
22522 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22523 else if (EQ (pointer, Qmodeline))
22524 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22525 else
22526 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22529 if (cursor != No_Cursor)
22530 rif->define_frame_cursor (f, cursor);
22533 /* Take proper action when mouse has moved to the mode or header line
22534 or marginal area AREA of window W, x-position X and y-position Y.
22535 X is relative to the start of the text display area of W, so the
22536 width of bitmap areas and scroll bars must be subtracted to get a
22537 position relative to the start of the mode line. */
22539 static void
22540 note_mode_line_or_margin_highlight (window, x, y, area)
22541 Lisp_Object window;
22542 int x, y;
22543 enum window_part area;
22545 struct window *w = XWINDOW (window);
22546 struct frame *f = XFRAME (w->frame);
22547 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22548 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22549 Lisp_Object pointer = Qnil;
22550 int charpos, dx, dy, width, height;
22551 Lisp_Object string, object = Qnil;
22552 Lisp_Object pos, help;
22554 Lisp_Object mouse_face;
22555 int original_x_pixel = x;
22556 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22557 struct glyph_row *row;
22559 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22561 int x0;
22562 struct glyph *end;
22564 string = mode_line_string (w, area, &x, &y, &charpos,
22565 &object, &dx, &dy, &width, &height);
22567 row = (area == ON_MODE_LINE
22568 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22569 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22571 /* Find glyph */
22572 if (row->mode_line_p && row->enabled_p)
22574 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22575 end = glyph + row->used[TEXT_AREA];
22577 for (x0 = original_x_pixel;
22578 glyph < end && x0 >= glyph->pixel_width;
22579 ++glyph)
22580 x0 -= glyph->pixel_width;
22582 if (glyph >= end)
22583 glyph = NULL;
22586 else
22588 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22589 string = marginal_area_string (w, area, &x, &y, &charpos,
22590 &object, &dx, &dy, &width, &height);
22593 help = Qnil;
22595 if (IMAGEP (object))
22597 Lisp_Object image_map, hotspot;
22598 if ((image_map = Fplist_get (XCDR (object), QCmap),
22599 !NILP (image_map))
22600 && (hotspot = find_hot_spot (image_map, dx, dy),
22601 CONSP (hotspot))
22602 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22604 Lisp_Object area_id, plist;
22606 area_id = XCAR (hotspot);
22607 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22608 If so, we could look for mouse-enter, mouse-leave
22609 properties in PLIST (and do something...). */
22610 hotspot = XCDR (hotspot);
22611 if (CONSP (hotspot)
22612 && (plist = XCAR (hotspot), CONSP (plist)))
22614 pointer = Fplist_get (plist, Qpointer);
22615 if (NILP (pointer))
22616 pointer = Qhand;
22617 help = Fplist_get (plist, Qhelp_echo);
22618 if (!NILP (help))
22620 help_echo_string = help;
22621 /* Is this correct? ++kfs */
22622 XSETWINDOW (help_echo_window, w);
22623 help_echo_object = w->buffer;
22624 help_echo_pos = charpos;
22628 if (NILP (pointer))
22629 pointer = Fplist_get (XCDR (object), QCpointer);
22632 if (STRINGP (string))
22634 pos = make_number (charpos);
22635 /* If we're on a string with `help-echo' text property, arrange
22636 for the help to be displayed. This is done by setting the
22637 global variable help_echo_string to the help string. */
22638 if (NILP (help))
22640 help = Fget_text_property (pos, Qhelp_echo, string);
22641 if (!NILP (help))
22643 help_echo_string = help;
22644 XSETWINDOW (help_echo_window, w);
22645 help_echo_object = string;
22646 help_echo_pos = charpos;
22650 if (NILP (pointer))
22651 pointer = Fget_text_property (pos, Qpointer, string);
22653 /* Change the mouse pointer according to what is under X/Y. */
22654 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22656 Lisp_Object map;
22657 map = Fget_text_property (pos, Qlocal_map, string);
22658 if (!KEYMAPP (map))
22659 map = Fget_text_property (pos, Qkeymap, string);
22660 if (!KEYMAPP (map))
22661 cursor = dpyinfo->vertical_scroll_bar_cursor;
22664 /* Change the mouse face according to what is under X/Y. */
22665 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22666 if (!NILP (mouse_face)
22667 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22668 && glyph)
22670 Lisp_Object b, e;
22672 struct glyph * tmp_glyph;
22674 int gpos;
22675 int gseq_length;
22676 int total_pixel_width;
22677 int ignore;
22679 int vpos, hpos;
22681 b = Fprevious_single_property_change (make_number (charpos + 1),
22682 Qmouse_face, string, Qnil);
22683 if (NILP (b))
22684 b = make_number (0);
22686 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22687 if (NILP (e))
22688 e = make_number (SCHARS (string));
22690 /* Calculate the position(glyph position: GPOS) of GLYPH in
22691 displayed string. GPOS is different from CHARPOS.
22693 CHARPOS is the position of glyph in internal string
22694 object. A mode line string format has structures which
22695 is converted to a flatten by emacs lisp interpreter.
22696 The internal string is an element of the structures.
22697 The displayed string is the flatten string. */
22698 gpos = 0;
22699 if (glyph > row_start_glyph)
22701 tmp_glyph = glyph - 1;
22702 while (tmp_glyph >= row_start_glyph
22703 && tmp_glyph->charpos >= XINT (b)
22704 && EQ (tmp_glyph->object, glyph->object))
22706 tmp_glyph--;
22707 gpos++;
22711 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22712 displayed string holding GLYPH.
22714 GSEQ_LENGTH is different from SCHARS (STRING).
22715 SCHARS (STRING) returns the length of the internal string. */
22716 for (tmp_glyph = glyph, gseq_length = gpos;
22717 tmp_glyph->charpos < XINT (e);
22718 tmp_glyph++, gseq_length++)
22720 if (!EQ (tmp_glyph->object, glyph->object))
22721 break;
22724 total_pixel_width = 0;
22725 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22726 total_pixel_width += tmp_glyph->pixel_width;
22728 /* Pre calculation of re-rendering position */
22729 vpos = (x - gpos);
22730 hpos = (area == ON_MODE_LINE
22731 ? (w->current_matrix)->nrows - 1
22732 : 0);
22734 /* If the re-rendering position is included in the last
22735 re-rendering area, we should do nothing. */
22736 if ( EQ (window, dpyinfo->mouse_face_window)
22737 && dpyinfo->mouse_face_beg_col <= vpos
22738 && vpos < dpyinfo->mouse_face_end_col
22739 && dpyinfo->mouse_face_beg_row == hpos )
22740 return;
22742 if (clear_mouse_face (dpyinfo))
22743 cursor = No_Cursor;
22745 dpyinfo->mouse_face_beg_col = vpos;
22746 dpyinfo->mouse_face_beg_row = hpos;
22748 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22749 dpyinfo->mouse_face_beg_y = 0;
22751 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22752 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22754 dpyinfo->mouse_face_end_x = 0;
22755 dpyinfo->mouse_face_end_y = 0;
22757 dpyinfo->mouse_face_past_end = 0;
22758 dpyinfo->mouse_face_window = window;
22760 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22761 charpos,
22762 0, 0, 0, &ignore,
22763 glyph->face_id, 1);
22764 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22766 if (NILP (pointer))
22767 pointer = Qhand;
22769 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22770 clear_mouse_face (dpyinfo);
22772 define_frame_cursor1 (f, cursor, pointer);
22776 /* EXPORT:
22777 Take proper action when the mouse has moved to position X, Y on
22778 frame F as regards highlighting characters that have mouse-face
22779 properties. Also de-highlighting chars where the mouse was before.
22780 X and Y can be negative or out of range. */
22782 void
22783 note_mouse_highlight (f, x, y)
22784 struct frame *f;
22785 int x, y;
22787 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22788 enum window_part part;
22789 Lisp_Object window;
22790 struct window *w;
22791 Cursor cursor = No_Cursor;
22792 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22793 struct buffer *b;
22795 /* When a menu is active, don't highlight because this looks odd. */
22796 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22797 if (popup_activated ())
22798 return;
22799 #endif
22801 if (NILP (Vmouse_highlight)
22802 || !f->glyphs_initialized_p)
22803 return;
22805 dpyinfo->mouse_face_mouse_x = x;
22806 dpyinfo->mouse_face_mouse_y = y;
22807 dpyinfo->mouse_face_mouse_frame = f;
22809 if (dpyinfo->mouse_face_defer)
22810 return;
22812 if (gc_in_progress)
22814 dpyinfo->mouse_face_deferred_gc = 1;
22815 return;
22818 /* Which window is that in? */
22819 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22821 /* If we were displaying active text in another window, clear that.
22822 Also clear if we move out of text area in same window. */
22823 if (! EQ (window, dpyinfo->mouse_face_window)
22824 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22825 && !NILP (dpyinfo->mouse_face_window)))
22826 clear_mouse_face (dpyinfo);
22828 /* Not on a window -> return. */
22829 if (!WINDOWP (window))
22830 return;
22832 /* Reset help_echo_string. It will get recomputed below. */
22833 help_echo_string = Qnil;
22835 /* Convert to window-relative pixel coordinates. */
22836 w = XWINDOW (window);
22837 frame_to_window_pixel_xy (w, &x, &y);
22839 /* Handle tool-bar window differently since it doesn't display a
22840 buffer. */
22841 if (EQ (window, f->tool_bar_window))
22843 note_tool_bar_highlight (f, x, y);
22844 return;
22847 /* Mouse is on the mode, header line or margin? */
22848 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22849 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22851 note_mode_line_or_margin_highlight (window, x, y, part);
22852 return;
22855 if (part == ON_VERTICAL_BORDER)
22857 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22858 help_echo_string = build_string ("drag-mouse-1: resize");
22860 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22861 || part == ON_SCROLL_BAR)
22862 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22863 else
22864 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22866 /* Are we in a window whose display is up to date?
22867 And verify the buffer's text has not changed. */
22868 b = XBUFFER (w->buffer);
22869 if (part == ON_TEXT
22870 && EQ (w->window_end_valid, w->buffer)
22871 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22872 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22874 int hpos, vpos, pos, i, dx, dy, area;
22875 struct glyph *glyph;
22876 Lisp_Object object;
22877 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22878 Lisp_Object *overlay_vec = NULL;
22879 int noverlays;
22880 struct buffer *obuf;
22881 int obegv, ozv, same_region;
22883 /* Find the glyph under X/Y. */
22884 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22886 /* Look for :pointer property on image. */
22887 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22889 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22890 if (img != NULL && IMAGEP (img->spec))
22892 Lisp_Object image_map, hotspot;
22893 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22894 !NILP (image_map))
22895 && (hotspot = find_hot_spot (image_map,
22896 glyph->slice.x + dx,
22897 glyph->slice.y + dy),
22898 CONSP (hotspot))
22899 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22901 Lisp_Object area_id, plist;
22903 area_id = XCAR (hotspot);
22904 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22905 If so, we could look for mouse-enter, mouse-leave
22906 properties in PLIST (and do something...). */
22907 hotspot = XCDR (hotspot);
22908 if (CONSP (hotspot)
22909 && (plist = XCAR (hotspot), CONSP (plist)))
22911 pointer = Fplist_get (plist, Qpointer);
22912 if (NILP (pointer))
22913 pointer = Qhand;
22914 help_echo_string = Fplist_get (plist, Qhelp_echo);
22915 if (!NILP (help_echo_string))
22917 help_echo_window = window;
22918 help_echo_object = glyph->object;
22919 help_echo_pos = glyph->charpos;
22923 if (NILP (pointer))
22924 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22928 /* Clear mouse face if X/Y not over text. */
22929 if (glyph == NULL
22930 || area != TEXT_AREA
22931 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22933 if (clear_mouse_face (dpyinfo))
22934 cursor = No_Cursor;
22935 if (NILP (pointer))
22937 if (area != TEXT_AREA)
22938 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22939 else
22940 pointer = Vvoid_text_area_pointer;
22942 goto set_cursor;
22945 pos = glyph->charpos;
22946 object = glyph->object;
22947 if (!STRINGP (object) && !BUFFERP (object))
22948 goto set_cursor;
22950 /* If we get an out-of-range value, return now; avoid an error. */
22951 if (BUFFERP (object) && pos > BUF_Z (b))
22952 goto set_cursor;
22954 /* Make the window's buffer temporarily current for
22955 overlays_at and compute_char_face. */
22956 obuf = current_buffer;
22957 current_buffer = b;
22958 obegv = BEGV;
22959 ozv = ZV;
22960 BEGV = BEG;
22961 ZV = Z;
22963 /* Is this char mouse-active or does it have help-echo? */
22964 position = make_number (pos);
22966 if (BUFFERP (object))
22968 /* Put all the overlays we want in a vector in overlay_vec. */
22969 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22970 /* Sort overlays into increasing priority order. */
22971 noverlays = sort_overlays (overlay_vec, noverlays, w);
22973 else
22974 noverlays = 0;
22976 same_region = (EQ (window, dpyinfo->mouse_face_window)
22977 && vpos >= dpyinfo->mouse_face_beg_row
22978 && vpos <= dpyinfo->mouse_face_end_row
22979 && (vpos > dpyinfo->mouse_face_beg_row
22980 || hpos >= dpyinfo->mouse_face_beg_col)
22981 && (vpos < dpyinfo->mouse_face_end_row
22982 || hpos < dpyinfo->mouse_face_end_col
22983 || dpyinfo->mouse_face_past_end));
22985 if (same_region)
22986 cursor = No_Cursor;
22988 /* Check mouse-face highlighting. */
22989 if (! same_region
22990 /* If there exists an overlay with mouse-face overlapping
22991 the one we are currently highlighting, we have to
22992 check if we enter the overlapping overlay, and then
22993 highlight only that. */
22994 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22995 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22997 /* Find the highest priority overlay that has a mouse-face
22998 property. */
22999 overlay = Qnil;
23000 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23002 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23003 if (!NILP (mouse_face))
23004 overlay = overlay_vec[i];
23007 /* If we're actually highlighting the same overlay as
23008 before, there's no need to do that again. */
23009 if (!NILP (overlay)
23010 && EQ (overlay, dpyinfo->mouse_face_overlay))
23011 goto check_help_echo;
23013 dpyinfo->mouse_face_overlay = overlay;
23015 /* Clear the display of the old active region, if any. */
23016 if (clear_mouse_face (dpyinfo))
23017 cursor = No_Cursor;
23019 /* If no overlay applies, get a text property. */
23020 if (NILP (overlay))
23021 mouse_face = Fget_text_property (position, Qmouse_face, object);
23023 /* Handle the overlay case. */
23024 if (!NILP (overlay))
23026 /* Find the range of text around this char that
23027 should be active. */
23028 Lisp_Object before, after;
23029 int ignore;
23031 before = Foverlay_start (overlay);
23032 after = Foverlay_end (overlay);
23033 /* Record this as the current active region. */
23034 fast_find_position (w, XFASTINT (before),
23035 &dpyinfo->mouse_face_beg_col,
23036 &dpyinfo->mouse_face_beg_row,
23037 &dpyinfo->mouse_face_beg_x,
23038 &dpyinfo->mouse_face_beg_y, Qnil);
23040 dpyinfo->mouse_face_past_end
23041 = !fast_find_position (w, XFASTINT (after),
23042 &dpyinfo->mouse_face_end_col,
23043 &dpyinfo->mouse_face_end_row,
23044 &dpyinfo->mouse_face_end_x,
23045 &dpyinfo->mouse_face_end_y, Qnil);
23046 dpyinfo->mouse_face_window = window;
23048 dpyinfo->mouse_face_face_id
23049 = face_at_buffer_position (w, pos, 0, 0,
23050 &ignore, pos + 1,
23051 !dpyinfo->mouse_face_hidden);
23053 /* Display it as active. */
23054 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23055 cursor = No_Cursor;
23057 /* Handle the text property case. */
23058 else if (!NILP (mouse_face) && BUFFERP (object))
23060 /* Find the range of text around this char that
23061 should be active. */
23062 Lisp_Object before, after, beginning, end;
23063 int ignore;
23065 beginning = Fmarker_position (w->start);
23066 end = make_number (BUF_Z (XBUFFER (object))
23067 - XFASTINT (w->window_end_pos));
23068 before
23069 = Fprevious_single_property_change (make_number (pos + 1),
23070 Qmouse_face,
23071 object, beginning);
23072 after
23073 = Fnext_single_property_change (position, Qmouse_face,
23074 object, end);
23076 /* Record this as the current active region. */
23077 fast_find_position (w, XFASTINT (before),
23078 &dpyinfo->mouse_face_beg_col,
23079 &dpyinfo->mouse_face_beg_row,
23080 &dpyinfo->mouse_face_beg_x,
23081 &dpyinfo->mouse_face_beg_y, Qnil);
23082 dpyinfo->mouse_face_past_end
23083 = !fast_find_position (w, XFASTINT (after),
23084 &dpyinfo->mouse_face_end_col,
23085 &dpyinfo->mouse_face_end_row,
23086 &dpyinfo->mouse_face_end_x,
23087 &dpyinfo->mouse_face_end_y, Qnil);
23088 dpyinfo->mouse_face_window = window;
23090 if (BUFFERP (object))
23091 dpyinfo->mouse_face_face_id
23092 = face_at_buffer_position (w, pos, 0, 0,
23093 &ignore, pos + 1,
23094 !dpyinfo->mouse_face_hidden);
23096 /* Display it as active. */
23097 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23098 cursor = No_Cursor;
23100 else if (!NILP (mouse_face) && STRINGP (object))
23102 Lisp_Object b, e;
23103 int ignore;
23105 b = Fprevious_single_property_change (make_number (pos + 1),
23106 Qmouse_face,
23107 object, Qnil);
23108 e = Fnext_single_property_change (position, Qmouse_face,
23109 object, Qnil);
23110 if (NILP (b))
23111 b = make_number (0);
23112 if (NILP (e))
23113 e = make_number (SCHARS (object) - 1);
23115 fast_find_string_pos (w, XINT (b), object,
23116 &dpyinfo->mouse_face_beg_col,
23117 &dpyinfo->mouse_face_beg_row,
23118 &dpyinfo->mouse_face_beg_x,
23119 &dpyinfo->mouse_face_beg_y, 0);
23120 fast_find_string_pos (w, XINT (e), object,
23121 &dpyinfo->mouse_face_end_col,
23122 &dpyinfo->mouse_face_end_row,
23123 &dpyinfo->mouse_face_end_x,
23124 &dpyinfo->mouse_face_end_y, 1);
23125 dpyinfo->mouse_face_past_end = 0;
23126 dpyinfo->mouse_face_window = window;
23127 dpyinfo->mouse_face_face_id
23128 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23129 glyph->face_id, 1);
23130 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23131 cursor = No_Cursor;
23133 else if (STRINGP (object) && NILP (mouse_face))
23135 /* A string which doesn't have mouse-face, but
23136 the text ``under'' it might have. */
23137 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23138 int start = MATRIX_ROW_START_CHARPOS (r);
23140 pos = string_buffer_position (w, object, start);
23141 if (pos > 0)
23142 mouse_face = get_char_property_and_overlay (make_number (pos),
23143 Qmouse_face,
23144 w->buffer,
23145 &overlay);
23146 if (!NILP (mouse_face) && !NILP (overlay))
23148 Lisp_Object before = Foverlay_start (overlay);
23149 Lisp_Object after = Foverlay_end (overlay);
23150 int ignore;
23152 /* Note that we might not be able to find position
23153 BEFORE in the glyph matrix if the overlay is
23154 entirely covered by a `display' property. In
23155 this case, we overshoot. So let's stop in
23156 the glyph matrix before glyphs for OBJECT. */
23157 fast_find_position (w, XFASTINT (before),
23158 &dpyinfo->mouse_face_beg_col,
23159 &dpyinfo->mouse_face_beg_row,
23160 &dpyinfo->mouse_face_beg_x,
23161 &dpyinfo->mouse_face_beg_y,
23162 object);
23164 dpyinfo->mouse_face_past_end
23165 = !fast_find_position (w, XFASTINT (after),
23166 &dpyinfo->mouse_face_end_col,
23167 &dpyinfo->mouse_face_end_row,
23168 &dpyinfo->mouse_face_end_x,
23169 &dpyinfo->mouse_face_end_y,
23170 Qnil);
23171 dpyinfo->mouse_face_window = window;
23172 dpyinfo->mouse_face_face_id
23173 = face_at_buffer_position (w, pos, 0, 0,
23174 &ignore, pos + 1,
23175 !dpyinfo->mouse_face_hidden);
23177 /* Display it as active. */
23178 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23179 cursor = No_Cursor;
23184 check_help_echo:
23186 /* Look for a `help-echo' property. */
23187 if (NILP (help_echo_string)) {
23188 Lisp_Object help, overlay;
23190 /* Check overlays first. */
23191 help = overlay = Qnil;
23192 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23194 overlay = overlay_vec[i];
23195 help = Foverlay_get (overlay, Qhelp_echo);
23198 if (!NILP (help))
23200 help_echo_string = help;
23201 help_echo_window = window;
23202 help_echo_object = overlay;
23203 help_echo_pos = pos;
23205 else
23207 Lisp_Object object = glyph->object;
23208 int charpos = glyph->charpos;
23210 /* Try text properties. */
23211 if (STRINGP (object)
23212 && charpos >= 0
23213 && charpos < SCHARS (object))
23215 help = Fget_text_property (make_number (charpos),
23216 Qhelp_echo, object);
23217 if (NILP (help))
23219 /* If the string itself doesn't specify a help-echo,
23220 see if the buffer text ``under'' it does. */
23221 struct glyph_row *r
23222 = MATRIX_ROW (w->current_matrix, vpos);
23223 int start = MATRIX_ROW_START_CHARPOS (r);
23224 int pos = string_buffer_position (w, object, start);
23225 if (pos > 0)
23227 help = Fget_char_property (make_number (pos),
23228 Qhelp_echo, w->buffer);
23229 if (!NILP (help))
23231 charpos = pos;
23232 object = w->buffer;
23237 else if (BUFFERP (object)
23238 && charpos >= BEGV
23239 && charpos < ZV)
23240 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23241 object);
23243 if (!NILP (help))
23245 help_echo_string = help;
23246 help_echo_window = window;
23247 help_echo_object = object;
23248 help_echo_pos = charpos;
23253 /* Look for a `pointer' property. */
23254 if (NILP (pointer))
23256 /* Check overlays first. */
23257 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23258 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23260 if (NILP (pointer))
23262 Lisp_Object object = glyph->object;
23263 int charpos = glyph->charpos;
23265 /* Try text properties. */
23266 if (STRINGP (object)
23267 && charpos >= 0
23268 && charpos < SCHARS (object))
23270 pointer = Fget_text_property (make_number (charpos),
23271 Qpointer, object);
23272 if (NILP (pointer))
23274 /* If the string itself doesn't specify a pointer,
23275 see if the buffer text ``under'' it does. */
23276 struct glyph_row *r
23277 = MATRIX_ROW (w->current_matrix, vpos);
23278 int start = MATRIX_ROW_START_CHARPOS (r);
23279 int pos = string_buffer_position (w, object, start);
23280 if (pos > 0)
23281 pointer = Fget_char_property (make_number (pos),
23282 Qpointer, w->buffer);
23285 else if (BUFFERP (object)
23286 && charpos >= BEGV
23287 && charpos < ZV)
23288 pointer = Fget_text_property (make_number (charpos),
23289 Qpointer, object);
23293 BEGV = obegv;
23294 ZV = ozv;
23295 current_buffer = obuf;
23298 set_cursor:
23300 define_frame_cursor1 (f, cursor, pointer);
23304 /* EXPORT for RIF:
23305 Clear any mouse-face on window W. This function is part of the
23306 redisplay interface, and is called from try_window_id and similar
23307 functions to ensure the mouse-highlight is off. */
23309 void
23310 x_clear_window_mouse_face (w)
23311 struct window *w;
23313 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23314 Lisp_Object window;
23316 BLOCK_INPUT;
23317 XSETWINDOW (window, w);
23318 if (EQ (window, dpyinfo->mouse_face_window))
23319 clear_mouse_face (dpyinfo);
23320 UNBLOCK_INPUT;
23324 /* EXPORT:
23325 Just discard the mouse face information for frame F, if any.
23326 This is used when the size of F is changed. */
23328 void
23329 cancel_mouse_face (f)
23330 struct frame *f;
23332 Lisp_Object window;
23333 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23335 window = dpyinfo->mouse_face_window;
23336 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23338 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23339 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23340 dpyinfo->mouse_face_window = Qnil;
23345 #endif /* HAVE_WINDOW_SYSTEM */
23348 /***********************************************************************
23349 Exposure Events
23350 ***********************************************************************/
23352 #ifdef HAVE_WINDOW_SYSTEM
23354 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23355 which intersects rectangle R. R is in window-relative coordinates. */
23357 static void
23358 expose_area (w, row, r, area)
23359 struct window *w;
23360 struct glyph_row *row;
23361 XRectangle *r;
23362 enum glyph_row_area area;
23364 struct glyph *first = row->glyphs[area];
23365 struct glyph *end = row->glyphs[area] + row->used[area];
23366 struct glyph *last;
23367 int first_x, start_x, x;
23369 if (area == TEXT_AREA && row->fill_line_p)
23370 /* If row extends face to end of line write the whole line. */
23371 draw_glyphs (w, 0, row, area,
23372 0, row->used[area],
23373 DRAW_NORMAL_TEXT, 0);
23374 else
23376 /* Set START_X to the window-relative start position for drawing glyphs of
23377 AREA. The first glyph of the text area can be partially visible.
23378 The first glyphs of other areas cannot. */
23379 start_x = window_box_left_offset (w, area);
23380 x = start_x;
23381 if (area == TEXT_AREA)
23382 x += row->x;
23384 /* Find the first glyph that must be redrawn. */
23385 while (first < end
23386 && x + first->pixel_width < r->x)
23388 x += first->pixel_width;
23389 ++first;
23392 /* Find the last one. */
23393 last = first;
23394 first_x = x;
23395 while (last < end
23396 && x < r->x + r->width)
23398 x += last->pixel_width;
23399 ++last;
23402 /* Repaint. */
23403 if (last > first)
23404 draw_glyphs (w, first_x - start_x, row, area,
23405 first - row->glyphs[area], last - row->glyphs[area],
23406 DRAW_NORMAL_TEXT, 0);
23411 /* Redraw the parts of the glyph row ROW on window W intersecting
23412 rectangle R. R is in window-relative coordinates. Value is
23413 non-zero if mouse-face was overwritten. */
23415 static int
23416 expose_line (w, row, r)
23417 struct window *w;
23418 struct glyph_row *row;
23419 XRectangle *r;
23421 xassert (row->enabled_p);
23423 if (row->mode_line_p || w->pseudo_window_p)
23424 draw_glyphs (w, 0, row, TEXT_AREA,
23425 0, row->used[TEXT_AREA],
23426 DRAW_NORMAL_TEXT, 0);
23427 else
23429 if (row->used[LEFT_MARGIN_AREA])
23430 expose_area (w, row, r, LEFT_MARGIN_AREA);
23431 if (row->used[TEXT_AREA])
23432 expose_area (w, row, r, TEXT_AREA);
23433 if (row->used[RIGHT_MARGIN_AREA])
23434 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23435 draw_row_fringe_bitmaps (w, row);
23438 return row->mouse_face_p;
23442 /* Redraw those parts of glyphs rows during expose event handling that
23443 overlap other rows. Redrawing of an exposed line writes over parts
23444 of lines overlapping that exposed line; this function fixes that.
23446 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23447 row in W's current matrix that is exposed and overlaps other rows.
23448 LAST_OVERLAPPING_ROW is the last such row. */
23450 static void
23451 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23452 struct window *w;
23453 struct glyph_row *first_overlapping_row;
23454 struct glyph_row *last_overlapping_row;
23456 struct glyph_row *row;
23458 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23459 if (row->overlapping_p)
23461 xassert (row->enabled_p && !row->mode_line_p);
23463 if (row->used[LEFT_MARGIN_AREA])
23464 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23466 if (row->used[TEXT_AREA])
23467 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23469 if (row->used[RIGHT_MARGIN_AREA])
23470 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23475 /* Return non-zero if W's cursor intersects rectangle R. */
23477 static int
23478 phys_cursor_in_rect_p (w, r)
23479 struct window *w;
23480 XRectangle *r;
23482 XRectangle cr, result;
23483 struct glyph *cursor_glyph;
23484 struct glyph_row *row;
23486 if (w->phys_cursor.vpos >= 0
23487 && w->phys_cursor.vpos < w->current_matrix->nrows
23488 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23489 row->enabled_p)
23490 && row->cursor_in_fringe_p)
23492 /* Cursor is in the fringe. */
23493 cr.x = window_box_right_offset (w,
23494 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23495 ? RIGHT_MARGIN_AREA
23496 : TEXT_AREA));
23497 cr.y = row->y;
23498 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23499 cr.height = row->height;
23500 return x_intersect_rectangles (&cr, r, &result);
23503 cursor_glyph = get_phys_cursor_glyph (w);
23504 if (cursor_glyph)
23506 /* r is relative to W's box, but w->phys_cursor.x is relative
23507 to left edge of W's TEXT area. Adjust it. */
23508 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23509 cr.y = w->phys_cursor.y;
23510 cr.width = cursor_glyph->pixel_width;
23511 cr.height = w->phys_cursor_height;
23512 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23513 I assume the effect is the same -- and this is portable. */
23514 return x_intersect_rectangles (&cr, r, &result);
23516 else
23517 return 0;
23521 /* EXPORT:
23522 Draw a vertical window border to the right of window W if W doesn't
23523 have vertical scroll bars. */
23525 void
23526 x_draw_vertical_border (w)
23527 struct window *w;
23529 /* We could do better, if we knew what type of scroll-bar the adjacent
23530 windows (on either side) have... But we don't :-(
23531 However, I think this works ok. ++KFS 2003-04-25 */
23533 /* Redraw borders between horizontally adjacent windows. Don't
23534 do it for frames with vertical scroll bars because either the
23535 right scroll bar of a window, or the left scroll bar of its
23536 neighbor will suffice as a border. */
23537 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23538 return;
23540 if (!WINDOW_RIGHTMOST_P (w)
23541 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23543 int x0, x1, y0, y1;
23545 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23546 y1 -= 1;
23548 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23549 x1 -= 1;
23551 rif->draw_vertical_window_border (w, x1, y0, y1);
23553 else if (!WINDOW_LEFTMOST_P (w)
23554 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23556 int x0, x1, y0, y1;
23558 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23559 y1 -= 1;
23561 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23562 x0 -= 1;
23564 rif->draw_vertical_window_border (w, x0, y0, y1);
23569 /* Redraw the part of window W intersection rectangle FR. Pixel
23570 coordinates in FR are frame-relative. Call this function with
23571 input blocked. Value is non-zero if the exposure overwrites
23572 mouse-face. */
23574 static int
23575 expose_window (w, fr)
23576 struct window *w;
23577 XRectangle *fr;
23579 struct frame *f = XFRAME (w->frame);
23580 XRectangle wr, r;
23581 int mouse_face_overwritten_p = 0;
23583 /* If window is not yet fully initialized, do nothing. This can
23584 happen when toolkit scroll bars are used and a window is split.
23585 Reconfiguring the scroll bar will generate an expose for a newly
23586 created window. */
23587 if (w->current_matrix == NULL)
23588 return 0;
23590 /* When we're currently updating the window, display and current
23591 matrix usually don't agree. Arrange for a thorough display
23592 later. */
23593 if (w == updated_window)
23595 SET_FRAME_GARBAGED (f);
23596 return 0;
23599 /* Frame-relative pixel rectangle of W. */
23600 wr.x = WINDOW_LEFT_EDGE_X (w);
23601 wr.y = WINDOW_TOP_EDGE_Y (w);
23602 wr.width = WINDOW_TOTAL_WIDTH (w);
23603 wr.height = WINDOW_TOTAL_HEIGHT (w);
23605 if (x_intersect_rectangles (fr, &wr, &r))
23607 int yb = window_text_bottom_y (w);
23608 struct glyph_row *row;
23609 int cursor_cleared_p;
23610 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23612 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23613 r.x, r.y, r.width, r.height));
23615 /* Convert to window coordinates. */
23616 r.x -= WINDOW_LEFT_EDGE_X (w);
23617 r.y -= WINDOW_TOP_EDGE_Y (w);
23619 /* Turn off the cursor. */
23620 if (!w->pseudo_window_p
23621 && phys_cursor_in_rect_p (w, &r))
23623 x_clear_cursor (w);
23624 cursor_cleared_p = 1;
23626 else
23627 cursor_cleared_p = 0;
23629 /* Update lines intersecting rectangle R. */
23630 first_overlapping_row = last_overlapping_row = NULL;
23631 for (row = w->current_matrix->rows;
23632 row->enabled_p;
23633 ++row)
23635 int y0 = row->y;
23636 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23638 if ((y0 >= r.y && y0 < r.y + r.height)
23639 || (y1 > r.y && y1 < r.y + r.height)
23640 || (r.y >= y0 && r.y < y1)
23641 || (r.y + r.height > y0 && r.y + r.height < y1))
23643 /* A header line may be overlapping, but there is no need
23644 to fix overlapping areas for them. KFS 2005-02-12 */
23645 if (row->overlapping_p && !row->mode_line_p)
23647 if (first_overlapping_row == NULL)
23648 first_overlapping_row = row;
23649 last_overlapping_row = row;
23652 if (expose_line (w, row, &r))
23653 mouse_face_overwritten_p = 1;
23656 if (y1 >= yb)
23657 break;
23660 /* Display the mode line if there is one. */
23661 if (WINDOW_WANTS_MODELINE_P (w)
23662 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23663 row->enabled_p)
23664 && row->y < r.y + r.height)
23666 if (expose_line (w, row, &r))
23667 mouse_face_overwritten_p = 1;
23670 if (!w->pseudo_window_p)
23672 /* Fix the display of overlapping rows. */
23673 if (first_overlapping_row)
23674 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23676 /* Draw border between windows. */
23677 x_draw_vertical_border (w);
23679 /* Turn the cursor on again. */
23680 if (cursor_cleared_p)
23681 update_window_cursor (w, 1);
23685 return mouse_face_overwritten_p;
23690 /* Redraw (parts) of all windows in the window tree rooted at W that
23691 intersect R. R contains frame pixel coordinates. Value is
23692 non-zero if the exposure overwrites mouse-face. */
23694 static int
23695 expose_window_tree (w, r)
23696 struct window *w;
23697 XRectangle *r;
23699 struct frame *f = XFRAME (w->frame);
23700 int mouse_face_overwritten_p = 0;
23702 while (w && !FRAME_GARBAGED_P (f))
23704 if (!NILP (w->hchild))
23705 mouse_face_overwritten_p
23706 |= expose_window_tree (XWINDOW (w->hchild), r);
23707 else if (!NILP (w->vchild))
23708 mouse_face_overwritten_p
23709 |= expose_window_tree (XWINDOW (w->vchild), r);
23710 else
23711 mouse_face_overwritten_p |= expose_window (w, r);
23713 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23716 return mouse_face_overwritten_p;
23720 /* EXPORT:
23721 Redisplay an exposed area of frame F. X and Y are the upper-left
23722 corner of the exposed rectangle. W and H are width and height of
23723 the exposed area. All are pixel values. W or H zero means redraw
23724 the entire frame. */
23726 void
23727 expose_frame (f, x, y, w, h)
23728 struct frame *f;
23729 int x, y, w, h;
23731 XRectangle r;
23732 int mouse_face_overwritten_p = 0;
23734 TRACE ((stderr, "expose_frame "));
23736 /* No need to redraw if frame will be redrawn soon. */
23737 if (FRAME_GARBAGED_P (f))
23739 TRACE ((stderr, " garbaged\n"));
23740 return;
23743 /* If basic faces haven't been realized yet, there is no point in
23744 trying to redraw anything. This can happen when we get an expose
23745 event while Emacs is starting, e.g. by moving another window. */
23746 if (FRAME_FACE_CACHE (f) == NULL
23747 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23749 TRACE ((stderr, " no faces\n"));
23750 return;
23753 if (w == 0 || h == 0)
23755 r.x = r.y = 0;
23756 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23757 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23759 else
23761 r.x = x;
23762 r.y = y;
23763 r.width = w;
23764 r.height = h;
23767 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23768 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23770 if (WINDOWP (f->tool_bar_window))
23771 mouse_face_overwritten_p
23772 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23774 #ifdef HAVE_X_WINDOWS
23775 #ifndef MSDOS
23776 #ifndef USE_X_TOOLKIT
23777 if (WINDOWP (f->menu_bar_window))
23778 mouse_face_overwritten_p
23779 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23780 #endif /* not USE_X_TOOLKIT */
23781 #endif
23782 #endif
23784 /* Some window managers support a focus-follows-mouse style with
23785 delayed raising of frames. Imagine a partially obscured frame,
23786 and moving the mouse into partially obscured mouse-face on that
23787 frame. The visible part of the mouse-face will be highlighted,
23788 then the WM raises the obscured frame. With at least one WM, KDE
23789 2.1, Emacs is not getting any event for the raising of the frame
23790 (even tried with SubstructureRedirectMask), only Expose events.
23791 These expose events will draw text normally, i.e. not
23792 highlighted. Which means we must redo the highlight here.
23793 Subsume it under ``we love X''. --gerd 2001-08-15 */
23794 /* Included in Windows version because Windows most likely does not
23795 do the right thing if any third party tool offers
23796 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23797 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23799 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23800 if (f == dpyinfo->mouse_face_mouse_frame)
23802 int x = dpyinfo->mouse_face_mouse_x;
23803 int y = dpyinfo->mouse_face_mouse_y;
23804 clear_mouse_face (dpyinfo);
23805 note_mouse_highlight (f, x, y);
23811 /* EXPORT:
23812 Determine the intersection of two rectangles R1 and R2. Return
23813 the intersection in *RESULT. Value is non-zero if RESULT is not
23814 empty. */
23817 x_intersect_rectangles (r1, r2, result)
23818 XRectangle *r1, *r2, *result;
23820 XRectangle *left, *right;
23821 XRectangle *upper, *lower;
23822 int intersection_p = 0;
23824 /* Rearrange so that R1 is the left-most rectangle. */
23825 if (r1->x < r2->x)
23826 left = r1, right = r2;
23827 else
23828 left = r2, right = r1;
23830 /* X0 of the intersection is right.x0, if this is inside R1,
23831 otherwise there is no intersection. */
23832 if (right->x <= left->x + left->width)
23834 result->x = right->x;
23836 /* The right end of the intersection is the minimum of the
23837 the right ends of left and right. */
23838 result->width = (min (left->x + left->width, right->x + right->width)
23839 - result->x);
23841 /* Same game for Y. */
23842 if (r1->y < r2->y)
23843 upper = r1, lower = r2;
23844 else
23845 upper = r2, lower = r1;
23847 /* The upper end of the intersection is lower.y0, if this is inside
23848 of upper. Otherwise, there is no intersection. */
23849 if (lower->y <= upper->y + upper->height)
23851 result->y = lower->y;
23853 /* The lower end of the intersection is the minimum of the lower
23854 ends of upper and lower. */
23855 result->height = (min (lower->y + lower->height,
23856 upper->y + upper->height)
23857 - result->y);
23858 intersection_p = 1;
23862 return intersection_p;
23865 #endif /* HAVE_WINDOW_SYSTEM */
23868 /***********************************************************************
23869 Initialization
23870 ***********************************************************************/
23872 void
23873 syms_of_xdisp ()
23875 Vwith_echo_area_save_vector = Qnil;
23876 staticpro (&Vwith_echo_area_save_vector);
23878 Vmessage_stack = Qnil;
23879 staticpro (&Vmessage_stack);
23881 Qinhibit_redisplay = intern ("inhibit-redisplay");
23882 staticpro (&Qinhibit_redisplay);
23884 message_dolog_marker1 = Fmake_marker ();
23885 staticpro (&message_dolog_marker1);
23886 message_dolog_marker2 = Fmake_marker ();
23887 staticpro (&message_dolog_marker2);
23888 message_dolog_marker3 = Fmake_marker ();
23889 staticpro (&message_dolog_marker3);
23891 #if GLYPH_DEBUG
23892 defsubr (&Sdump_frame_glyph_matrix);
23893 defsubr (&Sdump_glyph_matrix);
23894 defsubr (&Sdump_glyph_row);
23895 defsubr (&Sdump_tool_bar_row);
23896 defsubr (&Strace_redisplay);
23897 defsubr (&Strace_to_stderr);
23898 #endif
23899 #ifdef HAVE_WINDOW_SYSTEM
23900 defsubr (&Stool_bar_lines_needed);
23901 defsubr (&Slookup_image_map);
23902 #endif
23903 defsubr (&Sformat_mode_line);
23905 staticpro (&Qmenu_bar_update_hook);
23906 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23908 staticpro (&Qoverriding_terminal_local_map);
23909 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23911 staticpro (&Qoverriding_local_map);
23912 Qoverriding_local_map = intern ("overriding-local-map");
23914 staticpro (&Qwindow_scroll_functions);
23915 Qwindow_scroll_functions = intern ("window-scroll-functions");
23917 staticpro (&Qredisplay_end_trigger_functions);
23918 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23920 staticpro (&Qinhibit_point_motion_hooks);
23921 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23923 QCdata = intern (":data");
23924 staticpro (&QCdata);
23925 Qdisplay = intern ("display");
23926 staticpro (&Qdisplay);
23927 Qspace_width = intern ("space-width");
23928 staticpro (&Qspace_width);
23929 Qraise = intern ("raise");
23930 staticpro (&Qraise);
23931 Qslice = intern ("slice");
23932 staticpro (&Qslice);
23933 Qspace = intern ("space");
23934 staticpro (&Qspace);
23935 Qmargin = intern ("margin");
23936 staticpro (&Qmargin);
23937 Qpointer = intern ("pointer");
23938 staticpro (&Qpointer);
23939 Qleft_margin = intern ("left-margin");
23940 staticpro (&Qleft_margin);
23941 Qright_margin = intern ("right-margin");
23942 staticpro (&Qright_margin);
23943 Qcenter = intern ("center");
23944 staticpro (&Qcenter);
23945 Qline_height = intern ("line-height");
23946 staticpro (&Qline_height);
23947 QCalign_to = intern (":align-to");
23948 staticpro (&QCalign_to);
23949 QCrelative_width = intern (":relative-width");
23950 staticpro (&QCrelative_width);
23951 QCrelative_height = intern (":relative-height");
23952 staticpro (&QCrelative_height);
23953 QCeval = intern (":eval");
23954 staticpro (&QCeval);
23955 QCpropertize = intern (":propertize");
23956 staticpro (&QCpropertize);
23957 QCfile = intern (":file");
23958 staticpro (&QCfile);
23959 Qfontified = intern ("fontified");
23960 staticpro (&Qfontified);
23961 Qfontification_functions = intern ("fontification-functions");
23962 staticpro (&Qfontification_functions);
23963 Qtrailing_whitespace = intern ("trailing-whitespace");
23964 staticpro (&Qtrailing_whitespace);
23965 Qescape_glyph = intern ("escape-glyph");
23966 staticpro (&Qescape_glyph);
23967 Qnobreak_space = intern ("nobreak-space");
23968 staticpro (&Qnobreak_space);
23969 Qimage = intern ("image");
23970 staticpro (&Qimage);
23971 QCmap = intern (":map");
23972 staticpro (&QCmap);
23973 QCpointer = intern (":pointer");
23974 staticpro (&QCpointer);
23975 Qrect = intern ("rect");
23976 staticpro (&Qrect);
23977 Qcircle = intern ("circle");
23978 staticpro (&Qcircle);
23979 Qpoly = intern ("poly");
23980 staticpro (&Qpoly);
23981 Qmessage_truncate_lines = intern ("message-truncate-lines");
23982 staticpro (&Qmessage_truncate_lines);
23983 Qgrow_only = intern ("grow-only");
23984 staticpro (&Qgrow_only);
23985 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23986 staticpro (&Qinhibit_menubar_update);
23987 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23988 staticpro (&Qinhibit_eval_during_redisplay);
23989 Qposition = intern ("position");
23990 staticpro (&Qposition);
23991 Qbuffer_position = intern ("buffer-position");
23992 staticpro (&Qbuffer_position);
23993 Qobject = intern ("object");
23994 staticpro (&Qobject);
23995 Qbar = intern ("bar");
23996 staticpro (&Qbar);
23997 Qhbar = intern ("hbar");
23998 staticpro (&Qhbar);
23999 Qbox = intern ("box");
24000 staticpro (&Qbox);
24001 Qhollow = intern ("hollow");
24002 staticpro (&Qhollow);
24003 Qhand = intern ("hand");
24004 staticpro (&Qhand);
24005 Qarrow = intern ("arrow");
24006 staticpro (&Qarrow);
24007 Qtext = intern ("text");
24008 staticpro (&Qtext);
24009 Qrisky_local_variable = intern ("risky-local-variable");
24010 staticpro (&Qrisky_local_variable);
24011 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24012 staticpro (&Qinhibit_free_realized_faces);
24014 list_of_error = Fcons (Fcons (intern ("error"),
24015 Fcons (intern ("void-variable"), Qnil)),
24016 Qnil);
24017 staticpro (&list_of_error);
24019 Qlast_arrow_position = intern ("last-arrow-position");
24020 staticpro (&Qlast_arrow_position);
24021 Qlast_arrow_string = intern ("last-arrow-string");
24022 staticpro (&Qlast_arrow_string);
24024 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24025 staticpro (&Qoverlay_arrow_string);
24026 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24027 staticpro (&Qoverlay_arrow_bitmap);
24029 echo_buffer[0] = echo_buffer[1] = Qnil;
24030 staticpro (&echo_buffer[0]);
24031 staticpro (&echo_buffer[1]);
24033 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24034 staticpro (&echo_area_buffer[0]);
24035 staticpro (&echo_area_buffer[1]);
24037 Vmessages_buffer_name = build_string ("*Messages*");
24038 staticpro (&Vmessages_buffer_name);
24040 mode_line_proptrans_alist = Qnil;
24041 staticpro (&mode_line_proptrans_alist);
24042 mode_line_string_list = Qnil;
24043 staticpro (&mode_line_string_list);
24044 mode_line_string_face = Qnil;
24045 staticpro (&mode_line_string_face);
24046 mode_line_string_face_prop = Qnil;
24047 staticpro (&mode_line_string_face_prop);
24048 Vmode_line_unwind_vector = Qnil;
24049 staticpro (&Vmode_line_unwind_vector);
24051 help_echo_string = Qnil;
24052 staticpro (&help_echo_string);
24053 help_echo_object = Qnil;
24054 staticpro (&help_echo_object);
24055 help_echo_window = Qnil;
24056 staticpro (&help_echo_window);
24057 previous_help_echo_string = Qnil;
24058 staticpro (&previous_help_echo_string);
24059 help_echo_pos = -1;
24061 #ifdef HAVE_WINDOW_SYSTEM
24062 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24063 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24064 For example, if a block cursor is over a tab, it will be drawn as
24065 wide as that tab on the display. */);
24066 x_stretch_cursor_p = 0;
24067 #endif
24069 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24070 doc: /* *Non-nil means highlight trailing whitespace.
24071 The face used for trailing whitespace is `trailing-whitespace'. */);
24072 Vshow_trailing_whitespace = Qnil;
24074 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24075 doc: /* *Control highlighting of nobreak space and soft hyphen.
24076 A value of t means highlight the character itself (for nobreak space,
24077 use face `nobreak-space').
24078 A value of nil means no highlighting.
24079 Other values mean display the escape glyph followed by an ordinary
24080 space or ordinary hyphen. */);
24081 Vnobreak_char_display = Qt;
24083 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24084 doc: /* *The pointer shape to show in void text areas.
24085 A value of nil means to show the text pointer. Other options are `arrow',
24086 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24087 Vvoid_text_area_pointer = Qarrow;
24089 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24090 doc: /* Non-nil means don't actually do any redisplay.
24091 This is used for internal purposes. */);
24092 Vinhibit_redisplay = Qnil;
24094 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24095 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24096 Vglobal_mode_string = Qnil;
24098 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24099 doc: /* Marker for where to display an arrow on top of the buffer text.
24100 This must be the beginning of a line in order to work.
24101 See also `overlay-arrow-string'. */);
24102 Voverlay_arrow_position = Qnil;
24104 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24105 doc: /* String to display as an arrow in non-window frames.
24106 See also `overlay-arrow-position'. */);
24107 Voverlay_arrow_string = build_string ("=>");
24109 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24110 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24111 The symbols on this list are examined during redisplay to determine
24112 where to display overlay arrows. */);
24113 Voverlay_arrow_variable_list
24114 = Fcons (intern ("overlay-arrow-position"), Qnil);
24116 DEFVAR_INT ("scroll-step", &scroll_step,
24117 doc: /* *The number of lines to try scrolling a window by when point moves out.
24118 If that fails to bring point back on frame, point is centered instead.
24119 If this is zero, point is always centered after it moves off frame.
24120 If you want scrolling to always be a line at a time, you should set
24121 `scroll-conservatively' to a large value rather than set this to 1. */);
24123 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24124 doc: /* *Scroll up to this many lines, to bring point back on screen.
24125 If point moves off-screen, redisplay will scroll by up to
24126 `scroll-conservatively' lines in order to bring point just barely
24127 onto the screen again. If that cannot be done, then redisplay
24128 recenters point as usual.
24130 A value of zero means always recenter point if it moves off screen. */);
24131 scroll_conservatively = 0;
24133 DEFVAR_INT ("scroll-margin", &scroll_margin,
24134 doc: /* *Number of lines of margin at the top and bottom of a window.
24135 Recenter the window whenever point gets within this many lines
24136 of the top or bottom of the window. */);
24137 scroll_margin = 0;
24139 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24140 doc: /* Pixels per inch value for non-window system displays.
24141 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24142 Vdisplay_pixels_per_inch = make_float (72.0);
24144 #if GLYPH_DEBUG
24145 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24146 #endif
24148 DEFVAR_BOOL ("truncate-partial-width-windows",
24149 &truncate_partial_width_windows,
24150 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24151 truncate_partial_width_windows = 1;
24153 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24154 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24155 Any other value means to use the appropriate face, `mode-line',
24156 `header-line', or `menu' respectively. */);
24157 mode_line_inverse_video = 1;
24159 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24160 doc: /* *Maximum buffer size for which line number should be displayed.
24161 If the buffer is bigger than this, the line number does not appear
24162 in the mode line. A value of nil means no limit. */);
24163 Vline_number_display_limit = Qnil;
24165 DEFVAR_INT ("line-number-display-limit-width",
24166 &line_number_display_limit_width,
24167 doc: /* *Maximum line width (in characters) for line number display.
24168 If the average length of the lines near point is bigger than this, then the
24169 line number may be omitted from the mode line. */);
24170 line_number_display_limit_width = 200;
24172 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24173 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24174 highlight_nonselected_windows = 0;
24176 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24177 doc: /* Non-nil if more than one frame is visible on this display.
24178 Minibuffer-only frames don't count, but iconified frames do.
24179 This variable is not guaranteed to be accurate except while processing
24180 `frame-title-format' and `icon-title-format'. */);
24182 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24183 doc: /* Template for displaying the title bar of visible frames.
24184 \(Assuming the window manager supports this feature.)
24186 This variable has the same structure as `mode-line-format', except that
24187 the %c and %l constructs are ignored. It is used only on frames for
24188 which no explicit name has been set \(see `modify-frame-parameters'). */);
24190 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24191 doc: /* Template for displaying the title bar of an iconified frame.
24192 \(Assuming the window manager supports this feature.)
24193 This variable has the same structure as `mode-line-format' (which see),
24194 and is used only on frames for which no explicit name has been set
24195 \(see `modify-frame-parameters'). */);
24196 Vicon_title_format
24197 = Vframe_title_format
24198 = Fcons (intern ("multiple-frames"),
24199 Fcons (build_string ("%b"),
24200 Fcons (Fcons (empty_string,
24201 Fcons (intern ("invocation-name"),
24202 Fcons (build_string ("@"),
24203 Fcons (intern ("system-name"),
24204 Qnil)))),
24205 Qnil)));
24207 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24208 doc: /* Maximum number of lines to keep in the message log buffer.
24209 If nil, disable message logging. If t, log messages but don't truncate
24210 the buffer when it becomes large. */);
24211 Vmessage_log_max = make_number (100);
24213 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24214 doc: /* Functions called before redisplay, if window sizes have changed.
24215 The value should be a list of functions that take one argument.
24216 Just before redisplay, for each frame, if any of its windows have changed
24217 size since the last redisplay, or have been split or deleted,
24218 all the functions in the list are called, with the frame as argument. */);
24219 Vwindow_size_change_functions = Qnil;
24221 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24222 doc: /* List of functions to call before redisplaying a window with scrolling.
24223 Each function is called with two arguments, the window
24224 and its new display-start position. Note that the value of `window-end'
24225 is not valid when these functions are called. */);
24226 Vwindow_scroll_functions = Qnil;
24228 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24229 doc: /* Functions called when redisplay of a window reaches the end trigger.
24230 Each function is called with two arguments, the window and the end trigger value.
24231 See `set-window-redisplay-end-trigger'. */);
24232 Vredisplay_end_trigger_functions = Qnil;
24234 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24235 doc: /* *Non-nil means autoselect window with mouse pointer.
24236 If nil, do not autoselect windows.
24237 A positive number means delay autoselection by that many seconds: a
24238 window is autoselected only after the mouse has remained in that
24239 window for the duration of the delay.
24240 A negative number has a similar effect, but causes windows to be
24241 autoselected only after the mouse has stopped moving. \(Because of
24242 the way Emacs compares mouse events, you will occasionally wait twice
24243 that time before the window gets selected.\)
24244 Any other value means to autoselect window instantaneously when the
24245 mouse pointer enters it.
24247 Autoselection selects the minibuffer only if it is active, and never
24248 unselects the minibuffer if it is active.
24250 When customizing this variable make sure that the actual value of
24251 `focus-follows-mouse' matches the behavior of your window manager. */);
24252 Vmouse_autoselect_window = Qnil;
24254 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24255 doc: /* *Non-nil means automatically resize tool-bars.
24256 This dynamically changes the tool-bar's height to the minimum height
24257 that is needed to make all tool-bar items visible.
24258 If value is `grow-only', the tool-bar's height is only increased
24259 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24260 Vauto_resize_tool_bars = Qt;
24262 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24263 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24264 auto_raise_tool_bar_buttons_p = 1;
24266 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24267 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24268 make_cursor_line_fully_visible_p = 1;
24270 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24271 doc: /* *Border below tool-bar in pixels.
24272 If an integer, use it as the height of the border.
24273 If it is one of `internal-border-width' or `border-width', use the
24274 value of the corresponding frame parameter.
24275 Otherwise, no border is added below the tool-bar. */);
24276 Vtool_bar_border = Qinternal_border_width;
24278 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24279 doc: /* *Margin around tool-bar buttons in pixels.
24280 If an integer, use that for both horizontal and vertical margins.
24281 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24282 HORZ specifying the horizontal margin, and VERT specifying the
24283 vertical margin. */);
24284 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24286 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24287 doc: /* *Relief thickness of tool-bar buttons. */);
24288 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24290 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24291 doc: /* List of functions to call to fontify regions of text.
24292 Each function is called with one argument POS. Functions must
24293 fontify a region starting at POS in the current buffer, and give
24294 fontified regions the property `fontified'. */);
24295 Vfontification_functions = Qnil;
24296 Fmake_variable_buffer_local (Qfontification_functions);
24298 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24299 &unibyte_display_via_language_environment,
24300 doc: /* *Non-nil means display unibyte text according to language environment.
24301 Specifically this means that unibyte non-ASCII characters
24302 are displayed by converting them to the equivalent multibyte characters
24303 according to the current language environment. As a result, they are
24304 displayed according to the current fontset. */);
24305 unibyte_display_via_language_environment = 0;
24307 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24308 doc: /* *Maximum height for resizing mini-windows.
24309 If a float, it specifies a fraction of the mini-window frame's height.
24310 If an integer, it specifies a number of lines. */);
24311 Vmax_mini_window_height = make_float (0.25);
24313 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24314 doc: /* *How to resize mini-windows.
24315 A value of nil means don't automatically resize mini-windows.
24316 A value of t means resize them to fit the text displayed in them.
24317 A value of `grow-only', the default, means let mini-windows grow
24318 only, until their display becomes empty, at which point the windows
24319 go back to their normal size. */);
24320 Vresize_mini_windows = Qgrow_only;
24322 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24323 doc: /* Alist specifying how to blink the cursor off.
24324 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24325 `cursor-type' frame-parameter or variable equals ON-STATE,
24326 comparing using `equal', Emacs uses OFF-STATE to specify
24327 how to blink it off. ON-STATE and OFF-STATE are values for
24328 the `cursor-type' frame parameter.
24330 If a frame's ON-STATE has no entry in this list,
24331 the frame's other specifications determine how to blink the cursor off. */);
24332 Vblink_cursor_alist = Qnil;
24334 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24335 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24336 automatic_hscrolling_p = 1;
24338 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24339 doc: /* *How many columns away from the window edge point is allowed to get
24340 before automatic hscrolling will horizontally scroll the window. */);
24341 hscroll_margin = 5;
24343 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24344 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24345 When point is less than `hscroll-margin' columns from the window
24346 edge, automatic hscrolling will scroll the window by the amount of columns
24347 determined by this variable. If its value is a positive integer, scroll that
24348 many columns. If it's a positive floating-point number, it specifies the
24349 fraction of the window's width to scroll. If it's nil or zero, point will be
24350 centered horizontally after the scroll. Any other value, including negative
24351 numbers, are treated as if the value were zero.
24353 Automatic hscrolling always moves point outside the scroll margin, so if
24354 point was more than scroll step columns inside the margin, the window will
24355 scroll more than the value given by the scroll step.
24357 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24358 and `scroll-right' overrides this variable's effect. */);
24359 Vhscroll_step = make_number (0);
24361 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24362 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24363 Bind this around calls to `message' to let it take effect. */);
24364 message_truncate_lines = 0;
24366 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24367 doc: /* Normal hook run to update the menu bar definitions.
24368 Redisplay runs this hook before it redisplays the menu bar.
24369 This is used to update submenus such as Buffers,
24370 whose contents depend on various data. */);
24371 Vmenu_bar_update_hook = Qnil;
24373 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24374 doc: /* Frame for which we are updating a menu.
24375 The enable predicate for a menu binding should check this variable. */);
24376 Vmenu_updating_frame = Qnil;
24378 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24379 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24380 inhibit_menubar_update = 0;
24382 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24383 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24384 inhibit_eval_during_redisplay = 0;
24386 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24387 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24388 inhibit_free_realized_faces = 0;
24390 #if GLYPH_DEBUG
24391 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24392 doc: /* Inhibit try_window_id display optimization. */);
24393 inhibit_try_window_id = 0;
24395 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24396 doc: /* Inhibit try_window_reusing display optimization. */);
24397 inhibit_try_window_reusing = 0;
24399 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24400 doc: /* Inhibit try_cursor_movement display optimization. */);
24401 inhibit_try_cursor_movement = 0;
24402 #endif /* GLYPH_DEBUG */
24404 DEFVAR_INT ("overline-margin", &overline_margin,
24405 doc: /* *Space between overline and text, in pixels.
24406 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24407 margin to the caracter height. */);
24408 overline_margin = 2;
24412 /* Initialize this module when Emacs starts. */
24414 void
24415 init_xdisp ()
24417 Lisp_Object root_window;
24418 struct window *mini_w;
24420 current_header_line_height = current_mode_line_height = -1;
24422 CHARPOS (this_line_start_pos) = 0;
24424 mini_w = XWINDOW (minibuf_window);
24425 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24427 if (!noninteractive)
24429 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24430 int i;
24432 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24433 set_window_height (root_window,
24434 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24436 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24437 set_window_height (minibuf_window, 1, 0);
24439 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24440 mini_w->total_cols = make_number (FRAME_COLS (f));
24442 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24443 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24444 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24446 /* The default ellipsis glyphs `...'. */
24447 for (i = 0; i < 3; ++i)
24448 default_invis_vector[i] = make_number ('.');
24452 /* Allocate the buffer for frame titles.
24453 Also used for `format-mode-line'. */
24454 int size = 100;
24455 mode_line_noprop_buf = (char *) xmalloc (size);
24456 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24457 mode_line_noprop_ptr = mode_line_noprop_buf;
24458 mode_line_target = MODE_LINE_DISPLAY;
24461 help_echo_showing_p = 0;
24465 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24466 (do not change this comment) */