(redisplay_window): Check return value of
[emacs.git] / src / xdisp.c
blob4cc4b5f233e669aede063940ceb33b589da3ac72
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 #ifndef HAVE_CARBON
1704 static
1705 #endif
1706 struct glyph *
1707 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1708 struct window *w;
1709 int x, y;
1710 int *hpos, *vpos, *dx, *dy, *area;
1712 struct glyph *glyph, *end;
1713 struct glyph_row *row = NULL;
1714 int x0, i;
1716 /* Find row containing Y. Give up if some row is not enabled. */
1717 for (i = 0; i < w->current_matrix->nrows; ++i)
1719 row = MATRIX_ROW (w->current_matrix, i);
1720 if (!row->enabled_p)
1721 return NULL;
1722 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1723 break;
1726 *vpos = i;
1727 *hpos = 0;
1729 /* Give up if Y is not in the window. */
1730 if (i == w->current_matrix->nrows)
1731 return NULL;
1733 /* Get the glyph area containing X. */
1734 if (w->pseudo_window_p)
1736 *area = TEXT_AREA;
1737 x0 = 0;
1739 else
1741 if (x < window_box_left_offset (w, TEXT_AREA))
1743 *area = LEFT_MARGIN_AREA;
1744 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1746 else if (x < window_box_right_offset (w, TEXT_AREA))
1748 *area = TEXT_AREA;
1749 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1751 else
1753 *area = RIGHT_MARGIN_AREA;
1754 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1758 /* Find glyph containing X. */
1759 glyph = row->glyphs[*area];
1760 end = glyph + row->used[*area];
1761 x -= x0;
1762 while (glyph < end && x >= glyph->pixel_width)
1764 x -= glyph->pixel_width;
1765 ++glyph;
1768 if (glyph == end)
1769 return NULL;
1771 if (dx)
1773 *dx = x;
1774 *dy = y - (row->y + row->ascent - glyph->ascent);
1777 *hpos = glyph - row->glyphs[*area];
1778 return glyph;
1782 /* EXPORT:
1783 Convert frame-relative x/y to coordinates relative to window W.
1784 Takes pseudo-windows into account. */
1786 void
1787 frame_to_window_pixel_xy (w, x, y)
1788 struct window *w;
1789 int *x, *y;
1791 if (w->pseudo_window_p)
1793 /* A pseudo-window is always full-width, and starts at the
1794 left edge of the frame, plus a frame border. */
1795 struct frame *f = XFRAME (w->frame);
1796 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1797 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1799 else
1801 *x -= WINDOW_LEFT_EDGE_X (w);
1802 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1806 /* EXPORT:
1807 Return in RECTS[] at most N clipping rectangles for glyph string S.
1808 Return the number of stored rectangles. */
1811 get_glyph_string_clip_rects (s, rects, n)
1812 struct glyph_string *s;
1813 NativeRectangle *rects;
1814 int n;
1816 XRectangle r;
1818 if (n <= 0)
1819 return 0;
1821 if (s->row->full_width_p)
1823 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1824 r.x = WINDOW_LEFT_EDGE_X (s->w);
1825 r.width = WINDOW_TOTAL_WIDTH (s->w);
1827 /* Unless displaying a mode or menu bar line, which are always
1828 fully visible, clip to the visible part of the row. */
1829 if (s->w->pseudo_window_p)
1830 r.height = s->row->visible_height;
1831 else
1832 r.height = s->height;
1834 else
1836 /* This is a text line that may be partially visible. */
1837 r.x = window_box_left (s->w, s->area);
1838 r.width = window_box_width (s->w, s->area);
1839 r.height = s->row->visible_height;
1842 if (s->clip_head)
1843 if (r.x < s->clip_head->x)
1845 if (r.width >= s->clip_head->x - r.x)
1846 r.width -= s->clip_head->x - r.x;
1847 else
1848 r.width = 0;
1849 r.x = s->clip_head->x;
1851 if (s->clip_tail)
1852 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1854 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1855 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1856 else
1857 r.width = 0;
1860 /* If S draws overlapping rows, it's sufficient to use the top and
1861 bottom of the window for clipping because this glyph string
1862 intentionally draws over other lines. */
1863 if (s->for_overlaps)
1865 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1866 r.height = window_text_bottom_y (s->w) - r.y;
1868 /* Alas, the above simple strategy does not work for the
1869 environments with anti-aliased text: if the same text is
1870 drawn onto the same place multiple times, it gets thicker.
1871 If the overlap we are processing is for the erased cursor, we
1872 take the intersection with the rectagle of the cursor. */
1873 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1875 XRectangle rc, r_save = r;
1877 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1878 rc.y = s->w->phys_cursor.y;
1879 rc.width = s->w->phys_cursor_width;
1880 rc.height = s->w->phys_cursor_height;
1882 x_intersect_rectangles (&r_save, &rc, &r);
1885 else
1887 /* Don't use S->y for clipping because it doesn't take partially
1888 visible lines into account. For example, it can be negative for
1889 partially visible lines at the top of a window. */
1890 if (!s->row->full_width_p
1891 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1892 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1893 else
1894 r.y = max (0, s->row->y);
1896 /* If drawing a tool-bar window, draw it over the internal border
1897 at the top of the window. */
1898 if (WINDOWP (s->f->tool_bar_window)
1899 && s->w == XWINDOW (s->f->tool_bar_window))
1900 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1903 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1905 /* If drawing the cursor, don't let glyph draw outside its
1906 advertised boundaries. Cleartype does this under some circumstances. */
1907 if (s->hl == DRAW_CURSOR)
1909 struct glyph *glyph = s->first_glyph;
1910 int height, max_y;
1912 if (s->x > r.x)
1914 r.width -= s->x - r.x;
1915 r.x = s->x;
1917 r.width = min (r.width, glyph->pixel_width);
1919 /* If r.y is below window bottom, ensure that we still see a cursor. */
1920 height = min (glyph->ascent + glyph->descent,
1921 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1922 max_y = window_text_bottom_y (s->w) - height;
1923 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1924 if (s->ybase - glyph->ascent > max_y)
1926 r.y = max_y;
1927 r.height = height;
1929 else
1931 /* Don't draw cursor glyph taller than our actual glyph. */
1932 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1933 if (height < r.height)
1935 max_y = r.y + r.height;
1936 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1937 r.height = min (max_y - r.y, height);
1942 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1943 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1945 #ifdef CONVERT_FROM_XRECT
1946 CONVERT_FROM_XRECT (r, *rects);
1947 #else
1948 *rects = r;
1949 #endif
1950 return 1;
1952 else
1954 /* If we are processing overlapping and allowed to return
1955 multiple clipping rectangles, we exclude the row of the glyph
1956 string from the clipping rectangle. This is to avoid drawing
1957 the same text on the environment with anti-aliasing. */
1958 #ifdef CONVERT_FROM_XRECT
1959 XRectangle rs[2];
1960 #else
1961 XRectangle *rs = rects;
1962 #endif
1963 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1965 if (s->for_overlaps & OVERLAPS_PRED)
1967 rs[i] = r;
1968 if (r.y + r.height > row_y)
1970 if (r.y < row_y)
1971 rs[i].height = row_y - r.y;
1972 else
1973 rs[i].height = 0;
1975 i++;
1977 if (s->for_overlaps & OVERLAPS_SUCC)
1979 rs[i] = r;
1980 if (r.y < row_y + s->row->visible_height)
1982 if (r.y + r.height > row_y + s->row->visible_height)
1984 rs[i].y = row_y + s->row->visible_height;
1985 rs[i].height = r.y + r.height - rs[i].y;
1987 else
1988 rs[i].height = 0;
1990 i++;
1993 n = i;
1994 #ifdef CONVERT_FROM_XRECT
1995 for (i = 0; i < n; i++)
1996 CONVERT_FROM_XRECT (rs[i], rects[i]);
1997 #endif
1998 return n;
2002 /* EXPORT:
2003 Return in *NR the clipping rectangle for glyph string S. */
2005 void
2006 get_glyph_string_clip_rect (s, nr)
2007 struct glyph_string *s;
2008 NativeRectangle *nr;
2010 get_glyph_string_clip_rects (s, nr, 1);
2014 /* EXPORT:
2015 Return the position and height of the phys cursor in window W.
2016 Set w->phys_cursor_width to width of phys cursor.
2019 void
2020 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2021 struct window *w;
2022 struct glyph_row *row;
2023 struct glyph *glyph;
2024 int *xp, *yp, *heightp;
2026 struct frame *f = XFRAME (WINDOW_FRAME (w));
2027 int x, y, wd, h, h0, y0;
2029 /* Compute the width of the rectangle to draw. If on a stretch
2030 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2031 rectangle as wide as the glyph, but use a canonical character
2032 width instead. */
2033 wd = glyph->pixel_width - 1;
2034 #ifdef HAVE_NTGUI
2035 wd++; /* Why? */
2036 #endif
2038 x = w->phys_cursor.x;
2039 if (x < 0)
2041 wd += x;
2042 x = 0;
2045 if (glyph->type == STRETCH_GLYPH
2046 && !x_stretch_cursor_p)
2047 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2048 w->phys_cursor_width = wd;
2050 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2052 /* If y is below window bottom, ensure that we still see a cursor. */
2053 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2055 h = max (h0, glyph->ascent + glyph->descent);
2056 h0 = min (h0, glyph->ascent + glyph->descent);
2058 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2059 if (y < y0)
2061 h = max (h - (y0 - y) + 1, h0);
2062 y = y0 - 1;
2064 else
2066 y0 = window_text_bottom_y (w) - h0;
2067 if (y > y0)
2069 h += y - y0;
2070 y = y0;
2074 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2075 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2076 *heightp = h;
2080 * Remember which glyph the mouse is over.
2083 void
2084 remember_mouse_glyph (f, gx, gy, rect)
2085 struct frame *f;
2086 int gx, gy;
2087 NativeRectangle *rect;
2089 Lisp_Object window;
2090 struct window *w;
2091 struct glyph_row *r, *gr, *end_row;
2092 enum window_part part;
2093 enum glyph_row_area area;
2094 int x, y, width, height;
2096 /* Try to determine frame pixel position and size of the glyph under
2097 frame pixel coordinates X/Y on frame F. */
2099 if (!f->glyphs_initialized_p
2100 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2101 NILP (window)))
2103 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2104 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2105 goto virtual_glyph;
2108 w = XWINDOW (window);
2109 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2110 height = WINDOW_FRAME_LINE_HEIGHT (w);
2112 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2113 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2115 if (w->pseudo_window_p)
2117 area = TEXT_AREA;
2118 part = ON_MODE_LINE; /* Don't adjust margin. */
2119 goto text_glyph;
2122 switch (part)
2124 case ON_LEFT_MARGIN:
2125 area = LEFT_MARGIN_AREA;
2126 goto text_glyph;
2128 case ON_RIGHT_MARGIN:
2129 area = RIGHT_MARGIN_AREA;
2130 goto text_glyph;
2132 case ON_HEADER_LINE:
2133 case ON_MODE_LINE:
2134 gr = (part == ON_HEADER_LINE
2135 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2136 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2137 gy = gr->y;
2138 area = TEXT_AREA;
2139 goto text_glyph_row_found;
2141 case ON_TEXT:
2142 area = TEXT_AREA;
2144 text_glyph:
2145 gr = 0; gy = 0;
2146 for (; r <= end_row && r->enabled_p; ++r)
2147 if (r->y + r->height > y)
2149 gr = r; gy = r->y;
2150 break;
2153 text_glyph_row_found:
2154 if (gr && gy <= y)
2156 struct glyph *g = gr->glyphs[area];
2157 struct glyph *end = g + gr->used[area];
2159 height = gr->height;
2160 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2161 if (gx + g->pixel_width > x)
2162 break;
2164 if (g < end)
2166 if (g->type == IMAGE_GLYPH)
2168 /* Don't remember when mouse is over image, as
2169 image may have hot-spots. */
2170 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2171 return;
2173 width = g->pixel_width;
2175 else
2177 /* Use nominal char spacing at end of line. */
2178 x -= gx;
2179 gx += (x / width) * width;
2182 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2183 gx += window_box_left_offset (w, area);
2185 else
2187 /* Use nominal line height at end of window. */
2188 gx = (x / width) * width;
2189 y -= gy;
2190 gy += (y / height) * height;
2192 break;
2194 case ON_LEFT_FRINGE:
2195 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2196 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2197 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2198 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2199 goto row_glyph;
2201 case ON_RIGHT_FRINGE:
2202 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2203 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2204 : window_box_right_offset (w, TEXT_AREA));
2205 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2206 goto row_glyph;
2208 case ON_SCROLL_BAR:
2209 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2211 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2212 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2213 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2214 : 0)));
2215 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2217 row_glyph:
2218 gr = 0, gy = 0;
2219 for (; r <= end_row && r->enabled_p; ++r)
2220 if (r->y + r->height > y)
2222 gr = r; gy = r->y;
2223 break;
2226 if (gr && gy <= y)
2227 height = gr->height;
2228 else
2230 /* Use nominal line height at end of window. */
2231 y -= gy;
2232 gy += (y / height) * height;
2234 break;
2236 default:
2238 virtual_glyph:
2239 /* If there is no glyph under the mouse, then we divide the screen
2240 into a grid of the smallest glyph in the frame, and use that
2241 as our "glyph". */
2243 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2244 round down even for negative values. */
2245 if (gx < 0)
2246 gx -= width - 1;
2247 if (gy < 0)
2248 gy -= height - 1;
2250 gx = (gx / width) * width;
2251 gy = (gy / height) * height;
2253 goto store_rect;
2256 gx += WINDOW_LEFT_EDGE_X (w);
2257 gy += WINDOW_TOP_EDGE_Y (w);
2259 store_rect:
2260 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2262 /* Visible feedback for debugging. */
2263 #if 0
2264 #if HAVE_X_WINDOWS
2265 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2266 f->output_data.x->normal_gc,
2267 gx, gy, width, height);
2268 #endif
2269 #endif
2273 #endif /* HAVE_WINDOW_SYSTEM */
2276 /***********************************************************************
2277 Lisp form evaluation
2278 ***********************************************************************/
2280 /* Error handler for safe_eval and safe_call. */
2282 static Lisp_Object
2283 safe_eval_handler (arg)
2284 Lisp_Object arg;
2286 add_to_log ("Error during redisplay: %s", arg, Qnil);
2287 return Qnil;
2291 /* Evaluate SEXPR and return the result, or nil if something went
2292 wrong. Prevent redisplay during the evaluation. */
2294 Lisp_Object
2295 safe_eval (sexpr)
2296 Lisp_Object sexpr;
2298 Lisp_Object val;
2300 if (inhibit_eval_during_redisplay)
2301 val = Qnil;
2302 else
2304 int count = SPECPDL_INDEX ();
2305 struct gcpro gcpro1;
2307 GCPRO1 (sexpr);
2308 specbind (Qinhibit_redisplay, Qt);
2309 /* Use Qt to ensure debugger does not run,
2310 so there is no possibility of wanting to redisplay. */
2311 val = internal_condition_case_1 (Feval, sexpr, Qt,
2312 safe_eval_handler);
2313 UNGCPRO;
2314 val = unbind_to (count, val);
2317 return val;
2321 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2322 Return the result, or nil if something went wrong. Prevent
2323 redisplay during the evaluation. */
2325 Lisp_Object
2326 safe_call (nargs, args)
2327 int nargs;
2328 Lisp_Object *args;
2330 Lisp_Object val;
2332 if (inhibit_eval_during_redisplay)
2333 val = Qnil;
2334 else
2336 int count = SPECPDL_INDEX ();
2337 struct gcpro gcpro1;
2339 GCPRO1 (args[0]);
2340 gcpro1.nvars = nargs;
2341 specbind (Qinhibit_redisplay, Qt);
2342 /* Use Qt to ensure debugger does not run,
2343 so there is no possibility of wanting to redisplay. */
2344 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2345 safe_eval_handler);
2346 UNGCPRO;
2347 val = unbind_to (count, val);
2350 return val;
2354 /* Call function FN with one argument ARG.
2355 Return the result, or nil if something went wrong. */
2357 Lisp_Object
2358 safe_call1 (fn, arg)
2359 Lisp_Object fn, arg;
2361 Lisp_Object args[2];
2362 args[0] = fn;
2363 args[1] = arg;
2364 return safe_call (2, args);
2369 /***********************************************************************
2370 Debugging
2371 ***********************************************************************/
2373 #if 0
2375 /* Define CHECK_IT to perform sanity checks on iterators.
2376 This is for debugging. It is too slow to do unconditionally. */
2378 static void
2379 check_it (it)
2380 struct it *it;
2382 if (it->method == GET_FROM_STRING)
2384 xassert (STRINGP (it->string));
2385 xassert (IT_STRING_CHARPOS (*it) >= 0);
2387 else
2389 xassert (IT_STRING_CHARPOS (*it) < 0);
2390 if (it->method == GET_FROM_BUFFER)
2392 /* Check that character and byte positions agree. */
2393 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2397 if (it->dpvec)
2398 xassert (it->current.dpvec_index >= 0);
2399 else
2400 xassert (it->current.dpvec_index < 0);
2403 #define CHECK_IT(IT) check_it ((IT))
2405 #else /* not 0 */
2407 #define CHECK_IT(IT) (void) 0
2409 #endif /* not 0 */
2412 #if GLYPH_DEBUG
2414 /* Check that the window end of window W is what we expect it
2415 to be---the last row in the current matrix displaying text. */
2417 static void
2418 check_window_end (w)
2419 struct window *w;
2421 if (!MINI_WINDOW_P (w)
2422 && !NILP (w->window_end_valid))
2424 struct glyph_row *row;
2425 xassert ((row = MATRIX_ROW (w->current_matrix,
2426 XFASTINT (w->window_end_vpos)),
2427 !row->enabled_p
2428 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2429 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2433 #define CHECK_WINDOW_END(W) check_window_end ((W))
2435 #else /* not GLYPH_DEBUG */
2437 #define CHECK_WINDOW_END(W) (void) 0
2439 #endif /* not GLYPH_DEBUG */
2443 /***********************************************************************
2444 Iterator initialization
2445 ***********************************************************************/
2447 /* Initialize IT for displaying current_buffer in window W, starting
2448 at character position CHARPOS. CHARPOS < 0 means that no buffer
2449 position is specified which is useful when the iterator is assigned
2450 a position later. BYTEPOS is the byte position corresponding to
2451 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2453 If ROW is not null, calls to produce_glyphs with IT as parameter
2454 will produce glyphs in that row.
2456 BASE_FACE_ID is the id of a base face to use. It must be one of
2457 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2458 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2459 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2461 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2462 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2463 will be initialized to use the corresponding mode line glyph row of
2464 the desired matrix of W. */
2466 void
2467 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2468 struct it *it;
2469 struct window *w;
2470 int charpos, bytepos;
2471 struct glyph_row *row;
2472 enum face_id base_face_id;
2474 int highlight_region_p;
2476 /* Some precondition checks. */
2477 xassert (w != NULL && it != NULL);
2478 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2479 && charpos <= ZV));
2481 /* If face attributes have been changed since the last redisplay,
2482 free realized faces now because they depend on face definitions
2483 that might have changed. Don't free faces while there might be
2484 desired matrices pending which reference these faces. */
2485 if (face_change_count && !inhibit_free_realized_faces)
2487 face_change_count = 0;
2488 free_all_realized_faces (Qnil);
2491 /* Use one of the mode line rows of W's desired matrix if
2492 appropriate. */
2493 if (row == NULL)
2495 if (base_face_id == MODE_LINE_FACE_ID
2496 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2497 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2498 else if (base_face_id == HEADER_LINE_FACE_ID)
2499 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2502 /* Clear IT. */
2503 bzero (it, sizeof *it);
2504 it->current.overlay_string_index = -1;
2505 it->current.dpvec_index = -1;
2506 it->base_face_id = base_face_id;
2507 it->string = Qnil;
2508 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2510 /* The window in which we iterate over current_buffer: */
2511 XSETWINDOW (it->window, w);
2512 it->w = w;
2513 it->f = XFRAME (w->frame);
2515 /* Extra space between lines (on window systems only). */
2516 if (base_face_id == DEFAULT_FACE_ID
2517 && FRAME_WINDOW_P (it->f))
2519 if (NATNUMP (current_buffer->extra_line_spacing))
2520 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2521 else if (FLOATP (current_buffer->extra_line_spacing))
2522 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2523 * FRAME_LINE_HEIGHT (it->f));
2524 else if (it->f->extra_line_spacing > 0)
2525 it->extra_line_spacing = it->f->extra_line_spacing;
2526 it->max_extra_line_spacing = 0;
2529 /* If realized faces have been removed, e.g. because of face
2530 attribute changes of named faces, recompute them. When running
2531 in batch mode, the face cache of Vterminal_frame is null. If
2532 we happen to get called, make a dummy face cache. */
2533 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2534 init_frame_faces (it->f);
2535 if (FRAME_FACE_CACHE (it->f)->used == 0)
2536 recompute_basic_faces (it->f);
2538 /* Current value of the `slice', `space-width', and 'height' properties. */
2539 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2540 it->space_width = Qnil;
2541 it->font_height = Qnil;
2542 it->override_ascent = -1;
2544 /* Are control characters displayed as `^C'? */
2545 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2547 /* -1 means everything between a CR and the following line end
2548 is invisible. >0 means lines indented more than this value are
2549 invisible. */
2550 it->selective = (INTEGERP (current_buffer->selective_display)
2551 ? XFASTINT (current_buffer->selective_display)
2552 : (!NILP (current_buffer->selective_display)
2553 ? -1 : 0));
2554 it->selective_display_ellipsis_p
2555 = !NILP (current_buffer->selective_display_ellipses);
2557 /* Display table to use. */
2558 it->dp = window_display_table (w);
2560 /* Are multibyte characters enabled in current_buffer? */
2561 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2563 /* Non-zero if we should highlight the region. */
2564 highlight_region_p
2565 = (!NILP (Vtransient_mark_mode)
2566 && !NILP (current_buffer->mark_active)
2567 && XMARKER (current_buffer->mark)->buffer != 0);
2569 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2570 start and end of a visible region in window IT->w. Set both to
2571 -1 to indicate no region. */
2572 if (highlight_region_p
2573 /* Maybe highlight only in selected window. */
2574 && (/* Either show region everywhere. */
2575 highlight_nonselected_windows
2576 /* Or show region in the selected window. */
2577 || w == XWINDOW (selected_window)
2578 /* Or show the region if we are in the mini-buffer and W is
2579 the window the mini-buffer refers to. */
2580 || (MINI_WINDOW_P (XWINDOW (selected_window))
2581 && WINDOWP (minibuf_selected_window)
2582 && w == XWINDOW (minibuf_selected_window))))
2584 int charpos = marker_position (current_buffer->mark);
2585 it->region_beg_charpos = min (PT, charpos);
2586 it->region_end_charpos = max (PT, charpos);
2588 else
2589 it->region_beg_charpos = it->region_end_charpos = -1;
2591 /* Get the position at which the redisplay_end_trigger hook should
2592 be run, if it is to be run at all. */
2593 if (MARKERP (w->redisplay_end_trigger)
2594 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2595 it->redisplay_end_trigger_charpos
2596 = marker_position (w->redisplay_end_trigger);
2597 else if (INTEGERP (w->redisplay_end_trigger))
2598 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2600 /* Correct bogus values of tab_width. */
2601 it->tab_width = XINT (current_buffer->tab_width);
2602 if (it->tab_width <= 0 || it->tab_width > 1000)
2603 it->tab_width = 8;
2605 /* Are lines in the display truncated? */
2606 it->truncate_lines_p
2607 = (base_face_id != DEFAULT_FACE_ID
2608 || XINT (it->w->hscroll)
2609 || (truncate_partial_width_windows
2610 && !WINDOW_FULL_WIDTH_P (it->w))
2611 || !NILP (current_buffer->truncate_lines));
2613 /* Get dimensions of truncation and continuation glyphs. These are
2614 displayed as fringe bitmaps under X, so we don't need them for such
2615 frames. */
2616 if (!FRAME_WINDOW_P (it->f))
2618 if (it->truncate_lines_p)
2620 /* We will need the truncation glyph. */
2621 xassert (it->glyph_row == NULL);
2622 produce_special_glyphs (it, IT_TRUNCATION);
2623 it->truncation_pixel_width = it->pixel_width;
2625 else
2627 /* We will need the continuation glyph. */
2628 xassert (it->glyph_row == NULL);
2629 produce_special_glyphs (it, IT_CONTINUATION);
2630 it->continuation_pixel_width = it->pixel_width;
2633 /* Reset these values to zero because the produce_special_glyphs
2634 above has changed them. */
2635 it->pixel_width = it->ascent = it->descent = 0;
2636 it->phys_ascent = it->phys_descent = 0;
2639 /* Set this after getting the dimensions of truncation and
2640 continuation glyphs, so that we don't produce glyphs when calling
2641 produce_special_glyphs, above. */
2642 it->glyph_row = row;
2643 it->area = TEXT_AREA;
2645 /* Get the dimensions of the display area. The display area
2646 consists of the visible window area plus a horizontally scrolled
2647 part to the left of the window. All x-values are relative to the
2648 start of this total display area. */
2649 if (base_face_id != DEFAULT_FACE_ID)
2651 /* Mode lines, menu bar in terminal frames. */
2652 it->first_visible_x = 0;
2653 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2655 else
2657 it->first_visible_x
2658 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2659 it->last_visible_x = (it->first_visible_x
2660 + window_box_width (w, TEXT_AREA));
2662 /* If we truncate lines, leave room for the truncator glyph(s) at
2663 the right margin. Otherwise, leave room for the continuation
2664 glyph(s). Truncation and continuation glyphs are not inserted
2665 for window-based redisplay. */
2666 if (!FRAME_WINDOW_P (it->f))
2668 if (it->truncate_lines_p)
2669 it->last_visible_x -= it->truncation_pixel_width;
2670 else
2671 it->last_visible_x -= it->continuation_pixel_width;
2674 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2675 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2678 /* Leave room for a border glyph. */
2679 if (!FRAME_WINDOW_P (it->f)
2680 && !WINDOW_RIGHTMOST_P (it->w))
2681 it->last_visible_x -= 1;
2683 it->last_visible_y = window_text_bottom_y (w);
2685 /* For mode lines and alike, arrange for the first glyph having a
2686 left box line if the face specifies a box. */
2687 if (base_face_id != DEFAULT_FACE_ID)
2689 struct face *face;
2691 it->face_id = base_face_id;
2693 /* If we have a boxed mode line, make the first character appear
2694 with a left box line. */
2695 face = FACE_FROM_ID (it->f, base_face_id);
2696 if (face->box != FACE_NO_BOX)
2697 it->start_of_box_run_p = 1;
2700 /* If a buffer position was specified, set the iterator there,
2701 getting overlays and face properties from that position. */
2702 if (charpos >= BUF_BEG (current_buffer))
2704 it->end_charpos = ZV;
2705 it->face_id = -1;
2706 IT_CHARPOS (*it) = charpos;
2708 /* Compute byte position if not specified. */
2709 if (bytepos < charpos)
2710 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2711 else
2712 IT_BYTEPOS (*it) = bytepos;
2714 it->start = it->current;
2716 /* Compute faces etc. */
2717 reseat (it, it->current.pos, 1);
2720 CHECK_IT (it);
2724 /* Initialize IT for the display of window W with window start POS. */
2726 void
2727 start_display (it, w, pos)
2728 struct it *it;
2729 struct window *w;
2730 struct text_pos pos;
2732 struct glyph_row *row;
2733 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2735 row = w->desired_matrix->rows + first_vpos;
2736 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2737 it->first_vpos = first_vpos;
2739 /* Don't reseat to previous visible line start if current start
2740 position is in a string or image. */
2741 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2743 int start_at_line_beg_p;
2744 int first_y = it->current_y;
2746 /* If window start is not at a line start, skip forward to POS to
2747 get the correct continuation lines width. */
2748 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2749 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2750 if (!start_at_line_beg_p)
2752 int new_x;
2754 reseat_at_previous_visible_line_start (it);
2755 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2757 new_x = it->current_x + it->pixel_width;
2759 /* If lines are continued, this line may end in the middle
2760 of a multi-glyph character (e.g. a control character
2761 displayed as \003, or in the middle of an overlay
2762 string). In this case move_it_to above will not have
2763 taken us to the start of the continuation line but to the
2764 end of the continued line. */
2765 if (it->current_x > 0
2766 && !it->truncate_lines_p /* Lines are continued. */
2767 && (/* And glyph doesn't fit on the line. */
2768 new_x > it->last_visible_x
2769 /* Or it fits exactly and we're on a window
2770 system frame. */
2771 || (new_x == it->last_visible_x
2772 && FRAME_WINDOW_P (it->f))))
2774 if (it->current.dpvec_index >= 0
2775 || it->current.overlay_string_index >= 0)
2777 set_iterator_to_next (it, 1);
2778 move_it_in_display_line_to (it, -1, -1, 0);
2781 it->continuation_lines_width += it->current_x;
2784 /* We're starting a new display line, not affected by the
2785 height of the continued line, so clear the appropriate
2786 fields in the iterator structure. */
2787 it->max_ascent = it->max_descent = 0;
2788 it->max_phys_ascent = it->max_phys_descent = 0;
2790 it->current_y = first_y;
2791 it->vpos = 0;
2792 it->current_x = it->hpos = 0;
2796 #if 0 /* Don't assert the following because start_display is sometimes
2797 called intentionally with a window start that is not at a
2798 line start. Please leave this code in as a comment. */
2800 /* Window start should be on a line start, now. */
2801 xassert (it->continuation_lines_width
2802 || IT_CHARPOS (it) == BEGV
2803 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2804 #endif /* 0 */
2808 /* Return 1 if POS is a position in ellipses displayed for invisible
2809 text. W is the window we display, for text property lookup. */
2811 static int
2812 in_ellipses_for_invisible_text_p (pos, w)
2813 struct display_pos *pos;
2814 struct window *w;
2816 Lisp_Object prop, window;
2817 int ellipses_p = 0;
2818 int charpos = CHARPOS (pos->pos);
2820 /* If POS specifies a position in a display vector, this might
2821 be for an ellipsis displayed for invisible text. We won't
2822 get the iterator set up for delivering that ellipsis unless
2823 we make sure that it gets aware of the invisible text. */
2824 if (pos->dpvec_index >= 0
2825 && pos->overlay_string_index < 0
2826 && CHARPOS (pos->string_pos) < 0
2827 && charpos > BEGV
2828 && (XSETWINDOW (window, w),
2829 prop = Fget_char_property (make_number (charpos),
2830 Qinvisible, window),
2831 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2833 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2834 window);
2835 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2838 return ellipses_p;
2842 /* Initialize IT for stepping through current_buffer in window W,
2843 starting at position POS that includes overlay string and display
2844 vector/ control character translation position information. Value
2845 is zero if there are overlay strings with newlines at POS. */
2847 static int
2848 init_from_display_pos (it, w, pos)
2849 struct it *it;
2850 struct window *w;
2851 struct display_pos *pos;
2853 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2854 int i, overlay_strings_with_newlines = 0;
2856 /* If POS specifies a position in a display vector, this might
2857 be for an ellipsis displayed for invisible text. We won't
2858 get the iterator set up for delivering that ellipsis unless
2859 we make sure that it gets aware of the invisible text. */
2860 if (in_ellipses_for_invisible_text_p (pos, w))
2862 --charpos;
2863 bytepos = 0;
2866 /* Keep in mind: the call to reseat in init_iterator skips invisible
2867 text, so we might end up at a position different from POS. This
2868 is only a problem when POS is a row start after a newline and an
2869 overlay starts there with an after-string, and the overlay has an
2870 invisible property. Since we don't skip invisible text in
2871 display_line and elsewhere immediately after consuming the
2872 newline before the row start, such a POS will not be in a string,
2873 but the call to init_iterator below will move us to the
2874 after-string. */
2875 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2877 /* This only scans the current chunk -- it should scan all chunks.
2878 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2879 to 16 in 22.1 to make this a lesser problem. */
2880 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2882 const char *s = SDATA (it->overlay_strings[i]);
2883 const char *e = s + SBYTES (it->overlay_strings[i]);
2885 while (s < e && *s != '\n')
2886 ++s;
2888 if (s < e)
2890 overlay_strings_with_newlines = 1;
2891 break;
2895 /* If position is within an overlay string, set up IT to the right
2896 overlay string. */
2897 if (pos->overlay_string_index >= 0)
2899 int relative_index;
2901 /* If the first overlay string happens to have a `display'
2902 property for an image, the iterator will be set up for that
2903 image, and we have to undo that setup first before we can
2904 correct the overlay string index. */
2905 if (it->method == GET_FROM_IMAGE)
2906 pop_it (it);
2908 /* We already have the first chunk of overlay strings in
2909 IT->overlay_strings. Load more until the one for
2910 pos->overlay_string_index is in IT->overlay_strings. */
2911 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2913 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2914 it->current.overlay_string_index = 0;
2915 while (n--)
2917 load_overlay_strings (it, 0);
2918 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2922 it->current.overlay_string_index = pos->overlay_string_index;
2923 relative_index = (it->current.overlay_string_index
2924 % OVERLAY_STRING_CHUNK_SIZE);
2925 it->string = it->overlay_strings[relative_index];
2926 xassert (STRINGP (it->string));
2927 it->current.string_pos = pos->string_pos;
2928 it->method = GET_FROM_STRING;
2931 #if 0 /* This is bogus because POS not having an overlay string
2932 position does not mean it's after the string. Example: A
2933 line starting with a before-string and initialization of IT
2934 to the previous row's end position. */
2935 else if (it->current.overlay_string_index >= 0)
2937 /* If POS says we're already after an overlay string ending at
2938 POS, make sure to pop the iterator because it will be in
2939 front of that overlay string. When POS is ZV, we've thereby
2940 also ``processed'' overlay strings at ZV. */
2941 while (it->sp)
2942 pop_it (it);
2943 xassert (it->current.overlay_string_index == -1);
2944 xassert (it->method == GET_FROM_BUFFER);
2945 if (CHARPOS (pos->pos) == ZV)
2946 it->overlay_strings_at_end_processed_p = 1;
2948 #endif /* 0 */
2950 if (CHARPOS (pos->string_pos) >= 0)
2952 /* Recorded position is not in an overlay string, but in another
2953 string. This can only be a string from a `display' property.
2954 IT should already be filled with that string. */
2955 it->current.string_pos = pos->string_pos;
2956 xassert (STRINGP (it->string));
2959 /* Restore position in display vector translations, control
2960 character translations or ellipses. */
2961 if (pos->dpvec_index >= 0)
2963 if (it->dpvec == NULL)
2964 get_next_display_element (it);
2965 xassert (it->dpvec && it->current.dpvec_index == 0);
2966 it->current.dpvec_index = pos->dpvec_index;
2969 CHECK_IT (it);
2970 return !overlay_strings_with_newlines;
2974 /* Initialize IT for stepping through current_buffer in window W
2975 starting at ROW->start. */
2977 static void
2978 init_to_row_start (it, w, row)
2979 struct it *it;
2980 struct window *w;
2981 struct glyph_row *row;
2983 init_from_display_pos (it, w, &row->start);
2984 it->start = row->start;
2985 it->continuation_lines_width = row->continuation_lines_width;
2986 CHECK_IT (it);
2990 /* Initialize IT for stepping through current_buffer in window W
2991 starting in the line following ROW, i.e. starting at ROW->end.
2992 Value is zero if there are overlay strings with newlines at ROW's
2993 end position. */
2995 static int
2996 init_to_row_end (it, w, row)
2997 struct it *it;
2998 struct window *w;
2999 struct glyph_row *row;
3001 int success = 0;
3003 if (init_from_display_pos (it, w, &row->end))
3005 if (row->continued_p)
3006 it->continuation_lines_width
3007 = row->continuation_lines_width + row->pixel_width;
3008 CHECK_IT (it);
3009 success = 1;
3012 return success;
3018 /***********************************************************************
3019 Text properties
3020 ***********************************************************************/
3022 /* Called when IT reaches IT->stop_charpos. Handle text property and
3023 overlay changes. Set IT->stop_charpos to the next position where
3024 to stop. */
3026 static void
3027 handle_stop (it)
3028 struct it *it;
3030 enum prop_handled handled;
3031 int handle_overlay_change_p;
3032 struct props *p;
3034 it->dpvec = NULL;
3035 it->current.dpvec_index = -1;
3036 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3037 it->ignore_overlay_strings_at_pos_p = 0;
3039 /* Use face of preceding text for ellipsis (if invisible) */
3040 if (it->selective_display_ellipsis_p)
3041 it->saved_face_id = it->face_id;
3045 handled = HANDLED_NORMALLY;
3047 /* Call text property handlers. */
3048 for (p = it_props; p->handler; ++p)
3050 handled = p->handler (it);
3052 if (handled == HANDLED_RECOMPUTE_PROPS)
3053 break;
3054 else if (handled == HANDLED_RETURN)
3056 /* We still want to show before and after strings from
3057 overlays even if the actual buffer text is replaced. */
3058 if (!handle_overlay_change_p || it->sp > 1)
3059 return;
3060 if (!get_overlay_strings_1 (it, 0, 0))
3061 return;
3062 it->ignore_overlay_strings_at_pos_p = 1;
3063 it->string_from_display_prop_p = 0;
3064 handle_overlay_change_p = 0;
3065 handled = HANDLED_RECOMPUTE_PROPS;
3066 break;
3068 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3069 handle_overlay_change_p = 0;
3072 if (handled != HANDLED_RECOMPUTE_PROPS)
3074 /* Don't check for overlay strings below when set to deliver
3075 characters from a display vector. */
3076 if (it->method == GET_FROM_DISPLAY_VECTOR)
3077 handle_overlay_change_p = 0;
3079 /* Handle overlay changes.
3080 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3081 if it finds overlays. */
3082 if (handle_overlay_change_p)
3083 handled = handle_overlay_change (it);
3086 while (handled == HANDLED_RECOMPUTE_PROPS);
3088 /* Determine where to stop next. */
3089 if (handled == HANDLED_NORMALLY)
3090 compute_stop_pos (it);
3094 /* Compute IT->stop_charpos from text property and overlay change
3095 information for IT's current position. */
3097 static void
3098 compute_stop_pos (it)
3099 struct it *it;
3101 register INTERVAL iv, next_iv;
3102 Lisp_Object object, limit, position;
3104 /* If nowhere else, stop at the end. */
3105 it->stop_charpos = it->end_charpos;
3107 if (STRINGP (it->string))
3109 /* Strings are usually short, so don't limit the search for
3110 properties. */
3111 object = it->string;
3112 limit = Qnil;
3113 position = make_number (IT_STRING_CHARPOS (*it));
3115 else
3117 int charpos;
3119 /* If next overlay change is in front of the current stop pos
3120 (which is IT->end_charpos), stop there. Note: value of
3121 next_overlay_change is point-max if no overlay change
3122 follows. */
3123 charpos = next_overlay_change (IT_CHARPOS (*it));
3124 if (charpos < it->stop_charpos)
3125 it->stop_charpos = charpos;
3127 /* If showing the region, we have to stop at the region
3128 start or end because the face might change there. */
3129 if (it->region_beg_charpos > 0)
3131 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3132 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3133 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3134 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3137 /* Set up variables for computing the stop position from text
3138 property changes. */
3139 XSETBUFFER (object, current_buffer);
3140 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3141 position = make_number (IT_CHARPOS (*it));
3145 /* Get the interval containing IT's position. Value is a null
3146 interval if there isn't such an interval. */
3147 iv = validate_interval_range (object, &position, &position, 0);
3148 if (!NULL_INTERVAL_P (iv))
3150 Lisp_Object values_here[LAST_PROP_IDX];
3151 struct props *p;
3153 /* Get properties here. */
3154 for (p = it_props; p->handler; ++p)
3155 values_here[p->idx] = textget (iv->plist, *p->name);
3157 /* Look for an interval following iv that has different
3158 properties. */
3159 for (next_iv = next_interval (iv);
3160 (!NULL_INTERVAL_P (next_iv)
3161 && (NILP (limit)
3162 || XFASTINT (limit) > next_iv->position));
3163 next_iv = next_interval (next_iv))
3165 for (p = it_props; p->handler; ++p)
3167 Lisp_Object new_value;
3169 new_value = textget (next_iv->plist, *p->name);
3170 if (!EQ (values_here[p->idx], new_value))
3171 break;
3174 if (p->handler)
3175 break;
3178 if (!NULL_INTERVAL_P (next_iv))
3180 if (INTEGERP (limit)
3181 && next_iv->position >= XFASTINT (limit))
3182 /* No text property change up to limit. */
3183 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3184 else
3185 /* Text properties change in next_iv. */
3186 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3190 xassert (STRINGP (it->string)
3191 || (it->stop_charpos >= BEGV
3192 && it->stop_charpos >= IT_CHARPOS (*it)));
3196 /* Return the position of the next overlay change after POS in
3197 current_buffer. Value is point-max if no overlay change
3198 follows. This is like `next-overlay-change' but doesn't use
3199 xmalloc. */
3201 static int
3202 next_overlay_change (pos)
3203 int pos;
3205 int noverlays;
3206 int endpos;
3207 Lisp_Object *overlays;
3208 int i;
3210 /* Get all overlays at the given position. */
3211 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3213 /* If any of these overlays ends before endpos,
3214 use its ending point instead. */
3215 for (i = 0; i < noverlays; ++i)
3217 Lisp_Object oend;
3218 int oendpos;
3220 oend = OVERLAY_END (overlays[i]);
3221 oendpos = OVERLAY_POSITION (oend);
3222 endpos = min (endpos, oendpos);
3225 return endpos;
3230 /***********************************************************************
3231 Fontification
3232 ***********************************************************************/
3234 /* Handle changes in the `fontified' property of the current buffer by
3235 calling hook functions from Qfontification_functions to fontify
3236 regions of text. */
3238 static enum prop_handled
3239 handle_fontified_prop (it)
3240 struct it *it;
3242 Lisp_Object prop, pos;
3243 enum prop_handled handled = HANDLED_NORMALLY;
3245 if (!NILP (Vmemory_full))
3246 return handled;
3248 /* Get the value of the `fontified' property at IT's current buffer
3249 position. (The `fontified' property doesn't have a special
3250 meaning in strings.) If the value is nil, call functions from
3251 Qfontification_functions. */
3252 if (!STRINGP (it->string)
3253 && it->s == NULL
3254 && !NILP (Vfontification_functions)
3255 && !NILP (Vrun_hooks)
3256 && (pos = make_number (IT_CHARPOS (*it)),
3257 prop = Fget_char_property (pos, Qfontified, Qnil),
3258 /* Ignore the special cased nil value always present at EOB since
3259 no amount of fontifying will be able to change it. */
3260 NILP (prop) && IT_CHARPOS (*it) < Z))
3262 int count = SPECPDL_INDEX ();
3263 Lisp_Object val;
3265 val = Vfontification_functions;
3266 specbind (Qfontification_functions, Qnil);
3268 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3269 safe_call1 (val, pos);
3270 else
3272 Lisp_Object globals, fn;
3273 struct gcpro gcpro1, gcpro2;
3275 globals = Qnil;
3276 GCPRO2 (val, globals);
3278 for (; CONSP (val); val = XCDR (val))
3280 fn = XCAR (val);
3282 if (EQ (fn, Qt))
3284 /* A value of t indicates this hook has a local
3285 binding; it means to run the global binding too.
3286 In a global value, t should not occur. If it
3287 does, we must ignore it to avoid an endless
3288 loop. */
3289 for (globals = Fdefault_value (Qfontification_functions);
3290 CONSP (globals);
3291 globals = XCDR (globals))
3293 fn = XCAR (globals);
3294 if (!EQ (fn, Qt))
3295 safe_call1 (fn, pos);
3298 else
3299 safe_call1 (fn, pos);
3302 UNGCPRO;
3305 unbind_to (count, Qnil);
3307 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3308 something. This avoids an endless loop if they failed to
3309 fontify the text for which reason ever. */
3310 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3311 handled = HANDLED_RECOMPUTE_PROPS;
3314 return handled;
3319 /***********************************************************************
3320 Faces
3321 ***********************************************************************/
3323 /* Set up iterator IT from face properties at its current position.
3324 Called from handle_stop. */
3326 static enum prop_handled
3327 handle_face_prop (it)
3328 struct it *it;
3330 int new_face_id, next_stop;
3332 if (!STRINGP (it->string))
3334 new_face_id
3335 = face_at_buffer_position (it->w,
3336 IT_CHARPOS (*it),
3337 it->region_beg_charpos,
3338 it->region_end_charpos,
3339 &next_stop,
3340 (IT_CHARPOS (*it)
3341 + TEXT_PROP_DISTANCE_LIMIT),
3344 /* Is this a start of a run of characters with box face?
3345 Caveat: this can be called for a freshly initialized
3346 iterator; face_id is -1 in this case. We know that the new
3347 face will not change until limit, i.e. if the new face has a
3348 box, all characters up to limit will have one. But, as
3349 usual, we don't know whether limit is really the end. */
3350 if (new_face_id != it->face_id)
3352 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3354 /* If new face has a box but old face has not, this is
3355 the start of a run of characters with box, i.e. it has
3356 a shadow on the left side. The value of face_id of the
3357 iterator will be -1 if this is the initial call that gets
3358 the face. In this case, we have to look in front of IT's
3359 position and see whether there is a face != new_face_id. */
3360 it->start_of_box_run_p
3361 = (new_face->box != FACE_NO_BOX
3362 && (it->face_id >= 0
3363 || IT_CHARPOS (*it) == BEG
3364 || new_face_id != face_before_it_pos (it)));
3365 it->face_box_p = new_face->box != FACE_NO_BOX;
3368 else
3370 int base_face_id, bufpos;
3371 int i;
3372 Lisp_Object from_overlay
3373 = (it->current.overlay_string_index >= 0
3374 ? it->string_overlays[it->current.overlay_string_index]
3375 : Qnil);
3377 /* See if we got to this string directly or indirectly from
3378 an overlay property. That includes the before-string or
3379 after-string of an overlay, strings in display properties
3380 provided by an overlay, their text properties, etc.
3382 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3383 if (! NILP (from_overlay))
3384 for (i = it->sp - 1; i >= 0; i--)
3386 if (it->stack[i].current.overlay_string_index >= 0)
3387 from_overlay
3388 = it->string_overlays[it->stack[i].current.overlay_string_index];
3389 else if (! NILP (it->stack[i].from_overlay))
3390 from_overlay = it->stack[i].from_overlay;
3392 if (!NILP (from_overlay))
3393 break;
3396 if (! NILP (from_overlay))
3398 bufpos = IT_CHARPOS (*it);
3399 /* For a string from an overlay, the base face depends
3400 only on text properties and ignores overlays. */
3401 base_face_id
3402 = face_for_overlay_string (it->w,
3403 IT_CHARPOS (*it),
3404 it->region_beg_charpos,
3405 it->region_end_charpos,
3406 &next_stop,
3407 (IT_CHARPOS (*it)
3408 + TEXT_PROP_DISTANCE_LIMIT),
3410 from_overlay);
3412 else
3414 bufpos = 0;
3416 /* For strings from a `display' property, use the face at
3417 IT's current buffer position as the base face to merge
3418 with, so that overlay strings appear in the same face as
3419 surrounding text, unless they specify their own
3420 faces. */
3421 base_face_id = underlying_face_id (it);
3424 new_face_id = face_at_string_position (it->w,
3425 it->string,
3426 IT_STRING_CHARPOS (*it),
3427 bufpos,
3428 it->region_beg_charpos,
3429 it->region_end_charpos,
3430 &next_stop,
3431 base_face_id, 0);
3433 #if 0 /* This shouldn't be neccessary. Let's check it. */
3434 /* If IT is used to display a mode line we would really like to
3435 use the mode line face instead of the frame's default face. */
3436 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3437 && new_face_id == DEFAULT_FACE_ID)
3438 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3439 #endif
3441 /* Is this a start of a run of characters with box? Caveat:
3442 this can be called for a freshly allocated iterator; face_id
3443 is -1 is this case. We know that the new face will not
3444 change until the next check pos, i.e. if the new face has a
3445 box, all characters up to that position will have a
3446 box. But, as usual, we don't know whether that position
3447 is really the end. */
3448 if (new_face_id != it->face_id)
3450 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3451 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3453 /* If new face has a box but old face hasn't, this is the
3454 start of a run of characters with box, i.e. it has a
3455 shadow on the left side. */
3456 it->start_of_box_run_p
3457 = new_face->box && (old_face == NULL || !old_face->box);
3458 it->face_box_p = new_face->box != FACE_NO_BOX;
3462 it->face_id = new_face_id;
3463 return HANDLED_NORMALLY;
3467 /* Return the ID of the face ``underlying'' IT's current position,
3468 which is in a string. If the iterator is associated with a
3469 buffer, return the face at IT's current buffer position.
3470 Otherwise, use the iterator's base_face_id. */
3472 static int
3473 underlying_face_id (it)
3474 struct it *it;
3476 int face_id = it->base_face_id, i;
3478 xassert (STRINGP (it->string));
3480 for (i = it->sp - 1; i >= 0; --i)
3481 if (NILP (it->stack[i].string))
3482 face_id = it->stack[i].face_id;
3484 return face_id;
3488 /* Compute the face one character before or after the current position
3489 of IT. BEFORE_P non-zero means get the face in front of IT's
3490 position. Value is the id of the face. */
3492 static int
3493 face_before_or_after_it_pos (it, before_p)
3494 struct it *it;
3495 int before_p;
3497 int face_id, limit;
3498 int next_check_charpos;
3499 struct text_pos pos;
3501 xassert (it->s == NULL);
3503 if (STRINGP (it->string))
3505 int bufpos, base_face_id;
3507 /* No face change past the end of the string (for the case
3508 we are padding with spaces). No face change before the
3509 string start. */
3510 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3511 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3512 return it->face_id;
3514 /* Set pos to the position before or after IT's current position. */
3515 if (before_p)
3516 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3517 else
3518 /* For composition, we must check the character after the
3519 composition. */
3520 pos = (it->what == IT_COMPOSITION
3521 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3522 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3524 if (it->current.overlay_string_index >= 0)
3525 bufpos = IT_CHARPOS (*it);
3526 else
3527 bufpos = 0;
3529 base_face_id = underlying_face_id (it);
3531 /* Get the face for ASCII, or unibyte. */
3532 face_id = face_at_string_position (it->w,
3533 it->string,
3534 CHARPOS (pos),
3535 bufpos,
3536 it->region_beg_charpos,
3537 it->region_end_charpos,
3538 &next_check_charpos,
3539 base_face_id, 0);
3541 /* Correct the face for charsets different from ASCII. Do it
3542 for the multibyte case only. The face returned above is
3543 suitable for unibyte text if IT->string is unibyte. */
3544 if (STRING_MULTIBYTE (it->string))
3546 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3547 int rest = SBYTES (it->string) - BYTEPOS (pos);
3548 int c, len;
3549 struct face *face = FACE_FROM_ID (it->f, face_id);
3551 c = string_char_and_length (p, rest, &len);
3552 face_id = FACE_FOR_CHAR (it->f, face, c);
3555 else
3557 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3558 || (IT_CHARPOS (*it) <= BEGV && before_p))
3559 return it->face_id;
3561 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3562 pos = it->current.pos;
3564 if (before_p)
3565 DEC_TEXT_POS (pos, it->multibyte_p);
3566 else
3568 if (it->what == IT_COMPOSITION)
3569 /* For composition, we must check the position after the
3570 composition. */
3571 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3572 else
3573 INC_TEXT_POS (pos, it->multibyte_p);
3576 /* Determine face for CHARSET_ASCII, or unibyte. */
3577 face_id = face_at_buffer_position (it->w,
3578 CHARPOS (pos),
3579 it->region_beg_charpos,
3580 it->region_end_charpos,
3581 &next_check_charpos,
3582 limit, 0);
3584 /* Correct the face for charsets different from ASCII. Do it
3585 for the multibyte case only. The face returned above is
3586 suitable for unibyte text if current_buffer is unibyte. */
3587 if (it->multibyte_p)
3589 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3590 struct face *face = FACE_FROM_ID (it->f, face_id);
3591 face_id = FACE_FOR_CHAR (it->f, face, c);
3595 return face_id;
3600 /***********************************************************************
3601 Invisible text
3602 ***********************************************************************/
3604 /* Set up iterator IT from invisible properties at its current
3605 position. Called from handle_stop. */
3607 static enum prop_handled
3608 handle_invisible_prop (it)
3609 struct it *it;
3611 enum prop_handled handled = HANDLED_NORMALLY;
3613 if (STRINGP (it->string))
3615 extern Lisp_Object Qinvisible;
3616 Lisp_Object prop, end_charpos, limit, charpos;
3618 /* Get the value of the invisible text property at the
3619 current position. Value will be nil if there is no such
3620 property. */
3621 charpos = make_number (IT_STRING_CHARPOS (*it));
3622 prop = Fget_text_property (charpos, Qinvisible, it->string);
3624 if (!NILP (prop)
3625 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3627 handled = HANDLED_RECOMPUTE_PROPS;
3629 /* Get the position at which the next change of the
3630 invisible text property can be found in IT->string.
3631 Value will be nil if the property value is the same for
3632 all the rest of IT->string. */
3633 XSETINT (limit, SCHARS (it->string));
3634 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3635 it->string, limit);
3637 /* Text at current position is invisible. The next
3638 change in the property is at position end_charpos.
3639 Move IT's current position to that position. */
3640 if (INTEGERP (end_charpos)
3641 && XFASTINT (end_charpos) < XFASTINT (limit))
3643 struct text_pos old;
3644 old = it->current.string_pos;
3645 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3646 compute_string_pos (&it->current.string_pos, old, it->string);
3648 else
3650 /* The rest of the string is invisible. If this is an
3651 overlay string, proceed with the next overlay string
3652 or whatever comes and return a character from there. */
3653 if (it->current.overlay_string_index >= 0)
3655 next_overlay_string (it);
3656 /* Don't check for overlay strings when we just
3657 finished processing them. */
3658 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3660 else
3662 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3663 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3668 else
3670 int invis_p, newpos, next_stop, start_charpos;
3671 Lisp_Object pos, prop, overlay;
3673 /* First of all, is there invisible text at this position? */
3674 start_charpos = IT_CHARPOS (*it);
3675 pos = make_number (IT_CHARPOS (*it));
3676 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3677 &overlay);
3678 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3680 /* If we are on invisible text, skip over it. */
3681 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3683 /* Record whether we have to display an ellipsis for the
3684 invisible text. */
3685 int display_ellipsis_p = invis_p == 2;
3687 handled = HANDLED_RECOMPUTE_PROPS;
3689 /* Loop skipping over invisible text. The loop is left at
3690 ZV or with IT on the first char being visible again. */
3693 /* Try to skip some invisible text. Return value is the
3694 position reached which can be equal to IT's position
3695 if there is nothing invisible here. This skips both
3696 over invisible text properties and overlays with
3697 invisible property. */
3698 newpos = skip_invisible (IT_CHARPOS (*it),
3699 &next_stop, ZV, it->window);
3701 /* If we skipped nothing at all we weren't at invisible
3702 text in the first place. If everything to the end of
3703 the buffer was skipped, end the loop. */
3704 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3705 invis_p = 0;
3706 else
3708 /* We skipped some characters but not necessarily
3709 all there are. Check if we ended up on visible
3710 text. Fget_char_property returns the property of
3711 the char before the given position, i.e. if we
3712 get invis_p = 0, this means that the char at
3713 newpos is visible. */
3714 pos = make_number (newpos);
3715 prop = Fget_char_property (pos, Qinvisible, it->window);
3716 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3719 /* If we ended up on invisible text, proceed to
3720 skip starting with next_stop. */
3721 if (invis_p)
3722 IT_CHARPOS (*it) = next_stop;
3724 /* If there are adjacent invisible texts, don't lose the
3725 second one's ellipsis. */
3726 if (invis_p == 2)
3727 display_ellipsis_p = 1;
3729 while (invis_p);
3731 /* The position newpos is now either ZV or on visible text. */
3732 IT_CHARPOS (*it) = newpos;
3733 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3735 /* If there are before-strings at the start of invisible
3736 text, and the text is invisible because of a text
3737 property, arrange to show before-strings because 20.x did
3738 it that way. (If the text is invisible because of an
3739 overlay property instead of a text property, this is
3740 already handled in the overlay code.) */
3741 if (NILP (overlay)
3742 && get_overlay_strings (it, start_charpos))
3744 handled = HANDLED_RECOMPUTE_PROPS;
3745 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3747 else if (display_ellipsis_p)
3749 /* Make sure that the glyphs of the ellipsis will get
3750 correct `charpos' values. If we would not update
3751 it->position here, the glyphs would belong to the
3752 last visible character _before_ the invisible
3753 text, which confuses `set_cursor_from_row'.
3755 We use the last invisible position instead of the
3756 first because this way the cursor is always drawn on
3757 the first "." of the ellipsis, whenever PT is inside
3758 the invisible text. Otherwise the cursor would be
3759 placed _after_ the ellipsis when the point is after the
3760 first invisible character. */
3761 if (!STRINGP (it->object))
3763 it->position.charpos = IT_CHARPOS (*it) - 1;
3764 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3766 setup_for_ellipsis (it, 0);
3767 /* Let the ellipsis display before
3768 considering any properties of the following char.
3769 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3770 handled = HANDLED_RETURN;
3775 return handled;
3779 /* Make iterator IT return `...' next.
3780 Replaces LEN characters from buffer. */
3782 static void
3783 setup_for_ellipsis (it, len)
3784 struct it *it;
3785 int len;
3787 /* Use the display table definition for `...'. Invalid glyphs
3788 will be handled by the method returning elements from dpvec. */
3789 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3791 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3792 it->dpvec = v->contents;
3793 it->dpend = v->contents + v->size;
3795 else
3797 /* Default `...'. */
3798 it->dpvec = default_invis_vector;
3799 it->dpend = default_invis_vector + 3;
3802 it->dpvec_char_len = len;
3803 it->current.dpvec_index = 0;
3804 it->dpvec_face_id = -1;
3806 /* Remember the current face id in case glyphs specify faces.
3807 IT's face is restored in set_iterator_to_next.
3808 saved_face_id was set to preceding char's face in handle_stop. */
3809 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3810 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3812 it->method = GET_FROM_DISPLAY_VECTOR;
3813 it->ellipsis_p = 1;
3818 /***********************************************************************
3819 'display' property
3820 ***********************************************************************/
3822 /* Set up iterator IT from `display' property at its current position.
3823 Called from handle_stop.
3824 We return HANDLED_RETURN if some part of the display property
3825 overrides the display of the buffer text itself.
3826 Otherwise we return HANDLED_NORMALLY. */
3828 static enum prop_handled
3829 handle_display_prop (it)
3830 struct it *it;
3832 Lisp_Object prop, object, overlay;
3833 struct text_pos *position;
3834 /* Nonzero if some property replaces the display of the text itself. */
3835 int display_replaced_p = 0;
3837 if (STRINGP (it->string))
3839 object = it->string;
3840 position = &it->current.string_pos;
3842 else
3844 XSETWINDOW (object, it->w);
3845 position = &it->current.pos;
3848 /* Reset those iterator values set from display property values. */
3849 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3850 it->space_width = Qnil;
3851 it->font_height = Qnil;
3852 it->voffset = 0;
3854 /* We don't support recursive `display' properties, i.e. string
3855 values that have a string `display' property, that have a string
3856 `display' property etc. */
3857 if (!it->string_from_display_prop_p)
3858 it->area = TEXT_AREA;
3860 prop = get_char_property_and_overlay (make_number (position->charpos),
3861 Qdisplay, object, &overlay);
3862 if (NILP (prop))
3863 return HANDLED_NORMALLY;
3864 /* Now OVERLAY is the overlay that gave us this property, or nil
3865 if it was a text property. */
3867 if (!STRINGP (it->string))
3868 object = it->w->buffer;
3870 if (CONSP (prop)
3871 /* Simple properties. */
3872 && !EQ (XCAR (prop), Qimage)
3873 && !EQ (XCAR (prop), Qspace)
3874 && !EQ (XCAR (prop), Qwhen)
3875 && !EQ (XCAR (prop), Qslice)
3876 && !EQ (XCAR (prop), Qspace_width)
3877 && !EQ (XCAR (prop), Qheight)
3878 && !EQ (XCAR (prop), Qraise)
3879 /* Marginal area specifications. */
3880 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3881 && !EQ (XCAR (prop), Qleft_fringe)
3882 && !EQ (XCAR (prop), Qright_fringe)
3883 && !NILP (XCAR (prop)))
3885 for (; CONSP (prop); prop = XCDR (prop))
3887 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3888 position, display_replaced_p))
3890 display_replaced_p = 1;
3891 /* If some text in a string is replaced, `position' no
3892 longer points to the position of `object'. */
3893 if (STRINGP (object))
3894 break;
3898 else if (VECTORP (prop))
3900 int i;
3901 for (i = 0; i < ASIZE (prop); ++i)
3902 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3903 position, display_replaced_p))
3905 display_replaced_p = 1;
3906 /* If some text in a string is replaced, `position' no
3907 longer points to the position of `object'. */
3908 if (STRINGP (object))
3909 break;
3912 else
3914 int ret = handle_single_display_spec (it, prop, object, overlay,
3915 position, 0);
3916 if (ret < 0) /* Replaced by "", i.e. nothing. */
3917 return HANDLED_RECOMPUTE_PROPS;
3918 if (ret)
3919 display_replaced_p = 1;
3922 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3926 /* Value is the position of the end of the `display' property starting
3927 at START_POS in OBJECT. */
3929 static struct text_pos
3930 display_prop_end (it, object, start_pos)
3931 struct it *it;
3932 Lisp_Object object;
3933 struct text_pos start_pos;
3935 Lisp_Object end;
3936 struct text_pos end_pos;
3938 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3939 Qdisplay, object, Qnil);
3940 CHARPOS (end_pos) = XFASTINT (end);
3941 if (STRINGP (object))
3942 compute_string_pos (&end_pos, start_pos, it->string);
3943 else
3944 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3946 return end_pos;
3950 /* Set up IT from a single `display' specification PROP. OBJECT
3951 is the object in which the `display' property was found. *POSITION
3952 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3953 means that we previously saw a display specification which already
3954 replaced text display with something else, for example an image;
3955 we ignore such properties after the first one has been processed.
3957 OVERLAY is the overlay this `display' property came from,
3958 or nil if it was a text property.
3960 If PROP is a `space' or `image' specification, and in some other
3961 cases too, set *POSITION to the position where the `display'
3962 property ends.
3964 Value is non-zero if something was found which replaces the display
3965 of buffer or string text. Specifically, the value is -1 if that
3966 "something" is "nothing". */
3968 static int
3969 handle_single_display_spec (it, spec, object, overlay, position,
3970 display_replaced_before_p)
3971 struct it *it;
3972 Lisp_Object spec;
3973 Lisp_Object object;
3974 Lisp_Object overlay;
3975 struct text_pos *position;
3976 int display_replaced_before_p;
3978 Lisp_Object form;
3979 Lisp_Object location, value;
3980 struct text_pos start_pos, save_pos;
3981 int valid_p;
3983 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3984 If the result is non-nil, use VALUE instead of SPEC. */
3985 form = Qt;
3986 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3988 spec = XCDR (spec);
3989 if (!CONSP (spec))
3990 return 0;
3991 form = XCAR (spec);
3992 spec = XCDR (spec);
3995 if (!NILP (form) && !EQ (form, Qt))
3997 int count = SPECPDL_INDEX ();
3998 struct gcpro gcpro1;
4000 /* Bind `object' to the object having the `display' property, a
4001 buffer or string. Bind `position' to the position in the
4002 object where the property was found, and `buffer-position'
4003 to the current position in the buffer. */
4004 specbind (Qobject, object);
4005 specbind (Qposition, make_number (CHARPOS (*position)));
4006 specbind (Qbuffer_position,
4007 make_number (STRINGP (object)
4008 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4009 GCPRO1 (form);
4010 form = safe_eval (form);
4011 UNGCPRO;
4012 unbind_to (count, Qnil);
4015 if (NILP (form))
4016 return 0;
4018 /* Handle `(height HEIGHT)' specifications. */
4019 if (CONSP (spec)
4020 && EQ (XCAR (spec), Qheight)
4021 && CONSP (XCDR (spec)))
4023 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4024 return 0;
4026 it->font_height = XCAR (XCDR (spec));
4027 if (!NILP (it->font_height))
4029 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4030 int new_height = -1;
4032 if (CONSP (it->font_height)
4033 && (EQ (XCAR (it->font_height), Qplus)
4034 || EQ (XCAR (it->font_height), Qminus))
4035 && CONSP (XCDR (it->font_height))
4036 && INTEGERP (XCAR (XCDR (it->font_height))))
4038 /* `(+ N)' or `(- N)' where N is an integer. */
4039 int steps = XINT (XCAR (XCDR (it->font_height)));
4040 if (EQ (XCAR (it->font_height), Qplus))
4041 steps = - steps;
4042 it->face_id = smaller_face (it->f, it->face_id, steps);
4044 else if (FUNCTIONP (it->font_height))
4046 /* Call function with current height as argument.
4047 Value is the new height. */
4048 Lisp_Object height;
4049 height = safe_call1 (it->font_height,
4050 face->lface[LFACE_HEIGHT_INDEX]);
4051 if (NUMBERP (height))
4052 new_height = XFLOATINT (height);
4054 else if (NUMBERP (it->font_height))
4056 /* Value is a multiple of the canonical char height. */
4057 struct face *face;
4059 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4060 new_height = (XFLOATINT (it->font_height)
4061 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4063 else
4065 /* Evaluate IT->font_height with `height' bound to the
4066 current specified height to get the new height. */
4067 int count = SPECPDL_INDEX ();
4069 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4070 value = safe_eval (it->font_height);
4071 unbind_to (count, Qnil);
4073 if (NUMBERP (value))
4074 new_height = XFLOATINT (value);
4077 if (new_height > 0)
4078 it->face_id = face_with_height (it->f, it->face_id, new_height);
4081 return 0;
4084 /* Handle `(space-width WIDTH)'. */
4085 if (CONSP (spec)
4086 && EQ (XCAR (spec), Qspace_width)
4087 && CONSP (XCDR (spec)))
4089 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4090 return 0;
4092 value = XCAR (XCDR (spec));
4093 if (NUMBERP (value) && XFLOATINT (value) > 0)
4094 it->space_width = value;
4096 return 0;
4099 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4100 if (CONSP (spec)
4101 && EQ (XCAR (spec), Qslice))
4103 Lisp_Object tem;
4105 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4106 return 0;
4108 if (tem = XCDR (spec), CONSP (tem))
4110 it->slice.x = XCAR (tem);
4111 if (tem = XCDR (tem), CONSP (tem))
4113 it->slice.y = XCAR (tem);
4114 if (tem = XCDR (tem), CONSP (tem))
4116 it->slice.width = XCAR (tem);
4117 if (tem = XCDR (tem), CONSP (tem))
4118 it->slice.height = XCAR (tem);
4123 return 0;
4126 /* Handle `(raise FACTOR)'. */
4127 if (CONSP (spec)
4128 && EQ (XCAR (spec), Qraise)
4129 && CONSP (XCDR (spec)))
4131 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4132 return 0;
4134 #ifdef HAVE_WINDOW_SYSTEM
4135 value = XCAR (XCDR (spec));
4136 if (NUMBERP (value))
4138 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4139 it->voffset = - (XFLOATINT (value)
4140 * (FONT_HEIGHT (face->font)));
4142 #endif /* HAVE_WINDOW_SYSTEM */
4144 return 0;
4147 /* Don't handle the other kinds of display specifications
4148 inside a string that we got from a `display' property. */
4149 if (it->string_from_display_prop_p)
4150 return 0;
4152 /* Characters having this form of property are not displayed, so
4153 we have to find the end of the property. */
4154 start_pos = *position;
4155 *position = display_prop_end (it, object, start_pos);
4156 value = Qnil;
4158 /* Stop the scan at that end position--we assume that all
4159 text properties change there. */
4160 it->stop_charpos = position->charpos;
4162 /* Handle `(left-fringe BITMAP [FACE])'
4163 and `(right-fringe BITMAP [FACE])'. */
4164 if (CONSP (spec)
4165 && (EQ (XCAR (spec), Qleft_fringe)
4166 || EQ (XCAR (spec), Qright_fringe))
4167 && CONSP (XCDR (spec)))
4169 int face_id = DEFAULT_FACE_ID;
4170 int fringe_bitmap;
4172 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4173 /* If we return here, POSITION has been advanced
4174 across the text with this property. */
4175 return 0;
4177 #ifdef HAVE_WINDOW_SYSTEM
4178 value = XCAR (XCDR (spec));
4179 if (!SYMBOLP (value)
4180 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4181 /* If we return here, POSITION has been advanced
4182 across the text with this property. */
4183 return 0;
4185 if (CONSP (XCDR (XCDR (spec))))
4187 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4188 int face_id2 = lookup_derived_face (it->f, face_name,
4189 'A', FRINGE_FACE_ID, 0);
4190 if (face_id2 >= 0)
4191 face_id = face_id2;
4194 /* Save current settings of IT so that we can restore them
4195 when we are finished with the glyph property value. */
4197 save_pos = it->position;
4198 it->position = *position;
4199 push_it (it);
4200 it->position = save_pos;
4202 it->area = TEXT_AREA;
4203 it->what = IT_IMAGE;
4204 it->image_id = -1; /* no image */
4205 it->position = start_pos;
4206 it->object = NILP (object) ? it->w->buffer : object;
4207 it->method = GET_FROM_IMAGE;
4208 it->from_overlay = Qnil;
4209 it->face_id = face_id;
4211 /* Say that we haven't consumed the characters with
4212 `display' property yet. The call to pop_it in
4213 set_iterator_to_next will clean this up. */
4214 *position = start_pos;
4216 if (EQ (XCAR (spec), Qleft_fringe))
4218 it->left_user_fringe_bitmap = fringe_bitmap;
4219 it->left_user_fringe_face_id = face_id;
4221 else
4223 it->right_user_fringe_bitmap = fringe_bitmap;
4224 it->right_user_fringe_face_id = face_id;
4226 #endif /* HAVE_WINDOW_SYSTEM */
4227 return 1;
4230 /* Prepare to handle `((margin left-margin) ...)',
4231 `((margin right-margin) ...)' and `((margin nil) ...)'
4232 prefixes for display specifications. */
4233 location = Qunbound;
4234 if (CONSP (spec) && CONSP (XCAR (spec)))
4236 Lisp_Object tem;
4238 value = XCDR (spec);
4239 if (CONSP (value))
4240 value = XCAR (value);
4242 tem = XCAR (spec);
4243 if (EQ (XCAR (tem), Qmargin)
4244 && (tem = XCDR (tem),
4245 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4246 (NILP (tem)
4247 || EQ (tem, Qleft_margin)
4248 || EQ (tem, Qright_margin))))
4249 location = tem;
4252 if (EQ (location, Qunbound))
4254 location = Qnil;
4255 value = spec;
4258 /* After this point, VALUE is the property after any
4259 margin prefix has been stripped. It must be a string,
4260 an image specification, or `(space ...)'.
4262 LOCATION specifies where to display: `left-margin',
4263 `right-margin' or nil. */
4265 valid_p = (STRINGP (value)
4266 #ifdef HAVE_WINDOW_SYSTEM
4267 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4268 #endif /* not HAVE_WINDOW_SYSTEM */
4269 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4271 if (valid_p && !display_replaced_before_p)
4273 /* Save current settings of IT so that we can restore them
4274 when we are finished with the glyph property value. */
4275 save_pos = it->position;
4276 it->position = *position;
4277 push_it (it);
4278 it->position = save_pos;
4279 it->from_overlay = overlay;
4281 if (NILP (location))
4282 it->area = TEXT_AREA;
4283 else if (EQ (location, Qleft_margin))
4284 it->area = LEFT_MARGIN_AREA;
4285 else
4286 it->area = RIGHT_MARGIN_AREA;
4288 if (STRINGP (value))
4290 if (SCHARS (value) == 0)
4292 pop_it (it);
4293 return -1; /* Replaced by "", i.e. nothing. */
4295 it->string = value;
4296 it->multibyte_p = STRING_MULTIBYTE (it->string);
4297 it->current.overlay_string_index = -1;
4298 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4299 it->end_charpos = it->string_nchars = SCHARS (it->string);
4300 it->method = GET_FROM_STRING;
4301 it->stop_charpos = 0;
4302 it->string_from_display_prop_p = 1;
4303 /* Say that we haven't consumed the characters with
4304 `display' property yet. The call to pop_it in
4305 set_iterator_to_next will clean this up. */
4306 if (BUFFERP (object))
4307 *position = start_pos;
4309 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4311 it->method = GET_FROM_STRETCH;
4312 it->object = value;
4313 *position = it->position = start_pos;
4315 #ifdef HAVE_WINDOW_SYSTEM
4316 else
4318 it->what = IT_IMAGE;
4319 it->image_id = lookup_image (it->f, value);
4320 it->position = start_pos;
4321 it->object = NILP (object) ? it->w->buffer : object;
4322 it->method = GET_FROM_IMAGE;
4324 /* Say that we haven't consumed the characters with
4325 `display' property yet. The call to pop_it in
4326 set_iterator_to_next will clean this up. */
4327 *position = 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 (NILP (it->string) && 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;
4937 int compute_stop_p;
4939 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4940 process. This fills IT->overlay_strings with strings, and sets
4941 IT->n_overlay_strings to the total number of strings to process.
4942 IT->pos.overlay_string_index has to be set temporarily to zero
4943 because load_overlay_strings needs this; it must be set to -1
4944 when no overlay strings are found because a zero value would
4945 indicate a position in the first overlay string. */
4946 it->current.overlay_string_index = 0;
4947 load_overlay_strings (it, charpos);
4949 /* If we found overlay strings, set up IT to deliver display
4950 elements from the first one. Otherwise set up IT to deliver
4951 from current_buffer. */
4952 if (it->n_overlay_strings)
4954 /* Make sure we know settings in current_buffer, so that we can
4955 restore meaningful values when we're done with the overlay
4956 strings. */
4957 if (compute_stop_p)
4958 compute_stop_pos (it);
4959 xassert (it->face_id >= 0);
4961 /* Save IT's settings. They are restored after all overlay
4962 strings have been processed. */
4963 xassert (!compute_stop_p || it->sp == 0);
4964 push_it (it);
4966 /* Set up IT to deliver display elements from the first overlay
4967 string. */
4968 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4969 it->string = it->overlay_strings[0];
4970 it->from_overlay = Qnil;
4971 it->stop_charpos = 0;
4972 xassert (STRINGP (it->string));
4973 it->end_charpos = SCHARS (it->string);
4974 it->multibyte_p = STRING_MULTIBYTE (it->string);
4975 it->method = GET_FROM_STRING;
4976 return 1;
4979 it->current.overlay_string_index = -1;
4980 return 0;
4983 static int
4984 get_overlay_strings (it, charpos)
4985 struct it *it;
4986 int charpos;
4988 it->string = Qnil;
4989 it->method = GET_FROM_BUFFER;
4991 (void) get_overlay_strings_1 (it, charpos, 1);
4993 CHECK_IT (it);
4995 /* Value is non-zero if we found at least one overlay string. */
4996 return STRINGP (it->string);
5001 /***********************************************************************
5002 Saving and restoring state
5003 ***********************************************************************/
5005 /* Save current settings of IT on IT->stack. Called, for example,
5006 before setting up IT for an overlay string, to be able to restore
5007 IT's settings to what they were after the overlay string has been
5008 processed. */
5010 static void
5011 push_it (it)
5012 struct it *it;
5014 struct iterator_stack_entry *p;
5016 xassert (it->sp < IT_STACK_SIZE);
5017 p = it->stack + it->sp;
5019 p->stop_charpos = it->stop_charpos;
5020 xassert (it->face_id >= 0);
5021 p->face_id = it->face_id;
5022 p->string = it->string;
5023 p->method = it->method;
5024 p->from_overlay = it->from_overlay;
5025 switch (p->method)
5027 case GET_FROM_IMAGE:
5028 p->u.image.object = it->object;
5029 p->u.image.image_id = it->image_id;
5030 p->u.image.slice = it->slice;
5031 break;
5032 case GET_FROM_COMPOSITION:
5033 p->u.comp.object = it->object;
5034 p->u.comp.c = it->c;
5035 p->u.comp.len = it->len;
5036 p->u.comp.cmp_id = it->cmp_id;
5037 p->u.comp.cmp_len = it->cmp_len;
5038 break;
5039 case GET_FROM_STRETCH:
5040 p->u.stretch.object = it->object;
5041 break;
5043 p->position = it->position;
5044 p->current = it->current;
5045 p->end_charpos = it->end_charpos;
5046 p->string_nchars = it->string_nchars;
5047 p->area = it->area;
5048 p->multibyte_p = it->multibyte_p;
5049 p->space_width = it->space_width;
5050 p->font_height = it->font_height;
5051 p->voffset = it->voffset;
5052 p->string_from_display_prop_p = it->string_from_display_prop_p;
5053 p->display_ellipsis_p = 0;
5054 ++it->sp;
5058 /* Restore IT's settings from IT->stack. Called, for example, when no
5059 more overlay strings must be processed, and we return to delivering
5060 display elements from a buffer, or when the end of a string from a
5061 `display' property is reached and we return to delivering display
5062 elements from an overlay string, or from a buffer. */
5064 static void
5065 pop_it (it)
5066 struct it *it;
5068 struct iterator_stack_entry *p;
5070 xassert (it->sp > 0);
5071 --it->sp;
5072 p = it->stack + it->sp;
5073 it->stop_charpos = p->stop_charpos;
5074 it->face_id = p->face_id;
5075 it->current = p->current;
5076 it->position = p->position;
5077 it->string = p->string;
5078 it->from_overlay = p->from_overlay;
5079 if (NILP (it->string))
5080 SET_TEXT_POS (it->current.string_pos, -1, -1);
5081 it->method = p->method;
5082 switch (it->method)
5084 case GET_FROM_IMAGE:
5085 it->image_id = p->u.image.image_id;
5086 it->object = p->u.image.object;
5087 it->slice = p->u.image.slice;
5088 break;
5089 case GET_FROM_COMPOSITION:
5090 it->object = p->u.comp.object;
5091 it->c = p->u.comp.c;
5092 it->len = p->u.comp.len;
5093 it->cmp_id = p->u.comp.cmp_id;
5094 it->cmp_len = p->u.comp.cmp_len;
5095 break;
5096 case GET_FROM_STRETCH:
5097 it->object = p->u.comp.object;
5098 break;
5099 case GET_FROM_BUFFER:
5100 it->object = it->w->buffer;
5101 break;
5102 case GET_FROM_STRING:
5103 it->object = it->string;
5104 break;
5106 it->end_charpos = p->end_charpos;
5107 it->string_nchars = p->string_nchars;
5108 it->area = p->area;
5109 it->multibyte_p = p->multibyte_p;
5110 it->space_width = p->space_width;
5111 it->font_height = p->font_height;
5112 it->voffset = p->voffset;
5113 it->string_from_display_prop_p = p->string_from_display_prop_p;
5118 /***********************************************************************
5119 Moving over lines
5120 ***********************************************************************/
5122 /* Set IT's current position to the previous line start. */
5124 static void
5125 back_to_previous_line_start (it)
5126 struct it *it;
5128 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5129 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5133 /* Move IT to the next line start.
5135 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5136 we skipped over part of the text (as opposed to moving the iterator
5137 continuously over the text). Otherwise, don't change the value
5138 of *SKIPPED_P.
5140 Newlines may come from buffer text, overlay strings, or strings
5141 displayed via the `display' property. That's the reason we can't
5142 simply use find_next_newline_no_quit.
5144 Note that this function may not skip over invisible text that is so
5145 because of text properties and immediately follows a newline. If
5146 it would, function reseat_at_next_visible_line_start, when called
5147 from set_iterator_to_next, would effectively make invisible
5148 characters following a newline part of the wrong glyph row, which
5149 leads to wrong cursor motion. */
5151 static int
5152 forward_to_next_line_start (it, skipped_p)
5153 struct it *it;
5154 int *skipped_p;
5156 int old_selective, newline_found_p, n;
5157 const int MAX_NEWLINE_DISTANCE = 500;
5159 /* If already on a newline, just consume it to avoid unintended
5160 skipping over invisible text below. */
5161 if (it->what == IT_CHARACTER
5162 && it->c == '\n'
5163 && CHARPOS (it->position) == IT_CHARPOS (*it))
5165 set_iterator_to_next (it, 0);
5166 it->c = 0;
5167 return 1;
5170 /* Don't handle selective display in the following. It's (a)
5171 unnecessary because it's done by the caller, and (b) leads to an
5172 infinite recursion because next_element_from_ellipsis indirectly
5173 calls this function. */
5174 old_selective = it->selective;
5175 it->selective = 0;
5177 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5178 from buffer text. */
5179 for (n = newline_found_p = 0;
5180 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5181 n += STRINGP (it->string) ? 0 : 1)
5183 if (!get_next_display_element (it))
5184 return 0;
5185 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5186 set_iterator_to_next (it, 0);
5189 /* If we didn't find a newline near enough, see if we can use a
5190 short-cut. */
5191 if (!newline_found_p)
5193 int start = IT_CHARPOS (*it);
5194 int limit = find_next_newline_no_quit (start, 1);
5195 Lisp_Object pos;
5197 xassert (!STRINGP (it->string));
5199 /* If there isn't any `display' property in sight, and no
5200 overlays, we can just use the position of the newline in
5201 buffer text. */
5202 if (it->stop_charpos >= limit
5203 || ((pos = Fnext_single_property_change (make_number (start),
5204 Qdisplay,
5205 Qnil, make_number (limit)),
5206 NILP (pos))
5207 && next_overlay_change (start) == ZV))
5209 IT_CHARPOS (*it) = limit;
5210 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5211 *skipped_p = newline_found_p = 1;
5213 else
5215 while (get_next_display_element (it)
5216 && !newline_found_p)
5218 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5219 set_iterator_to_next (it, 0);
5224 it->selective = old_selective;
5225 return newline_found_p;
5229 /* Set IT's current position to the previous visible line start. Skip
5230 invisible text that is so either due to text properties or due to
5231 selective display. Caution: this does not change IT->current_x and
5232 IT->hpos. */
5234 static void
5235 back_to_previous_visible_line_start (it)
5236 struct it *it;
5238 while (IT_CHARPOS (*it) > BEGV)
5240 back_to_previous_line_start (it);
5242 if (IT_CHARPOS (*it) <= BEGV)
5243 break;
5245 /* If selective > 0, then lines indented more than that values
5246 are invisible. */
5247 if (it->selective > 0
5248 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5249 (double) it->selective)) /* iftc */
5250 continue;
5252 /* Check the newline before point for invisibility. */
5254 Lisp_Object prop;
5255 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5256 Qinvisible, it->window);
5257 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5258 continue;
5261 if (IT_CHARPOS (*it) <= BEGV)
5262 break;
5265 struct it it2;
5266 int pos;
5267 int beg, end;
5268 Lisp_Object val, overlay;
5270 /* If newline is part of a composition, continue from start of composition */
5271 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5272 && beg < IT_CHARPOS (*it))
5273 goto replaced;
5275 /* If newline is replaced by a display property, find start of overlay
5276 or interval and continue search from that point. */
5277 it2 = *it;
5278 pos = --IT_CHARPOS (it2);
5279 --IT_BYTEPOS (it2);
5280 it2.sp = 0;
5281 if (handle_display_prop (&it2) == HANDLED_RETURN
5282 && !NILP (val = get_char_property_and_overlay
5283 (make_number (pos), Qdisplay, Qnil, &overlay))
5284 && (OVERLAYP (overlay)
5285 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5286 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5287 goto replaced;
5289 /* Newline is not replaced by anything -- so we are done. */
5290 break;
5292 replaced:
5293 if (beg < BEGV)
5294 beg = BEGV;
5295 IT_CHARPOS (*it) = beg;
5296 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5300 it->continuation_lines_width = 0;
5302 xassert (IT_CHARPOS (*it) >= BEGV);
5303 xassert (IT_CHARPOS (*it) == BEGV
5304 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5305 CHECK_IT (it);
5309 /* Reseat iterator IT at the previous visible line start. Skip
5310 invisible text that is so either due to text properties or due to
5311 selective display. At the end, update IT's overlay information,
5312 face information etc. */
5314 void
5315 reseat_at_previous_visible_line_start (it)
5316 struct it *it;
5318 back_to_previous_visible_line_start (it);
5319 reseat (it, it->current.pos, 1);
5320 CHECK_IT (it);
5324 /* Reseat iterator IT on the next visible line start in the current
5325 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5326 preceding the line start. Skip over invisible text that is so
5327 because of selective display. Compute faces, overlays etc at the
5328 new position. Note that this function does not skip over text that
5329 is invisible because of text properties. */
5331 static void
5332 reseat_at_next_visible_line_start (it, on_newline_p)
5333 struct it *it;
5334 int on_newline_p;
5336 int newline_found_p, skipped_p = 0;
5338 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5340 /* Skip over lines that are invisible because they are indented
5341 more than the value of IT->selective. */
5342 if (it->selective > 0)
5343 while (IT_CHARPOS (*it) < ZV
5344 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5345 (double) it->selective)) /* iftc */
5347 xassert (IT_BYTEPOS (*it) == BEGV
5348 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5349 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5352 /* Position on the newline if that's what's requested. */
5353 if (on_newline_p && newline_found_p)
5355 if (STRINGP (it->string))
5357 if (IT_STRING_CHARPOS (*it) > 0)
5359 --IT_STRING_CHARPOS (*it);
5360 --IT_STRING_BYTEPOS (*it);
5363 else if (IT_CHARPOS (*it) > BEGV)
5365 --IT_CHARPOS (*it);
5366 --IT_BYTEPOS (*it);
5367 reseat (it, it->current.pos, 0);
5370 else if (skipped_p)
5371 reseat (it, it->current.pos, 0);
5373 CHECK_IT (it);
5378 /***********************************************************************
5379 Changing an iterator's position
5380 ***********************************************************************/
5382 /* Change IT's current position to POS in current_buffer. If FORCE_P
5383 is non-zero, always check for text properties at the new position.
5384 Otherwise, text properties are only looked up if POS >=
5385 IT->check_charpos of a property. */
5387 static void
5388 reseat (it, pos, force_p)
5389 struct it *it;
5390 struct text_pos pos;
5391 int force_p;
5393 int original_pos = IT_CHARPOS (*it);
5395 reseat_1 (it, pos, 0);
5397 /* Determine where to check text properties. Avoid doing it
5398 where possible because text property lookup is very expensive. */
5399 if (force_p
5400 || CHARPOS (pos) > it->stop_charpos
5401 || CHARPOS (pos) < original_pos)
5402 handle_stop (it);
5404 CHECK_IT (it);
5408 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5409 IT->stop_pos to POS, also. */
5411 static void
5412 reseat_1 (it, pos, set_stop_p)
5413 struct it *it;
5414 struct text_pos pos;
5415 int set_stop_p;
5417 /* Don't call this function when scanning a C string. */
5418 xassert (it->s == NULL);
5420 /* POS must be a reasonable value. */
5421 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5423 it->current.pos = it->position = pos;
5424 it->end_charpos = ZV;
5425 it->dpvec = NULL;
5426 it->current.dpvec_index = -1;
5427 it->current.overlay_string_index = -1;
5428 IT_STRING_CHARPOS (*it) = -1;
5429 IT_STRING_BYTEPOS (*it) = -1;
5430 it->string = Qnil;
5431 it->method = GET_FROM_BUFFER;
5432 it->object = it->w->buffer;
5433 it->area = TEXT_AREA;
5434 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5435 it->sp = 0;
5436 it->string_from_display_prop_p = 0;
5437 it->face_before_selective_p = 0;
5439 if (set_stop_p)
5440 it->stop_charpos = CHARPOS (pos);
5444 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5445 If S is non-null, it is a C string to iterate over. Otherwise,
5446 STRING gives a Lisp string to iterate over.
5448 If PRECISION > 0, don't return more then PRECISION number of
5449 characters from the string.
5451 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5452 characters have been returned. FIELD_WIDTH < 0 means an infinite
5453 field width.
5455 MULTIBYTE = 0 means disable processing of multibyte characters,
5456 MULTIBYTE > 0 means enable it,
5457 MULTIBYTE < 0 means use IT->multibyte_p.
5459 IT must be initialized via a prior call to init_iterator before
5460 calling this function. */
5462 static void
5463 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5464 struct it *it;
5465 unsigned char *s;
5466 Lisp_Object string;
5467 int charpos;
5468 int precision, field_width, multibyte;
5470 /* No region in strings. */
5471 it->region_beg_charpos = it->region_end_charpos = -1;
5473 /* No text property checks performed by default, but see below. */
5474 it->stop_charpos = -1;
5476 /* Set iterator position and end position. */
5477 bzero (&it->current, sizeof it->current);
5478 it->current.overlay_string_index = -1;
5479 it->current.dpvec_index = -1;
5480 xassert (charpos >= 0);
5482 /* If STRING is specified, use its multibyteness, otherwise use the
5483 setting of MULTIBYTE, if specified. */
5484 if (multibyte >= 0)
5485 it->multibyte_p = multibyte > 0;
5487 if (s == NULL)
5489 xassert (STRINGP (string));
5490 it->string = string;
5491 it->s = NULL;
5492 it->end_charpos = it->string_nchars = SCHARS (string);
5493 it->method = GET_FROM_STRING;
5494 it->current.string_pos = string_pos (charpos, string);
5496 else
5498 it->s = s;
5499 it->string = Qnil;
5501 /* Note that we use IT->current.pos, not it->current.string_pos,
5502 for displaying C strings. */
5503 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5504 if (it->multibyte_p)
5506 it->current.pos = c_string_pos (charpos, s, 1);
5507 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5509 else
5511 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5512 it->end_charpos = it->string_nchars = strlen (s);
5515 it->method = GET_FROM_C_STRING;
5518 /* PRECISION > 0 means don't return more than PRECISION characters
5519 from the string. */
5520 if (precision > 0 && it->end_charpos - charpos > precision)
5521 it->end_charpos = it->string_nchars = charpos + precision;
5523 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5524 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5525 FIELD_WIDTH < 0 means infinite field width. This is useful for
5526 padding with `-' at the end of a mode line. */
5527 if (field_width < 0)
5528 field_width = INFINITY;
5529 if (field_width > it->end_charpos - charpos)
5530 it->end_charpos = charpos + field_width;
5532 /* Use the standard display table for displaying strings. */
5533 if (DISP_TABLE_P (Vstandard_display_table))
5534 it->dp = XCHAR_TABLE (Vstandard_display_table);
5536 it->stop_charpos = charpos;
5537 CHECK_IT (it);
5542 /***********************************************************************
5543 Iteration
5544 ***********************************************************************/
5546 /* Map enum it_method value to corresponding next_element_from_* function. */
5548 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5550 next_element_from_buffer,
5551 next_element_from_display_vector,
5552 next_element_from_composition,
5553 next_element_from_string,
5554 next_element_from_c_string,
5555 next_element_from_image,
5556 next_element_from_stretch
5560 /* Load IT's display element fields with information about the next
5561 display element from the current position of IT. Value is zero if
5562 end of buffer (or C string) is reached. */
5564 static struct frame *last_escape_glyph_frame = NULL;
5565 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5566 static int last_escape_glyph_merged_face_id = 0;
5569 get_next_display_element (it)
5570 struct it *it;
5572 /* Non-zero means that we found a display element. Zero means that
5573 we hit the end of what we iterate over. Performance note: the
5574 function pointer `method' used here turns out to be faster than
5575 using a sequence of if-statements. */
5576 int success_p;
5578 get_next:
5579 success_p = (*get_next_element[it->method]) (it);
5581 if (it->what == IT_CHARACTER)
5583 /* Map via display table or translate control characters.
5584 IT->c, IT->len etc. have been set to the next character by
5585 the function call above. If we have a display table, and it
5586 contains an entry for IT->c, translate it. Don't do this if
5587 IT->c itself comes from a display table, otherwise we could
5588 end up in an infinite recursion. (An alternative could be to
5589 count the recursion depth of this function and signal an
5590 error when a certain maximum depth is reached.) Is it worth
5591 it? */
5592 if (success_p && it->dpvec == NULL)
5594 Lisp_Object dv;
5596 if (it->dp
5597 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5598 VECTORP (dv)))
5600 struct Lisp_Vector *v = XVECTOR (dv);
5602 /* Return the first character from the display table
5603 entry, if not empty. If empty, don't display the
5604 current character. */
5605 if (v->size)
5607 it->dpvec_char_len = it->len;
5608 it->dpvec = v->contents;
5609 it->dpend = v->contents + v->size;
5610 it->current.dpvec_index = 0;
5611 it->dpvec_face_id = -1;
5612 it->saved_face_id = it->face_id;
5613 it->method = GET_FROM_DISPLAY_VECTOR;
5614 it->ellipsis_p = 0;
5616 else
5618 set_iterator_to_next (it, 0);
5620 goto get_next;
5623 /* Translate control characters into `\003' or `^C' form.
5624 Control characters coming from a display table entry are
5625 currently not translated because we use IT->dpvec to hold
5626 the translation. This could easily be changed but I
5627 don't believe that it is worth doing.
5629 If it->multibyte_p is nonzero, eight-bit characters and
5630 non-printable multibyte characters are also translated to
5631 octal form.
5633 If it->multibyte_p is zero, eight-bit characters that
5634 don't have corresponding multibyte char code are also
5635 translated to octal form. */
5636 else if ((it->c < ' '
5637 && (it->area != TEXT_AREA
5638 /* In mode line, treat \n like other crl chars. */
5639 || (it->c != '\t'
5640 && it->glyph_row && it->glyph_row->mode_line_p)
5641 || (it->c != '\n' && it->c != '\t')))
5642 || (it->multibyte_p
5643 ? ((it->c >= 127
5644 && it->len == 1)
5645 || !CHAR_PRINTABLE_P (it->c)
5646 || (!NILP (Vnobreak_char_display)
5647 && (it->c == 0x8a0 || it->c == 0x8ad
5648 || it->c == 0x920 || it->c == 0x92d
5649 || it->c == 0xe20 || it->c == 0xe2d
5650 || it->c == 0xf20 || it->c == 0xf2d)))
5651 : (it->c >= 127
5652 && (!unibyte_display_via_language_environment
5653 || it->c == unibyte_char_to_multibyte (it->c)))))
5655 /* IT->c is a control character which must be displayed
5656 either as '\003' or as `^C' where the '\\' and '^'
5657 can be defined in the display table. Fill
5658 IT->ctl_chars with glyphs for what we have to
5659 display. Then, set IT->dpvec to these glyphs. */
5660 GLYPH g;
5661 int ctl_len;
5662 int face_id, lface_id = 0 ;
5663 GLYPH escape_glyph;
5665 /* Handle control characters with ^. */
5667 if (it->c < 128 && it->ctl_arrow_p)
5669 g = '^'; /* default glyph for Control */
5670 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5671 if (it->dp
5672 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5673 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5675 g = XINT (DISP_CTRL_GLYPH (it->dp));
5676 lface_id = FAST_GLYPH_FACE (g);
5678 if (lface_id)
5680 g = FAST_GLYPH_CHAR (g);
5681 face_id = merge_faces (it->f, Qt, lface_id,
5682 it->face_id);
5684 else if (it->f == last_escape_glyph_frame
5685 && it->face_id == last_escape_glyph_face_id)
5687 face_id = last_escape_glyph_merged_face_id;
5689 else
5691 /* Merge the escape-glyph face into the current face. */
5692 face_id = merge_faces (it->f, Qescape_glyph, 0,
5693 it->face_id);
5694 last_escape_glyph_frame = it->f;
5695 last_escape_glyph_face_id = it->face_id;
5696 last_escape_glyph_merged_face_id = face_id;
5699 XSETINT (it->ctl_chars[0], g);
5700 g = it->c ^ 0100;
5701 XSETINT (it->ctl_chars[1], g);
5702 ctl_len = 2;
5703 goto display_control;
5706 /* Handle non-break space in the mode where it only gets
5707 highlighting. */
5709 if (EQ (Vnobreak_char_display, Qt)
5710 && (it->c == 0x8a0 || it->c == 0x920
5711 || it->c == 0xe20 || it->c == 0xf20))
5713 /* Merge the no-break-space face into the current face. */
5714 face_id = merge_faces (it->f, Qnobreak_space, 0,
5715 it->face_id);
5717 g = it->c = ' ';
5718 XSETINT (it->ctl_chars[0], g);
5719 ctl_len = 1;
5720 goto display_control;
5723 /* Handle sequences that start with the "escape glyph". */
5725 /* the default escape glyph is \. */
5726 escape_glyph = '\\';
5728 if (it->dp
5729 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5730 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5732 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5733 lface_id = FAST_GLYPH_FACE (escape_glyph);
5735 if (lface_id)
5737 /* The display table specified a face.
5738 Merge it into face_id and also into escape_glyph. */
5739 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5740 face_id = merge_faces (it->f, Qt, lface_id,
5741 it->face_id);
5743 else if (it->f == last_escape_glyph_frame
5744 && it->face_id == last_escape_glyph_face_id)
5746 face_id = last_escape_glyph_merged_face_id;
5748 else
5750 /* Merge the escape-glyph face into the current face. */
5751 face_id = merge_faces (it->f, Qescape_glyph, 0,
5752 it->face_id);
5753 last_escape_glyph_frame = it->f;
5754 last_escape_glyph_face_id = it->face_id;
5755 last_escape_glyph_merged_face_id = face_id;
5758 /* Handle soft hyphens in the mode where they only get
5759 highlighting. */
5761 if (EQ (Vnobreak_char_display, Qt)
5762 && (it->c == 0x8ad || it->c == 0x92d
5763 || it->c == 0xe2d || it->c == 0xf2d))
5765 g = it->c = '-';
5766 XSETINT (it->ctl_chars[0], g);
5767 ctl_len = 1;
5768 goto display_control;
5771 /* Handle non-break space and soft hyphen
5772 with the escape glyph. */
5774 if (it->c == 0x8a0 || it->c == 0x8ad
5775 || it->c == 0x920 || it->c == 0x92d
5776 || it->c == 0xe20 || it->c == 0xe2d
5777 || it->c == 0xf20 || it->c == 0xf2d)
5779 XSETINT (it->ctl_chars[0], escape_glyph);
5780 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5781 XSETINT (it->ctl_chars[1], g);
5782 ctl_len = 2;
5783 goto display_control;
5787 unsigned char str[MAX_MULTIBYTE_LENGTH];
5788 int len;
5789 int i;
5791 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5792 if (SINGLE_BYTE_CHAR_P (it->c))
5793 str[0] = it->c, len = 1;
5794 else
5796 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5797 if (len < 0)
5799 /* It's an invalid character, which shouldn't
5800 happen actually, but due to bugs it may
5801 happen. Let's print the char as is, there's
5802 not much meaningful we can do with it. */
5803 str[0] = it->c;
5804 str[1] = it->c >> 8;
5805 str[2] = it->c >> 16;
5806 str[3] = it->c >> 24;
5807 len = 4;
5811 for (i = 0; i < len; i++)
5813 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5814 /* Insert three more glyphs into IT->ctl_chars for
5815 the octal display of the character. */
5816 g = ((str[i] >> 6) & 7) + '0';
5817 XSETINT (it->ctl_chars[i * 4 + 1], g);
5818 g = ((str[i] >> 3) & 7) + '0';
5819 XSETINT (it->ctl_chars[i * 4 + 2], g);
5820 g = (str[i] & 7) + '0';
5821 XSETINT (it->ctl_chars[i * 4 + 3], g);
5823 ctl_len = len * 4;
5826 display_control:
5827 /* Set up IT->dpvec and return first character from it. */
5828 it->dpvec_char_len = it->len;
5829 it->dpvec = it->ctl_chars;
5830 it->dpend = it->dpvec + ctl_len;
5831 it->current.dpvec_index = 0;
5832 it->dpvec_face_id = face_id;
5833 it->saved_face_id = it->face_id;
5834 it->method = GET_FROM_DISPLAY_VECTOR;
5835 it->ellipsis_p = 0;
5836 goto get_next;
5840 /* Adjust face id for a multibyte character. There are no
5841 multibyte character in unibyte text. */
5842 if (it->multibyte_p
5843 && success_p
5844 && FRAME_WINDOW_P (it->f))
5846 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5847 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5851 /* Is this character the last one of a run of characters with
5852 box? If yes, set IT->end_of_box_run_p to 1. */
5853 if (it->face_box_p
5854 && it->s == NULL)
5856 int face_id;
5857 struct face *face;
5859 it->end_of_box_run_p
5860 = ((face_id = face_after_it_pos (it),
5861 face_id != it->face_id)
5862 && (face = FACE_FROM_ID (it->f, face_id),
5863 face->box == FACE_NO_BOX));
5866 /* Value is 0 if end of buffer or string reached. */
5867 return success_p;
5871 /* Move IT to the next display element.
5873 RESEAT_P non-zero means if called on a newline in buffer text,
5874 skip to the next visible line start.
5876 Functions get_next_display_element and set_iterator_to_next are
5877 separate because I find this arrangement easier to handle than a
5878 get_next_display_element function that also increments IT's
5879 position. The way it is we can first look at an iterator's current
5880 display element, decide whether it fits on a line, and if it does,
5881 increment the iterator position. The other way around we probably
5882 would either need a flag indicating whether the iterator has to be
5883 incremented the next time, or we would have to implement a
5884 decrement position function which would not be easy to write. */
5886 void
5887 set_iterator_to_next (it, reseat_p)
5888 struct it *it;
5889 int reseat_p;
5891 /* Reset flags indicating start and end of a sequence of characters
5892 with box. Reset them at the start of this function because
5893 moving the iterator to a new position might set them. */
5894 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5896 switch (it->method)
5898 case GET_FROM_BUFFER:
5899 /* The current display element of IT is a character from
5900 current_buffer. Advance in the buffer, and maybe skip over
5901 invisible lines that are so because of selective display. */
5902 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5903 reseat_at_next_visible_line_start (it, 0);
5904 else
5906 xassert (it->len != 0);
5907 IT_BYTEPOS (*it) += it->len;
5908 IT_CHARPOS (*it) += 1;
5909 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5911 break;
5913 case GET_FROM_COMPOSITION:
5914 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5915 xassert (it->sp > 0);
5916 pop_it (it);
5917 if (it->method == GET_FROM_STRING)
5919 IT_STRING_BYTEPOS (*it) += it->len;
5920 IT_STRING_CHARPOS (*it) += it->cmp_len;
5921 goto consider_string_end;
5923 else if (it->method == GET_FROM_BUFFER)
5925 IT_BYTEPOS (*it) += it->len;
5926 IT_CHARPOS (*it) += it->cmp_len;
5928 break;
5930 case GET_FROM_C_STRING:
5931 /* Current display element of IT is from a C string. */
5932 IT_BYTEPOS (*it) += it->len;
5933 IT_CHARPOS (*it) += 1;
5934 break;
5936 case GET_FROM_DISPLAY_VECTOR:
5937 /* Current display element of IT is from a display table entry.
5938 Advance in the display table definition. Reset it to null if
5939 end reached, and continue with characters from buffers/
5940 strings. */
5941 ++it->current.dpvec_index;
5943 /* Restore face of the iterator to what they were before the
5944 display vector entry (these entries may contain faces). */
5945 it->face_id = it->saved_face_id;
5947 if (it->dpvec + it->current.dpvec_index == it->dpend)
5949 int recheck_faces = it->ellipsis_p;
5951 if (it->s)
5952 it->method = GET_FROM_C_STRING;
5953 else if (STRINGP (it->string))
5954 it->method = GET_FROM_STRING;
5955 else
5957 it->method = GET_FROM_BUFFER;
5958 it->object = it->w->buffer;
5961 it->dpvec = NULL;
5962 it->current.dpvec_index = -1;
5964 /* Skip over characters which were displayed via IT->dpvec. */
5965 if (it->dpvec_char_len < 0)
5966 reseat_at_next_visible_line_start (it, 1);
5967 else if (it->dpvec_char_len > 0)
5969 if (it->method == GET_FROM_STRING
5970 && it->n_overlay_strings > 0)
5971 it->ignore_overlay_strings_at_pos_p = 1;
5972 it->len = it->dpvec_char_len;
5973 set_iterator_to_next (it, reseat_p);
5976 /* Maybe recheck faces after display vector */
5977 if (recheck_faces)
5978 it->stop_charpos = IT_CHARPOS (*it);
5980 break;
5982 case GET_FROM_STRING:
5983 /* Current display element is a character from a Lisp string. */
5984 xassert (it->s == NULL && STRINGP (it->string));
5985 IT_STRING_BYTEPOS (*it) += it->len;
5986 IT_STRING_CHARPOS (*it) += 1;
5988 consider_string_end:
5990 if (it->current.overlay_string_index >= 0)
5992 /* IT->string is an overlay string. Advance to the
5993 next, if there is one. */
5994 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5995 next_overlay_string (it);
5997 else
5999 /* IT->string is not an overlay string. If we reached
6000 its end, and there is something on IT->stack, proceed
6001 with what is on the stack. This can be either another
6002 string, this time an overlay string, or a buffer. */
6003 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6004 && it->sp > 0)
6006 pop_it (it);
6007 if (it->method == GET_FROM_STRING)
6008 goto consider_string_end;
6011 break;
6013 case GET_FROM_IMAGE:
6014 case GET_FROM_STRETCH:
6015 /* The position etc with which we have to proceed are on
6016 the stack. The position may be at the end of a string,
6017 if the `display' property takes up the whole string. */
6018 xassert (it->sp > 0);
6019 pop_it (it);
6020 if (it->method == GET_FROM_STRING)
6021 goto consider_string_end;
6022 break;
6024 default:
6025 /* There are no other methods defined, so this should be a bug. */
6026 abort ();
6029 xassert (it->method != GET_FROM_STRING
6030 || (STRINGP (it->string)
6031 && IT_STRING_CHARPOS (*it) >= 0));
6034 /* Load IT's display element fields with information about the next
6035 display element which comes from a display table entry or from the
6036 result of translating a control character to one of the forms `^C'
6037 or `\003'.
6039 IT->dpvec holds the glyphs to return as characters.
6040 IT->saved_face_id holds the face id before the display vector--
6041 it is restored into IT->face_idin set_iterator_to_next. */
6043 static int
6044 next_element_from_display_vector (it)
6045 struct it *it;
6047 /* Precondition. */
6048 xassert (it->dpvec && it->current.dpvec_index >= 0);
6050 it->face_id = it->saved_face_id;
6052 if (INTEGERP (*it->dpvec)
6053 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6055 GLYPH g;
6057 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6058 it->c = FAST_GLYPH_CHAR (g);
6059 it->len = CHAR_BYTES (it->c);
6061 /* The entry may contain a face id to use. Such a face id is
6062 the id of a Lisp face, not a realized face. A face id of
6063 zero means no face is specified. */
6064 if (it->dpvec_face_id >= 0)
6065 it->face_id = it->dpvec_face_id;
6066 else
6068 int lface_id = FAST_GLYPH_FACE (g);
6069 if (lface_id > 0)
6070 it->face_id = merge_faces (it->f, Qt, lface_id,
6071 it->saved_face_id);
6074 else
6075 /* Display table entry is invalid. Return a space. */
6076 it->c = ' ', it->len = 1;
6078 /* Don't change position and object of the iterator here. They are
6079 still the values of the character that had this display table
6080 entry or was translated, and that's what we want. */
6081 it->what = IT_CHARACTER;
6082 return 1;
6086 /* Load IT with the next display element from Lisp string IT->string.
6087 IT->current.string_pos is the current position within the string.
6088 If IT->current.overlay_string_index >= 0, the Lisp string is an
6089 overlay string. */
6091 static int
6092 next_element_from_string (it)
6093 struct it *it;
6095 struct text_pos position;
6097 xassert (STRINGP (it->string));
6098 xassert (IT_STRING_CHARPOS (*it) >= 0);
6099 position = it->current.string_pos;
6101 /* Time to check for invisible text? */
6102 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6103 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6105 handle_stop (it);
6107 /* Since a handler may have changed IT->method, we must
6108 recurse here. */
6109 return get_next_display_element (it);
6112 if (it->current.overlay_string_index >= 0)
6114 /* Get the next character from an overlay string. In overlay
6115 strings, There is no field width or padding with spaces to
6116 do. */
6117 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6119 it->what = IT_EOB;
6120 return 0;
6122 else if (STRING_MULTIBYTE (it->string))
6124 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6125 const unsigned char *s = (SDATA (it->string)
6126 + IT_STRING_BYTEPOS (*it));
6127 it->c = string_char_and_length (s, remaining, &it->len);
6129 else
6131 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6132 it->len = 1;
6135 else
6137 /* Get the next character from a Lisp string that is not an
6138 overlay string. Such strings come from the mode line, for
6139 example. We may have to pad with spaces, or truncate the
6140 string. See also next_element_from_c_string. */
6141 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6143 it->what = IT_EOB;
6144 return 0;
6146 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6148 /* Pad with spaces. */
6149 it->c = ' ', it->len = 1;
6150 CHARPOS (position) = BYTEPOS (position) = -1;
6152 else if (STRING_MULTIBYTE (it->string))
6154 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6155 const unsigned char *s = (SDATA (it->string)
6156 + IT_STRING_BYTEPOS (*it));
6157 it->c = string_char_and_length (s, maxlen, &it->len);
6159 else
6161 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6162 it->len = 1;
6166 /* Record what we have and where it came from. */
6167 it->what = IT_CHARACTER;
6168 it->object = it->string;
6169 it->position = position;
6170 return 1;
6174 /* Load IT with next display element from C string IT->s.
6175 IT->string_nchars is the maximum number of characters to return
6176 from the string. IT->end_charpos may be greater than
6177 IT->string_nchars when this function is called, in which case we
6178 may have to return padding spaces. Value is zero if end of string
6179 reached, including padding spaces. */
6181 static int
6182 next_element_from_c_string (it)
6183 struct it *it;
6185 int success_p = 1;
6187 xassert (it->s);
6188 it->what = IT_CHARACTER;
6189 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6190 it->object = Qnil;
6192 /* IT's position can be greater IT->string_nchars in case a field
6193 width or precision has been specified when the iterator was
6194 initialized. */
6195 if (IT_CHARPOS (*it) >= it->end_charpos)
6197 /* End of the game. */
6198 it->what = IT_EOB;
6199 success_p = 0;
6201 else if (IT_CHARPOS (*it) >= it->string_nchars)
6203 /* Pad with spaces. */
6204 it->c = ' ', it->len = 1;
6205 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6207 else if (it->multibyte_p)
6209 /* Implementation note: The calls to strlen apparently aren't a
6210 performance problem because there is no noticeable performance
6211 difference between Emacs running in unibyte or multibyte mode. */
6212 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6213 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6214 maxlen, &it->len);
6216 else
6217 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6219 return success_p;
6223 /* Set up IT to return characters from an ellipsis, if appropriate.
6224 The definition of the ellipsis glyphs may come from a display table
6225 entry. This function Fills IT with the first glyph from the
6226 ellipsis if an ellipsis is to be displayed. */
6228 static int
6229 next_element_from_ellipsis (it)
6230 struct it *it;
6232 if (it->selective_display_ellipsis_p)
6233 setup_for_ellipsis (it, it->len);
6234 else
6236 /* The face at the current position may be different from the
6237 face we find after the invisible text. Remember what it
6238 was in IT->saved_face_id, and signal that it's there by
6239 setting face_before_selective_p. */
6240 it->saved_face_id = it->face_id;
6241 it->method = GET_FROM_BUFFER;
6242 it->object = it->w->buffer;
6243 reseat_at_next_visible_line_start (it, 1);
6244 it->face_before_selective_p = 1;
6247 return get_next_display_element (it);
6251 /* Deliver an image display element. The iterator IT is already
6252 filled with image information (done in handle_display_prop). Value
6253 is always 1. */
6256 static int
6257 next_element_from_image (it)
6258 struct it *it;
6260 it->what = IT_IMAGE;
6261 return 1;
6265 /* Fill iterator IT with next display element from a stretch glyph
6266 property. IT->object is the value of the text property. Value is
6267 always 1. */
6269 static int
6270 next_element_from_stretch (it)
6271 struct it *it;
6273 it->what = IT_STRETCH;
6274 return 1;
6278 /* Load IT with the next display element from current_buffer. Value
6279 is zero if end of buffer reached. IT->stop_charpos is the next
6280 position at which to stop and check for text properties or buffer
6281 end. */
6283 static int
6284 next_element_from_buffer (it)
6285 struct it *it;
6287 int success_p = 1;
6289 /* Check this assumption, otherwise, we would never enter the
6290 if-statement, below. */
6291 xassert (IT_CHARPOS (*it) >= BEGV
6292 && IT_CHARPOS (*it) <= it->stop_charpos);
6294 if (IT_CHARPOS (*it) >= it->stop_charpos)
6296 if (IT_CHARPOS (*it) >= it->end_charpos)
6298 int overlay_strings_follow_p;
6300 /* End of the game, except when overlay strings follow that
6301 haven't been returned yet. */
6302 if (it->overlay_strings_at_end_processed_p)
6303 overlay_strings_follow_p = 0;
6304 else
6306 it->overlay_strings_at_end_processed_p = 1;
6307 overlay_strings_follow_p = get_overlay_strings (it, 0);
6310 if (overlay_strings_follow_p)
6311 success_p = get_next_display_element (it);
6312 else
6314 it->what = IT_EOB;
6315 it->position = it->current.pos;
6316 success_p = 0;
6319 else
6321 handle_stop (it);
6322 return get_next_display_element (it);
6325 else
6327 /* No face changes, overlays etc. in sight, so just return a
6328 character from current_buffer. */
6329 unsigned char *p;
6331 /* Maybe run the redisplay end trigger hook. Performance note:
6332 This doesn't seem to cost measurable time. */
6333 if (it->redisplay_end_trigger_charpos
6334 && it->glyph_row
6335 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6336 run_redisplay_end_trigger_hook (it);
6338 /* Get the next character, maybe multibyte. */
6339 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6340 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6342 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6343 - IT_BYTEPOS (*it));
6344 it->c = string_char_and_length (p, maxlen, &it->len);
6346 else
6347 it->c = *p, it->len = 1;
6349 /* Record what we have and where it came from. */
6350 it->what = IT_CHARACTER;
6351 it->object = it->w->buffer;
6352 it->position = it->current.pos;
6354 /* Normally we return the character found above, except when we
6355 really want to return an ellipsis for selective display. */
6356 if (it->selective)
6358 if (it->c == '\n')
6360 /* A value of selective > 0 means hide lines indented more
6361 than that number of columns. */
6362 if (it->selective > 0
6363 && IT_CHARPOS (*it) + 1 < ZV
6364 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6365 IT_BYTEPOS (*it) + 1,
6366 (double) it->selective)) /* iftc */
6368 success_p = next_element_from_ellipsis (it);
6369 it->dpvec_char_len = -1;
6372 else if (it->c == '\r' && it->selective == -1)
6374 /* A value of selective == -1 means that everything from the
6375 CR to the end of the line is invisible, with maybe an
6376 ellipsis displayed for it. */
6377 success_p = next_element_from_ellipsis (it);
6378 it->dpvec_char_len = -1;
6383 /* Value is zero if end of buffer reached. */
6384 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6385 return success_p;
6389 /* Run the redisplay end trigger hook for IT. */
6391 static void
6392 run_redisplay_end_trigger_hook (it)
6393 struct it *it;
6395 Lisp_Object args[3];
6397 /* IT->glyph_row should be non-null, i.e. we should be actually
6398 displaying something, or otherwise we should not run the hook. */
6399 xassert (it->glyph_row);
6401 /* Set up hook arguments. */
6402 args[0] = Qredisplay_end_trigger_functions;
6403 args[1] = it->window;
6404 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6405 it->redisplay_end_trigger_charpos = 0;
6407 /* Since we are *trying* to run these functions, don't try to run
6408 them again, even if they get an error. */
6409 it->w->redisplay_end_trigger = Qnil;
6410 Frun_hook_with_args (3, args);
6412 /* Notice if it changed the face of the character we are on. */
6413 handle_face_prop (it);
6417 /* Deliver a composition display element. The iterator IT is already
6418 filled with composition information (done in
6419 handle_composition_prop). Value is always 1. */
6421 static int
6422 next_element_from_composition (it)
6423 struct it *it;
6425 it->what = IT_COMPOSITION;
6426 it->position = (STRINGP (it->string)
6427 ? it->current.string_pos
6428 : it->current.pos);
6429 if (STRINGP (it->string))
6430 it->object = it->string;
6431 else
6432 it->object = it->w->buffer;
6433 return 1;
6438 /***********************************************************************
6439 Moving an iterator without producing glyphs
6440 ***********************************************************************/
6442 /* Check if iterator is at a position corresponding to a valid buffer
6443 position after some move_it_ call. */
6445 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6446 ((it)->method == GET_FROM_STRING \
6447 ? IT_STRING_CHARPOS (*it) == 0 \
6448 : 1)
6451 /* Move iterator IT to a specified buffer or X position within one
6452 line on the display without producing glyphs.
6454 OP should be a bit mask including some or all of these bits:
6455 MOVE_TO_X: Stop on reaching x-position TO_X.
6456 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6457 Regardless of OP's value, stop in reaching the end of the display line.
6459 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6460 This means, in particular, that TO_X includes window's horizontal
6461 scroll amount.
6463 The return value has several possible values that
6464 say what condition caused the scan to stop:
6466 MOVE_POS_MATCH_OR_ZV
6467 - when TO_POS or ZV was reached.
6469 MOVE_X_REACHED
6470 -when TO_X was reached before TO_POS or ZV were reached.
6472 MOVE_LINE_CONTINUED
6473 - when we reached the end of the display area and the line must
6474 be continued.
6476 MOVE_LINE_TRUNCATED
6477 - when we reached the end of the display area and the line is
6478 truncated.
6480 MOVE_NEWLINE_OR_CR
6481 - when we stopped at a line end, i.e. a newline or a CR and selective
6482 display is on. */
6484 static enum move_it_result
6485 move_it_in_display_line_to (it, to_charpos, to_x, op)
6486 struct it *it;
6487 int to_charpos, to_x, op;
6489 enum move_it_result result = MOVE_UNDEFINED;
6490 struct glyph_row *saved_glyph_row;
6492 /* Don't produce glyphs in produce_glyphs. */
6493 saved_glyph_row = it->glyph_row;
6494 it->glyph_row = NULL;
6496 #define BUFFER_POS_REACHED_P() \
6497 ((op & MOVE_TO_POS) != 0 \
6498 && BUFFERP (it->object) \
6499 && IT_CHARPOS (*it) >= to_charpos \
6500 && (it->method == GET_FROM_BUFFER \
6501 || (it->method == GET_FROM_DISPLAY_VECTOR \
6502 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6505 while (1)
6507 int x, i, ascent = 0, descent = 0;
6509 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6510 if ((op & MOVE_TO_POS) != 0
6511 && BUFFERP (it->object)
6512 && it->method == GET_FROM_BUFFER
6513 && IT_CHARPOS (*it) > to_charpos)
6515 result = MOVE_POS_MATCH_OR_ZV;
6516 break;
6519 /* Stop when ZV reached.
6520 We used to stop here when TO_CHARPOS reached as well, but that is
6521 too soon if this glyph does not fit on this line. So we handle it
6522 explicitly below. */
6523 if (!get_next_display_element (it)
6524 || (it->truncate_lines_p
6525 && BUFFER_POS_REACHED_P ()))
6527 result = MOVE_POS_MATCH_OR_ZV;
6528 break;
6531 /* The call to produce_glyphs will get the metrics of the
6532 display element IT is loaded with. We record in x the
6533 x-position before this display element in case it does not
6534 fit on the line. */
6535 x = it->current_x;
6537 /* Remember the line height so far in case the next element doesn't
6538 fit on the line. */
6539 if (!it->truncate_lines_p)
6541 ascent = it->max_ascent;
6542 descent = it->max_descent;
6545 PRODUCE_GLYPHS (it);
6547 if (it->area != TEXT_AREA)
6549 set_iterator_to_next (it, 1);
6550 continue;
6553 /* The number of glyphs we get back in IT->nglyphs will normally
6554 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6555 character on a terminal frame, or (iii) a line end. For the
6556 second case, IT->nglyphs - 1 padding glyphs will be present
6557 (on X frames, there is only one glyph produced for a
6558 composite character.
6560 The behavior implemented below means, for continuation lines,
6561 that as many spaces of a TAB as fit on the current line are
6562 displayed there. For terminal frames, as many glyphs of a
6563 multi-glyph character are displayed in the current line, too.
6564 This is what the old redisplay code did, and we keep it that
6565 way. Under X, the whole shape of a complex character must
6566 fit on the line or it will be completely displayed in the
6567 next line.
6569 Note that both for tabs and padding glyphs, all glyphs have
6570 the same width. */
6571 if (it->nglyphs)
6573 /* More than one glyph or glyph doesn't fit on line. All
6574 glyphs have the same width. */
6575 int single_glyph_width = it->pixel_width / it->nglyphs;
6576 int new_x;
6577 int x_before_this_char = x;
6578 int hpos_before_this_char = it->hpos;
6580 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6582 new_x = x + single_glyph_width;
6584 /* We want to leave anything reaching TO_X to the caller. */
6585 if ((op & MOVE_TO_X) && new_x > to_x)
6587 if (BUFFER_POS_REACHED_P ())
6588 goto buffer_pos_reached;
6589 it->current_x = x;
6590 result = MOVE_X_REACHED;
6591 break;
6593 else if (/* Lines are continued. */
6594 !it->truncate_lines_p
6595 && (/* And glyph doesn't fit on the line. */
6596 new_x > it->last_visible_x
6597 /* Or it fits exactly and we're on a window
6598 system frame. */
6599 || (new_x == it->last_visible_x
6600 && FRAME_WINDOW_P (it->f))))
6602 if (/* IT->hpos == 0 means the very first glyph
6603 doesn't fit on the line, e.g. a wide image. */
6604 it->hpos == 0
6605 || (new_x == it->last_visible_x
6606 && FRAME_WINDOW_P (it->f)))
6608 ++it->hpos;
6609 it->current_x = new_x;
6611 /* The character's last glyph just barely fits
6612 in this row. */
6613 if (i == it->nglyphs - 1)
6615 /* If this is the destination position,
6616 return a position *before* it in this row,
6617 now that we know it fits in this row. */
6618 if (BUFFER_POS_REACHED_P ())
6620 it->hpos = hpos_before_this_char;
6621 it->current_x = x_before_this_char;
6622 result = MOVE_POS_MATCH_OR_ZV;
6623 break;
6626 set_iterator_to_next (it, 1);
6627 #ifdef HAVE_WINDOW_SYSTEM
6628 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6630 if (!get_next_display_element (it))
6632 result = MOVE_POS_MATCH_OR_ZV;
6633 break;
6635 if (BUFFER_POS_REACHED_P ())
6637 if (ITERATOR_AT_END_OF_LINE_P (it))
6638 result = MOVE_POS_MATCH_OR_ZV;
6639 else
6640 result = MOVE_LINE_CONTINUED;
6641 break;
6643 if (ITERATOR_AT_END_OF_LINE_P (it))
6645 result = MOVE_NEWLINE_OR_CR;
6646 break;
6649 #endif /* HAVE_WINDOW_SYSTEM */
6652 else
6654 it->current_x = x;
6655 it->max_ascent = ascent;
6656 it->max_descent = descent;
6659 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6660 IT_CHARPOS (*it)));
6661 result = MOVE_LINE_CONTINUED;
6662 break;
6664 else if (BUFFER_POS_REACHED_P ())
6665 goto buffer_pos_reached;
6666 else if (new_x > it->first_visible_x)
6668 /* Glyph is visible. Increment number of glyphs that
6669 would be displayed. */
6670 ++it->hpos;
6672 else
6674 /* Glyph is completely off the left margin of the display
6675 area. Nothing to do. */
6679 if (result != MOVE_UNDEFINED)
6680 break;
6682 else if (BUFFER_POS_REACHED_P ())
6684 buffer_pos_reached:
6685 it->current_x = x;
6686 it->max_ascent = ascent;
6687 it->max_descent = descent;
6688 result = MOVE_POS_MATCH_OR_ZV;
6689 break;
6691 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6693 /* Stop when TO_X specified and reached. This check is
6694 necessary here because of lines consisting of a line end,
6695 only. The line end will not produce any glyphs and we
6696 would never get MOVE_X_REACHED. */
6697 xassert (it->nglyphs == 0);
6698 result = MOVE_X_REACHED;
6699 break;
6702 /* Is this a line end? If yes, we're done. */
6703 if (ITERATOR_AT_END_OF_LINE_P (it))
6705 result = MOVE_NEWLINE_OR_CR;
6706 break;
6709 /* The current display element has been consumed. Advance
6710 to the next. */
6711 set_iterator_to_next (it, 1);
6713 /* Stop if lines are truncated and IT's current x-position is
6714 past the right edge of the window now. */
6715 if (it->truncate_lines_p
6716 && it->current_x >= it->last_visible_x)
6718 #ifdef HAVE_WINDOW_SYSTEM
6719 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6721 if (!get_next_display_element (it)
6722 || BUFFER_POS_REACHED_P ())
6724 result = MOVE_POS_MATCH_OR_ZV;
6725 break;
6727 if (ITERATOR_AT_END_OF_LINE_P (it))
6729 result = MOVE_NEWLINE_OR_CR;
6730 break;
6733 #endif /* HAVE_WINDOW_SYSTEM */
6734 result = MOVE_LINE_TRUNCATED;
6735 break;
6739 #undef BUFFER_POS_REACHED_P
6741 /* Restore the iterator settings altered at the beginning of this
6742 function. */
6743 it->glyph_row = saved_glyph_row;
6744 return result;
6748 /* Move IT forward until it satisfies one or more of the criteria in
6749 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6751 OP is a bit-mask that specifies where to stop, and in particular,
6752 which of those four position arguments makes a difference. See the
6753 description of enum move_operation_enum.
6755 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6756 screen line, this function will set IT to the next position >
6757 TO_CHARPOS. */
6759 void
6760 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6761 struct it *it;
6762 int to_charpos, to_x, to_y, to_vpos;
6763 int op;
6765 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6766 int line_height;
6767 int reached = 0;
6769 for (;;)
6771 if (op & MOVE_TO_VPOS)
6773 /* If no TO_CHARPOS and no TO_X specified, stop at the
6774 start of the line TO_VPOS. */
6775 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6777 if (it->vpos == to_vpos)
6779 reached = 1;
6780 break;
6782 else
6783 skip = move_it_in_display_line_to (it, -1, -1, 0);
6785 else
6787 /* TO_VPOS >= 0 means stop at TO_X in the line at
6788 TO_VPOS, or at TO_POS, whichever comes first. */
6789 if (it->vpos == to_vpos)
6791 reached = 2;
6792 break;
6795 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6797 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6799 reached = 3;
6800 break;
6802 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6804 /* We have reached TO_X but not in the line we want. */
6805 skip = move_it_in_display_line_to (it, to_charpos,
6806 -1, MOVE_TO_POS);
6807 if (skip == MOVE_POS_MATCH_OR_ZV)
6809 reached = 4;
6810 break;
6815 else if (op & MOVE_TO_Y)
6817 struct it it_backup;
6819 /* TO_Y specified means stop at TO_X in the line containing
6820 TO_Y---or at TO_CHARPOS if this is reached first. The
6821 problem is that we can't really tell whether the line
6822 contains TO_Y before we have completely scanned it, and
6823 this may skip past TO_X. What we do is to first scan to
6824 TO_X.
6826 If TO_X is not specified, use a TO_X of zero. The reason
6827 is to make the outcome of this function more predictable.
6828 If we didn't use TO_X == 0, we would stop at the end of
6829 the line which is probably not what a caller would expect
6830 to happen. */
6831 skip = move_it_in_display_line_to (it, to_charpos,
6832 ((op & MOVE_TO_X)
6833 ? to_x : 0),
6834 (MOVE_TO_X
6835 | (op & MOVE_TO_POS)));
6837 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6838 if (skip == MOVE_POS_MATCH_OR_ZV)
6840 reached = 5;
6841 break;
6844 /* If TO_X was reached, we would like to know whether TO_Y
6845 is in the line. This can only be said if we know the
6846 total line height which requires us to scan the rest of
6847 the line. */
6848 if (skip == MOVE_X_REACHED)
6850 it_backup = *it;
6851 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6852 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6853 op & MOVE_TO_POS);
6854 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6857 /* Now, decide whether TO_Y is in this line. */
6858 line_height = it->max_ascent + it->max_descent;
6859 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6861 if (to_y >= it->current_y
6862 && to_y < it->current_y + line_height)
6864 if (skip == MOVE_X_REACHED)
6865 /* If TO_Y is in this line and TO_X was reached above,
6866 we scanned too far. We have to restore IT's settings
6867 to the ones before skipping. */
6868 *it = it_backup;
6869 reached = 6;
6871 else if (skip == MOVE_X_REACHED)
6873 skip = skip2;
6874 if (skip == MOVE_POS_MATCH_OR_ZV)
6875 reached = 7;
6878 if (reached)
6879 break;
6881 else if (BUFFERP (it->object)
6882 && it->method == GET_FROM_BUFFER
6883 && IT_CHARPOS (*it) >= to_charpos)
6884 skip = MOVE_POS_MATCH_OR_ZV;
6885 else
6886 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6888 switch (skip)
6890 case MOVE_POS_MATCH_OR_ZV:
6891 reached = 8;
6892 goto out;
6894 case MOVE_NEWLINE_OR_CR:
6895 set_iterator_to_next (it, 1);
6896 it->continuation_lines_width = 0;
6897 break;
6899 case MOVE_LINE_TRUNCATED:
6900 it->continuation_lines_width = 0;
6901 reseat_at_next_visible_line_start (it, 0);
6902 if ((op & MOVE_TO_POS) != 0
6903 && IT_CHARPOS (*it) > to_charpos)
6905 reached = 9;
6906 goto out;
6908 break;
6910 case MOVE_LINE_CONTINUED:
6911 /* For continued lines ending in a tab, some of the glyphs
6912 associated with the tab are displayed on the current
6913 line. Since it->current_x does not include these glyphs,
6914 we use it->last_visible_x instead. */
6915 it->continuation_lines_width +=
6916 (it->c == '\t') ? it->last_visible_x : it->current_x;
6917 break;
6919 default:
6920 abort ();
6923 /* Reset/increment for the next run. */
6924 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6925 it->current_x = it->hpos = 0;
6926 it->current_y += it->max_ascent + it->max_descent;
6927 ++it->vpos;
6928 last_height = it->max_ascent + it->max_descent;
6929 last_max_ascent = it->max_ascent;
6930 it->max_ascent = it->max_descent = 0;
6933 out:
6935 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6939 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6941 If DY > 0, move IT backward at least that many pixels. DY = 0
6942 means move IT backward to the preceding line start or BEGV. This
6943 function may move over more than DY pixels if IT->current_y - DY
6944 ends up in the middle of a line; in this case IT->current_y will be
6945 set to the top of the line moved to. */
6947 void
6948 move_it_vertically_backward (it, dy)
6949 struct it *it;
6950 int dy;
6952 int nlines, h;
6953 struct it it2, it3;
6954 int start_pos;
6956 move_further_back:
6957 xassert (dy >= 0);
6959 start_pos = IT_CHARPOS (*it);
6961 /* Estimate how many newlines we must move back. */
6962 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6964 /* Set the iterator's position that many lines back. */
6965 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6966 back_to_previous_visible_line_start (it);
6968 /* Reseat the iterator here. When moving backward, we don't want
6969 reseat to skip forward over invisible text, set up the iterator
6970 to deliver from overlay strings at the new position etc. So,
6971 use reseat_1 here. */
6972 reseat_1 (it, it->current.pos, 1);
6974 /* We are now surely at a line start. */
6975 it->current_x = it->hpos = 0;
6976 it->continuation_lines_width = 0;
6978 /* Move forward and see what y-distance we moved. First move to the
6979 start of the next line so that we get its height. We need this
6980 height to be able to tell whether we reached the specified
6981 y-distance. */
6982 it2 = *it;
6983 it2.max_ascent = it2.max_descent = 0;
6986 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6987 MOVE_TO_POS | MOVE_TO_VPOS);
6989 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6990 xassert (IT_CHARPOS (*it) >= BEGV);
6991 it3 = it2;
6993 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6994 xassert (IT_CHARPOS (*it) >= BEGV);
6995 /* H is the actual vertical distance from the position in *IT
6996 and the starting position. */
6997 h = it2.current_y - it->current_y;
6998 /* NLINES is the distance in number of lines. */
6999 nlines = it2.vpos - it->vpos;
7001 /* Correct IT's y and vpos position
7002 so that they are relative to the starting point. */
7003 it->vpos -= nlines;
7004 it->current_y -= h;
7006 if (dy == 0)
7008 /* DY == 0 means move to the start of the screen line. The
7009 value of nlines is > 0 if continuation lines were involved. */
7010 if (nlines > 0)
7011 move_it_by_lines (it, nlines, 1);
7012 #if 0
7013 /* I think this assert is bogus if buffer contains
7014 invisible text or images. KFS. */
7015 xassert (IT_CHARPOS (*it) <= start_pos);
7016 #endif
7018 else
7020 /* The y-position we try to reach, relative to *IT.
7021 Note that H has been subtracted in front of the if-statement. */
7022 int target_y = it->current_y + h - dy;
7023 int y0 = it3.current_y;
7024 int y1 = line_bottom_y (&it3);
7025 int line_height = y1 - y0;
7027 /* If we did not reach target_y, try to move further backward if
7028 we can. If we moved too far backward, try to move forward. */
7029 if (target_y < it->current_y
7030 /* This is heuristic. In a window that's 3 lines high, with
7031 a line height of 13 pixels each, recentering with point
7032 on the bottom line will try to move -39/2 = 19 pixels
7033 backward. Try to avoid moving into the first line. */
7034 && (it->current_y - target_y
7035 > min (window_box_height (it->w), line_height * 2 / 3))
7036 && IT_CHARPOS (*it) > BEGV)
7038 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7039 target_y - it->current_y));
7040 dy = it->current_y - target_y;
7041 goto move_further_back;
7043 else if (target_y >= it->current_y + line_height
7044 && IT_CHARPOS (*it) < ZV)
7046 /* Should move forward by at least one line, maybe more.
7048 Note: Calling move_it_by_lines can be expensive on
7049 terminal frames, where compute_motion is used (via
7050 vmotion) to do the job, when there are very long lines
7051 and truncate-lines is nil. That's the reason for
7052 treating terminal frames specially here. */
7054 if (!FRAME_WINDOW_P (it->f))
7055 move_it_vertically (it, target_y - (it->current_y + line_height));
7056 else
7060 move_it_by_lines (it, 1, 1);
7062 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7065 #if 0
7066 /* I think this assert is bogus if buffer contains
7067 invisible text or images. KFS. */
7068 xassert (IT_CHARPOS (*it) >= BEGV);
7069 #endif
7075 /* Move IT by a specified amount of pixel lines DY. DY negative means
7076 move backwards. DY = 0 means move to start of screen line. At the
7077 end, IT will be on the start of a screen line. */
7079 void
7080 move_it_vertically (it, dy)
7081 struct it *it;
7082 int dy;
7084 if (dy <= 0)
7085 move_it_vertically_backward (it, -dy);
7086 else
7088 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7089 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7090 MOVE_TO_POS | MOVE_TO_Y);
7091 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7093 /* If buffer ends in ZV without a newline, move to the start of
7094 the line to satisfy the post-condition. */
7095 if (IT_CHARPOS (*it) == ZV
7096 && ZV > BEGV
7097 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7098 move_it_by_lines (it, 0, 0);
7103 /* Move iterator IT past the end of the text line it is in. */
7105 void
7106 move_it_past_eol (it)
7107 struct it *it;
7109 enum move_it_result rc;
7111 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7112 if (rc == MOVE_NEWLINE_OR_CR)
7113 set_iterator_to_next (it, 0);
7117 #if 0 /* Currently not used. */
7119 /* Return non-zero if some text between buffer positions START_CHARPOS
7120 and END_CHARPOS is invisible. IT->window is the window for text
7121 property lookup. */
7123 static int
7124 invisible_text_between_p (it, start_charpos, end_charpos)
7125 struct it *it;
7126 int start_charpos, end_charpos;
7128 Lisp_Object prop, limit;
7129 int invisible_found_p;
7131 xassert (it != NULL && start_charpos <= end_charpos);
7133 /* Is text at START invisible? */
7134 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7135 it->window);
7136 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7137 invisible_found_p = 1;
7138 else
7140 limit = Fnext_single_char_property_change (make_number (start_charpos),
7141 Qinvisible, Qnil,
7142 make_number (end_charpos));
7143 invisible_found_p = XFASTINT (limit) < end_charpos;
7146 return invisible_found_p;
7149 #endif /* 0 */
7152 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7153 negative means move up. DVPOS == 0 means move to the start of the
7154 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7155 NEED_Y_P is zero, IT->current_y will be left unchanged.
7157 Further optimization ideas: If we would know that IT->f doesn't use
7158 a face with proportional font, we could be faster for
7159 truncate-lines nil. */
7161 void
7162 move_it_by_lines (it, dvpos, need_y_p)
7163 struct it *it;
7164 int dvpos, need_y_p;
7166 struct position pos;
7168 /* The commented-out optimization uses vmotion on terminals. This
7169 gives bad results, because elements like it->what, on which
7170 callers such as pos_visible_p rely, aren't updated. */
7171 /* if (!FRAME_WINDOW_P (it->f))
7173 struct text_pos textpos;
7175 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7176 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7177 reseat (it, textpos, 1);
7178 it->vpos += pos.vpos;
7179 it->current_y += pos.vpos;
7181 else */
7183 if (dvpos == 0)
7185 /* DVPOS == 0 means move to the start of the screen line. */
7186 move_it_vertically_backward (it, 0);
7187 xassert (it->current_x == 0 && it->hpos == 0);
7188 /* Let next call to line_bottom_y calculate real line height */
7189 last_height = 0;
7191 else if (dvpos > 0)
7193 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7194 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7195 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7197 else
7199 struct it it2;
7200 int start_charpos, i;
7202 /* Start at the beginning of the screen line containing IT's
7203 position. This may actually move vertically backwards,
7204 in case of overlays, so adjust dvpos accordingly. */
7205 dvpos += it->vpos;
7206 move_it_vertically_backward (it, 0);
7207 dvpos -= it->vpos;
7209 /* Go back -DVPOS visible lines and reseat the iterator there. */
7210 start_charpos = IT_CHARPOS (*it);
7211 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7212 back_to_previous_visible_line_start (it);
7213 reseat (it, it->current.pos, 1);
7215 /* Move further back if we end up in a string or an image. */
7216 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7218 /* First try to move to start of display line. */
7219 dvpos += it->vpos;
7220 move_it_vertically_backward (it, 0);
7221 dvpos -= it->vpos;
7222 if (IT_POS_VALID_AFTER_MOVE_P (it))
7223 break;
7224 /* If start of line is still in string or image,
7225 move further back. */
7226 back_to_previous_visible_line_start (it);
7227 reseat (it, it->current.pos, 1);
7228 dvpos--;
7231 it->current_x = it->hpos = 0;
7233 /* Above call may have moved too far if continuation lines
7234 are involved. Scan forward and see if it did. */
7235 it2 = *it;
7236 it2.vpos = it2.current_y = 0;
7237 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7238 it->vpos -= it2.vpos;
7239 it->current_y -= it2.current_y;
7240 it->current_x = it->hpos = 0;
7242 /* If we moved too far back, move IT some lines forward. */
7243 if (it2.vpos > -dvpos)
7245 int delta = it2.vpos + dvpos;
7246 it2 = *it;
7247 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7248 /* Move back again if we got too far ahead. */
7249 if (IT_CHARPOS (*it) >= start_charpos)
7250 *it = it2;
7255 /* Return 1 if IT points into the middle of a display vector. */
7258 in_display_vector_p (it)
7259 struct it *it;
7261 return (it->method == GET_FROM_DISPLAY_VECTOR
7262 && it->current.dpvec_index > 0
7263 && it->dpvec + it->current.dpvec_index != it->dpend);
7267 /***********************************************************************
7268 Messages
7269 ***********************************************************************/
7272 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7273 to *Messages*. */
7275 void
7276 add_to_log (format, arg1, arg2)
7277 char *format;
7278 Lisp_Object arg1, arg2;
7280 Lisp_Object args[3];
7281 Lisp_Object msg, fmt;
7282 char *buffer;
7283 int len;
7284 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7285 USE_SAFE_ALLOCA;
7287 /* Do nothing if called asynchronously. Inserting text into
7288 a buffer may call after-change-functions and alike and
7289 that would means running Lisp asynchronously. */
7290 if (handling_signal)
7291 return;
7293 fmt = msg = Qnil;
7294 GCPRO4 (fmt, msg, arg1, arg2);
7296 args[0] = fmt = build_string (format);
7297 args[1] = arg1;
7298 args[2] = arg2;
7299 msg = Fformat (3, args);
7301 len = SBYTES (msg) + 1;
7302 SAFE_ALLOCA (buffer, char *, len);
7303 bcopy (SDATA (msg), buffer, len);
7305 message_dolog (buffer, len - 1, 1, 0);
7306 SAFE_FREE ();
7308 UNGCPRO;
7312 /* Output a newline in the *Messages* buffer if "needs" one. */
7314 void
7315 message_log_maybe_newline ()
7317 if (message_log_need_newline)
7318 message_dolog ("", 0, 1, 0);
7322 /* Add a string M of length NBYTES to the message log, optionally
7323 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7324 nonzero, means interpret the contents of M as multibyte. This
7325 function calls low-level routines in order to bypass text property
7326 hooks, etc. which might not be safe to run.
7328 This may GC (insert may run before/after change hooks),
7329 so the buffer M must NOT point to a Lisp string. */
7331 void
7332 message_dolog (m, nbytes, nlflag, multibyte)
7333 const char *m;
7334 int nbytes, nlflag, multibyte;
7336 if (!NILP (Vmemory_full))
7337 return;
7339 if (!NILP (Vmessage_log_max))
7341 struct buffer *oldbuf;
7342 Lisp_Object oldpoint, oldbegv, oldzv;
7343 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7344 int point_at_end = 0;
7345 int zv_at_end = 0;
7346 Lisp_Object old_deactivate_mark, tem;
7347 struct gcpro gcpro1;
7349 old_deactivate_mark = Vdeactivate_mark;
7350 oldbuf = current_buffer;
7351 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7352 current_buffer->undo_list = Qt;
7354 oldpoint = message_dolog_marker1;
7355 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7356 oldbegv = message_dolog_marker2;
7357 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7358 oldzv = message_dolog_marker3;
7359 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7360 GCPRO1 (old_deactivate_mark);
7362 if (PT == Z)
7363 point_at_end = 1;
7364 if (ZV == Z)
7365 zv_at_end = 1;
7367 BEGV = BEG;
7368 BEGV_BYTE = BEG_BYTE;
7369 ZV = Z;
7370 ZV_BYTE = Z_BYTE;
7371 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7373 /* Insert the string--maybe converting multibyte to single byte
7374 or vice versa, so that all the text fits the buffer. */
7375 if (multibyte
7376 && NILP (current_buffer->enable_multibyte_characters))
7378 int i, c, char_bytes;
7379 unsigned char work[1];
7381 /* Convert a multibyte string to single-byte
7382 for the *Message* buffer. */
7383 for (i = 0; i < nbytes; i += char_bytes)
7385 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7386 work[0] = (SINGLE_BYTE_CHAR_P (c)
7388 : multibyte_char_to_unibyte (c, Qnil));
7389 insert_1_both (work, 1, 1, 1, 0, 0);
7392 else if (! multibyte
7393 && ! NILP (current_buffer->enable_multibyte_characters))
7395 int i, c, char_bytes;
7396 unsigned char *msg = (unsigned char *) m;
7397 unsigned char str[MAX_MULTIBYTE_LENGTH];
7398 /* Convert a single-byte string to multibyte
7399 for the *Message* buffer. */
7400 for (i = 0; i < nbytes; i++)
7402 c = unibyte_char_to_multibyte (msg[i]);
7403 char_bytes = CHAR_STRING (c, str);
7404 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7407 else if (nbytes)
7408 insert_1 (m, nbytes, 1, 0, 0);
7410 if (nlflag)
7412 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7413 insert_1 ("\n", 1, 1, 0, 0);
7415 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7416 this_bol = PT;
7417 this_bol_byte = PT_BYTE;
7419 /* See if this line duplicates the previous one.
7420 If so, combine duplicates. */
7421 if (this_bol > BEG)
7423 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7424 prev_bol = PT;
7425 prev_bol_byte = PT_BYTE;
7427 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7428 this_bol, this_bol_byte);
7429 if (dup)
7431 del_range_both (prev_bol, prev_bol_byte,
7432 this_bol, this_bol_byte, 0);
7433 if (dup > 1)
7435 char dupstr[40];
7436 int duplen;
7438 /* If you change this format, don't forget to also
7439 change message_log_check_duplicate. */
7440 sprintf (dupstr, " [%d times]", dup);
7441 duplen = strlen (dupstr);
7442 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7443 insert_1 (dupstr, duplen, 1, 0, 1);
7448 /* If we have more than the desired maximum number of lines
7449 in the *Messages* buffer now, delete the oldest ones.
7450 This is safe because we don't have undo in this buffer. */
7452 if (NATNUMP (Vmessage_log_max))
7454 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7455 -XFASTINT (Vmessage_log_max) - 1, 0);
7456 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7459 BEGV = XMARKER (oldbegv)->charpos;
7460 BEGV_BYTE = marker_byte_position (oldbegv);
7462 if (zv_at_end)
7464 ZV = Z;
7465 ZV_BYTE = Z_BYTE;
7467 else
7469 ZV = XMARKER (oldzv)->charpos;
7470 ZV_BYTE = marker_byte_position (oldzv);
7473 if (point_at_end)
7474 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7475 else
7476 /* We can't do Fgoto_char (oldpoint) because it will run some
7477 Lisp code. */
7478 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7479 XMARKER (oldpoint)->bytepos);
7481 UNGCPRO;
7482 unchain_marker (XMARKER (oldpoint));
7483 unchain_marker (XMARKER (oldbegv));
7484 unchain_marker (XMARKER (oldzv));
7486 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7487 set_buffer_internal (oldbuf);
7488 if (NILP (tem))
7489 windows_or_buffers_changed = old_windows_or_buffers_changed;
7490 message_log_need_newline = !nlflag;
7491 Vdeactivate_mark = old_deactivate_mark;
7496 /* We are at the end of the buffer after just having inserted a newline.
7497 (Note: We depend on the fact we won't be crossing the gap.)
7498 Check to see if the most recent message looks a lot like the previous one.
7499 Return 0 if different, 1 if the new one should just replace it, or a
7500 value N > 1 if we should also append " [N times]". */
7502 static int
7503 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7504 int prev_bol, this_bol;
7505 int prev_bol_byte, this_bol_byte;
7507 int i;
7508 int len = Z_BYTE - 1 - this_bol_byte;
7509 int seen_dots = 0;
7510 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7511 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7513 for (i = 0; i < len; i++)
7515 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7516 seen_dots = 1;
7517 if (p1[i] != p2[i])
7518 return seen_dots;
7520 p1 += len;
7521 if (*p1 == '\n')
7522 return 2;
7523 if (*p1++ == ' ' && *p1++ == '[')
7525 int n = 0;
7526 while (*p1 >= '0' && *p1 <= '9')
7527 n = n * 10 + *p1++ - '0';
7528 if (strncmp (p1, " times]\n", 8) == 0)
7529 return n+1;
7531 return 0;
7535 /* Display an echo area message M with a specified length of NBYTES
7536 bytes. The string may include null characters. If M is 0, clear
7537 out any existing message, and let the mini-buffer text show
7538 through.
7540 This may GC, so the buffer M must NOT point to a Lisp string. */
7542 void
7543 message2 (m, nbytes, multibyte)
7544 const char *m;
7545 int nbytes;
7546 int multibyte;
7548 /* First flush out any partial line written with print. */
7549 message_log_maybe_newline ();
7550 if (m)
7551 message_dolog (m, nbytes, 1, multibyte);
7552 message2_nolog (m, nbytes, multibyte);
7556 /* The non-logging counterpart of message2. */
7558 void
7559 message2_nolog (m, nbytes, multibyte)
7560 const char *m;
7561 int nbytes, multibyte;
7563 struct frame *sf = SELECTED_FRAME ();
7564 message_enable_multibyte = multibyte;
7566 if (noninteractive)
7568 if (noninteractive_need_newline)
7569 putc ('\n', stderr);
7570 noninteractive_need_newline = 0;
7571 if (m)
7572 fwrite (m, nbytes, 1, stderr);
7573 if (cursor_in_echo_area == 0)
7574 fprintf (stderr, "\n");
7575 fflush (stderr);
7577 /* A null message buffer means that the frame hasn't really been
7578 initialized yet. Error messages get reported properly by
7579 cmd_error, so this must be just an informative message; toss it. */
7580 else if (INTERACTIVE
7581 && sf->glyphs_initialized_p
7582 && FRAME_MESSAGE_BUF (sf))
7584 Lisp_Object mini_window;
7585 struct frame *f;
7587 /* Get the frame containing the mini-buffer
7588 that the selected frame is using. */
7589 mini_window = FRAME_MINIBUF_WINDOW (sf);
7590 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7592 FRAME_SAMPLE_VISIBILITY (f);
7593 if (FRAME_VISIBLE_P (sf)
7594 && ! FRAME_VISIBLE_P (f))
7595 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7597 if (m)
7599 set_message (m, Qnil, nbytes, multibyte);
7600 if (minibuffer_auto_raise)
7601 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7603 else
7604 clear_message (1, 1);
7606 do_pending_window_change (0);
7607 echo_area_display (1);
7608 do_pending_window_change (0);
7609 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7610 (*frame_up_to_date_hook) (f);
7615 /* Display an echo area message M with a specified length of NBYTES
7616 bytes. The string may include null characters. If M is not a
7617 string, clear out any existing message, and let the mini-buffer
7618 text show through.
7620 This function cancels echoing. */
7622 void
7623 message3 (m, nbytes, multibyte)
7624 Lisp_Object m;
7625 int nbytes;
7626 int multibyte;
7628 struct gcpro gcpro1;
7630 GCPRO1 (m);
7631 clear_message (1,1);
7632 cancel_echoing ();
7634 /* First flush out any partial line written with print. */
7635 message_log_maybe_newline ();
7636 if (STRINGP (m))
7638 char *buffer;
7639 USE_SAFE_ALLOCA;
7641 SAFE_ALLOCA (buffer, char *, nbytes);
7642 bcopy (SDATA (m), buffer, nbytes);
7643 message_dolog (buffer, nbytes, 1, multibyte);
7644 SAFE_FREE ();
7646 message3_nolog (m, nbytes, multibyte);
7648 UNGCPRO;
7652 /* The non-logging version of message3.
7653 This does not cancel echoing, because it is used for echoing.
7654 Perhaps we need to make a separate function for echoing
7655 and make this cancel echoing. */
7657 void
7658 message3_nolog (m, nbytes, multibyte)
7659 Lisp_Object m;
7660 int nbytes, multibyte;
7662 struct frame *sf = SELECTED_FRAME ();
7663 message_enable_multibyte = multibyte;
7665 if (noninteractive)
7667 if (noninteractive_need_newline)
7668 putc ('\n', stderr);
7669 noninteractive_need_newline = 0;
7670 if (STRINGP (m))
7671 fwrite (SDATA (m), nbytes, 1, stderr);
7672 if (cursor_in_echo_area == 0)
7673 fprintf (stderr, "\n");
7674 fflush (stderr);
7676 /* A null message buffer means that the frame hasn't really been
7677 initialized yet. Error messages get reported properly by
7678 cmd_error, so this must be just an informative message; toss it. */
7679 else if (INTERACTIVE
7680 && sf->glyphs_initialized_p
7681 && FRAME_MESSAGE_BUF (sf))
7683 Lisp_Object mini_window;
7684 Lisp_Object frame;
7685 struct frame *f;
7687 /* Get the frame containing the mini-buffer
7688 that the selected frame is using. */
7689 mini_window = FRAME_MINIBUF_WINDOW (sf);
7690 frame = XWINDOW (mini_window)->frame;
7691 f = XFRAME (frame);
7693 FRAME_SAMPLE_VISIBILITY (f);
7694 if (FRAME_VISIBLE_P (sf)
7695 && !FRAME_VISIBLE_P (f))
7696 Fmake_frame_visible (frame);
7698 if (STRINGP (m) && SCHARS (m) > 0)
7700 set_message (NULL, m, nbytes, multibyte);
7701 if (minibuffer_auto_raise)
7702 Fraise_frame (frame);
7703 /* Assume we are not echoing.
7704 (If we are, echo_now will override this.) */
7705 echo_message_buffer = Qnil;
7707 else
7708 clear_message (1, 1);
7710 do_pending_window_change (0);
7711 echo_area_display (1);
7712 do_pending_window_change (0);
7713 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7714 (*frame_up_to_date_hook) (f);
7719 /* Display a null-terminated echo area message M. If M is 0, clear
7720 out any existing message, and let the mini-buffer text show through.
7722 The buffer M must continue to exist until after the echo area gets
7723 cleared or some other message gets displayed there. Do not pass
7724 text that is stored in a Lisp string. Do not pass text in a buffer
7725 that was alloca'd. */
7727 void
7728 message1 (m)
7729 char *m;
7731 message2 (m, (m ? strlen (m) : 0), 0);
7735 /* The non-logging counterpart of message1. */
7737 void
7738 message1_nolog (m)
7739 char *m;
7741 message2_nolog (m, (m ? strlen (m) : 0), 0);
7744 /* Display a message M which contains a single %s
7745 which gets replaced with STRING. */
7747 void
7748 message_with_string (m, string, log)
7749 char *m;
7750 Lisp_Object string;
7751 int log;
7753 CHECK_STRING (string);
7755 if (noninteractive)
7757 if (m)
7759 if (noninteractive_need_newline)
7760 putc ('\n', stderr);
7761 noninteractive_need_newline = 0;
7762 fprintf (stderr, m, SDATA (string));
7763 if (cursor_in_echo_area == 0)
7764 fprintf (stderr, "\n");
7765 fflush (stderr);
7768 else if (INTERACTIVE)
7770 /* The frame whose minibuffer we're going to display the message on.
7771 It may be larger than the selected frame, so we need
7772 to use its buffer, not the selected frame's buffer. */
7773 Lisp_Object mini_window;
7774 struct frame *f, *sf = SELECTED_FRAME ();
7776 /* Get the frame containing the minibuffer
7777 that the selected frame is using. */
7778 mini_window = FRAME_MINIBUF_WINDOW (sf);
7779 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7781 /* A null message buffer means that the frame hasn't really been
7782 initialized yet. Error messages get reported properly by
7783 cmd_error, so this must be just an informative message; toss it. */
7784 if (FRAME_MESSAGE_BUF (f))
7786 Lisp_Object args[2], message;
7787 struct gcpro gcpro1, gcpro2;
7789 args[0] = build_string (m);
7790 args[1] = message = string;
7791 GCPRO2 (args[0], message);
7792 gcpro1.nvars = 2;
7794 message = Fformat (2, args);
7796 if (log)
7797 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7798 else
7799 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7801 UNGCPRO;
7803 /* Print should start at the beginning of the message
7804 buffer next time. */
7805 message_buf_print = 0;
7811 /* Dump an informative message to the minibuf. If M is 0, clear out
7812 any existing message, and let the mini-buffer text show through. */
7814 /* VARARGS 1 */
7815 void
7816 message (m, a1, a2, a3)
7817 char *m;
7818 EMACS_INT a1, a2, a3;
7820 if (noninteractive)
7822 if (m)
7824 if (noninteractive_need_newline)
7825 putc ('\n', stderr);
7826 noninteractive_need_newline = 0;
7827 fprintf (stderr, m, a1, a2, a3);
7828 if (cursor_in_echo_area == 0)
7829 fprintf (stderr, "\n");
7830 fflush (stderr);
7833 else if (INTERACTIVE)
7835 /* The frame whose mini-buffer we're going to display the message
7836 on. It may be larger than the selected frame, so we need to
7837 use its buffer, not the selected frame's buffer. */
7838 Lisp_Object mini_window;
7839 struct frame *f, *sf = SELECTED_FRAME ();
7841 /* Get the frame containing the mini-buffer
7842 that the selected frame is using. */
7843 mini_window = FRAME_MINIBUF_WINDOW (sf);
7844 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7846 /* A null message buffer means that the frame hasn't really been
7847 initialized yet. Error messages get reported properly by
7848 cmd_error, so this must be just an informative message; toss
7849 it. */
7850 if (FRAME_MESSAGE_BUF (f))
7852 if (m)
7854 int len;
7855 #ifdef NO_ARG_ARRAY
7856 char *a[3];
7857 a[0] = (char *) a1;
7858 a[1] = (char *) a2;
7859 a[2] = (char *) a3;
7861 len = doprnt (FRAME_MESSAGE_BUF (f),
7862 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7863 #else
7864 len = doprnt (FRAME_MESSAGE_BUF (f),
7865 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7866 (char **) &a1);
7867 #endif /* NO_ARG_ARRAY */
7869 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7871 else
7872 message1 (0);
7874 /* Print should start at the beginning of the message
7875 buffer next time. */
7876 message_buf_print = 0;
7882 /* The non-logging version of message. */
7884 void
7885 message_nolog (m, a1, a2, a3)
7886 char *m;
7887 EMACS_INT a1, a2, a3;
7889 Lisp_Object old_log_max;
7890 old_log_max = Vmessage_log_max;
7891 Vmessage_log_max = Qnil;
7892 message (m, a1, a2, a3);
7893 Vmessage_log_max = old_log_max;
7897 /* Display the current message in the current mini-buffer. This is
7898 only called from error handlers in process.c, and is not time
7899 critical. */
7901 void
7902 update_echo_area ()
7904 if (!NILP (echo_area_buffer[0]))
7906 Lisp_Object string;
7907 string = Fcurrent_message ();
7908 message3 (string, SBYTES (string),
7909 !NILP (current_buffer->enable_multibyte_characters));
7914 /* Make sure echo area buffers in `echo_buffers' are live.
7915 If they aren't, make new ones. */
7917 static void
7918 ensure_echo_area_buffers ()
7920 int i;
7922 for (i = 0; i < 2; ++i)
7923 if (!BUFFERP (echo_buffer[i])
7924 || NILP (XBUFFER (echo_buffer[i])->name))
7926 char name[30];
7927 Lisp_Object old_buffer;
7928 int j;
7930 old_buffer = echo_buffer[i];
7931 sprintf (name, " *Echo Area %d*", i);
7932 echo_buffer[i] = Fget_buffer_create (build_string (name));
7933 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7935 for (j = 0; j < 2; ++j)
7936 if (EQ (old_buffer, echo_area_buffer[j]))
7937 echo_area_buffer[j] = echo_buffer[i];
7942 /* Call FN with args A1..A4 with either the current or last displayed
7943 echo_area_buffer as current buffer.
7945 WHICH zero means use the current message buffer
7946 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7947 from echo_buffer[] and clear it.
7949 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7950 suitable buffer from echo_buffer[] and clear it.
7952 Value is what FN returns. */
7954 static int
7955 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7956 struct window *w;
7957 int which;
7958 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7959 EMACS_INT a1;
7960 Lisp_Object a2;
7961 EMACS_INT a3, a4;
7963 Lisp_Object buffer;
7964 int this_one, the_other, clear_buffer_p, rc;
7965 int count = SPECPDL_INDEX ();
7967 /* If buffers aren't live, make new ones. */
7968 ensure_echo_area_buffers ();
7970 clear_buffer_p = 0;
7972 if (which == 0)
7973 this_one = 0, the_other = 1;
7974 else if (which > 0)
7975 this_one = 1, the_other = 0;
7977 /* Choose a suitable buffer from echo_buffer[] is we don't
7978 have one. */
7979 if (NILP (echo_area_buffer[this_one]))
7981 echo_area_buffer[this_one]
7982 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7983 ? echo_buffer[the_other]
7984 : echo_buffer[this_one]);
7985 clear_buffer_p = 1;
7988 buffer = echo_area_buffer[this_one];
7990 /* Don't get confused by reusing the buffer used for echoing
7991 for a different purpose. */
7992 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7993 cancel_echoing ();
7995 record_unwind_protect (unwind_with_echo_area_buffer,
7996 with_echo_area_buffer_unwind_data (w));
7998 /* Make the echo area buffer current. Note that for display
7999 purposes, it is not necessary that the displayed window's buffer
8000 == current_buffer, except for text property lookup. So, let's
8001 only set that buffer temporarily here without doing a full
8002 Fset_window_buffer. We must also change w->pointm, though,
8003 because otherwise an assertions in unshow_buffer fails, and Emacs
8004 aborts. */
8005 set_buffer_internal_1 (XBUFFER (buffer));
8006 if (w)
8008 w->buffer = buffer;
8009 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8012 current_buffer->undo_list = Qt;
8013 current_buffer->read_only = Qnil;
8014 specbind (Qinhibit_read_only, Qt);
8015 specbind (Qinhibit_modification_hooks, Qt);
8017 if (clear_buffer_p && Z > BEG)
8018 del_range (BEG, Z);
8020 xassert (BEGV >= BEG);
8021 xassert (ZV <= Z && ZV >= BEGV);
8023 rc = fn (a1, a2, a3, a4);
8025 xassert (BEGV >= BEG);
8026 xassert (ZV <= Z && ZV >= BEGV);
8028 unbind_to (count, Qnil);
8029 return rc;
8033 /* Save state that should be preserved around the call to the function
8034 FN called in with_echo_area_buffer. */
8036 static Lisp_Object
8037 with_echo_area_buffer_unwind_data (w)
8038 struct window *w;
8040 int i = 0;
8041 Lisp_Object vector;
8043 /* Reduce consing by keeping one vector in
8044 Vwith_echo_area_save_vector. */
8045 vector = Vwith_echo_area_save_vector;
8046 Vwith_echo_area_save_vector = Qnil;
8048 if (NILP (vector))
8049 vector = Fmake_vector (make_number (7), Qnil);
8051 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8052 AREF (vector, i) = Vdeactivate_mark, ++i;
8053 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8055 if (w)
8057 XSETWINDOW (AREF (vector, i), w); ++i;
8058 AREF (vector, i) = w->buffer; ++i;
8059 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8060 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8062 else
8064 int end = i + 4;
8065 for (; i < end; ++i)
8066 AREF (vector, i) = Qnil;
8069 xassert (i == ASIZE (vector));
8070 return vector;
8074 /* Restore global state from VECTOR which was created by
8075 with_echo_area_buffer_unwind_data. */
8077 static Lisp_Object
8078 unwind_with_echo_area_buffer (vector)
8079 Lisp_Object vector;
8081 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8082 Vdeactivate_mark = AREF (vector, 1);
8083 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8085 if (WINDOWP (AREF (vector, 3)))
8087 struct window *w;
8088 Lisp_Object buffer, charpos, bytepos;
8090 w = XWINDOW (AREF (vector, 3));
8091 buffer = AREF (vector, 4);
8092 charpos = AREF (vector, 5);
8093 bytepos = AREF (vector, 6);
8095 w->buffer = buffer;
8096 set_marker_both (w->pointm, buffer,
8097 XFASTINT (charpos), XFASTINT (bytepos));
8100 Vwith_echo_area_save_vector = vector;
8101 return Qnil;
8105 /* Set up the echo area for use by print functions. MULTIBYTE_P
8106 non-zero means we will print multibyte. */
8108 void
8109 setup_echo_area_for_printing (multibyte_p)
8110 int multibyte_p;
8112 /* If we can't find an echo area any more, exit. */
8113 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8114 Fkill_emacs (Qnil);
8116 ensure_echo_area_buffers ();
8118 if (!message_buf_print)
8120 /* A message has been output since the last time we printed.
8121 Choose a fresh echo area buffer. */
8122 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8123 echo_area_buffer[0] = echo_buffer[1];
8124 else
8125 echo_area_buffer[0] = echo_buffer[0];
8127 /* Switch to that buffer and clear it. */
8128 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8129 current_buffer->truncate_lines = Qnil;
8131 if (Z > BEG)
8133 int count = SPECPDL_INDEX ();
8134 specbind (Qinhibit_read_only, Qt);
8135 /* Note that undo recording is always disabled. */
8136 del_range (BEG, Z);
8137 unbind_to (count, Qnil);
8139 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8141 /* Set up the buffer for the multibyteness we need. */
8142 if (multibyte_p
8143 != !NILP (current_buffer->enable_multibyte_characters))
8144 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8146 /* Raise the frame containing the echo area. */
8147 if (minibuffer_auto_raise)
8149 struct frame *sf = SELECTED_FRAME ();
8150 Lisp_Object mini_window;
8151 mini_window = FRAME_MINIBUF_WINDOW (sf);
8152 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8155 message_log_maybe_newline ();
8156 message_buf_print = 1;
8158 else
8160 if (NILP (echo_area_buffer[0]))
8162 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8163 echo_area_buffer[0] = echo_buffer[1];
8164 else
8165 echo_area_buffer[0] = echo_buffer[0];
8168 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8170 /* Someone switched buffers between print requests. */
8171 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8172 current_buffer->truncate_lines = Qnil;
8178 /* Display an echo area message in window W. Value is non-zero if W's
8179 height is changed. If display_last_displayed_message_p is
8180 non-zero, display the message that was last displayed, otherwise
8181 display the current message. */
8183 static int
8184 display_echo_area (w)
8185 struct window *w;
8187 int i, no_message_p, window_height_changed_p, count;
8189 /* Temporarily disable garbage collections while displaying the echo
8190 area. This is done because a GC can print a message itself.
8191 That message would modify the echo area buffer's contents while a
8192 redisplay of the buffer is going on, and seriously confuse
8193 redisplay. */
8194 count = inhibit_garbage_collection ();
8196 /* If there is no message, we must call display_echo_area_1
8197 nevertheless because it resizes the window. But we will have to
8198 reset the echo_area_buffer in question to nil at the end because
8199 with_echo_area_buffer will sets it to an empty buffer. */
8200 i = display_last_displayed_message_p ? 1 : 0;
8201 no_message_p = NILP (echo_area_buffer[i]);
8203 window_height_changed_p
8204 = with_echo_area_buffer (w, display_last_displayed_message_p,
8205 display_echo_area_1,
8206 (EMACS_INT) w, Qnil, 0, 0);
8208 if (no_message_p)
8209 echo_area_buffer[i] = Qnil;
8211 unbind_to (count, Qnil);
8212 return window_height_changed_p;
8216 /* Helper for display_echo_area. Display the current buffer which
8217 contains the current echo area message in window W, a mini-window,
8218 a pointer to which is passed in A1. A2..A4 are currently not used.
8219 Change the height of W so that all of the message is displayed.
8220 Value is non-zero if height of W was changed. */
8222 static int
8223 display_echo_area_1 (a1, a2, a3, a4)
8224 EMACS_INT a1;
8225 Lisp_Object a2;
8226 EMACS_INT a3, a4;
8228 struct window *w = (struct window *) a1;
8229 Lisp_Object window;
8230 struct text_pos start;
8231 int window_height_changed_p = 0;
8233 /* Do this before displaying, so that we have a large enough glyph
8234 matrix for the display. If we can't get enough space for the
8235 whole text, display the last N lines. That works by setting w->start. */
8236 window_height_changed_p = resize_mini_window (w, 0);
8238 /* Use the starting position chosen by resize_mini_window. */
8239 SET_TEXT_POS_FROM_MARKER (start, w->start);
8241 /* Display. */
8242 clear_glyph_matrix (w->desired_matrix);
8243 XSETWINDOW (window, w);
8244 try_window (window, start, 0);
8246 return window_height_changed_p;
8250 /* Resize the echo area window to exactly the size needed for the
8251 currently displayed message, if there is one. If a mini-buffer
8252 is active, don't shrink it. */
8254 void
8255 resize_echo_area_exactly ()
8257 if (BUFFERP (echo_area_buffer[0])
8258 && WINDOWP (echo_area_window))
8260 struct window *w = XWINDOW (echo_area_window);
8261 int resized_p;
8262 Lisp_Object resize_exactly;
8264 if (minibuf_level == 0)
8265 resize_exactly = Qt;
8266 else
8267 resize_exactly = Qnil;
8269 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8270 (EMACS_INT) w, resize_exactly, 0, 0);
8271 if (resized_p)
8273 ++windows_or_buffers_changed;
8274 ++update_mode_lines;
8275 redisplay_internal (0);
8281 /* Callback function for with_echo_area_buffer, when used from
8282 resize_echo_area_exactly. A1 contains a pointer to the window to
8283 resize, EXACTLY non-nil means resize the mini-window exactly to the
8284 size of the text displayed. A3 and A4 are not used. Value is what
8285 resize_mini_window returns. */
8287 static int
8288 resize_mini_window_1 (a1, exactly, a3, a4)
8289 EMACS_INT a1;
8290 Lisp_Object exactly;
8291 EMACS_INT a3, a4;
8293 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8297 /* Resize mini-window W to fit the size of its contents. EXACT_P
8298 means size the window exactly to the size needed. Otherwise, it's
8299 only enlarged until W's buffer is empty.
8301 Set W->start to the right place to begin display. If the whole
8302 contents fit, start at the beginning. Otherwise, start so as
8303 to make the end of the contents appear. This is particularly
8304 important for y-or-n-p, but seems desirable generally.
8306 Value is non-zero if the window height has been changed. */
8309 resize_mini_window (w, exact_p)
8310 struct window *w;
8311 int exact_p;
8313 struct frame *f = XFRAME (w->frame);
8314 int window_height_changed_p = 0;
8316 xassert (MINI_WINDOW_P (w));
8318 /* By default, start display at the beginning. */
8319 set_marker_both (w->start, w->buffer,
8320 BUF_BEGV (XBUFFER (w->buffer)),
8321 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8323 /* Don't resize windows while redisplaying a window; it would
8324 confuse redisplay functions when the size of the window they are
8325 displaying changes from under them. Such a resizing can happen,
8326 for instance, when which-func prints a long message while
8327 we are running fontification-functions. We're running these
8328 functions with safe_call which binds inhibit-redisplay to t. */
8329 if (!NILP (Vinhibit_redisplay))
8330 return 0;
8332 /* Nil means don't try to resize. */
8333 if (NILP (Vresize_mini_windows)
8334 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8335 return 0;
8337 if (!FRAME_MINIBUF_ONLY_P (f))
8339 struct it it;
8340 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8341 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8342 int height, max_height;
8343 int unit = FRAME_LINE_HEIGHT (f);
8344 struct text_pos start;
8345 struct buffer *old_current_buffer = NULL;
8347 if (current_buffer != XBUFFER (w->buffer))
8349 old_current_buffer = current_buffer;
8350 set_buffer_internal (XBUFFER (w->buffer));
8353 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8355 /* Compute the max. number of lines specified by the user. */
8356 if (FLOATP (Vmax_mini_window_height))
8357 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8358 else if (INTEGERP (Vmax_mini_window_height))
8359 max_height = XINT (Vmax_mini_window_height);
8360 else
8361 max_height = total_height / 4;
8363 /* Correct that max. height if it's bogus. */
8364 max_height = max (1, max_height);
8365 max_height = min (total_height, max_height);
8367 /* Find out the height of the text in the window. */
8368 if (it.truncate_lines_p)
8369 height = 1;
8370 else
8372 last_height = 0;
8373 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8374 if (it.max_ascent == 0 && it.max_descent == 0)
8375 height = it.current_y + last_height;
8376 else
8377 height = it.current_y + it.max_ascent + it.max_descent;
8378 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8379 height = (height + unit - 1) / unit;
8382 /* Compute a suitable window start. */
8383 if (height > max_height)
8385 height = max_height;
8386 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8387 move_it_vertically_backward (&it, (height - 1) * unit);
8388 start = it.current.pos;
8390 else
8391 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8392 SET_MARKER_FROM_TEXT_POS (w->start, start);
8394 if (EQ (Vresize_mini_windows, Qgrow_only))
8396 /* Let it grow only, until we display an empty message, in which
8397 case the window shrinks again. */
8398 if (height > WINDOW_TOTAL_LINES (w))
8400 int old_height = WINDOW_TOTAL_LINES (w);
8401 freeze_window_starts (f, 1);
8402 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8403 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8405 else if (height < WINDOW_TOTAL_LINES (w)
8406 && (exact_p || BEGV == ZV))
8408 int old_height = WINDOW_TOTAL_LINES (w);
8409 freeze_window_starts (f, 0);
8410 shrink_mini_window (w);
8411 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8414 else
8416 /* Always resize to exact size needed. */
8417 if (height > WINDOW_TOTAL_LINES (w))
8419 int old_height = WINDOW_TOTAL_LINES (w);
8420 freeze_window_starts (f, 1);
8421 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8422 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8424 else if (height < WINDOW_TOTAL_LINES (w))
8426 int old_height = WINDOW_TOTAL_LINES (w);
8427 freeze_window_starts (f, 0);
8428 shrink_mini_window (w);
8430 if (height)
8432 freeze_window_starts (f, 1);
8433 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8436 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8440 if (old_current_buffer)
8441 set_buffer_internal (old_current_buffer);
8444 return window_height_changed_p;
8448 /* Value is the current message, a string, or nil if there is no
8449 current message. */
8451 Lisp_Object
8452 current_message ()
8454 Lisp_Object msg;
8456 if (NILP (echo_area_buffer[0]))
8457 msg = Qnil;
8458 else
8460 with_echo_area_buffer (0, 0, current_message_1,
8461 (EMACS_INT) &msg, Qnil, 0, 0);
8462 if (NILP (msg))
8463 echo_area_buffer[0] = Qnil;
8466 return msg;
8470 static int
8471 current_message_1 (a1, a2, a3, a4)
8472 EMACS_INT a1;
8473 Lisp_Object a2;
8474 EMACS_INT a3, a4;
8476 Lisp_Object *msg = (Lisp_Object *) a1;
8478 if (Z > BEG)
8479 *msg = make_buffer_string (BEG, Z, 1);
8480 else
8481 *msg = Qnil;
8482 return 0;
8486 /* Push the current message on Vmessage_stack for later restauration
8487 by restore_message. Value is non-zero if the current message isn't
8488 empty. This is a relatively infrequent operation, so it's not
8489 worth optimizing. */
8492 push_message ()
8494 Lisp_Object msg;
8495 msg = current_message ();
8496 Vmessage_stack = Fcons (msg, Vmessage_stack);
8497 return STRINGP (msg);
8501 /* Restore message display from the top of Vmessage_stack. */
8503 void
8504 restore_message ()
8506 Lisp_Object msg;
8508 xassert (CONSP (Vmessage_stack));
8509 msg = XCAR (Vmessage_stack);
8510 if (STRINGP (msg))
8511 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8512 else
8513 message3_nolog (msg, 0, 0);
8517 /* Handler for record_unwind_protect calling pop_message. */
8519 Lisp_Object
8520 pop_message_unwind (dummy)
8521 Lisp_Object dummy;
8523 pop_message ();
8524 return Qnil;
8527 /* Pop the top-most entry off Vmessage_stack. */
8529 void
8530 pop_message ()
8532 xassert (CONSP (Vmessage_stack));
8533 Vmessage_stack = XCDR (Vmessage_stack);
8537 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8538 exits. If the stack is not empty, we have a missing pop_message
8539 somewhere. */
8541 void
8542 check_message_stack ()
8544 if (!NILP (Vmessage_stack))
8545 abort ();
8549 /* Truncate to NCHARS what will be displayed in the echo area the next
8550 time we display it---but don't redisplay it now. */
8552 void
8553 truncate_echo_area (nchars)
8554 int nchars;
8556 if (nchars == 0)
8557 echo_area_buffer[0] = Qnil;
8558 /* A null message buffer means that the frame hasn't really been
8559 initialized yet. Error messages get reported properly by
8560 cmd_error, so this must be just an informative message; toss it. */
8561 else if (!noninteractive
8562 && INTERACTIVE
8563 && !NILP (echo_area_buffer[0]))
8565 struct frame *sf = SELECTED_FRAME ();
8566 if (FRAME_MESSAGE_BUF (sf))
8567 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8572 /* Helper function for truncate_echo_area. Truncate the current
8573 message to at most NCHARS characters. */
8575 static int
8576 truncate_message_1 (nchars, a2, a3, a4)
8577 EMACS_INT nchars;
8578 Lisp_Object a2;
8579 EMACS_INT a3, a4;
8581 if (BEG + nchars < Z)
8582 del_range (BEG + nchars, Z);
8583 if (Z == BEG)
8584 echo_area_buffer[0] = Qnil;
8585 return 0;
8589 /* Set the current message to a substring of S or STRING.
8591 If STRING is a Lisp string, set the message to the first NBYTES
8592 bytes from STRING. NBYTES zero means use the whole string. If
8593 STRING is multibyte, the message will be displayed multibyte.
8595 If S is not null, set the message to the first LEN bytes of S. LEN
8596 zero means use the whole string. MULTIBYTE_P non-zero means S is
8597 multibyte. Display the message multibyte in that case.
8599 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8600 to t before calling set_message_1 (which calls insert).
8603 void
8604 set_message (s, string, nbytes, multibyte_p)
8605 const char *s;
8606 Lisp_Object string;
8607 int nbytes, multibyte_p;
8609 message_enable_multibyte
8610 = ((s && multibyte_p)
8611 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8613 with_echo_area_buffer (0, 0, set_message_1,
8614 (EMACS_INT) s, string, nbytes, multibyte_p);
8615 message_buf_print = 0;
8616 help_echo_showing_p = 0;
8620 /* Helper function for set_message. Arguments have the same meaning
8621 as there, with A1 corresponding to S and A2 corresponding to STRING
8622 This function is called with the echo area buffer being
8623 current. */
8625 static int
8626 set_message_1 (a1, a2, nbytes, multibyte_p)
8627 EMACS_INT a1;
8628 Lisp_Object a2;
8629 EMACS_INT nbytes, multibyte_p;
8631 const char *s = (const char *) a1;
8632 Lisp_Object string = a2;
8634 /* Change multibyteness of the echo buffer appropriately. */
8635 if (message_enable_multibyte
8636 != !NILP (current_buffer->enable_multibyte_characters))
8637 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8639 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8641 /* Insert new message at BEG. */
8642 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8643 Ferase_buffer ();
8645 if (STRINGP (string))
8647 int nchars;
8649 if (nbytes == 0)
8650 nbytes = SBYTES (string);
8651 nchars = string_byte_to_char (string, nbytes);
8653 /* This function takes care of single/multibyte conversion. We
8654 just have to ensure that the echo area buffer has the right
8655 setting of enable_multibyte_characters. */
8656 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8658 else if (s)
8660 if (nbytes == 0)
8661 nbytes = strlen (s);
8663 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8665 /* Convert from multi-byte to single-byte. */
8666 int i, c, n;
8667 unsigned char work[1];
8669 /* Convert a multibyte string to single-byte. */
8670 for (i = 0; i < nbytes; i += n)
8672 c = string_char_and_length (s + i, nbytes - i, &n);
8673 work[0] = (SINGLE_BYTE_CHAR_P (c)
8675 : multibyte_char_to_unibyte (c, Qnil));
8676 insert_1_both (work, 1, 1, 1, 0, 0);
8679 else if (!multibyte_p
8680 && !NILP (current_buffer->enable_multibyte_characters))
8682 /* Convert from single-byte to multi-byte. */
8683 int i, c, n;
8684 const unsigned char *msg = (const unsigned char *) s;
8685 unsigned char str[MAX_MULTIBYTE_LENGTH];
8687 /* Convert a single-byte string to multibyte. */
8688 for (i = 0; i < nbytes; i++)
8690 c = unibyte_char_to_multibyte (msg[i]);
8691 n = CHAR_STRING (c, str);
8692 insert_1_both (str, 1, n, 1, 0, 0);
8695 else
8696 insert_1 (s, nbytes, 1, 0, 0);
8699 return 0;
8703 /* Clear messages. CURRENT_P non-zero means clear the current
8704 message. LAST_DISPLAYED_P non-zero means clear the message
8705 last displayed. */
8707 void
8708 clear_message (current_p, last_displayed_p)
8709 int current_p, last_displayed_p;
8711 if (current_p)
8713 echo_area_buffer[0] = Qnil;
8714 message_cleared_p = 1;
8717 if (last_displayed_p)
8718 echo_area_buffer[1] = Qnil;
8720 message_buf_print = 0;
8723 /* Clear garbaged frames.
8725 This function is used where the old redisplay called
8726 redraw_garbaged_frames which in turn called redraw_frame which in
8727 turn called clear_frame. The call to clear_frame was a source of
8728 flickering. I believe a clear_frame is not necessary. It should
8729 suffice in the new redisplay to invalidate all current matrices,
8730 and ensure a complete redisplay of all windows. */
8732 static void
8733 clear_garbaged_frames ()
8735 if (frame_garbaged)
8737 Lisp_Object tail, frame;
8738 int changed_count = 0;
8740 FOR_EACH_FRAME (tail, frame)
8742 struct frame *f = XFRAME (frame);
8744 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8746 if (f->resized_p)
8748 Fredraw_frame (frame);
8749 f->force_flush_display_p = 1;
8751 clear_current_matrices (f);
8752 changed_count++;
8753 f->garbaged = 0;
8754 f->resized_p = 0;
8758 frame_garbaged = 0;
8759 if (changed_count)
8760 ++windows_or_buffers_changed;
8765 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8766 is non-zero update selected_frame. Value is non-zero if the
8767 mini-windows height has been changed. */
8769 static int
8770 echo_area_display (update_frame_p)
8771 int update_frame_p;
8773 Lisp_Object mini_window;
8774 struct window *w;
8775 struct frame *f;
8776 int window_height_changed_p = 0;
8777 struct frame *sf = SELECTED_FRAME ();
8779 mini_window = FRAME_MINIBUF_WINDOW (sf);
8780 w = XWINDOW (mini_window);
8781 f = XFRAME (WINDOW_FRAME (w));
8783 /* Don't display if frame is invisible or not yet initialized. */
8784 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8785 return 0;
8787 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8788 #ifndef MAC_OS8
8789 #ifdef HAVE_WINDOW_SYSTEM
8790 /* When Emacs starts, selected_frame may be a visible terminal
8791 frame, even if we run under a window system. If we let this
8792 through, a message would be displayed on the terminal. */
8793 if (EQ (selected_frame, Vterminal_frame)
8794 && !NILP (Vwindow_system))
8795 return 0;
8796 #endif /* HAVE_WINDOW_SYSTEM */
8797 #endif
8799 /* Redraw garbaged frames. */
8800 if (frame_garbaged)
8801 clear_garbaged_frames ();
8803 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8805 echo_area_window = mini_window;
8806 window_height_changed_p = display_echo_area (w);
8807 w->must_be_updated_p = 1;
8809 /* Update the display, unless called from redisplay_internal.
8810 Also don't update the screen during redisplay itself. The
8811 update will happen at the end of redisplay, and an update
8812 here could cause confusion. */
8813 if (update_frame_p && !redisplaying_p)
8815 int n = 0;
8817 /* If the display update has been interrupted by pending
8818 input, update mode lines in the frame. Due to the
8819 pending input, it might have been that redisplay hasn't
8820 been called, so that mode lines above the echo area are
8821 garbaged. This looks odd, so we prevent it here. */
8822 if (!display_completed)
8823 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8825 if (window_height_changed_p
8826 /* Don't do this if Emacs is shutting down. Redisplay
8827 needs to run hooks. */
8828 && !NILP (Vrun_hooks))
8830 /* Must update other windows. Likewise as in other
8831 cases, don't let this update be interrupted by
8832 pending input. */
8833 int count = SPECPDL_INDEX ();
8834 specbind (Qredisplay_dont_pause, Qt);
8835 windows_or_buffers_changed = 1;
8836 redisplay_internal (0);
8837 unbind_to (count, Qnil);
8839 else if (FRAME_WINDOW_P (f) && n == 0)
8841 /* Window configuration is the same as before.
8842 Can do with a display update of the echo area,
8843 unless we displayed some mode lines. */
8844 update_single_window (w, 1);
8845 rif->flush_display (f);
8847 else
8848 update_frame (f, 1, 1);
8850 /* If cursor is in the echo area, make sure that the next
8851 redisplay displays the minibuffer, so that the cursor will
8852 be replaced with what the minibuffer wants. */
8853 if (cursor_in_echo_area)
8854 ++windows_or_buffers_changed;
8857 else if (!EQ (mini_window, selected_window))
8858 windows_or_buffers_changed++;
8860 /* The current message is now also the last one displayed. */
8861 echo_area_buffer[1] = echo_area_buffer[0];
8863 /* Prevent redisplay optimization in redisplay_internal by resetting
8864 this_line_start_pos. This is done because the mini-buffer now
8865 displays the message instead of its buffer text. */
8866 if (EQ (mini_window, selected_window))
8867 CHARPOS (this_line_start_pos) = 0;
8869 return window_height_changed_p;
8874 /***********************************************************************
8875 Mode Lines and Frame Titles
8876 ***********************************************************************/
8878 /* A buffer for constructing non-propertized mode-line strings and
8879 frame titles in it; allocated from the heap in init_xdisp and
8880 resized as needed in store_mode_line_noprop_char. */
8882 static char *mode_line_noprop_buf;
8884 /* The buffer's end, and a current output position in it. */
8886 static char *mode_line_noprop_buf_end;
8887 static char *mode_line_noprop_ptr;
8889 #define MODE_LINE_NOPROP_LEN(start) \
8890 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8892 static enum {
8893 MODE_LINE_DISPLAY = 0,
8894 MODE_LINE_TITLE,
8895 MODE_LINE_NOPROP,
8896 MODE_LINE_STRING
8897 } mode_line_target;
8899 /* Alist that caches the results of :propertize.
8900 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8901 static Lisp_Object mode_line_proptrans_alist;
8903 /* List of strings making up the mode-line. */
8904 static Lisp_Object mode_line_string_list;
8906 /* Base face property when building propertized mode line string. */
8907 static Lisp_Object mode_line_string_face;
8908 static Lisp_Object mode_line_string_face_prop;
8911 /* Unwind data for mode line strings */
8913 static Lisp_Object Vmode_line_unwind_vector;
8915 static Lisp_Object
8916 format_mode_line_unwind_data (obuf, save_proptrans)
8917 struct buffer *obuf;
8919 Lisp_Object vector;
8921 /* Reduce consing by keeping one vector in
8922 Vwith_echo_area_save_vector. */
8923 vector = Vmode_line_unwind_vector;
8924 Vmode_line_unwind_vector = Qnil;
8926 if (NILP (vector))
8927 vector = Fmake_vector (make_number (7), Qnil);
8929 AREF (vector, 0) = make_number (mode_line_target);
8930 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8931 AREF (vector, 2) = mode_line_string_list;
8932 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8933 AREF (vector, 4) = mode_line_string_face;
8934 AREF (vector, 5) = mode_line_string_face_prop;
8936 if (obuf)
8937 XSETBUFFER (AREF (vector, 6), obuf);
8938 else
8939 AREF (vector, 6) = Qnil;
8941 return vector;
8944 static Lisp_Object
8945 unwind_format_mode_line (vector)
8946 Lisp_Object vector;
8948 mode_line_target = XINT (AREF (vector, 0));
8949 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8950 mode_line_string_list = AREF (vector, 2);
8951 if (! EQ (AREF (vector, 3), Qt))
8952 mode_line_proptrans_alist = AREF (vector, 3);
8953 mode_line_string_face = AREF (vector, 4);
8954 mode_line_string_face_prop = AREF (vector, 5);
8956 if (!NILP (AREF (vector, 6)))
8958 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8959 AREF (vector, 6) = Qnil;
8962 Vmode_line_unwind_vector = vector;
8963 return Qnil;
8967 /* Store a single character C for the frame title in mode_line_noprop_buf.
8968 Re-allocate mode_line_noprop_buf if necessary. */
8970 static void
8971 #ifdef PROTOTYPES
8972 store_mode_line_noprop_char (char c)
8973 #else
8974 store_mode_line_noprop_char (c)
8975 char c;
8976 #endif
8978 /* If output position has reached the end of the allocated buffer,
8979 double the buffer's size. */
8980 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8982 int len = MODE_LINE_NOPROP_LEN (0);
8983 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8984 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8985 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8986 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8989 *mode_line_noprop_ptr++ = c;
8993 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8994 mode_line_noprop_ptr. STR is the string to store. Do not copy
8995 characters that yield more columns than PRECISION; PRECISION <= 0
8996 means copy the whole string. Pad with spaces until FIELD_WIDTH
8997 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8998 pad. Called from display_mode_element when it is used to build a
8999 frame title. */
9001 static int
9002 store_mode_line_noprop (str, field_width, precision)
9003 const unsigned char *str;
9004 int field_width, precision;
9006 int n = 0;
9007 int dummy, nbytes;
9009 /* Copy at most PRECISION chars from STR. */
9010 nbytes = strlen (str);
9011 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9012 while (nbytes--)
9013 store_mode_line_noprop_char (*str++);
9015 /* Fill up with spaces until FIELD_WIDTH reached. */
9016 while (field_width > 0
9017 && n < field_width)
9019 store_mode_line_noprop_char (' ');
9020 ++n;
9023 return n;
9026 /***********************************************************************
9027 Frame Titles
9028 ***********************************************************************/
9030 #ifdef HAVE_WINDOW_SYSTEM
9032 /* Set the title of FRAME, if it has changed. The title format is
9033 Vicon_title_format if FRAME is iconified, otherwise it is
9034 frame_title_format. */
9036 static void
9037 x_consider_frame_title (frame)
9038 Lisp_Object frame;
9040 struct frame *f = XFRAME (frame);
9042 if (FRAME_WINDOW_P (f)
9043 || FRAME_MINIBUF_ONLY_P (f)
9044 || f->explicit_name)
9046 /* Do we have more than one visible frame on this X display? */
9047 Lisp_Object tail;
9048 Lisp_Object fmt;
9049 int title_start;
9050 char *title;
9051 int len;
9052 struct it it;
9053 int count = SPECPDL_INDEX ();
9055 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9057 Lisp_Object other_frame = XCAR (tail);
9058 struct frame *tf = XFRAME (other_frame);
9060 if (tf != f
9061 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9062 && !FRAME_MINIBUF_ONLY_P (tf)
9063 && !EQ (other_frame, tip_frame)
9064 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9065 break;
9068 /* Set global variable indicating that multiple frames exist. */
9069 multiple_frames = CONSP (tail);
9071 /* Switch to the buffer of selected window of the frame. Set up
9072 mode_line_target so that display_mode_element will output into
9073 mode_line_noprop_buf; then display the title. */
9074 record_unwind_protect (unwind_format_mode_line,
9075 format_mode_line_unwind_data (current_buffer, 0));
9077 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9078 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9080 mode_line_target = MODE_LINE_TITLE;
9081 title_start = MODE_LINE_NOPROP_LEN (0);
9082 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9083 NULL, DEFAULT_FACE_ID);
9084 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9085 len = MODE_LINE_NOPROP_LEN (title_start);
9086 title = mode_line_noprop_buf + title_start;
9087 unbind_to (count, Qnil);
9089 /* Set the title only if it's changed. This avoids consing in
9090 the common case where it hasn't. (If it turns out that we've
9091 already wasted too much time by walking through the list with
9092 display_mode_element, then we might need to optimize at a
9093 higher level than this.) */
9094 if (! STRINGP (f->name)
9095 || SBYTES (f->name) != len
9096 || bcmp (title, SDATA (f->name), len) != 0)
9097 x_implicitly_set_name (f, make_string (title, len), Qnil);
9101 #endif /* not HAVE_WINDOW_SYSTEM */
9106 /***********************************************************************
9107 Menu Bars
9108 ***********************************************************************/
9111 /* Prepare for redisplay by updating menu-bar item lists when
9112 appropriate. This can call eval. */
9114 void
9115 prepare_menu_bars ()
9117 int all_windows;
9118 struct gcpro gcpro1, gcpro2;
9119 struct frame *f;
9120 Lisp_Object tooltip_frame;
9122 #ifdef HAVE_WINDOW_SYSTEM
9123 tooltip_frame = tip_frame;
9124 #else
9125 tooltip_frame = Qnil;
9126 #endif
9128 /* Update all frame titles based on their buffer names, etc. We do
9129 this before the menu bars so that the buffer-menu will show the
9130 up-to-date frame titles. */
9131 #ifdef HAVE_WINDOW_SYSTEM
9132 if (windows_or_buffers_changed || update_mode_lines)
9134 Lisp_Object tail, frame;
9136 FOR_EACH_FRAME (tail, frame)
9138 f = XFRAME (frame);
9139 if (!EQ (frame, tooltip_frame)
9140 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9141 x_consider_frame_title (frame);
9144 #endif /* HAVE_WINDOW_SYSTEM */
9146 /* Update the menu bar item lists, if appropriate. This has to be
9147 done before any actual redisplay or generation of display lines. */
9148 all_windows = (update_mode_lines
9149 || buffer_shared > 1
9150 || windows_or_buffers_changed);
9151 if (all_windows)
9153 Lisp_Object tail, frame;
9154 int count = SPECPDL_INDEX ();
9155 /* 1 means that update_menu_bar has run its hooks
9156 so any further calls to update_menu_bar shouldn't do so again. */
9157 int menu_bar_hooks_run = 0;
9159 record_unwind_save_match_data ();
9161 FOR_EACH_FRAME (tail, frame)
9163 f = XFRAME (frame);
9165 /* Ignore tooltip frame. */
9166 if (EQ (frame, tooltip_frame))
9167 continue;
9169 /* If a window on this frame changed size, report that to
9170 the user and clear the size-change flag. */
9171 if (FRAME_WINDOW_SIZES_CHANGED (f))
9173 Lisp_Object functions;
9175 /* Clear flag first in case we get an error below. */
9176 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9177 functions = Vwindow_size_change_functions;
9178 GCPRO2 (tail, functions);
9180 while (CONSP (functions))
9182 call1 (XCAR (functions), frame);
9183 functions = XCDR (functions);
9185 UNGCPRO;
9188 GCPRO1 (tail);
9189 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9190 #ifdef HAVE_WINDOW_SYSTEM
9191 update_tool_bar (f, 0);
9192 #ifdef MAC_OS
9193 mac_update_title_bar (f, 0);
9194 #endif
9195 #endif
9196 UNGCPRO;
9199 unbind_to (count, Qnil);
9201 else
9203 struct frame *sf = SELECTED_FRAME ();
9204 update_menu_bar (sf, 1, 0);
9205 #ifdef HAVE_WINDOW_SYSTEM
9206 update_tool_bar (sf, 1);
9207 #ifdef MAC_OS
9208 mac_update_title_bar (sf, 1);
9209 #endif
9210 #endif
9213 /* Motif needs this. See comment in xmenu.c. Turn it off when
9214 pending_menu_activation is not defined. */
9215 #ifdef USE_X_TOOLKIT
9216 pending_menu_activation = 0;
9217 #endif
9221 /* Update the menu bar item list for frame F. This has to be done
9222 before we start to fill in any display lines, because it can call
9223 eval.
9225 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9227 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9228 already ran the menu bar hooks for this redisplay, so there
9229 is no need to run them again. The return value is the
9230 updated value of this flag, to pass to the next call. */
9232 static int
9233 update_menu_bar (f, save_match_data, hooks_run)
9234 struct frame *f;
9235 int save_match_data;
9236 int hooks_run;
9238 Lisp_Object window;
9239 register struct window *w;
9241 /* If called recursively during a menu update, do nothing. This can
9242 happen when, for instance, an activate-menubar-hook causes a
9243 redisplay. */
9244 if (inhibit_menubar_update)
9245 return hooks_run;
9247 window = FRAME_SELECTED_WINDOW (f);
9248 w = XWINDOW (window);
9250 #if 0 /* The if statement below this if statement used to include the
9251 condition !NILP (w->update_mode_line), rather than using
9252 update_mode_lines directly, and this if statement may have
9253 been added to make that condition work. Now the if
9254 statement below matches its comment, this isn't needed. */
9255 if (update_mode_lines)
9256 w->update_mode_line = Qt;
9257 #endif
9259 if (FRAME_WINDOW_P (f)
9261 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9262 || defined (USE_GTK)
9263 FRAME_EXTERNAL_MENU_BAR (f)
9264 #else
9265 FRAME_MENU_BAR_LINES (f) > 0
9266 #endif
9267 : FRAME_MENU_BAR_LINES (f) > 0)
9269 /* If the user has switched buffers or windows, we need to
9270 recompute to reflect the new bindings. But we'll
9271 recompute when update_mode_lines is set too; that means
9272 that people can use force-mode-line-update to request
9273 that the menu bar be recomputed. The adverse effect on
9274 the rest of the redisplay algorithm is about the same as
9275 windows_or_buffers_changed anyway. */
9276 if (windows_or_buffers_changed
9277 /* This used to test w->update_mode_line, but we believe
9278 there is no need to recompute the menu in that case. */
9279 || update_mode_lines
9280 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9281 < BUF_MODIFF (XBUFFER (w->buffer)))
9282 != !NILP (w->last_had_star))
9283 || ((!NILP (Vtransient_mark_mode)
9284 && !NILP (XBUFFER (w->buffer)->mark_active))
9285 != !NILP (w->region_showing)))
9287 struct buffer *prev = current_buffer;
9288 int count = SPECPDL_INDEX ();
9290 specbind (Qinhibit_menubar_update, Qt);
9292 set_buffer_internal_1 (XBUFFER (w->buffer));
9293 if (save_match_data)
9294 record_unwind_save_match_data ();
9295 if (NILP (Voverriding_local_map_menu_flag))
9297 specbind (Qoverriding_terminal_local_map, Qnil);
9298 specbind (Qoverriding_local_map, Qnil);
9301 if (!hooks_run)
9303 /* Run the Lucid hook. */
9304 safe_run_hooks (Qactivate_menubar_hook);
9306 /* If it has changed current-menubar from previous value,
9307 really recompute the menu-bar from the value. */
9308 if (! NILP (Vlucid_menu_bar_dirty_flag))
9309 call0 (Qrecompute_lucid_menubar);
9311 safe_run_hooks (Qmenu_bar_update_hook);
9313 hooks_run = 1;
9316 XSETFRAME (Vmenu_updating_frame, f);
9317 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9319 /* Redisplay the menu bar in case we changed it. */
9320 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9321 || defined (USE_GTK)
9322 if (FRAME_WINDOW_P (f))
9324 #ifdef MAC_OS
9325 /* All frames on Mac OS share the same menubar. So only
9326 the selected frame should be allowed to set it. */
9327 if (f == SELECTED_FRAME ())
9328 #endif
9329 set_frame_menubar (f, 0, 0);
9331 else
9332 /* On a terminal screen, the menu bar is an ordinary screen
9333 line, and this makes it get updated. */
9334 w->update_mode_line = Qt;
9335 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9336 /* In the non-toolkit version, the menu bar is an ordinary screen
9337 line, and this makes it get updated. */
9338 w->update_mode_line = Qt;
9339 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9341 unbind_to (count, Qnil);
9342 set_buffer_internal_1 (prev);
9346 return hooks_run;
9351 /***********************************************************************
9352 Output Cursor
9353 ***********************************************************************/
9355 #ifdef HAVE_WINDOW_SYSTEM
9357 /* EXPORT:
9358 Nominal cursor position -- where to draw output.
9359 HPOS and VPOS are window relative glyph matrix coordinates.
9360 X and Y are window relative pixel coordinates. */
9362 struct cursor_pos output_cursor;
9365 /* EXPORT:
9366 Set the global variable output_cursor to CURSOR. All cursor
9367 positions are relative to updated_window. */
9369 void
9370 set_output_cursor (cursor)
9371 struct cursor_pos *cursor;
9373 output_cursor.hpos = cursor->hpos;
9374 output_cursor.vpos = cursor->vpos;
9375 output_cursor.x = cursor->x;
9376 output_cursor.y = cursor->y;
9380 /* EXPORT for RIF:
9381 Set a nominal cursor position.
9383 HPOS and VPOS are column/row positions in a window glyph matrix. X
9384 and Y are window text area relative pixel positions.
9386 If this is done during an update, updated_window will contain the
9387 window that is being updated and the position is the future output
9388 cursor position for that window. If updated_window is null, use
9389 selected_window and display the cursor at the given position. */
9391 void
9392 x_cursor_to (vpos, hpos, y, x)
9393 int vpos, hpos, y, x;
9395 struct window *w;
9397 /* If updated_window is not set, work on selected_window. */
9398 if (updated_window)
9399 w = updated_window;
9400 else
9401 w = XWINDOW (selected_window);
9403 /* Set the output cursor. */
9404 output_cursor.hpos = hpos;
9405 output_cursor.vpos = vpos;
9406 output_cursor.x = x;
9407 output_cursor.y = y;
9409 /* If not called as part of an update, really display the cursor.
9410 This will also set the cursor position of W. */
9411 if (updated_window == NULL)
9413 BLOCK_INPUT;
9414 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9415 if (rif->flush_display_optional)
9416 rif->flush_display_optional (SELECTED_FRAME ());
9417 UNBLOCK_INPUT;
9421 #endif /* HAVE_WINDOW_SYSTEM */
9424 /***********************************************************************
9425 Tool-bars
9426 ***********************************************************************/
9428 #ifdef HAVE_WINDOW_SYSTEM
9430 /* Where the mouse was last time we reported a mouse event. */
9432 FRAME_PTR last_mouse_frame;
9434 /* Tool-bar item index of the item on which a mouse button was pressed
9435 or -1. */
9437 int last_tool_bar_item;
9440 /* Update the tool-bar item list for frame F. This has to be done
9441 before we start to fill in any display lines. Called from
9442 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9443 and restore it here. */
9445 static void
9446 update_tool_bar (f, save_match_data)
9447 struct frame *f;
9448 int save_match_data;
9450 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9451 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9452 #else
9453 int do_update = WINDOWP (f->tool_bar_window)
9454 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9455 #endif
9457 if (do_update)
9459 Lisp_Object window;
9460 struct window *w;
9462 window = FRAME_SELECTED_WINDOW (f);
9463 w = XWINDOW (window);
9465 /* If the user has switched buffers or windows, we need to
9466 recompute to reflect the new bindings. But we'll
9467 recompute when update_mode_lines is set too; that means
9468 that people can use force-mode-line-update to request
9469 that the menu bar be recomputed. The adverse effect on
9470 the rest of the redisplay algorithm is about the same as
9471 windows_or_buffers_changed anyway. */
9472 if (windows_or_buffers_changed
9473 || !NILP (w->update_mode_line)
9474 || update_mode_lines
9475 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9476 < BUF_MODIFF (XBUFFER (w->buffer)))
9477 != !NILP (w->last_had_star))
9478 || ((!NILP (Vtransient_mark_mode)
9479 && !NILP (XBUFFER (w->buffer)->mark_active))
9480 != !NILP (w->region_showing)))
9482 struct buffer *prev = current_buffer;
9483 int count = SPECPDL_INDEX ();
9484 Lisp_Object new_tool_bar;
9485 int new_n_tool_bar;
9486 struct gcpro gcpro1;
9488 /* Set current_buffer to the buffer of the selected
9489 window of the frame, so that we get the right local
9490 keymaps. */
9491 set_buffer_internal_1 (XBUFFER (w->buffer));
9493 /* Save match data, if we must. */
9494 if (save_match_data)
9495 record_unwind_save_match_data ();
9497 /* Make sure that we don't accidentally use bogus keymaps. */
9498 if (NILP (Voverriding_local_map_menu_flag))
9500 specbind (Qoverriding_terminal_local_map, Qnil);
9501 specbind (Qoverriding_local_map, Qnil);
9504 GCPRO1 (new_tool_bar);
9506 /* Build desired tool-bar items from keymaps. */
9507 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9508 &new_n_tool_bar);
9510 /* Redisplay the tool-bar if we changed it. */
9511 if (new_n_tool_bar != f->n_tool_bar_items
9512 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9514 /* Redisplay that happens asynchronously due to an expose event
9515 may access f->tool_bar_items. Make sure we update both
9516 variables within BLOCK_INPUT so no such event interrupts. */
9517 BLOCK_INPUT;
9518 f->tool_bar_items = new_tool_bar;
9519 f->n_tool_bar_items = new_n_tool_bar;
9520 w->update_mode_line = Qt;
9521 UNBLOCK_INPUT;
9524 UNGCPRO;
9526 unbind_to (count, Qnil);
9527 set_buffer_internal_1 (prev);
9533 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9534 F's desired tool-bar contents. F->tool_bar_items must have
9535 been set up previously by calling prepare_menu_bars. */
9537 static void
9538 build_desired_tool_bar_string (f)
9539 struct frame *f;
9541 int i, size, size_needed;
9542 struct gcpro gcpro1, gcpro2, gcpro3;
9543 Lisp_Object image, plist, props;
9545 image = plist = props = Qnil;
9546 GCPRO3 (image, plist, props);
9548 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9549 Otherwise, make a new string. */
9551 /* The size of the string we might be able to reuse. */
9552 size = (STRINGP (f->desired_tool_bar_string)
9553 ? SCHARS (f->desired_tool_bar_string)
9554 : 0);
9556 /* We need one space in the string for each image. */
9557 size_needed = f->n_tool_bar_items;
9559 /* Reuse f->desired_tool_bar_string, if possible. */
9560 if (size < size_needed || NILP (f->desired_tool_bar_string))
9561 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9562 make_number (' '));
9563 else
9565 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9566 Fremove_text_properties (make_number (0), make_number (size),
9567 props, f->desired_tool_bar_string);
9570 /* Put a `display' property on the string for the images to display,
9571 put a `menu_item' property on tool-bar items with a value that
9572 is the index of the item in F's tool-bar item vector. */
9573 for (i = 0; i < f->n_tool_bar_items; ++i)
9575 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9577 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9578 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9579 int hmargin, vmargin, relief, idx, end;
9580 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9582 /* If image is a vector, choose the image according to the
9583 button state. */
9584 image = PROP (TOOL_BAR_ITEM_IMAGES);
9585 if (VECTORP (image))
9587 if (enabled_p)
9588 idx = (selected_p
9589 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9590 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9591 else
9592 idx = (selected_p
9593 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9594 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9596 xassert (ASIZE (image) >= idx);
9597 image = AREF (image, idx);
9599 else
9600 idx = -1;
9602 /* Ignore invalid image specifications. */
9603 if (!valid_image_p (image))
9604 continue;
9606 /* Display the tool-bar button pressed, or depressed. */
9607 plist = Fcopy_sequence (XCDR (image));
9609 /* Compute margin and relief to draw. */
9610 relief = (tool_bar_button_relief >= 0
9611 ? tool_bar_button_relief
9612 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9613 hmargin = vmargin = relief;
9615 if (INTEGERP (Vtool_bar_button_margin)
9616 && XINT (Vtool_bar_button_margin) > 0)
9618 hmargin += XFASTINT (Vtool_bar_button_margin);
9619 vmargin += XFASTINT (Vtool_bar_button_margin);
9621 else if (CONSP (Vtool_bar_button_margin))
9623 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9624 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9625 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9627 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9628 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9629 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9632 if (auto_raise_tool_bar_buttons_p)
9634 /* Add a `:relief' property to the image spec if the item is
9635 selected. */
9636 if (selected_p)
9638 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9639 hmargin -= relief;
9640 vmargin -= relief;
9643 else
9645 /* If image is selected, display it pressed, i.e. with a
9646 negative relief. If it's not selected, display it with a
9647 raised relief. */
9648 plist = Fplist_put (plist, QCrelief,
9649 (selected_p
9650 ? make_number (-relief)
9651 : make_number (relief)));
9652 hmargin -= relief;
9653 vmargin -= relief;
9656 /* Put a margin around the image. */
9657 if (hmargin || vmargin)
9659 if (hmargin == vmargin)
9660 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9661 else
9662 plist = Fplist_put (plist, QCmargin,
9663 Fcons (make_number (hmargin),
9664 make_number (vmargin)));
9667 /* If button is not enabled, and we don't have special images
9668 for the disabled state, make the image appear disabled by
9669 applying an appropriate algorithm to it. */
9670 if (!enabled_p && idx < 0)
9671 plist = Fplist_put (plist, QCconversion, Qdisabled);
9673 /* Put a `display' text property on the string for the image to
9674 display. Put a `menu-item' property on the string that gives
9675 the start of this item's properties in the tool-bar items
9676 vector. */
9677 image = Fcons (Qimage, plist);
9678 props = list4 (Qdisplay, image,
9679 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9681 /* Let the last image hide all remaining spaces in the tool bar
9682 string. The string can be longer than needed when we reuse a
9683 previous string. */
9684 if (i + 1 == f->n_tool_bar_items)
9685 end = SCHARS (f->desired_tool_bar_string);
9686 else
9687 end = i + 1;
9688 Fadd_text_properties (make_number (i), make_number (end),
9689 props, f->desired_tool_bar_string);
9690 #undef PROP
9693 UNGCPRO;
9697 /* Display one line of the tool-bar of frame IT->f.
9699 HEIGHT specifies the desired height of the tool-bar line.
9700 If the actual height of the glyph row is less than HEIGHT, the
9701 row's height is increased to HEIGHT, and the icons are centered
9702 vertically in the new height.
9704 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9705 count a final empty row in case the tool-bar width exactly matches
9706 the window width.
9709 static void
9710 display_tool_bar_line (it, height)
9711 struct it *it;
9712 int height;
9714 struct glyph_row *row = it->glyph_row;
9715 int max_x = it->last_visible_x;
9716 struct glyph *last;
9718 prepare_desired_row (row);
9719 row->y = it->current_y;
9721 /* Note that this isn't made use of if the face hasn't a box,
9722 so there's no need to check the face here. */
9723 it->start_of_box_run_p = 1;
9725 while (it->current_x < max_x)
9727 int x, n_glyphs_before, i, nglyphs;
9728 struct it it_before;
9730 /* Get the next display element. */
9731 if (!get_next_display_element (it))
9733 /* Don't count empty row if we are counting needed tool-bar lines. */
9734 if (height < 0 && !it->hpos)
9735 return;
9736 break;
9739 /* Produce glyphs. */
9740 n_glyphs_before = row->used[TEXT_AREA];
9741 it_before = *it;
9743 PRODUCE_GLYPHS (it);
9745 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9746 i = 0;
9747 x = it_before.current_x;
9748 while (i < nglyphs)
9750 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9752 if (x + glyph->pixel_width > max_x)
9754 /* Glyph doesn't fit on line. Backtrack. */
9755 row->used[TEXT_AREA] = n_glyphs_before;
9756 *it = it_before;
9757 /* If this is the only glyph on this line, it will never fit on the
9758 toolbar, so skip it. But ensure there is at least one glyph,
9759 so we don't accidentally disable the tool-bar. */
9760 if (n_glyphs_before == 0
9761 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9762 break;
9763 goto out;
9766 ++it->hpos;
9767 x += glyph->pixel_width;
9768 ++i;
9771 /* Stop at line ends. */
9772 if (ITERATOR_AT_END_OF_LINE_P (it))
9773 break;
9775 set_iterator_to_next (it, 1);
9778 out:;
9780 row->displays_text_p = row->used[TEXT_AREA] != 0;
9782 /* Use default face for the border below the tool bar.
9784 FIXME: When auto-resize-tool-bars is grow-only, there is
9785 no additional border below the possibly empty tool-bar lines.
9786 So to make the extra empty lines look "normal", we have to
9787 use the tool-bar face for the border too. */
9788 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9789 it->face_id = DEFAULT_FACE_ID;
9791 extend_face_to_end_of_line (it);
9792 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9793 last->right_box_line_p = 1;
9794 if (last == row->glyphs[TEXT_AREA])
9795 last->left_box_line_p = 1;
9797 /* Make line the desired height and center it vertically. */
9798 if ((height -= it->max_ascent + it->max_descent) > 0)
9800 /* Don't add more than one line height. */
9801 height %= FRAME_LINE_HEIGHT (it->f);
9802 it->max_ascent += height / 2;
9803 it->max_descent += (height + 1) / 2;
9806 compute_line_metrics (it);
9808 /* If line is empty, make it occupy the rest of the tool-bar. */
9809 if (!row->displays_text_p)
9811 row->height = row->phys_height = it->last_visible_y - row->y;
9812 row->visible_height = row->height;
9813 row->ascent = row->phys_ascent = 0;
9814 row->extra_line_spacing = 0;
9817 row->full_width_p = 1;
9818 row->continued_p = 0;
9819 row->truncated_on_left_p = 0;
9820 row->truncated_on_right_p = 0;
9822 it->current_x = it->hpos = 0;
9823 it->current_y += row->height;
9824 ++it->vpos;
9825 ++it->glyph_row;
9829 /* Max tool-bar height. */
9831 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9832 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9834 /* Value is the number of screen lines needed to make all tool-bar
9835 items of frame F visible. The number of actual rows needed is
9836 returned in *N_ROWS if non-NULL. */
9838 static int
9839 tool_bar_lines_needed (f, n_rows)
9840 struct frame *f;
9841 int *n_rows;
9843 struct window *w = XWINDOW (f->tool_bar_window);
9844 struct it it;
9845 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9846 the desired matrix, so use (unused) mode-line row as temporary row to
9847 avoid destroying the first tool-bar row. */
9848 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9850 /* Initialize an iterator for iteration over
9851 F->desired_tool_bar_string in the tool-bar window of frame F. */
9852 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9853 it.first_visible_x = 0;
9854 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9855 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9857 while (!ITERATOR_AT_END_P (&it))
9859 clear_glyph_row (temp_row);
9860 it.glyph_row = temp_row;
9861 display_tool_bar_line (&it, -1);
9863 clear_glyph_row (temp_row);
9865 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9866 if (n_rows)
9867 *n_rows = it.vpos > 0 ? it.vpos : -1;
9869 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9873 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9874 0, 1, 0,
9875 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9876 (frame)
9877 Lisp_Object frame;
9879 struct frame *f;
9880 struct window *w;
9881 int nlines = 0;
9883 if (NILP (frame))
9884 frame = selected_frame;
9885 else
9886 CHECK_FRAME (frame);
9887 f = XFRAME (frame);
9889 if (WINDOWP (f->tool_bar_window)
9890 || (w = XWINDOW (f->tool_bar_window),
9891 WINDOW_TOTAL_LINES (w) > 0))
9893 update_tool_bar (f, 1);
9894 if (f->n_tool_bar_items)
9896 build_desired_tool_bar_string (f);
9897 nlines = tool_bar_lines_needed (f, NULL);
9901 return make_number (nlines);
9905 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9906 height should be changed. */
9908 static int
9909 redisplay_tool_bar (f)
9910 struct frame *f;
9912 struct window *w;
9913 struct it it;
9914 struct glyph_row *row;
9916 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9917 if (FRAME_EXTERNAL_TOOL_BAR (f))
9918 update_frame_tool_bar (f);
9919 return 0;
9920 #endif
9922 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9923 do anything. This means you must start with tool-bar-lines
9924 non-zero to get the auto-sizing effect. Or in other words, you
9925 can turn off tool-bars by specifying tool-bar-lines zero. */
9926 if (!WINDOWP (f->tool_bar_window)
9927 || (w = XWINDOW (f->tool_bar_window),
9928 WINDOW_TOTAL_LINES (w) == 0))
9929 return 0;
9931 /* Set up an iterator for the tool-bar window. */
9932 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9933 it.first_visible_x = 0;
9934 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9935 row = it.glyph_row;
9937 /* Build a string that represents the contents of the tool-bar. */
9938 build_desired_tool_bar_string (f);
9939 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9941 if (f->n_tool_bar_rows == 0)
9943 int nlines;
9945 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9946 nlines != WINDOW_TOTAL_LINES (w)))
9948 extern Lisp_Object Qtool_bar_lines;
9949 Lisp_Object frame;
9950 int old_height = WINDOW_TOTAL_LINES (w);
9952 XSETFRAME (frame, f);
9953 Fmodify_frame_parameters (frame,
9954 Fcons (Fcons (Qtool_bar_lines,
9955 make_number (nlines)),
9956 Qnil));
9957 if (WINDOW_TOTAL_LINES (w) != old_height)
9959 clear_glyph_matrix (w->desired_matrix);
9960 fonts_changed_p = 1;
9961 return 1;
9966 /* Display as many lines as needed to display all tool-bar items. */
9968 if (f->n_tool_bar_rows > 0)
9970 int border, rows, height, extra;
9972 if (INTEGERP (Vtool_bar_border))
9973 border = XINT (Vtool_bar_border);
9974 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9975 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9976 else if (EQ (Vtool_bar_border, Qborder_width))
9977 border = f->border_width;
9978 else
9979 border = 0;
9980 if (border < 0)
9981 border = 0;
9983 rows = f->n_tool_bar_rows;
9984 height = max (1, (it.last_visible_y - border) / rows);
9985 extra = it.last_visible_y - border - height * rows;
9987 while (it.current_y < it.last_visible_y)
9989 int h = 0;
9990 if (extra > 0 && rows-- > 0)
9992 h = (extra + rows - 1) / rows;
9993 extra -= h;
9995 display_tool_bar_line (&it, height + h);
9998 else
10000 while (it.current_y < it.last_visible_y)
10001 display_tool_bar_line (&it, 0);
10004 /* It doesn't make much sense to try scrolling in the tool-bar
10005 window, so don't do it. */
10006 w->desired_matrix->no_scrolling_p = 1;
10007 w->must_be_updated_p = 1;
10009 if (!NILP (Vauto_resize_tool_bars))
10011 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10012 int change_height_p = 0;
10014 /* If we couldn't display everything, change the tool-bar's
10015 height if there is room for more. */
10016 if (IT_STRING_CHARPOS (it) < it.end_charpos
10017 && it.current_y < max_tool_bar_height)
10018 change_height_p = 1;
10020 row = it.glyph_row - 1;
10022 /* If there are blank lines at the end, except for a partially
10023 visible blank line at the end that is smaller than
10024 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10025 if (!row->displays_text_p
10026 && row->height >= FRAME_LINE_HEIGHT (f))
10027 change_height_p = 1;
10029 /* If row displays tool-bar items, but is partially visible,
10030 change the tool-bar's height. */
10031 if (row->displays_text_p
10032 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10033 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10034 change_height_p = 1;
10036 /* Resize windows as needed by changing the `tool-bar-lines'
10037 frame parameter. */
10038 if (change_height_p)
10040 extern Lisp_Object Qtool_bar_lines;
10041 Lisp_Object frame;
10042 int old_height = WINDOW_TOTAL_LINES (w);
10043 int nrows;
10044 int nlines = tool_bar_lines_needed (f, &nrows);
10046 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10047 && !f->minimize_tool_bar_window_p)
10048 ? (nlines > old_height)
10049 : (nlines != old_height));
10050 f->minimize_tool_bar_window_p = 0;
10052 if (change_height_p)
10054 XSETFRAME (frame, f);
10055 Fmodify_frame_parameters (frame,
10056 Fcons (Fcons (Qtool_bar_lines,
10057 make_number (nlines)),
10058 Qnil));
10059 if (WINDOW_TOTAL_LINES (w) != old_height)
10061 clear_glyph_matrix (w->desired_matrix);
10062 f->n_tool_bar_rows = nrows;
10063 fonts_changed_p = 1;
10064 return 1;
10070 f->minimize_tool_bar_window_p = 0;
10071 return 0;
10075 /* Get information about the tool-bar item which is displayed in GLYPH
10076 on frame F. Return in *PROP_IDX the index where tool-bar item
10077 properties start in F->tool_bar_items. Value is zero if
10078 GLYPH doesn't display a tool-bar item. */
10080 static int
10081 tool_bar_item_info (f, glyph, prop_idx)
10082 struct frame *f;
10083 struct glyph *glyph;
10084 int *prop_idx;
10086 Lisp_Object prop;
10087 int success_p;
10088 int charpos;
10090 /* This function can be called asynchronously, which means we must
10091 exclude any possibility that Fget_text_property signals an
10092 error. */
10093 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10094 charpos = max (0, charpos);
10096 /* Get the text property `menu-item' at pos. The value of that
10097 property is the start index of this item's properties in
10098 F->tool_bar_items. */
10099 prop = Fget_text_property (make_number (charpos),
10100 Qmenu_item, f->current_tool_bar_string);
10101 if (INTEGERP (prop))
10103 *prop_idx = XINT (prop);
10104 success_p = 1;
10106 else
10107 success_p = 0;
10109 return success_p;
10113 /* Get information about the tool-bar item at position X/Y on frame F.
10114 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10115 the current matrix of the tool-bar window of F, or NULL if not
10116 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10117 item in F->tool_bar_items. Value is
10119 -1 if X/Y is not on a tool-bar item
10120 0 if X/Y is on the same item that was highlighted before.
10121 1 otherwise. */
10123 static int
10124 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10125 struct frame *f;
10126 int x, y;
10127 struct glyph **glyph;
10128 int *hpos, *vpos, *prop_idx;
10130 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10131 struct window *w = XWINDOW (f->tool_bar_window);
10132 int area;
10134 /* Find the glyph under X/Y. */
10135 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10136 if (*glyph == NULL)
10137 return -1;
10139 /* Get the start of this tool-bar item's properties in
10140 f->tool_bar_items. */
10141 if (!tool_bar_item_info (f, *glyph, prop_idx))
10142 return -1;
10144 /* Is mouse on the highlighted item? */
10145 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10146 && *vpos >= dpyinfo->mouse_face_beg_row
10147 && *vpos <= dpyinfo->mouse_face_end_row
10148 && (*vpos > dpyinfo->mouse_face_beg_row
10149 || *hpos >= dpyinfo->mouse_face_beg_col)
10150 && (*vpos < dpyinfo->mouse_face_end_row
10151 || *hpos < dpyinfo->mouse_face_end_col
10152 || dpyinfo->mouse_face_past_end))
10153 return 0;
10155 return 1;
10159 /* EXPORT:
10160 Handle mouse button event on the tool-bar of frame F, at
10161 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10162 0 for button release. MODIFIERS is event modifiers for button
10163 release. */
10165 void
10166 handle_tool_bar_click (f, x, y, down_p, modifiers)
10167 struct frame *f;
10168 int x, y, down_p;
10169 unsigned int modifiers;
10171 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10172 struct window *w = XWINDOW (f->tool_bar_window);
10173 int hpos, vpos, prop_idx;
10174 struct glyph *glyph;
10175 Lisp_Object enabled_p;
10177 /* If not on the highlighted tool-bar item, return. */
10178 frame_to_window_pixel_xy (w, &x, &y);
10179 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10180 return;
10182 /* If item is disabled, do nothing. */
10183 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10184 if (NILP (enabled_p))
10185 return;
10187 if (down_p)
10189 /* Show item in pressed state. */
10190 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10191 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10192 last_tool_bar_item = prop_idx;
10194 else
10196 Lisp_Object key, frame;
10197 struct input_event event;
10198 EVENT_INIT (event);
10200 /* Show item in released state. */
10201 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10202 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10204 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10206 XSETFRAME (frame, f);
10207 event.kind = TOOL_BAR_EVENT;
10208 event.frame_or_window = frame;
10209 event.arg = frame;
10210 kbd_buffer_store_event (&event);
10212 event.kind = TOOL_BAR_EVENT;
10213 event.frame_or_window = frame;
10214 event.arg = key;
10215 event.modifiers = modifiers;
10216 kbd_buffer_store_event (&event);
10217 last_tool_bar_item = -1;
10222 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10223 tool-bar window-relative coordinates X/Y. Called from
10224 note_mouse_highlight. */
10226 static void
10227 note_tool_bar_highlight (f, x, y)
10228 struct frame *f;
10229 int x, y;
10231 Lisp_Object window = f->tool_bar_window;
10232 struct window *w = XWINDOW (window);
10233 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10234 int hpos, vpos;
10235 struct glyph *glyph;
10236 struct glyph_row *row;
10237 int i;
10238 Lisp_Object enabled_p;
10239 int prop_idx;
10240 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10241 int mouse_down_p, rc;
10243 /* Function note_mouse_highlight is called with negative x(y
10244 values when mouse moves outside of the frame. */
10245 if (x <= 0 || y <= 0)
10247 clear_mouse_face (dpyinfo);
10248 return;
10251 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10252 if (rc < 0)
10254 /* Not on tool-bar item. */
10255 clear_mouse_face (dpyinfo);
10256 return;
10258 else if (rc == 0)
10259 /* On same tool-bar item as before. */
10260 goto set_help_echo;
10262 clear_mouse_face (dpyinfo);
10264 /* Mouse is down, but on different tool-bar item? */
10265 mouse_down_p = (dpyinfo->grabbed
10266 && f == last_mouse_frame
10267 && FRAME_LIVE_P (f));
10268 if (mouse_down_p
10269 && last_tool_bar_item != prop_idx)
10270 return;
10272 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10273 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10275 /* If tool-bar item is not enabled, don't highlight it. */
10276 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10277 if (!NILP (enabled_p))
10279 /* Compute the x-position of the glyph. In front and past the
10280 image is a space. We include this in the highlighted area. */
10281 row = MATRIX_ROW (w->current_matrix, vpos);
10282 for (i = x = 0; i < hpos; ++i)
10283 x += row->glyphs[TEXT_AREA][i].pixel_width;
10285 /* Record this as the current active region. */
10286 dpyinfo->mouse_face_beg_col = hpos;
10287 dpyinfo->mouse_face_beg_row = vpos;
10288 dpyinfo->mouse_face_beg_x = x;
10289 dpyinfo->mouse_face_beg_y = row->y;
10290 dpyinfo->mouse_face_past_end = 0;
10292 dpyinfo->mouse_face_end_col = hpos + 1;
10293 dpyinfo->mouse_face_end_row = vpos;
10294 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10295 dpyinfo->mouse_face_end_y = row->y;
10296 dpyinfo->mouse_face_window = window;
10297 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10299 /* Display it as active. */
10300 show_mouse_face (dpyinfo, draw);
10301 dpyinfo->mouse_face_image_state = draw;
10304 set_help_echo:
10306 /* Set help_echo_string to a help string to display for this tool-bar item.
10307 XTread_socket does the rest. */
10308 help_echo_object = help_echo_window = Qnil;
10309 help_echo_pos = -1;
10310 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10311 if (NILP (help_echo_string))
10312 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10315 #endif /* HAVE_WINDOW_SYSTEM */
10319 /************************************************************************
10320 Horizontal scrolling
10321 ************************************************************************/
10323 static int hscroll_window_tree P_ ((Lisp_Object));
10324 static int hscroll_windows P_ ((Lisp_Object));
10326 /* For all leaf windows in the window tree rooted at WINDOW, set their
10327 hscroll value so that PT is (i) visible in the window, and (ii) so
10328 that it is not within a certain margin at the window's left and
10329 right border. Value is non-zero if any window's hscroll has been
10330 changed. */
10332 static int
10333 hscroll_window_tree (window)
10334 Lisp_Object window;
10336 int hscrolled_p = 0;
10337 int hscroll_relative_p = FLOATP (Vhscroll_step);
10338 int hscroll_step_abs = 0;
10339 double hscroll_step_rel = 0;
10341 if (hscroll_relative_p)
10343 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10344 if (hscroll_step_rel < 0)
10346 hscroll_relative_p = 0;
10347 hscroll_step_abs = 0;
10350 else if (INTEGERP (Vhscroll_step))
10352 hscroll_step_abs = XINT (Vhscroll_step);
10353 if (hscroll_step_abs < 0)
10354 hscroll_step_abs = 0;
10356 else
10357 hscroll_step_abs = 0;
10359 while (WINDOWP (window))
10361 struct window *w = XWINDOW (window);
10363 if (WINDOWP (w->hchild))
10364 hscrolled_p |= hscroll_window_tree (w->hchild);
10365 else if (WINDOWP (w->vchild))
10366 hscrolled_p |= hscroll_window_tree (w->vchild);
10367 else if (w->cursor.vpos >= 0)
10369 int h_margin;
10370 int text_area_width;
10371 struct glyph_row *current_cursor_row
10372 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10373 struct glyph_row *desired_cursor_row
10374 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10375 struct glyph_row *cursor_row
10376 = (desired_cursor_row->enabled_p
10377 ? desired_cursor_row
10378 : current_cursor_row);
10380 text_area_width = window_box_width (w, TEXT_AREA);
10382 /* Scroll when cursor is inside this scroll margin. */
10383 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10385 if ((XFASTINT (w->hscroll)
10386 && w->cursor.x <= h_margin)
10387 || (cursor_row->enabled_p
10388 && cursor_row->truncated_on_right_p
10389 && (w->cursor.x >= text_area_width - h_margin)))
10391 struct it it;
10392 int hscroll;
10393 struct buffer *saved_current_buffer;
10394 int pt;
10395 int wanted_x;
10397 /* Find point in a display of infinite width. */
10398 saved_current_buffer = current_buffer;
10399 current_buffer = XBUFFER (w->buffer);
10401 if (w == XWINDOW (selected_window))
10402 pt = BUF_PT (current_buffer);
10403 else
10405 pt = marker_position (w->pointm);
10406 pt = max (BEGV, pt);
10407 pt = min (ZV, pt);
10410 /* Move iterator to pt starting at cursor_row->start in
10411 a line with infinite width. */
10412 init_to_row_start (&it, w, cursor_row);
10413 it.last_visible_x = INFINITY;
10414 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10415 current_buffer = saved_current_buffer;
10417 /* Position cursor in window. */
10418 if (!hscroll_relative_p && hscroll_step_abs == 0)
10419 hscroll = max (0, (it.current_x
10420 - (ITERATOR_AT_END_OF_LINE_P (&it)
10421 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10422 : (text_area_width / 2))))
10423 / FRAME_COLUMN_WIDTH (it.f);
10424 else if (w->cursor.x >= text_area_width - h_margin)
10426 if (hscroll_relative_p)
10427 wanted_x = text_area_width * (1 - hscroll_step_rel)
10428 - h_margin;
10429 else
10430 wanted_x = text_area_width
10431 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10432 - h_margin;
10433 hscroll
10434 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10436 else
10438 if (hscroll_relative_p)
10439 wanted_x = text_area_width * hscroll_step_rel
10440 + h_margin;
10441 else
10442 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10443 + h_margin;
10444 hscroll
10445 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10447 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10449 /* Don't call Fset_window_hscroll if value hasn't
10450 changed because it will prevent redisplay
10451 optimizations. */
10452 if (XFASTINT (w->hscroll) != hscroll)
10454 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10455 w->hscroll = make_number (hscroll);
10456 hscrolled_p = 1;
10461 window = w->next;
10464 /* Value is non-zero if hscroll of any leaf window has been changed. */
10465 return hscrolled_p;
10469 /* Set hscroll so that cursor is visible and not inside horizontal
10470 scroll margins for all windows in the tree rooted at WINDOW. See
10471 also hscroll_window_tree above. Value is non-zero if any window's
10472 hscroll has been changed. If it has, desired matrices on the frame
10473 of WINDOW are cleared. */
10475 static int
10476 hscroll_windows (window)
10477 Lisp_Object window;
10479 int hscrolled_p;
10481 if (automatic_hscrolling_p)
10483 hscrolled_p = hscroll_window_tree (window);
10484 if (hscrolled_p)
10485 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10487 else
10488 hscrolled_p = 0;
10489 return hscrolled_p;
10494 /************************************************************************
10495 Redisplay
10496 ************************************************************************/
10498 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10499 to a non-zero value. This is sometimes handy to have in a debugger
10500 session. */
10502 #if GLYPH_DEBUG
10504 /* First and last unchanged row for try_window_id. */
10506 int debug_first_unchanged_at_end_vpos;
10507 int debug_last_unchanged_at_beg_vpos;
10509 /* Delta vpos and y. */
10511 int debug_dvpos, debug_dy;
10513 /* Delta in characters and bytes for try_window_id. */
10515 int debug_delta, debug_delta_bytes;
10517 /* Values of window_end_pos and window_end_vpos at the end of
10518 try_window_id. */
10520 EMACS_INT debug_end_pos, debug_end_vpos;
10522 /* Append a string to W->desired_matrix->method. FMT is a printf
10523 format string. A1...A9 are a supplement for a variable-length
10524 argument list. If trace_redisplay_p is non-zero also printf the
10525 resulting string to stderr. */
10527 static void
10528 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10529 struct window *w;
10530 char *fmt;
10531 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10533 char buffer[512];
10534 char *method = w->desired_matrix->method;
10535 int len = strlen (method);
10536 int size = sizeof w->desired_matrix->method;
10537 int remaining = size - len - 1;
10539 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10540 if (len && remaining)
10542 method[len] = '|';
10543 --remaining, ++len;
10546 strncpy (method + len, buffer, remaining);
10548 if (trace_redisplay_p)
10549 fprintf (stderr, "%p (%s): %s\n",
10551 ((BUFFERP (w->buffer)
10552 && STRINGP (XBUFFER (w->buffer)->name))
10553 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10554 : "no buffer"),
10555 buffer);
10558 #endif /* GLYPH_DEBUG */
10561 /* Value is non-zero if all changes in window W, which displays
10562 current_buffer, are in the text between START and END. START is a
10563 buffer position, END is given as a distance from Z. Used in
10564 redisplay_internal for display optimization. */
10566 static INLINE int
10567 text_outside_line_unchanged_p (w, start, end)
10568 struct window *w;
10569 int start, end;
10571 int unchanged_p = 1;
10573 /* If text or overlays have changed, see where. */
10574 if (XFASTINT (w->last_modified) < MODIFF
10575 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10577 /* Gap in the line? */
10578 if (GPT < start || Z - GPT < end)
10579 unchanged_p = 0;
10581 /* Changes start in front of the line, or end after it? */
10582 if (unchanged_p
10583 && (BEG_UNCHANGED < start - 1
10584 || END_UNCHANGED < end))
10585 unchanged_p = 0;
10587 /* If selective display, can't optimize if changes start at the
10588 beginning of the line. */
10589 if (unchanged_p
10590 && INTEGERP (current_buffer->selective_display)
10591 && XINT (current_buffer->selective_display) > 0
10592 && (BEG_UNCHANGED < start || GPT <= start))
10593 unchanged_p = 0;
10595 /* If there are overlays at the start or end of the line, these
10596 may have overlay strings with newlines in them. A change at
10597 START, for instance, may actually concern the display of such
10598 overlay strings as well, and they are displayed on different
10599 lines. So, quickly rule out this case. (For the future, it
10600 might be desirable to implement something more telling than
10601 just BEG/END_UNCHANGED.) */
10602 if (unchanged_p)
10604 if (BEG + BEG_UNCHANGED == start
10605 && overlay_touches_p (start))
10606 unchanged_p = 0;
10607 if (END_UNCHANGED == end
10608 && overlay_touches_p (Z - end))
10609 unchanged_p = 0;
10613 return unchanged_p;
10617 /* Do a frame update, taking possible shortcuts into account. This is
10618 the main external entry point for redisplay.
10620 If the last redisplay displayed an echo area message and that message
10621 is no longer requested, we clear the echo area or bring back the
10622 mini-buffer if that is in use. */
10624 void
10625 redisplay ()
10627 redisplay_internal (0);
10631 static Lisp_Object
10632 overlay_arrow_string_or_property (var)
10633 Lisp_Object var;
10635 Lisp_Object val;
10637 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10638 return val;
10640 return Voverlay_arrow_string;
10643 /* Return 1 if there are any overlay-arrows in current_buffer. */
10644 static int
10645 overlay_arrow_in_current_buffer_p ()
10647 Lisp_Object vlist;
10649 for (vlist = Voverlay_arrow_variable_list;
10650 CONSP (vlist);
10651 vlist = XCDR (vlist))
10653 Lisp_Object var = XCAR (vlist);
10654 Lisp_Object val;
10656 if (!SYMBOLP (var))
10657 continue;
10658 val = find_symbol_value (var);
10659 if (MARKERP (val)
10660 && current_buffer == XMARKER (val)->buffer)
10661 return 1;
10663 return 0;
10667 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10668 has changed. */
10670 static int
10671 overlay_arrows_changed_p ()
10673 Lisp_Object vlist;
10675 for (vlist = Voverlay_arrow_variable_list;
10676 CONSP (vlist);
10677 vlist = XCDR (vlist))
10679 Lisp_Object var = XCAR (vlist);
10680 Lisp_Object val, pstr;
10682 if (!SYMBOLP (var))
10683 continue;
10684 val = find_symbol_value (var);
10685 if (!MARKERP (val))
10686 continue;
10687 if (! EQ (COERCE_MARKER (val),
10688 Fget (var, Qlast_arrow_position))
10689 || ! (pstr = overlay_arrow_string_or_property (var),
10690 EQ (pstr, Fget (var, Qlast_arrow_string))))
10691 return 1;
10693 return 0;
10696 /* Mark overlay arrows to be updated on next redisplay. */
10698 static void
10699 update_overlay_arrows (up_to_date)
10700 int up_to_date;
10702 Lisp_Object vlist;
10704 for (vlist = Voverlay_arrow_variable_list;
10705 CONSP (vlist);
10706 vlist = XCDR (vlist))
10708 Lisp_Object var = XCAR (vlist);
10710 if (!SYMBOLP (var))
10711 continue;
10713 if (up_to_date > 0)
10715 Lisp_Object val = find_symbol_value (var);
10716 Fput (var, Qlast_arrow_position,
10717 COERCE_MARKER (val));
10718 Fput (var, Qlast_arrow_string,
10719 overlay_arrow_string_or_property (var));
10721 else if (up_to_date < 0
10722 || !NILP (Fget (var, Qlast_arrow_position)))
10724 Fput (var, Qlast_arrow_position, Qt);
10725 Fput (var, Qlast_arrow_string, Qt);
10731 /* Return overlay arrow string to display at row.
10732 Return integer (bitmap number) for arrow bitmap in left fringe.
10733 Return nil if no overlay arrow. */
10735 static Lisp_Object
10736 overlay_arrow_at_row (it, row)
10737 struct it *it;
10738 struct glyph_row *row;
10740 Lisp_Object vlist;
10742 for (vlist = Voverlay_arrow_variable_list;
10743 CONSP (vlist);
10744 vlist = XCDR (vlist))
10746 Lisp_Object var = XCAR (vlist);
10747 Lisp_Object val;
10749 if (!SYMBOLP (var))
10750 continue;
10752 val = find_symbol_value (var);
10754 if (MARKERP (val)
10755 && current_buffer == XMARKER (val)->buffer
10756 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10758 if (FRAME_WINDOW_P (it->f)
10759 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10761 #ifdef HAVE_WINDOW_SYSTEM
10762 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10764 int fringe_bitmap;
10765 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10766 return make_number (fringe_bitmap);
10768 #endif
10769 return make_number (-1); /* Use default arrow bitmap */
10771 return overlay_arrow_string_or_property (var);
10775 return Qnil;
10778 /* Return 1 if point moved out of or into a composition. Otherwise
10779 return 0. PREV_BUF and PREV_PT are the last point buffer and
10780 position. BUF and PT are the current point buffer and position. */
10783 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10784 struct buffer *prev_buf, *buf;
10785 int prev_pt, pt;
10787 int start, end;
10788 Lisp_Object prop;
10789 Lisp_Object buffer;
10791 XSETBUFFER (buffer, buf);
10792 /* Check a composition at the last point if point moved within the
10793 same buffer. */
10794 if (prev_buf == buf)
10796 if (prev_pt == pt)
10797 /* Point didn't move. */
10798 return 0;
10800 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10801 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10802 && COMPOSITION_VALID_P (start, end, prop)
10803 && start < prev_pt && end > prev_pt)
10804 /* The last point was within the composition. Return 1 iff
10805 point moved out of the composition. */
10806 return (pt <= start || pt >= end);
10809 /* Check a composition at the current point. */
10810 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10811 && find_composition (pt, -1, &start, &end, &prop, buffer)
10812 && COMPOSITION_VALID_P (start, end, prop)
10813 && start < pt && end > pt);
10817 /* Reconsider the setting of B->clip_changed which is displayed
10818 in window W. */
10820 static INLINE void
10821 reconsider_clip_changes (w, b)
10822 struct window *w;
10823 struct buffer *b;
10825 if (b->clip_changed
10826 && !NILP (w->window_end_valid)
10827 && w->current_matrix->buffer == b
10828 && w->current_matrix->zv == BUF_ZV (b)
10829 && w->current_matrix->begv == BUF_BEGV (b))
10830 b->clip_changed = 0;
10832 /* If display wasn't paused, and W is not a tool bar window, see if
10833 point has been moved into or out of a composition. In that case,
10834 we set b->clip_changed to 1 to force updating the screen. If
10835 b->clip_changed has already been set to 1, we can skip this
10836 check. */
10837 if (!b->clip_changed
10838 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10840 int pt;
10842 if (w == XWINDOW (selected_window))
10843 pt = BUF_PT (current_buffer);
10844 else
10845 pt = marker_position (w->pointm);
10847 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10848 || pt != XINT (w->last_point))
10849 && check_point_in_composition (w->current_matrix->buffer,
10850 XINT (w->last_point),
10851 XBUFFER (w->buffer), pt))
10852 b->clip_changed = 1;
10857 /* Select FRAME to forward the values of frame-local variables into C
10858 variables so that the redisplay routines can access those values
10859 directly. */
10861 static void
10862 select_frame_for_redisplay (frame)
10863 Lisp_Object frame;
10865 Lisp_Object tail, sym, val;
10866 Lisp_Object old = selected_frame;
10868 selected_frame = frame;
10870 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10871 if (CONSP (XCAR (tail))
10872 && (sym = XCAR (XCAR (tail)),
10873 SYMBOLP (sym))
10874 && (sym = indirect_variable (sym),
10875 val = SYMBOL_VALUE (sym),
10876 (BUFFER_LOCAL_VALUEP (val)
10877 || SOME_BUFFER_LOCAL_VALUEP (val)))
10878 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10879 /* Use find_symbol_value rather than Fsymbol_value
10880 to avoid an error if it is void. */
10881 find_symbol_value (sym);
10883 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10884 if (CONSP (XCAR (tail))
10885 && (sym = XCAR (XCAR (tail)),
10886 SYMBOLP (sym))
10887 && (sym = indirect_variable (sym),
10888 val = SYMBOL_VALUE (sym),
10889 (BUFFER_LOCAL_VALUEP (val)
10890 || SOME_BUFFER_LOCAL_VALUEP (val)))
10891 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10892 find_symbol_value (sym);
10896 #define STOP_POLLING \
10897 do { if (! polling_stopped_here) stop_polling (); \
10898 polling_stopped_here = 1; } while (0)
10900 #define RESUME_POLLING \
10901 do { if (polling_stopped_here) start_polling (); \
10902 polling_stopped_here = 0; } while (0)
10905 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10906 response to any user action; therefore, we should preserve the echo
10907 area. (Actually, our caller does that job.) Perhaps in the future
10908 avoid recentering windows if it is not necessary; currently that
10909 causes some problems. */
10911 static void
10912 redisplay_internal (preserve_echo_area)
10913 int preserve_echo_area;
10915 struct window *w = XWINDOW (selected_window);
10916 struct frame *f;
10917 int pause;
10918 int must_finish = 0;
10919 struct text_pos tlbufpos, tlendpos;
10920 int number_of_visible_frames;
10921 int count, count1;
10922 struct frame *sf;
10923 int polling_stopped_here = 0;
10925 /* Non-zero means redisplay has to consider all windows on all
10926 frames. Zero means, only selected_window is considered. */
10927 int consider_all_windows_p;
10929 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10931 /* No redisplay if running in batch mode or frame is not yet fully
10932 initialized, or redisplay is explicitly turned off by setting
10933 Vinhibit_redisplay. */
10934 if (noninteractive
10935 || !NILP (Vinhibit_redisplay))
10936 return;
10938 /* Don't examine these until after testing Vinhibit_redisplay.
10939 When Emacs is shutting down, perhaps because its connection to
10940 X has dropped, we should not look at them at all. */
10941 f = XFRAME (w->frame);
10942 sf = SELECTED_FRAME ();
10944 if (!f->glyphs_initialized_p)
10945 return;
10947 /* The flag redisplay_performed_directly_p is set by
10948 direct_output_for_insert when it already did the whole screen
10949 update necessary. */
10950 if (redisplay_performed_directly_p)
10952 redisplay_performed_directly_p = 0;
10953 if (!hscroll_windows (selected_window))
10954 return;
10957 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10958 if (popup_activated ())
10959 return;
10960 #endif
10962 /* I don't think this happens but let's be paranoid. */
10963 if (redisplaying_p)
10964 return;
10966 /* Record a function that resets redisplaying_p to its old value
10967 when we leave this function. */
10968 count = SPECPDL_INDEX ();
10969 record_unwind_protect (unwind_redisplay,
10970 Fcons (make_number (redisplaying_p), selected_frame));
10971 ++redisplaying_p;
10972 specbind (Qinhibit_free_realized_faces, Qnil);
10975 Lisp_Object tail, frame;
10977 FOR_EACH_FRAME (tail, frame)
10979 struct frame *f = XFRAME (frame);
10980 f->already_hscrolled_p = 0;
10984 retry:
10985 pause = 0;
10986 reconsider_clip_changes (w, current_buffer);
10987 last_escape_glyph_frame = NULL;
10988 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10990 /* If new fonts have been loaded that make a glyph matrix adjustment
10991 necessary, do it. */
10992 if (fonts_changed_p)
10994 adjust_glyphs (NULL);
10995 ++windows_or_buffers_changed;
10996 fonts_changed_p = 0;
10999 /* If face_change_count is non-zero, init_iterator will free all
11000 realized faces, which includes the faces referenced from current
11001 matrices. So, we can't reuse current matrices in this case. */
11002 if (face_change_count)
11003 ++windows_or_buffers_changed;
11005 if (! FRAME_WINDOW_P (sf)
11006 && previous_terminal_frame != sf)
11008 /* Since frames on an ASCII terminal share the same display
11009 area, displaying a different frame means redisplay the whole
11010 thing. */
11011 windows_or_buffers_changed++;
11012 SET_FRAME_GARBAGED (sf);
11013 XSETFRAME (Vterminal_frame, sf);
11015 previous_terminal_frame = sf;
11017 /* Set the visible flags for all frames. Do this before checking
11018 for resized or garbaged frames; they want to know if their frames
11019 are visible. See the comment in frame.h for
11020 FRAME_SAMPLE_VISIBILITY. */
11022 Lisp_Object tail, frame;
11024 number_of_visible_frames = 0;
11026 FOR_EACH_FRAME (tail, frame)
11028 struct frame *f = XFRAME (frame);
11030 FRAME_SAMPLE_VISIBILITY (f);
11031 if (FRAME_VISIBLE_P (f))
11032 ++number_of_visible_frames;
11033 clear_desired_matrices (f);
11037 /* Notice any pending interrupt request to change frame size. */
11038 do_pending_window_change (1);
11040 /* Clear frames marked as garbaged. */
11041 if (frame_garbaged)
11042 clear_garbaged_frames ();
11044 /* Build menubar and tool-bar items. */
11045 if (NILP (Vmemory_full))
11046 prepare_menu_bars ();
11048 if (windows_or_buffers_changed)
11049 update_mode_lines++;
11051 /* Detect case that we need to write or remove a star in the mode line. */
11052 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11054 w->update_mode_line = Qt;
11055 if (buffer_shared > 1)
11056 update_mode_lines++;
11059 /* Avoid invocation of point motion hooks by `current_column' below. */
11060 count1 = SPECPDL_INDEX ();
11061 specbind (Qinhibit_point_motion_hooks, Qt);
11063 /* If %c is in the mode line, update it if needed. */
11064 if (!NILP (w->column_number_displayed)
11065 /* This alternative quickly identifies a common case
11066 where no change is needed. */
11067 && !(PT == XFASTINT (w->last_point)
11068 && XFASTINT (w->last_modified) >= MODIFF
11069 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11070 && (XFASTINT (w->column_number_displayed)
11071 != (int) current_column ())) /* iftc */
11072 w->update_mode_line = Qt;
11074 unbind_to (count1, Qnil);
11076 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11078 /* The variable buffer_shared is set in redisplay_window and
11079 indicates that we redisplay a buffer in different windows. See
11080 there. */
11081 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11082 || cursor_type_changed);
11084 /* If specs for an arrow have changed, do thorough redisplay
11085 to ensure we remove any arrow that should no longer exist. */
11086 if (overlay_arrows_changed_p ())
11087 consider_all_windows_p = windows_or_buffers_changed = 1;
11089 /* Normally the message* functions will have already displayed and
11090 updated the echo area, but the frame may have been trashed, or
11091 the update may have been preempted, so display the echo area
11092 again here. Checking message_cleared_p captures the case that
11093 the echo area should be cleared. */
11094 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11095 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11096 || (message_cleared_p
11097 && minibuf_level == 0
11098 /* If the mini-window is currently selected, this means the
11099 echo-area doesn't show through. */
11100 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11102 int window_height_changed_p = echo_area_display (0);
11103 must_finish = 1;
11105 /* If we don't display the current message, don't clear the
11106 message_cleared_p flag, because, if we did, we wouldn't clear
11107 the echo area in the next redisplay which doesn't preserve
11108 the echo area. */
11109 if (!display_last_displayed_message_p)
11110 message_cleared_p = 0;
11112 if (fonts_changed_p)
11113 goto retry;
11114 else if (window_height_changed_p)
11116 consider_all_windows_p = 1;
11117 ++update_mode_lines;
11118 ++windows_or_buffers_changed;
11120 /* If window configuration was changed, frames may have been
11121 marked garbaged. Clear them or we will experience
11122 surprises wrt scrolling. */
11123 if (frame_garbaged)
11124 clear_garbaged_frames ();
11127 else if (EQ (selected_window, minibuf_window)
11128 && (current_buffer->clip_changed
11129 || XFASTINT (w->last_modified) < MODIFF
11130 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11131 && resize_mini_window (w, 0))
11133 /* Resized active mini-window to fit the size of what it is
11134 showing if its contents might have changed. */
11135 must_finish = 1;
11136 consider_all_windows_p = 1;
11137 ++windows_or_buffers_changed;
11138 ++update_mode_lines;
11140 /* If window configuration was changed, frames may have been
11141 marked garbaged. Clear them or we will experience
11142 surprises wrt scrolling. */
11143 if (frame_garbaged)
11144 clear_garbaged_frames ();
11148 /* If showing the region, and mark has changed, we must redisplay
11149 the whole window. The assignment to this_line_start_pos prevents
11150 the optimization directly below this if-statement. */
11151 if (((!NILP (Vtransient_mark_mode)
11152 && !NILP (XBUFFER (w->buffer)->mark_active))
11153 != !NILP (w->region_showing))
11154 || (!NILP (w->region_showing)
11155 && !EQ (w->region_showing,
11156 Fmarker_position (XBUFFER (w->buffer)->mark))))
11157 CHARPOS (this_line_start_pos) = 0;
11159 /* Optimize the case that only the line containing the cursor in the
11160 selected window has changed. Variables starting with this_ are
11161 set in display_line and record information about the line
11162 containing the cursor. */
11163 tlbufpos = this_line_start_pos;
11164 tlendpos = this_line_end_pos;
11165 if (!consider_all_windows_p
11166 && CHARPOS (tlbufpos) > 0
11167 && NILP (w->update_mode_line)
11168 && !current_buffer->clip_changed
11169 && !current_buffer->prevent_redisplay_optimizations_p
11170 && FRAME_VISIBLE_P (XFRAME (w->frame))
11171 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11172 /* Make sure recorded data applies to current buffer, etc. */
11173 && this_line_buffer == current_buffer
11174 && current_buffer == XBUFFER (w->buffer)
11175 && NILP (w->force_start)
11176 && NILP (w->optional_new_start)
11177 /* Point must be on the line that we have info recorded about. */
11178 && PT >= CHARPOS (tlbufpos)
11179 && PT <= Z - CHARPOS (tlendpos)
11180 /* All text outside that line, including its final newline,
11181 must be unchanged */
11182 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11183 CHARPOS (tlendpos)))
11185 if (CHARPOS (tlbufpos) > BEGV
11186 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11187 && (CHARPOS (tlbufpos) == ZV
11188 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11189 /* Former continuation line has disappeared by becoming empty */
11190 goto cancel;
11191 else if (XFASTINT (w->last_modified) < MODIFF
11192 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11193 || MINI_WINDOW_P (w))
11195 /* We have to handle the case of continuation around a
11196 wide-column character (See the comment in indent.c around
11197 line 885).
11199 For instance, in the following case:
11201 -------- Insert --------
11202 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11203 J_I_ ==> J_I_ `^^' are cursors.
11204 ^^ ^^
11205 -------- --------
11207 As we have to redraw the line above, we should goto cancel. */
11209 struct it it;
11210 int line_height_before = this_line_pixel_height;
11212 /* Note that start_display will handle the case that the
11213 line starting at tlbufpos is a continuation lines. */
11214 start_display (&it, w, tlbufpos);
11216 /* Implementation note: It this still necessary? */
11217 if (it.current_x != this_line_start_x)
11218 goto cancel;
11220 TRACE ((stderr, "trying display optimization 1\n"));
11221 w->cursor.vpos = -1;
11222 overlay_arrow_seen = 0;
11223 it.vpos = this_line_vpos;
11224 it.current_y = this_line_y;
11225 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11226 display_line (&it);
11228 /* If line contains point, is not continued,
11229 and ends at same distance from eob as before, we win */
11230 if (w->cursor.vpos >= 0
11231 /* Line is not continued, otherwise this_line_start_pos
11232 would have been set to 0 in display_line. */
11233 && CHARPOS (this_line_start_pos)
11234 /* Line ends as before. */
11235 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11236 /* Line has same height as before. Otherwise other lines
11237 would have to be shifted up or down. */
11238 && this_line_pixel_height == line_height_before)
11240 /* If this is not the window's last line, we must adjust
11241 the charstarts of the lines below. */
11242 if (it.current_y < it.last_visible_y)
11244 struct glyph_row *row
11245 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11246 int delta, delta_bytes;
11248 if (Z - CHARPOS (tlendpos) == ZV)
11250 /* This line ends at end of (accessible part of)
11251 buffer. There is no newline to count. */
11252 delta = (Z
11253 - CHARPOS (tlendpos)
11254 - MATRIX_ROW_START_CHARPOS (row));
11255 delta_bytes = (Z_BYTE
11256 - BYTEPOS (tlendpos)
11257 - MATRIX_ROW_START_BYTEPOS (row));
11259 else
11261 /* This line ends in a newline. Must take
11262 account of the newline and the rest of the
11263 text that follows. */
11264 delta = (Z
11265 - CHARPOS (tlendpos)
11266 - MATRIX_ROW_START_CHARPOS (row));
11267 delta_bytes = (Z_BYTE
11268 - BYTEPOS (tlendpos)
11269 - MATRIX_ROW_START_BYTEPOS (row));
11272 increment_matrix_positions (w->current_matrix,
11273 this_line_vpos + 1,
11274 w->current_matrix->nrows,
11275 delta, delta_bytes);
11278 /* If this row displays text now but previously didn't,
11279 or vice versa, w->window_end_vpos may have to be
11280 adjusted. */
11281 if ((it.glyph_row - 1)->displays_text_p)
11283 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11284 XSETINT (w->window_end_vpos, this_line_vpos);
11286 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11287 && this_line_vpos > 0)
11288 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11289 w->window_end_valid = Qnil;
11291 /* Update hint: No need to try to scroll in update_window. */
11292 w->desired_matrix->no_scrolling_p = 1;
11294 #if GLYPH_DEBUG
11295 *w->desired_matrix->method = 0;
11296 debug_method_add (w, "optimization 1");
11297 #endif
11298 #ifdef HAVE_WINDOW_SYSTEM
11299 update_window_fringes (w, 0);
11300 #endif
11301 goto update;
11303 else
11304 goto cancel;
11306 else if (/* Cursor position hasn't changed. */
11307 PT == XFASTINT (w->last_point)
11308 /* Make sure the cursor was last displayed
11309 in this window. Otherwise we have to reposition it. */
11310 && 0 <= w->cursor.vpos
11311 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11313 if (!must_finish)
11315 do_pending_window_change (1);
11317 /* We used to always goto end_of_redisplay here, but this
11318 isn't enough if we have a blinking cursor. */
11319 if (w->cursor_off_p == w->last_cursor_off_p)
11320 goto end_of_redisplay;
11322 goto update;
11324 /* If highlighting the region, or if the cursor is in the echo area,
11325 then we can't just move the cursor. */
11326 else if (! (!NILP (Vtransient_mark_mode)
11327 && !NILP (current_buffer->mark_active))
11328 && (EQ (selected_window, current_buffer->last_selected_window)
11329 || highlight_nonselected_windows)
11330 && NILP (w->region_showing)
11331 && NILP (Vshow_trailing_whitespace)
11332 && !cursor_in_echo_area)
11334 struct it it;
11335 struct glyph_row *row;
11337 /* Skip from tlbufpos to PT and see where it is. Note that
11338 PT may be in invisible text. If so, we will end at the
11339 next visible position. */
11340 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11341 NULL, DEFAULT_FACE_ID);
11342 it.current_x = this_line_start_x;
11343 it.current_y = this_line_y;
11344 it.vpos = this_line_vpos;
11346 /* The call to move_it_to stops in front of PT, but
11347 moves over before-strings. */
11348 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11350 if (it.vpos == this_line_vpos
11351 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11352 row->enabled_p))
11354 xassert (this_line_vpos == it.vpos);
11355 xassert (this_line_y == it.current_y);
11356 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11357 #if GLYPH_DEBUG
11358 *w->desired_matrix->method = 0;
11359 debug_method_add (w, "optimization 3");
11360 #endif
11361 goto update;
11363 else
11364 goto cancel;
11367 cancel:
11368 /* Text changed drastically or point moved off of line. */
11369 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11372 CHARPOS (this_line_start_pos) = 0;
11373 consider_all_windows_p |= buffer_shared > 1;
11374 ++clear_face_cache_count;
11375 #ifdef HAVE_WINDOW_SYSTEM
11376 ++clear_image_cache_count;
11377 #endif
11379 /* Build desired matrices, and update the display. If
11380 consider_all_windows_p is non-zero, do it for all windows on all
11381 frames. Otherwise do it for selected_window, only. */
11383 if (consider_all_windows_p)
11385 Lisp_Object tail, frame;
11387 FOR_EACH_FRAME (tail, frame)
11388 XFRAME (frame)->updated_p = 0;
11390 /* Recompute # windows showing selected buffer. This will be
11391 incremented each time such a window is displayed. */
11392 buffer_shared = 0;
11394 FOR_EACH_FRAME (tail, frame)
11396 struct frame *f = XFRAME (frame);
11398 if (FRAME_WINDOW_P (f) || f == sf)
11400 if (! EQ (frame, selected_frame))
11401 /* Select the frame, for the sake of frame-local
11402 variables. */
11403 select_frame_for_redisplay (frame);
11405 /* Mark all the scroll bars to be removed; we'll redeem
11406 the ones we want when we redisplay their windows. */
11407 if (condemn_scroll_bars_hook)
11408 condemn_scroll_bars_hook (f);
11410 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11411 redisplay_windows (FRAME_ROOT_WINDOW (f));
11413 /* Any scroll bars which redisplay_windows should have
11414 nuked should now go away. */
11415 if (judge_scroll_bars_hook)
11416 judge_scroll_bars_hook (f);
11418 /* If fonts changed, display again. */
11419 /* ??? rms: I suspect it is a mistake to jump all the way
11420 back to retry here. It should just retry this frame. */
11421 if (fonts_changed_p)
11422 goto retry;
11424 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11426 /* See if we have to hscroll. */
11427 if (!f->already_hscrolled_p)
11429 f->already_hscrolled_p = 1;
11430 if (hscroll_windows (f->root_window))
11431 goto retry;
11434 /* Prevent various kinds of signals during display
11435 update. stdio is not robust about handling
11436 signals, which can cause an apparent I/O
11437 error. */
11438 if (interrupt_input)
11439 unrequest_sigio ();
11440 STOP_POLLING;
11442 /* Update the display. */
11443 set_window_update_flags (XWINDOW (f->root_window), 1);
11444 pause |= update_frame (f, 0, 0);
11445 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11446 if (pause)
11447 break;
11448 #endif
11450 f->updated_p = 1;
11455 if (!pause)
11457 /* Do the mark_window_display_accurate after all windows have
11458 been redisplayed because this call resets flags in buffers
11459 which are needed for proper redisplay. */
11460 FOR_EACH_FRAME (tail, frame)
11462 struct frame *f = XFRAME (frame);
11463 if (f->updated_p)
11465 mark_window_display_accurate (f->root_window, 1);
11466 if (frame_up_to_date_hook)
11467 frame_up_to_date_hook (f);
11472 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11474 Lisp_Object mini_window;
11475 struct frame *mini_frame;
11477 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11478 /* Use list_of_error, not Qerror, so that
11479 we catch only errors and don't run the debugger. */
11480 internal_condition_case_1 (redisplay_window_1, selected_window,
11481 list_of_error,
11482 redisplay_window_error);
11484 /* Compare desired and current matrices, perform output. */
11486 update:
11487 /* If fonts changed, display again. */
11488 if (fonts_changed_p)
11489 goto retry;
11491 /* Prevent various kinds of signals during display update.
11492 stdio is not robust about handling signals,
11493 which can cause an apparent I/O error. */
11494 if (interrupt_input)
11495 unrequest_sigio ();
11496 STOP_POLLING;
11498 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11500 if (hscroll_windows (selected_window))
11501 goto retry;
11503 XWINDOW (selected_window)->must_be_updated_p = 1;
11504 pause = update_frame (sf, 0, 0);
11507 /* We may have called echo_area_display at the top of this
11508 function. If the echo area is on another frame, that may
11509 have put text on a frame other than the selected one, so the
11510 above call to update_frame would not have caught it. Catch
11511 it here. */
11512 mini_window = FRAME_MINIBUF_WINDOW (sf);
11513 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11515 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11517 XWINDOW (mini_window)->must_be_updated_p = 1;
11518 pause |= update_frame (mini_frame, 0, 0);
11519 if (!pause && hscroll_windows (mini_window))
11520 goto retry;
11524 /* If display was paused because of pending input, make sure we do a
11525 thorough update the next time. */
11526 if (pause)
11528 /* Prevent the optimization at the beginning of
11529 redisplay_internal that tries a single-line update of the
11530 line containing the cursor in the selected window. */
11531 CHARPOS (this_line_start_pos) = 0;
11533 /* Let the overlay arrow be updated the next time. */
11534 update_overlay_arrows (0);
11536 /* If we pause after scrolling, some rows in the current
11537 matrices of some windows are not valid. */
11538 if (!WINDOW_FULL_WIDTH_P (w)
11539 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11540 update_mode_lines = 1;
11542 else
11544 if (!consider_all_windows_p)
11546 /* This has already been done above if
11547 consider_all_windows_p is set. */
11548 mark_window_display_accurate_1 (w, 1);
11550 /* Say overlay arrows are up to date. */
11551 update_overlay_arrows (1);
11553 if (frame_up_to_date_hook != 0)
11554 frame_up_to_date_hook (sf);
11557 update_mode_lines = 0;
11558 windows_or_buffers_changed = 0;
11559 cursor_type_changed = 0;
11562 /* Start SIGIO interrupts coming again. Having them off during the
11563 code above makes it less likely one will discard output, but not
11564 impossible, since there might be stuff in the system buffer here.
11565 But it is much hairier to try to do anything about that. */
11566 if (interrupt_input)
11567 request_sigio ();
11568 RESUME_POLLING;
11570 /* If a frame has become visible which was not before, redisplay
11571 again, so that we display it. Expose events for such a frame
11572 (which it gets when becoming visible) don't call the parts of
11573 redisplay constructing glyphs, so simply exposing a frame won't
11574 display anything in this case. So, we have to display these
11575 frames here explicitly. */
11576 if (!pause)
11578 Lisp_Object tail, frame;
11579 int new_count = 0;
11581 FOR_EACH_FRAME (tail, frame)
11583 int this_is_visible = 0;
11585 if (XFRAME (frame)->visible)
11586 this_is_visible = 1;
11587 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11588 if (XFRAME (frame)->visible)
11589 this_is_visible = 1;
11591 if (this_is_visible)
11592 new_count++;
11595 if (new_count != number_of_visible_frames)
11596 windows_or_buffers_changed++;
11599 /* Change frame size now if a change is pending. */
11600 do_pending_window_change (1);
11602 /* If we just did a pending size change, or have additional
11603 visible frames, redisplay again. */
11604 if (windows_or_buffers_changed && !pause)
11605 goto retry;
11607 /* Clear the face cache eventually. */
11608 if (consider_all_windows_p)
11610 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11612 clear_face_cache (0);
11613 clear_face_cache_count = 0;
11615 #ifdef HAVE_WINDOW_SYSTEM
11616 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11618 Lisp_Object tail, frame;
11619 FOR_EACH_FRAME (tail, frame)
11621 struct frame *f = XFRAME (frame);
11622 if (FRAME_WINDOW_P (f))
11623 clear_image_cache (f, 0);
11625 clear_image_cache_count = 0;
11627 #endif /* HAVE_WINDOW_SYSTEM */
11630 end_of_redisplay:
11631 unbind_to (count, Qnil);
11632 RESUME_POLLING;
11636 /* Redisplay, but leave alone any recent echo area message unless
11637 another message has been requested in its place.
11639 This is useful in situations where you need to redisplay but no
11640 user action has occurred, making it inappropriate for the message
11641 area to be cleared. See tracking_off and
11642 wait_reading_process_output for examples of these situations.
11644 FROM_WHERE is an integer saying from where this function was
11645 called. This is useful for debugging. */
11647 void
11648 redisplay_preserve_echo_area (from_where)
11649 int from_where;
11651 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11653 if (!NILP (echo_area_buffer[1]))
11655 /* We have a previously displayed message, but no current
11656 message. Redisplay the previous message. */
11657 display_last_displayed_message_p = 1;
11658 redisplay_internal (1);
11659 display_last_displayed_message_p = 0;
11661 else
11662 redisplay_internal (1);
11664 if (rif != NULL && rif->flush_display_optional)
11665 rif->flush_display_optional (NULL);
11669 /* Function registered with record_unwind_protect in
11670 redisplay_internal. Reset redisplaying_p to the value it had
11671 before redisplay_internal was called, and clear
11672 prevent_freeing_realized_faces_p. It also selects the previously
11673 selected frame. */
11675 static Lisp_Object
11676 unwind_redisplay (val)
11677 Lisp_Object val;
11679 Lisp_Object old_redisplaying_p, old_frame;
11681 old_redisplaying_p = XCAR (val);
11682 redisplaying_p = XFASTINT (old_redisplaying_p);
11683 old_frame = XCDR (val);
11684 if (! EQ (old_frame, selected_frame))
11685 select_frame_for_redisplay (old_frame);
11686 return Qnil;
11690 /* Mark the display of window W as accurate or inaccurate. If
11691 ACCURATE_P is non-zero mark display of W as accurate. If
11692 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11693 redisplay_internal is called. */
11695 static void
11696 mark_window_display_accurate_1 (w, accurate_p)
11697 struct window *w;
11698 int accurate_p;
11700 if (BUFFERP (w->buffer))
11702 struct buffer *b = XBUFFER (w->buffer);
11704 w->last_modified
11705 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11706 w->last_overlay_modified
11707 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11708 w->last_had_star
11709 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11711 if (accurate_p)
11713 b->clip_changed = 0;
11714 b->prevent_redisplay_optimizations_p = 0;
11716 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11717 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11718 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11719 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11721 w->current_matrix->buffer = b;
11722 w->current_matrix->begv = BUF_BEGV (b);
11723 w->current_matrix->zv = BUF_ZV (b);
11725 w->last_cursor = w->cursor;
11726 w->last_cursor_off_p = w->cursor_off_p;
11728 if (w == XWINDOW (selected_window))
11729 w->last_point = make_number (BUF_PT (b));
11730 else
11731 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11735 if (accurate_p)
11737 w->window_end_valid = w->buffer;
11738 #if 0 /* This is incorrect with variable-height lines. */
11739 xassert (XINT (w->window_end_vpos)
11740 < (WINDOW_TOTAL_LINES (w)
11741 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11742 #endif
11743 w->update_mode_line = Qnil;
11748 /* Mark the display of windows in the window tree rooted at WINDOW as
11749 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11750 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11751 be redisplayed the next time redisplay_internal is called. */
11753 void
11754 mark_window_display_accurate (window, accurate_p)
11755 Lisp_Object window;
11756 int accurate_p;
11758 struct window *w;
11760 for (; !NILP (window); window = w->next)
11762 w = XWINDOW (window);
11763 mark_window_display_accurate_1 (w, accurate_p);
11765 if (!NILP (w->vchild))
11766 mark_window_display_accurate (w->vchild, accurate_p);
11767 if (!NILP (w->hchild))
11768 mark_window_display_accurate (w->hchild, accurate_p);
11771 if (accurate_p)
11773 update_overlay_arrows (1);
11775 else
11777 /* Force a thorough redisplay the next time by setting
11778 last_arrow_position and last_arrow_string to t, which is
11779 unequal to any useful value of Voverlay_arrow_... */
11780 update_overlay_arrows (-1);
11785 /* Return value in display table DP (Lisp_Char_Table *) for character
11786 C. Since a display table doesn't have any parent, we don't have to
11787 follow parent. Do not call this function directly but use the
11788 macro DISP_CHAR_VECTOR. */
11790 Lisp_Object
11791 disp_char_vector (dp, c)
11792 struct Lisp_Char_Table *dp;
11793 int c;
11795 int code[4], i;
11796 Lisp_Object val;
11798 if (SINGLE_BYTE_CHAR_P (c))
11799 return (dp->contents[c]);
11801 SPLIT_CHAR (c, code[0], code[1], code[2]);
11802 if (code[1] < 32)
11803 code[1] = -1;
11804 else if (code[2] < 32)
11805 code[2] = -1;
11807 /* Here, the possible range of code[0] (== charset ID) is
11808 128..max_charset. Since the top level char table contains data
11809 for multibyte characters after 256th element, we must increment
11810 code[0] by 128 to get a correct index. */
11811 code[0] += 128;
11812 code[3] = -1; /* anchor */
11814 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11816 val = dp->contents[code[i]];
11817 if (!SUB_CHAR_TABLE_P (val))
11818 return (NILP (val) ? dp->defalt : val);
11821 /* Here, val is a sub char table. We return the default value of
11822 it. */
11823 return (dp->defalt);
11828 /***********************************************************************
11829 Window Redisplay
11830 ***********************************************************************/
11832 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11834 static void
11835 redisplay_windows (window)
11836 Lisp_Object window;
11838 while (!NILP (window))
11840 struct window *w = XWINDOW (window);
11842 if (!NILP (w->hchild))
11843 redisplay_windows (w->hchild);
11844 else if (!NILP (w->vchild))
11845 redisplay_windows (w->vchild);
11846 else
11848 displayed_buffer = XBUFFER (w->buffer);
11849 /* Use list_of_error, not Qerror, so that
11850 we catch only errors and don't run the debugger. */
11851 internal_condition_case_1 (redisplay_window_0, window,
11852 list_of_error,
11853 redisplay_window_error);
11856 window = w->next;
11860 static Lisp_Object
11861 redisplay_window_error ()
11863 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11864 return Qnil;
11867 static Lisp_Object
11868 redisplay_window_0 (window)
11869 Lisp_Object window;
11871 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11872 redisplay_window (window, 0);
11873 return Qnil;
11876 static Lisp_Object
11877 redisplay_window_1 (window)
11878 Lisp_Object window;
11880 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11881 redisplay_window (window, 1);
11882 return Qnil;
11886 /* Increment GLYPH until it reaches END or CONDITION fails while
11887 adding (GLYPH)->pixel_width to X. */
11889 #define SKIP_GLYPHS(glyph, end, x, condition) \
11890 do \
11892 (x) += (glyph)->pixel_width; \
11893 ++(glyph); \
11895 while ((glyph) < (end) && (condition))
11898 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11899 DELTA is the number of bytes by which positions recorded in ROW
11900 differ from current buffer positions.
11902 Return 0 if cursor is not on this row. 1 otherwise. */
11905 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11906 struct window *w;
11907 struct glyph_row *row;
11908 struct glyph_matrix *matrix;
11909 int delta, delta_bytes, dy, dvpos;
11911 struct glyph *glyph = row->glyphs[TEXT_AREA];
11912 struct glyph *end = glyph + row->used[TEXT_AREA];
11913 struct glyph *cursor = NULL;
11914 /* The first glyph that starts a sequence of glyphs from string. */
11915 struct glyph *string_start;
11916 /* The X coordinate of string_start. */
11917 int string_start_x;
11918 /* The last known character position. */
11919 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11920 /* The last known character position before string_start. */
11921 int string_before_pos;
11922 int x = row->x;
11923 int cursor_x = x;
11924 int cursor_from_overlay_pos = 0;
11925 int pt_old = PT - delta;
11927 /* Skip over glyphs not having an object at the start of the row.
11928 These are special glyphs like truncation marks on terminal
11929 frames. */
11930 if (row->displays_text_p)
11931 while (glyph < end
11932 && INTEGERP (glyph->object)
11933 && glyph->charpos < 0)
11935 x += glyph->pixel_width;
11936 ++glyph;
11939 string_start = NULL;
11940 while (glyph < end
11941 && !INTEGERP (glyph->object)
11942 && (!BUFFERP (glyph->object)
11943 || (last_pos = glyph->charpos) < pt_old))
11945 if (! STRINGP (glyph->object))
11947 string_start = NULL;
11948 x += glyph->pixel_width;
11949 ++glyph;
11950 if (cursor_from_overlay_pos
11951 && last_pos >= cursor_from_overlay_pos)
11953 cursor_from_overlay_pos = 0;
11954 cursor = 0;
11957 else
11959 if (string_start == NULL)
11961 string_before_pos = last_pos;
11962 string_start = glyph;
11963 string_start_x = x;
11965 /* Skip all glyphs from string. */
11968 Lisp_Object cprop;
11969 int pos;
11970 if ((cursor == NULL || glyph > cursor)
11971 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11972 Qcursor, (glyph)->object),
11973 !NILP (cprop))
11974 && (pos = string_buffer_position (w, glyph->object,
11975 string_before_pos),
11976 (pos == 0 /* From overlay */
11977 || pos == pt_old)))
11979 /* Estimate overlay buffer position from the buffer
11980 positions of the glyphs before and after the overlay.
11981 Add 1 to last_pos so that if point corresponds to the
11982 glyph right after the overlay, we still use a 'cursor'
11983 property found in that overlay. */
11984 cursor_from_overlay_pos = (pos ? 0 : last_pos
11985 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11986 cursor = glyph;
11987 cursor_x = x;
11989 x += glyph->pixel_width;
11990 ++glyph;
11992 while (glyph < end && EQ (glyph->object, string_start->object));
11996 if (cursor != NULL)
11998 glyph = cursor;
11999 x = cursor_x;
12001 else if (row->ends_in_ellipsis_p && glyph == end)
12003 /* Scan back over the ellipsis glyphs, decrementing positions. */
12004 while (glyph > row->glyphs[TEXT_AREA]
12005 && (glyph - 1)->charpos == last_pos)
12006 glyph--, x -= glyph->pixel_width;
12007 /* That loop always goes one position too far,
12008 including the glyph before the ellipsis.
12009 So scan forward over that one. */
12010 x += glyph->pixel_width;
12011 glyph++;
12013 else if (string_start
12014 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12016 /* We may have skipped over point because the previous glyphs
12017 are from string. As there's no easy way to know the
12018 character position of the current glyph, find the correct
12019 glyph on point by scanning from string_start again. */
12020 Lisp_Object limit;
12021 Lisp_Object string;
12022 struct glyph *stop = glyph;
12023 int pos;
12025 limit = make_number (pt_old + 1);
12026 glyph = string_start;
12027 x = string_start_x;
12028 string = glyph->object;
12029 pos = string_buffer_position (w, string, string_before_pos);
12030 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12031 because we always put cursor after overlay strings. */
12032 while (pos == 0 && glyph < stop)
12034 string = glyph->object;
12035 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12036 if (glyph < stop)
12037 pos = string_buffer_position (w, glyph->object, string_before_pos);
12040 while (glyph < stop)
12042 pos = XINT (Fnext_single_char_property_change
12043 (make_number (pos), Qdisplay, Qnil, limit));
12044 if (pos > pt_old)
12045 break;
12046 /* Skip glyphs from the same string. */
12047 string = glyph->object;
12048 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12049 /* Skip glyphs from an overlay. */
12050 while (glyph < stop
12051 && ! string_buffer_position (w, glyph->object, pos))
12053 string = glyph->object;
12054 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12058 /* If we reached the end of the line, and end was from a string,
12059 cursor is not on this line. */
12060 if (glyph == end && row->continued_p)
12061 return 0;
12064 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12065 w->cursor.x = x;
12066 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12067 w->cursor.y = row->y + dy;
12069 if (w == XWINDOW (selected_window))
12071 if (!row->continued_p
12072 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12073 && row->x == 0)
12075 this_line_buffer = XBUFFER (w->buffer);
12077 CHARPOS (this_line_start_pos)
12078 = MATRIX_ROW_START_CHARPOS (row) + delta;
12079 BYTEPOS (this_line_start_pos)
12080 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12082 CHARPOS (this_line_end_pos)
12083 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12084 BYTEPOS (this_line_end_pos)
12085 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12087 this_line_y = w->cursor.y;
12088 this_line_pixel_height = row->height;
12089 this_line_vpos = w->cursor.vpos;
12090 this_line_start_x = row->x;
12092 else
12093 CHARPOS (this_line_start_pos) = 0;
12096 return 1;
12100 /* Run window scroll functions, if any, for WINDOW with new window
12101 start STARTP. Sets the window start of WINDOW to that position.
12103 We assume that the window's buffer is really current. */
12105 static INLINE struct text_pos
12106 run_window_scroll_functions (window, startp)
12107 Lisp_Object window;
12108 struct text_pos startp;
12110 struct window *w = XWINDOW (window);
12111 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12113 if (current_buffer != XBUFFER (w->buffer))
12114 abort ();
12116 if (!NILP (Vwindow_scroll_functions))
12118 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12119 make_number (CHARPOS (startp)));
12120 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12121 /* In case the hook functions switch buffers. */
12122 if (current_buffer != XBUFFER (w->buffer))
12123 set_buffer_internal_1 (XBUFFER (w->buffer));
12126 return startp;
12130 /* Make sure the line containing the cursor is fully visible.
12131 A value of 1 means there is nothing to be done.
12132 (Either the line is fully visible, or it cannot be made so,
12133 or we cannot tell.)
12135 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12136 is higher than window.
12138 A value of 0 means the caller should do scrolling
12139 as if point had gone off the screen. */
12141 static int
12142 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12143 struct window *w;
12144 int force_p;
12145 int current_matrix_p;
12147 struct glyph_matrix *matrix;
12148 struct glyph_row *row;
12149 int window_height;
12151 if (!make_cursor_line_fully_visible_p)
12152 return 1;
12154 /* It's not always possible to find the cursor, e.g, when a window
12155 is full of overlay strings. Don't do anything in that case. */
12156 if (w->cursor.vpos < 0)
12157 return 1;
12159 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12160 row = MATRIX_ROW (matrix, w->cursor.vpos);
12162 /* If the cursor row is not partially visible, there's nothing to do. */
12163 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12164 return 1;
12166 /* If the row the cursor is in is taller than the window's height,
12167 it's not clear what to do, so do nothing. */
12168 window_height = window_box_height (w);
12169 if (row->height >= window_height)
12171 if (!force_p || MINI_WINDOW_P (w)
12172 || w->vscroll || w->cursor.vpos == 0)
12173 return 1;
12175 return 0;
12177 #if 0
12178 /* This code used to try to scroll the window just enough to make
12179 the line visible. It returned 0 to say that the caller should
12180 allocate larger glyph matrices. */
12182 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12184 int dy = row->height - row->visible_height;
12185 w->vscroll = 0;
12186 w->cursor.y += dy;
12187 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12189 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12191 int dy = - (row->height - row->visible_height);
12192 w->vscroll = dy;
12193 w->cursor.y += dy;
12194 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12197 /* When we change the cursor y-position of the selected window,
12198 change this_line_y as well so that the display optimization for
12199 the cursor line of the selected window in redisplay_internal uses
12200 the correct y-position. */
12201 if (w == XWINDOW (selected_window))
12202 this_line_y = w->cursor.y;
12204 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12205 redisplay with larger matrices. */
12206 if (matrix->nrows < required_matrix_height (w))
12208 fonts_changed_p = 1;
12209 return 0;
12212 return 1;
12213 #endif /* 0 */
12217 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12218 non-zero means only WINDOW is redisplayed in redisplay_internal.
12219 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12220 in redisplay_window to bring a partially visible line into view in
12221 the case that only the cursor has moved.
12223 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12224 last screen line's vertical height extends past the end of the screen.
12226 Value is
12228 1 if scrolling succeeded
12230 0 if scrolling didn't find point.
12232 -1 if new fonts have been loaded so that we must interrupt
12233 redisplay, adjust glyph matrices, and try again. */
12235 enum
12237 SCROLLING_SUCCESS,
12238 SCROLLING_FAILED,
12239 SCROLLING_NEED_LARGER_MATRICES
12242 static int
12243 try_scrolling (window, just_this_one_p, scroll_conservatively,
12244 scroll_step, temp_scroll_step, last_line_misfit)
12245 Lisp_Object window;
12246 int just_this_one_p;
12247 EMACS_INT scroll_conservatively, scroll_step;
12248 int temp_scroll_step;
12249 int last_line_misfit;
12251 struct window *w = XWINDOW (window);
12252 struct frame *f = XFRAME (w->frame);
12253 struct text_pos scroll_margin_pos;
12254 struct text_pos pos;
12255 struct text_pos startp;
12256 struct it it;
12257 Lisp_Object window_end;
12258 int this_scroll_margin;
12259 int dy = 0;
12260 int scroll_max;
12261 int rc;
12262 int amount_to_scroll = 0;
12263 Lisp_Object aggressive;
12264 int height;
12265 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12267 #if GLYPH_DEBUG
12268 debug_method_add (w, "try_scrolling");
12269 #endif
12271 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12273 /* Compute scroll margin height in pixels. We scroll when point is
12274 within this distance from the top or bottom of the window. */
12275 if (scroll_margin > 0)
12277 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12278 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12280 else
12281 this_scroll_margin = 0;
12283 /* Force scroll_conservatively to have a reasonable value so it doesn't
12284 cause an overflow while computing how much to scroll. */
12285 if (scroll_conservatively)
12286 scroll_conservatively = min (scroll_conservatively,
12287 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12289 /* Compute how much we should try to scroll maximally to bring point
12290 into view. */
12291 if (scroll_step || scroll_conservatively || temp_scroll_step)
12292 scroll_max = max (scroll_step,
12293 max (scroll_conservatively, temp_scroll_step));
12294 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12295 || NUMBERP (current_buffer->scroll_up_aggressively))
12296 /* We're trying to scroll because of aggressive scrolling
12297 but no scroll_step is set. Choose an arbitrary one. Maybe
12298 there should be a variable for this. */
12299 scroll_max = 10;
12300 else
12301 scroll_max = 0;
12302 scroll_max *= FRAME_LINE_HEIGHT (f);
12304 /* Decide whether we have to scroll down. Start at the window end
12305 and move this_scroll_margin up to find the position of the scroll
12306 margin. */
12307 window_end = Fwindow_end (window, Qt);
12309 too_near_end:
12311 CHARPOS (scroll_margin_pos) = XINT (window_end);
12312 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12314 if (this_scroll_margin || extra_scroll_margin_lines)
12316 start_display (&it, w, scroll_margin_pos);
12317 if (this_scroll_margin)
12318 move_it_vertically_backward (&it, this_scroll_margin);
12319 if (extra_scroll_margin_lines)
12320 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12321 scroll_margin_pos = it.current.pos;
12324 if (PT >= CHARPOS (scroll_margin_pos))
12326 int y0;
12328 /* Point is in the scroll margin at the bottom of the window, or
12329 below. Compute a new window start that makes point visible. */
12331 /* Compute the distance from the scroll margin to PT.
12332 Give up if the distance is greater than scroll_max. */
12333 start_display (&it, w, scroll_margin_pos);
12334 y0 = it.current_y;
12335 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12336 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12338 /* To make point visible, we have to move the window start
12339 down so that the line the cursor is in is visible, which
12340 means we have to add in the height of the cursor line. */
12341 dy = line_bottom_y (&it) - y0;
12343 if (dy > scroll_max)
12344 return SCROLLING_FAILED;
12346 /* Move the window start down. If scrolling conservatively,
12347 move it just enough down to make point visible. If
12348 scroll_step is set, move it down by scroll_step. */
12349 start_display (&it, w, startp);
12351 if (scroll_conservatively)
12352 /* Set AMOUNT_TO_SCROLL to at least one line,
12353 and at most scroll_conservatively lines. */
12354 amount_to_scroll
12355 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12356 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12357 else if (scroll_step || temp_scroll_step)
12358 amount_to_scroll = scroll_max;
12359 else
12361 aggressive = current_buffer->scroll_up_aggressively;
12362 height = WINDOW_BOX_TEXT_HEIGHT (w);
12363 if (NUMBERP (aggressive))
12365 double float_amount = XFLOATINT (aggressive) * height;
12366 amount_to_scroll = float_amount;
12367 if (amount_to_scroll == 0 && float_amount > 0)
12368 amount_to_scroll = 1;
12372 if (amount_to_scroll <= 0)
12373 return SCROLLING_FAILED;
12375 /* If moving by amount_to_scroll leaves STARTP unchanged,
12376 move it down one screen line. */
12378 move_it_vertically (&it, amount_to_scroll);
12379 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12380 move_it_by_lines (&it, 1, 1);
12381 startp = it.current.pos;
12383 else
12385 /* See if point is inside the scroll margin at the top of the
12386 window. */
12387 scroll_margin_pos = startp;
12388 if (this_scroll_margin)
12390 start_display (&it, w, startp);
12391 move_it_vertically (&it, this_scroll_margin);
12392 scroll_margin_pos = it.current.pos;
12395 if (PT < CHARPOS (scroll_margin_pos))
12397 /* Point is in the scroll margin at the top of the window or
12398 above what is displayed in the window. */
12399 int y0;
12401 /* Compute the vertical distance from PT to the scroll
12402 margin position. Give up if distance is greater than
12403 scroll_max. */
12404 SET_TEXT_POS (pos, PT, PT_BYTE);
12405 start_display (&it, w, pos);
12406 y0 = it.current_y;
12407 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12408 it.last_visible_y, -1,
12409 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12410 dy = it.current_y - y0;
12411 if (dy > scroll_max)
12412 return SCROLLING_FAILED;
12414 /* Compute new window start. */
12415 start_display (&it, w, startp);
12417 if (scroll_conservatively)
12418 amount_to_scroll
12419 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12420 else if (scroll_step || temp_scroll_step)
12421 amount_to_scroll = scroll_max;
12422 else
12424 aggressive = current_buffer->scroll_down_aggressively;
12425 height = WINDOW_BOX_TEXT_HEIGHT (w);
12426 if (NUMBERP (aggressive))
12428 double float_amount = XFLOATINT (aggressive) * height;
12429 amount_to_scroll = float_amount;
12430 if (amount_to_scroll == 0 && float_amount > 0)
12431 amount_to_scroll = 1;
12435 if (amount_to_scroll <= 0)
12436 return SCROLLING_FAILED;
12438 move_it_vertically_backward (&it, amount_to_scroll);
12439 startp = it.current.pos;
12443 /* Run window scroll functions. */
12444 startp = run_window_scroll_functions (window, startp);
12446 /* Display the window. Give up if new fonts are loaded, or if point
12447 doesn't appear. */
12448 if (!try_window (window, startp, 0))
12449 rc = SCROLLING_NEED_LARGER_MATRICES;
12450 else if (w->cursor.vpos < 0)
12452 clear_glyph_matrix (w->desired_matrix);
12453 rc = SCROLLING_FAILED;
12455 else
12457 /* Maybe forget recorded base line for line number display. */
12458 if (!just_this_one_p
12459 || current_buffer->clip_changed
12460 || BEG_UNCHANGED < CHARPOS (startp))
12461 w->base_line_number = Qnil;
12463 /* If cursor ends up on a partially visible line,
12464 treat that as being off the bottom of the screen. */
12465 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12467 clear_glyph_matrix (w->desired_matrix);
12468 ++extra_scroll_margin_lines;
12469 goto too_near_end;
12471 rc = SCROLLING_SUCCESS;
12474 return rc;
12478 /* Compute a suitable window start for window W if display of W starts
12479 on a continuation line. Value is non-zero if a new window start
12480 was computed.
12482 The new window start will be computed, based on W's width, starting
12483 from the start of the continued line. It is the start of the
12484 screen line with the minimum distance from the old start W->start. */
12486 static int
12487 compute_window_start_on_continuation_line (w)
12488 struct window *w;
12490 struct text_pos pos, start_pos;
12491 int window_start_changed_p = 0;
12493 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12495 /* If window start is on a continuation line... Window start may be
12496 < BEGV in case there's invisible text at the start of the
12497 buffer (M-x rmail, for example). */
12498 if (CHARPOS (start_pos) > BEGV
12499 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12501 struct it it;
12502 struct glyph_row *row;
12504 /* Handle the case that the window start is out of range. */
12505 if (CHARPOS (start_pos) < BEGV)
12506 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12507 else if (CHARPOS (start_pos) > ZV)
12508 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12510 /* Find the start of the continued line. This should be fast
12511 because scan_buffer is fast (newline cache). */
12512 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12513 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12514 row, DEFAULT_FACE_ID);
12515 reseat_at_previous_visible_line_start (&it);
12517 /* If the line start is "too far" away from the window start,
12518 say it takes too much time to compute a new window start. */
12519 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12520 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12522 int min_distance, distance;
12524 /* Move forward by display lines to find the new window
12525 start. If window width was enlarged, the new start can
12526 be expected to be > the old start. If window width was
12527 decreased, the new window start will be < the old start.
12528 So, we're looking for the display line start with the
12529 minimum distance from the old window start. */
12530 pos = it.current.pos;
12531 min_distance = INFINITY;
12532 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12533 distance < min_distance)
12535 min_distance = distance;
12536 pos = it.current.pos;
12537 move_it_by_lines (&it, 1, 0);
12540 /* Set the window start there. */
12541 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12542 window_start_changed_p = 1;
12546 return window_start_changed_p;
12550 /* Try cursor movement in case text has not changed in window WINDOW,
12551 with window start STARTP. Value is
12553 CURSOR_MOVEMENT_SUCCESS if successful
12555 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12557 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12558 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12559 we want to scroll as if scroll-step were set to 1. See the code.
12561 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12562 which case we have to abort this redisplay, and adjust matrices
12563 first. */
12565 enum
12567 CURSOR_MOVEMENT_SUCCESS,
12568 CURSOR_MOVEMENT_CANNOT_BE_USED,
12569 CURSOR_MOVEMENT_MUST_SCROLL,
12570 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12573 static int
12574 try_cursor_movement (window, startp, scroll_step)
12575 Lisp_Object window;
12576 struct text_pos startp;
12577 int *scroll_step;
12579 struct window *w = XWINDOW (window);
12580 struct frame *f = XFRAME (w->frame);
12581 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12583 #if GLYPH_DEBUG
12584 if (inhibit_try_cursor_movement)
12585 return rc;
12586 #endif
12588 /* Handle case where text has not changed, only point, and it has
12589 not moved off the frame. */
12590 if (/* Point may be in this window. */
12591 PT >= CHARPOS (startp)
12592 /* Selective display hasn't changed. */
12593 && !current_buffer->clip_changed
12594 /* Function force-mode-line-update is used to force a thorough
12595 redisplay. It sets either windows_or_buffers_changed or
12596 update_mode_lines. So don't take a shortcut here for these
12597 cases. */
12598 && !update_mode_lines
12599 && !windows_or_buffers_changed
12600 && !cursor_type_changed
12601 /* Can't use this case if highlighting a region. When a
12602 region exists, cursor movement has to do more than just
12603 set the cursor. */
12604 && !(!NILP (Vtransient_mark_mode)
12605 && !NILP (current_buffer->mark_active))
12606 && NILP (w->region_showing)
12607 && NILP (Vshow_trailing_whitespace)
12608 /* Right after splitting windows, last_point may be nil. */
12609 && INTEGERP (w->last_point)
12610 /* This code is not used for mini-buffer for the sake of the case
12611 of redisplaying to replace an echo area message; since in
12612 that case the mini-buffer contents per se are usually
12613 unchanged. This code is of no real use in the mini-buffer
12614 since the handling of this_line_start_pos, etc., in redisplay
12615 handles the same cases. */
12616 && !EQ (window, minibuf_window)
12617 /* When splitting windows or for new windows, it happens that
12618 redisplay is called with a nil window_end_vpos or one being
12619 larger than the window. This should really be fixed in
12620 window.c. I don't have this on my list, now, so we do
12621 approximately the same as the old redisplay code. --gerd. */
12622 && INTEGERP (w->window_end_vpos)
12623 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12624 && (FRAME_WINDOW_P (f)
12625 || !overlay_arrow_in_current_buffer_p ()))
12627 int this_scroll_margin, top_scroll_margin;
12628 struct glyph_row *row = NULL;
12630 #if GLYPH_DEBUG
12631 debug_method_add (w, "cursor movement");
12632 #endif
12634 /* Scroll if point within this distance from the top or bottom
12635 of the window. This is a pixel value. */
12636 this_scroll_margin = max (0, scroll_margin);
12637 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12638 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12640 top_scroll_margin = this_scroll_margin;
12641 if (WINDOW_WANTS_HEADER_LINE_P (w))
12642 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12644 /* Start with the row the cursor was displayed during the last
12645 not paused redisplay. Give up if that row is not valid. */
12646 if (w->last_cursor.vpos < 0
12647 || w->last_cursor.vpos >= w->current_matrix->nrows)
12648 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12649 else
12651 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12652 if (row->mode_line_p)
12653 ++row;
12654 if (!row->enabled_p)
12655 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12658 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12660 int scroll_p = 0;
12661 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12663 if (PT > XFASTINT (w->last_point))
12665 /* Point has moved forward. */
12666 while (MATRIX_ROW_END_CHARPOS (row) < PT
12667 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12669 xassert (row->enabled_p);
12670 ++row;
12673 /* The end position of a row equals the start position
12674 of the next row. If PT is there, we would rather
12675 display it in the next line. */
12676 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12677 && MATRIX_ROW_END_CHARPOS (row) == PT
12678 && !cursor_row_p (w, row))
12679 ++row;
12681 /* If within the scroll margin, scroll. Note that
12682 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12683 the next line would be drawn, and that
12684 this_scroll_margin can be zero. */
12685 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12686 || PT > MATRIX_ROW_END_CHARPOS (row)
12687 /* Line is completely visible last line in window
12688 and PT is to be set in the next line. */
12689 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12690 && PT == MATRIX_ROW_END_CHARPOS (row)
12691 && !row->ends_at_zv_p
12692 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12693 scroll_p = 1;
12695 else if (PT < XFASTINT (w->last_point))
12697 /* Cursor has to be moved backward. Note that PT >=
12698 CHARPOS (startp) because of the outer if-statement. */
12699 while (!row->mode_line_p
12700 && (MATRIX_ROW_START_CHARPOS (row) > PT
12701 || (MATRIX_ROW_START_CHARPOS (row) == PT
12702 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12703 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12704 row > w->current_matrix->rows
12705 && (row-1)->ends_in_newline_from_string_p))))
12706 && (row->y > top_scroll_margin
12707 || CHARPOS (startp) == BEGV))
12709 xassert (row->enabled_p);
12710 --row;
12713 /* Consider the following case: Window starts at BEGV,
12714 there is invisible, intangible text at BEGV, so that
12715 display starts at some point START > BEGV. It can
12716 happen that we are called with PT somewhere between
12717 BEGV and START. Try to handle that case. */
12718 if (row < w->current_matrix->rows
12719 || row->mode_line_p)
12721 row = w->current_matrix->rows;
12722 if (row->mode_line_p)
12723 ++row;
12726 /* Due to newlines in overlay strings, we may have to
12727 skip forward over overlay strings. */
12728 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12729 && MATRIX_ROW_END_CHARPOS (row) == PT
12730 && !cursor_row_p (w, row))
12731 ++row;
12733 /* If within the scroll margin, scroll. */
12734 if (row->y < top_scroll_margin
12735 && CHARPOS (startp) != BEGV)
12736 scroll_p = 1;
12738 else
12740 /* Cursor did not move. So don't scroll even if cursor line
12741 is partially visible, as it was so before. */
12742 rc = CURSOR_MOVEMENT_SUCCESS;
12745 if (PT < MATRIX_ROW_START_CHARPOS (row)
12746 || PT > MATRIX_ROW_END_CHARPOS (row))
12748 /* if PT is not in the glyph row, give up. */
12749 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12751 else if (rc != CURSOR_MOVEMENT_SUCCESS
12752 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12753 && make_cursor_line_fully_visible_p)
12755 if (PT == MATRIX_ROW_END_CHARPOS (row)
12756 && !row->ends_at_zv_p
12757 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12758 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12759 else if (row->height > window_box_height (w))
12761 /* If we end up in a partially visible line, let's
12762 make it fully visible, except when it's taller
12763 than the window, in which case we can't do much
12764 about it. */
12765 *scroll_step = 1;
12766 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12768 else
12770 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12771 if (!cursor_row_fully_visible_p (w, 0, 1))
12772 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12773 else
12774 rc = CURSOR_MOVEMENT_SUCCESS;
12777 else if (scroll_p)
12778 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12779 else
12783 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12785 rc = CURSOR_MOVEMENT_SUCCESS;
12786 break;
12788 ++row;
12790 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12791 && MATRIX_ROW_START_CHARPOS (row) == PT
12792 && cursor_row_p (w, row));
12797 return rc;
12800 void
12801 set_vertical_scroll_bar (w)
12802 struct window *w;
12804 int start, end, whole;
12806 /* Calculate the start and end positions for the current window.
12807 At some point, it would be nice to choose between scrollbars
12808 which reflect the whole buffer size, with special markers
12809 indicating narrowing, and scrollbars which reflect only the
12810 visible region.
12812 Note that mini-buffers sometimes aren't displaying any text. */
12813 if (!MINI_WINDOW_P (w)
12814 || (w == XWINDOW (minibuf_window)
12815 && NILP (echo_area_buffer[0])))
12817 struct buffer *buf = XBUFFER (w->buffer);
12818 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12819 start = marker_position (w->start) - BUF_BEGV (buf);
12820 /* I don't think this is guaranteed to be right. For the
12821 moment, we'll pretend it is. */
12822 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12824 if (end < start)
12825 end = start;
12826 if (whole < (end - start))
12827 whole = end - start;
12829 else
12830 start = end = whole = 0;
12832 /* Indicate what this scroll bar ought to be displaying now. */
12833 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12837 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12838 selected_window is redisplayed.
12840 We can return without actually redisplaying the window if
12841 fonts_changed_p is nonzero. In that case, redisplay_internal will
12842 retry. */
12844 static void
12845 redisplay_window (window, just_this_one_p)
12846 Lisp_Object window;
12847 int just_this_one_p;
12849 struct window *w = XWINDOW (window);
12850 struct frame *f = XFRAME (w->frame);
12851 struct buffer *buffer = XBUFFER (w->buffer);
12852 struct buffer *old = current_buffer;
12853 struct text_pos lpoint, opoint, startp;
12854 int update_mode_line;
12855 int tem;
12856 struct it it;
12857 /* Record it now because it's overwritten. */
12858 int current_matrix_up_to_date_p = 0;
12859 int used_current_matrix_p = 0;
12860 /* This is less strict than current_matrix_up_to_date_p.
12861 It indictes that the buffer contents and narrowing are unchanged. */
12862 int buffer_unchanged_p = 0;
12863 int temp_scroll_step = 0;
12864 int count = SPECPDL_INDEX ();
12865 int rc;
12866 int centering_position = -1;
12867 int last_line_misfit = 0;
12868 int save_beg_unchanged, save_end_unchanged;
12870 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12871 opoint = lpoint;
12873 /* W must be a leaf window here. */
12874 xassert (!NILP (w->buffer));
12875 #if GLYPH_DEBUG
12876 *w->desired_matrix->method = 0;
12877 #endif
12879 specbind (Qinhibit_point_motion_hooks, Qt);
12881 reconsider_clip_changes (w, buffer);
12883 /* Has the mode line to be updated? */
12884 update_mode_line = (!NILP (w->update_mode_line)
12885 || update_mode_lines
12886 || buffer->clip_changed
12887 || buffer->prevent_redisplay_optimizations_p);
12889 if (MINI_WINDOW_P (w))
12891 if (w == XWINDOW (echo_area_window)
12892 && !NILP (echo_area_buffer[0]))
12894 if (update_mode_line)
12895 /* We may have to update a tty frame's menu bar or a
12896 tool-bar. Example `M-x C-h C-h C-g'. */
12897 goto finish_menu_bars;
12898 else
12899 /* We've already displayed the echo area glyphs in this window. */
12900 goto finish_scroll_bars;
12902 else if ((w != XWINDOW (minibuf_window)
12903 || minibuf_level == 0)
12904 /* When buffer is nonempty, redisplay window normally. */
12905 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12906 /* Quail displays non-mini buffers in minibuffer window.
12907 In that case, redisplay the window normally. */
12908 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12910 /* W is a mini-buffer window, but it's not active, so clear
12911 it. */
12912 int yb = window_text_bottom_y (w);
12913 struct glyph_row *row;
12914 int y;
12916 for (y = 0, row = w->desired_matrix->rows;
12917 y < yb;
12918 y += row->height, ++row)
12919 blank_row (w, row, y);
12920 goto finish_scroll_bars;
12923 clear_glyph_matrix (w->desired_matrix);
12926 /* Otherwise set up data on this window; select its buffer and point
12927 value. */
12928 /* Really select the buffer, for the sake of buffer-local
12929 variables. */
12930 set_buffer_internal_1 (XBUFFER (w->buffer));
12931 SET_TEXT_POS (opoint, PT, PT_BYTE);
12933 save_beg_unchanged = BEG_UNCHANGED;
12934 save_end_unchanged = END_UNCHANGED;
12936 current_matrix_up_to_date_p
12937 = (!NILP (w->window_end_valid)
12938 && !current_buffer->clip_changed
12939 && !current_buffer->prevent_redisplay_optimizations_p
12940 && XFASTINT (w->last_modified) >= MODIFF
12941 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12943 buffer_unchanged_p
12944 = (!NILP (w->window_end_valid)
12945 && !current_buffer->clip_changed
12946 && XFASTINT (w->last_modified) >= MODIFF
12947 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12949 /* When windows_or_buffers_changed is non-zero, we can't rely on
12950 the window end being valid, so set it to nil there. */
12951 if (windows_or_buffers_changed)
12953 /* If window starts on a continuation line, maybe adjust the
12954 window start in case the window's width changed. */
12955 if (XMARKER (w->start)->buffer == current_buffer)
12956 compute_window_start_on_continuation_line (w);
12958 w->window_end_valid = Qnil;
12961 /* Some sanity checks. */
12962 CHECK_WINDOW_END (w);
12963 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12964 abort ();
12965 if (BYTEPOS (opoint) < CHARPOS (opoint))
12966 abort ();
12968 /* If %c is in mode line, update it if needed. */
12969 if (!NILP (w->column_number_displayed)
12970 /* This alternative quickly identifies a common case
12971 where no change is needed. */
12972 && !(PT == XFASTINT (w->last_point)
12973 && XFASTINT (w->last_modified) >= MODIFF
12974 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12975 && (XFASTINT (w->column_number_displayed)
12976 != (int) current_column ())) /* iftc */
12977 update_mode_line = 1;
12979 /* Count number of windows showing the selected buffer. An indirect
12980 buffer counts as its base buffer. */
12981 if (!just_this_one_p)
12983 struct buffer *current_base, *window_base;
12984 current_base = current_buffer;
12985 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12986 if (current_base->base_buffer)
12987 current_base = current_base->base_buffer;
12988 if (window_base->base_buffer)
12989 window_base = window_base->base_buffer;
12990 if (current_base == window_base)
12991 buffer_shared++;
12994 /* Point refers normally to the selected window. For any other
12995 window, set up appropriate value. */
12996 if (!EQ (window, selected_window))
12998 int new_pt = XMARKER (w->pointm)->charpos;
12999 int new_pt_byte = marker_byte_position (w->pointm);
13000 if (new_pt < BEGV)
13002 new_pt = BEGV;
13003 new_pt_byte = BEGV_BYTE;
13004 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13006 else if (new_pt > (ZV - 1))
13008 new_pt = ZV;
13009 new_pt_byte = ZV_BYTE;
13010 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13013 /* We don't use SET_PT so that the point-motion hooks don't run. */
13014 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13017 /* If any of the character widths specified in the display table
13018 have changed, invalidate the width run cache. It's true that
13019 this may be a bit late to catch such changes, but the rest of
13020 redisplay goes (non-fatally) haywire when the display table is
13021 changed, so why should we worry about doing any better? */
13022 if (current_buffer->width_run_cache)
13024 struct Lisp_Char_Table *disptab = buffer_display_table ();
13026 if (! disptab_matches_widthtab (disptab,
13027 XVECTOR (current_buffer->width_table)))
13029 invalidate_region_cache (current_buffer,
13030 current_buffer->width_run_cache,
13031 BEG, Z);
13032 recompute_width_table (current_buffer, disptab);
13036 /* If window-start is screwed up, choose a new one. */
13037 if (XMARKER (w->start)->buffer != current_buffer)
13038 goto recenter;
13040 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13042 /* If someone specified a new starting point but did not insist,
13043 check whether it can be used. */
13044 if (!NILP (w->optional_new_start)
13045 && CHARPOS (startp) >= BEGV
13046 && CHARPOS (startp) <= ZV)
13048 w->optional_new_start = Qnil;
13049 start_display (&it, w, startp);
13050 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13051 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13052 if (IT_CHARPOS (it) == PT)
13053 w->force_start = Qt;
13054 /* IT may overshoot PT if text at PT is invisible. */
13055 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13056 w->force_start = Qt;
13059 force_start:
13061 /* Handle case where place to start displaying has been specified,
13062 unless the specified location is outside the accessible range. */
13063 if (!NILP (w->force_start)
13064 || w->frozen_window_start_p)
13066 /* We set this later on if we have to adjust point. */
13067 int new_vpos = -1;
13068 int val;
13070 w->force_start = Qnil;
13071 w->vscroll = 0;
13072 w->window_end_valid = Qnil;
13074 /* Forget any recorded base line for line number display. */
13075 if (!buffer_unchanged_p)
13076 w->base_line_number = Qnil;
13078 /* Redisplay the mode line. Select the buffer properly for that.
13079 Also, run the hook window-scroll-functions
13080 because we have scrolled. */
13081 /* Note, we do this after clearing force_start because
13082 if there's an error, it is better to forget about force_start
13083 than to get into an infinite loop calling the hook functions
13084 and having them get more errors. */
13085 if (!update_mode_line
13086 || ! NILP (Vwindow_scroll_functions))
13088 update_mode_line = 1;
13089 w->update_mode_line = Qt;
13090 startp = run_window_scroll_functions (window, startp);
13093 w->last_modified = make_number (0);
13094 w->last_overlay_modified = make_number (0);
13095 if (CHARPOS (startp) < BEGV)
13096 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13097 else if (CHARPOS (startp) > ZV)
13098 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13100 /* Redisplay, then check if cursor has been set during the
13101 redisplay. Give up if new fonts were loaded. */
13102 val = try_window (window, startp, 1);
13103 if (!val)
13105 w->force_start = Qt;
13106 clear_glyph_matrix (w->desired_matrix);
13107 goto need_larger_matrices;
13109 /* Point was outside the scroll margins. */
13110 if (val < 0)
13111 new_vpos = window_box_height (w) / 2;
13113 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13115 /* If point does not appear, try to move point so it does
13116 appear. The desired matrix has been built above, so we
13117 can use it here. */
13118 new_vpos = window_box_height (w) / 2;
13121 if (!cursor_row_fully_visible_p (w, 0, 0))
13123 /* Point does appear, but on a line partly visible at end of window.
13124 Move it back to a fully-visible line. */
13125 new_vpos = window_box_height (w);
13128 /* If we need to move point for either of the above reasons,
13129 now actually do it. */
13130 if (new_vpos >= 0)
13132 struct glyph_row *row;
13134 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13135 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13136 ++row;
13138 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13139 MATRIX_ROW_START_BYTEPOS (row));
13141 if (w != XWINDOW (selected_window))
13142 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13143 else if (current_buffer == old)
13144 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13146 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13148 /* If we are highlighting the region, then we just changed
13149 the region, so redisplay to show it. */
13150 if (!NILP (Vtransient_mark_mode)
13151 && !NILP (current_buffer->mark_active))
13153 clear_glyph_matrix (w->desired_matrix);
13154 if (!try_window (window, startp, 0))
13155 goto need_larger_matrices;
13159 #if GLYPH_DEBUG
13160 debug_method_add (w, "forced window start");
13161 #endif
13162 goto done;
13165 /* Handle case where text has not changed, only point, and it has
13166 not moved off the frame, and we are not retrying after hscroll.
13167 (current_matrix_up_to_date_p is nonzero when retrying.) */
13168 if (current_matrix_up_to_date_p
13169 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13170 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13172 switch (rc)
13174 case CURSOR_MOVEMENT_SUCCESS:
13175 used_current_matrix_p = 1;
13176 goto done;
13178 #if 0 /* try_cursor_movement never returns this value. */
13179 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13180 goto need_larger_matrices;
13181 #endif
13183 case CURSOR_MOVEMENT_MUST_SCROLL:
13184 goto try_to_scroll;
13186 default:
13187 abort ();
13190 /* If current starting point was originally the beginning of a line
13191 but no longer is, find a new starting point. */
13192 else if (!NILP (w->start_at_line_beg)
13193 && !(CHARPOS (startp) <= BEGV
13194 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13196 #if GLYPH_DEBUG
13197 debug_method_add (w, "recenter 1");
13198 #endif
13199 goto recenter;
13202 /* Try scrolling with try_window_id. Value is > 0 if update has
13203 been done, it is -1 if we know that the same window start will
13204 not work. It is 0 if unsuccessful for some other reason. */
13205 else if ((tem = try_window_id (w)) != 0)
13207 #if GLYPH_DEBUG
13208 debug_method_add (w, "try_window_id %d", tem);
13209 #endif
13211 if (fonts_changed_p)
13212 goto need_larger_matrices;
13213 if (tem > 0)
13214 goto done;
13216 /* Otherwise try_window_id has returned -1 which means that we
13217 don't want the alternative below this comment to execute. */
13219 else if (CHARPOS (startp) >= BEGV
13220 && CHARPOS (startp) <= ZV
13221 && PT >= CHARPOS (startp)
13222 && (CHARPOS (startp) < ZV
13223 /* Avoid starting at end of buffer. */
13224 || CHARPOS (startp) == BEGV
13225 || (XFASTINT (w->last_modified) >= MODIFF
13226 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13229 /* If first window line is a continuation line, and window start
13230 is inside the modified region, but the first change is before
13231 current window start, we must select a new window start.
13233 However, if this is the result of a down-mouse event (e.g. by
13234 extending the mouse-drag-overlay), we don't want to select a
13235 new window start, since that would change the position under
13236 the mouse, resulting in an unwanted mouse-movement rather
13237 than a simple mouse-click. */
13238 if (NILP (w->start_at_line_beg)
13239 && NILP (do_mouse_tracking)
13240 && CHARPOS (startp) > BEGV
13241 && CHARPOS (startp) > BEG + save_beg_unchanged
13242 && CHARPOS (startp) <= Z - save_end_unchanged
13243 /* Even if w->start_at_line_beg is nil, a new window may
13244 start at a line_beg, since that's how set_buffer_window
13245 sets it. So, we need to check the return value of
13246 compute_window_start_on_continuation_line. (See also
13247 bug#197). */
13248 && XMARKER (w->start)->buffer == current_buffer
13249 && compute_window_start_on_continuation_line (w))
13251 w->force_start = Qt;
13252 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13253 goto force_start;
13256 #if GLYPH_DEBUG
13257 debug_method_add (w, "same window start");
13258 #endif
13260 /* Try to redisplay starting at same place as before.
13261 If point has not moved off frame, accept the results. */
13262 if (!current_matrix_up_to_date_p
13263 /* Don't use try_window_reusing_current_matrix in this case
13264 because a window scroll function can have changed the
13265 buffer. */
13266 || !NILP (Vwindow_scroll_functions)
13267 || MINI_WINDOW_P (w)
13268 || !(used_current_matrix_p
13269 = try_window_reusing_current_matrix (w)))
13271 IF_DEBUG (debug_method_add (w, "1"));
13272 if (try_window (window, startp, 1) < 0)
13273 /* -1 means we need to scroll.
13274 0 means we need new matrices, but fonts_changed_p
13275 is set in that case, so we will detect it below. */
13276 goto try_to_scroll;
13279 if (fonts_changed_p)
13280 goto need_larger_matrices;
13282 if (w->cursor.vpos >= 0)
13284 if (!just_this_one_p
13285 || current_buffer->clip_changed
13286 || BEG_UNCHANGED < CHARPOS (startp))
13287 /* Forget any recorded base line for line number display. */
13288 w->base_line_number = Qnil;
13290 if (!cursor_row_fully_visible_p (w, 1, 0))
13292 clear_glyph_matrix (w->desired_matrix);
13293 last_line_misfit = 1;
13295 /* Drop through and scroll. */
13296 else
13297 goto done;
13299 else
13300 clear_glyph_matrix (w->desired_matrix);
13303 try_to_scroll:
13305 w->last_modified = make_number (0);
13306 w->last_overlay_modified = make_number (0);
13308 /* Redisplay the mode line. Select the buffer properly for that. */
13309 if (!update_mode_line)
13311 update_mode_line = 1;
13312 w->update_mode_line = Qt;
13315 /* Try to scroll by specified few lines. */
13316 if ((scroll_conservatively
13317 || scroll_step
13318 || temp_scroll_step
13319 || NUMBERP (current_buffer->scroll_up_aggressively)
13320 || NUMBERP (current_buffer->scroll_down_aggressively))
13321 && !current_buffer->clip_changed
13322 && CHARPOS (startp) >= BEGV
13323 && CHARPOS (startp) <= ZV)
13325 /* The function returns -1 if new fonts were loaded, 1 if
13326 successful, 0 if not successful. */
13327 int rc = try_scrolling (window, just_this_one_p,
13328 scroll_conservatively,
13329 scroll_step,
13330 temp_scroll_step, last_line_misfit);
13331 switch (rc)
13333 case SCROLLING_SUCCESS:
13334 goto done;
13336 case SCROLLING_NEED_LARGER_MATRICES:
13337 goto need_larger_matrices;
13339 case SCROLLING_FAILED:
13340 break;
13342 default:
13343 abort ();
13347 /* Finally, just choose place to start which centers point */
13349 recenter:
13350 if (centering_position < 0)
13351 centering_position = window_box_height (w) / 2;
13353 #if GLYPH_DEBUG
13354 debug_method_add (w, "recenter");
13355 #endif
13357 /* w->vscroll = 0; */
13359 /* Forget any previously recorded base line for line number display. */
13360 if (!buffer_unchanged_p)
13361 w->base_line_number = Qnil;
13363 /* Move backward half the height of the window. */
13364 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13365 it.current_y = it.last_visible_y;
13366 move_it_vertically_backward (&it, centering_position);
13367 xassert (IT_CHARPOS (it) >= BEGV);
13369 /* The function move_it_vertically_backward may move over more
13370 than the specified y-distance. If it->w is small, e.g. a
13371 mini-buffer window, we may end up in front of the window's
13372 display area. Start displaying at the start of the line
13373 containing PT in this case. */
13374 if (it.current_y <= 0)
13376 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13377 move_it_vertically_backward (&it, 0);
13378 #if 0
13379 /* I think this assert is bogus if buffer contains
13380 invisible text or images. KFS. */
13381 xassert (IT_CHARPOS (it) <= PT);
13382 #endif
13383 it.current_y = 0;
13386 it.current_x = it.hpos = 0;
13388 /* Set startp here explicitly in case that helps avoid an infinite loop
13389 in case the window-scroll-functions functions get errors. */
13390 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13392 /* Run scroll hooks. */
13393 startp = run_window_scroll_functions (window, it.current.pos);
13395 /* Redisplay the window. */
13396 if (!current_matrix_up_to_date_p
13397 || windows_or_buffers_changed
13398 || cursor_type_changed
13399 /* Don't use try_window_reusing_current_matrix in this case
13400 because it can have changed the buffer. */
13401 || !NILP (Vwindow_scroll_functions)
13402 || !just_this_one_p
13403 || MINI_WINDOW_P (w)
13404 || !(used_current_matrix_p
13405 = try_window_reusing_current_matrix (w)))
13406 try_window (window, startp, 0);
13408 /* If new fonts have been loaded (due to fontsets), give up. We
13409 have to start a new redisplay since we need to re-adjust glyph
13410 matrices. */
13411 if (fonts_changed_p)
13412 goto need_larger_matrices;
13414 /* If cursor did not appear assume that the middle of the window is
13415 in the first line of the window. Do it again with the next line.
13416 (Imagine a window of height 100, displaying two lines of height
13417 60. Moving back 50 from it->last_visible_y will end in the first
13418 line.) */
13419 if (w->cursor.vpos < 0)
13421 if (!NILP (w->window_end_valid)
13422 && PT >= Z - XFASTINT (w->window_end_pos))
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 if (PT < IT_CHARPOS (it))
13430 clear_glyph_matrix (w->desired_matrix);
13431 move_it_by_lines (&it, -1, 0);
13432 try_window (window, it.current.pos, 0);
13434 else
13436 /* Not much we can do about it. */
13440 /* Consider the following case: Window starts at BEGV, there is
13441 invisible, intangible text at BEGV, so that display starts at
13442 some point START > BEGV. It can happen that we are called with
13443 PT somewhere between BEGV and START. Try to handle that case. */
13444 if (w->cursor.vpos < 0)
13446 struct glyph_row *row = w->current_matrix->rows;
13447 if (row->mode_line_p)
13448 ++row;
13449 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13452 if (!cursor_row_fully_visible_p (w, 0, 0))
13454 /* If vscroll is enabled, disable it and try again. */
13455 if (w->vscroll)
13457 w->vscroll = 0;
13458 clear_glyph_matrix (w->desired_matrix);
13459 goto recenter;
13462 /* If centering point failed to make the whole line visible,
13463 put point at the top instead. That has to make the whole line
13464 visible, if it can be done. */
13465 if (centering_position == 0)
13466 goto done;
13468 clear_glyph_matrix (w->desired_matrix);
13469 centering_position = 0;
13470 goto recenter;
13473 done:
13475 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13476 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13477 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13478 ? Qt : Qnil);
13480 /* Display the mode line, if we must. */
13481 if ((update_mode_line
13482 /* If window not full width, must redo its mode line
13483 if (a) the window to its side is being redone and
13484 (b) we do a frame-based redisplay. This is a consequence
13485 of how inverted lines are drawn in frame-based redisplay. */
13486 || (!just_this_one_p
13487 && !FRAME_WINDOW_P (f)
13488 && !WINDOW_FULL_WIDTH_P (w))
13489 /* Line number to display. */
13490 || INTEGERP (w->base_line_pos)
13491 /* Column number is displayed and different from the one displayed. */
13492 || (!NILP (w->column_number_displayed)
13493 && (XFASTINT (w->column_number_displayed)
13494 != (int) current_column ()))) /* iftc */
13495 /* This means that the window has a mode line. */
13496 && (WINDOW_WANTS_MODELINE_P (w)
13497 || WINDOW_WANTS_HEADER_LINE_P (w)))
13499 display_mode_lines (w);
13501 /* If mode line height has changed, arrange for a thorough
13502 immediate redisplay using the correct mode line height. */
13503 if (WINDOW_WANTS_MODELINE_P (w)
13504 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13506 fonts_changed_p = 1;
13507 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13508 = DESIRED_MODE_LINE_HEIGHT (w);
13511 /* If top line height has changed, arrange for a thorough
13512 immediate redisplay using the correct mode line height. */
13513 if (WINDOW_WANTS_HEADER_LINE_P (w)
13514 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13516 fonts_changed_p = 1;
13517 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13518 = DESIRED_HEADER_LINE_HEIGHT (w);
13521 if (fonts_changed_p)
13522 goto need_larger_matrices;
13525 if (!line_number_displayed
13526 && !BUFFERP (w->base_line_pos))
13528 w->base_line_pos = Qnil;
13529 w->base_line_number = Qnil;
13532 finish_menu_bars:
13534 /* When we reach a frame's selected window, redo the frame's menu bar. */
13535 if (update_mode_line
13536 && EQ (FRAME_SELECTED_WINDOW (f), window))
13538 int redisplay_menu_p = 0;
13539 int redisplay_tool_bar_p = 0;
13541 if (FRAME_WINDOW_P (f))
13543 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13544 || defined (USE_GTK)
13545 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13546 #else
13547 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13548 #endif
13550 else
13551 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13553 if (redisplay_menu_p)
13554 display_menu_bar (w);
13556 #ifdef HAVE_WINDOW_SYSTEM
13557 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13558 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13559 #else
13560 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13561 && (FRAME_TOOL_BAR_LINES (f) > 0
13562 || !NILP (Vauto_resize_tool_bars));
13564 #endif
13566 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13568 extern int ignore_mouse_drag_p;
13569 ignore_mouse_drag_p = 1;
13571 #endif
13574 #ifdef HAVE_WINDOW_SYSTEM
13575 if (FRAME_WINDOW_P (f)
13576 && update_window_fringes (w, (just_this_one_p
13577 || (!used_current_matrix_p && !overlay_arrow_seen)
13578 || w->pseudo_window_p)))
13580 update_begin (f);
13581 BLOCK_INPUT;
13582 if (draw_window_fringes (w, 1))
13583 x_draw_vertical_border (w);
13584 UNBLOCK_INPUT;
13585 update_end (f);
13587 #endif /* HAVE_WINDOW_SYSTEM */
13589 /* We go to this label, with fonts_changed_p nonzero,
13590 if it is necessary to try again using larger glyph matrices.
13591 We have to redeem the scroll bar even in this case,
13592 because the loop in redisplay_internal expects that. */
13593 need_larger_matrices:
13595 finish_scroll_bars:
13597 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13599 /* Set the thumb's position and size. */
13600 set_vertical_scroll_bar (w);
13602 /* Note that we actually used the scroll bar attached to this
13603 window, so it shouldn't be deleted at the end of redisplay. */
13604 redeem_scroll_bar_hook (w);
13607 /* Restore current_buffer and value of point in it. */
13608 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13609 set_buffer_internal_1 (old);
13610 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13611 shorter. This can be caused by log truncation in *Messages*. */
13612 if (CHARPOS (lpoint) <= ZV)
13613 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13615 unbind_to (count, Qnil);
13619 /* Build the complete desired matrix of WINDOW with a window start
13620 buffer position POS.
13622 Value is 1 if successful. It is zero if fonts were loaded during
13623 redisplay which makes re-adjusting glyph matrices necessary, and -1
13624 if point would appear in the scroll margins.
13625 (We check that only if CHECK_MARGINS is nonzero. */
13628 try_window (window, pos, check_margins)
13629 Lisp_Object window;
13630 struct text_pos pos;
13631 int check_margins;
13633 struct window *w = XWINDOW (window);
13634 struct it it;
13635 struct glyph_row *last_text_row = NULL;
13636 struct frame *f = XFRAME (w->frame);
13638 /* Make POS the new window start. */
13639 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13641 /* Mark cursor position as unknown. No overlay arrow seen. */
13642 w->cursor.vpos = -1;
13643 overlay_arrow_seen = 0;
13645 /* Initialize iterator and info to start at POS. */
13646 start_display (&it, w, pos);
13648 /* Display all lines of W. */
13649 while (it.current_y < it.last_visible_y)
13651 if (display_line (&it))
13652 last_text_row = it.glyph_row - 1;
13653 if (fonts_changed_p)
13654 return 0;
13657 /* Don't let the cursor end in the scroll margins. */
13658 if (check_margins
13659 && !MINI_WINDOW_P (w))
13661 int this_scroll_margin;
13663 this_scroll_margin = max (0, scroll_margin);
13664 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13665 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13667 if ((w->cursor.y >= 0 /* not vscrolled */
13668 && w->cursor.y < this_scroll_margin
13669 && CHARPOS (pos) > BEGV
13670 && IT_CHARPOS (it) < ZV)
13671 /* rms: considering make_cursor_line_fully_visible_p here
13672 seems to give wrong results. We don't want to recenter
13673 when the last line is partly visible, we want to allow
13674 that case to be handled in the usual way. */
13675 || (w->cursor.y + 1) > it.last_visible_y)
13677 w->cursor.vpos = -1;
13678 clear_glyph_matrix (w->desired_matrix);
13679 return -1;
13683 /* If bottom moved off end of frame, change mode line percentage. */
13684 if (XFASTINT (w->window_end_pos) <= 0
13685 && Z != IT_CHARPOS (it))
13686 w->update_mode_line = Qt;
13688 /* Set window_end_pos to the offset of the last character displayed
13689 on the window from the end of current_buffer. Set
13690 window_end_vpos to its row number. */
13691 if (last_text_row)
13693 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13694 w->window_end_bytepos
13695 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13696 w->window_end_pos
13697 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13698 w->window_end_vpos
13699 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13700 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13701 ->displays_text_p);
13703 else
13705 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13706 w->window_end_pos = make_number (Z - ZV);
13707 w->window_end_vpos = make_number (0);
13710 /* But that is not valid info until redisplay finishes. */
13711 w->window_end_valid = Qnil;
13712 return 1;
13717 /************************************************************************
13718 Window redisplay reusing current matrix when buffer has not changed
13719 ************************************************************************/
13721 /* Try redisplay of window W showing an unchanged buffer with a
13722 different window start than the last time it was displayed by
13723 reusing its current matrix. Value is non-zero if successful.
13724 W->start is the new window start. */
13726 static int
13727 try_window_reusing_current_matrix (w)
13728 struct window *w;
13730 struct frame *f = XFRAME (w->frame);
13731 struct glyph_row *row, *bottom_row;
13732 struct it it;
13733 struct run run;
13734 struct text_pos start, new_start;
13735 int nrows_scrolled, i;
13736 struct glyph_row *last_text_row;
13737 struct glyph_row *last_reused_text_row;
13738 struct glyph_row *start_row;
13739 int start_vpos, min_y, max_y;
13741 #if GLYPH_DEBUG
13742 if (inhibit_try_window_reusing)
13743 return 0;
13744 #endif
13746 if (/* This function doesn't handle terminal frames. */
13747 !FRAME_WINDOW_P (f)
13748 /* Don't try to reuse the display if windows have been split
13749 or such. */
13750 || windows_or_buffers_changed
13751 || cursor_type_changed)
13752 return 0;
13754 /* Can't do this if region may have changed. */
13755 if ((!NILP (Vtransient_mark_mode)
13756 && !NILP (current_buffer->mark_active))
13757 || !NILP (w->region_showing)
13758 || !NILP (Vshow_trailing_whitespace))
13759 return 0;
13761 /* If top-line visibility has changed, give up. */
13762 if (WINDOW_WANTS_HEADER_LINE_P (w)
13763 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13764 return 0;
13766 /* Give up if old or new display is scrolled vertically. We could
13767 make this function handle this, but right now it doesn't. */
13768 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13769 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13770 return 0;
13772 /* The variable new_start now holds the new window start. The old
13773 start `start' can be determined from the current matrix. */
13774 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13775 start = start_row->start.pos;
13776 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13778 /* Clear the desired matrix for the display below. */
13779 clear_glyph_matrix (w->desired_matrix);
13781 if (CHARPOS (new_start) <= CHARPOS (start))
13783 int first_row_y;
13785 /* Don't use this method if the display starts with an ellipsis
13786 displayed for invisible text. It's not easy to handle that case
13787 below, and it's certainly not worth the effort since this is
13788 not a frequent case. */
13789 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13790 return 0;
13792 IF_DEBUG (debug_method_add (w, "twu1"));
13794 /* Display up to a row that can be reused. The variable
13795 last_text_row is set to the last row displayed that displays
13796 text. Note that it.vpos == 0 if or if not there is a
13797 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13798 start_display (&it, w, new_start);
13799 first_row_y = it.current_y;
13800 w->cursor.vpos = -1;
13801 last_text_row = last_reused_text_row = NULL;
13803 while (it.current_y < it.last_visible_y
13804 && !fonts_changed_p)
13806 /* If we have reached into the characters in the START row,
13807 that means the line boundaries have changed. So we
13808 can't start copying with the row START. Maybe it will
13809 work to start copying with the following row. */
13810 while (IT_CHARPOS (it) > CHARPOS (start))
13812 /* Advance to the next row as the "start". */
13813 start_row++;
13814 start = start_row->start.pos;
13815 /* If there are no more rows to try, or just one, give up. */
13816 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13817 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13818 || CHARPOS (start) == ZV)
13820 clear_glyph_matrix (w->desired_matrix);
13821 return 0;
13824 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13826 /* If we have reached alignment,
13827 we can copy the rest of the rows. */
13828 if (IT_CHARPOS (it) == CHARPOS (start))
13829 break;
13831 if (display_line (&it))
13832 last_text_row = it.glyph_row - 1;
13835 /* A value of current_y < last_visible_y means that we stopped
13836 at the previous window start, which in turn means that we
13837 have at least one reusable row. */
13838 if (it.current_y < it.last_visible_y)
13840 /* IT.vpos always starts from 0; it counts text lines. */
13841 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13843 /* Find PT if not already found in the lines displayed. */
13844 if (w->cursor.vpos < 0)
13846 int dy = it.current_y - start_row->y;
13848 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13849 row = row_containing_pos (w, PT, row, NULL, dy);
13850 if (row)
13851 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13852 dy, nrows_scrolled);
13853 else
13855 clear_glyph_matrix (w->desired_matrix);
13856 return 0;
13860 /* Scroll the display. Do it before the current matrix is
13861 changed. The problem here is that update has not yet
13862 run, i.e. part of the current matrix is not up to date.
13863 scroll_run_hook will clear the cursor, and use the
13864 current matrix to get the height of the row the cursor is
13865 in. */
13866 run.current_y = start_row->y;
13867 run.desired_y = it.current_y;
13868 run.height = it.last_visible_y - it.current_y;
13870 if (run.height > 0 && run.current_y != run.desired_y)
13872 update_begin (f);
13873 rif->update_window_begin_hook (w);
13874 rif->clear_window_mouse_face (w);
13875 rif->scroll_run_hook (w, &run);
13876 rif->update_window_end_hook (w, 0, 0);
13877 update_end (f);
13880 /* Shift current matrix down by nrows_scrolled lines. */
13881 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13882 rotate_matrix (w->current_matrix,
13883 start_vpos,
13884 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13885 nrows_scrolled);
13887 /* Disable lines that must be updated. */
13888 for (i = 0; i < nrows_scrolled; ++i)
13889 (start_row + i)->enabled_p = 0;
13891 /* Re-compute Y positions. */
13892 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13893 max_y = it.last_visible_y;
13894 for (row = start_row + nrows_scrolled;
13895 row < bottom_row;
13896 ++row)
13898 row->y = it.current_y;
13899 row->visible_height = row->height;
13901 if (row->y < min_y)
13902 row->visible_height -= min_y - row->y;
13903 if (row->y + row->height > max_y)
13904 row->visible_height -= row->y + row->height - max_y;
13905 row->redraw_fringe_bitmaps_p = 1;
13907 it.current_y += row->height;
13909 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13910 last_reused_text_row = row;
13911 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13912 break;
13915 /* Disable lines in the current matrix which are now
13916 below the window. */
13917 for (++row; row < bottom_row; ++row)
13918 row->enabled_p = row->mode_line_p = 0;
13921 /* Update window_end_pos etc.; last_reused_text_row is the last
13922 reused row from the current matrix containing text, if any.
13923 The value of last_text_row is the last displayed line
13924 containing text. */
13925 if (last_reused_text_row)
13927 w->window_end_bytepos
13928 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13929 w->window_end_pos
13930 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13931 w->window_end_vpos
13932 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13933 w->current_matrix));
13935 else if (last_text_row)
13937 w->window_end_bytepos
13938 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13939 w->window_end_pos
13940 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13941 w->window_end_vpos
13942 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13944 else
13946 /* This window must be completely empty. */
13947 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13948 w->window_end_pos = make_number (Z - ZV);
13949 w->window_end_vpos = make_number (0);
13951 w->window_end_valid = Qnil;
13953 /* Update hint: don't try scrolling again in update_window. */
13954 w->desired_matrix->no_scrolling_p = 1;
13956 #if GLYPH_DEBUG
13957 debug_method_add (w, "try_window_reusing_current_matrix 1");
13958 #endif
13959 return 1;
13961 else if (CHARPOS (new_start) > CHARPOS (start))
13963 struct glyph_row *pt_row, *row;
13964 struct glyph_row *first_reusable_row;
13965 struct glyph_row *first_row_to_display;
13966 int dy;
13967 int yb = window_text_bottom_y (w);
13969 /* Find the row starting at new_start, if there is one. Don't
13970 reuse a partially visible line at the end. */
13971 first_reusable_row = start_row;
13972 while (first_reusable_row->enabled_p
13973 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13974 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13975 < CHARPOS (new_start)))
13976 ++first_reusable_row;
13978 /* Give up if there is no row to reuse. */
13979 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13980 || !first_reusable_row->enabled_p
13981 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13982 != CHARPOS (new_start)))
13983 return 0;
13985 /* We can reuse fully visible rows beginning with
13986 first_reusable_row to the end of the window. Set
13987 first_row_to_display to the first row that cannot be reused.
13988 Set pt_row to the row containing point, if there is any. */
13989 pt_row = NULL;
13990 for (first_row_to_display = first_reusable_row;
13991 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13992 ++first_row_to_display)
13994 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13995 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13996 pt_row = first_row_to_display;
13999 /* Start displaying at the start of first_row_to_display. */
14000 xassert (first_row_to_display->y < yb);
14001 init_to_row_start (&it, w, first_row_to_display);
14003 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14004 - start_vpos);
14005 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14006 - nrows_scrolled);
14007 it.current_y = (first_row_to_display->y - first_reusable_row->y
14008 + WINDOW_HEADER_LINE_HEIGHT (w));
14010 /* Display lines beginning with first_row_to_display in the
14011 desired matrix. Set last_text_row to the last row displayed
14012 that displays text. */
14013 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14014 if (pt_row == NULL)
14015 w->cursor.vpos = -1;
14016 last_text_row = NULL;
14017 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14018 if (display_line (&it))
14019 last_text_row = it.glyph_row - 1;
14021 /* Give up If point isn't in a row displayed or reused. */
14022 if (w->cursor.vpos < 0)
14024 clear_glyph_matrix (w->desired_matrix);
14025 return 0;
14028 /* If point is in a reused row, adjust y and vpos of the cursor
14029 position. */
14030 if (pt_row)
14032 w->cursor.vpos -= nrows_scrolled;
14033 w->cursor.y -= first_reusable_row->y - start_row->y;
14036 /* Scroll the display. */
14037 run.current_y = first_reusable_row->y;
14038 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14039 run.height = it.last_visible_y - run.current_y;
14040 dy = run.current_y - run.desired_y;
14042 if (run.height)
14044 update_begin (f);
14045 rif->update_window_begin_hook (w);
14046 rif->clear_window_mouse_face (w);
14047 rif->scroll_run_hook (w, &run);
14048 rif->update_window_end_hook (w, 0, 0);
14049 update_end (f);
14052 /* Adjust Y positions of reused rows. */
14053 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14054 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14055 max_y = it.last_visible_y;
14056 for (row = first_reusable_row; row < first_row_to_display; ++row)
14058 row->y -= dy;
14059 row->visible_height = row->height;
14060 if (row->y < min_y)
14061 row->visible_height -= min_y - row->y;
14062 if (row->y + row->height > max_y)
14063 row->visible_height -= row->y + row->height - max_y;
14064 row->redraw_fringe_bitmaps_p = 1;
14067 /* Scroll the current matrix. */
14068 xassert (nrows_scrolled > 0);
14069 rotate_matrix (w->current_matrix,
14070 start_vpos,
14071 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14072 -nrows_scrolled);
14074 /* Disable rows not reused. */
14075 for (row -= nrows_scrolled; row < bottom_row; ++row)
14076 row->enabled_p = 0;
14078 /* Point may have moved to a different line, so we cannot assume that
14079 the previous cursor position is valid; locate the correct row. */
14080 if (pt_row)
14082 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14083 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14084 row++)
14086 w->cursor.vpos++;
14087 w->cursor.y = row->y;
14089 if (row < bottom_row)
14091 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14092 while (glyph->charpos < PT)
14094 w->cursor.hpos++;
14095 w->cursor.x += glyph->pixel_width;
14096 glyph++;
14101 /* Adjust window end. A null value of last_text_row means that
14102 the window end is in reused rows which in turn means that
14103 only its vpos can have changed. */
14104 if (last_text_row)
14106 w->window_end_bytepos
14107 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14108 w->window_end_pos
14109 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14110 w->window_end_vpos
14111 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14113 else
14115 w->window_end_vpos
14116 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14119 w->window_end_valid = Qnil;
14120 w->desired_matrix->no_scrolling_p = 1;
14122 #if GLYPH_DEBUG
14123 debug_method_add (w, "try_window_reusing_current_matrix 2");
14124 #endif
14125 return 1;
14128 return 0;
14133 /************************************************************************
14134 Window redisplay reusing current matrix when buffer has changed
14135 ************************************************************************/
14137 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14138 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14139 int *, int *));
14140 static struct glyph_row *
14141 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14142 struct glyph_row *));
14145 /* Return the last row in MATRIX displaying text. If row START is
14146 non-null, start searching with that row. IT gives the dimensions
14147 of the display. Value is null if matrix is empty; otherwise it is
14148 a pointer to the row found. */
14150 static struct glyph_row *
14151 find_last_row_displaying_text (matrix, it, start)
14152 struct glyph_matrix *matrix;
14153 struct it *it;
14154 struct glyph_row *start;
14156 struct glyph_row *row, *row_found;
14158 /* Set row_found to the last row in IT->w's current matrix
14159 displaying text. The loop looks funny but think of partially
14160 visible lines. */
14161 row_found = NULL;
14162 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14163 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14165 xassert (row->enabled_p);
14166 row_found = row;
14167 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14168 break;
14169 ++row;
14172 return row_found;
14176 /* Return the last row in the current matrix of W that is not affected
14177 by changes at the start of current_buffer that occurred since W's
14178 current matrix was built. Value is null if no such row exists.
14180 BEG_UNCHANGED us the number of characters unchanged at the start of
14181 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14182 first changed character in current_buffer. Characters at positions <
14183 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14184 when the current matrix was built. */
14186 static struct glyph_row *
14187 find_last_unchanged_at_beg_row (w)
14188 struct window *w;
14190 int first_changed_pos = BEG + BEG_UNCHANGED;
14191 struct glyph_row *row;
14192 struct glyph_row *row_found = NULL;
14193 int yb = window_text_bottom_y (w);
14195 /* Find the last row displaying unchanged text. */
14196 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14197 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14198 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14200 if (/* If row ends before first_changed_pos, it is unchanged,
14201 except in some case. */
14202 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14203 /* When row ends in ZV and we write at ZV it is not
14204 unchanged. */
14205 && !row->ends_at_zv_p
14206 /* When first_changed_pos is the end of a continued line,
14207 row is not unchanged because it may be no longer
14208 continued. */
14209 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14210 && (row->continued_p
14211 || row->exact_window_width_line_p)))
14212 row_found = row;
14214 /* Stop if last visible row. */
14215 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14216 break;
14218 ++row;
14221 return row_found;
14225 /* Find the first glyph row in the current matrix of W that is not
14226 affected by changes at the end of current_buffer since the
14227 time W's current matrix was built.
14229 Return in *DELTA the number of chars by which buffer positions in
14230 unchanged text at the end of current_buffer must be adjusted.
14232 Return in *DELTA_BYTES the corresponding number of bytes.
14234 Value is null if no such row exists, i.e. all rows are affected by
14235 changes. */
14237 static struct glyph_row *
14238 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14239 struct window *w;
14240 int *delta, *delta_bytes;
14242 struct glyph_row *row;
14243 struct glyph_row *row_found = NULL;
14245 *delta = *delta_bytes = 0;
14247 /* Display must not have been paused, otherwise the current matrix
14248 is not up to date. */
14249 if (NILP (w->window_end_valid))
14250 abort ();
14252 /* A value of window_end_pos >= END_UNCHANGED means that the window
14253 end is in the range of changed text. If so, there is no
14254 unchanged row at the end of W's current matrix. */
14255 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14256 return NULL;
14258 /* Set row to the last row in W's current matrix displaying text. */
14259 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14261 /* If matrix is entirely empty, no unchanged row exists. */
14262 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14264 /* The value of row is the last glyph row in the matrix having a
14265 meaningful buffer position in it. The end position of row
14266 corresponds to window_end_pos. This allows us to translate
14267 buffer positions in the current matrix to current buffer
14268 positions for characters not in changed text. */
14269 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14270 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14271 int last_unchanged_pos, last_unchanged_pos_old;
14272 struct glyph_row *first_text_row
14273 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14275 *delta = Z - Z_old;
14276 *delta_bytes = Z_BYTE - Z_BYTE_old;
14278 /* Set last_unchanged_pos to the buffer position of the last
14279 character in the buffer that has not been changed. Z is the
14280 index + 1 of the last character in current_buffer, i.e. by
14281 subtracting END_UNCHANGED we get the index of the last
14282 unchanged character, and we have to add BEG to get its buffer
14283 position. */
14284 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14285 last_unchanged_pos_old = last_unchanged_pos - *delta;
14287 /* Search backward from ROW for a row displaying a line that
14288 starts at a minimum position >= last_unchanged_pos_old. */
14289 for (; row > first_text_row; --row)
14291 /* This used to abort, but it can happen.
14292 It is ok to just stop the search instead here. KFS. */
14293 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14294 break;
14296 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14297 row_found = row;
14301 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14302 abort ();
14304 return row_found;
14308 /* Make sure that glyph rows in the current matrix of window W
14309 reference the same glyph memory as corresponding rows in the
14310 frame's frame matrix. This function is called after scrolling W's
14311 current matrix on a terminal frame in try_window_id and
14312 try_window_reusing_current_matrix. */
14314 static void
14315 sync_frame_with_window_matrix_rows (w)
14316 struct window *w;
14318 struct frame *f = XFRAME (w->frame);
14319 struct glyph_row *window_row, *window_row_end, *frame_row;
14321 /* Preconditions: W must be a leaf window and full-width. Its frame
14322 must have a frame matrix. */
14323 xassert (NILP (w->hchild) && NILP (w->vchild));
14324 xassert (WINDOW_FULL_WIDTH_P (w));
14325 xassert (!FRAME_WINDOW_P (f));
14327 /* If W is a full-width window, glyph pointers in W's current matrix
14328 have, by definition, to be the same as glyph pointers in the
14329 corresponding frame matrix. Note that frame matrices have no
14330 marginal areas (see build_frame_matrix). */
14331 window_row = w->current_matrix->rows;
14332 window_row_end = window_row + w->current_matrix->nrows;
14333 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14334 while (window_row < window_row_end)
14336 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14337 struct glyph *end = window_row->glyphs[LAST_AREA];
14339 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14340 frame_row->glyphs[TEXT_AREA] = start;
14341 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14342 frame_row->glyphs[LAST_AREA] = end;
14344 /* Disable frame rows whose corresponding window rows have
14345 been disabled in try_window_id. */
14346 if (!window_row->enabled_p)
14347 frame_row->enabled_p = 0;
14349 ++window_row, ++frame_row;
14354 /* Find the glyph row in window W containing CHARPOS. Consider all
14355 rows between START and END (not inclusive). END null means search
14356 all rows to the end of the display area of W. Value is the row
14357 containing CHARPOS or null. */
14359 struct glyph_row *
14360 row_containing_pos (w, charpos, start, end, dy)
14361 struct window *w;
14362 int charpos;
14363 struct glyph_row *start, *end;
14364 int dy;
14366 struct glyph_row *row = start;
14367 int last_y;
14369 /* If we happen to start on a header-line, skip that. */
14370 if (row->mode_line_p)
14371 ++row;
14373 if ((end && row >= end) || !row->enabled_p)
14374 return NULL;
14376 last_y = window_text_bottom_y (w) - dy;
14378 while (1)
14380 /* Give up if we have gone too far. */
14381 if (end && row >= end)
14382 return NULL;
14383 /* This formerly returned if they were equal.
14384 I think that both quantities are of a "last plus one" type;
14385 if so, when they are equal, the row is within the screen. -- rms. */
14386 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14387 return NULL;
14389 /* If it is in this row, return this row. */
14390 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14391 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14392 /* The end position of a row equals the start
14393 position of the next row. If CHARPOS is there, we
14394 would rather display it in the next line, except
14395 when this line ends in ZV. */
14396 && !row->ends_at_zv_p
14397 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14398 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14399 return row;
14400 ++row;
14405 /* Try to redisplay window W by reusing its existing display. W's
14406 current matrix must be up to date when this function is called,
14407 i.e. window_end_valid must not be nil.
14409 Value is
14411 1 if display has been updated
14412 0 if otherwise unsuccessful
14413 -1 if redisplay with same window start is known not to succeed
14415 The following steps are performed:
14417 1. Find the last row in the current matrix of W that is not
14418 affected by changes at the start of current_buffer. If no such row
14419 is found, give up.
14421 2. Find the first row in W's current matrix that is not affected by
14422 changes at the end of current_buffer. Maybe there is no such row.
14424 3. Display lines beginning with the row + 1 found in step 1 to the
14425 row found in step 2 or, if step 2 didn't find a row, to the end of
14426 the window.
14428 4. If cursor is not known to appear on the window, give up.
14430 5. If display stopped at the row found in step 2, scroll the
14431 display and current matrix as needed.
14433 6. Maybe display some lines at the end of W, if we must. This can
14434 happen under various circumstances, like a partially visible line
14435 becoming fully visible, or because newly displayed lines are displayed
14436 in smaller font sizes.
14438 7. Update W's window end information. */
14440 static int
14441 try_window_id (w)
14442 struct window *w;
14444 struct frame *f = XFRAME (w->frame);
14445 struct glyph_matrix *current_matrix = w->current_matrix;
14446 struct glyph_matrix *desired_matrix = w->desired_matrix;
14447 struct glyph_row *last_unchanged_at_beg_row;
14448 struct glyph_row *first_unchanged_at_end_row;
14449 struct glyph_row *row;
14450 struct glyph_row *bottom_row;
14451 int bottom_vpos;
14452 struct it it;
14453 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14454 struct text_pos start_pos;
14455 struct run run;
14456 int first_unchanged_at_end_vpos = 0;
14457 struct glyph_row *last_text_row, *last_text_row_at_end;
14458 struct text_pos start;
14459 int first_changed_charpos, last_changed_charpos;
14461 #if GLYPH_DEBUG
14462 if (inhibit_try_window_id)
14463 return 0;
14464 #endif
14466 /* This is handy for debugging. */
14467 #if 0
14468 #define GIVE_UP(X) \
14469 do { \
14470 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14471 return 0; \
14472 } while (0)
14473 #else
14474 #define GIVE_UP(X) return 0
14475 #endif
14477 SET_TEXT_POS_FROM_MARKER (start, w->start);
14479 /* Don't use this for mini-windows because these can show
14480 messages and mini-buffers, and we don't handle that here. */
14481 if (MINI_WINDOW_P (w))
14482 GIVE_UP (1);
14484 /* This flag is used to prevent redisplay optimizations. */
14485 if (windows_or_buffers_changed || cursor_type_changed)
14486 GIVE_UP (2);
14488 /* Verify that narrowing has not changed.
14489 Also verify that we were not told to prevent redisplay optimizations.
14490 It would be nice to further
14491 reduce the number of cases where this prevents try_window_id. */
14492 if (current_buffer->clip_changed
14493 || current_buffer->prevent_redisplay_optimizations_p)
14494 GIVE_UP (3);
14496 /* Window must either use window-based redisplay or be full width. */
14497 if (!FRAME_WINDOW_P (f)
14498 && (!line_ins_del_ok
14499 || !WINDOW_FULL_WIDTH_P (w)))
14500 GIVE_UP (4);
14502 /* Give up if point is not known NOT to appear in W. */
14503 if (PT < CHARPOS (start))
14504 GIVE_UP (5);
14506 /* Another way to prevent redisplay optimizations. */
14507 if (XFASTINT (w->last_modified) == 0)
14508 GIVE_UP (6);
14510 /* Verify that window is not hscrolled. */
14511 if (XFASTINT (w->hscroll) != 0)
14512 GIVE_UP (7);
14514 /* Verify that display wasn't paused. */
14515 if (NILP (w->window_end_valid))
14516 GIVE_UP (8);
14518 /* Can't use this if highlighting a region because a cursor movement
14519 will do more than just set the cursor. */
14520 if (!NILP (Vtransient_mark_mode)
14521 && !NILP (current_buffer->mark_active))
14522 GIVE_UP (9);
14524 /* Likewise if highlighting trailing whitespace. */
14525 if (!NILP (Vshow_trailing_whitespace))
14526 GIVE_UP (11);
14528 /* Likewise if showing a region. */
14529 if (!NILP (w->region_showing))
14530 GIVE_UP (10);
14532 /* Can use this if overlay arrow position and or string have changed. */
14533 if (overlay_arrows_changed_p ())
14534 GIVE_UP (12);
14537 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14538 only if buffer has really changed. The reason is that the gap is
14539 initially at Z for freshly visited files. The code below would
14540 set end_unchanged to 0 in that case. */
14541 if (MODIFF > SAVE_MODIFF
14542 /* This seems to happen sometimes after saving a buffer. */
14543 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14545 if (GPT - BEG < BEG_UNCHANGED)
14546 BEG_UNCHANGED = GPT - BEG;
14547 if (Z - GPT < END_UNCHANGED)
14548 END_UNCHANGED = Z - GPT;
14551 /* The position of the first and last character that has been changed. */
14552 first_changed_charpos = BEG + BEG_UNCHANGED;
14553 last_changed_charpos = Z - END_UNCHANGED;
14555 /* If window starts after a line end, and the last change is in
14556 front of that newline, then changes don't affect the display.
14557 This case happens with stealth-fontification. Note that although
14558 the display is unchanged, glyph positions in the matrix have to
14559 be adjusted, of course. */
14560 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14561 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14562 && ((last_changed_charpos < CHARPOS (start)
14563 && CHARPOS (start) == BEGV)
14564 || (last_changed_charpos < CHARPOS (start) - 1
14565 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14567 int Z_old, delta, Z_BYTE_old, delta_bytes;
14568 struct glyph_row *r0;
14570 /* Compute how many chars/bytes have been added to or removed
14571 from the buffer. */
14572 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14573 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14574 delta = Z - Z_old;
14575 delta_bytes = Z_BYTE - Z_BYTE_old;
14577 /* Give up if PT is not in the window. Note that it already has
14578 been checked at the start of try_window_id that PT is not in
14579 front of the window start. */
14580 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14581 GIVE_UP (13);
14583 /* If window start is unchanged, we can reuse the whole matrix
14584 as is, after adjusting glyph positions. No need to compute
14585 the window end again, since its offset from Z hasn't changed. */
14586 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14587 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14588 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14589 /* PT must not be in a partially visible line. */
14590 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14591 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14593 /* Adjust positions in the glyph matrix. */
14594 if (delta || delta_bytes)
14596 struct glyph_row *r1
14597 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14598 increment_matrix_positions (w->current_matrix,
14599 MATRIX_ROW_VPOS (r0, current_matrix),
14600 MATRIX_ROW_VPOS (r1, current_matrix),
14601 delta, delta_bytes);
14604 /* Set the cursor. */
14605 row = row_containing_pos (w, PT, r0, NULL, 0);
14606 if (row)
14607 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14608 else
14609 abort ();
14610 return 1;
14614 /* Handle the case that changes are all below what is displayed in
14615 the window, and that PT is in the window. This shortcut cannot
14616 be taken if ZV is visible in the window, and text has been added
14617 there that is visible in the window. */
14618 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14619 /* ZV is not visible in the window, or there are no
14620 changes at ZV, actually. */
14621 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14622 || first_changed_charpos == last_changed_charpos))
14624 struct glyph_row *r0;
14626 /* Give up if PT is not in the window. Note that it already has
14627 been checked at the start of try_window_id that PT is not in
14628 front of the window start. */
14629 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14630 GIVE_UP (14);
14632 /* If window start is unchanged, we can reuse the whole matrix
14633 as is, without changing glyph positions since no text has
14634 been added/removed in front of the window end. */
14635 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14636 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14637 /* PT must not be in a partially visible line. */
14638 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14639 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14641 /* We have to compute the window end anew since text
14642 can have been added/removed after it. */
14643 w->window_end_pos
14644 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14645 w->window_end_bytepos
14646 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14648 /* Set the cursor. */
14649 row = row_containing_pos (w, PT, r0, NULL, 0);
14650 if (row)
14651 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14652 else
14653 abort ();
14654 return 2;
14658 /* Give up if window start is in the changed area.
14660 The condition used to read
14662 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14664 but why that was tested escapes me at the moment. */
14665 if (CHARPOS (start) >= first_changed_charpos
14666 && CHARPOS (start) <= last_changed_charpos)
14667 GIVE_UP (15);
14669 /* Check that window start agrees with the start of the first glyph
14670 row in its current matrix. Check this after we know the window
14671 start is not in changed text, otherwise positions would not be
14672 comparable. */
14673 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14674 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14675 GIVE_UP (16);
14677 /* Give up if the window ends in strings. Overlay strings
14678 at the end are difficult to handle, so don't try. */
14679 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14680 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14681 GIVE_UP (20);
14683 /* Compute the position at which we have to start displaying new
14684 lines. Some of the lines at the top of the window might be
14685 reusable because they are not displaying changed text. Find the
14686 last row in W's current matrix not affected by changes at the
14687 start of current_buffer. Value is null if changes start in the
14688 first line of window. */
14689 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14690 if (last_unchanged_at_beg_row)
14692 /* Avoid starting to display in the moddle of a character, a TAB
14693 for instance. This is easier than to set up the iterator
14694 exactly, and it's not a frequent case, so the additional
14695 effort wouldn't really pay off. */
14696 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14697 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14698 && last_unchanged_at_beg_row > w->current_matrix->rows)
14699 --last_unchanged_at_beg_row;
14701 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14702 GIVE_UP (17);
14704 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14705 GIVE_UP (18);
14706 start_pos = it.current.pos;
14708 /* Start displaying new lines in the desired matrix at the same
14709 vpos we would use in the current matrix, i.e. below
14710 last_unchanged_at_beg_row. */
14711 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14712 current_matrix);
14713 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14714 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14716 xassert (it.hpos == 0 && it.current_x == 0);
14718 else
14720 /* There are no reusable lines at the start of the window.
14721 Start displaying in the first text line. */
14722 start_display (&it, w, start);
14723 it.vpos = it.first_vpos;
14724 start_pos = it.current.pos;
14727 /* Find the first row that is not affected by changes at the end of
14728 the buffer. Value will be null if there is no unchanged row, in
14729 which case we must redisplay to the end of the window. delta
14730 will be set to the value by which buffer positions beginning with
14731 first_unchanged_at_end_row have to be adjusted due to text
14732 changes. */
14733 first_unchanged_at_end_row
14734 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14735 IF_DEBUG (debug_delta = delta);
14736 IF_DEBUG (debug_delta_bytes = delta_bytes);
14738 /* Set stop_pos to the buffer position up to which we will have to
14739 display new lines. If first_unchanged_at_end_row != NULL, this
14740 is the buffer position of the start of the line displayed in that
14741 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14742 that we don't stop at a buffer position. */
14743 stop_pos = 0;
14744 if (first_unchanged_at_end_row)
14746 xassert (last_unchanged_at_beg_row == NULL
14747 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14749 /* If this is a continuation line, move forward to the next one
14750 that isn't. Changes in lines above affect this line.
14751 Caution: this may move first_unchanged_at_end_row to a row
14752 not displaying text. */
14753 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14754 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14755 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14756 < it.last_visible_y))
14757 ++first_unchanged_at_end_row;
14759 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14760 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14761 >= it.last_visible_y))
14762 first_unchanged_at_end_row = NULL;
14763 else
14765 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14766 + delta);
14767 first_unchanged_at_end_vpos
14768 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14769 xassert (stop_pos >= Z - END_UNCHANGED);
14772 else if (last_unchanged_at_beg_row == NULL)
14773 GIVE_UP (19);
14776 #if GLYPH_DEBUG
14778 /* Either there is no unchanged row at the end, or the one we have
14779 now displays text. This is a necessary condition for the window
14780 end pos calculation at the end of this function. */
14781 xassert (first_unchanged_at_end_row == NULL
14782 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14784 debug_last_unchanged_at_beg_vpos
14785 = (last_unchanged_at_beg_row
14786 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14787 : -1);
14788 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14790 #endif /* GLYPH_DEBUG != 0 */
14793 /* Display new lines. Set last_text_row to the last new line
14794 displayed which has text on it, i.e. might end up as being the
14795 line where the window_end_vpos is. */
14796 w->cursor.vpos = -1;
14797 last_text_row = NULL;
14798 overlay_arrow_seen = 0;
14799 while (it.current_y < it.last_visible_y
14800 && !fonts_changed_p
14801 && (first_unchanged_at_end_row == NULL
14802 || IT_CHARPOS (it) < stop_pos))
14804 if (display_line (&it))
14805 last_text_row = it.glyph_row - 1;
14808 if (fonts_changed_p)
14809 return -1;
14812 /* Compute differences in buffer positions, y-positions etc. for
14813 lines reused at the bottom of the window. Compute what we can
14814 scroll. */
14815 if (first_unchanged_at_end_row
14816 /* No lines reused because we displayed everything up to the
14817 bottom of the window. */
14818 && it.current_y < it.last_visible_y)
14820 dvpos = (it.vpos
14821 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14822 current_matrix));
14823 dy = it.current_y - first_unchanged_at_end_row->y;
14824 run.current_y = first_unchanged_at_end_row->y;
14825 run.desired_y = run.current_y + dy;
14826 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14828 else
14830 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14831 first_unchanged_at_end_row = NULL;
14833 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14836 /* Find the cursor if not already found. We have to decide whether
14837 PT will appear on this window (it sometimes doesn't, but this is
14838 not a very frequent case.) This decision has to be made before
14839 the current matrix is altered. A value of cursor.vpos < 0 means
14840 that PT is either in one of the lines beginning at
14841 first_unchanged_at_end_row or below the window. Don't care for
14842 lines that might be displayed later at the window end; as
14843 mentioned, this is not a frequent case. */
14844 if (w->cursor.vpos < 0)
14846 /* Cursor in unchanged rows at the top? */
14847 if (PT < CHARPOS (start_pos)
14848 && last_unchanged_at_beg_row)
14850 row = row_containing_pos (w, PT,
14851 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14852 last_unchanged_at_beg_row + 1, 0);
14853 if (row)
14854 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14857 /* Start from first_unchanged_at_end_row looking for PT. */
14858 else if (first_unchanged_at_end_row)
14860 row = row_containing_pos (w, PT - delta,
14861 first_unchanged_at_end_row, NULL, 0);
14862 if (row)
14863 set_cursor_from_row (w, row, w->current_matrix, delta,
14864 delta_bytes, dy, dvpos);
14867 /* Give up if cursor was not found. */
14868 if (w->cursor.vpos < 0)
14870 clear_glyph_matrix (w->desired_matrix);
14871 return -1;
14875 /* Don't let the cursor end in the scroll margins. */
14877 int this_scroll_margin, cursor_height;
14879 this_scroll_margin = max (0, scroll_margin);
14880 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14881 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14882 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14884 if ((w->cursor.y < this_scroll_margin
14885 && CHARPOS (start) > BEGV)
14886 /* Old redisplay didn't take scroll margin into account at the bottom,
14887 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14888 || (w->cursor.y + (make_cursor_line_fully_visible_p
14889 ? cursor_height + this_scroll_margin
14890 : 1)) > it.last_visible_y)
14892 w->cursor.vpos = -1;
14893 clear_glyph_matrix (w->desired_matrix);
14894 return -1;
14898 /* Scroll the display. Do it before changing the current matrix so
14899 that xterm.c doesn't get confused about where the cursor glyph is
14900 found. */
14901 if (dy && run.height)
14903 update_begin (f);
14905 if (FRAME_WINDOW_P (f))
14907 rif->update_window_begin_hook (w);
14908 rif->clear_window_mouse_face (w);
14909 rif->scroll_run_hook (w, &run);
14910 rif->update_window_end_hook (w, 0, 0);
14912 else
14914 /* Terminal frame. In this case, dvpos gives the number of
14915 lines to scroll by; dvpos < 0 means scroll up. */
14916 int first_unchanged_at_end_vpos
14917 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14918 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14919 int end = (WINDOW_TOP_EDGE_LINE (w)
14920 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14921 + window_internal_height (w));
14923 /* Perform the operation on the screen. */
14924 if (dvpos > 0)
14926 /* Scroll last_unchanged_at_beg_row to the end of the
14927 window down dvpos lines. */
14928 set_terminal_window (end);
14930 /* On dumb terminals delete dvpos lines at the end
14931 before inserting dvpos empty lines. */
14932 if (!scroll_region_ok)
14933 ins_del_lines (end - dvpos, -dvpos);
14935 /* Insert dvpos empty lines in front of
14936 last_unchanged_at_beg_row. */
14937 ins_del_lines (from, dvpos);
14939 else if (dvpos < 0)
14941 /* Scroll up last_unchanged_at_beg_vpos to the end of
14942 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14943 set_terminal_window (end);
14945 /* Delete dvpos lines in front of
14946 last_unchanged_at_beg_vpos. ins_del_lines will set
14947 the cursor to the given vpos and emit |dvpos| delete
14948 line sequences. */
14949 ins_del_lines (from + dvpos, dvpos);
14951 /* On a dumb terminal insert dvpos empty lines at the
14952 end. */
14953 if (!scroll_region_ok)
14954 ins_del_lines (end + dvpos, -dvpos);
14957 set_terminal_window (0);
14960 update_end (f);
14963 /* Shift reused rows of the current matrix to the right position.
14964 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14965 text. */
14966 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14967 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14968 if (dvpos < 0)
14970 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14971 bottom_vpos, dvpos);
14972 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14973 bottom_vpos, 0);
14975 else if (dvpos > 0)
14977 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14978 bottom_vpos, dvpos);
14979 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14980 first_unchanged_at_end_vpos + dvpos, 0);
14983 /* For frame-based redisplay, make sure that current frame and window
14984 matrix are in sync with respect to glyph memory. */
14985 if (!FRAME_WINDOW_P (f))
14986 sync_frame_with_window_matrix_rows (w);
14988 /* Adjust buffer positions in reused rows. */
14989 if (delta || delta_bytes)
14990 increment_matrix_positions (current_matrix,
14991 first_unchanged_at_end_vpos + dvpos,
14992 bottom_vpos, delta, delta_bytes);
14994 /* Adjust Y positions. */
14995 if (dy)
14996 shift_glyph_matrix (w, current_matrix,
14997 first_unchanged_at_end_vpos + dvpos,
14998 bottom_vpos, dy);
15000 if (first_unchanged_at_end_row)
15002 first_unchanged_at_end_row += dvpos;
15003 if (first_unchanged_at_end_row->y >= it.last_visible_y
15004 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15005 first_unchanged_at_end_row = NULL;
15008 /* If scrolling up, there may be some lines to display at the end of
15009 the window. */
15010 last_text_row_at_end = NULL;
15011 if (dy < 0)
15013 /* Scrolling up can leave for example a partially visible line
15014 at the end of the window to be redisplayed. */
15015 /* Set last_row to the glyph row in the current matrix where the
15016 window end line is found. It has been moved up or down in
15017 the matrix by dvpos. */
15018 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15019 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15021 /* If last_row is the window end line, it should display text. */
15022 xassert (last_row->displays_text_p);
15024 /* If window end line was partially visible before, begin
15025 displaying at that line. Otherwise begin displaying with the
15026 line following it. */
15027 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15029 init_to_row_start (&it, w, last_row);
15030 it.vpos = last_vpos;
15031 it.current_y = last_row->y;
15033 else
15035 init_to_row_end (&it, w, last_row);
15036 it.vpos = 1 + last_vpos;
15037 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15038 ++last_row;
15041 /* We may start in a continuation line. If so, we have to
15042 get the right continuation_lines_width and current_x. */
15043 it.continuation_lines_width = last_row->continuation_lines_width;
15044 it.hpos = it.current_x = 0;
15046 /* Display the rest of the lines at the window end. */
15047 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15048 while (it.current_y < it.last_visible_y
15049 && !fonts_changed_p)
15051 /* Is it always sure that the display agrees with lines in
15052 the current matrix? I don't think so, so we mark rows
15053 displayed invalid in the current matrix by setting their
15054 enabled_p flag to zero. */
15055 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15056 if (display_line (&it))
15057 last_text_row_at_end = it.glyph_row - 1;
15061 /* Update window_end_pos and window_end_vpos. */
15062 if (first_unchanged_at_end_row
15063 && !last_text_row_at_end)
15065 /* Window end line if one of the preserved rows from the current
15066 matrix. Set row to the last row displaying text in current
15067 matrix starting at first_unchanged_at_end_row, after
15068 scrolling. */
15069 xassert (first_unchanged_at_end_row->displays_text_p);
15070 row = find_last_row_displaying_text (w->current_matrix, &it,
15071 first_unchanged_at_end_row);
15072 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15074 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15075 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15076 w->window_end_vpos
15077 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15078 xassert (w->window_end_bytepos >= 0);
15079 IF_DEBUG (debug_method_add (w, "A"));
15081 else if (last_text_row_at_end)
15083 w->window_end_pos
15084 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15085 w->window_end_bytepos
15086 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15087 w->window_end_vpos
15088 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15089 xassert (w->window_end_bytepos >= 0);
15090 IF_DEBUG (debug_method_add (w, "B"));
15092 else if (last_text_row)
15094 /* We have displayed either to the end of the window or at the
15095 end of the window, i.e. the last row with text is to be found
15096 in the desired matrix. */
15097 w->window_end_pos
15098 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15099 w->window_end_bytepos
15100 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15101 w->window_end_vpos
15102 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15103 xassert (w->window_end_bytepos >= 0);
15105 else if (first_unchanged_at_end_row == NULL
15106 && last_text_row == NULL
15107 && last_text_row_at_end == NULL)
15109 /* Displayed to end of window, but no line containing text was
15110 displayed. Lines were deleted at the end of the window. */
15111 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15112 int vpos = XFASTINT (w->window_end_vpos);
15113 struct glyph_row *current_row = current_matrix->rows + vpos;
15114 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15116 for (row = NULL;
15117 row == NULL && vpos >= first_vpos;
15118 --vpos, --current_row, --desired_row)
15120 if (desired_row->enabled_p)
15122 if (desired_row->displays_text_p)
15123 row = desired_row;
15125 else if (current_row->displays_text_p)
15126 row = current_row;
15129 xassert (row != NULL);
15130 w->window_end_vpos = make_number (vpos + 1);
15131 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15132 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15133 xassert (w->window_end_bytepos >= 0);
15134 IF_DEBUG (debug_method_add (w, "C"));
15136 else
15137 abort ();
15139 #if 0 /* This leads to problems, for instance when the cursor is
15140 at ZV, and the cursor line displays no text. */
15141 /* Disable rows below what's displayed in the window. This makes
15142 debugging easier. */
15143 enable_glyph_matrix_rows (current_matrix,
15144 XFASTINT (w->window_end_vpos) + 1,
15145 bottom_vpos, 0);
15146 #endif
15148 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15149 debug_end_vpos = XFASTINT (w->window_end_vpos));
15151 /* Record that display has not been completed. */
15152 w->window_end_valid = Qnil;
15153 w->desired_matrix->no_scrolling_p = 1;
15154 return 3;
15156 #undef GIVE_UP
15161 /***********************************************************************
15162 More debugging support
15163 ***********************************************************************/
15165 #if GLYPH_DEBUG
15167 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15168 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15169 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15172 /* Dump the contents of glyph matrix MATRIX on stderr.
15174 GLYPHS 0 means don't show glyph contents.
15175 GLYPHS 1 means show glyphs in short form
15176 GLYPHS > 1 means show glyphs in long form. */
15178 void
15179 dump_glyph_matrix (matrix, glyphs)
15180 struct glyph_matrix *matrix;
15181 int glyphs;
15183 int i;
15184 for (i = 0; i < matrix->nrows; ++i)
15185 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15189 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15190 the glyph row and area where the glyph comes from. */
15192 void
15193 dump_glyph (row, glyph, area)
15194 struct glyph_row *row;
15195 struct glyph *glyph;
15196 int area;
15198 if (glyph->type == CHAR_GLYPH)
15200 fprintf (stderr,
15201 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15202 glyph - row->glyphs[TEXT_AREA],
15203 'C',
15204 glyph->charpos,
15205 (BUFFERP (glyph->object)
15206 ? 'B'
15207 : (STRINGP (glyph->object)
15208 ? 'S'
15209 : '-')),
15210 glyph->pixel_width,
15211 glyph->u.ch,
15212 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15213 ? glyph->u.ch
15214 : '.'),
15215 glyph->face_id,
15216 glyph->left_box_line_p,
15217 glyph->right_box_line_p);
15219 else if (glyph->type == STRETCH_GLYPH)
15221 fprintf (stderr,
15222 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15223 glyph - row->glyphs[TEXT_AREA],
15224 'S',
15225 glyph->charpos,
15226 (BUFFERP (glyph->object)
15227 ? 'B'
15228 : (STRINGP (glyph->object)
15229 ? 'S'
15230 : '-')),
15231 glyph->pixel_width,
15233 '.',
15234 glyph->face_id,
15235 glyph->left_box_line_p,
15236 glyph->right_box_line_p);
15238 else if (glyph->type == IMAGE_GLYPH)
15240 fprintf (stderr,
15241 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15242 glyph - row->glyphs[TEXT_AREA],
15243 'I',
15244 glyph->charpos,
15245 (BUFFERP (glyph->object)
15246 ? 'B'
15247 : (STRINGP (glyph->object)
15248 ? 'S'
15249 : '-')),
15250 glyph->pixel_width,
15251 glyph->u.img_id,
15252 '.',
15253 glyph->face_id,
15254 glyph->left_box_line_p,
15255 glyph->right_box_line_p);
15257 else if (glyph->type == COMPOSITE_GLYPH)
15259 fprintf (stderr,
15260 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15261 glyph - row->glyphs[TEXT_AREA],
15262 '+',
15263 glyph->charpos,
15264 (BUFFERP (glyph->object)
15265 ? 'B'
15266 : (STRINGP (glyph->object)
15267 ? 'S'
15268 : '-')),
15269 glyph->pixel_width,
15270 glyph->u.cmp_id,
15271 '.',
15272 glyph->face_id,
15273 glyph->left_box_line_p,
15274 glyph->right_box_line_p);
15279 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15280 GLYPHS 0 means don't show glyph contents.
15281 GLYPHS 1 means show glyphs in short form
15282 GLYPHS > 1 means show glyphs in long form. */
15284 void
15285 dump_glyph_row (row, vpos, glyphs)
15286 struct glyph_row *row;
15287 int vpos, glyphs;
15289 if (glyphs != 1)
15291 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15292 fprintf (stderr, "======================================================================\n");
15294 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15295 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15296 vpos,
15297 MATRIX_ROW_START_CHARPOS (row),
15298 MATRIX_ROW_END_CHARPOS (row),
15299 row->used[TEXT_AREA],
15300 row->contains_overlapping_glyphs_p,
15301 row->enabled_p,
15302 row->truncated_on_left_p,
15303 row->truncated_on_right_p,
15304 row->continued_p,
15305 MATRIX_ROW_CONTINUATION_LINE_P (row),
15306 row->displays_text_p,
15307 row->ends_at_zv_p,
15308 row->fill_line_p,
15309 row->ends_in_middle_of_char_p,
15310 row->starts_in_middle_of_char_p,
15311 row->mouse_face_p,
15312 row->x,
15313 row->y,
15314 row->pixel_width,
15315 row->height,
15316 row->visible_height,
15317 row->ascent,
15318 row->phys_ascent);
15319 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15320 row->end.overlay_string_index,
15321 row->continuation_lines_width);
15322 fprintf (stderr, "%9d %5d\n",
15323 CHARPOS (row->start.string_pos),
15324 CHARPOS (row->end.string_pos));
15325 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15326 row->end.dpvec_index);
15329 if (glyphs > 1)
15331 int area;
15333 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15335 struct glyph *glyph = row->glyphs[area];
15336 struct glyph *glyph_end = glyph + row->used[area];
15338 /* Glyph for a line end in text. */
15339 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15340 ++glyph_end;
15342 if (glyph < glyph_end)
15343 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15345 for (; glyph < glyph_end; ++glyph)
15346 dump_glyph (row, glyph, area);
15349 else if (glyphs == 1)
15351 int area;
15353 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15355 char *s = (char *) alloca (row->used[area] + 1);
15356 int i;
15358 for (i = 0; i < row->used[area]; ++i)
15360 struct glyph *glyph = row->glyphs[area] + i;
15361 if (glyph->type == CHAR_GLYPH
15362 && glyph->u.ch < 0x80
15363 && glyph->u.ch >= ' ')
15364 s[i] = glyph->u.ch;
15365 else
15366 s[i] = '.';
15369 s[i] = '\0';
15370 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15376 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15377 Sdump_glyph_matrix, 0, 1, "p",
15378 doc: /* Dump the current matrix of the selected window to stderr.
15379 Shows contents of glyph row structures. With non-nil
15380 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15381 glyphs in short form, otherwise show glyphs in long form. */)
15382 (glyphs)
15383 Lisp_Object glyphs;
15385 struct window *w = XWINDOW (selected_window);
15386 struct buffer *buffer = XBUFFER (w->buffer);
15388 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15389 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15390 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15391 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15392 fprintf (stderr, "=============================================\n");
15393 dump_glyph_matrix (w->current_matrix,
15394 NILP (glyphs) ? 0 : XINT (glyphs));
15395 return Qnil;
15399 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15400 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15403 struct frame *f = XFRAME (selected_frame);
15404 dump_glyph_matrix (f->current_matrix, 1);
15405 return Qnil;
15409 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15410 doc: /* Dump glyph row ROW to stderr.
15411 GLYPH 0 means don't dump glyphs.
15412 GLYPH 1 means dump glyphs in short form.
15413 GLYPH > 1 or omitted means dump glyphs in long form. */)
15414 (row, glyphs)
15415 Lisp_Object row, glyphs;
15417 struct glyph_matrix *matrix;
15418 int vpos;
15420 CHECK_NUMBER (row);
15421 matrix = XWINDOW (selected_window)->current_matrix;
15422 vpos = XINT (row);
15423 if (vpos >= 0 && vpos < matrix->nrows)
15424 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15425 vpos,
15426 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15427 return Qnil;
15431 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15432 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15433 GLYPH 0 means don't dump glyphs.
15434 GLYPH 1 means dump glyphs in short form.
15435 GLYPH > 1 or omitted means dump glyphs in long form. */)
15436 (row, glyphs)
15437 Lisp_Object row, glyphs;
15439 struct frame *sf = SELECTED_FRAME ();
15440 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15441 int vpos;
15443 CHECK_NUMBER (row);
15444 vpos = XINT (row);
15445 if (vpos >= 0 && vpos < m->nrows)
15446 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15447 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15448 return Qnil;
15452 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15453 doc: /* Toggle tracing of redisplay.
15454 With ARG, turn tracing on if and only if ARG is positive. */)
15455 (arg)
15456 Lisp_Object arg;
15458 if (NILP (arg))
15459 trace_redisplay_p = !trace_redisplay_p;
15460 else
15462 arg = Fprefix_numeric_value (arg);
15463 trace_redisplay_p = XINT (arg) > 0;
15466 return Qnil;
15470 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15471 doc: /* Like `format', but print result to stderr.
15472 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15473 (nargs, args)
15474 int nargs;
15475 Lisp_Object *args;
15477 Lisp_Object s = Fformat (nargs, args);
15478 fprintf (stderr, "%s", SDATA (s));
15479 return Qnil;
15482 #endif /* GLYPH_DEBUG */
15486 /***********************************************************************
15487 Building Desired Matrix Rows
15488 ***********************************************************************/
15490 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15491 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15493 static struct glyph_row *
15494 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15495 struct window *w;
15496 Lisp_Object overlay_arrow_string;
15498 struct frame *f = XFRAME (WINDOW_FRAME (w));
15499 struct buffer *buffer = XBUFFER (w->buffer);
15500 struct buffer *old = current_buffer;
15501 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15502 int arrow_len = SCHARS (overlay_arrow_string);
15503 const unsigned char *arrow_end = arrow_string + arrow_len;
15504 const unsigned char *p;
15505 struct it it;
15506 int multibyte_p;
15507 int n_glyphs_before;
15509 set_buffer_temp (buffer);
15510 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15511 it.glyph_row->used[TEXT_AREA] = 0;
15512 SET_TEXT_POS (it.position, 0, 0);
15514 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15515 p = arrow_string;
15516 while (p < arrow_end)
15518 Lisp_Object face, ilisp;
15520 /* Get the next character. */
15521 if (multibyte_p)
15522 it.c = string_char_and_length (p, arrow_len, &it.len);
15523 else
15524 it.c = *p, it.len = 1;
15525 p += it.len;
15527 /* Get its face. */
15528 ilisp = make_number (p - arrow_string);
15529 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15530 it.face_id = compute_char_face (f, it.c, face);
15532 /* Compute its width, get its glyphs. */
15533 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15534 SET_TEXT_POS (it.position, -1, -1);
15535 PRODUCE_GLYPHS (&it);
15537 /* If this character doesn't fit any more in the line, we have
15538 to remove some glyphs. */
15539 if (it.current_x > it.last_visible_x)
15541 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15542 break;
15546 set_buffer_temp (old);
15547 return it.glyph_row;
15551 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15552 glyphs are only inserted for terminal frames since we can't really
15553 win with truncation glyphs when partially visible glyphs are
15554 involved. Which glyphs to insert is determined by
15555 produce_special_glyphs. */
15557 static void
15558 insert_left_trunc_glyphs (it)
15559 struct it *it;
15561 struct it truncate_it;
15562 struct glyph *from, *end, *to, *toend;
15564 xassert (!FRAME_WINDOW_P (it->f));
15566 /* Get the truncation glyphs. */
15567 truncate_it = *it;
15568 truncate_it.current_x = 0;
15569 truncate_it.face_id = DEFAULT_FACE_ID;
15570 truncate_it.glyph_row = &scratch_glyph_row;
15571 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15572 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15573 truncate_it.object = make_number (0);
15574 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15576 /* Overwrite glyphs from IT with truncation glyphs. */
15577 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15578 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15579 to = it->glyph_row->glyphs[TEXT_AREA];
15580 toend = to + it->glyph_row->used[TEXT_AREA];
15582 while (from < end)
15583 *to++ = *from++;
15585 /* There may be padding glyphs left over. Overwrite them too. */
15586 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15588 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15589 while (from < end)
15590 *to++ = *from++;
15593 if (to > toend)
15594 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15598 /* Compute the pixel height and width of IT->glyph_row.
15600 Most of the time, ascent and height of a display line will be equal
15601 to the max_ascent and max_height values of the display iterator
15602 structure. This is not the case if
15604 1. We hit ZV without displaying anything. In this case, max_ascent
15605 and max_height will be zero.
15607 2. We have some glyphs that don't contribute to the line height.
15608 (The glyph row flag contributes_to_line_height_p is for future
15609 pixmap extensions).
15611 The first case is easily covered by using default values because in
15612 these cases, the line height does not really matter, except that it
15613 must not be zero. */
15615 static void
15616 compute_line_metrics (it)
15617 struct it *it;
15619 struct glyph_row *row = it->glyph_row;
15620 int area, i;
15622 if (FRAME_WINDOW_P (it->f))
15624 int i, min_y, max_y;
15626 /* The line may consist of one space only, that was added to
15627 place the cursor on it. If so, the row's height hasn't been
15628 computed yet. */
15629 if (row->height == 0)
15631 if (it->max_ascent + it->max_descent == 0)
15632 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15633 row->ascent = it->max_ascent;
15634 row->height = it->max_ascent + it->max_descent;
15635 row->phys_ascent = it->max_phys_ascent;
15636 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15637 row->extra_line_spacing = it->max_extra_line_spacing;
15640 /* Compute the width of this line. */
15641 row->pixel_width = row->x;
15642 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15643 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15645 xassert (row->pixel_width >= 0);
15646 xassert (row->ascent >= 0 && row->height > 0);
15648 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15649 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15651 /* If first line's physical ascent is larger than its logical
15652 ascent, use the physical ascent, and make the row taller.
15653 This makes accented characters fully visible. */
15654 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15655 && row->phys_ascent > row->ascent)
15657 row->height += row->phys_ascent - row->ascent;
15658 row->ascent = row->phys_ascent;
15661 /* Compute how much of the line is visible. */
15662 row->visible_height = row->height;
15664 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15665 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15667 if (row->y < min_y)
15668 row->visible_height -= min_y - row->y;
15669 if (row->y + row->height > max_y)
15670 row->visible_height -= row->y + row->height - max_y;
15672 else
15674 row->pixel_width = row->used[TEXT_AREA];
15675 if (row->continued_p)
15676 row->pixel_width -= it->continuation_pixel_width;
15677 else if (row->truncated_on_right_p)
15678 row->pixel_width -= it->truncation_pixel_width;
15679 row->ascent = row->phys_ascent = 0;
15680 row->height = row->phys_height = row->visible_height = 1;
15681 row->extra_line_spacing = 0;
15684 /* Compute a hash code for this row. */
15685 row->hash = 0;
15686 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15687 for (i = 0; i < row->used[area]; ++i)
15688 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15689 + row->glyphs[area][i].u.val
15690 + row->glyphs[area][i].face_id
15691 + row->glyphs[area][i].padding_p
15692 + (row->glyphs[area][i].type << 2));
15694 it->max_ascent = it->max_descent = 0;
15695 it->max_phys_ascent = it->max_phys_descent = 0;
15699 /* Append one space to the glyph row of iterator IT if doing a
15700 window-based redisplay. The space has the same face as
15701 IT->face_id. Value is non-zero if a space was added.
15703 This function is called to make sure that there is always one glyph
15704 at the end of a glyph row that the cursor can be set on under
15705 window-systems. (If there weren't such a glyph we would not know
15706 how wide and tall a box cursor should be displayed).
15708 At the same time this space let's a nicely handle clearing to the
15709 end of the line if the row ends in italic text. */
15711 static int
15712 append_space_for_newline (it, default_face_p)
15713 struct it *it;
15714 int default_face_p;
15716 if (FRAME_WINDOW_P (it->f))
15718 int n = it->glyph_row->used[TEXT_AREA];
15720 if (it->glyph_row->glyphs[TEXT_AREA] + n
15721 < it->glyph_row->glyphs[1 + TEXT_AREA])
15723 /* Save some values that must not be changed.
15724 Must save IT->c and IT->len because otherwise
15725 ITERATOR_AT_END_P wouldn't work anymore after
15726 append_space_for_newline has been called. */
15727 enum display_element_type saved_what = it->what;
15728 int saved_c = it->c, saved_len = it->len;
15729 int saved_x = it->current_x;
15730 int saved_face_id = it->face_id;
15731 struct text_pos saved_pos;
15732 Lisp_Object saved_object;
15733 struct face *face;
15735 saved_object = it->object;
15736 saved_pos = it->position;
15738 it->what = IT_CHARACTER;
15739 bzero (&it->position, sizeof it->position);
15740 it->object = make_number (0);
15741 it->c = ' ';
15742 it->len = 1;
15744 if (default_face_p)
15745 it->face_id = DEFAULT_FACE_ID;
15746 else if (it->face_before_selective_p)
15747 it->face_id = it->saved_face_id;
15748 face = FACE_FROM_ID (it->f, it->face_id);
15749 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15751 PRODUCE_GLYPHS (it);
15753 it->override_ascent = -1;
15754 it->constrain_row_ascent_descent_p = 0;
15755 it->current_x = saved_x;
15756 it->object = saved_object;
15757 it->position = saved_pos;
15758 it->what = saved_what;
15759 it->face_id = saved_face_id;
15760 it->len = saved_len;
15761 it->c = saved_c;
15762 return 1;
15766 return 0;
15770 /* Extend the face of the last glyph in the text area of IT->glyph_row
15771 to the end of the display line. Called from display_line.
15772 If the glyph row is empty, add a space glyph to it so that we
15773 know the face to draw. Set the glyph row flag fill_line_p. */
15775 static void
15776 extend_face_to_end_of_line (it)
15777 struct it *it;
15779 struct face *face;
15780 struct frame *f = it->f;
15782 /* If line is already filled, do nothing. */
15783 if (it->current_x >= it->last_visible_x)
15784 return;
15786 /* Face extension extends the background and box of IT->face_id
15787 to the end of the line. If the background equals the background
15788 of the frame, we don't have to do anything. */
15789 if (it->face_before_selective_p)
15790 face = FACE_FROM_ID (it->f, it->saved_face_id);
15791 else
15792 face = FACE_FROM_ID (f, it->face_id);
15794 if (FRAME_WINDOW_P (f)
15795 && it->glyph_row->displays_text_p
15796 && face->box == FACE_NO_BOX
15797 && face->background == FRAME_BACKGROUND_PIXEL (f)
15798 && !face->stipple)
15799 return;
15801 /* Set the glyph row flag indicating that the face of the last glyph
15802 in the text area has to be drawn to the end of the text area. */
15803 it->glyph_row->fill_line_p = 1;
15805 /* If current character of IT is not ASCII, make sure we have the
15806 ASCII face. This will be automatically undone the next time
15807 get_next_display_element returns a multibyte character. Note
15808 that the character will always be single byte in unibyte text. */
15809 if (!SINGLE_BYTE_CHAR_P (it->c))
15811 it->face_id = FACE_FOR_CHAR (f, face, 0);
15814 if (FRAME_WINDOW_P (f))
15816 /* If the row is empty, add a space with the current face of IT,
15817 so that we know which face to draw. */
15818 if (it->glyph_row->used[TEXT_AREA] == 0)
15820 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15821 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15822 it->glyph_row->used[TEXT_AREA] = 1;
15825 else
15827 /* Save some values that must not be changed. */
15828 int saved_x = it->current_x;
15829 struct text_pos saved_pos;
15830 Lisp_Object saved_object;
15831 enum display_element_type saved_what = it->what;
15832 int saved_face_id = it->face_id;
15834 saved_object = it->object;
15835 saved_pos = it->position;
15837 it->what = IT_CHARACTER;
15838 bzero (&it->position, sizeof it->position);
15839 it->object = make_number (0);
15840 it->c = ' ';
15841 it->len = 1;
15842 it->face_id = face->id;
15844 PRODUCE_GLYPHS (it);
15846 while (it->current_x <= it->last_visible_x)
15847 PRODUCE_GLYPHS (it);
15849 /* Don't count these blanks really. It would let us insert a left
15850 truncation glyph below and make us set the cursor on them, maybe. */
15851 it->current_x = saved_x;
15852 it->object = saved_object;
15853 it->position = saved_pos;
15854 it->what = saved_what;
15855 it->face_id = saved_face_id;
15860 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15861 trailing whitespace. */
15863 static int
15864 trailing_whitespace_p (charpos)
15865 int charpos;
15867 int bytepos = CHAR_TO_BYTE (charpos);
15868 int c = 0;
15870 while (bytepos < ZV_BYTE
15871 && (c = FETCH_CHAR (bytepos),
15872 c == ' ' || c == '\t'))
15873 ++bytepos;
15875 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15877 if (bytepos != PT_BYTE)
15878 return 1;
15880 return 0;
15884 /* Highlight trailing whitespace, if any, in ROW. */
15886 void
15887 highlight_trailing_whitespace (f, row)
15888 struct frame *f;
15889 struct glyph_row *row;
15891 int used = row->used[TEXT_AREA];
15893 if (used)
15895 struct glyph *start = row->glyphs[TEXT_AREA];
15896 struct glyph *glyph = start + used - 1;
15898 /* Skip over glyphs inserted to display the cursor at the
15899 end of a line, for extending the face of the last glyph
15900 to the end of the line on terminals, and for truncation
15901 and continuation glyphs. */
15902 while (glyph >= start
15903 && glyph->type == CHAR_GLYPH
15904 && INTEGERP (glyph->object))
15905 --glyph;
15907 /* If last glyph is a space or stretch, and it's trailing
15908 whitespace, set the face of all trailing whitespace glyphs in
15909 IT->glyph_row to `trailing-whitespace'. */
15910 if (glyph >= start
15911 && BUFFERP (glyph->object)
15912 && (glyph->type == STRETCH_GLYPH
15913 || (glyph->type == CHAR_GLYPH
15914 && glyph->u.ch == ' '))
15915 && trailing_whitespace_p (glyph->charpos))
15917 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15918 if (face_id < 0)
15919 return;
15921 while (glyph >= start
15922 && BUFFERP (glyph->object)
15923 && (glyph->type == STRETCH_GLYPH
15924 || (glyph->type == CHAR_GLYPH
15925 && glyph->u.ch == ' ')))
15926 (glyph--)->face_id = face_id;
15932 /* Value is non-zero if glyph row ROW in window W should be
15933 used to hold the cursor. */
15935 static int
15936 cursor_row_p (w, row)
15937 struct window *w;
15938 struct glyph_row *row;
15940 int cursor_row_p = 1;
15942 if (PT == MATRIX_ROW_END_CHARPOS (row))
15944 /* Suppose the row ends on a string.
15945 Unless the row is continued, that means it ends on a newline
15946 in the string. If it's anything other than a display string
15947 (e.g. a before-string from an overlay), we don't want the
15948 cursor there. (This heuristic seems to give the optimal
15949 behavior for the various types of multi-line strings.) */
15950 if (CHARPOS (row->end.string_pos) >= 0)
15952 if (row->continued_p)
15953 cursor_row_p = 1;
15954 else
15956 /* Check for `display' property. */
15957 struct glyph *beg = row->glyphs[TEXT_AREA];
15958 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15959 struct glyph *glyph;
15961 cursor_row_p = 0;
15962 for (glyph = end; glyph >= beg; --glyph)
15963 if (STRINGP (glyph->object))
15965 Lisp_Object prop
15966 = Fget_char_property (make_number (PT),
15967 Qdisplay, Qnil);
15968 cursor_row_p =
15969 (!NILP (prop)
15970 && display_prop_string_p (prop, glyph->object));
15971 break;
15975 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15977 /* If the row ends in middle of a real character,
15978 and the line is continued, we want the cursor here.
15979 That's because MATRIX_ROW_END_CHARPOS would equal
15980 PT if PT is before the character. */
15981 if (!row->ends_in_ellipsis_p)
15982 cursor_row_p = row->continued_p;
15983 else
15984 /* If the row ends in an ellipsis, then
15985 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15986 We want that position to be displayed after the ellipsis. */
15987 cursor_row_p = 0;
15989 /* If the row ends at ZV, display the cursor at the end of that
15990 row instead of at the start of the row below. */
15991 else if (row->ends_at_zv_p)
15992 cursor_row_p = 1;
15993 else
15994 cursor_row_p = 0;
15997 return cursor_row_p;
16001 /* Construct the glyph row IT->glyph_row in the desired matrix of
16002 IT->w from text at the current position of IT. See dispextern.h
16003 for an overview of struct it. Value is non-zero if
16004 IT->glyph_row displays text, as opposed to a line displaying ZV
16005 only. */
16007 static int
16008 display_line (it)
16009 struct it *it;
16011 struct glyph_row *row = it->glyph_row;
16012 Lisp_Object overlay_arrow_string;
16014 /* We always start displaying at hpos zero even if hscrolled. */
16015 xassert (it->hpos == 0 && it->current_x == 0);
16017 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16018 >= it->w->desired_matrix->nrows)
16020 it->w->nrows_scale_factor++;
16021 fonts_changed_p = 1;
16022 return 0;
16025 /* Is IT->w showing the region? */
16026 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16028 /* Clear the result glyph row and enable it. */
16029 prepare_desired_row (row);
16031 row->y = it->current_y;
16032 row->start = it->start;
16033 row->continuation_lines_width = it->continuation_lines_width;
16034 row->displays_text_p = 1;
16035 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16036 it->starts_in_middle_of_char_p = 0;
16038 /* Arrange the overlays nicely for our purposes. Usually, we call
16039 display_line on only one line at a time, in which case this
16040 can't really hurt too much, or we call it on lines which appear
16041 one after another in the buffer, in which case all calls to
16042 recenter_overlay_lists but the first will be pretty cheap. */
16043 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16045 /* Move over display elements that are not visible because we are
16046 hscrolled. This may stop at an x-position < IT->first_visible_x
16047 if the first glyph is partially visible or if we hit a line end. */
16048 if (it->current_x < it->first_visible_x)
16050 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16051 MOVE_TO_POS | MOVE_TO_X);
16054 /* Get the initial row height. This is either the height of the
16055 text hscrolled, if there is any, or zero. */
16056 row->ascent = it->max_ascent;
16057 row->height = it->max_ascent + it->max_descent;
16058 row->phys_ascent = it->max_phys_ascent;
16059 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16060 row->extra_line_spacing = it->max_extra_line_spacing;
16062 /* Loop generating characters. The loop is left with IT on the next
16063 character to display. */
16064 while (1)
16066 int n_glyphs_before, hpos_before, x_before;
16067 int x, i, nglyphs;
16068 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16070 /* Retrieve the next thing to display. Value is zero if end of
16071 buffer reached. */
16072 if (!get_next_display_element (it))
16074 /* Maybe add a space at the end of this line that is used to
16075 display the cursor there under X. Set the charpos of the
16076 first glyph of blank lines not corresponding to any text
16077 to -1. */
16078 #ifdef HAVE_WINDOW_SYSTEM
16079 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16080 row->exact_window_width_line_p = 1;
16081 else
16082 #endif /* HAVE_WINDOW_SYSTEM */
16083 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16084 || row->used[TEXT_AREA] == 0)
16086 row->glyphs[TEXT_AREA]->charpos = -1;
16087 row->displays_text_p = 0;
16089 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16090 && (!MINI_WINDOW_P (it->w)
16091 || (minibuf_level && EQ (it->window, minibuf_window))))
16092 row->indicate_empty_line_p = 1;
16095 it->continuation_lines_width = 0;
16096 row->ends_at_zv_p = 1;
16097 break;
16100 /* Now, get the metrics of what we want to display. This also
16101 generates glyphs in `row' (which is IT->glyph_row). */
16102 n_glyphs_before = row->used[TEXT_AREA];
16103 x = it->current_x;
16105 /* Remember the line height so far in case the next element doesn't
16106 fit on the line. */
16107 if (!it->truncate_lines_p)
16109 ascent = it->max_ascent;
16110 descent = it->max_descent;
16111 phys_ascent = it->max_phys_ascent;
16112 phys_descent = it->max_phys_descent;
16115 PRODUCE_GLYPHS (it);
16117 /* If this display element was in marginal areas, continue with
16118 the next one. */
16119 if (it->area != TEXT_AREA)
16121 row->ascent = max (row->ascent, it->max_ascent);
16122 row->height = max (row->height, it->max_ascent + it->max_descent);
16123 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16124 row->phys_height = max (row->phys_height,
16125 it->max_phys_ascent + it->max_phys_descent);
16126 row->extra_line_spacing = max (row->extra_line_spacing,
16127 it->max_extra_line_spacing);
16128 set_iterator_to_next (it, 1);
16129 continue;
16132 /* Does the display element fit on the line? If we truncate
16133 lines, we should draw past the right edge of the window. If
16134 we don't truncate, we want to stop so that we can display the
16135 continuation glyph before the right margin. If lines are
16136 continued, there are two possible strategies for characters
16137 resulting in more than 1 glyph (e.g. tabs): Display as many
16138 glyphs as possible in this line and leave the rest for the
16139 continuation line, or display the whole element in the next
16140 line. Original redisplay did the former, so we do it also. */
16141 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16142 hpos_before = it->hpos;
16143 x_before = x;
16145 if (/* Not a newline. */
16146 nglyphs > 0
16147 /* Glyphs produced fit entirely in the line. */
16148 && it->current_x < it->last_visible_x)
16150 it->hpos += nglyphs;
16151 row->ascent = max (row->ascent, it->max_ascent);
16152 row->height = max (row->height, it->max_ascent + it->max_descent);
16153 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16154 row->phys_height = max (row->phys_height,
16155 it->max_phys_ascent + it->max_phys_descent);
16156 row->extra_line_spacing = max (row->extra_line_spacing,
16157 it->max_extra_line_spacing);
16158 if (it->current_x - it->pixel_width < it->first_visible_x)
16159 row->x = x - it->first_visible_x;
16161 else
16163 int new_x;
16164 struct glyph *glyph;
16166 for (i = 0; i < nglyphs; ++i, x = new_x)
16168 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16169 new_x = x + glyph->pixel_width;
16171 if (/* Lines are continued. */
16172 !it->truncate_lines_p
16173 && (/* Glyph doesn't fit on the line. */
16174 new_x > it->last_visible_x
16175 /* Or it fits exactly on a window system frame. */
16176 || (new_x == it->last_visible_x
16177 && FRAME_WINDOW_P (it->f))))
16179 /* End of a continued line. */
16181 if (it->hpos == 0
16182 || (new_x == it->last_visible_x
16183 && FRAME_WINDOW_P (it->f)))
16185 /* Current glyph is the only one on the line or
16186 fits exactly on the line. We must continue
16187 the line because we can't draw the cursor
16188 after the glyph. */
16189 row->continued_p = 1;
16190 it->current_x = new_x;
16191 it->continuation_lines_width += new_x;
16192 ++it->hpos;
16193 if (i == nglyphs - 1)
16195 set_iterator_to_next (it, 1);
16196 #ifdef HAVE_WINDOW_SYSTEM
16197 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16199 if (!get_next_display_element (it))
16201 row->exact_window_width_line_p = 1;
16202 it->continuation_lines_width = 0;
16203 row->continued_p = 0;
16204 row->ends_at_zv_p = 1;
16206 else if (ITERATOR_AT_END_OF_LINE_P (it))
16208 row->continued_p = 0;
16209 row->exact_window_width_line_p = 1;
16212 #endif /* HAVE_WINDOW_SYSTEM */
16215 else if (CHAR_GLYPH_PADDING_P (*glyph)
16216 && !FRAME_WINDOW_P (it->f))
16218 /* A padding glyph that doesn't fit on this line.
16219 This means the whole character doesn't fit
16220 on the line. */
16221 row->used[TEXT_AREA] = n_glyphs_before;
16223 /* Fill the rest of the row with continuation
16224 glyphs like in 20.x. */
16225 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16226 < row->glyphs[1 + TEXT_AREA])
16227 produce_special_glyphs (it, IT_CONTINUATION);
16229 row->continued_p = 1;
16230 it->current_x = x_before;
16231 it->continuation_lines_width += x_before;
16233 /* Restore the height to what it was before the
16234 element not fitting on the line. */
16235 it->max_ascent = ascent;
16236 it->max_descent = descent;
16237 it->max_phys_ascent = phys_ascent;
16238 it->max_phys_descent = phys_descent;
16240 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16242 /* A TAB that extends past the right edge of the
16243 window. This produces a single glyph on
16244 window system frames. We leave the glyph in
16245 this row and let it fill the row, but don't
16246 consume the TAB. */
16247 it->continuation_lines_width += it->last_visible_x;
16248 row->ends_in_middle_of_char_p = 1;
16249 row->continued_p = 1;
16250 glyph->pixel_width = it->last_visible_x - x;
16251 it->starts_in_middle_of_char_p = 1;
16253 else
16255 /* Something other than a TAB that draws past
16256 the right edge of the window. Restore
16257 positions to values before the element. */
16258 row->used[TEXT_AREA] = n_glyphs_before + i;
16260 /* Display continuation glyphs. */
16261 if (!FRAME_WINDOW_P (it->f))
16262 produce_special_glyphs (it, IT_CONTINUATION);
16263 row->continued_p = 1;
16265 it->current_x = x_before;
16266 it->continuation_lines_width += x;
16267 extend_face_to_end_of_line (it);
16269 if (nglyphs > 1 && i > 0)
16271 row->ends_in_middle_of_char_p = 1;
16272 it->starts_in_middle_of_char_p = 1;
16275 /* Restore the height to what it was before the
16276 element not fitting on the line. */
16277 it->max_ascent = ascent;
16278 it->max_descent = descent;
16279 it->max_phys_ascent = phys_ascent;
16280 it->max_phys_descent = phys_descent;
16283 break;
16285 else if (new_x > it->first_visible_x)
16287 /* Increment number of glyphs actually displayed. */
16288 ++it->hpos;
16290 if (x < it->first_visible_x)
16291 /* Glyph is partially visible, i.e. row starts at
16292 negative X position. */
16293 row->x = x - it->first_visible_x;
16295 else
16297 /* Glyph is completely off the left margin of the
16298 window. This should not happen because of the
16299 move_it_in_display_line at the start of this
16300 function, unless the text display area of the
16301 window is empty. */
16302 xassert (it->first_visible_x <= it->last_visible_x);
16306 row->ascent = max (row->ascent, it->max_ascent);
16307 row->height = max (row->height, it->max_ascent + it->max_descent);
16308 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16309 row->phys_height = max (row->phys_height,
16310 it->max_phys_ascent + it->max_phys_descent);
16311 row->extra_line_spacing = max (row->extra_line_spacing,
16312 it->max_extra_line_spacing);
16314 /* End of this display line if row is continued. */
16315 if (row->continued_p || row->ends_at_zv_p)
16316 break;
16319 at_end_of_line:
16320 /* Is this a line end? If yes, we're also done, after making
16321 sure that a non-default face is extended up to the right
16322 margin of the window. */
16323 if (ITERATOR_AT_END_OF_LINE_P (it))
16325 int used_before = row->used[TEXT_AREA];
16327 row->ends_in_newline_from_string_p = STRINGP (it->object);
16329 #ifdef HAVE_WINDOW_SYSTEM
16330 /* Add a space at the end of the line that is used to
16331 display the cursor there. */
16332 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16333 append_space_for_newline (it, 0);
16334 #endif /* HAVE_WINDOW_SYSTEM */
16336 /* Extend the face to the end of the line. */
16337 extend_face_to_end_of_line (it);
16339 /* Make sure we have the position. */
16340 if (used_before == 0)
16341 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16343 /* Consume the line end. This skips over invisible lines. */
16344 set_iterator_to_next (it, 1);
16345 it->continuation_lines_width = 0;
16346 break;
16349 /* Proceed with next display element. Note that this skips
16350 over lines invisible because of selective display. */
16351 set_iterator_to_next (it, 1);
16353 /* If we truncate lines, we are done when the last displayed
16354 glyphs reach past the right margin of the window. */
16355 if (it->truncate_lines_p
16356 && (FRAME_WINDOW_P (it->f)
16357 ? (it->current_x >= it->last_visible_x)
16358 : (it->current_x > it->last_visible_x)))
16360 /* Maybe add truncation glyphs. */
16361 if (!FRAME_WINDOW_P (it->f))
16363 int i, n;
16365 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16366 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16367 break;
16369 for (n = row->used[TEXT_AREA]; i < n; ++i)
16371 row->used[TEXT_AREA] = i;
16372 produce_special_glyphs (it, IT_TRUNCATION);
16375 #ifdef HAVE_WINDOW_SYSTEM
16376 else
16378 /* Don't truncate if we can overflow newline into fringe. */
16379 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16381 if (!get_next_display_element (it))
16383 it->continuation_lines_width = 0;
16384 row->ends_at_zv_p = 1;
16385 row->exact_window_width_line_p = 1;
16386 break;
16388 if (ITERATOR_AT_END_OF_LINE_P (it))
16390 row->exact_window_width_line_p = 1;
16391 goto at_end_of_line;
16395 #endif /* HAVE_WINDOW_SYSTEM */
16397 row->truncated_on_right_p = 1;
16398 it->continuation_lines_width = 0;
16399 reseat_at_next_visible_line_start (it, 0);
16400 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16401 it->hpos = hpos_before;
16402 it->current_x = x_before;
16403 break;
16407 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16408 at the left window margin. */
16409 if (it->first_visible_x
16410 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16412 if (!FRAME_WINDOW_P (it->f))
16413 insert_left_trunc_glyphs (it);
16414 row->truncated_on_left_p = 1;
16417 /* If the start of this line is the overlay arrow-position, then
16418 mark this glyph row as the one containing the overlay arrow.
16419 This is clearly a mess with variable size fonts. It would be
16420 better to let it be displayed like cursors under X. */
16421 if ((row->displays_text_p || !overlay_arrow_seen)
16422 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16423 !NILP (overlay_arrow_string)))
16425 /* Overlay arrow in window redisplay is a fringe bitmap. */
16426 if (STRINGP (overlay_arrow_string))
16428 struct glyph_row *arrow_row
16429 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16430 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16431 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16432 struct glyph *p = row->glyphs[TEXT_AREA];
16433 struct glyph *p2, *end;
16435 /* Copy the arrow glyphs. */
16436 while (glyph < arrow_end)
16437 *p++ = *glyph++;
16439 /* Throw away padding glyphs. */
16440 p2 = p;
16441 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16442 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16443 ++p2;
16444 if (p2 > p)
16446 while (p2 < end)
16447 *p++ = *p2++;
16448 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16451 else
16453 xassert (INTEGERP (overlay_arrow_string));
16454 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16456 overlay_arrow_seen = 1;
16459 /* Compute pixel dimensions of this line. */
16460 compute_line_metrics (it);
16462 /* Remember the position at which this line ends. */
16463 row->end = it->current;
16465 /* Record whether this row ends inside an ellipsis. */
16466 row->ends_in_ellipsis_p
16467 = (it->method == GET_FROM_DISPLAY_VECTOR
16468 && it->ellipsis_p);
16470 /* Save fringe bitmaps in this row. */
16471 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16472 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16473 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16474 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16476 it->left_user_fringe_bitmap = 0;
16477 it->left_user_fringe_face_id = 0;
16478 it->right_user_fringe_bitmap = 0;
16479 it->right_user_fringe_face_id = 0;
16481 /* Maybe set the cursor. */
16482 if (it->w->cursor.vpos < 0
16483 && PT >= MATRIX_ROW_START_CHARPOS (row)
16484 && PT <= MATRIX_ROW_END_CHARPOS (row)
16485 && cursor_row_p (it->w, row))
16486 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16488 /* Highlight trailing whitespace. */
16489 if (!NILP (Vshow_trailing_whitespace))
16490 highlight_trailing_whitespace (it->f, it->glyph_row);
16492 /* Prepare for the next line. This line starts horizontally at (X
16493 HPOS) = (0 0). Vertical positions are incremented. As a
16494 convenience for the caller, IT->glyph_row is set to the next
16495 row to be used. */
16496 it->current_x = it->hpos = 0;
16497 it->current_y += row->height;
16498 ++it->vpos;
16499 ++it->glyph_row;
16500 it->start = it->current;
16501 return row->displays_text_p;
16506 /***********************************************************************
16507 Menu Bar
16508 ***********************************************************************/
16510 /* Redisplay the menu bar in the frame for window W.
16512 The menu bar of X frames that don't have X toolkit support is
16513 displayed in a special window W->frame->menu_bar_window.
16515 The menu bar of terminal frames is treated specially as far as
16516 glyph matrices are concerned. Menu bar lines are not part of
16517 windows, so the update is done directly on the frame matrix rows
16518 for the menu bar. */
16520 static void
16521 display_menu_bar (w)
16522 struct window *w;
16524 struct frame *f = XFRAME (WINDOW_FRAME (w));
16525 struct it it;
16526 Lisp_Object items;
16527 int i;
16529 /* Don't do all this for graphical frames. */
16530 #ifdef HAVE_NTGUI
16531 if (!NILP (Vwindow_system))
16532 return;
16533 #endif
16534 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16535 if (FRAME_X_P (f))
16536 return;
16537 #endif
16538 #ifdef MAC_OS
16539 if (FRAME_MAC_P (f))
16540 return;
16541 #endif
16543 #ifdef USE_X_TOOLKIT
16544 xassert (!FRAME_WINDOW_P (f));
16545 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16546 it.first_visible_x = 0;
16547 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16548 #else /* not USE_X_TOOLKIT */
16549 if (FRAME_WINDOW_P (f))
16551 /* Menu bar lines are displayed in the desired matrix of the
16552 dummy window menu_bar_window. */
16553 struct window *menu_w;
16554 xassert (WINDOWP (f->menu_bar_window));
16555 menu_w = XWINDOW (f->menu_bar_window);
16556 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16557 MENU_FACE_ID);
16558 it.first_visible_x = 0;
16559 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16561 else
16563 /* This is a TTY frame, i.e. character hpos/vpos are used as
16564 pixel x/y. */
16565 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16566 MENU_FACE_ID);
16567 it.first_visible_x = 0;
16568 it.last_visible_x = FRAME_COLS (f);
16570 #endif /* not USE_X_TOOLKIT */
16572 if (! mode_line_inverse_video)
16573 /* Force the menu-bar to be displayed in the default face. */
16574 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16576 /* Clear all rows of the menu bar. */
16577 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16579 struct glyph_row *row = it.glyph_row + i;
16580 clear_glyph_row (row);
16581 row->enabled_p = 1;
16582 row->full_width_p = 1;
16585 /* Display all items of the menu bar. */
16586 items = FRAME_MENU_BAR_ITEMS (it.f);
16587 for (i = 0; i < XVECTOR (items)->size; i += 4)
16589 Lisp_Object string;
16591 /* Stop at nil string. */
16592 string = AREF (items, i + 1);
16593 if (NILP (string))
16594 break;
16596 /* Remember where item was displayed. */
16597 AREF (items, i + 3) = make_number (it.hpos);
16599 /* Display the item, pad with one space. */
16600 if (it.current_x < it.last_visible_x)
16601 display_string (NULL, string, Qnil, 0, 0, &it,
16602 SCHARS (string) + 1, 0, 0, -1);
16605 /* Fill out the line with spaces. */
16606 if (it.current_x < it.last_visible_x)
16607 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16609 /* Compute the total height of the lines. */
16610 compute_line_metrics (&it);
16615 /***********************************************************************
16616 Mode Line
16617 ***********************************************************************/
16619 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16620 FORCE is non-zero, redisplay mode lines unconditionally.
16621 Otherwise, redisplay only mode lines that are garbaged. Value is
16622 the number of windows whose mode lines were redisplayed. */
16624 static int
16625 redisplay_mode_lines (window, force)
16626 Lisp_Object window;
16627 int force;
16629 int nwindows = 0;
16631 while (!NILP (window))
16633 struct window *w = XWINDOW (window);
16635 if (WINDOWP (w->hchild))
16636 nwindows += redisplay_mode_lines (w->hchild, force);
16637 else if (WINDOWP (w->vchild))
16638 nwindows += redisplay_mode_lines (w->vchild, force);
16639 else if (force
16640 || FRAME_GARBAGED_P (XFRAME (w->frame))
16641 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16643 struct text_pos lpoint;
16644 struct buffer *old = current_buffer;
16646 /* Set the window's buffer for the mode line display. */
16647 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16648 set_buffer_internal_1 (XBUFFER (w->buffer));
16650 /* Point refers normally to the selected window. For any
16651 other window, set up appropriate value. */
16652 if (!EQ (window, selected_window))
16654 struct text_pos pt;
16656 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16657 if (CHARPOS (pt) < BEGV)
16658 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16659 else if (CHARPOS (pt) > (ZV - 1))
16660 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16661 else
16662 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16665 /* Display mode lines. */
16666 clear_glyph_matrix (w->desired_matrix);
16667 if (display_mode_lines (w))
16669 ++nwindows;
16670 w->must_be_updated_p = 1;
16673 /* Restore old settings. */
16674 set_buffer_internal_1 (old);
16675 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16678 window = w->next;
16681 return nwindows;
16685 /* Display the mode and/or top line of window W. Value is the number
16686 of mode lines displayed. */
16688 static int
16689 display_mode_lines (w)
16690 struct window *w;
16692 Lisp_Object old_selected_window, old_selected_frame;
16693 int n = 0;
16695 old_selected_frame = selected_frame;
16696 selected_frame = w->frame;
16697 old_selected_window = selected_window;
16698 XSETWINDOW (selected_window, w);
16700 /* These will be set while the mode line specs are processed. */
16701 line_number_displayed = 0;
16702 w->column_number_displayed = Qnil;
16704 if (WINDOW_WANTS_MODELINE_P (w))
16706 struct window *sel_w = XWINDOW (old_selected_window);
16708 /* Select mode line face based on the real selected window. */
16709 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16710 current_buffer->mode_line_format);
16711 ++n;
16714 if (WINDOW_WANTS_HEADER_LINE_P (w))
16716 display_mode_line (w, HEADER_LINE_FACE_ID,
16717 current_buffer->header_line_format);
16718 ++n;
16721 selected_frame = old_selected_frame;
16722 selected_window = old_selected_window;
16723 return n;
16727 /* Display mode or top line of window W. FACE_ID specifies which line
16728 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16729 FORMAT is the mode line format to display. Value is the pixel
16730 height of the mode line displayed. */
16732 static int
16733 display_mode_line (w, face_id, format)
16734 struct window *w;
16735 enum face_id face_id;
16736 Lisp_Object format;
16738 struct it it;
16739 struct face *face;
16740 int count = SPECPDL_INDEX ();
16742 init_iterator (&it, w, -1, -1, NULL, face_id);
16743 /* Don't extend on a previously drawn mode-line.
16744 This may happen if called from pos_visible_p. */
16745 it.glyph_row->enabled_p = 0;
16746 prepare_desired_row (it.glyph_row);
16748 it.glyph_row->mode_line_p = 1;
16750 if (! mode_line_inverse_video)
16751 /* Force the mode-line to be displayed in the default face. */
16752 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16754 record_unwind_protect (unwind_format_mode_line,
16755 format_mode_line_unwind_data (NULL, 0));
16757 mode_line_target = MODE_LINE_DISPLAY;
16759 /* Temporarily make frame's keyboard the current kboard so that
16760 kboard-local variables in the mode_line_format will get the right
16761 values. */
16762 push_frame_kboard (it.f);
16763 record_unwind_save_match_data ();
16764 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16765 pop_frame_kboard ();
16767 unbind_to (count, Qnil);
16769 /* Fill up with spaces. */
16770 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16772 compute_line_metrics (&it);
16773 it.glyph_row->full_width_p = 1;
16774 it.glyph_row->continued_p = 0;
16775 it.glyph_row->truncated_on_left_p = 0;
16776 it.glyph_row->truncated_on_right_p = 0;
16778 /* Make a 3D mode-line have a shadow at its right end. */
16779 face = FACE_FROM_ID (it.f, face_id);
16780 extend_face_to_end_of_line (&it);
16781 if (face->box != FACE_NO_BOX)
16783 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16784 + it.glyph_row->used[TEXT_AREA] - 1);
16785 last->right_box_line_p = 1;
16788 return it.glyph_row->height;
16791 /* Move element ELT in LIST to the front of LIST.
16792 Return the updated list. */
16794 static Lisp_Object
16795 move_elt_to_front (elt, list)
16796 Lisp_Object elt, list;
16798 register Lisp_Object tail, prev;
16799 register Lisp_Object tem;
16801 tail = list;
16802 prev = Qnil;
16803 while (CONSP (tail))
16805 tem = XCAR (tail);
16807 if (EQ (elt, tem))
16809 /* Splice out the link TAIL. */
16810 if (NILP (prev))
16811 list = XCDR (tail);
16812 else
16813 Fsetcdr (prev, XCDR (tail));
16815 /* Now make it the first. */
16816 Fsetcdr (tail, list);
16817 return tail;
16819 else
16820 prev = tail;
16821 tail = XCDR (tail);
16822 QUIT;
16825 /* Not found--return unchanged LIST. */
16826 return list;
16829 /* Contribute ELT to the mode line for window IT->w. How it
16830 translates into text depends on its data type.
16832 IT describes the display environment in which we display, as usual.
16834 DEPTH is the depth in recursion. It is used to prevent
16835 infinite recursion here.
16837 FIELD_WIDTH is the number of characters the display of ELT should
16838 occupy in the mode line, and PRECISION is the maximum number of
16839 characters to display from ELT's representation. See
16840 display_string for details.
16842 Returns the hpos of the end of the text generated by ELT.
16844 PROPS is a property list to add to any string we encounter.
16846 If RISKY is nonzero, remove (disregard) any properties in any string
16847 we encounter, and ignore :eval and :propertize.
16849 The global variable `mode_line_target' determines whether the
16850 output is passed to `store_mode_line_noprop',
16851 `store_mode_line_string', or `display_string'. */
16853 static int
16854 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16855 struct it *it;
16856 int depth;
16857 int field_width, precision;
16858 Lisp_Object elt, props;
16859 int risky;
16861 int n = 0, field, prec;
16862 int literal = 0;
16864 tail_recurse:
16865 if (depth > 100)
16866 elt = build_string ("*too-deep*");
16868 depth++;
16870 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16872 case Lisp_String:
16874 /* A string: output it and check for %-constructs within it. */
16875 unsigned char c;
16876 int offset = 0;
16878 if (SCHARS (elt) > 0
16879 && (!NILP (props) || risky))
16881 Lisp_Object oprops, aelt;
16882 oprops = Ftext_properties_at (make_number (0), elt);
16884 /* If the starting string's properties are not what
16885 we want, translate the string. Also, if the string
16886 is risky, do that anyway. */
16888 if (NILP (Fequal (props, oprops)) || risky)
16890 /* If the starting string has properties,
16891 merge the specified ones onto the existing ones. */
16892 if (! NILP (oprops) && !risky)
16894 Lisp_Object tem;
16896 oprops = Fcopy_sequence (oprops);
16897 tem = props;
16898 while (CONSP (tem))
16900 oprops = Fplist_put (oprops, XCAR (tem),
16901 XCAR (XCDR (tem)));
16902 tem = XCDR (XCDR (tem));
16904 props = oprops;
16907 aelt = Fassoc (elt, mode_line_proptrans_alist);
16908 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16910 /* AELT is what we want. Move it to the front
16911 without consing. */
16912 elt = XCAR (aelt);
16913 mode_line_proptrans_alist
16914 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16916 else
16918 Lisp_Object tem;
16920 /* If AELT has the wrong props, it is useless.
16921 so get rid of it. */
16922 if (! NILP (aelt))
16923 mode_line_proptrans_alist
16924 = Fdelq (aelt, mode_line_proptrans_alist);
16926 elt = Fcopy_sequence (elt);
16927 Fset_text_properties (make_number (0), Flength (elt),
16928 props, elt);
16929 /* Add this item to mode_line_proptrans_alist. */
16930 mode_line_proptrans_alist
16931 = Fcons (Fcons (elt, props),
16932 mode_line_proptrans_alist);
16933 /* Truncate mode_line_proptrans_alist
16934 to at most 50 elements. */
16935 tem = Fnthcdr (make_number (50),
16936 mode_line_proptrans_alist);
16937 if (! NILP (tem))
16938 XSETCDR (tem, Qnil);
16943 offset = 0;
16945 if (literal)
16947 prec = precision - n;
16948 switch (mode_line_target)
16950 case MODE_LINE_NOPROP:
16951 case MODE_LINE_TITLE:
16952 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16953 break;
16954 case MODE_LINE_STRING:
16955 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16956 break;
16957 case MODE_LINE_DISPLAY:
16958 n += display_string (NULL, elt, Qnil, 0, 0, it,
16959 0, prec, 0, STRING_MULTIBYTE (elt));
16960 break;
16963 break;
16966 /* Handle the non-literal case. */
16968 while ((precision <= 0 || n < precision)
16969 && SREF (elt, offset) != 0
16970 && (mode_line_target != MODE_LINE_DISPLAY
16971 || it->current_x < it->last_visible_x))
16973 int last_offset = offset;
16975 /* Advance to end of string or next format specifier. */
16976 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16979 if (offset - 1 != last_offset)
16981 int nchars, nbytes;
16983 /* Output to end of string or up to '%'. Field width
16984 is length of string. Don't output more than
16985 PRECISION allows us. */
16986 offset--;
16988 prec = c_string_width (SDATA (elt) + last_offset,
16989 offset - last_offset, precision - n,
16990 &nchars, &nbytes);
16992 switch (mode_line_target)
16994 case MODE_LINE_NOPROP:
16995 case MODE_LINE_TITLE:
16996 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16997 break;
16998 case MODE_LINE_STRING:
17000 int bytepos = last_offset;
17001 int charpos = string_byte_to_char (elt, bytepos);
17002 int endpos = (precision <= 0
17003 ? string_byte_to_char (elt, offset)
17004 : charpos + nchars);
17006 n += store_mode_line_string (NULL,
17007 Fsubstring (elt, make_number (charpos),
17008 make_number (endpos)),
17009 0, 0, 0, Qnil);
17011 break;
17012 case MODE_LINE_DISPLAY:
17014 int bytepos = last_offset;
17015 int charpos = string_byte_to_char (elt, bytepos);
17017 if (precision <= 0)
17018 nchars = string_byte_to_char (elt, offset) - charpos;
17019 n += display_string (NULL, elt, Qnil, 0, charpos,
17020 it, 0, nchars, 0,
17021 STRING_MULTIBYTE (elt));
17023 break;
17026 else /* c == '%' */
17028 int percent_position = offset;
17030 /* Get the specified minimum width. Zero means
17031 don't pad. */
17032 field = 0;
17033 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17034 field = field * 10 + c - '0';
17036 /* Don't pad beyond the total padding allowed. */
17037 if (field_width - n > 0 && field > field_width - n)
17038 field = field_width - n;
17040 /* Note that either PRECISION <= 0 or N < PRECISION. */
17041 prec = precision - n;
17043 if (c == 'M')
17044 n += display_mode_element (it, depth, field, prec,
17045 Vglobal_mode_string, props,
17046 risky);
17047 else if (c != 0)
17049 int multibyte;
17050 int bytepos, charpos;
17051 unsigned char *spec;
17053 bytepos = percent_position;
17054 charpos = (STRING_MULTIBYTE (elt)
17055 ? string_byte_to_char (elt, bytepos)
17056 : bytepos);
17058 spec
17059 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17061 switch (mode_line_target)
17063 case MODE_LINE_NOPROP:
17064 case MODE_LINE_TITLE:
17065 n += store_mode_line_noprop (spec, field, prec);
17066 break;
17067 case MODE_LINE_STRING:
17069 int len = strlen (spec);
17070 Lisp_Object tem = make_string (spec, len);
17071 props = Ftext_properties_at (make_number (charpos), elt);
17072 /* Should only keep face property in props */
17073 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17075 break;
17076 case MODE_LINE_DISPLAY:
17078 int nglyphs_before, nwritten;
17080 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17081 nwritten = display_string (spec, Qnil, elt,
17082 charpos, 0, it,
17083 field, prec, 0,
17084 multibyte);
17086 /* Assign to the glyphs written above the
17087 string where the `%x' came from, position
17088 of the `%'. */
17089 if (nwritten > 0)
17091 struct glyph *glyph
17092 = (it->glyph_row->glyphs[TEXT_AREA]
17093 + nglyphs_before);
17094 int i;
17096 for (i = 0; i < nwritten; ++i)
17098 glyph[i].object = elt;
17099 glyph[i].charpos = charpos;
17102 n += nwritten;
17105 break;
17108 else /* c == 0 */
17109 break;
17113 break;
17115 case Lisp_Symbol:
17116 /* A symbol: process the value of the symbol recursively
17117 as if it appeared here directly. Avoid error if symbol void.
17118 Special case: if value of symbol is a string, output the string
17119 literally. */
17121 register Lisp_Object tem;
17123 /* If the variable is not marked as risky to set
17124 then its contents are risky to use. */
17125 if (NILP (Fget (elt, Qrisky_local_variable)))
17126 risky = 1;
17128 tem = Fboundp (elt);
17129 if (!NILP (tem))
17131 tem = Fsymbol_value (elt);
17132 /* If value is a string, output that string literally:
17133 don't check for % within it. */
17134 if (STRINGP (tem))
17135 literal = 1;
17137 if (!EQ (tem, elt))
17139 /* Give up right away for nil or t. */
17140 elt = tem;
17141 goto tail_recurse;
17145 break;
17147 case Lisp_Cons:
17149 register Lisp_Object car, tem;
17151 /* A cons cell: five distinct cases.
17152 If first element is :eval or :propertize, do something special.
17153 If first element is a string or a cons, process all the elements
17154 and effectively concatenate them.
17155 If first element is a negative number, truncate displaying cdr to
17156 at most that many characters. If positive, pad (with spaces)
17157 to at least that many characters.
17158 If first element is a symbol, process the cadr or caddr recursively
17159 according to whether the symbol's value is non-nil or nil. */
17160 car = XCAR (elt);
17161 if (EQ (car, QCeval))
17163 /* An element of the form (:eval FORM) means evaluate FORM
17164 and use the result as mode line elements. */
17166 if (risky)
17167 break;
17169 if (CONSP (XCDR (elt)))
17171 Lisp_Object spec;
17172 spec = safe_eval (XCAR (XCDR (elt)));
17173 n += display_mode_element (it, depth, field_width - n,
17174 precision - n, spec, props,
17175 risky);
17178 else if (EQ (car, QCpropertize))
17180 /* An element of the form (:propertize ELT PROPS...)
17181 means display ELT but applying properties PROPS. */
17183 if (risky)
17184 break;
17186 if (CONSP (XCDR (elt)))
17187 n += display_mode_element (it, depth, field_width - n,
17188 precision - n, XCAR (XCDR (elt)),
17189 XCDR (XCDR (elt)), risky);
17191 else if (SYMBOLP (car))
17193 tem = Fboundp (car);
17194 elt = XCDR (elt);
17195 if (!CONSP (elt))
17196 goto invalid;
17197 /* elt is now the cdr, and we know it is a cons cell.
17198 Use its car if CAR has a non-nil value. */
17199 if (!NILP (tem))
17201 tem = Fsymbol_value (car);
17202 if (!NILP (tem))
17204 elt = XCAR (elt);
17205 goto tail_recurse;
17208 /* Symbol's value is nil (or symbol is unbound)
17209 Get the cddr of the original list
17210 and if possible find the caddr and use that. */
17211 elt = XCDR (elt);
17212 if (NILP (elt))
17213 break;
17214 else if (!CONSP (elt))
17215 goto invalid;
17216 elt = XCAR (elt);
17217 goto tail_recurse;
17219 else if (INTEGERP (car))
17221 register int lim = XINT (car);
17222 elt = XCDR (elt);
17223 if (lim < 0)
17225 /* Negative int means reduce maximum width. */
17226 if (precision <= 0)
17227 precision = -lim;
17228 else
17229 precision = min (precision, -lim);
17231 else if (lim > 0)
17233 /* Padding specified. Don't let it be more than
17234 current maximum. */
17235 if (precision > 0)
17236 lim = min (precision, lim);
17238 /* If that's more padding than already wanted, queue it.
17239 But don't reduce padding already specified even if
17240 that is beyond the current truncation point. */
17241 field_width = max (lim, field_width);
17243 goto tail_recurse;
17245 else if (STRINGP (car) || CONSP (car))
17247 register int limit = 50;
17248 /* Limit is to protect against circular lists. */
17249 while (CONSP (elt)
17250 && --limit > 0
17251 && (precision <= 0 || n < precision))
17253 n += display_mode_element (it, depth,
17254 /* Do padding only after the last
17255 element in the list. */
17256 (! CONSP (XCDR (elt))
17257 ? field_width - n
17258 : 0),
17259 precision - n, XCAR (elt),
17260 props, risky);
17261 elt = XCDR (elt);
17265 break;
17267 default:
17268 invalid:
17269 elt = build_string ("*invalid*");
17270 goto tail_recurse;
17273 /* Pad to FIELD_WIDTH. */
17274 if (field_width > 0 && n < field_width)
17276 switch (mode_line_target)
17278 case MODE_LINE_NOPROP:
17279 case MODE_LINE_TITLE:
17280 n += store_mode_line_noprop ("", field_width - n, 0);
17281 break;
17282 case MODE_LINE_STRING:
17283 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17284 break;
17285 case MODE_LINE_DISPLAY:
17286 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17287 0, 0, 0);
17288 break;
17292 return n;
17295 /* Store a mode-line string element in mode_line_string_list.
17297 If STRING is non-null, display that C string. Otherwise, the Lisp
17298 string LISP_STRING is displayed.
17300 FIELD_WIDTH is the minimum number of output glyphs to produce.
17301 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17302 with spaces. FIELD_WIDTH <= 0 means don't pad.
17304 PRECISION is the maximum number of characters to output from
17305 STRING. PRECISION <= 0 means don't truncate the string.
17307 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17308 properties to the string.
17310 PROPS are the properties to add to the string.
17311 The mode_line_string_face face property is always added to the string.
17314 static int
17315 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17316 char *string;
17317 Lisp_Object lisp_string;
17318 int copy_string;
17319 int field_width;
17320 int precision;
17321 Lisp_Object props;
17323 int len;
17324 int n = 0;
17326 if (string != NULL)
17328 len = strlen (string);
17329 if (precision > 0 && len > precision)
17330 len = precision;
17331 lisp_string = make_string (string, len);
17332 if (NILP (props))
17333 props = mode_line_string_face_prop;
17334 else if (!NILP (mode_line_string_face))
17336 Lisp_Object face = Fplist_get (props, Qface);
17337 props = Fcopy_sequence (props);
17338 if (NILP (face))
17339 face = mode_line_string_face;
17340 else
17341 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17342 props = Fplist_put (props, Qface, face);
17344 Fadd_text_properties (make_number (0), make_number (len),
17345 props, lisp_string);
17347 else
17349 len = XFASTINT (Flength (lisp_string));
17350 if (precision > 0 && len > precision)
17352 len = precision;
17353 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17354 precision = -1;
17356 if (!NILP (mode_line_string_face))
17358 Lisp_Object face;
17359 if (NILP (props))
17360 props = Ftext_properties_at (make_number (0), lisp_string);
17361 face = Fplist_get (props, Qface);
17362 if (NILP (face))
17363 face = mode_line_string_face;
17364 else
17365 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17366 props = Fcons (Qface, Fcons (face, Qnil));
17367 if (copy_string)
17368 lisp_string = Fcopy_sequence (lisp_string);
17370 if (!NILP (props))
17371 Fadd_text_properties (make_number (0), make_number (len),
17372 props, lisp_string);
17375 if (len > 0)
17377 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17378 n += len;
17381 if (field_width > len)
17383 field_width -= len;
17384 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17385 if (!NILP (props))
17386 Fadd_text_properties (make_number (0), make_number (field_width),
17387 props, lisp_string);
17388 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17389 n += field_width;
17392 return n;
17396 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17397 1, 4, 0,
17398 doc: /* Format a string out of a mode line format specification.
17399 First arg FORMAT specifies the mode line format (see `mode-line-format'
17400 for details) to use.
17402 Optional second arg FACE specifies the face property to put
17403 on all characters for which no face is specified.
17404 The value t means whatever face the window's mode line currently uses
17405 \(either `mode-line' or `mode-line-inactive', depending).
17406 A value of nil means the default is no face property.
17407 If FACE is an integer, the value string has no text properties.
17409 Optional third and fourth args WINDOW and BUFFER specify the window
17410 and buffer to use as the context for the formatting (defaults
17411 are the selected window and the window's buffer). */)
17412 (format, face, window, buffer)
17413 Lisp_Object format, face, window, buffer;
17415 struct it it;
17416 int len;
17417 struct window *w;
17418 struct buffer *old_buffer = NULL;
17419 int face_id = -1;
17420 int no_props = INTEGERP (face);
17421 int count = SPECPDL_INDEX ();
17422 Lisp_Object str;
17423 int string_start = 0;
17425 if (NILP (window))
17426 window = selected_window;
17427 CHECK_WINDOW (window);
17428 w = XWINDOW (window);
17430 if (NILP (buffer))
17431 buffer = w->buffer;
17432 CHECK_BUFFER (buffer);
17434 /* Make formatting the modeline a non-op when noninteractive, otherwise
17435 there will be problems later caused by a partially initialized frame. */
17436 if (NILP (format) || noninteractive)
17437 return build_string ("");
17439 if (no_props)
17440 face = Qnil;
17442 if (!NILP (face))
17444 if (EQ (face, Qt))
17445 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17446 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17449 if (face_id < 0)
17450 face_id = DEFAULT_FACE_ID;
17452 if (XBUFFER (buffer) != current_buffer)
17453 old_buffer = current_buffer;
17455 /* Save things including mode_line_proptrans_alist,
17456 and set that to nil so that we don't alter the outer value. */
17457 record_unwind_protect (unwind_format_mode_line,
17458 format_mode_line_unwind_data (old_buffer, 1));
17459 mode_line_proptrans_alist = Qnil;
17461 if (old_buffer)
17462 set_buffer_internal_1 (XBUFFER (buffer));
17464 init_iterator (&it, w, -1, -1, NULL, face_id);
17466 if (no_props)
17468 mode_line_target = MODE_LINE_NOPROP;
17469 mode_line_string_face_prop = Qnil;
17470 mode_line_string_list = Qnil;
17471 string_start = MODE_LINE_NOPROP_LEN (0);
17473 else
17475 mode_line_target = MODE_LINE_STRING;
17476 mode_line_string_list = Qnil;
17477 mode_line_string_face = face;
17478 mode_line_string_face_prop
17479 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17482 push_frame_kboard (it.f);
17483 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17484 pop_frame_kboard ();
17486 if (no_props)
17488 len = MODE_LINE_NOPROP_LEN (string_start);
17489 str = make_string (mode_line_noprop_buf + string_start, len);
17491 else
17493 mode_line_string_list = Fnreverse (mode_line_string_list);
17494 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17495 make_string ("", 0));
17498 unbind_to (count, Qnil);
17499 return str;
17502 /* Write a null-terminated, right justified decimal representation of
17503 the positive integer D to BUF using a minimal field width WIDTH. */
17505 static void
17506 pint2str (buf, width, d)
17507 register char *buf;
17508 register int width;
17509 register int d;
17511 register char *p = buf;
17513 if (d <= 0)
17514 *p++ = '0';
17515 else
17517 while (d > 0)
17519 *p++ = d % 10 + '0';
17520 d /= 10;
17524 for (width -= (int) (p - buf); width > 0; --width)
17525 *p++ = ' ';
17526 *p-- = '\0';
17527 while (p > buf)
17529 d = *buf;
17530 *buf++ = *p;
17531 *p-- = d;
17535 /* Write a null-terminated, right justified decimal and "human
17536 readable" representation of the nonnegative integer D to BUF using
17537 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17539 static const char power_letter[] =
17541 0, /* not used */
17542 'k', /* kilo */
17543 'M', /* mega */
17544 'G', /* giga */
17545 'T', /* tera */
17546 'P', /* peta */
17547 'E', /* exa */
17548 'Z', /* zetta */
17549 'Y' /* yotta */
17552 static void
17553 pint2hrstr (buf, width, d)
17554 char *buf;
17555 int width;
17556 int d;
17558 /* We aim to represent the nonnegative integer D as
17559 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17560 int quotient = d;
17561 int remainder = 0;
17562 /* -1 means: do not use TENTHS. */
17563 int tenths = -1;
17564 int exponent = 0;
17566 /* Length of QUOTIENT.TENTHS as a string. */
17567 int length;
17569 char * psuffix;
17570 char * p;
17572 if (1000 <= quotient)
17574 /* Scale to the appropriate EXPONENT. */
17577 remainder = quotient % 1000;
17578 quotient /= 1000;
17579 exponent++;
17581 while (1000 <= quotient);
17583 /* Round to nearest and decide whether to use TENTHS or not. */
17584 if (quotient <= 9)
17586 tenths = remainder / 100;
17587 if (50 <= remainder % 100)
17589 if (tenths < 9)
17590 tenths++;
17591 else
17593 quotient++;
17594 if (quotient == 10)
17595 tenths = -1;
17596 else
17597 tenths = 0;
17601 else
17602 if (500 <= remainder)
17604 if (quotient < 999)
17605 quotient++;
17606 else
17608 quotient = 1;
17609 exponent++;
17610 tenths = 0;
17615 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17616 if (tenths == -1 && quotient <= 99)
17617 if (quotient <= 9)
17618 length = 1;
17619 else
17620 length = 2;
17621 else
17622 length = 3;
17623 p = psuffix = buf + max (width, length);
17625 /* Print EXPONENT. */
17626 if (exponent)
17627 *psuffix++ = power_letter[exponent];
17628 *psuffix = '\0';
17630 /* Print TENTHS. */
17631 if (tenths >= 0)
17633 *--p = '0' + tenths;
17634 *--p = '.';
17637 /* Print QUOTIENT. */
17640 int digit = quotient % 10;
17641 *--p = '0' + digit;
17643 while ((quotient /= 10) != 0);
17645 /* Print leading spaces. */
17646 while (buf < p)
17647 *--p = ' ';
17650 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17651 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17652 type of CODING_SYSTEM. Return updated pointer into BUF. */
17654 static unsigned char invalid_eol_type[] = "(*invalid*)";
17656 static char *
17657 decode_mode_spec_coding (coding_system, buf, eol_flag)
17658 Lisp_Object coding_system;
17659 register char *buf;
17660 int eol_flag;
17662 Lisp_Object val;
17663 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17664 const unsigned char *eol_str;
17665 int eol_str_len;
17666 /* The EOL conversion we are using. */
17667 Lisp_Object eoltype;
17669 val = Fget (coding_system, Qcoding_system);
17670 eoltype = Qnil;
17672 if (!VECTORP (val)) /* Not yet decided. */
17674 if (multibyte)
17675 *buf++ = '-';
17676 if (eol_flag)
17677 eoltype = eol_mnemonic_undecided;
17678 /* Don't mention EOL conversion if it isn't decided. */
17680 else
17682 Lisp_Object eolvalue;
17684 eolvalue = Fget (coding_system, Qeol_type);
17686 if (multibyte)
17687 *buf++ = XFASTINT (AREF (val, 1));
17689 if (eol_flag)
17691 /* The EOL conversion that is normal on this system. */
17693 if (NILP (eolvalue)) /* Not yet decided. */
17694 eoltype = eol_mnemonic_undecided;
17695 else if (VECTORP (eolvalue)) /* Not yet decided. */
17696 eoltype = eol_mnemonic_undecided;
17697 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17698 eoltype = (XFASTINT (eolvalue) == 0
17699 ? eol_mnemonic_unix
17700 : (XFASTINT (eolvalue) == 1
17701 ? eol_mnemonic_dos : eol_mnemonic_mac));
17705 if (eol_flag)
17707 /* Mention the EOL conversion if it is not the usual one. */
17708 if (STRINGP (eoltype))
17710 eol_str = SDATA (eoltype);
17711 eol_str_len = SBYTES (eoltype);
17713 else if (INTEGERP (eoltype)
17714 && CHAR_VALID_P (XINT (eoltype), 0))
17716 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17717 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17718 eol_str = tmp;
17720 else
17722 eol_str = invalid_eol_type;
17723 eol_str_len = sizeof (invalid_eol_type) - 1;
17725 bcopy (eol_str, buf, eol_str_len);
17726 buf += eol_str_len;
17729 return buf;
17732 /* Return a string for the output of a mode line %-spec for window W,
17733 generated by character C. PRECISION >= 0 means don't return a
17734 string longer than that value. FIELD_WIDTH > 0 means pad the
17735 string returned with spaces to that value. Return 1 in *MULTIBYTE
17736 if the result is multibyte text.
17738 Note we operate on the current buffer for most purposes,
17739 the exception being w->base_line_pos. */
17741 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17743 static char *
17744 decode_mode_spec (w, c, field_width, precision, multibyte)
17745 struct window *w;
17746 register int c;
17747 int field_width, precision;
17748 int *multibyte;
17750 Lisp_Object obj;
17751 struct frame *f = XFRAME (WINDOW_FRAME (w));
17752 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17753 struct buffer *b = current_buffer;
17755 obj = Qnil;
17756 *multibyte = 0;
17758 switch (c)
17760 case '*':
17761 if (!NILP (b->read_only))
17762 return "%";
17763 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17764 return "*";
17765 return "-";
17767 case '+':
17768 /* This differs from %* only for a modified read-only buffer. */
17769 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17770 return "*";
17771 if (!NILP (b->read_only))
17772 return "%";
17773 return "-";
17775 case '&':
17776 /* This differs from %* in ignoring read-only-ness. */
17777 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17778 return "*";
17779 return "-";
17781 case '%':
17782 return "%";
17784 case '[':
17786 int i;
17787 char *p;
17789 if (command_loop_level > 5)
17790 return "[[[... ";
17791 p = decode_mode_spec_buf;
17792 for (i = 0; i < command_loop_level; i++)
17793 *p++ = '[';
17794 *p = 0;
17795 return decode_mode_spec_buf;
17798 case ']':
17800 int i;
17801 char *p;
17803 if (command_loop_level > 5)
17804 return " ...]]]";
17805 p = decode_mode_spec_buf;
17806 for (i = 0; i < command_loop_level; i++)
17807 *p++ = ']';
17808 *p = 0;
17809 return decode_mode_spec_buf;
17812 case '-':
17814 register int i;
17816 /* Let lots_of_dashes be a string of infinite length. */
17817 if (mode_line_target == MODE_LINE_NOPROP ||
17818 mode_line_target == MODE_LINE_STRING)
17819 return "--";
17820 if (field_width <= 0
17821 || field_width > sizeof (lots_of_dashes))
17823 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17824 decode_mode_spec_buf[i] = '-';
17825 decode_mode_spec_buf[i] = '\0';
17826 return decode_mode_spec_buf;
17828 else
17829 return lots_of_dashes;
17832 case 'b':
17833 obj = b->name;
17834 break;
17836 case 'c':
17837 /* %c and %l are ignored in `frame-title-format'.
17838 (In redisplay_internal, the frame title is drawn _before_ the
17839 windows are updated, so the stuff which depends on actual
17840 window contents (such as %l) may fail to render properly, or
17841 even crash emacs.) */
17842 if (mode_line_target == MODE_LINE_TITLE)
17843 return "";
17844 else
17846 int col = (int) current_column (); /* iftc */
17847 w->column_number_displayed = make_number (col);
17848 pint2str (decode_mode_spec_buf, field_width, col);
17849 return decode_mode_spec_buf;
17852 case 'e':
17853 #ifndef SYSTEM_MALLOC
17855 if (NILP (Vmemory_full))
17856 return "";
17857 else
17858 return "!MEM FULL! ";
17860 #else
17861 return "";
17862 #endif
17864 case 'F':
17865 /* %F displays the frame name. */
17866 if (!NILP (f->title))
17867 return (char *) SDATA (f->title);
17868 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17869 return (char *) SDATA (f->name);
17870 return "Emacs";
17872 case 'f':
17873 obj = b->filename;
17874 break;
17876 case 'i':
17878 int size = ZV - BEGV;
17879 pint2str (decode_mode_spec_buf, field_width, size);
17880 return decode_mode_spec_buf;
17883 case 'I':
17885 int size = ZV - BEGV;
17886 pint2hrstr (decode_mode_spec_buf, field_width, size);
17887 return decode_mode_spec_buf;
17890 case 'l':
17892 int startpos, startpos_byte, line, linepos, linepos_byte;
17893 int topline, nlines, junk, height;
17895 /* %c and %l are ignored in `frame-title-format'. */
17896 if (mode_line_target == MODE_LINE_TITLE)
17897 return "";
17899 startpos = XMARKER (w->start)->charpos;
17900 startpos_byte = marker_byte_position (w->start);
17901 height = WINDOW_TOTAL_LINES (w);
17903 /* If we decided that this buffer isn't suitable for line numbers,
17904 don't forget that too fast. */
17905 if (EQ (w->base_line_pos, w->buffer))
17906 goto no_value;
17907 /* But do forget it, if the window shows a different buffer now. */
17908 else if (BUFFERP (w->base_line_pos))
17909 w->base_line_pos = Qnil;
17911 /* If the buffer is very big, don't waste time. */
17912 if (INTEGERP (Vline_number_display_limit)
17913 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17915 w->base_line_pos = Qnil;
17916 w->base_line_number = Qnil;
17917 goto no_value;
17920 if (!NILP (w->base_line_number)
17921 && !NILP (w->base_line_pos)
17922 && XFASTINT (w->base_line_pos) <= startpos)
17924 line = XFASTINT (w->base_line_number);
17925 linepos = XFASTINT (w->base_line_pos);
17926 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17928 else
17930 line = 1;
17931 linepos = BUF_BEGV (b);
17932 linepos_byte = BUF_BEGV_BYTE (b);
17935 /* Count lines from base line to window start position. */
17936 nlines = display_count_lines (linepos, linepos_byte,
17937 startpos_byte,
17938 startpos, &junk);
17940 topline = nlines + line;
17942 /* Determine a new base line, if the old one is too close
17943 or too far away, or if we did not have one.
17944 "Too close" means it's plausible a scroll-down would
17945 go back past it. */
17946 if (startpos == BUF_BEGV (b))
17948 w->base_line_number = make_number (topline);
17949 w->base_line_pos = make_number (BUF_BEGV (b));
17951 else if (nlines < height + 25 || nlines > height * 3 + 50
17952 || linepos == BUF_BEGV (b))
17954 int limit = BUF_BEGV (b);
17955 int limit_byte = BUF_BEGV_BYTE (b);
17956 int position;
17957 int distance = (height * 2 + 30) * line_number_display_limit_width;
17959 if (startpos - distance > limit)
17961 limit = startpos - distance;
17962 limit_byte = CHAR_TO_BYTE (limit);
17965 nlines = display_count_lines (startpos, startpos_byte,
17966 limit_byte,
17967 - (height * 2 + 30),
17968 &position);
17969 /* If we couldn't find the lines we wanted within
17970 line_number_display_limit_width chars per line,
17971 give up on line numbers for this window. */
17972 if (position == limit_byte && limit == startpos - distance)
17974 w->base_line_pos = w->buffer;
17975 w->base_line_number = Qnil;
17976 goto no_value;
17979 w->base_line_number = make_number (topline - nlines);
17980 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17983 /* Now count lines from the start pos to point. */
17984 nlines = display_count_lines (startpos, startpos_byte,
17985 PT_BYTE, PT, &junk);
17987 /* Record that we did display the line number. */
17988 line_number_displayed = 1;
17990 /* Make the string to show. */
17991 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17992 return decode_mode_spec_buf;
17993 no_value:
17995 char* p = decode_mode_spec_buf;
17996 int pad = field_width - 2;
17997 while (pad-- > 0)
17998 *p++ = ' ';
17999 *p++ = '?';
18000 *p++ = '?';
18001 *p = '\0';
18002 return decode_mode_spec_buf;
18005 break;
18007 case 'm':
18008 obj = b->mode_name;
18009 break;
18011 case 'n':
18012 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18013 return " Narrow";
18014 break;
18016 case 'p':
18018 int pos = marker_position (w->start);
18019 int total = BUF_ZV (b) - BUF_BEGV (b);
18021 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18023 if (pos <= BUF_BEGV (b))
18024 return "All";
18025 else
18026 return "Bottom";
18028 else if (pos <= BUF_BEGV (b))
18029 return "Top";
18030 else
18032 if (total > 1000000)
18033 /* Do it differently for a large value, to avoid overflow. */
18034 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18035 else
18036 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18037 /* We can't normally display a 3-digit number,
18038 so get us a 2-digit number that is close. */
18039 if (total == 100)
18040 total = 99;
18041 sprintf (decode_mode_spec_buf, "%2d%%", total);
18042 return decode_mode_spec_buf;
18046 /* Display percentage of size above the bottom of the screen. */
18047 case 'P':
18049 int toppos = marker_position (w->start);
18050 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18051 int total = BUF_ZV (b) - BUF_BEGV (b);
18053 if (botpos >= BUF_ZV (b))
18055 if (toppos <= BUF_BEGV (b))
18056 return "All";
18057 else
18058 return "Bottom";
18060 else
18062 if (total > 1000000)
18063 /* Do it differently for a large value, to avoid overflow. */
18064 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18065 else
18066 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18067 /* We can't normally display a 3-digit number,
18068 so get us a 2-digit number that is close. */
18069 if (total == 100)
18070 total = 99;
18071 if (toppos <= BUF_BEGV (b))
18072 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18073 else
18074 sprintf (decode_mode_spec_buf, "%2d%%", total);
18075 return decode_mode_spec_buf;
18079 case 's':
18080 /* status of process */
18081 obj = Fget_buffer_process (Fcurrent_buffer ());
18082 if (NILP (obj))
18083 return "no process";
18084 #ifdef subprocesses
18085 obj = Fsymbol_name (Fprocess_status (obj));
18086 #endif
18087 break;
18089 case 't': /* indicate TEXT or BINARY */
18090 #ifdef MODE_LINE_BINARY_TEXT
18091 return MODE_LINE_BINARY_TEXT (b);
18092 #else
18093 return "T";
18094 #endif
18096 case 'z':
18097 /* coding-system (not including end-of-line format) */
18098 case 'Z':
18099 /* coding-system (including end-of-line type) */
18101 int eol_flag = (c == 'Z');
18102 char *p = decode_mode_spec_buf;
18104 if (! FRAME_WINDOW_P (f))
18106 /* No need to mention EOL here--the terminal never needs
18107 to do EOL conversion. */
18108 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
18109 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
18111 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18112 p, eol_flag);
18114 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18115 #ifdef subprocesses
18116 obj = Fget_buffer_process (Fcurrent_buffer ());
18117 if (PROCESSP (obj))
18119 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18120 p, eol_flag);
18121 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18122 p, eol_flag);
18124 #endif /* subprocesses */
18125 #endif /* 0 */
18126 *p = 0;
18127 return decode_mode_spec_buf;
18131 if (STRINGP (obj))
18133 *multibyte = STRING_MULTIBYTE (obj);
18134 return (char *) SDATA (obj);
18136 else
18137 return "";
18141 /* Count up to COUNT lines starting from START / START_BYTE.
18142 But don't go beyond LIMIT_BYTE.
18143 Return the number of lines thus found (always nonnegative).
18145 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18147 static int
18148 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18149 int start, start_byte, limit_byte, count;
18150 int *byte_pos_ptr;
18152 register unsigned char *cursor;
18153 unsigned char *base;
18155 register int ceiling;
18156 register unsigned char *ceiling_addr;
18157 int orig_count = count;
18159 /* If we are not in selective display mode,
18160 check only for newlines. */
18161 int selective_display = (!NILP (current_buffer->selective_display)
18162 && !INTEGERP (current_buffer->selective_display));
18164 if (count > 0)
18166 while (start_byte < limit_byte)
18168 ceiling = BUFFER_CEILING_OF (start_byte);
18169 ceiling = min (limit_byte - 1, ceiling);
18170 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18171 base = (cursor = BYTE_POS_ADDR (start_byte));
18172 while (1)
18174 if (selective_display)
18175 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18177 else
18178 while (*cursor != '\n' && ++cursor != ceiling_addr)
18181 if (cursor != ceiling_addr)
18183 if (--count == 0)
18185 start_byte += cursor - base + 1;
18186 *byte_pos_ptr = start_byte;
18187 return orig_count;
18189 else
18190 if (++cursor == ceiling_addr)
18191 break;
18193 else
18194 break;
18196 start_byte += cursor - base;
18199 else
18201 while (start_byte > limit_byte)
18203 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18204 ceiling = max (limit_byte, ceiling);
18205 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18206 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18207 while (1)
18209 if (selective_display)
18210 while (--cursor != ceiling_addr
18211 && *cursor != '\n' && *cursor != 015)
18213 else
18214 while (--cursor != ceiling_addr && *cursor != '\n')
18217 if (cursor != ceiling_addr)
18219 if (++count == 0)
18221 start_byte += cursor - base + 1;
18222 *byte_pos_ptr = start_byte;
18223 /* When scanning backwards, we should
18224 not count the newline posterior to which we stop. */
18225 return - orig_count - 1;
18228 else
18229 break;
18231 /* Here we add 1 to compensate for the last decrement
18232 of CURSOR, which took it past the valid range. */
18233 start_byte += cursor - base + 1;
18237 *byte_pos_ptr = limit_byte;
18239 if (count < 0)
18240 return - orig_count + count;
18241 return orig_count - count;
18247 /***********************************************************************
18248 Displaying strings
18249 ***********************************************************************/
18251 /* Display a NUL-terminated string, starting with index START.
18253 If STRING is non-null, display that C string. Otherwise, the Lisp
18254 string LISP_STRING is displayed.
18256 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18257 FACE_STRING. Display STRING or LISP_STRING with the face at
18258 FACE_STRING_POS in FACE_STRING:
18260 Display the string in the environment given by IT, but use the
18261 standard display table, temporarily.
18263 FIELD_WIDTH is the minimum number of output glyphs to produce.
18264 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18265 with spaces. If STRING has more characters, more than FIELD_WIDTH
18266 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18268 PRECISION is the maximum number of characters to output from
18269 STRING. PRECISION < 0 means don't truncate the string.
18271 This is roughly equivalent to printf format specifiers:
18273 FIELD_WIDTH PRECISION PRINTF
18274 ----------------------------------------
18275 -1 -1 %s
18276 -1 10 %.10s
18277 10 -1 %10s
18278 20 10 %20.10s
18280 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18281 display them, and < 0 means obey the current buffer's value of
18282 enable_multibyte_characters.
18284 Value is the number of columns displayed. */
18286 static int
18287 display_string (string, lisp_string, face_string, face_string_pos,
18288 start, it, field_width, precision, max_x, multibyte)
18289 unsigned char *string;
18290 Lisp_Object lisp_string;
18291 Lisp_Object face_string;
18292 int face_string_pos;
18293 int start;
18294 struct it *it;
18295 int field_width, precision, max_x;
18296 int multibyte;
18298 int hpos_at_start = it->hpos;
18299 int saved_face_id = it->face_id;
18300 struct glyph_row *row = it->glyph_row;
18302 /* Initialize the iterator IT for iteration over STRING beginning
18303 with index START. */
18304 reseat_to_string (it, string, lisp_string, start,
18305 precision, field_width, multibyte);
18307 /* If displaying STRING, set up the face of the iterator
18308 from LISP_STRING, if that's given. */
18309 if (STRINGP (face_string))
18311 int endptr;
18312 struct face *face;
18314 it->face_id
18315 = face_at_string_position (it->w, face_string, face_string_pos,
18316 0, it->region_beg_charpos,
18317 it->region_end_charpos,
18318 &endptr, it->base_face_id, 0);
18319 face = FACE_FROM_ID (it->f, it->face_id);
18320 it->face_box_p = face->box != FACE_NO_BOX;
18323 /* Set max_x to the maximum allowed X position. Don't let it go
18324 beyond the right edge of the window. */
18325 if (max_x <= 0)
18326 max_x = it->last_visible_x;
18327 else
18328 max_x = min (max_x, it->last_visible_x);
18330 /* Skip over display elements that are not visible. because IT->w is
18331 hscrolled. */
18332 if (it->current_x < it->first_visible_x)
18333 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18334 MOVE_TO_POS | MOVE_TO_X);
18336 row->ascent = it->max_ascent;
18337 row->height = it->max_ascent + it->max_descent;
18338 row->phys_ascent = it->max_phys_ascent;
18339 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18340 row->extra_line_spacing = it->max_extra_line_spacing;
18342 /* This condition is for the case that we are called with current_x
18343 past last_visible_x. */
18344 while (it->current_x < max_x)
18346 int x_before, x, n_glyphs_before, i, nglyphs;
18348 /* Get the next display element. */
18349 if (!get_next_display_element (it))
18350 break;
18352 /* Produce glyphs. */
18353 x_before = it->current_x;
18354 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18355 PRODUCE_GLYPHS (it);
18357 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18358 i = 0;
18359 x = x_before;
18360 while (i < nglyphs)
18362 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18364 if (!it->truncate_lines_p
18365 && x + glyph->pixel_width > max_x)
18367 /* End of continued line or max_x reached. */
18368 if (CHAR_GLYPH_PADDING_P (*glyph))
18370 /* A wide character is unbreakable. */
18371 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18372 it->current_x = x_before;
18374 else
18376 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18377 it->current_x = x;
18379 break;
18381 else if (x + glyph->pixel_width > it->first_visible_x)
18383 /* Glyph is at least partially visible. */
18384 ++it->hpos;
18385 if (x < it->first_visible_x)
18386 it->glyph_row->x = x - it->first_visible_x;
18388 else
18390 /* Glyph is off the left margin of the display area.
18391 Should not happen. */
18392 abort ();
18395 row->ascent = max (row->ascent, it->max_ascent);
18396 row->height = max (row->height, it->max_ascent + it->max_descent);
18397 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18398 row->phys_height = max (row->phys_height,
18399 it->max_phys_ascent + it->max_phys_descent);
18400 row->extra_line_spacing = max (row->extra_line_spacing,
18401 it->max_extra_line_spacing);
18402 x += glyph->pixel_width;
18403 ++i;
18406 /* Stop if max_x reached. */
18407 if (i < nglyphs)
18408 break;
18410 /* Stop at line ends. */
18411 if (ITERATOR_AT_END_OF_LINE_P (it))
18413 it->continuation_lines_width = 0;
18414 break;
18417 set_iterator_to_next (it, 1);
18419 /* Stop if truncating at the right edge. */
18420 if (it->truncate_lines_p
18421 && it->current_x >= it->last_visible_x)
18423 /* Add truncation mark, but don't do it if the line is
18424 truncated at a padding space. */
18425 if (IT_CHARPOS (*it) < it->string_nchars)
18427 if (!FRAME_WINDOW_P (it->f))
18429 int i, n;
18431 if (it->current_x > it->last_visible_x)
18433 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18434 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18435 break;
18436 for (n = row->used[TEXT_AREA]; i < n; ++i)
18438 row->used[TEXT_AREA] = i;
18439 produce_special_glyphs (it, IT_TRUNCATION);
18442 produce_special_glyphs (it, IT_TRUNCATION);
18444 it->glyph_row->truncated_on_right_p = 1;
18446 break;
18450 /* Maybe insert a truncation at the left. */
18451 if (it->first_visible_x
18452 && IT_CHARPOS (*it) > 0)
18454 if (!FRAME_WINDOW_P (it->f))
18455 insert_left_trunc_glyphs (it);
18456 it->glyph_row->truncated_on_left_p = 1;
18459 it->face_id = saved_face_id;
18461 /* Value is number of columns displayed. */
18462 return it->hpos - hpos_at_start;
18467 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18468 appears as an element of LIST or as the car of an element of LIST.
18469 If PROPVAL is a list, compare each element against LIST in that
18470 way, and return 1/2 if any element of PROPVAL is found in LIST.
18471 Otherwise return 0. This function cannot quit.
18472 The return value is 2 if the text is invisible but with an ellipsis
18473 and 1 if it's invisible and without an ellipsis. */
18476 invisible_p (propval, list)
18477 register Lisp_Object propval;
18478 Lisp_Object list;
18480 register Lisp_Object tail, proptail;
18482 for (tail = list; CONSP (tail); tail = XCDR (tail))
18484 register Lisp_Object tem;
18485 tem = XCAR (tail);
18486 if (EQ (propval, tem))
18487 return 1;
18488 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18489 return NILP (XCDR (tem)) ? 1 : 2;
18492 if (CONSP (propval))
18494 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18496 Lisp_Object propelt;
18497 propelt = XCAR (proptail);
18498 for (tail = list; CONSP (tail); tail = XCDR (tail))
18500 register Lisp_Object tem;
18501 tem = XCAR (tail);
18502 if (EQ (propelt, tem))
18503 return 1;
18504 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18505 return NILP (XCDR (tem)) ? 1 : 2;
18510 return 0;
18513 /* Calculate a width or height in pixels from a specification using
18514 the following elements:
18516 SPEC ::=
18517 NUM - a (fractional) multiple of the default font width/height
18518 (NUM) - specifies exactly NUM pixels
18519 UNIT - a fixed number of pixels, see below.
18520 ELEMENT - size of a display element in pixels, see below.
18521 (NUM . SPEC) - equals NUM * SPEC
18522 (+ SPEC SPEC ...) - add pixel values
18523 (- SPEC SPEC ...) - subtract pixel values
18524 (- SPEC) - negate pixel value
18526 NUM ::=
18527 INT or FLOAT - a number constant
18528 SYMBOL - use symbol's (buffer local) variable binding.
18530 UNIT ::=
18531 in - pixels per inch *)
18532 mm - pixels per 1/1000 meter *)
18533 cm - pixels per 1/100 meter *)
18534 width - width of current font in pixels.
18535 height - height of current font in pixels.
18537 *) using the ratio(s) defined in display-pixels-per-inch.
18539 ELEMENT ::=
18541 left-fringe - left fringe width in pixels
18542 right-fringe - right fringe width in pixels
18544 left-margin - left margin width in pixels
18545 right-margin - right margin width in pixels
18547 scroll-bar - scroll-bar area width in pixels
18549 Examples:
18551 Pixels corresponding to 5 inches:
18552 (5 . in)
18554 Total width of non-text areas on left side of window (if scroll-bar is on left):
18555 '(space :width (+ left-fringe left-margin scroll-bar))
18557 Align to first text column (in header line):
18558 '(space :align-to 0)
18560 Align to middle of text area minus half the width of variable `my-image'
18561 containing a loaded image:
18562 '(space :align-to (0.5 . (- text my-image)))
18564 Width of left margin minus width of 1 character in the default font:
18565 '(space :width (- left-margin 1))
18567 Width of left margin minus width of 2 characters in the current font:
18568 '(space :width (- left-margin (2 . width)))
18570 Center 1 character over left-margin (in header line):
18571 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18573 Different ways to express width of left fringe plus left margin minus one pixel:
18574 '(space :width (- (+ left-fringe left-margin) (1)))
18575 '(space :width (+ left-fringe left-margin (- (1))))
18576 '(space :width (+ left-fringe left-margin (-1)))
18580 #define NUMVAL(X) \
18581 ((INTEGERP (X) || FLOATP (X)) \
18582 ? XFLOATINT (X) \
18583 : - 1)
18586 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18587 double *res;
18588 struct it *it;
18589 Lisp_Object prop;
18590 void *font;
18591 int width_p, *align_to;
18593 double pixels;
18595 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18596 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18598 if (NILP (prop))
18599 return OK_PIXELS (0);
18601 if (SYMBOLP (prop))
18603 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18605 char *unit = SDATA (SYMBOL_NAME (prop));
18607 if (unit[0] == 'i' && unit[1] == 'n')
18608 pixels = 1.0;
18609 else if (unit[0] == 'm' && unit[1] == 'm')
18610 pixels = 25.4;
18611 else if (unit[0] == 'c' && unit[1] == 'm')
18612 pixels = 2.54;
18613 else
18614 pixels = 0;
18615 if (pixels > 0)
18617 double ppi;
18618 #ifdef HAVE_WINDOW_SYSTEM
18619 if (FRAME_WINDOW_P (it->f)
18620 && (ppi = (width_p
18621 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18622 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18623 ppi > 0))
18624 return OK_PIXELS (ppi / pixels);
18625 #endif
18627 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18628 || (CONSP (Vdisplay_pixels_per_inch)
18629 && (ppi = (width_p
18630 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18631 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18632 ppi > 0)))
18633 return OK_PIXELS (ppi / pixels);
18635 return 0;
18639 #ifdef HAVE_WINDOW_SYSTEM
18640 if (EQ (prop, Qheight))
18641 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18642 if (EQ (prop, Qwidth))
18643 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18644 #else
18645 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18646 return OK_PIXELS (1);
18647 #endif
18649 if (EQ (prop, Qtext))
18650 return OK_PIXELS (width_p
18651 ? window_box_width (it->w, TEXT_AREA)
18652 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18654 if (align_to && *align_to < 0)
18656 *res = 0;
18657 if (EQ (prop, Qleft))
18658 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18659 if (EQ (prop, Qright))
18660 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18661 if (EQ (prop, Qcenter))
18662 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18663 + window_box_width (it->w, TEXT_AREA) / 2);
18664 if (EQ (prop, Qleft_fringe))
18665 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18666 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18667 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18668 if (EQ (prop, Qright_fringe))
18669 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18670 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18671 : window_box_right_offset (it->w, TEXT_AREA));
18672 if (EQ (prop, Qleft_margin))
18673 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18674 if (EQ (prop, Qright_margin))
18675 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18676 if (EQ (prop, Qscroll_bar))
18677 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18679 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18680 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18681 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18682 : 0)));
18684 else
18686 if (EQ (prop, Qleft_fringe))
18687 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18688 if (EQ (prop, Qright_fringe))
18689 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18690 if (EQ (prop, Qleft_margin))
18691 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18692 if (EQ (prop, Qright_margin))
18693 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18694 if (EQ (prop, Qscroll_bar))
18695 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18698 prop = Fbuffer_local_value (prop, it->w->buffer);
18701 if (INTEGERP (prop) || FLOATP (prop))
18703 int base_unit = (width_p
18704 ? FRAME_COLUMN_WIDTH (it->f)
18705 : FRAME_LINE_HEIGHT (it->f));
18706 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18709 if (CONSP (prop))
18711 Lisp_Object car = XCAR (prop);
18712 Lisp_Object cdr = XCDR (prop);
18714 if (SYMBOLP (car))
18716 #ifdef HAVE_WINDOW_SYSTEM
18717 if (valid_image_p (prop))
18719 int id = lookup_image (it->f, prop);
18720 struct image *img = IMAGE_FROM_ID (it->f, id);
18722 return OK_PIXELS (width_p ? img->width : img->height);
18724 #endif
18725 if (EQ (car, Qplus) || EQ (car, Qminus))
18727 int first = 1;
18728 double px;
18730 pixels = 0;
18731 while (CONSP (cdr))
18733 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18734 font, width_p, align_to))
18735 return 0;
18736 if (first)
18737 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18738 else
18739 pixels += px;
18740 cdr = XCDR (cdr);
18742 if (EQ (car, Qminus))
18743 pixels = -pixels;
18744 return OK_PIXELS (pixels);
18747 car = Fbuffer_local_value (car, it->w->buffer);
18750 if (INTEGERP (car) || FLOATP (car))
18752 double fact;
18753 pixels = XFLOATINT (car);
18754 if (NILP (cdr))
18755 return OK_PIXELS (pixels);
18756 if (calc_pixel_width_or_height (&fact, it, cdr,
18757 font, width_p, align_to))
18758 return OK_PIXELS (pixels * fact);
18759 return 0;
18762 return 0;
18765 return 0;
18769 /***********************************************************************
18770 Glyph Display
18771 ***********************************************************************/
18773 #ifdef HAVE_WINDOW_SYSTEM
18775 #if GLYPH_DEBUG
18777 void
18778 dump_glyph_string (s)
18779 struct glyph_string *s;
18781 fprintf (stderr, "glyph string\n");
18782 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18783 s->x, s->y, s->width, s->height);
18784 fprintf (stderr, " ybase = %d\n", s->ybase);
18785 fprintf (stderr, " hl = %d\n", s->hl);
18786 fprintf (stderr, " left overhang = %d, right = %d\n",
18787 s->left_overhang, s->right_overhang);
18788 fprintf (stderr, " nchars = %d\n", s->nchars);
18789 fprintf (stderr, " extends to end of line = %d\n",
18790 s->extends_to_end_of_line_p);
18791 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18792 fprintf (stderr, " bg width = %d\n", s->background_width);
18795 #endif /* GLYPH_DEBUG */
18797 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18798 of XChar2b structures for S; it can't be allocated in
18799 init_glyph_string because it must be allocated via `alloca'. W
18800 is the window on which S is drawn. ROW and AREA are the glyph row
18801 and area within the row from which S is constructed. START is the
18802 index of the first glyph structure covered by S. HL is a
18803 face-override for drawing S. */
18805 #ifdef HAVE_NTGUI
18806 #define OPTIONAL_HDC(hdc) hdc,
18807 #define DECLARE_HDC(hdc) HDC hdc;
18808 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18809 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18810 #endif
18812 #ifndef OPTIONAL_HDC
18813 #define OPTIONAL_HDC(hdc)
18814 #define DECLARE_HDC(hdc)
18815 #define ALLOCATE_HDC(hdc, f)
18816 #define RELEASE_HDC(hdc, f)
18817 #endif
18819 static void
18820 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18821 struct glyph_string *s;
18822 DECLARE_HDC (hdc)
18823 XChar2b *char2b;
18824 struct window *w;
18825 struct glyph_row *row;
18826 enum glyph_row_area area;
18827 int start;
18828 enum draw_glyphs_face hl;
18830 bzero (s, sizeof *s);
18831 s->w = w;
18832 s->f = XFRAME (w->frame);
18833 #ifdef HAVE_NTGUI
18834 s->hdc = hdc;
18835 #endif
18836 s->display = FRAME_X_DISPLAY (s->f);
18837 s->window = FRAME_X_WINDOW (s->f);
18838 s->char2b = char2b;
18839 s->hl = hl;
18840 s->row = row;
18841 s->area = area;
18842 s->first_glyph = row->glyphs[area] + start;
18843 s->height = row->height;
18844 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18846 /* Display the internal border below the tool-bar window. */
18847 if (WINDOWP (s->f->tool_bar_window)
18848 && s->w == XWINDOW (s->f->tool_bar_window))
18849 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18851 s->ybase = s->y + row->ascent;
18855 /* Append the list of glyph strings with head H and tail T to the list
18856 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18858 static INLINE void
18859 append_glyph_string_lists (head, tail, h, t)
18860 struct glyph_string **head, **tail;
18861 struct glyph_string *h, *t;
18863 if (h)
18865 if (*head)
18866 (*tail)->next = h;
18867 else
18868 *head = h;
18869 h->prev = *tail;
18870 *tail = t;
18875 /* Prepend the list of glyph strings with head H and tail T to the
18876 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18877 result. */
18879 static INLINE void
18880 prepend_glyph_string_lists (head, tail, h, t)
18881 struct glyph_string **head, **tail;
18882 struct glyph_string *h, *t;
18884 if (h)
18886 if (*head)
18887 (*head)->prev = t;
18888 else
18889 *tail = t;
18890 t->next = *head;
18891 *head = h;
18896 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18897 Set *HEAD and *TAIL to the resulting list. */
18899 static INLINE void
18900 append_glyph_string (head, tail, s)
18901 struct glyph_string **head, **tail;
18902 struct glyph_string *s;
18904 s->next = s->prev = NULL;
18905 append_glyph_string_lists (head, tail, s, s);
18909 /* Get face and two-byte form of character glyph GLYPH on frame F.
18910 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18911 a pointer to a realized face that is ready for display. */
18913 static INLINE struct face *
18914 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18915 struct frame *f;
18916 struct glyph *glyph;
18917 XChar2b *char2b;
18918 int *two_byte_p;
18920 struct face *face;
18922 xassert (glyph->type == CHAR_GLYPH);
18923 face = FACE_FROM_ID (f, glyph->face_id);
18925 if (two_byte_p)
18926 *two_byte_p = 0;
18928 if (!glyph->multibyte_p)
18930 /* Unibyte case. We don't have to encode, but we have to make
18931 sure to use a face suitable for unibyte. */
18932 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18934 else if (glyph->u.ch < 128)
18936 /* Case of ASCII in a face known to fit ASCII. */
18937 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18939 else
18941 int c1, c2, charset;
18943 /* Split characters into bytes. If c2 is -1 afterwards, C is
18944 really a one-byte character so that byte1 is zero. */
18945 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18946 if (c2 > 0)
18947 STORE_XCHAR2B (char2b, c1, c2);
18948 else
18949 STORE_XCHAR2B (char2b, 0, c1);
18951 /* Maybe encode the character in *CHAR2B. */
18952 if (charset != CHARSET_ASCII)
18954 struct font_info *font_info
18955 = FONT_INFO_FROM_ID (f, face->font_info_id);
18956 if (font_info)
18957 glyph->font_type
18958 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18962 /* Make sure X resources of the face are allocated. */
18963 xassert (face != NULL);
18964 PREPARE_FACE_FOR_DISPLAY (f, face);
18965 return face;
18969 /* Fill glyph string S with composition components specified by S->cmp.
18971 FACES is an array of faces for all components of this composition.
18972 S->gidx is the index of the first component for S.
18974 OVERLAPS non-zero means S should draw the foreground only, and use
18975 its physical height for clipping. See also draw_glyphs.
18977 Value is the index of a component not in S. */
18979 static int
18980 fill_composite_glyph_string (s, faces, overlaps)
18981 struct glyph_string *s;
18982 struct face **faces;
18983 int overlaps;
18985 int i;
18987 xassert (s);
18989 s->for_overlaps = overlaps;
18991 s->face = faces[s->gidx];
18992 s->font = s->face->font;
18993 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18995 /* For all glyphs of this composition, starting at the offset
18996 S->gidx, until we reach the end of the definition or encounter a
18997 glyph that requires the different face, add it to S. */
18998 ++s->nchars;
18999 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
19000 ++s->nchars;
19002 /* All glyph strings for the same composition has the same width,
19003 i.e. the width set for the first component of the composition. */
19005 s->width = s->first_glyph->pixel_width;
19007 /* If the specified font could not be loaded, use the frame's
19008 default font, but record the fact that we couldn't load it in
19009 the glyph string so that we can draw rectangles for the
19010 characters of the glyph string. */
19011 if (s->font == NULL)
19013 s->font_not_found_p = 1;
19014 s->font = FRAME_FONT (s->f);
19017 /* Adjust base line for subscript/superscript text. */
19018 s->ybase += s->first_glyph->voffset;
19020 xassert (s->face && s->face->gc);
19022 /* This glyph string must always be drawn with 16-bit functions. */
19023 s->two_byte_p = 1;
19025 return s->gidx + s->nchars;
19029 /* Fill glyph string S from a sequence of character glyphs.
19031 FACE_ID is the face id of the string. START is the index of the
19032 first glyph to consider, END is the index of the last + 1.
19033 OVERLAPS non-zero means S should draw the foreground only, and use
19034 its physical height for clipping. See also draw_glyphs.
19036 Value is the index of the first glyph not in S. */
19038 static int
19039 fill_glyph_string (s, face_id, start, end, overlaps)
19040 struct glyph_string *s;
19041 int face_id;
19042 int start, end, overlaps;
19044 struct glyph *glyph, *last;
19045 int voffset;
19046 int glyph_not_available_p;
19048 xassert (s->f == XFRAME (s->w->frame));
19049 xassert (s->nchars == 0);
19050 xassert (start >= 0 && end > start);
19052 s->for_overlaps = overlaps,
19053 glyph = s->row->glyphs[s->area] + start;
19054 last = s->row->glyphs[s->area] + end;
19055 voffset = glyph->voffset;
19057 glyph_not_available_p = glyph->glyph_not_available_p;
19059 while (glyph < last
19060 && glyph->type == CHAR_GLYPH
19061 && glyph->voffset == voffset
19062 /* Same face id implies same font, nowadays. */
19063 && glyph->face_id == face_id
19064 && glyph->glyph_not_available_p == glyph_not_available_p)
19066 int two_byte_p;
19068 s->face = get_glyph_face_and_encoding (s->f, glyph,
19069 s->char2b + s->nchars,
19070 &two_byte_p);
19071 s->two_byte_p = two_byte_p;
19072 ++s->nchars;
19073 xassert (s->nchars <= end - start);
19074 s->width += glyph->pixel_width;
19075 ++glyph;
19078 s->font = s->face->font;
19079 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19081 /* If the specified font could not be loaded, use the frame's font,
19082 but record the fact that we couldn't load it in
19083 S->font_not_found_p so that we can draw rectangles for the
19084 characters of the glyph string. */
19085 if (s->font == NULL || glyph_not_available_p)
19087 s->font_not_found_p = 1;
19088 s->font = FRAME_FONT (s->f);
19091 /* Adjust base line for subscript/superscript text. */
19092 s->ybase += voffset;
19094 xassert (s->face && s->face->gc);
19095 return glyph - s->row->glyphs[s->area];
19099 /* Fill glyph string S from image glyph S->first_glyph. */
19101 static void
19102 fill_image_glyph_string (s)
19103 struct glyph_string *s;
19105 xassert (s->first_glyph->type == IMAGE_GLYPH);
19106 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19107 xassert (s->img);
19108 s->slice = s->first_glyph->slice;
19109 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19110 s->font = s->face->font;
19111 s->width = s->first_glyph->pixel_width;
19113 /* Adjust base line for subscript/superscript text. */
19114 s->ybase += s->first_glyph->voffset;
19118 /* Fill glyph string S from a sequence of stretch glyphs.
19120 ROW is the glyph row in which the glyphs are found, AREA is the
19121 area within the row. START is the index of the first glyph to
19122 consider, END is the index of the last + 1.
19124 Value is the index of the first glyph not in S. */
19126 static int
19127 fill_stretch_glyph_string (s, row, area, start, end)
19128 struct glyph_string *s;
19129 struct glyph_row *row;
19130 enum glyph_row_area area;
19131 int start, end;
19133 struct glyph *glyph, *last;
19134 int voffset, face_id;
19136 xassert (s->first_glyph->type == STRETCH_GLYPH);
19138 glyph = s->row->glyphs[s->area] + start;
19139 last = s->row->glyphs[s->area] + end;
19140 face_id = glyph->face_id;
19141 s->face = FACE_FROM_ID (s->f, face_id);
19142 s->font = s->face->font;
19143 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19144 s->width = glyph->pixel_width;
19145 s->nchars = 1;
19146 voffset = glyph->voffset;
19148 for (++glyph;
19149 (glyph < last
19150 && glyph->type == STRETCH_GLYPH
19151 && glyph->voffset == voffset
19152 && glyph->face_id == face_id);
19153 ++glyph)
19154 s->width += glyph->pixel_width;
19156 /* Adjust base line for subscript/superscript text. */
19157 s->ybase += voffset;
19159 /* The case that face->gc == 0 is handled when drawing the glyph
19160 string by calling PREPARE_FACE_FOR_DISPLAY. */
19161 xassert (s->face);
19162 return glyph - s->row->glyphs[s->area];
19166 /* EXPORT for RIF:
19167 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19168 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19169 assumed to be zero. */
19171 void
19172 x_get_glyph_overhangs (glyph, f, left, right)
19173 struct glyph *glyph;
19174 struct frame *f;
19175 int *left, *right;
19177 *left = *right = 0;
19179 if (glyph->type == CHAR_GLYPH)
19181 XFontStruct *font;
19182 struct face *face;
19183 struct font_info *font_info;
19184 XChar2b char2b;
19185 XCharStruct *pcm;
19187 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19188 font = face->font;
19189 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19190 if (font /* ++KFS: Should this be font_info ? */
19191 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19193 if (pcm->rbearing > pcm->width)
19194 *right = pcm->rbearing - pcm->width;
19195 if (pcm->lbearing < 0)
19196 *left = -pcm->lbearing;
19202 /* Return the index of the first glyph preceding glyph string S that
19203 is overwritten by S because of S's left overhang. Value is -1
19204 if no glyphs are overwritten. */
19206 static int
19207 left_overwritten (s)
19208 struct glyph_string *s;
19210 int k;
19212 if (s->left_overhang)
19214 int x = 0, i;
19215 struct glyph *glyphs = s->row->glyphs[s->area];
19216 int first = s->first_glyph - glyphs;
19218 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19219 x -= glyphs[i].pixel_width;
19221 k = i + 1;
19223 else
19224 k = -1;
19226 return k;
19230 /* Return the index of the first glyph preceding glyph string S that
19231 is overwriting S because of its right overhang. Value is -1 if no
19232 glyph in front of S overwrites S. */
19234 static int
19235 left_overwriting (s)
19236 struct glyph_string *s;
19238 int i, k, x;
19239 struct glyph *glyphs = s->row->glyphs[s->area];
19240 int first = s->first_glyph - glyphs;
19242 k = -1;
19243 x = 0;
19244 for (i = first - 1; i >= 0; --i)
19246 int left, right;
19247 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19248 if (x + right > 0)
19249 k = i;
19250 x -= glyphs[i].pixel_width;
19253 return k;
19257 /* Return the index of the last glyph following glyph string S that is
19258 not overwritten by S because of S's right overhang. Value is -1 if
19259 no such glyph is found. */
19261 static int
19262 right_overwritten (s)
19263 struct glyph_string *s;
19265 int k = -1;
19267 if (s->right_overhang)
19269 int x = 0, i;
19270 struct glyph *glyphs = s->row->glyphs[s->area];
19271 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19272 int end = s->row->used[s->area];
19274 for (i = first; i < end && s->right_overhang > x; ++i)
19275 x += glyphs[i].pixel_width;
19277 k = i;
19280 return k;
19284 /* Return the index of the last glyph following glyph string S that
19285 overwrites S because of its left overhang. Value is negative
19286 if no such glyph is found. */
19288 static int
19289 right_overwriting (s)
19290 struct glyph_string *s;
19292 int i, k, x;
19293 int end = s->row->used[s->area];
19294 struct glyph *glyphs = s->row->glyphs[s->area];
19295 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19297 k = -1;
19298 x = 0;
19299 for (i = first; i < end; ++i)
19301 int left, right;
19302 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19303 if (x - left < 0)
19304 k = i;
19305 x += glyphs[i].pixel_width;
19308 return k;
19312 /* Get face and two-byte form of character C in face FACE_ID on frame
19313 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19314 means we want to display multibyte text. DISPLAY_P non-zero means
19315 make sure that X resources for the face returned are allocated.
19316 Value is a pointer to a realized face that is ready for display if
19317 DISPLAY_P is non-zero. */
19319 static INLINE struct face *
19320 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19321 struct frame *f;
19322 int c, face_id;
19323 XChar2b *char2b;
19324 int multibyte_p, display_p;
19326 struct face *face = FACE_FROM_ID (f, face_id);
19328 if (!multibyte_p)
19330 /* Unibyte case. We don't have to encode, but we have to make
19331 sure to use a face suitable for unibyte. */
19332 STORE_XCHAR2B (char2b, 0, c);
19333 face_id = FACE_FOR_CHAR (f, face, c);
19334 face = FACE_FROM_ID (f, face_id);
19336 else if (c < 128)
19338 /* Case of ASCII in a face known to fit ASCII. */
19339 STORE_XCHAR2B (char2b, 0, c);
19341 else
19343 int c1, c2, charset;
19345 /* Split characters into bytes. If c2 is -1 afterwards, C is
19346 really a one-byte character so that byte1 is zero. */
19347 SPLIT_CHAR (c, charset, c1, c2);
19348 if (c2 > 0)
19349 STORE_XCHAR2B (char2b, c1, c2);
19350 else
19351 STORE_XCHAR2B (char2b, 0, c1);
19353 /* Maybe encode the character in *CHAR2B. */
19354 if (face->font != NULL)
19356 struct font_info *font_info
19357 = FONT_INFO_FROM_ID (f, face->font_info_id);
19358 if (font_info)
19359 rif->encode_char (c, char2b, font_info, 0);
19363 /* Make sure X resources of the face are allocated. */
19364 #ifdef HAVE_X_WINDOWS
19365 if (display_p)
19366 #endif
19368 xassert (face != NULL);
19369 PREPARE_FACE_FOR_DISPLAY (f, face);
19372 return face;
19376 /* Set background width of glyph string S. START is the index of the
19377 first glyph following S. LAST_X is the right-most x-position + 1
19378 in the drawing area. */
19380 static INLINE void
19381 set_glyph_string_background_width (s, start, last_x)
19382 struct glyph_string *s;
19383 int start;
19384 int last_x;
19386 /* If the face of this glyph string has to be drawn to the end of
19387 the drawing area, set S->extends_to_end_of_line_p. */
19389 if (start == s->row->used[s->area]
19390 && s->area == TEXT_AREA
19391 && ((s->row->fill_line_p
19392 && (s->hl == DRAW_NORMAL_TEXT
19393 || s->hl == DRAW_IMAGE_RAISED
19394 || s->hl == DRAW_IMAGE_SUNKEN))
19395 || s->hl == DRAW_MOUSE_FACE))
19396 s->extends_to_end_of_line_p = 1;
19398 /* If S extends its face to the end of the line, set its
19399 background_width to the distance to the right edge of the drawing
19400 area. */
19401 if (s->extends_to_end_of_line_p)
19402 s->background_width = last_x - s->x + 1;
19403 else
19404 s->background_width = s->width;
19408 /* Compute overhangs and x-positions for glyph string S and its
19409 predecessors, or successors. X is the starting x-position for S.
19410 BACKWARD_P non-zero means process predecessors. */
19412 static void
19413 compute_overhangs_and_x (s, x, backward_p)
19414 struct glyph_string *s;
19415 int x;
19416 int backward_p;
19418 if (backward_p)
19420 while (s)
19422 if (rif->compute_glyph_string_overhangs)
19423 rif->compute_glyph_string_overhangs (s);
19424 x -= s->width;
19425 s->x = x;
19426 s = s->prev;
19429 else
19431 while (s)
19433 if (rif->compute_glyph_string_overhangs)
19434 rif->compute_glyph_string_overhangs (s);
19435 s->x = x;
19436 x += s->width;
19437 s = s->next;
19444 /* The following macros are only called from draw_glyphs below.
19445 They reference the following parameters of that function directly:
19446 `w', `row', `area', and `overlap_p'
19447 as well as the following local variables:
19448 `s', `f', and `hdc' (in W32) */
19450 #ifdef HAVE_NTGUI
19451 /* On W32, silently add local `hdc' variable to argument list of
19452 init_glyph_string. */
19453 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19454 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19455 #else
19456 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19457 init_glyph_string (s, char2b, w, row, area, start, hl)
19458 #endif
19460 /* Add a glyph string for a stretch glyph to the list of strings
19461 between HEAD and TAIL. START is the index of the stretch glyph in
19462 row area AREA of glyph row ROW. END is the index of the last glyph
19463 in that glyph row area. X is the current output position assigned
19464 to the new glyph string constructed. HL overrides that face of the
19465 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19466 is the right-most x-position of the drawing area. */
19468 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19469 and below -- keep them on one line. */
19470 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19471 do \
19473 s = (struct glyph_string *) alloca (sizeof *s); \
19474 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19475 START = fill_stretch_glyph_string (s, row, area, START, END); \
19476 append_glyph_string (&HEAD, &TAIL, s); \
19477 s->x = (X); \
19479 while (0)
19482 /* Add a glyph string for an image glyph to the list of strings
19483 between HEAD and TAIL. START is the index of the image glyph in
19484 row area AREA of glyph row ROW. END is the index of the last glyph
19485 in that glyph row area. X is the current output position assigned
19486 to the new glyph string constructed. HL overrides that face of the
19487 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19488 is the right-most x-position of the drawing area. */
19490 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19491 do \
19493 s = (struct glyph_string *) alloca (sizeof *s); \
19494 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19495 fill_image_glyph_string (s); \
19496 append_glyph_string (&HEAD, &TAIL, s); \
19497 ++START; \
19498 s->x = (X); \
19500 while (0)
19503 /* Add a glyph string for a sequence of character glyphs to the list
19504 of strings between HEAD and TAIL. START is the index of the first
19505 glyph in row area AREA of glyph row ROW that is part of the new
19506 glyph string. END is the index of the last glyph in that glyph row
19507 area. X is the current output position assigned to the new glyph
19508 string constructed. HL overrides that face of the glyph; e.g. it
19509 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19510 right-most x-position of the drawing area. */
19512 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19513 do \
19515 int c, face_id; \
19516 XChar2b *char2b; \
19518 c = (row)->glyphs[area][START].u.ch; \
19519 face_id = (row)->glyphs[area][START].face_id; \
19521 s = (struct glyph_string *) alloca (sizeof *s); \
19522 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19523 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19524 append_glyph_string (&HEAD, &TAIL, s); \
19525 s->x = (X); \
19526 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19528 while (0)
19531 /* Add a glyph string for a composite sequence to the list of strings
19532 between HEAD and TAIL. START is the index of the first glyph in
19533 row area AREA of glyph row ROW that is part of the new glyph
19534 string. END is the index of the last glyph in that glyph row area.
19535 X is the current output position assigned to the new glyph string
19536 constructed. HL overrides that face of the glyph; e.g. it is
19537 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19538 x-position of the drawing area. */
19540 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19541 do { \
19542 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19543 int face_id = (row)->glyphs[area][START].face_id; \
19544 struct face *base_face = FACE_FROM_ID (f, face_id); \
19545 struct composition *cmp = composition_table[cmp_id]; \
19546 int glyph_len = cmp->glyph_len; \
19547 XChar2b *char2b; \
19548 struct face **faces; \
19549 struct glyph_string *first_s = NULL; \
19550 int n; \
19552 base_face = base_face->ascii_face; \
19553 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19554 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19555 /* At first, fill in `char2b' and `faces'. */ \
19556 for (n = 0; n < glyph_len; n++) \
19558 int c = COMPOSITION_GLYPH (cmp, n); \
19559 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19560 faces[n] = FACE_FROM_ID (f, this_face_id); \
19561 get_char_face_and_encoding (f, c, this_face_id, \
19562 char2b + n, 1, 1); \
19565 /* Make glyph_strings for each glyph sequence that is drawable by \
19566 the same face, and append them to HEAD/TAIL. */ \
19567 for (n = 0; n < cmp->glyph_len;) \
19569 s = (struct glyph_string *) alloca (sizeof *s); \
19570 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19571 append_glyph_string (&(HEAD), &(TAIL), s); \
19572 s->cmp = cmp; \
19573 s->gidx = n; \
19574 s->x = (X); \
19576 if (n == 0) \
19577 first_s = s; \
19579 n = fill_composite_glyph_string (s, faces, overlaps); \
19582 ++START; \
19583 s = first_s; \
19584 } while (0)
19587 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19588 of AREA of glyph row ROW on window W between indices START and END.
19589 HL overrides the face for drawing glyph strings, e.g. it is
19590 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19591 x-positions of the drawing area.
19593 This is an ugly monster macro construct because we must use alloca
19594 to allocate glyph strings (because draw_glyphs can be called
19595 asynchronously). */
19597 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19598 do \
19600 HEAD = TAIL = NULL; \
19601 while (START < END) \
19603 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19604 switch (first_glyph->type) \
19606 case CHAR_GLYPH: \
19607 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19608 HL, X, LAST_X); \
19609 break; \
19611 case COMPOSITE_GLYPH: \
19612 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19613 HL, X, LAST_X); \
19614 break; \
19616 case STRETCH_GLYPH: \
19617 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19618 HL, X, LAST_X); \
19619 break; \
19621 case IMAGE_GLYPH: \
19622 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19623 HL, X, LAST_X); \
19624 break; \
19626 default: \
19627 abort (); \
19630 set_glyph_string_background_width (s, START, LAST_X); \
19631 (X) += s->width; \
19634 while (0)
19637 /* Draw glyphs between START and END in AREA of ROW on window W,
19638 starting at x-position X. X is relative to AREA in W. HL is a
19639 face-override with the following meaning:
19641 DRAW_NORMAL_TEXT draw normally
19642 DRAW_CURSOR draw in cursor face
19643 DRAW_MOUSE_FACE draw in mouse face.
19644 DRAW_INVERSE_VIDEO draw in mode line face
19645 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19646 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19648 If OVERLAPS is non-zero, draw only the foreground of characters and
19649 clip to the physical height of ROW. Non-zero value also defines
19650 the overlapping part to be drawn:
19652 OVERLAPS_PRED overlap with preceding rows
19653 OVERLAPS_SUCC overlap with succeeding rows
19654 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19655 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19657 Value is the x-position reached, relative to AREA of W. */
19659 static int
19660 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19661 struct window *w;
19662 int x;
19663 struct glyph_row *row;
19664 enum glyph_row_area area;
19665 int start, end;
19666 enum draw_glyphs_face hl;
19667 int overlaps;
19669 struct glyph_string *head, *tail;
19670 struct glyph_string *s;
19671 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19672 int last_x, area_width;
19673 int x_reached;
19674 int i, j;
19675 struct frame *f = XFRAME (WINDOW_FRAME (w));
19676 DECLARE_HDC (hdc);
19678 ALLOCATE_HDC (hdc, f);
19680 /* Let's rather be paranoid than getting a SEGV. */
19681 end = min (end, row->used[area]);
19682 start = max (0, start);
19683 start = min (end, start);
19685 /* Translate X to frame coordinates. Set last_x to the right
19686 end of the drawing area. */
19687 if (row->full_width_p)
19689 /* X is relative to the left edge of W, without scroll bars
19690 or fringes. */
19691 x += WINDOW_LEFT_EDGE_X (w);
19692 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19694 else
19696 int area_left = window_box_left (w, area);
19697 x += area_left;
19698 area_width = window_box_width (w, area);
19699 last_x = area_left + area_width;
19702 /* Build a doubly-linked list of glyph_string structures between
19703 head and tail from what we have to draw. Note that the macro
19704 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19705 the reason we use a separate variable `i'. */
19706 i = start;
19707 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19708 if (tail)
19709 x_reached = tail->x + tail->background_width;
19710 else
19711 x_reached = x;
19713 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19714 the row, redraw some glyphs in front or following the glyph
19715 strings built above. */
19716 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19718 int dummy_x = 0;
19719 struct glyph_string *h, *t;
19721 /* Compute overhangs for all glyph strings. */
19722 if (rif->compute_glyph_string_overhangs)
19723 for (s = head; s; s = s->next)
19724 rif->compute_glyph_string_overhangs (s);
19726 /* Prepend glyph strings for glyphs in front of the first glyph
19727 string that are overwritten because of the first glyph
19728 string's left overhang. The background of all strings
19729 prepended must be drawn because the first glyph string
19730 draws over it. */
19731 i = left_overwritten (head);
19732 if (i >= 0)
19734 j = i;
19735 BUILD_GLYPH_STRINGS (j, start, h, t,
19736 DRAW_NORMAL_TEXT, dummy_x, last_x);
19737 start = i;
19738 compute_overhangs_and_x (t, head->x, 1);
19739 prepend_glyph_string_lists (&head, &tail, h, t);
19740 clip_head = head;
19743 /* Prepend glyph strings for glyphs in front of the first glyph
19744 string that overwrite that glyph string because of their
19745 right overhang. For these strings, only the foreground must
19746 be drawn, because it draws over the glyph string at `head'.
19747 The background must not be drawn because this would overwrite
19748 right overhangs of preceding glyphs for which no glyph
19749 strings exist. */
19750 i = left_overwriting (head);
19751 if (i >= 0)
19753 clip_head = head;
19754 BUILD_GLYPH_STRINGS (i, start, h, t,
19755 DRAW_NORMAL_TEXT, dummy_x, last_x);
19756 for (s = h; s; s = s->next)
19757 s->background_filled_p = 1;
19758 compute_overhangs_and_x (t, head->x, 1);
19759 prepend_glyph_string_lists (&head, &tail, h, t);
19762 /* Append glyphs strings for glyphs following the last glyph
19763 string tail that are overwritten by tail. The background of
19764 these strings has to be drawn because tail's foreground draws
19765 over it. */
19766 i = right_overwritten (tail);
19767 if (i >= 0)
19769 BUILD_GLYPH_STRINGS (end, i, h, t,
19770 DRAW_NORMAL_TEXT, x, last_x);
19771 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19772 append_glyph_string_lists (&head, &tail, h, t);
19773 clip_tail = tail;
19776 /* Append glyph strings for glyphs following the last glyph
19777 string tail that overwrite tail. The foreground of such
19778 glyphs has to be drawn because it writes into the background
19779 of tail. The background must not be drawn because it could
19780 paint over the foreground of following glyphs. */
19781 i = right_overwriting (tail);
19782 if (i >= 0)
19784 clip_tail = tail;
19785 BUILD_GLYPH_STRINGS (end, i, h, t,
19786 DRAW_NORMAL_TEXT, x, last_x);
19787 for (s = h; s; s = s->next)
19788 s->background_filled_p = 1;
19789 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19790 append_glyph_string_lists (&head, &tail, h, t);
19792 if (clip_head || clip_tail)
19793 for (s = head; s; s = s->next)
19795 s->clip_head = clip_head;
19796 s->clip_tail = clip_tail;
19800 /* Draw all strings. */
19801 for (s = head; s; s = s->next)
19802 rif->draw_glyph_string (s);
19804 if (area == TEXT_AREA
19805 && !row->full_width_p
19806 /* When drawing overlapping rows, only the glyph strings'
19807 foreground is drawn, which doesn't erase a cursor
19808 completely. */
19809 && !overlaps)
19811 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19812 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19813 : (tail ? tail->x + tail->background_width : x));
19815 int text_left = window_box_left (w, TEXT_AREA);
19816 x0 -= text_left;
19817 x1 -= text_left;
19819 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19820 row->y, MATRIX_ROW_BOTTOM_Y (row));
19823 /* Value is the x-position up to which drawn, relative to AREA of W.
19824 This doesn't include parts drawn because of overhangs. */
19825 if (row->full_width_p)
19826 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19827 else
19828 x_reached -= window_box_left (w, area);
19830 RELEASE_HDC (hdc, f);
19832 return x_reached;
19835 /* Expand row matrix if too narrow. Don't expand if area
19836 is not present. */
19838 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19840 if (!fonts_changed_p \
19841 && (it->glyph_row->glyphs[area] \
19842 < it->glyph_row->glyphs[area + 1])) \
19844 it->w->ncols_scale_factor++; \
19845 fonts_changed_p = 1; \
19849 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19850 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19852 static INLINE void
19853 append_glyph (it)
19854 struct it *it;
19856 struct glyph *glyph;
19857 enum glyph_row_area area = it->area;
19859 xassert (it->glyph_row);
19860 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19862 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19863 if (glyph < it->glyph_row->glyphs[area + 1])
19865 glyph->charpos = CHARPOS (it->position);
19866 glyph->object = it->object;
19867 glyph->pixel_width = it->pixel_width;
19868 glyph->ascent = it->ascent;
19869 glyph->descent = it->descent;
19870 glyph->voffset = it->voffset;
19871 glyph->type = CHAR_GLYPH;
19872 glyph->multibyte_p = it->multibyte_p;
19873 glyph->left_box_line_p = it->start_of_box_run_p;
19874 glyph->right_box_line_p = it->end_of_box_run_p;
19875 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19876 || it->phys_descent > it->descent);
19877 glyph->padding_p = 0;
19878 glyph->glyph_not_available_p = it->glyph_not_available_p;
19879 glyph->face_id = it->face_id;
19880 glyph->u.ch = it->char_to_display;
19881 glyph->slice = null_glyph_slice;
19882 glyph->font_type = FONT_TYPE_UNKNOWN;
19883 ++it->glyph_row->used[area];
19885 else
19886 IT_EXPAND_MATRIX_WIDTH (it, area);
19889 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19890 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19892 static INLINE void
19893 append_composite_glyph (it)
19894 struct it *it;
19896 struct glyph *glyph;
19897 enum glyph_row_area area = it->area;
19899 xassert (it->glyph_row);
19901 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19902 if (glyph < it->glyph_row->glyphs[area + 1])
19904 glyph->charpos = CHARPOS (it->position);
19905 glyph->object = it->object;
19906 glyph->pixel_width = it->pixel_width;
19907 glyph->ascent = it->ascent;
19908 glyph->descent = it->descent;
19909 glyph->voffset = it->voffset;
19910 glyph->type = COMPOSITE_GLYPH;
19911 glyph->multibyte_p = it->multibyte_p;
19912 glyph->left_box_line_p = it->start_of_box_run_p;
19913 glyph->right_box_line_p = it->end_of_box_run_p;
19914 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19915 || it->phys_descent > it->descent);
19916 glyph->padding_p = 0;
19917 glyph->glyph_not_available_p = 0;
19918 glyph->face_id = it->face_id;
19919 glyph->u.cmp_id = it->cmp_id;
19920 glyph->slice = null_glyph_slice;
19921 glyph->font_type = FONT_TYPE_UNKNOWN;
19922 ++it->glyph_row->used[area];
19924 else
19925 IT_EXPAND_MATRIX_WIDTH (it, area);
19929 /* Change IT->ascent and IT->height according to the setting of
19930 IT->voffset. */
19932 static INLINE void
19933 take_vertical_position_into_account (it)
19934 struct it *it;
19936 if (it->voffset)
19938 if (it->voffset < 0)
19939 /* Increase the ascent so that we can display the text higher
19940 in the line. */
19941 it->ascent -= it->voffset;
19942 else
19943 /* Increase the descent so that we can display the text lower
19944 in the line. */
19945 it->descent += it->voffset;
19950 /* Produce glyphs/get display metrics for the image IT is loaded with.
19951 See the description of struct display_iterator in dispextern.h for
19952 an overview of struct display_iterator. */
19954 static void
19955 produce_image_glyph (it)
19956 struct it *it;
19958 struct image *img;
19959 struct face *face;
19960 int glyph_ascent, crop;
19961 struct glyph_slice slice;
19963 xassert (it->what == IT_IMAGE);
19965 face = FACE_FROM_ID (it->f, it->face_id);
19966 xassert (face);
19967 /* Make sure X resources of the face is loaded. */
19968 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19970 if (it->image_id < 0)
19972 /* Fringe bitmap. */
19973 it->ascent = it->phys_ascent = 0;
19974 it->descent = it->phys_descent = 0;
19975 it->pixel_width = 0;
19976 it->nglyphs = 0;
19977 return;
19980 img = IMAGE_FROM_ID (it->f, it->image_id);
19981 xassert (img);
19982 /* Make sure X resources of the image is loaded. */
19983 prepare_image_for_display (it->f, img);
19985 slice.x = slice.y = 0;
19986 slice.width = img->width;
19987 slice.height = img->height;
19989 if (INTEGERP (it->slice.x))
19990 slice.x = XINT (it->slice.x);
19991 else if (FLOATP (it->slice.x))
19992 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19994 if (INTEGERP (it->slice.y))
19995 slice.y = XINT (it->slice.y);
19996 else if (FLOATP (it->slice.y))
19997 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19999 if (INTEGERP (it->slice.width))
20000 slice.width = XINT (it->slice.width);
20001 else if (FLOATP (it->slice.width))
20002 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20004 if (INTEGERP (it->slice.height))
20005 slice.height = XINT (it->slice.height);
20006 else if (FLOATP (it->slice.height))
20007 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20009 if (slice.x >= img->width)
20010 slice.x = img->width;
20011 if (slice.y >= img->height)
20012 slice.y = img->height;
20013 if (slice.x + slice.width >= img->width)
20014 slice.width = img->width - slice.x;
20015 if (slice.y + slice.height > img->height)
20016 slice.height = img->height - slice.y;
20018 if (slice.width == 0 || slice.height == 0)
20019 return;
20021 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20023 it->descent = slice.height - glyph_ascent;
20024 if (slice.y == 0)
20025 it->descent += img->vmargin;
20026 if (slice.y + slice.height == img->height)
20027 it->descent += img->vmargin;
20028 it->phys_descent = it->descent;
20030 it->pixel_width = slice.width;
20031 if (slice.x == 0)
20032 it->pixel_width += img->hmargin;
20033 if (slice.x + slice.width == img->width)
20034 it->pixel_width += img->hmargin;
20036 /* It's quite possible for images to have an ascent greater than
20037 their height, so don't get confused in that case. */
20038 if (it->descent < 0)
20039 it->descent = 0;
20041 #if 0 /* this breaks image tiling */
20042 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20043 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20044 if (face_ascent > it->ascent)
20045 it->ascent = it->phys_ascent = face_ascent;
20046 #endif
20048 it->nglyphs = 1;
20050 if (face->box != FACE_NO_BOX)
20052 if (face->box_line_width > 0)
20054 if (slice.y == 0)
20055 it->ascent += face->box_line_width;
20056 if (slice.y + slice.height == img->height)
20057 it->descent += face->box_line_width;
20060 if (it->start_of_box_run_p && slice.x == 0)
20061 it->pixel_width += abs (face->box_line_width);
20062 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20063 it->pixel_width += abs (face->box_line_width);
20066 take_vertical_position_into_account (it);
20068 /* Automatically crop wide image glyphs at right edge so we can
20069 draw the cursor on same display row. */
20070 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20071 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20073 it->pixel_width -= crop;
20074 slice.width -= crop;
20077 if (it->glyph_row)
20079 struct glyph *glyph;
20080 enum glyph_row_area area = it->area;
20082 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20083 if (glyph < it->glyph_row->glyphs[area + 1])
20085 glyph->charpos = CHARPOS (it->position);
20086 glyph->object = it->object;
20087 glyph->pixel_width = it->pixel_width;
20088 glyph->ascent = glyph_ascent;
20089 glyph->descent = it->descent;
20090 glyph->voffset = it->voffset;
20091 glyph->type = IMAGE_GLYPH;
20092 glyph->multibyte_p = it->multibyte_p;
20093 glyph->left_box_line_p = it->start_of_box_run_p;
20094 glyph->right_box_line_p = it->end_of_box_run_p;
20095 glyph->overlaps_vertically_p = 0;
20096 glyph->padding_p = 0;
20097 glyph->glyph_not_available_p = 0;
20098 glyph->face_id = it->face_id;
20099 glyph->u.img_id = img->id;
20100 glyph->slice = slice;
20101 glyph->font_type = FONT_TYPE_UNKNOWN;
20102 ++it->glyph_row->used[area];
20104 else
20105 IT_EXPAND_MATRIX_WIDTH (it, area);
20110 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20111 of the glyph, WIDTH and HEIGHT are the width and height of the
20112 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20114 static void
20115 append_stretch_glyph (it, object, width, height, ascent)
20116 struct it *it;
20117 Lisp_Object object;
20118 int width, height;
20119 int ascent;
20121 struct glyph *glyph;
20122 enum glyph_row_area area = it->area;
20124 xassert (ascent >= 0 && ascent <= height);
20126 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20127 if (glyph < it->glyph_row->glyphs[area + 1])
20129 glyph->charpos = CHARPOS (it->position);
20130 glyph->object = object;
20131 glyph->pixel_width = width;
20132 glyph->ascent = ascent;
20133 glyph->descent = height - ascent;
20134 glyph->voffset = it->voffset;
20135 glyph->type = STRETCH_GLYPH;
20136 glyph->multibyte_p = it->multibyte_p;
20137 glyph->left_box_line_p = it->start_of_box_run_p;
20138 glyph->right_box_line_p = it->end_of_box_run_p;
20139 glyph->overlaps_vertically_p = 0;
20140 glyph->padding_p = 0;
20141 glyph->glyph_not_available_p = 0;
20142 glyph->face_id = it->face_id;
20143 glyph->u.stretch.ascent = ascent;
20144 glyph->u.stretch.height = height;
20145 glyph->slice = null_glyph_slice;
20146 glyph->font_type = FONT_TYPE_UNKNOWN;
20147 ++it->glyph_row->used[area];
20149 else
20150 IT_EXPAND_MATRIX_WIDTH (it, area);
20154 /* Produce a stretch glyph for iterator IT. IT->object is the value
20155 of the glyph property displayed. The value must be a list
20156 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20157 being recognized:
20159 1. `:width WIDTH' specifies that the space should be WIDTH *
20160 canonical char width wide. WIDTH may be an integer or floating
20161 point number.
20163 2. `:relative-width FACTOR' specifies that the width of the stretch
20164 should be computed from the width of the first character having the
20165 `glyph' property, and should be FACTOR times that width.
20167 3. `:align-to HPOS' specifies that the space should be wide enough
20168 to reach HPOS, a value in canonical character units.
20170 Exactly one of the above pairs must be present.
20172 4. `:height HEIGHT' specifies that the height of the stretch produced
20173 should be HEIGHT, measured in canonical character units.
20175 5. `:relative-height FACTOR' specifies that the height of the
20176 stretch should be FACTOR times the height of the characters having
20177 the glyph property.
20179 Either none or exactly one of 4 or 5 must be present.
20181 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20182 of the stretch should be used for the ascent of the stretch.
20183 ASCENT must be in the range 0 <= ASCENT <= 100. */
20185 static void
20186 produce_stretch_glyph (it)
20187 struct it *it;
20189 /* (space :width WIDTH :height HEIGHT ...) */
20190 Lisp_Object prop, plist;
20191 int width = 0, height = 0, align_to = -1;
20192 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20193 int ascent = 0;
20194 double tem;
20195 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20196 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20198 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20200 /* List should start with `space'. */
20201 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20202 plist = XCDR (it->object);
20204 /* Compute the width of the stretch. */
20205 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20206 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20208 /* Absolute width `:width WIDTH' specified and valid. */
20209 zero_width_ok_p = 1;
20210 width = (int)tem;
20212 else if (prop = Fplist_get (plist, QCrelative_width),
20213 NUMVAL (prop) > 0)
20215 /* Relative width `:relative-width FACTOR' specified and valid.
20216 Compute the width of the characters having the `glyph'
20217 property. */
20218 struct it it2;
20219 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20221 it2 = *it;
20222 if (it->multibyte_p)
20224 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20225 - IT_BYTEPOS (*it));
20226 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20228 else
20229 it2.c = *p, it2.len = 1;
20231 it2.glyph_row = NULL;
20232 it2.what = IT_CHARACTER;
20233 x_produce_glyphs (&it2);
20234 width = NUMVAL (prop) * it2.pixel_width;
20236 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20237 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20239 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20240 align_to = (align_to < 0
20242 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20243 else if (align_to < 0)
20244 align_to = window_box_left_offset (it->w, TEXT_AREA);
20245 width = max (0, (int)tem + align_to - it->current_x);
20246 zero_width_ok_p = 1;
20248 else
20249 /* Nothing specified -> width defaults to canonical char width. */
20250 width = FRAME_COLUMN_WIDTH (it->f);
20252 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20253 width = 1;
20255 /* Compute height. */
20256 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20257 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20259 height = (int)tem;
20260 zero_height_ok_p = 1;
20262 else if (prop = Fplist_get (plist, QCrelative_height),
20263 NUMVAL (prop) > 0)
20264 height = FONT_HEIGHT (font) * NUMVAL (prop);
20265 else
20266 height = FONT_HEIGHT (font);
20268 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20269 height = 1;
20271 /* Compute percentage of height used for ascent. If
20272 `:ascent ASCENT' is present and valid, use that. Otherwise,
20273 derive the ascent from the font in use. */
20274 if (prop = Fplist_get (plist, QCascent),
20275 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20276 ascent = height * NUMVAL (prop) / 100.0;
20277 else if (!NILP (prop)
20278 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20279 ascent = min (max (0, (int)tem), height);
20280 else
20281 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20283 if (width > 0 && !it->truncate_lines_p
20284 && it->current_x + width > it->last_visible_x)
20285 width = it->last_visible_x - it->current_x - 1;
20287 if (width > 0 && height > 0 && it->glyph_row)
20289 Lisp_Object object = it->stack[it->sp - 1].string;
20290 if (!STRINGP (object))
20291 object = it->w->buffer;
20292 append_stretch_glyph (it, object, width, height, ascent);
20295 it->pixel_width = width;
20296 it->ascent = it->phys_ascent = ascent;
20297 it->descent = it->phys_descent = height - it->ascent;
20298 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20300 take_vertical_position_into_account (it);
20303 /* Get line-height and line-spacing property at point.
20304 If line-height has format (HEIGHT TOTAL), return TOTAL
20305 in TOTAL_HEIGHT. */
20307 static Lisp_Object
20308 get_line_height_property (it, prop)
20309 struct it *it;
20310 Lisp_Object prop;
20312 Lisp_Object position;
20314 if (STRINGP (it->object))
20315 position = make_number (IT_STRING_CHARPOS (*it));
20316 else if (BUFFERP (it->object))
20317 position = make_number (IT_CHARPOS (*it));
20318 else
20319 return Qnil;
20321 return Fget_char_property (position, prop, it->object);
20324 /* Calculate line-height and line-spacing properties.
20325 An integer value specifies explicit pixel value.
20326 A float value specifies relative value to current face height.
20327 A cons (float . face-name) specifies relative value to
20328 height of specified face font.
20330 Returns height in pixels, or nil. */
20333 static Lisp_Object
20334 calc_line_height_property (it, val, font, boff, override)
20335 struct it *it;
20336 Lisp_Object val;
20337 XFontStruct *font;
20338 int boff, override;
20340 Lisp_Object face_name = Qnil;
20341 int ascent, descent, height;
20343 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20344 return val;
20346 if (CONSP (val))
20348 face_name = XCAR (val);
20349 val = XCDR (val);
20350 if (!NUMBERP (val))
20351 val = make_number (1);
20352 if (NILP (face_name))
20354 height = it->ascent + it->descent;
20355 goto scale;
20359 if (NILP (face_name))
20361 font = FRAME_FONT (it->f);
20362 boff = FRAME_BASELINE_OFFSET (it->f);
20364 else if (EQ (face_name, Qt))
20366 override = 0;
20368 else
20370 int face_id;
20371 struct face *face;
20372 struct font_info *font_info;
20374 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20375 if (face_id < 0)
20376 return make_number (-1);
20378 face = FACE_FROM_ID (it->f, face_id);
20379 font = face->font;
20380 if (font == NULL)
20381 return make_number (-1);
20383 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20384 boff = font_info->baseline_offset;
20385 if (font_info->vertical_centering)
20386 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20389 ascent = FONT_BASE (font) + boff;
20390 descent = FONT_DESCENT (font) - boff;
20392 if (override)
20394 it->override_ascent = ascent;
20395 it->override_descent = descent;
20396 it->override_boff = boff;
20399 height = ascent + descent;
20401 scale:
20402 if (FLOATP (val))
20403 height = (int)(XFLOAT_DATA (val) * height);
20404 else if (INTEGERP (val))
20405 height *= XINT (val);
20407 return make_number (height);
20411 /* RIF:
20412 Produce glyphs/get display metrics for the display element IT is
20413 loaded with. See the description of struct it in dispextern.h
20414 for an overview of struct it. */
20416 void
20417 x_produce_glyphs (it)
20418 struct it *it;
20420 int extra_line_spacing = it->extra_line_spacing;
20422 it->glyph_not_available_p = 0;
20424 if (it->what == IT_CHARACTER)
20426 XChar2b char2b;
20427 XFontStruct *font;
20428 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20429 XCharStruct *pcm;
20430 int font_not_found_p;
20431 struct font_info *font_info;
20432 int boff; /* baseline offset */
20433 /* We may change it->multibyte_p upon unibyte<->multibyte
20434 conversion. So, save the current value now and restore it
20435 later.
20437 Note: It seems that we don't have to record multibyte_p in
20438 struct glyph because the character code itself tells if or
20439 not the character is multibyte. Thus, in the future, we must
20440 consider eliminating the field `multibyte_p' in the struct
20441 glyph. */
20442 int saved_multibyte_p = it->multibyte_p;
20444 /* Maybe translate single-byte characters to multibyte, or the
20445 other way. */
20446 it->char_to_display = it->c;
20447 if (!ASCII_BYTE_P (it->c))
20449 if (unibyte_display_via_language_environment
20450 && SINGLE_BYTE_CHAR_P (it->c)
20451 && (it->c >= 0240
20452 || !NILP (Vnonascii_translation_table)))
20454 it->char_to_display = unibyte_char_to_multibyte (it->c);
20455 it->multibyte_p = 1;
20456 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20457 face = FACE_FROM_ID (it->f, it->face_id);
20459 else if (!SINGLE_BYTE_CHAR_P (it->c)
20460 && !it->multibyte_p)
20462 it->multibyte_p = 1;
20463 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20464 face = FACE_FROM_ID (it->f, it->face_id);
20468 /* Get font to use. Encode IT->char_to_display. */
20469 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20470 &char2b, it->multibyte_p, 0);
20471 font = face->font;
20473 /* When no suitable font found, use the default font. */
20474 font_not_found_p = font == NULL;
20475 if (font_not_found_p)
20477 font = FRAME_FONT (it->f);
20478 boff = FRAME_BASELINE_OFFSET (it->f);
20479 font_info = NULL;
20481 else
20483 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20484 boff = font_info->baseline_offset;
20485 if (font_info->vertical_centering)
20486 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20489 if (it->char_to_display >= ' '
20490 && (!it->multibyte_p || it->char_to_display < 128))
20492 /* Either unibyte or ASCII. */
20493 int stretched_p;
20495 it->nglyphs = 1;
20497 pcm = rif->per_char_metric (font, &char2b,
20498 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20500 if (it->override_ascent >= 0)
20502 it->ascent = it->override_ascent;
20503 it->descent = it->override_descent;
20504 boff = it->override_boff;
20506 else
20508 it->ascent = FONT_BASE (font) + boff;
20509 it->descent = FONT_DESCENT (font) - boff;
20512 if (pcm)
20514 it->phys_ascent = pcm->ascent + boff;
20515 it->phys_descent = pcm->descent - boff;
20516 it->pixel_width = pcm->width;
20518 else
20520 it->glyph_not_available_p = 1;
20521 it->phys_ascent = it->ascent;
20522 it->phys_descent = it->descent;
20523 it->pixel_width = FONT_WIDTH (font);
20526 if (it->constrain_row_ascent_descent_p)
20528 if (it->descent > it->max_descent)
20530 it->ascent += it->descent - it->max_descent;
20531 it->descent = it->max_descent;
20533 if (it->ascent > it->max_ascent)
20535 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20536 it->ascent = it->max_ascent;
20538 it->phys_ascent = min (it->phys_ascent, it->ascent);
20539 it->phys_descent = min (it->phys_descent, it->descent);
20540 extra_line_spacing = 0;
20543 /* If this is a space inside a region of text with
20544 `space-width' property, change its width. */
20545 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20546 if (stretched_p)
20547 it->pixel_width *= XFLOATINT (it->space_width);
20549 /* If face has a box, add the box thickness to the character
20550 height. If character has a box line to the left and/or
20551 right, add the box line width to the character's width. */
20552 if (face->box != FACE_NO_BOX)
20554 int thick = face->box_line_width;
20556 if (thick > 0)
20558 it->ascent += thick;
20559 it->descent += thick;
20561 else
20562 thick = -thick;
20564 if (it->start_of_box_run_p)
20565 it->pixel_width += thick;
20566 if (it->end_of_box_run_p)
20567 it->pixel_width += thick;
20570 /* If face has an overline, add the height of the overline
20571 (1 pixel) and a 1 pixel margin to the character height. */
20572 if (face->overline_p)
20573 it->ascent += overline_margin;
20575 if (it->constrain_row_ascent_descent_p)
20577 if (it->ascent > it->max_ascent)
20578 it->ascent = it->max_ascent;
20579 if (it->descent > it->max_descent)
20580 it->descent = it->max_descent;
20583 take_vertical_position_into_account (it);
20585 /* If we have to actually produce glyphs, do it. */
20586 if (it->glyph_row)
20588 if (stretched_p)
20590 /* Translate a space with a `space-width' property
20591 into a stretch glyph. */
20592 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20593 / FONT_HEIGHT (font));
20594 append_stretch_glyph (it, it->object, it->pixel_width,
20595 it->ascent + it->descent, ascent);
20597 else
20598 append_glyph (it);
20600 /* If characters with lbearing or rbearing are displayed
20601 in this line, record that fact in a flag of the
20602 glyph row. This is used to optimize X output code. */
20603 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20604 it->glyph_row->contains_overlapping_glyphs_p = 1;
20607 else if (it->char_to_display == '\n')
20609 /* A newline has no width but we need the height of the line.
20610 But if previous part of the line set a height, don't
20611 increase that height */
20613 Lisp_Object height;
20614 Lisp_Object total_height = Qnil;
20616 it->override_ascent = -1;
20617 it->pixel_width = 0;
20618 it->nglyphs = 0;
20620 height = get_line_height_property(it, Qline_height);
20621 /* Split (line-height total-height) list */
20622 if (CONSP (height)
20623 && CONSP (XCDR (height))
20624 && NILP (XCDR (XCDR (height))))
20626 total_height = XCAR (XCDR (height));
20627 height = XCAR (height);
20629 height = calc_line_height_property(it, height, font, boff, 1);
20631 if (it->override_ascent >= 0)
20633 it->ascent = it->override_ascent;
20634 it->descent = it->override_descent;
20635 boff = it->override_boff;
20637 else
20639 it->ascent = FONT_BASE (font) + boff;
20640 it->descent = FONT_DESCENT (font) - boff;
20643 if (EQ (height, Qt))
20645 if (it->descent > it->max_descent)
20647 it->ascent += it->descent - it->max_descent;
20648 it->descent = it->max_descent;
20650 if (it->ascent > it->max_ascent)
20652 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20653 it->ascent = it->max_ascent;
20655 it->phys_ascent = min (it->phys_ascent, it->ascent);
20656 it->phys_descent = min (it->phys_descent, it->descent);
20657 it->constrain_row_ascent_descent_p = 1;
20658 extra_line_spacing = 0;
20660 else
20662 Lisp_Object spacing;
20664 it->phys_ascent = it->ascent;
20665 it->phys_descent = it->descent;
20667 if ((it->max_ascent > 0 || it->max_descent > 0)
20668 && face->box != FACE_NO_BOX
20669 && face->box_line_width > 0)
20671 it->ascent += face->box_line_width;
20672 it->descent += face->box_line_width;
20674 if (!NILP (height)
20675 && XINT (height) > it->ascent + it->descent)
20676 it->ascent = XINT (height) - it->descent;
20678 if (!NILP (total_height))
20679 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20680 else
20682 spacing = get_line_height_property(it, Qline_spacing);
20683 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20685 if (INTEGERP (spacing))
20687 extra_line_spacing = XINT (spacing);
20688 if (!NILP (total_height))
20689 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20693 else if (it->char_to_display == '\t')
20695 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20696 int x = it->current_x + it->continuation_lines_width;
20697 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20699 /* If the distance from the current position to the next tab
20700 stop is less than a space character width, use the
20701 tab stop after that. */
20702 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20703 next_tab_x += tab_width;
20705 it->pixel_width = next_tab_x - x;
20706 it->nglyphs = 1;
20707 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20708 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20710 if (it->glyph_row)
20712 append_stretch_glyph (it, it->object, it->pixel_width,
20713 it->ascent + it->descent, it->ascent);
20716 else
20718 /* A multi-byte character. Assume that the display width of the
20719 character is the width of the character multiplied by the
20720 width of the font. */
20722 /* If we found a font, this font should give us the right
20723 metrics. If we didn't find a font, use the frame's
20724 default font and calculate the width of the character
20725 from the charset width; this is what old redisplay code
20726 did. */
20728 pcm = rif->per_char_metric (font, &char2b,
20729 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20731 if (font_not_found_p || !pcm)
20733 int charset = CHAR_CHARSET (it->char_to_display);
20735 it->glyph_not_available_p = 1;
20736 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20737 * CHARSET_WIDTH (charset));
20738 it->phys_ascent = FONT_BASE (font) + boff;
20739 it->phys_descent = FONT_DESCENT (font) - boff;
20741 else
20743 it->pixel_width = pcm->width;
20744 it->phys_ascent = pcm->ascent + boff;
20745 it->phys_descent = pcm->descent - boff;
20746 if (it->glyph_row
20747 && (pcm->lbearing < 0
20748 || pcm->rbearing > pcm->width))
20749 it->glyph_row->contains_overlapping_glyphs_p = 1;
20751 it->nglyphs = 1;
20752 it->ascent = FONT_BASE (font) + boff;
20753 it->descent = FONT_DESCENT (font) - boff;
20754 if (face->box != FACE_NO_BOX)
20756 int thick = face->box_line_width;
20758 if (thick > 0)
20760 it->ascent += thick;
20761 it->descent += thick;
20763 else
20764 thick = - thick;
20766 if (it->start_of_box_run_p)
20767 it->pixel_width += thick;
20768 if (it->end_of_box_run_p)
20769 it->pixel_width += thick;
20772 /* If face has an overline, add the height of the overline
20773 (1 pixel) and a 1 pixel margin to the character height. */
20774 if (face->overline_p)
20775 it->ascent += overline_margin;
20777 take_vertical_position_into_account (it);
20779 if (it->glyph_row)
20780 append_glyph (it);
20782 it->multibyte_p = saved_multibyte_p;
20784 else if (it->what == IT_COMPOSITION)
20786 /* Note: A composition is represented as one glyph in the
20787 glyph matrix. There are no padding glyphs. */
20788 XChar2b char2b;
20789 XFontStruct *font;
20790 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20791 XCharStruct *pcm;
20792 int font_not_found_p;
20793 struct font_info *font_info;
20794 int boff; /* baseline offset */
20795 struct composition *cmp = composition_table[it->cmp_id];
20797 /* Maybe translate single-byte characters to multibyte. */
20798 it->char_to_display = it->c;
20799 if (unibyte_display_via_language_environment
20800 && SINGLE_BYTE_CHAR_P (it->c)
20801 && (it->c >= 0240
20802 || (it->c >= 0200
20803 && !NILP (Vnonascii_translation_table))))
20805 it->char_to_display = unibyte_char_to_multibyte (it->c);
20808 /* Get face and font to use. Encode IT->char_to_display. */
20809 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20810 face = FACE_FROM_ID (it->f, it->face_id);
20811 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20812 &char2b, it->multibyte_p, 0);
20813 font = face->font;
20815 /* When no suitable font found, use the default font. */
20816 font_not_found_p = font == NULL;
20817 if (font_not_found_p)
20819 font = FRAME_FONT (it->f);
20820 boff = FRAME_BASELINE_OFFSET (it->f);
20821 font_info = NULL;
20823 else
20825 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20826 boff = font_info->baseline_offset;
20827 if (font_info->vertical_centering)
20828 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20831 /* There are no padding glyphs, so there is only one glyph to
20832 produce for the composition. Important is that pixel_width,
20833 ascent and descent are the values of what is drawn by
20834 draw_glyphs (i.e. the values of the overall glyphs composed). */
20835 it->nglyphs = 1;
20837 /* If we have not yet calculated pixel size data of glyphs of
20838 the composition for the current face font, calculate them
20839 now. Theoretically, we have to check all fonts for the
20840 glyphs, but that requires much time and memory space. So,
20841 here we check only the font of the first glyph. This leads
20842 to incorrect display very rarely, and C-l (recenter) can
20843 correct the display anyway. */
20844 if (cmp->font != (void *) font)
20846 /* Ascent and descent of the font of the first character of
20847 this composition (adjusted by baseline offset). Ascent
20848 and descent of overall glyphs should not be less than
20849 them respectively. */
20850 int font_ascent = FONT_BASE (font) + boff;
20851 int font_descent = FONT_DESCENT (font) - boff;
20852 /* Bounding box of the overall glyphs. */
20853 int leftmost, rightmost, lowest, highest;
20854 int i, width, ascent, descent;
20856 cmp->font = (void *) font;
20858 /* Initialize the bounding box. */
20859 if (font_info
20860 && (pcm = rif->per_char_metric (font, &char2b,
20861 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20863 width = pcm->width;
20864 ascent = pcm->ascent;
20865 descent = pcm->descent;
20867 else
20869 width = FONT_WIDTH (font);
20870 ascent = FONT_BASE (font);
20871 descent = FONT_DESCENT (font);
20874 rightmost = width;
20875 lowest = - descent + boff;
20876 highest = ascent + boff;
20877 leftmost = 0;
20879 if (font_info
20880 && font_info->default_ascent
20881 && CHAR_TABLE_P (Vuse_default_ascent)
20882 && !NILP (Faref (Vuse_default_ascent,
20883 make_number (it->char_to_display))))
20884 highest = font_info->default_ascent + boff;
20886 /* Draw the first glyph at the normal position. It may be
20887 shifted to right later if some other glyphs are drawn at
20888 the left. */
20889 cmp->offsets[0] = 0;
20890 cmp->offsets[1] = boff;
20892 /* Set cmp->offsets for the remaining glyphs. */
20893 for (i = 1; i < cmp->glyph_len; i++)
20895 int left, right, btm, top;
20896 int ch = COMPOSITION_GLYPH (cmp, i);
20897 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20899 face = FACE_FROM_ID (it->f, face_id);
20900 get_char_face_and_encoding (it->f, ch, face->id,
20901 &char2b, it->multibyte_p, 0);
20902 font = face->font;
20903 if (font == NULL)
20905 font = FRAME_FONT (it->f);
20906 boff = FRAME_BASELINE_OFFSET (it->f);
20907 font_info = NULL;
20909 else
20911 font_info
20912 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20913 boff = font_info->baseline_offset;
20914 if (font_info->vertical_centering)
20915 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20918 if (font_info
20919 && (pcm = rif->per_char_metric (font, &char2b,
20920 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20922 width = pcm->width;
20923 ascent = pcm->ascent;
20924 descent = pcm->descent;
20926 else
20928 width = FONT_WIDTH (font);
20929 ascent = 1;
20930 descent = 0;
20933 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20935 /* Relative composition with or without
20936 alternate chars. */
20937 left = (leftmost + rightmost - width) / 2;
20938 btm = - descent + boff;
20939 if (font_info && font_info->relative_compose
20940 && (! CHAR_TABLE_P (Vignore_relative_composition)
20941 || NILP (Faref (Vignore_relative_composition,
20942 make_number (ch)))))
20945 if (- descent >= font_info->relative_compose)
20946 /* One extra pixel between two glyphs. */
20947 btm = highest + 1;
20948 else if (ascent <= 0)
20949 /* One extra pixel between two glyphs. */
20950 btm = lowest - 1 - ascent - descent;
20953 else
20955 /* A composition rule is specified by an integer
20956 value that encodes global and new reference
20957 points (GREF and NREF). GREF and NREF are
20958 specified by numbers as below:
20960 0---1---2 -- ascent
20964 9--10--11 -- center
20966 ---3---4---5--- baseline
20968 6---7---8 -- descent
20970 int rule = COMPOSITION_RULE (cmp, i);
20971 int gref, nref, grefx, grefy, nrefx, nrefy;
20973 COMPOSITION_DECODE_RULE (rule, gref, nref);
20974 grefx = gref % 3, nrefx = nref % 3;
20975 grefy = gref / 3, nrefy = nref / 3;
20977 left = (leftmost
20978 + grefx * (rightmost - leftmost) / 2
20979 - nrefx * width / 2);
20980 btm = ((grefy == 0 ? highest
20981 : grefy == 1 ? 0
20982 : grefy == 2 ? lowest
20983 : (highest + lowest) / 2)
20984 - (nrefy == 0 ? ascent + descent
20985 : nrefy == 1 ? descent - boff
20986 : nrefy == 2 ? 0
20987 : (ascent + descent) / 2));
20990 cmp->offsets[i * 2] = left;
20991 cmp->offsets[i * 2 + 1] = btm + descent;
20993 /* Update the bounding box of the overall glyphs. */
20994 right = left + width;
20995 top = btm + descent + ascent;
20996 if (left < leftmost)
20997 leftmost = left;
20998 if (right > rightmost)
20999 rightmost = right;
21000 if (top > highest)
21001 highest = top;
21002 if (btm < lowest)
21003 lowest = btm;
21006 /* If there are glyphs whose x-offsets are negative,
21007 shift all glyphs to the right and make all x-offsets
21008 non-negative. */
21009 if (leftmost < 0)
21011 for (i = 0; i < cmp->glyph_len; i++)
21012 cmp->offsets[i * 2] -= leftmost;
21013 rightmost -= leftmost;
21016 cmp->pixel_width = rightmost;
21017 cmp->ascent = highest;
21018 cmp->descent = - lowest;
21019 if (cmp->ascent < font_ascent)
21020 cmp->ascent = font_ascent;
21021 if (cmp->descent < font_descent)
21022 cmp->descent = font_descent;
21025 it->pixel_width = cmp->pixel_width;
21026 it->ascent = it->phys_ascent = cmp->ascent;
21027 it->descent = it->phys_descent = cmp->descent;
21029 if (face->box != FACE_NO_BOX)
21031 int thick = face->box_line_width;
21033 if (thick > 0)
21035 it->ascent += thick;
21036 it->descent += thick;
21038 else
21039 thick = - thick;
21041 if (it->start_of_box_run_p)
21042 it->pixel_width += thick;
21043 if (it->end_of_box_run_p)
21044 it->pixel_width += thick;
21047 /* If face has an overline, add the height of the overline
21048 (1 pixel) and a 1 pixel margin to the character height. */
21049 if (face->overline_p)
21050 it->ascent += overline_margin;
21052 take_vertical_position_into_account (it);
21054 if (it->glyph_row)
21055 append_composite_glyph (it);
21057 else if (it->what == IT_IMAGE)
21058 produce_image_glyph (it);
21059 else if (it->what == IT_STRETCH)
21060 produce_stretch_glyph (it);
21062 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21063 because this isn't true for images with `:ascent 100'. */
21064 xassert (it->ascent >= 0 && it->descent >= 0);
21065 if (it->area == TEXT_AREA)
21066 it->current_x += it->pixel_width;
21068 if (extra_line_spacing > 0)
21070 it->descent += extra_line_spacing;
21071 if (extra_line_spacing > it->max_extra_line_spacing)
21072 it->max_extra_line_spacing = extra_line_spacing;
21075 it->max_ascent = max (it->max_ascent, it->ascent);
21076 it->max_descent = max (it->max_descent, it->descent);
21077 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21078 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21081 /* EXPORT for RIF:
21082 Output LEN glyphs starting at START at the nominal cursor position.
21083 Advance the nominal cursor over the text. The global variable
21084 updated_window contains the window being updated, updated_row is
21085 the glyph row being updated, and updated_area is the area of that
21086 row being updated. */
21088 void
21089 x_write_glyphs (start, len)
21090 struct glyph *start;
21091 int len;
21093 int x, hpos;
21095 xassert (updated_window && updated_row);
21096 BLOCK_INPUT;
21098 /* Write glyphs. */
21100 hpos = start - updated_row->glyphs[updated_area];
21101 x = draw_glyphs (updated_window, output_cursor.x,
21102 updated_row, updated_area,
21103 hpos, hpos + len,
21104 DRAW_NORMAL_TEXT, 0);
21106 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21107 if (updated_area == TEXT_AREA
21108 && updated_window->phys_cursor_on_p
21109 && updated_window->phys_cursor.vpos == output_cursor.vpos
21110 && updated_window->phys_cursor.hpos >= hpos
21111 && updated_window->phys_cursor.hpos < hpos + len)
21112 updated_window->phys_cursor_on_p = 0;
21114 UNBLOCK_INPUT;
21116 /* Advance the output cursor. */
21117 output_cursor.hpos += len;
21118 output_cursor.x = x;
21122 /* EXPORT for RIF:
21123 Insert LEN glyphs from START at the nominal cursor position. */
21125 void
21126 x_insert_glyphs (start, len)
21127 struct glyph *start;
21128 int len;
21130 struct frame *f;
21131 struct window *w;
21132 int line_height, shift_by_width, shifted_region_width;
21133 struct glyph_row *row;
21134 struct glyph *glyph;
21135 int frame_x, frame_y, hpos;
21137 xassert (updated_window && updated_row);
21138 BLOCK_INPUT;
21139 w = updated_window;
21140 f = XFRAME (WINDOW_FRAME (w));
21142 /* Get the height of the line we are in. */
21143 row = updated_row;
21144 line_height = row->height;
21146 /* Get the width of the glyphs to insert. */
21147 shift_by_width = 0;
21148 for (glyph = start; glyph < start + len; ++glyph)
21149 shift_by_width += glyph->pixel_width;
21151 /* Get the width of the region to shift right. */
21152 shifted_region_width = (window_box_width (w, updated_area)
21153 - output_cursor.x
21154 - shift_by_width);
21156 /* Shift right. */
21157 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21158 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21160 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21161 line_height, shift_by_width);
21163 /* Write the glyphs. */
21164 hpos = start - row->glyphs[updated_area];
21165 draw_glyphs (w, output_cursor.x, row, updated_area,
21166 hpos, hpos + len,
21167 DRAW_NORMAL_TEXT, 0);
21169 /* Advance the output cursor. */
21170 output_cursor.hpos += len;
21171 output_cursor.x += shift_by_width;
21172 UNBLOCK_INPUT;
21176 /* EXPORT for RIF:
21177 Erase the current text line from the nominal cursor position
21178 (inclusive) to pixel column TO_X (exclusive). The idea is that
21179 everything from TO_X onward is already erased.
21181 TO_X is a pixel position relative to updated_area of
21182 updated_window. TO_X == -1 means clear to the end of this area. */
21184 void
21185 x_clear_end_of_line (to_x)
21186 int to_x;
21188 struct frame *f;
21189 struct window *w = updated_window;
21190 int max_x, min_y, max_y;
21191 int from_x, from_y, to_y;
21193 xassert (updated_window && updated_row);
21194 f = XFRAME (w->frame);
21196 if (updated_row->full_width_p)
21197 max_x = WINDOW_TOTAL_WIDTH (w);
21198 else
21199 max_x = window_box_width (w, updated_area);
21200 max_y = window_text_bottom_y (w);
21202 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21203 of window. For TO_X > 0, truncate to end of drawing area. */
21204 if (to_x == 0)
21205 return;
21206 else if (to_x < 0)
21207 to_x = max_x;
21208 else
21209 to_x = min (to_x, max_x);
21211 to_y = min (max_y, output_cursor.y + updated_row->height);
21213 /* Notice if the cursor will be cleared by this operation. */
21214 if (!updated_row->full_width_p)
21215 notice_overwritten_cursor (w, updated_area,
21216 output_cursor.x, -1,
21217 updated_row->y,
21218 MATRIX_ROW_BOTTOM_Y (updated_row));
21220 from_x = output_cursor.x;
21222 /* Translate to frame coordinates. */
21223 if (updated_row->full_width_p)
21225 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21226 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21228 else
21230 int area_left = window_box_left (w, updated_area);
21231 from_x += area_left;
21232 to_x += area_left;
21235 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21236 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21237 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21239 /* Prevent inadvertently clearing to end of the X window. */
21240 if (to_x > from_x && to_y > from_y)
21242 BLOCK_INPUT;
21243 rif->clear_frame_area (f, from_x, from_y,
21244 to_x - from_x, to_y - from_y);
21245 UNBLOCK_INPUT;
21249 #endif /* HAVE_WINDOW_SYSTEM */
21253 /***********************************************************************
21254 Cursor types
21255 ***********************************************************************/
21257 /* Value is the internal representation of the specified cursor type
21258 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21259 of the bar cursor. */
21261 static enum text_cursor_kinds
21262 get_specified_cursor_type (arg, width)
21263 Lisp_Object arg;
21264 int *width;
21266 enum text_cursor_kinds type;
21268 if (NILP (arg))
21269 return NO_CURSOR;
21271 if (EQ (arg, Qbox))
21272 return FILLED_BOX_CURSOR;
21274 if (EQ (arg, Qhollow))
21275 return HOLLOW_BOX_CURSOR;
21277 if (EQ (arg, Qbar))
21279 *width = 2;
21280 return BAR_CURSOR;
21283 if (CONSP (arg)
21284 && EQ (XCAR (arg), Qbar)
21285 && INTEGERP (XCDR (arg))
21286 && XINT (XCDR (arg)) >= 0)
21288 *width = XINT (XCDR (arg));
21289 return BAR_CURSOR;
21292 if (EQ (arg, Qhbar))
21294 *width = 2;
21295 return HBAR_CURSOR;
21298 if (CONSP (arg)
21299 && EQ (XCAR (arg), Qhbar)
21300 && INTEGERP (XCDR (arg))
21301 && XINT (XCDR (arg)) >= 0)
21303 *width = XINT (XCDR (arg));
21304 return HBAR_CURSOR;
21307 /* Treat anything unknown as "hollow box cursor".
21308 It was bad to signal an error; people have trouble fixing
21309 .Xdefaults with Emacs, when it has something bad in it. */
21310 type = HOLLOW_BOX_CURSOR;
21312 return type;
21315 /* Set the default cursor types for specified frame. */
21316 void
21317 set_frame_cursor_types (f, arg)
21318 struct frame *f;
21319 Lisp_Object arg;
21321 int width;
21322 Lisp_Object tem;
21324 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21325 FRAME_CURSOR_WIDTH (f) = width;
21327 /* By default, set up the blink-off state depending on the on-state. */
21329 tem = Fassoc (arg, Vblink_cursor_alist);
21330 if (!NILP (tem))
21332 FRAME_BLINK_OFF_CURSOR (f)
21333 = get_specified_cursor_type (XCDR (tem), &width);
21334 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21336 else
21337 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21341 /* Return the cursor we want to be displayed in window W. Return
21342 width of bar/hbar cursor through WIDTH arg. Return with
21343 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21344 (i.e. if the `system caret' should track this cursor).
21346 In a mini-buffer window, we want the cursor only to appear if we
21347 are reading input from this window. For the selected window, we
21348 want the cursor type given by the frame parameter or buffer local
21349 setting of cursor-type. If explicitly marked off, draw no cursor.
21350 In all other cases, we want a hollow box cursor. */
21352 static enum text_cursor_kinds
21353 get_window_cursor_type (w, glyph, width, active_cursor)
21354 struct window *w;
21355 struct glyph *glyph;
21356 int *width;
21357 int *active_cursor;
21359 struct frame *f = XFRAME (w->frame);
21360 struct buffer *b = XBUFFER (w->buffer);
21361 int cursor_type = DEFAULT_CURSOR;
21362 Lisp_Object alt_cursor;
21363 int non_selected = 0;
21365 *active_cursor = 1;
21367 /* Echo area */
21368 if (cursor_in_echo_area
21369 && FRAME_HAS_MINIBUF_P (f)
21370 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21372 if (w == XWINDOW (echo_area_window))
21374 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21376 *width = FRAME_CURSOR_WIDTH (f);
21377 return FRAME_DESIRED_CURSOR (f);
21379 else
21380 return get_specified_cursor_type (b->cursor_type, width);
21383 *active_cursor = 0;
21384 non_selected = 1;
21387 /* Detect a nonselected window or nonselected frame. */
21388 else if (w != XWINDOW (f->selected_window)
21389 #ifdef HAVE_WINDOW_SYSTEM
21390 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21391 #endif
21394 *active_cursor = 0;
21396 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21397 return NO_CURSOR;
21399 non_selected = 1;
21402 /* Never display a cursor in a window in which cursor-type is nil. */
21403 if (NILP (b->cursor_type))
21404 return NO_CURSOR;
21406 /* Get the normal cursor type for this window. */
21407 if (EQ (b->cursor_type, Qt))
21409 cursor_type = FRAME_DESIRED_CURSOR (f);
21410 *width = FRAME_CURSOR_WIDTH (f);
21412 else
21413 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21415 /* Use cursor-in-non-selected-windows instead
21416 for non-selected window or frame. */
21417 if (non_selected)
21419 alt_cursor = b->cursor_in_non_selected_windows;
21420 if (!EQ (Qt, alt_cursor))
21421 return get_specified_cursor_type (alt_cursor, width);
21422 /* t means modify the normal cursor type. */
21423 if (cursor_type == FILLED_BOX_CURSOR)
21424 cursor_type = HOLLOW_BOX_CURSOR;
21425 else if (cursor_type == BAR_CURSOR && *width > 1)
21426 --*width;
21427 return cursor_type;
21430 /* Use normal cursor if not blinked off. */
21431 if (!w->cursor_off_p)
21433 #ifdef HAVE_WINDOW_SYSTEM
21434 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21436 if (cursor_type == FILLED_BOX_CURSOR)
21438 /* Using a block cursor on large images can be very annoying.
21439 So use a hollow cursor for "large" images.
21440 If image is not transparent (no mask), also use hollow cursor. */
21441 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21442 if (img != NULL && IMAGEP (img->spec))
21444 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21445 where N = size of default frame font size.
21446 This should cover most of the "tiny" icons people may use. */
21447 if (!img->mask
21448 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21449 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21450 cursor_type = HOLLOW_BOX_CURSOR;
21453 else if (cursor_type != NO_CURSOR)
21455 /* Display current only supports BOX and HOLLOW cursors for images.
21456 So for now, unconditionally use a HOLLOW cursor when cursor is
21457 not a solid box cursor. */
21458 cursor_type = HOLLOW_BOX_CURSOR;
21461 #endif
21462 return cursor_type;
21465 /* Cursor is blinked off, so determine how to "toggle" it. */
21467 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21468 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21469 return get_specified_cursor_type (XCDR (alt_cursor), width);
21471 /* Then see if frame has specified a specific blink off cursor type. */
21472 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21474 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21475 return FRAME_BLINK_OFF_CURSOR (f);
21478 #if 0
21479 /* Some people liked having a permanently visible blinking cursor,
21480 while others had very strong opinions against it. So it was
21481 decided to remove it. KFS 2003-09-03 */
21483 /* Finally perform built-in cursor blinking:
21484 filled box <-> hollow box
21485 wide [h]bar <-> narrow [h]bar
21486 narrow [h]bar <-> no cursor
21487 other type <-> no cursor */
21489 if (cursor_type == FILLED_BOX_CURSOR)
21490 return HOLLOW_BOX_CURSOR;
21492 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21494 *width = 1;
21495 return cursor_type;
21497 #endif
21499 return NO_CURSOR;
21503 #ifdef HAVE_WINDOW_SYSTEM
21505 /* Notice when the text cursor of window W has been completely
21506 overwritten by a drawing operation that outputs glyphs in AREA
21507 starting at X0 and ending at X1 in the line starting at Y0 and
21508 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21509 the rest of the line after X0 has been written. Y coordinates
21510 are window-relative. */
21512 static void
21513 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21514 struct window *w;
21515 enum glyph_row_area area;
21516 int x0, y0, x1, y1;
21518 int cx0, cx1, cy0, cy1;
21519 struct glyph_row *row;
21521 if (!w->phys_cursor_on_p)
21522 return;
21523 if (area != TEXT_AREA)
21524 return;
21526 if (w->phys_cursor.vpos < 0
21527 || w->phys_cursor.vpos >= w->current_matrix->nrows
21528 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21529 !(row->enabled_p && row->displays_text_p)))
21530 return;
21532 if (row->cursor_in_fringe_p)
21534 row->cursor_in_fringe_p = 0;
21535 draw_fringe_bitmap (w, row, 0);
21536 w->phys_cursor_on_p = 0;
21537 return;
21540 cx0 = w->phys_cursor.x;
21541 cx1 = cx0 + w->phys_cursor_width;
21542 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21543 return;
21545 /* The cursor image will be completely removed from the
21546 screen if the output area intersects the cursor area in
21547 y-direction. When we draw in [y0 y1[, and some part of
21548 the cursor is at y < y0, that part must have been drawn
21549 before. When scrolling, the cursor is erased before
21550 actually scrolling, so we don't come here. When not
21551 scrolling, the rows above the old cursor row must have
21552 changed, and in this case these rows must have written
21553 over the cursor image.
21555 Likewise if part of the cursor is below y1, with the
21556 exception of the cursor being in the first blank row at
21557 the buffer and window end because update_text_area
21558 doesn't draw that row. (Except when it does, but
21559 that's handled in update_text_area.) */
21561 cy0 = w->phys_cursor.y;
21562 cy1 = cy0 + w->phys_cursor_height;
21563 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21564 return;
21566 w->phys_cursor_on_p = 0;
21569 #endif /* HAVE_WINDOW_SYSTEM */
21572 /************************************************************************
21573 Mouse Face
21574 ************************************************************************/
21576 #ifdef HAVE_WINDOW_SYSTEM
21578 /* EXPORT for RIF:
21579 Fix the display of area AREA of overlapping row ROW in window W
21580 with respect to the overlapping part OVERLAPS. */
21582 void
21583 x_fix_overlapping_area (w, row, area, overlaps)
21584 struct window *w;
21585 struct glyph_row *row;
21586 enum glyph_row_area area;
21587 int overlaps;
21589 int i, x;
21591 BLOCK_INPUT;
21593 x = 0;
21594 for (i = 0; i < row->used[area];)
21596 if (row->glyphs[area][i].overlaps_vertically_p)
21598 int start = i, start_x = x;
21602 x += row->glyphs[area][i].pixel_width;
21603 ++i;
21605 while (i < row->used[area]
21606 && row->glyphs[area][i].overlaps_vertically_p);
21608 draw_glyphs (w, start_x, row, area,
21609 start, i,
21610 DRAW_NORMAL_TEXT, overlaps);
21612 else
21614 x += row->glyphs[area][i].pixel_width;
21615 ++i;
21619 UNBLOCK_INPUT;
21623 /* EXPORT:
21624 Draw the cursor glyph of window W in glyph row ROW. See the
21625 comment of draw_glyphs for the meaning of HL. */
21627 void
21628 draw_phys_cursor_glyph (w, row, hl)
21629 struct window *w;
21630 struct glyph_row *row;
21631 enum draw_glyphs_face hl;
21633 /* If cursor hpos is out of bounds, don't draw garbage. This can
21634 happen in mini-buffer windows when switching between echo area
21635 glyphs and mini-buffer. */
21636 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21638 int on_p = w->phys_cursor_on_p;
21639 int x1;
21640 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21641 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21642 hl, 0);
21643 w->phys_cursor_on_p = on_p;
21645 if (hl == DRAW_CURSOR)
21646 w->phys_cursor_width = x1 - w->phys_cursor.x;
21647 /* When we erase the cursor, and ROW is overlapped by other
21648 rows, make sure that these overlapping parts of other rows
21649 are redrawn. */
21650 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21652 w->phys_cursor_width = x1 - w->phys_cursor.x;
21654 if (row > w->current_matrix->rows
21655 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21656 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21657 OVERLAPS_ERASED_CURSOR);
21659 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21660 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21661 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21662 OVERLAPS_ERASED_CURSOR);
21668 /* EXPORT:
21669 Erase the image of a cursor of window W from the screen. */
21671 void
21672 erase_phys_cursor (w)
21673 struct window *w;
21675 struct frame *f = XFRAME (w->frame);
21676 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21677 int hpos = w->phys_cursor.hpos;
21678 int vpos = w->phys_cursor.vpos;
21679 int mouse_face_here_p = 0;
21680 struct glyph_matrix *active_glyphs = w->current_matrix;
21681 struct glyph_row *cursor_row;
21682 struct glyph *cursor_glyph;
21683 enum draw_glyphs_face hl;
21685 /* No cursor displayed or row invalidated => nothing to do on the
21686 screen. */
21687 if (w->phys_cursor_type == NO_CURSOR)
21688 goto mark_cursor_off;
21690 /* VPOS >= active_glyphs->nrows means that window has been resized.
21691 Don't bother to erase the cursor. */
21692 if (vpos >= active_glyphs->nrows)
21693 goto mark_cursor_off;
21695 /* If row containing cursor is marked invalid, there is nothing we
21696 can do. */
21697 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21698 if (!cursor_row->enabled_p)
21699 goto mark_cursor_off;
21701 /* If line spacing is > 0, old cursor may only be partially visible in
21702 window after split-window. So adjust visible height. */
21703 cursor_row->visible_height = min (cursor_row->visible_height,
21704 window_text_bottom_y (w) - cursor_row->y);
21706 /* If row is completely invisible, don't attempt to delete a cursor which
21707 isn't there. This can happen if cursor is at top of a window, and
21708 we switch to a buffer with a header line in that window. */
21709 if (cursor_row->visible_height <= 0)
21710 goto mark_cursor_off;
21712 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21713 if (cursor_row->cursor_in_fringe_p)
21715 cursor_row->cursor_in_fringe_p = 0;
21716 draw_fringe_bitmap (w, cursor_row, 0);
21717 goto mark_cursor_off;
21720 /* This can happen when the new row is shorter than the old one.
21721 In this case, either draw_glyphs or clear_end_of_line
21722 should have cleared the cursor. Note that we wouldn't be
21723 able to erase the cursor in this case because we don't have a
21724 cursor glyph at hand. */
21725 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21726 goto mark_cursor_off;
21728 /* If the cursor is in the mouse face area, redisplay that when
21729 we clear the cursor. */
21730 if (! NILP (dpyinfo->mouse_face_window)
21731 && w == XWINDOW (dpyinfo->mouse_face_window)
21732 && (vpos > dpyinfo->mouse_face_beg_row
21733 || (vpos == dpyinfo->mouse_face_beg_row
21734 && hpos >= dpyinfo->mouse_face_beg_col))
21735 && (vpos < dpyinfo->mouse_face_end_row
21736 || (vpos == dpyinfo->mouse_face_end_row
21737 && hpos < dpyinfo->mouse_face_end_col))
21738 /* Don't redraw the cursor's spot in mouse face if it is at the
21739 end of a line (on a newline). The cursor appears there, but
21740 mouse highlighting does not. */
21741 && cursor_row->used[TEXT_AREA] > hpos)
21742 mouse_face_here_p = 1;
21744 /* Maybe clear the display under the cursor. */
21745 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21747 int x, y, left_x;
21748 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21749 int width;
21751 cursor_glyph = get_phys_cursor_glyph (w);
21752 if (cursor_glyph == NULL)
21753 goto mark_cursor_off;
21755 width = cursor_glyph->pixel_width;
21756 left_x = window_box_left_offset (w, TEXT_AREA);
21757 x = w->phys_cursor.x;
21758 if (x < left_x)
21759 width -= left_x - x;
21760 width = min (width, window_box_width (w, TEXT_AREA) - x);
21761 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21762 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21764 if (width > 0)
21765 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21768 /* Erase the cursor by redrawing the character underneath it. */
21769 if (mouse_face_here_p)
21770 hl = DRAW_MOUSE_FACE;
21771 else
21772 hl = DRAW_NORMAL_TEXT;
21773 draw_phys_cursor_glyph (w, cursor_row, hl);
21775 mark_cursor_off:
21776 w->phys_cursor_on_p = 0;
21777 w->phys_cursor_type = NO_CURSOR;
21781 /* EXPORT:
21782 Display or clear cursor of window W. If ON is zero, clear the
21783 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21784 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21786 void
21787 display_and_set_cursor (w, on, hpos, vpos, x, y)
21788 struct window *w;
21789 int on, hpos, vpos, x, y;
21791 struct frame *f = XFRAME (w->frame);
21792 int new_cursor_type;
21793 int new_cursor_width;
21794 int active_cursor;
21795 struct glyph_row *glyph_row;
21796 struct glyph *glyph;
21798 /* This is pointless on invisible frames, and dangerous on garbaged
21799 windows and frames; in the latter case, the frame or window may
21800 be in the midst of changing its size, and x and y may be off the
21801 window. */
21802 if (! FRAME_VISIBLE_P (f)
21803 || FRAME_GARBAGED_P (f)
21804 || vpos >= w->current_matrix->nrows
21805 || hpos >= w->current_matrix->matrix_w)
21806 return;
21808 /* If cursor is off and we want it off, return quickly. */
21809 if (!on && !w->phys_cursor_on_p)
21810 return;
21812 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21813 /* If cursor row is not enabled, we don't really know where to
21814 display the cursor. */
21815 if (!glyph_row->enabled_p)
21817 w->phys_cursor_on_p = 0;
21818 return;
21821 glyph = NULL;
21822 if (!glyph_row->exact_window_width_line_p
21823 || hpos < glyph_row->used[TEXT_AREA])
21824 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21826 xassert (interrupt_input_blocked);
21828 /* Set new_cursor_type to the cursor we want to be displayed. */
21829 new_cursor_type = get_window_cursor_type (w, glyph,
21830 &new_cursor_width, &active_cursor);
21832 /* If cursor is currently being shown and we don't want it to be or
21833 it is in the wrong place, or the cursor type is not what we want,
21834 erase it. */
21835 if (w->phys_cursor_on_p
21836 && (!on
21837 || w->phys_cursor.x != x
21838 || w->phys_cursor.y != y
21839 || new_cursor_type != w->phys_cursor_type
21840 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21841 && new_cursor_width != w->phys_cursor_width)))
21842 erase_phys_cursor (w);
21844 /* Don't check phys_cursor_on_p here because that flag is only set
21845 to zero in some cases where we know that the cursor has been
21846 completely erased, to avoid the extra work of erasing the cursor
21847 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21848 still not be visible, or it has only been partly erased. */
21849 if (on)
21851 w->phys_cursor_ascent = glyph_row->ascent;
21852 w->phys_cursor_height = glyph_row->height;
21854 /* Set phys_cursor_.* before x_draw_.* is called because some
21855 of them may need the information. */
21856 w->phys_cursor.x = x;
21857 w->phys_cursor.y = glyph_row->y;
21858 w->phys_cursor.hpos = hpos;
21859 w->phys_cursor.vpos = vpos;
21862 rif->draw_window_cursor (w, glyph_row, x, y,
21863 new_cursor_type, new_cursor_width,
21864 on, active_cursor);
21868 /* Switch the display of W's cursor on or off, according to the value
21869 of ON. */
21871 static void
21872 update_window_cursor (w, on)
21873 struct window *w;
21874 int on;
21876 /* Don't update cursor in windows whose frame is in the process
21877 of being deleted. */
21878 if (w->current_matrix)
21880 BLOCK_INPUT;
21881 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21882 w->phys_cursor.x, w->phys_cursor.y);
21883 UNBLOCK_INPUT;
21888 /* Call update_window_cursor with parameter ON_P on all leaf windows
21889 in the window tree rooted at W. */
21891 static void
21892 update_cursor_in_window_tree (w, on_p)
21893 struct window *w;
21894 int on_p;
21896 while (w)
21898 if (!NILP (w->hchild))
21899 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21900 else if (!NILP (w->vchild))
21901 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21902 else
21903 update_window_cursor (w, on_p);
21905 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21910 /* EXPORT:
21911 Display the cursor on window W, or clear it, according to ON_P.
21912 Don't change the cursor's position. */
21914 void
21915 x_update_cursor (f, on_p)
21916 struct frame *f;
21917 int on_p;
21919 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21923 /* EXPORT:
21924 Clear the cursor of window W to background color, and mark the
21925 cursor as not shown. This is used when the text where the cursor
21926 is is about to be rewritten. */
21928 void
21929 x_clear_cursor (w)
21930 struct window *w;
21932 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21933 update_window_cursor (w, 0);
21937 /* EXPORT:
21938 Display the active region described by mouse_face_* according to DRAW. */
21940 void
21941 show_mouse_face (dpyinfo, draw)
21942 Display_Info *dpyinfo;
21943 enum draw_glyphs_face draw;
21945 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21946 struct frame *f = XFRAME (WINDOW_FRAME (w));
21948 if (/* If window is in the process of being destroyed, don't bother
21949 to do anything. */
21950 w->current_matrix != NULL
21951 /* Don't update mouse highlight if hidden */
21952 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21953 /* Recognize when we are called to operate on rows that don't exist
21954 anymore. This can happen when a window is split. */
21955 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21957 int phys_cursor_on_p = w->phys_cursor_on_p;
21958 struct glyph_row *row, *first, *last;
21960 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21961 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21963 for (row = first; row <= last && row->enabled_p; ++row)
21965 int start_hpos, end_hpos, start_x;
21967 /* For all but the first row, the highlight starts at column 0. */
21968 if (row == first)
21970 start_hpos = dpyinfo->mouse_face_beg_col;
21971 start_x = dpyinfo->mouse_face_beg_x;
21973 else
21975 start_hpos = 0;
21976 start_x = 0;
21979 if (row == last)
21980 end_hpos = dpyinfo->mouse_face_end_col;
21981 else
21983 end_hpos = row->used[TEXT_AREA];
21984 if (draw == DRAW_NORMAL_TEXT)
21985 row->fill_line_p = 1; /* Clear to end of line */
21988 if (end_hpos > start_hpos)
21990 draw_glyphs (w, start_x, row, TEXT_AREA,
21991 start_hpos, end_hpos,
21992 draw, 0);
21994 row->mouse_face_p
21995 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21999 /* When we've written over the cursor, arrange for it to
22000 be displayed again. */
22001 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22003 BLOCK_INPUT;
22004 display_and_set_cursor (w, 1,
22005 w->phys_cursor.hpos, w->phys_cursor.vpos,
22006 w->phys_cursor.x, w->phys_cursor.y);
22007 UNBLOCK_INPUT;
22011 /* Change the mouse cursor. */
22012 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22013 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22014 else if (draw == DRAW_MOUSE_FACE)
22015 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22016 else
22017 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22020 /* EXPORT:
22021 Clear out the mouse-highlighted active region.
22022 Redraw it un-highlighted first. Value is non-zero if mouse
22023 face was actually drawn unhighlighted. */
22026 clear_mouse_face (dpyinfo)
22027 Display_Info *dpyinfo;
22029 int cleared = 0;
22031 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22033 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22034 cleared = 1;
22037 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22038 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22039 dpyinfo->mouse_face_window = Qnil;
22040 dpyinfo->mouse_face_overlay = Qnil;
22041 return cleared;
22045 /* EXPORT:
22046 Non-zero if physical cursor of window W is within mouse face. */
22049 cursor_in_mouse_face_p (w)
22050 struct window *w;
22052 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22053 int in_mouse_face = 0;
22055 if (WINDOWP (dpyinfo->mouse_face_window)
22056 && XWINDOW (dpyinfo->mouse_face_window) == w)
22058 int hpos = w->phys_cursor.hpos;
22059 int vpos = w->phys_cursor.vpos;
22061 if (vpos >= dpyinfo->mouse_face_beg_row
22062 && vpos <= dpyinfo->mouse_face_end_row
22063 && (vpos > dpyinfo->mouse_face_beg_row
22064 || hpos >= dpyinfo->mouse_face_beg_col)
22065 && (vpos < dpyinfo->mouse_face_end_row
22066 || hpos < dpyinfo->mouse_face_end_col
22067 || dpyinfo->mouse_face_past_end))
22068 in_mouse_face = 1;
22071 return in_mouse_face;
22077 /* Find the glyph matrix position of buffer position CHARPOS in window
22078 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22079 current glyphs must be up to date. If CHARPOS is above window
22080 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22081 of last line in W. In the row containing CHARPOS, stop before glyphs
22082 having STOP as object. */
22084 #if 1 /* This is a version of fast_find_position that's more correct
22085 in the presence of hscrolling, for example. I didn't install
22086 it right away because the problem fixed is minor, it failed
22087 in 20.x as well, and I think it's too risky to install
22088 so near the release of 21.1. 2001-09-25 gerd. */
22090 #ifndef HAVE_CARBON
22091 static
22092 #endif
22094 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22095 struct window *w;
22096 int charpos;
22097 int *hpos, *vpos, *x, *y;
22098 Lisp_Object stop;
22100 struct glyph_row *row, *first;
22101 struct glyph *glyph, *end;
22102 int past_end = 0;
22104 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22105 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22107 *x = first->x;
22108 *y = first->y;
22109 *hpos = 0;
22110 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22111 return 1;
22114 row = row_containing_pos (w, charpos, first, NULL, 0);
22115 if (row == NULL)
22117 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22118 past_end = 1;
22121 /* If whole rows or last part of a row came from a display overlay,
22122 row_containing_pos will skip over such rows because their end pos
22123 equals the start pos of the overlay or interval.
22125 Move back if we have a STOP object and previous row's
22126 end glyph came from STOP. */
22127 if (!NILP (stop))
22129 struct glyph_row *prev;
22130 while ((prev = row - 1, prev >= first)
22131 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22132 && prev->used[TEXT_AREA] > 0)
22134 struct glyph *beg = prev->glyphs[TEXT_AREA];
22135 glyph = beg + prev->used[TEXT_AREA];
22136 while (--glyph >= beg
22137 && INTEGERP (glyph->object));
22138 if (glyph < beg
22139 || !EQ (stop, glyph->object))
22140 break;
22141 row = prev;
22145 *x = row->x;
22146 *y = row->y;
22147 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22149 glyph = row->glyphs[TEXT_AREA];
22150 end = glyph + row->used[TEXT_AREA];
22152 /* Skip over glyphs not having an object at the start of the row.
22153 These are special glyphs like truncation marks on terminal
22154 frames. */
22155 if (row->displays_text_p)
22156 while (glyph < end
22157 && INTEGERP (glyph->object)
22158 && !EQ (stop, glyph->object)
22159 && glyph->charpos < 0)
22161 *x += glyph->pixel_width;
22162 ++glyph;
22165 while (glyph < end
22166 && !INTEGERP (glyph->object)
22167 && !EQ (stop, glyph->object)
22168 && (!BUFFERP (glyph->object)
22169 || glyph->charpos < charpos))
22171 *x += glyph->pixel_width;
22172 ++glyph;
22175 *hpos = glyph - row->glyphs[TEXT_AREA];
22176 return !past_end;
22179 #else /* not 1 */
22181 static int
22182 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22183 struct window *w;
22184 int pos;
22185 int *hpos, *vpos, *x, *y;
22186 Lisp_Object stop;
22188 int i;
22189 int lastcol;
22190 int maybe_next_line_p = 0;
22191 int line_start_position;
22192 int yb = window_text_bottom_y (w);
22193 struct glyph_row *row, *best_row;
22194 int row_vpos, best_row_vpos;
22195 int current_x;
22197 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22198 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22200 while (row->y < yb)
22202 if (row->used[TEXT_AREA])
22203 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22204 else
22205 line_start_position = 0;
22207 if (line_start_position > pos)
22208 break;
22209 /* If the position sought is the end of the buffer,
22210 don't include the blank lines at the bottom of the window. */
22211 else if (line_start_position == pos
22212 && pos == BUF_ZV (XBUFFER (w->buffer)))
22214 maybe_next_line_p = 1;
22215 break;
22217 else if (line_start_position > 0)
22219 best_row = row;
22220 best_row_vpos = row_vpos;
22223 if (row->y + row->height >= yb)
22224 break;
22226 ++row;
22227 ++row_vpos;
22230 /* Find the right column within BEST_ROW. */
22231 lastcol = 0;
22232 current_x = best_row->x;
22233 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22235 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22236 int charpos = glyph->charpos;
22238 if (BUFFERP (glyph->object))
22240 if (charpos == pos)
22242 *hpos = i;
22243 *vpos = best_row_vpos;
22244 *x = current_x;
22245 *y = best_row->y;
22246 return 1;
22248 else if (charpos > pos)
22249 break;
22251 else if (EQ (glyph->object, stop))
22252 break;
22254 if (charpos > 0)
22255 lastcol = i;
22256 current_x += glyph->pixel_width;
22259 /* If we're looking for the end of the buffer,
22260 and we didn't find it in the line we scanned,
22261 use the start of the following line. */
22262 if (maybe_next_line_p)
22264 ++best_row;
22265 ++best_row_vpos;
22266 lastcol = 0;
22267 current_x = best_row->x;
22270 *vpos = best_row_vpos;
22271 *hpos = lastcol + 1;
22272 *x = current_x;
22273 *y = best_row->y;
22274 return 0;
22277 #endif /* not 1 */
22280 /* Find the position of the glyph for position POS in OBJECT in
22281 window W's current matrix, and return in *X, *Y the pixel
22282 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22284 RIGHT_P non-zero means return the position of the right edge of the
22285 glyph, RIGHT_P zero means return the left edge position.
22287 If no glyph for POS exists in the matrix, return the position of
22288 the glyph with the next smaller position that is in the matrix, if
22289 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22290 exists in the matrix, return the position of the glyph with the
22291 next larger position in OBJECT.
22293 Value is non-zero if a glyph was found. */
22295 static int
22296 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22297 struct window *w;
22298 int pos;
22299 Lisp_Object object;
22300 int *hpos, *vpos, *x, *y;
22301 int right_p;
22303 int yb = window_text_bottom_y (w);
22304 struct glyph_row *r;
22305 struct glyph *best_glyph = NULL;
22306 struct glyph_row *best_row = NULL;
22307 int best_x = 0;
22309 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22310 r->enabled_p && r->y < yb;
22311 ++r)
22313 struct glyph *g = r->glyphs[TEXT_AREA];
22314 struct glyph *e = g + r->used[TEXT_AREA];
22315 int gx;
22317 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22318 if (EQ (g->object, object))
22320 if (g->charpos == pos)
22322 best_glyph = g;
22323 best_x = gx;
22324 best_row = r;
22325 goto found;
22327 else if (best_glyph == NULL
22328 || ((abs (g->charpos - pos)
22329 < abs (best_glyph->charpos - pos))
22330 && (right_p
22331 ? g->charpos < pos
22332 : g->charpos > pos)))
22334 best_glyph = g;
22335 best_x = gx;
22336 best_row = r;
22341 found:
22343 if (best_glyph)
22345 *x = best_x;
22346 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22348 if (right_p)
22350 *x += best_glyph->pixel_width;
22351 ++*hpos;
22354 *y = best_row->y;
22355 *vpos = best_row - w->current_matrix->rows;
22358 return best_glyph != NULL;
22362 /* See if position X, Y is within a hot-spot of an image. */
22364 static int
22365 on_hot_spot_p (hot_spot, x, y)
22366 Lisp_Object hot_spot;
22367 int x, y;
22369 if (!CONSP (hot_spot))
22370 return 0;
22372 if (EQ (XCAR (hot_spot), Qrect))
22374 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22375 Lisp_Object rect = XCDR (hot_spot);
22376 Lisp_Object tem;
22377 if (!CONSP (rect))
22378 return 0;
22379 if (!CONSP (XCAR (rect)))
22380 return 0;
22381 if (!CONSP (XCDR (rect)))
22382 return 0;
22383 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22384 return 0;
22385 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22386 return 0;
22387 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22388 return 0;
22389 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22390 return 0;
22391 return 1;
22393 else if (EQ (XCAR (hot_spot), Qcircle))
22395 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22396 Lisp_Object circ = XCDR (hot_spot);
22397 Lisp_Object lr, lx0, ly0;
22398 if (CONSP (circ)
22399 && CONSP (XCAR (circ))
22400 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22401 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22402 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22404 double r = XFLOATINT (lr);
22405 double dx = XINT (lx0) - x;
22406 double dy = XINT (ly0) - y;
22407 return (dx * dx + dy * dy <= r * r);
22410 else if (EQ (XCAR (hot_spot), Qpoly))
22412 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22413 if (VECTORP (XCDR (hot_spot)))
22415 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22416 Lisp_Object *poly = v->contents;
22417 int n = v->size;
22418 int i;
22419 int inside = 0;
22420 Lisp_Object lx, ly;
22421 int x0, y0;
22423 /* Need an even number of coordinates, and at least 3 edges. */
22424 if (n < 6 || n & 1)
22425 return 0;
22427 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22428 If count is odd, we are inside polygon. Pixels on edges
22429 may or may not be included depending on actual geometry of the
22430 polygon. */
22431 if ((lx = poly[n-2], !INTEGERP (lx))
22432 || (ly = poly[n-1], !INTEGERP (lx)))
22433 return 0;
22434 x0 = XINT (lx), y0 = XINT (ly);
22435 for (i = 0; i < n; i += 2)
22437 int x1 = x0, y1 = y0;
22438 if ((lx = poly[i], !INTEGERP (lx))
22439 || (ly = poly[i+1], !INTEGERP (ly)))
22440 return 0;
22441 x0 = XINT (lx), y0 = XINT (ly);
22443 /* Does this segment cross the X line? */
22444 if (x0 >= x)
22446 if (x1 >= x)
22447 continue;
22449 else if (x1 < x)
22450 continue;
22451 if (y > y0 && y > y1)
22452 continue;
22453 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22454 inside = !inside;
22456 return inside;
22459 /* If we don't understand the format, pretend we're not in the hot-spot. */
22460 return 0;
22463 Lisp_Object
22464 find_hot_spot (map, x, y)
22465 Lisp_Object map;
22466 int x, y;
22468 while (CONSP (map))
22470 if (CONSP (XCAR (map))
22471 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22472 return XCAR (map);
22473 map = XCDR (map);
22476 return Qnil;
22479 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22480 3, 3, 0,
22481 doc: /* Lookup in image map MAP coordinates X and Y.
22482 An image map is an alist where each element has the format (AREA ID PLIST).
22483 An AREA is specified as either a rectangle, a circle, or a polygon:
22484 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22485 pixel coordinates of the upper left and bottom right corners.
22486 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22487 and the radius of the circle; r may be a float or integer.
22488 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22489 vector describes one corner in the polygon.
22490 Returns the alist element for the first matching AREA in MAP. */)
22491 (map, x, y)
22492 Lisp_Object map;
22493 Lisp_Object x, y;
22495 if (NILP (map))
22496 return Qnil;
22498 CHECK_NUMBER (x);
22499 CHECK_NUMBER (y);
22501 return find_hot_spot (map, XINT (x), XINT (y));
22505 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22506 static void
22507 define_frame_cursor1 (f, cursor, pointer)
22508 struct frame *f;
22509 Cursor cursor;
22510 Lisp_Object pointer;
22512 /* Do not change cursor shape while dragging mouse. */
22513 if (!NILP (do_mouse_tracking))
22514 return;
22516 if (!NILP (pointer))
22518 if (EQ (pointer, Qarrow))
22519 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22520 else if (EQ (pointer, Qhand))
22521 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22522 else if (EQ (pointer, Qtext))
22523 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22524 else if (EQ (pointer, intern ("hdrag")))
22525 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22526 #ifdef HAVE_X_WINDOWS
22527 else if (EQ (pointer, intern ("vdrag")))
22528 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22529 #endif
22530 else if (EQ (pointer, intern ("hourglass")))
22531 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22532 else if (EQ (pointer, Qmodeline))
22533 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22534 else
22535 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22538 if (cursor != No_Cursor)
22539 rif->define_frame_cursor (f, cursor);
22542 /* Take proper action when mouse has moved to the mode or header line
22543 or marginal area AREA of window W, x-position X and y-position Y.
22544 X is relative to the start of the text display area of W, so the
22545 width of bitmap areas and scroll bars must be subtracted to get a
22546 position relative to the start of the mode line. */
22548 static void
22549 note_mode_line_or_margin_highlight (window, x, y, area)
22550 Lisp_Object window;
22551 int x, y;
22552 enum window_part area;
22554 struct window *w = XWINDOW (window);
22555 struct frame *f = XFRAME (w->frame);
22556 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22557 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22558 Lisp_Object pointer = Qnil;
22559 int charpos, dx, dy, width, height;
22560 Lisp_Object string, object = Qnil;
22561 Lisp_Object pos, help;
22563 Lisp_Object mouse_face;
22564 int original_x_pixel = x;
22565 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22566 struct glyph_row *row;
22568 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22570 int x0;
22571 struct glyph *end;
22573 string = mode_line_string (w, area, &x, &y, &charpos,
22574 &object, &dx, &dy, &width, &height);
22576 row = (area == ON_MODE_LINE
22577 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22578 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22580 /* Find glyph */
22581 if (row->mode_line_p && row->enabled_p)
22583 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22584 end = glyph + row->used[TEXT_AREA];
22586 for (x0 = original_x_pixel;
22587 glyph < end && x0 >= glyph->pixel_width;
22588 ++glyph)
22589 x0 -= glyph->pixel_width;
22591 if (glyph >= end)
22592 glyph = NULL;
22595 else
22597 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22598 string = marginal_area_string (w, area, &x, &y, &charpos,
22599 &object, &dx, &dy, &width, &height);
22602 help = Qnil;
22604 if (IMAGEP (object))
22606 Lisp_Object image_map, hotspot;
22607 if ((image_map = Fplist_get (XCDR (object), QCmap),
22608 !NILP (image_map))
22609 && (hotspot = find_hot_spot (image_map, dx, dy),
22610 CONSP (hotspot))
22611 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22613 Lisp_Object area_id, plist;
22615 area_id = XCAR (hotspot);
22616 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22617 If so, we could look for mouse-enter, mouse-leave
22618 properties in PLIST (and do something...). */
22619 hotspot = XCDR (hotspot);
22620 if (CONSP (hotspot)
22621 && (plist = XCAR (hotspot), CONSP (plist)))
22623 pointer = Fplist_get (plist, Qpointer);
22624 if (NILP (pointer))
22625 pointer = Qhand;
22626 help = Fplist_get (plist, Qhelp_echo);
22627 if (!NILP (help))
22629 help_echo_string = help;
22630 /* Is this correct? ++kfs */
22631 XSETWINDOW (help_echo_window, w);
22632 help_echo_object = w->buffer;
22633 help_echo_pos = charpos;
22637 if (NILP (pointer))
22638 pointer = Fplist_get (XCDR (object), QCpointer);
22641 if (STRINGP (string))
22643 pos = make_number (charpos);
22644 /* If we're on a string with `help-echo' text property, arrange
22645 for the help to be displayed. This is done by setting the
22646 global variable help_echo_string to the help string. */
22647 if (NILP (help))
22649 help = Fget_text_property (pos, Qhelp_echo, string);
22650 if (!NILP (help))
22652 help_echo_string = help;
22653 XSETWINDOW (help_echo_window, w);
22654 help_echo_object = string;
22655 help_echo_pos = charpos;
22659 if (NILP (pointer))
22660 pointer = Fget_text_property (pos, Qpointer, string);
22662 /* Change the mouse pointer according to what is under X/Y. */
22663 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22665 Lisp_Object map;
22666 map = Fget_text_property (pos, Qlocal_map, string);
22667 if (!KEYMAPP (map))
22668 map = Fget_text_property (pos, Qkeymap, string);
22669 if (!KEYMAPP (map))
22670 cursor = dpyinfo->vertical_scroll_bar_cursor;
22673 /* Change the mouse face according to what is under X/Y. */
22674 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22675 if (!NILP (mouse_face)
22676 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22677 && glyph)
22679 Lisp_Object b, e;
22681 struct glyph * tmp_glyph;
22683 int gpos;
22684 int gseq_length;
22685 int total_pixel_width;
22686 int ignore;
22688 int vpos, hpos;
22690 b = Fprevious_single_property_change (make_number (charpos + 1),
22691 Qmouse_face, string, Qnil);
22692 if (NILP (b))
22693 b = make_number (0);
22695 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22696 if (NILP (e))
22697 e = make_number (SCHARS (string));
22699 /* Calculate the position(glyph position: GPOS) of GLYPH in
22700 displayed string. GPOS is different from CHARPOS.
22702 CHARPOS is the position of glyph in internal string
22703 object. A mode line string format has structures which
22704 is converted to a flatten by emacs lisp interpreter.
22705 The internal string is an element of the structures.
22706 The displayed string is the flatten string. */
22707 gpos = 0;
22708 if (glyph > row_start_glyph)
22710 tmp_glyph = glyph - 1;
22711 while (tmp_glyph >= row_start_glyph
22712 && tmp_glyph->charpos >= XINT (b)
22713 && EQ (tmp_glyph->object, glyph->object))
22715 tmp_glyph--;
22716 gpos++;
22720 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22721 displayed string holding GLYPH.
22723 GSEQ_LENGTH is different from SCHARS (STRING).
22724 SCHARS (STRING) returns the length of the internal string. */
22725 for (tmp_glyph = glyph, gseq_length = gpos;
22726 tmp_glyph->charpos < XINT (e);
22727 tmp_glyph++, gseq_length++)
22729 if (!EQ (tmp_glyph->object, glyph->object))
22730 break;
22733 total_pixel_width = 0;
22734 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22735 total_pixel_width += tmp_glyph->pixel_width;
22737 /* Pre calculation of re-rendering position */
22738 vpos = (x - gpos);
22739 hpos = (area == ON_MODE_LINE
22740 ? (w->current_matrix)->nrows - 1
22741 : 0);
22743 /* If the re-rendering position is included in the last
22744 re-rendering area, we should do nothing. */
22745 if ( EQ (window, dpyinfo->mouse_face_window)
22746 && dpyinfo->mouse_face_beg_col <= vpos
22747 && vpos < dpyinfo->mouse_face_end_col
22748 && dpyinfo->mouse_face_beg_row == hpos )
22749 return;
22751 if (clear_mouse_face (dpyinfo))
22752 cursor = No_Cursor;
22754 dpyinfo->mouse_face_beg_col = vpos;
22755 dpyinfo->mouse_face_beg_row = hpos;
22757 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22758 dpyinfo->mouse_face_beg_y = 0;
22760 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22761 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22763 dpyinfo->mouse_face_end_x = 0;
22764 dpyinfo->mouse_face_end_y = 0;
22766 dpyinfo->mouse_face_past_end = 0;
22767 dpyinfo->mouse_face_window = window;
22769 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22770 charpos,
22771 0, 0, 0, &ignore,
22772 glyph->face_id, 1);
22773 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22775 if (NILP (pointer))
22776 pointer = Qhand;
22778 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22779 clear_mouse_face (dpyinfo);
22781 define_frame_cursor1 (f, cursor, pointer);
22785 /* EXPORT:
22786 Take proper action when the mouse has moved to position X, Y on
22787 frame F as regards highlighting characters that have mouse-face
22788 properties. Also de-highlighting chars where the mouse was before.
22789 X and Y can be negative or out of range. */
22791 void
22792 note_mouse_highlight (f, x, y)
22793 struct frame *f;
22794 int x, y;
22796 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22797 enum window_part part;
22798 Lisp_Object window;
22799 struct window *w;
22800 Cursor cursor = No_Cursor;
22801 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22802 struct buffer *b;
22804 /* When a menu is active, don't highlight because this looks odd. */
22805 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22806 if (popup_activated ())
22807 return;
22808 #endif
22810 if (NILP (Vmouse_highlight)
22811 || !f->glyphs_initialized_p)
22812 return;
22814 dpyinfo->mouse_face_mouse_x = x;
22815 dpyinfo->mouse_face_mouse_y = y;
22816 dpyinfo->mouse_face_mouse_frame = f;
22818 if (dpyinfo->mouse_face_defer)
22819 return;
22821 if (gc_in_progress)
22823 dpyinfo->mouse_face_deferred_gc = 1;
22824 return;
22827 /* Which window is that in? */
22828 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22830 /* If we were displaying active text in another window, clear that.
22831 Also clear if we move out of text area in same window. */
22832 if (! EQ (window, dpyinfo->mouse_face_window)
22833 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22834 && !NILP (dpyinfo->mouse_face_window)))
22835 clear_mouse_face (dpyinfo);
22837 /* Not on a window -> return. */
22838 if (!WINDOWP (window))
22839 return;
22841 /* Reset help_echo_string. It will get recomputed below. */
22842 help_echo_string = Qnil;
22844 /* Convert to window-relative pixel coordinates. */
22845 w = XWINDOW (window);
22846 frame_to_window_pixel_xy (w, &x, &y);
22848 /* Handle tool-bar window differently since it doesn't display a
22849 buffer. */
22850 if (EQ (window, f->tool_bar_window))
22852 note_tool_bar_highlight (f, x, y);
22853 return;
22856 /* Mouse is on the mode, header line or margin? */
22857 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22858 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22860 note_mode_line_or_margin_highlight (window, x, y, part);
22861 return;
22864 if (part == ON_VERTICAL_BORDER)
22866 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22867 help_echo_string = build_string ("drag-mouse-1: resize");
22869 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22870 || part == ON_SCROLL_BAR)
22871 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22872 else
22873 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22875 /* Are we in a window whose display is up to date?
22876 And verify the buffer's text has not changed. */
22877 b = XBUFFER (w->buffer);
22878 if (part == ON_TEXT
22879 && EQ (w->window_end_valid, w->buffer)
22880 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22881 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22883 int hpos, vpos, pos, i, dx, dy, area;
22884 struct glyph *glyph;
22885 Lisp_Object object;
22886 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22887 Lisp_Object *overlay_vec = NULL;
22888 int noverlays;
22889 struct buffer *obuf;
22890 int obegv, ozv, same_region;
22892 /* Find the glyph under X/Y. */
22893 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22895 /* Look for :pointer property on image. */
22896 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22898 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22899 if (img != NULL && IMAGEP (img->spec))
22901 Lisp_Object image_map, hotspot;
22902 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22903 !NILP (image_map))
22904 && (hotspot = find_hot_spot (image_map,
22905 glyph->slice.x + dx,
22906 glyph->slice.y + dy),
22907 CONSP (hotspot))
22908 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22910 Lisp_Object area_id, plist;
22912 area_id = XCAR (hotspot);
22913 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22914 If so, we could look for mouse-enter, mouse-leave
22915 properties in PLIST (and do something...). */
22916 hotspot = XCDR (hotspot);
22917 if (CONSP (hotspot)
22918 && (plist = XCAR (hotspot), CONSP (plist)))
22920 pointer = Fplist_get (plist, Qpointer);
22921 if (NILP (pointer))
22922 pointer = Qhand;
22923 help_echo_string = Fplist_get (plist, Qhelp_echo);
22924 if (!NILP (help_echo_string))
22926 help_echo_window = window;
22927 help_echo_object = glyph->object;
22928 help_echo_pos = glyph->charpos;
22932 if (NILP (pointer))
22933 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22937 /* Clear mouse face if X/Y not over text. */
22938 if (glyph == NULL
22939 || area != TEXT_AREA
22940 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22942 if (clear_mouse_face (dpyinfo))
22943 cursor = No_Cursor;
22944 if (NILP (pointer))
22946 if (area != TEXT_AREA)
22947 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22948 else
22949 pointer = Vvoid_text_area_pointer;
22951 goto set_cursor;
22954 pos = glyph->charpos;
22955 object = glyph->object;
22956 if (!STRINGP (object) && !BUFFERP (object))
22957 goto set_cursor;
22959 /* If we get an out-of-range value, return now; avoid an error. */
22960 if (BUFFERP (object) && pos > BUF_Z (b))
22961 goto set_cursor;
22963 /* Make the window's buffer temporarily current for
22964 overlays_at and compute_char_face. */
22965 obuf = current_buffer;
22966 current_buffer = b;
22967 obegv = BEGV;
22968 ozv = ZV;
22969 BEGV = BEG;
22970 ZV = Z;
22972 /* Is this char mouse-active or does it have help-echo? */
22973 position = make_number (pos);
22975 if (BUFFERP (object))
22977 /* Put all the overlays we want in a vector in overlay_vec. */
22978 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22979 /* Sort overlays into increasing priority order. */
22980 noverlays = sort_overlays (overlay_vec, noverlays, w);
22982 else
22983 noverlays = 0;
22985 same_region = (EQ (window, dpyinfo->mouse_face_window)
22986 && vpos >= dpyinfo->mouse_face_beg_row
22987 && vpos <= dpyinfo->mouse_face_end_row
22988 && (vpos > dpyinfo->mouse_face_beg_row
22989 || hpos >= dpyinfo->mouse_face_beg_col)
22990 && (vpos < dpyinfo->mouse_face_end_row
22991 || hpos < dpyinfo->mouse_face_end_col
22992 || dpyinfo->mouse_face_past_end));
22994 if (same_region)
22995 cursor = No_Cursor;
22997 /* Check mouse-face highlighting. */
22998 if (! same_region
22999 /* If there exists an overlay with mouse-face overlapping
23000 the one we are currently highlighting, we have to
23001 check if we enter the overlapping overlay, and then
23002 highlight only that. */
23003 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23004 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23006 /* Find the highest priority overlay that has a mouse-face
23007 property. */
23008 overlay = Qnil;
23009 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23011 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23012 if (!NILP (mouse_face))
23013 overlay = overlay_vec[i];
23016 /* If we're actually highlighting the same overlay as
23017 before, there's no need to do that again. */
23018 if (!NILP (overlay)
23019 && EQ (overlay, dpyinfo->mouse_face_overlay))
23020 goto check_help_echo;
23022 dpyinfo->mouse_face_overlay = overlay;
23024 /* Clear the display of the old active region, if any. */
23025 if (clear_mouse_face (dpyinfo))
23026 cursor = No_Cursor;
23028 /* If no overlay applies, get a text property. */
23029 if (NILP (overlay))
23030 mouse_face = Fget_text_property (position, Qmouse_face, object);
23032 /* Handle the overlay case. */
23033 if (!NILP (overlay))
23035 /* Find the range of text around this char that
23036 should be active. */
23037 Lisp_Object before, after;
23038 int ignore;
23040 before = Foverlay_start (overlay);
23041 after = Foverlay_end (overlay);
23042 /* Record this as the current active region. */
23043 fast_find_position (w, XFASTINT (before),
23044 &dpyinfo->mouse_face_beg_col,
23045 &dpyinfo->mouse_face_beg_row,
23046 &dpyinfo->mouse_face_beg_x,
23047 &dpyinfo->mouse_face_beg_y, Qnil);
23049 dpyinfo->mouse_face_past_end
23050 = !fast_find_position (w, XFASTINT (after),
23051 &dpyinfo->mouse_face_end_col,
23052 &dpyinfo->mouse_face_end_row,
23053 &dpyinfo->mouse_face_end_x,
23054 &dpyinfo->mouse_face_end_y, Qnil);
23055 dpyinfo->mouse_face_window = window;
23057 dpyinfo->mouse_face_face_id
23058 = face_at_buffer_position (w, pos, 0, 0,
23059 &ignore, pos + 1,
23060 !dpyinfo->mouse_face_hidden);
23062 /* Display it as active. */
23063 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23064 cursor = No_Cursor;
23066 /* Handle the text property case. */
23067 else if (!NILP (mouse_face) && BUFFERP (object))
23069 /* Find the range of text around this char that
23070 should be active. */
23071 Lisp_Object before, after, beginning, end;
23072 int ignore;
23074 beginning = Fmarker_position (w->start);
23075 end = make_number (BUF_Z (XBUFFER (object))
23076 - XFASTINT (w->window_end_pos));
23077 before
23078 = Fprevious_single_property_change (make_number (pos + 1),
23079 Qmouse_face,
23080 object, beginning);
23081 after
23082 = Fnext_single_property_change (position, Qmouse_face,
23083 object, end);
23085 /* Record this as the current active region. */
23086 fast_find_position (w, XFASTINT (before),
23087 &dpyinfo->mouse_face_beg_col,
23088 &dpyinfo->mouse_face_beg_row,
23089 &dpyinfo->mouse_face_beg_x,
23090 &dpyinfo->mouse_face_beg_y, Qnil);
23091 dpyinfo->mouse_face_past_end
23092 = !fast_find_position (w, XFASTINT (after),
23093 &dpyinfo->mouse_face_end_col,
23094 &dpyinfo->mouse_face_end_row,
23095 &dpyinfo->mouse_face_end_x,
23096 &dpyinfo->mouse_face_end_y, Qnil);
23097 dpyinfo->mouse_face_window = window;
23099 if (BUFFERP (object))
23100 dpyinfo->mouse_face_face_id
23101 = face_at_buffer_position (w, pos, 0, 0,
23102 &ignore, pos + 1,
23103 !dpyinfo->mouse_face_hidden);
23105 /* Display it as active. */
23106 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23107 cursor = No_Cursor;
23109 else if (!NILP (mouse_face) && STRINGP (object))
23111 Lisp_Object b, e;
23112 int ignore;
23114 b = Fprevious_single_property_change (make_number (pos + 1),
23115 Qmouse_face,
23116 object, Qnil);
23117 e = Fnext_single_property_change (position, Qmouse_face,
23118 object, Qnil);
23119 if (NILP (b))
23120 b = make_number (0);
23121 if (NILP (e))
23122 e = make_number (SCHARS (object) - 1);
23124 fast_find_string_pos (w, XINT (b), object,
23125 &dpyinfo->mouse_face_beg_col,
23126 &dpyinfo->mouse_face_beg_row,
23127 &dpyinfo->mouse_face_beg_x,
23128 &dpyinfo->mouse_face_beg_y, 0);
23129 fast_find_string_pos (w, XINT (e), object,
23130 &dpyinfo->mouse_face_end_col,
23131 &dpyinfo->mouse_face_end_row,
23132 &dpyinfo->mouse_face_end_x,
23133 &dpyinfo->mouse_face_end_y, 1);
23134 dpyinfo->mouse_face_past_end = 0;
23135 dpyinfo->mouse_face_window = window;
23136 dpyinfo->mouse_face_face_id
23137 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23138 glyph->face_id, 1);
23139 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23140 cursor = No_Cursor;
23142 else if (STRINGP (object) && NILP (mouse_face))
23144 /* A string which doesn't have mouse-face, but
23145 the text ``under'' it might have. */
23146 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23147 int start = MATRIX_ROW_START_CHARPOS (r);
23149 pos = string_buffer_position (w, object, start);
23150 if (pos > 0)
23151 mouse_face = get_char_property_and_overlay (make_number (pos),
23152 Qmouse_face,
23153 w->buffer,
23154 &overlay);
23155 if (!NILP (mouse_face) && !NILP (overlay))
23157 Lisp_Object before = Foverlay_start (overlay);
23158 Lisp_Object after = Foverlay_end (overlay);
23159 int ignore;
23161 /* Note that we might not be able to find position
23162 BEFORE in the glyph matrix if the overlay is
23163 entirely covered by a `display' property. In
23164 this case, we overshoot. So let's stop in
23165 the glyph matrix before glyphs for OBJECT. */
23166 fast_find_position (w, XFASTINT (before),
23167 &dpyinfo->mouse_face_beg_col,
23168 &dpyinfo->mouse_face_beg_row,
23169 &dpyinfo->mouse_face_beg_x,
23170 &dpyinfo->mouse_face_beg_y,
23171 object);
23173 dpyinfo->mouse_face_past_end
23174 = !fast_find_position (w, XFASTINT (after),
23175 &dpyinfo->mouse_face_end_col,
23176 &dpyinfo->mouse_face_end_row,
23177 &dpyinfo->mouse_face_end_x,
23178 &dpyinfo->mouse_face_end_y,
23179 Qnil);
23180 dpyinfo->mouse_face_window = window;
23181 dpyinfo->mouse_face_face_id
23182 = face_at_buffer_position (w, pos, 0, 0,
23183 &ignore, pos + 1,
23184 !dpyinfo->mouse_face_hidden);
23186 /* Display it as active. */
23187 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23188 cursor = No_Cursor;
23193 check_help_echo:
23195 /* Look for a `help-echo' property. */
23196 if (NILP (help_echo_string)) {
23197 Lisp_Object help, overlay;
23199 /* Check overlays first. */
23200 help = overlay = Qnil;
23201 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23203 overlay = overlay_vec[i];
23204 help = Foverlay_get (overlay, Qhelp_echo);
23207 if (!NILP (help))
23209 help_echo_string = help;
23210 help_echo_window = window;
23211 help_echo_object = overlay;
23212 help_echo_pos = pos;
23214 else
23216 Lisp_Object object = glyph->object;
23217 int charpos = glyph->charpos;
23219 /* Try text properties. */
23220 if (STRINGP (object)
23221 && charpos >= 0
23222 && charpos < SCHARS (object))
23224 help = Fget_text_property (make_number (charpos),
23225 Qhelp_echo, object);
23226 if (NILP (help))
23228 /* If the string itself doesn't specify a help-echo,
23229 see if the buffer text ``under'' it does. */
23230 struct glyph_row *r
23231 = MATRIX_ROW (w->current_matrix, vpos);
23232 int start = MATRIX_ROW_START_CHARPOS (r);
23233 int pos = string_buffer_position (w, object, start);
23234 if (pos > 0)
23236 help = Fget_char_property (make_number (pos),
23237 Qhelp_echo, w->buffer);
23238 if (!NILP (help))
23240 charpos = pos;
23241 object = w->buffer;
23246 else if (BUFFERP (object)
23247 && charpos >= BEGV
23248 && charpos < ZV)
23249 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23250 object);
23252 if (!NILP (help))
23254 help_echo_string = help;
23255 help_echo_window = window;
23256 help_echo_object = object;
23257 help_echo_pos = charpos;
23262 /* Look for a `pointer' property. */
23263 if (NILP (pointer))
23265 /* Check overlays first. */
23266 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23267 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23269 if (NILP (pointer))
23271 Lisp_Object object = glyph->object;
23272 int charpos = glyph->charpos;
23274 /* Try text properties. */
23275 if (STRINGP (object)
23276 && charpos >= 0
23277 && charpos < SCHARS (object))
23279 pointer = Fget_text_property (make_number (charpos),
23280 Qpointer, object);
23281 if (NILP (pointer))
23283 /* If the string itself doesn't specify a pointer,
23284 see if the buffer text ``under'' it does. */
23285 struct glyph_row *r
23286 = MATRIX_ROW (w->current_matrix, vpos);
23287 int start = MATRIX_ROW_START_CHARPOS (r);
23288 int pos = string_buffer_position (w, object, start);
23289 if (pos > 0)
23290 pointer = Fget_char_property (make_number (pos),
23291 Qpointer, w->buffer);
23294 else if (BUFFERP (object)
23295 && charpos >= BEGV
23296 && charpos < ZV)
23297 pointer = Fget_text_property (make_number (charpos),
23298 Qpointer, object);
23302 BEGV = obegv;
23303 ZV = ozv;
23304 current_buffer = obuf;
23307 set_cursor:
23309 define_frame_cursor1 (f, cursor, pointer);
23313 /* EXPORT for RIF:
23314 Clear any mouse-face on window W. This function is part of the
23315 redisplay interface, and is called from try_window_id and similar
23316 functions to ensure the mouse-highlight is off. */
23318 void
23319 x_clear_window_mouse_face (w)
23320 struct window *w;
23322 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23323 Lisp_Object window;
23325 BLOCK_INPUT;
23326 XSETWINDOW (window, w);
23327 if (EQ (window, dpyinfo->mouse_face_window))
23328 clear_mouse_face (dpyinfo);
23329 UNBLOCK_INPUT;
23333 /* EXPORT:
23334 Just discard the mouse face information for frame F, if any.
23335 This is used when the size of F is changed. */
23337 void
23338 cancel_mouse_face (f)
23339 struct frame *f;
23341 Lisp_Object window;
23342 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23344 window = dpyinfo->mouse_face_window;
23345 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23347 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23348 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23349 dpyinfo->mouse_face_window = Qnil;
23354 #endif /* HAVE_WINDOW_SYSTEM */
23357 /***********************************************************************
23358 Exposure Events
23359 ***********************************************************************/
23361 #ifdef HAVE_WINDOW_SYSTEM
23363 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23364 which intersects rectangle R. R is in window-relative coordinates. */
23366 static void
23367 expose_area (w, row, r, area)
23368 struct window *w;
23369 struct glyph_row *row;
23370 XRectangle *r;
23371 enum glyph_row_area area;
23373 struct glyph *first = row->glyphs[area];
23374 struct glyph *end = row->glyphs[area] + row->used[area];
23375 struct glyph *last;
23376 int first_x, start_x, x;
23378 if (area == TEXT_AREA && row->fill_line_p)
23379 /* If row extends face to end of line write the whole line. */
23380 draw_glyphs (w, 0, row, area,
23381 0, row->used[area],
23382 DRAW_NORMAL_TEXT, 0);
23383 else
23385 /* Set START_X to the window-relative start position for drawing glyphs of
23386 AREA. The first glyph of the text area can be partially visible.
23387 The first glyphs of other areas cannot. */
23388 start_x = window_box_left_offset (w, area);
23389 x = start_x;
23390 if (area == TEXT_AREA)
23391 x += row->x;
23393 /* Find the first glyph that must be redrawn. */
23394 while (first < end
23395 && x + first->pixel_width < r->x)
23397 x += first->pixel_width;
23398 ++first;
23401 /* Find the last one. */
23402 last = first;
23403 first_x = x;
23404 while (last < end
23405 && x < r->x + r->width)
23407 x += last->pixel_width;
23408 ++last;
23411 /* Repaint. */
23412 if (last > first)
23413 draw_glyphs (w, first_x - start_x, row, area,
23414 first - row->glyphs[area], last - row->glyphs[area],
23415 DRAW_NORMAL_TEXT, 0);
23420 /* Redraw the parts of the glyph row ROW on window W intersecting
23421 rectangle R. R is in window-relative coordinates. Value is
23422 non-zero if mouse-face was overwritten. */
23424 static int
23425 expose_line (w, row, r)
23426 struct window *w;
23427 struct glyph_row *row;
23428 XRectangle *r;
23430 xassert (row->enabled_p);
23432 if (row->mode_line_p || w->pseudo_window_p)
23433 draw_glyphs (w, 0, row, TEXT_AREA,
23434 0, row->used[TEXT_AREA],
23435 DRAW_NORMAL_TEXT, 0);
23436 else
23438 if (row->used[LEFT_MARGIN_AREA])
23439 expose_area (w, row, r, LEFT_MARGIN_AREA);
23440 if (row->used[TEXT_AREA])
23441 expose_area (w, row, r, TEXT_AREA);
23442 if (row->used[RIGHT_MARGIN_AREA])
23443 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23444 draw_row_fringe_bitmaps (w, row);
23447 return row->mouse_face_p;
23451 /* Redraw those parts of glyphs rows during expose event handling that
23452 overlap other rows. Redrawing of an exposed line writes over parts
23453 of lines overlapping that exposed line; this function fixes that.
23455 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23456 row in W's current matrix that is exposed and overlaps other rows.
23457 LAST_OVERLAPPING_ROW is the last such row. */
23459 static void
23460 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23461 struct window *w;
23462 struct glyph_row *first_overlapping_row;
23463 struct glyph_row *last_overlapping_row;
23465 struct glyph_row *row;
23467 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23468 if (row->overlapping_p)
23470 xassert (row->enabled_p && !row->mode_line_p);
23472 if (row->used[LEFT_MARGIN_AREA])
23473 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23475 if (row->used[TEXT_AREA])
23476 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23478 if (row->used[RIGHT_MARGIN_AREA])
23479 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23484 /* Return non-zero if W's cursor intersects rectangle R. */
23486 static int
23487 phys_cursor_in_rect_p (w, r)
23488 struct window *w;
23489 XRectangle *r;
23491 XRectangle cr, result;
23492 struct glyph *cursor_glyph;
23493 struct glyph_row *row;
23495 if (w->phys_cursor.vpos >= 0
23496 && w->phys_cursor.vpos < w->current_matrix->nrows
23497 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23498 row->enabled_p)
23499 && row->cursor_in_fringe_p)
23501 /* Cursor is in the fringe. */
23502 cr.x = window_box_right_offset (w,
23503 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23504 ? RIGHT_MARGIN_AREA
23505 : TEXT_AREA));
23506 cr.y = row->y;
23507 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23508 cr.height = row->height;
23509 return x_intersect_rectangles (&cr, r, &result);
23512 cursor_glyph = get_phys_cursor_glyph (w);
23513 if (cursor_glyph)
23515 /* r is relative to W's box, but w->phys_cursor.x is relative
23516 to left edge of W's TEXT area. Adjust it. */
23517 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23518 cr.y = w->phys_cursor.y;
23519 cr.width = cursor_glyph->pixel_width;
23520 cr.height = w->phys_cursor_height;
23521 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23522 I assume the effect is the same -- and this is portable. */
23523 return x_intersect_rectangles (&cr, r, &result);
23525 else
23526 return 0;
23530 /* EXPORT:
23531 Draw a vertical window border to the right of window W if W doesn't
23532 have vertical scroll bars. */
23534 void
23535 x_draw_vertical_border (w)
23536 struct window *w;
23538 /* We could do better, if we knew what type of scroll-bar the adjacent
23539 windows (on either side) have... But we don't :-(
23540 However, I think this works ok. ++KFS 2003-04-25 */
23542 /* Redraw borders between horizontally adjacent windows. Don't
23543 do it for frames with vertical scroll bars because either the
23544 right scroll bar of a window, or the left scroll bar of its
23545 neighbor will suffice as a border. */
23546 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23547 return;
23549 if (!WINDOW_RIGHTMOST_P (w)
23550 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23552 int x0, x1, y0, y1;
23554 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23555 y1 -= 1;
23557 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23558 x1 -= 1;
23560 rif->draw_vertical_window_border (w, x1, y0, y1);
23562 else if (!WINDOW_LEFTMOST_P (w)
23563 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23565 int x0, x1, y0, y1;
23567 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23568 y1 -= 1;
23570 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23571 x0 -= 1;
23573 rif->draw_vertical_window_border (w, x0, y0, y1);
23578 /* Redraw the part of window W intersection rectangle FR. Pixel
23579 coordinates in FR are frame-relative. Call this function with
23580 input blocked. Value is non-zero if the exposure overwrites
23581 mouse-face. */
23583 static int
23584 expose_window (w, fr)
23585 struct window *w;
23586 XRectangle *fr;
23588 struct frame *f = XFRAME (w->frame);
23589 XRectangle wr, r;
23590 int mouse_face_overwritten_p = 0;
23592 /* If window is not yet fully initialized, do nothing. This can
23593 happen when toolkit scroll bars are used and a window is split.
23594 Reconfiguring the scroll bar will generate an expose for a newly
23595 created window. */
23596 if (w->current_matrix == NULL)
23597 return 0;
23599 /* When we're currently updating the window, display and current
23600 matrix usually don't agree. Arrange for a thorough display
23601 later. */
23602 if (w == updated_window)
23604 SET_FRAME_GARBAGED (f);
23605 return 0;
23608 /* Frame-relative pixel rectangle of W. */
23609 wr.x = WINDOW_LEFT_EDGE_X (w);
23610 wr.y = WINDOW_TOP_EDGE_Y (w);
23611 wr.width = WINDOW_TOTAL_WIDTH (w);
23612 wr.height = WINDOW_TOTAL_HEIGHT (w);
23614 if (x_intersect_rectangles (fr, &wr, &r))
23616 int yb = window_text_bottom_y (w);
23617 struct glyph_row *row;
23618 int cursor_cleared_p;
23619 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23621 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23622 r.x, r.y, r.width, r.height));
23624 /* Convert to window coordinates. */
23625 r.x -= WINDOW_LEFT_EDGE_X (w);
23626 r.y -= WINDOW_TOP_EDGE_Y (w);
23628 /* Turn off the cursor. */
23629 if (!w->pseudo_window_p
23630 && phys_cursor_in_rect_p (w, &r))
23632 x_clear_cursor (w);
23633 cursor_cleared_p = 1;
23635 else
23636 cursor_cleared_p = 0;
23638 /* Update lines intersecting rectangle R. */
23639 first_overlapping_row = last_overlapping_row = NULL;
23640 for (row = w->current_matrix->rows;
23641 row->enabled_p;
23642 ++row)
23644 int y0 = row->y;
23645 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23647 if ((y0 >= r.y && y0 < r.y + r.height)
23648 || (y1 > r.y && y1 < r.y + r.height)
23649 || (r.y >= y0 && r.y < y1)
23650 || (r.y + r.height > y0 && r.y + r.height < y1))
23652 /* A header line may be overlapping, but there is no need
23653 to fix overlapping areas for them. KFS 2005-02-12 */
23654 if (row->overlapping_p && !row->mode_line_p)
23656 if (first_overlapping_row == NULL)
23657 first_overlapping_row = row;
23658 last_overlapping_row = row;
23661 if (expose_line (w, row, &r))
23662 mouse_face_overwritten_p = 1;
23665 if (y1 >= yb)
23666 break;
23669 /* Display the mode line if there is one. */
23670 if (WINDOW_WANTS_MODELINE_P (w)
23671 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23672 row->enabled_p)
23673 && row->y < r.y + r.height)
23675 if (expose_line (w, row, &r))
23676 mouse_face_overwritten_p = 1;
23679 if (!w->pseudo_window_p)
23681 /* Fix the display of overlapping rows. */
23682 if (first_overlapping_row)
23683 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23685 /* Draw border between windows. */
23686 x_draw_vertical_border (w);
23688 /* Turn the cursor on again. */
23689 if (cursor_cleared_p)
23690 update_window_cursor (w, 1);
23694 return mouse_face_overwritten_p;
23699 /* Redraw (parts) of all windows in the window tree rooted at W that
23700 intersect R. R contains frame pixel coordinates. Value is
23701 non-zero if the exposure overwrites mouse-face. */
23703 static int
23704 expose_window_tree (w, r)
23705 struct window *w;
23706 XRectangle *r;
23708 struct frame *f = XFRAME (w->frame);
23709 int mouse_face_overwritten_p = 0;
23711 while (w && !FRAME_GARBAGED_P (f))
23713 if (!NILP (w->hchild))
23714 mouse_face_overwritten_p
23715 |= expose_window_tree (XWINDOW (w->hchild), r);
23716 else if (!NILP (w->vchild))
23717 mouse_face_overwritten_p
23718 |= expose_window_tree (XWINDOW (w->vchild), r);
23719 else
23720 mouse_face_overwritten_p |= expose_window (w, r);
23722 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23725 return mouse_face_overwritten_p;
23729 /* EXPORT:
23730 Redisplay an exposed area of frame F. X and Y are the upper-left
23731 corner of the exposed rectangle. W and H are width and height of
23732 the exposed area. All are pixel values. W or H zero means redraw
23733 the entire frame. */
23735 void
23736 expose_frame (f, x, y, w, h)
23737 struct frame *f;
23738 int x, y, w, h;
23740 XRectangle r;
23741 int mouse_face_overwritten_p = 0;
23743 TRACE ((stderr, "expose_frame "));
23745 /* No need to redraw if frame will be redrawn soon. */
23746 if (FRAME_GARBAGED_P (f))
23748 TRACE ((stderr, " garbaged\n"));
23749 return;
23752 /* If basic faces haven't been realized yet, there is no point in
23753 trying to redraw anything. This can happen when we get an expose
23754 event while Emacs is starting, e.g. by moving another window. */
23755 if (FRAME_FACE_CACHE (f) == NULL
23756 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23758 TRACE ((stderr, " no faces\n"));
23759 return;
23762 if (w == 0 || h == 0)
23764 r.x = r.y = 0;
23765 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23766 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23768 else
23770 r.x = x;
23771 r.y = y;
23772 r.width = w;
23773 r.height = h;
23776 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23777 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23779 if (WINDOWP (f->tool_bar_window))
23780 mouse_face_overwritten_p
23781 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23783 #ifdef HAVE_X_WINDOWS
23784 #ifndef MSDOS
23785 #ifndef USE_X_TOOLKIT
23786 if (WINDOWP (f->menu_bar_window))
23787 mouse_face_overwritten_p
23788 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23789 #endif /* not USE_X_TOOLKIT */
23790 #endif
23791 #endif
23793 /* Some window managers support a focus-follows-mouse style with
23794 delayed raising of frames. Imagine a partially obscured frame,
23795 and moving the mouse into partially obscured mouse-face on that
23796 frame. The visible part of the mouse-face will be highlighted,
23797 then the WM raises the obscured frame. With at least one WM, KDE
23798 2.1, Emacs is not getting any event for the raising of the frame
23799 (even tried with SubstructureRedirectMask), only Expose events.
23800 These expose events will draw text normally, i.e. not
23801 highlighted. Which means we must redo the highlight here.
23802 Subsume it under ``we love X''. --gerd 2001-08-15 */
23803 /* Included in Windows version because Windows most likely does not
23804 do the right thing if any third party tool offers
23805 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23806 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23808 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23809 if (f == dpyinfo->mouse_face_mouse_frame)
23811 int x = dpyinfo->mouse_face_mouse_x;
23812 int y = dpyinfo->mouse_face_mouse_y;
23813 clear_mouse_face (dpyinfo);
23814 note_mouse_highlight (f, x, y);
23820 /* EXPORT:
23821 Determine the intersection of two rectangles R1 and R2. Return
23822 the intersection in *RESULT. Value is non-zero if RESULT is not
23823 empty. */
23826 x_intersect_rectangles (r1, r2, result)
23827 XRectangle *r1, *r2, *result;
23829 XRectangle *left, *right;
23830 XRectangle *upper, *lower;
23831 int intersection_p = 0;
23833 /* Rearrange so that R1 is the left-most rectangle. */
23834 if (r1->x < r2->x)
23835 left = r1, right = r2;
23836 else
23837 left = r2, right = r1;
23839 /* X0 of the intersection is right.x0, if this is inside R1,
23840 otherwise there is no intersection. */
23841 if (right->x <= left->x + left->width)
23843 result->x = right->x;
23845 /* The right end of the intersection is the minimum of the
23846 the right ends of left and right. */
23847 result->width = (min (left->x + left->width, right->x + right->width)
23848 - result->x);
23850 /* Same game for Y. */
23851 if (r1->y < r2->y)
23852 upper = r1, lower = r2;
23853 else
23854 upper = r2, lower = r1;
23856 /* The upper end of the intersection is lower.y0, if this is inside
23857 of upper. Otherwise, there is no intersection. */
23858 if (lower->y <= upper->y + upper->height)
23860 result->y = lower->y;
23862 /* The lower end of the intersection is the minimum of the lower
23863 ends of upper and lower. */
23864 result->height = (min (lower->y + lower->height,
23865 upper->y + upper->height)
23866 - result->y);
23867 intersection_p = 1;
23871 return intersection_p;
23874 #endif /* HAVE_WINDOW_SYSTEM */
23877 /***********************************************************************
23878 Initialization
23879 ***********************************************************************/
23881 void
23882 syms_of_xdisp ()
23884 Vwith_echo_area_save_vector = Qnil;
23885 staticpro (&Vwith_echo_area_save_vector);
23887 Vmessage_stack = Qnil;
23888 staticpro (&Vmessage_stack);
23890 Qinhibit_redisplay = intern ("inhibit-redisplay");
23891 staticpro (&Qinhibit_redisplay);
23893 message_dolog_marker1 = Fmake_marker ();
23894 staticpro (&message_dolog_marker1);
23895 message_dolog_marker2 = Fmake_marker ();
23896 staticpro (&message_dolog_marker2);
23897 message_dolog_marker3 = Fmake_marker ();
23898 staticpro (&message_dolog_marker3);
23900 #if GLYPH_DEBUG
23901 defsubr (&Sdump_frame_glyph_matrix);
23902 defsubr (&Sdump_glyph_matrix);
23903 defsubr (&Sdump_glyph_row);
23904 defsubr (&Sdump_tool_bar_row);
23905 defsubr (&Strace_redisplay);
23906 defsubr (&Strace_to_stderr);
23907 #endif
23908 #ifdef HAVE_WINDOW_SYSTEM
23909 defsubr (&Stool_bar_lines_needed);
23910 defsubr (&Slookup_image_map);
23911 #endif
23912 defsubr (&Sformat_mode_line);
23914 staticpro (&Qmenu_bar_update_hook);
23915 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23917 staticpro (&Qoverriding_terminal_local_map);
23918 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23920 staticpro (&Qoverriding_local_map);
23921 Qoverriding_local_map = intern ("overriding-local-map");
23923 staticpro (&Qwindow_scroll_functions);
23924 Qwindow_scroll_functions = intern ("window-scroll-functions");
23926 staticpro (&Qredisplay_end_trigger_functions);
23927 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23929 staticpro (&Qinhibit_point_motion_hooks);
23930 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23932 QCdata = intern (":data");
23933 staticpro (&QCdata);
23934 Qdisplay = intern ("display");
23935 staticpro (&Qdisplay);
23936 Qspace_width = intern ("space-width");
23937 staticpro (&Qspace_width);
23938 Qraise = intern ("raise");
23939 staticpro (&Qraise);
23940 Qslice = intern ("slice");
23941 staticpro (&Qslice);
23942 Qspace = intern ("space");
23943 staticpro (&Qspace);
23944 Qmargin = intern ("margin");
23945 staticpro (&Qmargin);
23946 Qpointer = intern ("pointer");
23947 staticpro (&Qpointer);
23948 Qleft_margin = intern ("left-margin");
23949 staticpro (&Qleft_margin);
23950 Qright_margin = intern ("right-margin");
23951 staticpro (&Qright_margin);
23952 Qcenter = intern ("center");
23953 staticpro (&Qcenter);
23954 Qline_height = intern ("line-height");
23955 staticpro (&Qline_height);
23956 QCalign_to = intern (":align-to");
23957 staticpro (&QCalign_to);
23958 QCrelative_width = intern (":relative-width");
23959 staticpro (&QCrelative_width);
23960 QCrelative_height = intern (":relative-height");
23961 staticpro (&QCrelative_height);
23962 QCeval = intern (":eval");
23963 staticpro (&QCeval);
23964 QCpropertize = intern (":propertize");
23965 staticpro (&QCpropertize);
23966 QCfile = intern (":file");
23967 staticpro (&QCfile);
23968 Qfontified = intern ("fontified");
23969 staticpro (&Qfontified);
23970 Qfontification_functions = intern ("fontification-functions");
23971 staticpro (&Qfontification_functions);
23972 Qtrailing_whitespace = intern ("trailing-whitespace");
23973 staticpro (&Qtrailing_whitespace);
23974 Qescape_glyph = intern ("escape-glyph");
23975 staticpro (&Qescape_glyph);
23976 Qnobreak_space = intern ("nobreak-space");
23977 staticpro (&Qnobreak_space);
23978 Qimage = intern ("image");
23979 staticpro (&Qimage);
23980 QCmap = intern (":map");
23981 staticpro (&QCmap);
23982 QCpointer = intern (":pointer");
23983 staticpro (&QCpointer);
23984 Qrect = intern ("rect");
23985 staticpro (&Qrect);
23986 Qcircle = intern ("circle");
23987 staticpro (&Qcircle);
23988 Qpoly = intern ("poly");
23989 staticpro (&Qpoly);
23990 Qmessage_truncate_lines = intern ("message-truncate-lines");
23991 staticpro (&Qmessage_truncate_lines);
23992 Qgrow_only = intern ("grow-only");
23993 staticpro (&Qgrow_only);
23994 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23995 staticpro (&Qinhibit_menubar_update);
23996 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23997 staticpro (&Qinhibit_eval_during_redisplay);
23998 Qposition = intern ("position");
23999 staticpro (&Qposition);
24000 Qbuffer_position = intern ("buffer-position");
24001 staticpro (&Qbuffer_position);
24002 Qobject = intern ("object");
24003 staticpro (&Qobject);
24004 Qbar = intern ("bar");
24005 staticpro (&Qbar);
24006 Qhbar = intern ("hbar");
24007 staticpro (&Qhbar);
24008 Qbox = intern ("box");
24009 staticpro (&Qbox);
24010 Qhollow = intern ("hollow");
24011 staticpro (&Qhollow);
24012 Qhand = intern ("hand");
24013 staticpro (&Qhand);
24014 Qarrow = intern ("arrow");
24015 staticpro (&Qarrow);
24016 Qtext = intern ("text");
24017 staticpro (&Qtext);
24018 Qrisky_local_variable = intern ("risky-local-variable");
24019 staticpro (&Qrisky_local_variable);
24020 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24021 staticpro (&Qinhibit_free_realized_faces);
24023 list_of_error = Fcons (Fcons (intern ("error"),
24024 Fcons (intern ("void-variable"), Qnil)),
24025 Qnil);
24026 staticpro (&list_of_error);
24028 Qlast_arrow_position = intern ("last-arrow-position");
24029 staticpro (&Qlast_arrow_position);
24030 Qlast_arrow_string = intern ("last-arrow-string");
24031 staticpro (&Qlast_arrow_string);
24033 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24034 staticpro (&Qoverlay_arrow_string);
24035 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24036 staticpro (&Qoverlay_arrow_bitmap);
24038 echo_buffer[0] = echo_buffer[1] = Qnil;
24039 staticpro (&echo_buffer[0]);
24040 staticpro (&echo_buffer[1]);
24042 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24043 staticpro (&echo_area_buffer[0]);
24044 staticpro (&echo_area_buffer[1]);
24046 Vmessages_buffer_name = build_string ("*Messages*");
24047 staticpro (&Vmessages_buffer_name);
24049 mode_line_proptrans_alist = Qnil;
24050 staticpro (&mode_line_proptrans_alist);
24051 mode_line_string_list = Qnil;
24052 staticpro (&mode_line_string_list);
24053 mode_line_string_face = Qnil;
24054 staticpro (&mode_line_string_face);
24055 mode_line_string_face_prop = Qnil;
24056 staticpro (&mode_line_string_face_prop);
24057 Vmode_line_unwind_vector = Qnil;
24058 staticpro (&Vmode_line_unwind_vector);
24060 help_echo_string = Qnil;
24061 staticpro (&help_echo_string);
24062 help_echo_object = Qnil;
24063 staticpro (&help_echo_object);
24064 help_echo_window = Qnil;
24065 staticpro (&help_echo_window);
24066 previous_help_echo_string = Qnil;
24067 staticpro (&previous_help_echo_string);
24068 help_echo_pos = -1;
24070 #ifdef HAVE_WINDOW_SYSTEM
24071 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24072 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24073 For example, if a block cursor is over a tab, it will be drawn as
24074 wide as that tab on the display. */);
24075 x_stretch_cursor_p = 0;
24076 #endif
24078 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24079 doc: /* *Non-nil means highlight trailing whitespace.
24080 The face used for trailing whitespace is `trailing-whitespace'. */);
24081 Vshow_trailing_whitespace = Qnil;
24083 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24084 doc: /* *Control highlighting of nobreak space and soft hyphen.
24085 A value of t means highlight the character itself (for nobreak space,
24086 use face `nobreak-space').
24087 A value of nil means no highlighting.
24088 Other values mean display the escape glyph followed by an ordinary
24089 space or ordinary hyphen. */);
24090 Vnobreak_char_display = Qt;
24092 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24093 doc: /* *The pointer shape to show in void text areas.
24094 A value of nil means to show the text pointer. Other options are `arrow',
24095 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24096 Vvoid_text_area_pointer = Qarrow;
24098 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24099 doc: /* Non-nil means don't actually do any redisplay.
24100 This is used for internal purposes. */);
24101 Vinhibit_redisplay = Qnil;
24103 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24104 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24105 Vglobal_mode_string = Qnil;
24107 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24108 doc: /* Marker for where to display an arrow on top of the buffer text.
24109 This must be the beginning of a line in order to work.
24110 See also `overlay-arrow-string'. */);
24111 Voverlay_arrow_position = Qnil;
24113 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24114 doc: /* String to display as an arrow in non-window frames.
24115 See also `overlay-arrow-position'. */);
24116 Voverlay_arrow_string = build_string ("=>");
24118 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24119 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24120 The symbols on this list are examined during redisplay to determine
24121 where to display overlay arrows. */);
24122 Voverlay_arrow_variable_list
24123 = Fcons (intern ("overlay-arrow-position"), Qnil);
24125 DEFVAR_INT ("scroll-step", &scroll_step,
24126 doc: /* *The number of lines to try scrolling a window by when point moves out.
24127 If that fails to bring point back on frame, point is centered instead.
24128 If this is zero, point is always centered after it moves off frame.
24129 If you want scrolling to always be a line at a time, you should set
24130 `scroll-conservatively' to a large value rather than set this to 1. */);
24132 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24133 doc: /* *Scroll up to this many lines, to bring point back on screen.
24134 If point moves off-screen, redisplay will scroll by up to
24135 `scroll-conservatively' lines in order to bring point just barely
24136 onto the screen again. If that cannot be done, then redisplay
24137 recenters point as usual.
24139 A value of zero means always recenter point if it moves off screen. */);
24140 scroll_conservatively = 0;
24142 DEFVAR_INT ("scroll-margin", &scroll_margin,
24143 doc: /* *Number of lines of margin at the top and bottom of a window.
24144 Recenter the window whenever point gets within this many lines
24145 of the top or bottom of the window. */);
24146 scroll_margin = 0;
24148 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24149 doc: /* Pixels per inch value for non-window system displays.
24150 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24151 Vdisplay_pixels_per_inch = make_float (72.0);
24153 #if GLYPH_DEBUG
24154 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24155 #endif
24157 DEFVAR_BOOL ("truncate-partial-width-windows",
24158 &truncate_partial_width_windows,
24159 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24160 truncate_partial_width_windows = 1;
24162 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24163 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24164 Any other value means to use the appropriate face, `mode-line',
24165 `header-line', or `menu' respectively. */);
24166 mode_line_inverse_video = 1;
24168 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24169 doc: /* *Maximum buffer size for which line number should be displayed.
24170 If the buffer is bigger than this, the line number does not appear
24171 in the mode line. A value of nil means no limit. */);
24172 Vline_number_display_limit = Qnil;
24174 DEFVAR_INT ("line-number-display-limit-width",
24175 &line_number_display_limit_width,
24176 doc: /* *Maximum line width (in characters) for line number display.
24177 If the average length of the lines near point is bigger than this, then the
24178 line number may be omitted from the mode line. */);
24179 line_number_display_limit_width = 200;
24181 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24182 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24183 highlight_nonselected_windows = 0;
24185 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24186 doc: /* Non-nil if more than one frame is visible on this display.
24187 Minibuffer-only frames don't count, but iconified frames do.
24188 This variable is not guaranteed to be accurate except while processing
24189 `frame-title-format' and `icon-title-format'. */);
24191 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24192 doc: /* Template for displaying the title bar of visible frames.
24193 \(Assuming the window manager supports this feature.)
24195 This variable has the same structure as `mode-line-format', except that
24196 the %c and %l constructs are ignored. It is used only on frames for
24197 which no explicit name has been set \(see `modify-frame-parameters'). */);
24199 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24200 doc: /* Template for displaying the title bar of an iconified frame.
24201 \(Assuming the window manager supports this feature.)
24202 This variable has the same structure as `mode-line-format' (which see),
24203 and is used only on frames for which no explicit name has been set
24204 \(see `modify-frame-parameters'). */);
24205 Vicon_title_format
24206 = Vframe_title_format
24207 = Fcons (intern ("multiple-frames"),
24208 Fcons (build_string ("%b"),
24209 Fcons (Fcons (empty_string,
24210 Fcons (intern ("invocation-name"),
24211 Fcons (build_string ("@"),
24212 Fcons (intern ("system-name"),
24213 Qnil)))),
24214 Qnil)));
24216 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24217 doc: /* Maximum number of lines to keep in the message log buffer.
24218 If nil, disable message logging. If t, log messages but don't truncate
24219 the buffer when it becomes large. */);
24220 Vmessage_log_max = make_number (100);
24222 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24223 doc: /* Functions called before redisplay, if window sizes have changed.
24224 The value should be a list of functions that take one argument.
24225 Just before redisplay, for each frame, if any of its windows have changed
24226 size since the last redisplay, or have been split or deleted,
24227 all the functions in the list are called, with the frame as argument. */);
24228 Vwindow_size_change_functions = Qnil;
24230 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24231 doc: /* List of functions to call before redisplaying a window with scrolling.
24232 Each function is called with two arguments, the window
24233 and its new display-start position. Note that the value of `window-end'
24234 is not valid when these functions are called. */);
24235 Vwindow_scroll_functions = Qnil;
24237 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24238 doc: /* Functions called when redisplay of a window reaches the end trigger.
24239 Each function is called with two arguments, the window and the end trigger value.
24240 See `set-window-redisplay-end-trigger'. */);
24241 Vredisplay_end_trigger_functions = Qnil;
24243 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24244 doc: /* *Non-nil means autoselect window with mouse pointer.
24245 If nil, do not autoselect windows.
24246 A positive number means delay autoselection by that many seconds: a
24247 window is autoselected only after the mouse has remained in that
24248 window for the duration of the delay.
24249 A negative number has a similar effect, but causes windows to be
24250 autoselected only after the mouse has stopped moving. \(Because of
24251 the way Emacs compares mouse events, you will occasionally wait twice
24252 that time before the window gets selected.\)
24253 Any other value means to autoselect window instantaneously when the
24254 mouse pointer enters it.
24256 Autoselection selects the minibuffer only if it is active, and never
24257 unselects the minibuffer if it is active.
24259 When customizing this variable make sure that the actual value of
24260 `focus-follows-mouse' matches the behavior of your window manager. */);
24261 Vmouse_autoselect_window = Qnil;
24263 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24264 doc: /* *Non-nil means automatically resize tool-bars.
24265 This dynamically changes the tool-bar's height to the minimum height
24266 that is needed to make all tool-bar items visible.
24267 If value is `grow-only', the tool-bar's height is only increased
24268 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24269 Vauto_resize_tool_bars = Qt;
24271 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24272 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24273 auto_raise_tool_bar_buttons_p = 1;
24275 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24276 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24277 make_cursor_line_fully_visible_p = 1;
24279 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24280 doc: /* *Border below tool-bar in pixels.
24281 If an integer, use it as the height of the border.
24282 If it is one of `internal-border-width' or `border-width', use the
24283 value of the corresponding frame parameter.
24284 Otherwise, no border is added below the tool-bar. */);
24285 Vtool_bar_border = Qinternal_border_width;
24287 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24288 doc: /* *Margin around tool-bar buttons in pixels.
24289 If an integer, use that for both horizontal and vertical margins.
24290 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24291 HORZ specifying the horizontal margin, and VERT specifying the
24292 vertical margin. */);
24293 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24295 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24296 doc: /* *Relief thickness of tool-bar buttons. */);
24297 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24299 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24300 doc: /* List of functions to call to fontify regions of text.
24301 Each function is called with one argument POS. Functions must
24302 fontify a region starting at POS in the current buffer, and give
24303 fontified regions the property `fontified'. */);
24304 Vfontification_functions = Qnil;
24305 Fmake_variable_buffer_local (Qfontification_functions);
24307 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24308 &unibyte_display_via_language_environment,
24309 doc: /* *Non-nil means display unibyte text according to language environment.
24310 Specifically this means that unibyte non-ASCII characters
24311 are displayed by converting them to the equivalent multibyte characters
24312 according to the current language environment. As a result, they are
24313 displayed according to the current fontset. */);
24314 unibyte_display_via_language_environment = 0;
24316 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24317 doc: /* *Maximum height for resizing mini-windows.
24318 If a float, it specifies a fraction of the mini-window frame's height.
24319 If an integer, it specifies a number of lines. */);
24320 Vmax_mini_window_height = make_float (0.25);
24322 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24323 doc: /* *How to resize mini-windows.
24324 A value of nil means don't automatically resize mini-windows.
24325 A value of t means resize them to fit the text displayed in them.
24326 A value of `grow-only', the default, means let mini-windows grow
24327 only, until their display becomes empty, at which point the windows
24328 go back to their normal size. */);
24329 Vresize_mini_windows = Qgrow_only;
24331 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24332 doc: /* Alist specifying how to blink the cursor off.
24333 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24334 `cursor-type' frame-parameter or variable equals ON-STATE,
24335 comparing using `equal', Emacs uses OFF-STATE to specify
24336 how to blink it off. ON-STATE and OFF-STATE are values for
24337 the `cursor-type' frame parameter.
24339 If a frame's ON-STATE has no entry in this list,
24340 the frame's other specifications determine how to blink the cursor off. */);
24341 Vblink_cursor_alist = Qnil;
24343 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24344 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24345 automatic_hscrolling_p = 1;
24347 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24348 doc: /* *How many columns away from the window edge point is allowed to get
24349 before automatic hscrolling will horizontally scroll the window. */);
24350 hscroll_margin = 5;
24352 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24353 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24354 When point is less than `hscroll-margin' columns from the window
24355 edge, automatic hscrolling will scroll the window by the amount of columns
24356 determined by this variable. If its value is a positive integer, scroll that
24357 many columns. If it's a positive floating-point number, it specifies the
24358 fraction of the window's width to scroll. If it's nil or zero, point will be
24359 centered horizontally after the scroll. Any other value, including negative
24360 numbers, are treated as if the value were zero.
24362 Automatic hscrolling always moves point outside the scroll margin, so if
24363 point was more than scroll step columns inside the margin, the window will
24364 scroll more than the value given by the scroll step.
24366 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24367 and `scroll-right' overrides this variable's effect. */);
24368 Vhscroll_step = make_number (0);
24370 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24371 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24372 Bind this around calls to `message' to let it take effect. */);
24373 message_truncate_lines = 0;
24375 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24376 doc: /* Normal hook run to update the menu bar definitions.
24377 Redisplay runs this hook before it redisplays the menu bar.
24378 This is used to update submenus such as Buffers,
24379 whose contents depend on various data. */);
24380 Vmenu_bar_update_hook = Qnil;
24382 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24383 doc: /* Frame for which we are updating a menu.
24384 The enable predicate for a menu binding should check this variable. */);
24385 Vmenu_updating_frame = Qnil;
24387 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24388 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24389 inhibit_menubar_update = 0;
24391 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24392 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24393 inhibit_eval_during_redisplay = 0;
24395 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24396 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24397 inhibit_free_realized_faces = 0;
24399 #if GLYPH_DEBUG
24400 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24401 doc: /* Inhibit try_window_id display optimization. */);
24402 inhibit_try_window_id = 0;
24404 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24405 doc: /* Inhibit try_window_reusing display optimization. */);
24406 inhibit_try_window_reusing = 0;
24408 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24409 doc: /* Inhibit try_cursor_movement display optimization. */);
24410 inhibit_try_cursor_movement = 0;
24411 #endif /* GLYPH_DEBUG */
24413 DEFVAR_INT ("overline-margin", &overline_margin,
24414 doc: /* *Space between overline and text, in pixels.
24415 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24416 margin to the caracter height. */);
24417 overline_margin = 2;
24421 /* Initialize this module when Emacs starts. */
24423 void
24424 init_xdisp ()
24426 Lisp_Object root_window;
24427 struct window *mini_w;
24429 current_header_line_height = current_mode_line_height = -1;
24431 CHARPOS (this_line_start_pos) = 0;
24433 mini_w = XWINDOW (minibuf_window);
24434 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24436 if (!noninteractive)
24438 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24439 int i;
24441 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24442 set_window_height (root_window,
24443 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24445 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24446 set_window_height (minibuf_window, 1, 0);
24448 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24449 mini_w->total_cols = make_number (FRAME_COLS (f));
24451 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24452 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24453 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24455 /* The default ellipsis glyphs `...'. */
24456 for (i = 0; i < 3; ++i)
24457 default_invis_vector[i] = make_number ('.');
24461 /* Allocate the buffer for frame titles.
24462 Also used for `format-mode-line'. */
24463 int size = 100;
24464 mode_line_noprop_buf = (char *) xmalloc (size);
24465 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24466 mode_line_noprop_ptr = mode_line_noprop_buf;
24467 mode_line_target = MODE_LINE_DISPLAY;
24470 help_echo_showing_p = 0;
24474 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24475 (do not change this comment) */