Don't require `cl'.
[emacs.git] / src / xdisp.c
blobd22c9a53bc3cc01f8d6e51cc0832856f9b5c0398
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 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, struct text_pos *,
971 int));
972 static int underlying_face_id P_ ((struct it *));
973 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
974 struct window *));
976 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
977 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
979 #ifdef HAVE_WINDOW_SYSTEM
981 static void update_tool_bar P_ ((struct frame *, int));
982 static void build_desired_tool_bar_string P_ ((struct frame *f));
983 static int redisplay_tool_bar P_ ((struct frame *));
984 static void display_tool_bar_line P_ ((struct it *, int));
985 static void notice_overwritten_cursor P_ ((struct window *,
986 enum glyph_row_area,
987 int, int, int, int));
991 #endif /* HAVE_WINDOW_SYSTEM */
994 /***********************************************************************
995 Window display dimensions
996 ***********************************************************************/
998 /* Return the bottom boundary y-position for text lines in window W.
999 This is the first y position at which a line cannot start.
1000 It is relative to the top of the window.
1002 This is the height of W minus the height of a mode line, if any. */
1004 INLINE int
1005 window_text_bottom_y (w)
1006 struct window *w;
1008 int height = WINDOW_TOTAL_HEIGHT (w);
1010 if (WINDOW_WANTS_MODELINE_P (w))
1011 height -= CURRENT_MODE_LINE_HEIGHT (w);
1012 return height;
1015 /* Return the pixel width of display area AREA of window W. AREA < 0
1016 means return the total width of W, not including fringes to
1017 the left and right of the window. */
1019 INLINE int
1020 window_box_width (w, area)
1021 struct window *w;
1022 int area;
1024 int cols = XFASTINT (w->total_cols);
1025 int pixels = 0;
1027 if (!w->pseudo_window_p)
1029 cols -= WINDOW_SCROLL_BAR_COLS (w);
1031 if (area == TEXT_AREA)
1033 if (INTEGERP (w->left_margin_cols))
1034 cols -= XFASTINT (w->left_margin_cols);
1035 if (INTEGERP (w->right_margin_cols))
1036 cols -= XFASTINT (w->right_margin_cols);
1037 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1039 else if (area == LEFT_MARGIN_AREA)
1041 cols = (INTEGERP (w->left_margin_cols)
1042 ? XFASTINT (w->left_margin_cols) : 0);
1043 pixels = 0;
1045 else if (area == RIGHT_MARGIN_AREA)
1047 cols = (INTEGERP (w->right_margin_cols)
1048 ? XFASTINT (w->right_margin_cols) : 0);
1049 pixels = 0;
1053 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1057 /* Return the pixel height of the display area of window W, not
1058 including mode lines of W, if any. */
1060 INLINE int
1061 window_box_height (w)
1062 struct window *w;
1064 struct frame *f = XFRAME (w->frame);
1065 int height = WINDOW_TOTAL_HEIGHT (w);
1067 xassert (height >= 0);
1069 /* Note: the code below that determines the mode-line/header-line
1070 height is essentially the same as that contained in the macro
1071 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1072 the appropriate glyph row has its `mode_line_p' flag set,
1073 and if it doesn't, uses estimate_mode_line_height instead. */
1075 if (WINDOW_WANTS_MODELINE_P (w))
1077 struct glyph_row *ml_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (ml_row && ml_row->mode_line_p)
1082 height -= ml_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1087 if (WINDOW_WANTS_HEADER_LINE_P (w))
1089 struct glyph_row *hl_row
1090 = (w->current_matrix && w->current_matrix->rows
1091 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1092 : 0);
1093 if (hl_row && hl_row->mode_line_p)
1094 height -= hl_row->height;
1095 else
1096 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1099 /* With a very small font and a mode-line that's taller than
1100 default, we might end up with a negative height. */
1101 return max (0, height);
1104 /* Return the window-relative coordinate of the left edge of display
1105 area AREA of window W. AREA < 0 means return the left edge of the
1106 whole window, to the right of the left fringe of W. */
1108 INLINE int
1109 window_box_left_offset (w, area)
1110 struct window *w;
1111 int area;
1113 int x;
1115 if (w->pseudo_window_p)
1116 return 0;
1118 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1120 if (area == TEXT_AREA)
1121 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1122 + window_box_width (w, LEFT_MARGIN_AREA));
1123 else if (area == RIGHT_MARGIN_AREA)
1124 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1125 + window_box_width (w, LEFT_MARGIN_AREA)
1126 + window_box_width (w, TEXT_AREA)
1127 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1129 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1130 else if (area == LEFT_MARGIN_AREA
1131 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1132 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1134 return x;
1138 /* Return the window-relative coordinate of the right edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the left of the right fringe of W. */
1142 INLINE int
1143 window_box_right_offset (w, area)
1144 struct window *w;
1145 int area;
1147 return window_box_left_offset (w, area) + window_box_width (w, area);
1150 /* Return the frame-relative coordinate of the left edge of display
1151 area AREA of window W. AREA < 0 means return the left edge of the
1152 whole window, to the right of the left fringe of W. */
1154 INLINE int
1155 window_box_left (w, area)
1156 struct window *w;
1157 int area;
1159 struct frame *f = XFRAME (w->frame);
1160 int x;
1162 if (w->pseudo_window_p)
1163 return FRAME_INTERNAL_BORDER_WIDTH (f);
1165 x = (WINDOW_LEFT_EDGE_X (w)
1166 + window_box_left_offset (w, area));
1168 return x;
1172 /* Return the frame-relative coordinate of the right edge of display
1173 area AREA of window W. AREA < 0 means return the left edge of the
1174 whole window, to the left of the right fringe of W. */
1176 INLINE int
1177 window_box_right (w, area)
1178 struct window *w;
1179 int area;
1181 return window_box_left (w, area) + window_box_width (w, area);
1184 /* Get the bounding box of the display area AREA of window W, without
1185 mode lines, in frame-relative coordinates. AREA < 0 means the
1186 whole window, not including the left and right fringes of
1187 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1188 coordinates of the upper-left corner of the box. Return in
1189 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1191 INLINE void
1192 window_box (w, area, box_x, box_y, box_width, box_height)
1193 struct window *w;
1194 int area;
1195 int *box_x, *box_y, *box_width, *box_height;
1197 if (box_width)
1198 *box_width = window_box_width (w, area);
1199 if (box_height)
1200 *box_height = window_box_height (w);
1201 if (box_x)
1202 *box_x = window_box_left (w, area);
1203 if (box_y)
1205 *box_y = WINDOW_TOP_EDGE_Y (w);
1206 if (WINDOW_WANTS_HEADER_LINE_P (w))
1207 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1212 /* Get the bounding box of the display area AREA of window W, without
1213 mode lines. AREA < 0 means the whole window, not including the
1214 left and right fringe of the window. Return in *TOP_LEFT_X
1215 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1216 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1217 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1218 box. */
1220 INLINE void
1221 window_box_edges (w, area, top_left_x, top_left_y,
1222 bottom_right_x, bottom_right_y)
1223 struct window *w;
1224 int area;
1225 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1227 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1228 bottom_right_y);
1229 *bottom_right_x += *top_left_x;
1230 *bottom_right_y += *top_left_y;
1235 /***********************************************************************
1236 Utilities
1237 ***********************************************************************/
1239 /* Return the bottom y-position of the line the iterator IT is in.
1240 This can modify IT's settings. */
1243 line_bottom_y (it)
1244 struct it *it;
1246 int line_height = it->max_ascent + it->max_descent;
1247 int line_top_y = it->current_y;
1249 if (line_height == 0)
1251 if (last_height)
1252 line_height = last_height;
1253 else if (IT_CHARPOS (*it) < ZV)
1255 move_it_by_lines (it, 1, 1);
1256 line_height = (it->max_ascent || it->max_descent
1257 ? it->max_ascent + it->max_descent
1258 : last_height);
1260 else
1262 struct glyph_row *row = it->glyph_row;
1264 /* Use the default character height. */
1265 it->glyph_row = NULL;
1266 it->what = IT_CHARACTER;
1267 it->c = ' ';
1268 it->len = 1;
1269 PRODUCE_GLYPHS (it);
1270 line_height = it->ascent + it->descent;
1271 it->glyph_row = row;
1275 return line_top_y + line_height;
1279 /* Return 1 if position CHARPOS is visible in window W.
1280 CHARPOS < 0 means return info about WINDOW_END position.
1281 If visible, set *X and *Y to pixel coordinates of top left corner.
1282 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1283 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1286 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1287 struct window *w;
1288 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1290 struct it it;
1291 struct text_pos top;
1292 int visible_p = 0;
1293 struct buffer *old_buffer = NULL;
1295 if (noninteractive)
1296 return visible_p;
1298 if (XBUFFER (w->buffer) != current_buffer)
1300 old_buffer = current_buffer;
1301 set_buffer_internal_1 (XBUFFER (w->buffer));
1304 SET_TEXT_POS_FROM_MARKER (top, w->start);
1306 /* Compute exact mode line heights. */
1307 if (WINDOW_WANTS_MODELINE_P (w))
1308 current_mode_line_height
1309 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1310 current_buffer->mode_line_format);
1312 if (WINDOW_WANTS_HEADER_LINE_P (w))
1313 current_header_line_height
1314 = display_mode_line (w, HEADER_LINE_FACE_ID,
1315 current_buffer->header_line_format);
1317 start_display (&it, w, top);
1318 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1319 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1321 /* Note that we may overshoot because of invisible text. */
1322 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1324 int top_x = it.current_x;
1325 int top_y = it.current_y;
1326 int bottom_y = (last_height = 0, line_bottom_y (&it));
1327 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1329 if (top_y < window_top_y)
1330 visible_p = bottom_y > window_top_y;
1331 else if (top_y < it.last_visible_y)
1332 visible_p = 1;
1333 if (visible_p)
1335 *x = top_x;
1336 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1337 *rtop = max (0, window_top_y - top_y);
1338 *rbot = max (0, bottom_y - it.last_visible_y);
1339 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1340 - max (top_y, window_top_y)));
1341 *vpos = it.vpos;
1344 else
1346 struct it it2;
1348 it2 = it;
1349 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1350 move_it_by_lines (&it, 1, 0);
1351 if (charpos < IT_CHARPOS (it)
1352 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1354 visible_p = 1;
1355 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1356 *x = it2.current_x;
1357 *y = it2.current_y + it2.max_ascent - it2.ascent;
1358 *rtop = max (0, -it2.current_y);
1359 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1360 - it.last_visible_y));
1361 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1362 it.last_visible_y)
1363 - max (it2.current_y,
1364 WINDOW_HEADER_LINE_HEIGHT (w))));
1365 *vpos = it2.vpos;
1369 if (old_buffer)
1370 set_buffer_internal_1 (old_buffer);
1372 current_header_line_height = current_mode_line_height = -1;
1374 if (visible_p && XFASTINT (w->hscroll) > 0)
1375 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1377 #if 0
1378 /* Debugging code. */
1379 if (visible_p)
1380 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1381 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1382 else
1383 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1384 #endif
1386 return visible_p;
1390 /* Return the next character from STR which is MAXLEN bytes long.
1391 Return in *LEN the length of the character. This is like
1392 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1393 we find one, we return a `?', but with the length of the invalid
1394 character. */
1396 static INLINE int
1397 string_char_and_length (str, maxlen, len)
1398 const unsigned char *str;
1399 int maxlen, *len;
1401 int c;
1403 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1404 if (!CHAR_VALID_P (c, 1))
1405 /* We may not change the length here because other places in Emacs
1406 don't use this function, i.e. they silently accept invalid
1407 characters. */
1408 c = '?';
1410 return c;
1415 /* Given a position POS containing a valid character and byte position
1416 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1418 static struct text_pos
1419 string_pos_nchars_ahead (pos, string, nchars)
1420 struct text_pos pos;
1421 Lisp_Object string;
1422 int nchars;
1424 xassert (STRINGP (string) && nchars >= 0);
1426 if (STRING_MULTIBYTE (string))
1428 int rest = SBYTES (string) - BYTEPOS (pos);
1429 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1430 int len;
1432 while (nchars--)
1434 string_char_and_length (p, rest, &len);
1435 p += len, rest -= len;
1436 xassert (rest >= 0);
1437 CHARPOS (pos) += 1;
1438 BYTEPOS (pos) += len;
1441 else
1442 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1444 return pos;
1448 /* Value is the text position, i.e. character and byte position,
1449 for character position CHARPOS in STRING. */
1451 static INLINE struct text_pos
1452 string_pos (charpos, string)
1453 int charpos;
1454 Lisp_Object string;
1456 struct text_pos pos;
1457 xassert (STRINGP (string));
1458 xassert (charpos >= 0);
1459 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1460 return pos;
1464 /* Value is a text position, i.e. character and byte position, for
1465 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1466 means recognize multibyte characters. */
1468 static struct text_pos
1469 c_string_pos (charpos, s, multibyte_p)
1470 int charpos;
1471 unsigned char *s;
1472 int multibyte_p;
1474 struct text_pos pos;
1476 xassert (s != NULL);
1477 xassert (charpos >= 0);
1479 if (multibyte_p)
1481 int rest = strlen (s), len;
1483 SET_TEXT_POS (pos, 0, 0);
1484 while (charpos--)
1486 string_char_and_length (s, rest, &len);
1487 s += len, rest -= len;
1488 xassert (rest >= 0);
1489 CHARPOS (pos) += 1;
1490 BYTEPOS (pos) += len;
1493 else
1494 SET_TEXT_POS (pos, charpos, charpos);
1496 return pos;
1500 /* Value is the number of characters in C string S. MULTIBYTE_P
1501 non-zero means recognize multibyte characters. */
1503 static int
1504 number_of_chars (s, multibyte_p)
1505 unsigned char *s;
1506 int multibyte_p;
1508 int nchars;
1510 if (multibyte_p)
1512 int rest = strlen (s), len;
1513 unsigned char *p = (unsigned char *) s;
1515 for (nchars = 0; rest > 0; ++nchars)
1517 string_char_and_length (p, rest, &len);
1518 rest -= len, p += len;
1521 else
1522 nchars = strlen (s);
1524 return nchars;
1528 /* Compute byte position NEWPOS->bytepos corresponding to
1529 NEWPOS->charpos. POS is a known position in string STRING.
1530 NEWPOS->charpos must be >= POS.charpos. */
1532 static void
1533 compute_string_pos (newpos, pos, string)
1534 struct text_pos *newpos, pos;
1535 Lisp_Object string;
1537 xassert (STRINGP (string));
1538 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1540 if (STRING_MULTIBYTE (string))
1541 *newpos = string_pos_nchars_ahead (pos, string,
1542 CHARPOS (*newpos) - CHARPOS (pos));
1543 else
1544 BYTEPOS (*newpos) = CHARPOS (*newpos);
1547 /* EXPORT:
1548 Return an estimation of the pixel height of mode or top lines on
1549 frame F. FACE_ID specifies what line's height to estimate. */
1552 estimate_mode_line_height (f, face_id)
1553 struct frame *f;
1554 enum face_id face_id;
1556 #ifdef HAVE_WINDOW_SYSTEM
1557 if (FRAME_WINDOW_P (f))
1559 int height = FONT_HEIGHT (FRAME_FONT (f));
1561 /* This function is called so early when Emacs starts that the face
1562 cache and mode line face are not yet initialized. */
1563 if (FRAME_FACE_CACHE (f))
1565 struct face *face = FACE_FROM_ID (f, face_id);
1566 if (face)
1568 if (face->font)
1569 height = FONT_HEIGHT (face->font);
1570 if (face->box_line_width > 0)
1571 height += 2 * face->box_line_width;
1575 return height;
1577 #endif
1579 return 1;
1582 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1583 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1584 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1585 not force the value into range. */
1587 void
1588 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1589 FRAME_PTR f;
1590 register int pix_x, pix_y;
1591 int *x, *y;
1592 NativeRectangle *bounds;
1593 int noclip;
1596 #ifdef HAVE_WINDOW_SYSTEM
1597 if (FRAME_WINDOW_P (f))
1599 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1600 even for negative values. */
1601 if (pix_x < 0)
1602 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1603 if (pix_y < 0)
1604 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1606 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1607 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1609 if (bounds)
1610 STORE_NATIVE_RECT (*bounds,
1611 FRAME_COL_TO_PIXEL_X (f, pix_x),
1612 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1613 FRAME_COLUMN_WIDTH (f) - 1,
1614 FRAME_LINE_HEIGHT (f) - 1);
1616 if (!noclip)
1618 if (pix_x < 0)
1619 pix_x = 0;
1620 else if (pix_x > FRAME_TOTAL_COLS (f))
1621 pix_x = FRAME_TOTAL_COLS (f);
1623 if (pix_y < 0)
1624 pix_y = 0;
1625 else if (pix_y > FRAME_LINES (f))
1626 pix_y = FRAME_LINES (f);
1629 #endif
1631 *x = pix_x;
1632 *y = pix_y;
1636 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1637 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1638 can't tell the positions because W's display is not up to date,
1639 return 0. */
1642 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1643 struct window *w;
1644 int hpos, vpos;
1645 int *frame_x, *frame_y;
1647 #ifdef HAVE_WINDOW_SYSTEM
1648 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1650 int success_p;
1652 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1653 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1655 if (display_completed)
1657 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1658 struct glyph *glyph = row->glyphs[TEXT_AREA];
1659 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1661 hpos = row->x;
1662 vpos = row->y;
1663 while (glyph < end)
1665 hpos += glyph->pixel_width;
1666 ++glyph;
1669 /* If first glyph is partially visible, its first visible position is still 0. */
1670 if (hpos < 0)
1671 hpos = 0;
1673 success_p = 1;
1675 else
1677 hpos = vpos = 0;
1678 success_p = 0;
1681 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1682 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1683 return success_p;
1685 #endif
1687 *frame_x = hpos;
1688 *frame_y = vpos;
1689 return 1;
1693 #ifdef HAVE_WINDOW_SYSTEM
1695 /* Find the glyph under window-relative coordinates X/Y in window W.
1696 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1697 strings. Return in *HPOS and *VPOS the row and column number of
1698 the glyph found. Return in *AREA the glyph area containing X.
1699 Value is a pointer to the glyph found or null if X/Y is not on
1700 text, or we can't tell because W's current matrix is not up to
1701 date. */
1703 static struct glyph *
1704 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1705 struct window *w;
1706 int x, y;
1707 int *hpos, *vpos, *dx, *dy, *area;
1709 struct glyph *glyph, *end;
1710 struct glyph_row *row = NULL;
1711 int x0, i;
1713 /* Find row containing Y. Give up if some row is not enabled. */
1714 for (i = 0; i < w->current_matrix->nrows; ++i)
1716 row = MATRIX_ROW (w->current_matrix, i);
1717 if (!row->enabled_p)
1718 return NULL;
1719 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1720 break;
1723 *vpos = i;
1724 *hpos = 0;
1726 /* Give up if Y is not in the window. */
1727 if (i == w->current_matrix->nrows)
1728 return NULL;
1730 /* Get the glyph area containing X. */
1731 if (w->pseudo_window_p)
1733 *area = TEXT_AREA;
1734 x0 = 0;
1736 else
1738 if (x < window_box_left_offset (w, TEXT_AREA))
1740 *area = LEFT_MARGIN_AREA;
1741 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1743 else if (x < window_box_right_offset (w, TEXT_AREA))
1745 *area = TEXT_AREA;
1746 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1748 else
1750 *area = RIGHT_MARGIN_AREA;
1751 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1755 /* Find glyph containing X. */
1756 glyph = row->glyphs[*area];
1757 end = glyph + row->used[*area];
1758 x -= x0;
1759 while (glyph < end && x >= glyph->pixel_width)
1761 x -= glyph->pixel_width;
1762 ++glyph;
1765 if (glyph == end)
1766 return NULL;
1768 if (dx)
1770 *dx = x;
1771 *dy = y - (row->y + row->ascent - glyph->ascent);
1774 *hpos = glyph - row->glyphs[*area];
1775 return glyph;
1779 /* EXPORT:
1780 Convert frame-relative x/y to coordinates relative to window W.
1781 Takes pseudo-windows into account. */
1783 void
1784 frame_to_window_pixel_xy (w, x, y)
1785 struct window *w;
1786 int *x, *y;
1788 if (w->pseudo_window_p)
1790 /* A pseudo-window is always full-width, and starts at the
1791 left edge of the frame, plus a frame border. */
1792 struct frame *f = XFRAME (w->frame);
1793 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1794 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1796 else
1798 *x -= WINDOW_LEFT_EDGE_X (w);
1799 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1803 /* EXPORT:
1804 Return in RECTS[] at most N clipping rectangles for glyph string S.
1805 Return the number of stored rectangles. */
1808 get_glyph_string_clip_rects (s, rects, n)
1809 struct glyph_string *s;
1810 NativeRectangle *rects;
1811 int n;
1813 XRectangle r;
1815 if (n <= 0)
1816 return 0;
1818 if (s->row->full_width_p)
1820 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1821 r.x = WINDOW_LEFT_EDGE_X (s->w);
1822 r.width = WINDOW_TOTAL_WIDTH (s->w);
1824 /* Unless displaying a mode or menu bar line, which are always
1825 fully visible, clip to the visible part of the row. */
1826 if (s->w->pseudo_window_p)
1827 r.height = s->row->visible_height;
1828 else
1829 r.height = s->height;
1831 else
1833 /* This is a text line that may be partially visible. */
1834 r.x = window_box_left (s->w, s->area);
1835 r.width = window_box_width (s->w, s->area);
1836 r.height = s->row->visible_height;
1839 if (s->clip_head)
1840 if (r.x < s->clip_head->x)
1842 if (r.width >= s->clip_head->x - r.x)
1843 r.width -= s->clip_head->x - r.x;
1844 else
1845 r.width = 0;
1846 r.x = s->clip_head->x;
1848 if (s->clip_tail)
1849 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1851 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1852 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1853 else
1854 r.width = 0;
1857 /* If S draws overlapping rows, it's sufficient to use the top and
1858 bottom of the window for clipping because this glyph string
1859 intentionally draws over other lines. */
1860 if (s->for_overlaps)
1862 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1863 r.height = window_text_bottom_y (s->w) - r.y;
1865 /* Alas, the above simple strategy does not work for the
1866 environments with anti-aliased text: if the same text is
1867 drawn onto the same place multiple times, it gets thicker.
1868 If the overlap we are processing is for the erased cursor, we
1869 take the intersection with the rectagle of the cursor. */
1870 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1872 XRectangle rc, r_save = r;
1874 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1875 rc.y = s->w->phys_cursor.y;
1876 rc.width = s->w->phys_cursor_width;
1877 rc.height = s->w->phys_cursor_height;
1879 x_intersect_rectangles (&r_save, &rc, &r);
1882 else
1884 /* Don't use S->y for clipping because it doesn't take partially
1885 visible lines into account. For example, it can be negative for
1886 partially visible lines at the top of a window. */
1887 if (!s->row->full_width_p
1888 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1889 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1890 else
1891 r.y = max (0, s->row->y);
1893 /* If drawing a tool-bar window, draw it over the internal border
1894 at the top of the window. */
1895 if (WINDOWP (s->f->tool_bar_window)
1896 && s->w == XWINDOW (s->f->tool_bar_window))
1897 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1900 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1902 /* If drawing the cursor, don't let glyph draw outside its
1903 advertised boundaries. Cleartype does this under some circumstances. */
1904 if (s->hl == DRAW_CURSOR)
1906 struct glyph *glyph = s->first_glyph;
1907 int height, max_y;
1909 if (s->x > r.x)
1911 r.width -= s->x - r.x;
1912 r.x = s->x;
1914 r.width = min (r.width, glyph->pixel_width);
1916 /* If r.y is below window bottom, ensure that we still see a cursor. */
1917 height = min (glyph->ascent + glyph->descent,
1918 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1919 max_y = window_text_bottom_y (s->w) - height;
1920 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1921 if (s->ybase - glyph->ascent > max_y)
1923 r.y = max_y;
1924 r.height = height;
1926 else
1928 /* Don't draw cursor glyph taller than our actual glyph. */
1929 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1930 if (height < r.height)
1932 max_y = r.y + r.height;
1933 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1934 r.height = min (max_y - r.y, height);
1939 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1940 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1942 #ifdef CONVERT_FROM_XRECT
1943 CONVERT_FROM_XRECT (r, *rects);
1944 #else
1945 *rects = r;
1946 #endif
1947 return 1;
1949 else
1951 /* If we are processing overlapping and allowed to return
1952 multiple clipping rectangles, we exclude the row of the glyph
1953 string from the clipping rectangle. This is to avoid drawing
1954 the same text on the environment with anti-aliasing. */
1955 #ifdef CONVERT_FROM_XRECT
1956 XRectangle rs[2];
1957 #else
1958 XRectangle *rs = rects;
1959 #endif
1960 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1962 if (s->for_overlaps & OVERLAPS_PRED)
1964 rs[i] = r;
1965 if (r.y + r.height > row_y)
1967 if (r.y < row_y)
1968 rs[i].height = row_y - r.y;
1969 else
1970 rs[i].height = 0;
1972 i++;
1974 if (s->for_overlaps & OVERLAPS_SUCC)
1976 rs[i] = r;
1977 if (r.y < row_y + s->row->visible_height)
1979 if (r.y + r.height > row_y + s->row->visible_height)
1981 rs[i].y = row_y + s->row->visible_height;
1982 rs[i].height = r.y + r.height - rs[i].y;
1984 else
1985 rs[i].height = 0;
1987 i++;
1990 n = i;
1991 #ifdef CONVERT_FROM_XRECT
1992 for (i = 0; i < n; i++)
1993 CONVERT_FROM_XRECT (rs[i], rects[i]);
1994 #endif
1995 return n;
1999 /* EXPORT:
2000 Return in *NR the clipping rectangle for glyph string S. */
2002 void
2003 get_glyph_string_clip_rect (s, nr)
2004 struct glyph_string *s;
2005 NativeRectangle *nr;
2007 get_glyph_string_clip_rects (s, nr, 1);
2011 /* EXPORT:
2012 Return the position and height of the phys cursor in window W.
2013 Set w->phys_cursor_width to width of phys cursor.
2016 void
2017 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2018 struct window *w;
2019 struct glyph_row *row;
2020 struct glyph *glyph;
2021 int *xp, *yp, *heightp;
2023 struct frame *f = XFRAME (WINDOW_FRAME (w));
2024 int x, y, wd, h, h0, y0;
2026 /* Compute the width of the rectangle to draw. If on a stretch
2027 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2028 rectangle as wide as the glyph, but use a canonical character
2029 width instead. */
2030 wd = glyph->pixel_width - 1;
2031 #ifdef HAVE_NTGUI
2032 wd++; /* Why? */
2033 #endif
2035 x = w->phys_cursor.x;
2036 if (x < 0)
2038 wd += x;
2039 x = 0;
2042 if (glyph->type == STRETCH_GLYPH
2043 && !x_stretch_cursor_p)
2044 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2045 w->phys_cursor_width = wd;
2047 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2049 /* If y is below window bottom, ensure that we still see a cursor. */
2050 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2052 h = max (h0, glyph->ascent + glyph->descent);
2053 h0 = min (h0, glyph->ascent + glyph->descent);
2055 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2056 if (y < y0)
2058 h = max (h - (y0 - y) + 1, h0);
2059 y = y0 - 1;
2061 else
2063 y0 = window_text_bottom_y (w) - h0;
2064 if (y > y0)
2066 h += y - y0;
2067 y = y0;
2071 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2072 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2073 *heightp = h;
2077 * Remember which glyph the mouse is over.
2080 void
2081 remember_mouse_glyph (f, gx, gy, rect)
2082 struct frame *f;
2083 int gx, gy;
2084 NativeRectangle *rect;
2086 Lisp_Object window;
2087 struct window *w;
2088 struct glyph_row *r, *gr, *end_row;
2089 enum window_part part;
2090 enum glyph_row_area area;
2091 int x, y, width, height;
2093 /* Try to determine frame pixel position and size of the glyph under
2094 frame pixel coordinates X/Y on frame F. */
2096 if (!f->glyphs_initialized_p
2097 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2098 NILP (window)))
2100 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2101 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2102 goto virtual_glyph;
2105 w = XWINDOW (window);
2106 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2107 height = WINDOW_FRAME_LINE_HEIGHT (w);
2109 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2110 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2112 if (w->pseudo_window_p)
2114 area = TEXT_AREA;
2115 part = ON_MODE_LINE; /* Don't adjust margin. */
2116 goto text_glyph;
2119 switch (part)
2121 case ON_LEFT_MARGIN:
2122 area = LEFT_MARGIN_AREA;
2123 goto text_glyph;
2125 case ON_RIGHT_MARGIN:
2126 area = RIGHT_MARGIN_AREA;
2127 goto text_glyph;
2129 case ON_HEADER_LINE:
2130 case ON_MODE_LINE:
2131 gr = (part == ON_HEADER_LINE
2132 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2133 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2134 gy = gr->y;
2135 area = TEXT_AREA;
2136 goto text_glyph_row_found;
2138 case ON_TEXT:
2139 area = TEXT_AREA;
2141 text_glyph:
2142 gr = 0; gy = 0;
2143 for (; r <= end_row && r->enabled_p; ++r)
2144 if (r->y + r->height > y)
2146 gr = r; gy = r->y;
2147 break;
2150 text_glyph_row_found:
2151 if (gr && gy <= y)
2153 struct glyph *g = gr->glyphs[area];
2154 struct glyph *end = g + gr->used[area];
2156 height = gr->height;
2157 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2158 if (gx + g->pixel_width > x)
2159 break;
2161 if (g < end)
2163 if (g->type == IMAGE_GLYPH)
2165 /* Don't remember when mouse is over image, as
2166 image may have hot-spots. */
2167 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2168 return;
2170 width = g->pixel_width;
2172 else
2174 /* Use nominal char spacing at end of line. */
2175 x -= gx;
2176 gx += (x / width) * width;
2179 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2180 gx += window_box_left_offset (w, area);
2182 else
2184 /* Use nominal line height at end of window. */
2185 gx = (x / width) * width;
2186 y -= gy;
2187 gy += (y / height) * height;
2189 break;
2191 case ON_LEFT_FRINGE:
2192 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2193 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2194 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2195 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2196 goto row_glyph;
2198 case ON_RIGHT_FRINGE:
2199 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2200 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2201 : window_box_right_offset (w, TEXT_AREA));
2202 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2203 goto row_glyph;
2205 case ON_SCROLL_BAR:
2206 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2208 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2209 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2210 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2211 : 0)));
2212 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2214 row_glyph:
2215 gr = 0, gy = 0;
2216 for (; r <= end_row && r->enabled_p; ++r)
2217 if (r->y + r->height > y)
2219 gr = r; gy = r->y;
2220 break;
2223 if (gr && gy <= y)
2224 height = gr->height;
2225 else
2227 /* Use nominal line height at end of window. */
2228 y -= gy;
2229 gy += (y / height) * height;
2231 break;
2233 default:
2235 virtual_glyph:
2236 /* If there is no glyph under the mouse, then we divide the screen
2237 into a grid of the smallest glyph in the frame, and use that
2238 as our "glyph". */
2240 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2241 round down even for negative values. */
2242 if (gx < 0)
2243 gx -= width - 1;
2244 if (gy < 0)
2245 gy -= height - 1;
2247 gx = (gx / width) * width;
2248 gy = (gy / height) * height;
2250 goto store_rect;
2253 gx += WINDOW_LEFT_EDGE_X (w);
2254 gy += WINDOW_TOP_EDGE_Y (w);
2256 store_rect:
2257 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2259 /* Visible feedback for debugging. */
2260 #if 0
2261 #if HAVE_X_WINDOWS
2262 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2263 f->output_data.x->normal_gc,
2264 gx, gy, width, height);
2265 #endif
2266 #endif
2270 #endif /* HAVE_WINDOW_SYSTEM */
2273 /***********************************************************************
2274 Lisp form evaluation
2275 ***********************************************************************/
2277 /* Error handler for safe_eval and safe_call. */
2279 static Lisp_Object
2280 safe_eval_handler (arg)
2281 Lisp_Object arg;
2283 add_to_log ("Error during redisplay: %s", arg, Qnil);
2284 return Qnil;
2288 /* Evaluate SEXPR and return the result, or nil if something went
2289 wrong. Prevent redisplay during the evaluation. */
2291 Lisp_Object
2292 safe_eval (sexpr)
2293 Lisp_Object sexpr;
2295 Lisp_Object val;
2297 if (inhibit_eval_during_redisplay)
2298 val = Qnil;
2299 else
2301 int count = SPECPDL_INDEX ();
2302 struct gcpro gcpro1;
2304 GCPRO1 (sexpr);
2305 specbind (Qinhibit_redisplay, Qt);
2306 /* Use Qt to ensure debugger does not run,
2307 so there is no possibility of wanting to redisplay. */
2308 val = internal_condition_case_1 (Feval, sexpr, Qt,
2309 safe_eval_handler);
2310 UNGCPRO;
2311 val = unbind_to (count, val);
2314 return val;
2318 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2319 Return the result, or nil if something went wrong. Prevent
2320 redisplay during the evaluation. */
2322 Lisp_Object
2323 safe_call (nargs, args)
2324 int nargs;
2325 Lisp_Object *args;
2327 Lisp_Object val;
2329 if (inhibit_eval_during_redisplay)
2330 val = Qnil;
2331 else
2333 int count = SPECPDL_INDEX ();
2334 struct gcpro gcpro1;
2336 GCPRO1 (args[0]);
2337 gcpro1.nvars = nargs;
2338 specbind (Qinhibit_redisplay, Qt);
2339 /* Use Qt to ensure debugger does not run,
2340 so there is no possibility of wanting to redisplay. */
2341 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2342 safe_eval_handler);
2343 UNGCPRO;
2344 val = unbind_to (count, val);
2347 return val;
2351 /* Call function FN with one argument ARG.
2352 Return the result, or nil if something went wrong. */
2354 Lisp_Object
2355 safe_call1 (fn, arg)
2356 Lisp_Object fn, arg;
2358 Lisp_Object args[2];
2359 args[0] = fn;
2360 args[1] = arg;
2361 return safe_call (2, args);
2366 /***********************************************************************
2367 Debugging
2368 ***********************************************************************/
2370 #if 0
2372 /* Define CHECK_IT to perform sanity checks on iterators.
2373 This is for debugging. It is too slow to do unconditionally. */
2375 static void
2376 check_it (it)
2377 struct it *it;
2379 if (it->method == GET_FROM_STRING)
2381 xassert (STRINGP (it->string));
2382 xassert (IT_STRING_CHARPOS (*it) >= 0);
2384 else
2386 xassert (IT_STRING_CHARPOS (*it) < 0);
2387 if (it->method == GET_FROM_BUFFER)
2389 /* Check that character and byte positions agree. */
2390 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2394 if (it->dpvec)
2395 xassert (it->current.dpvec_index >= 0);
2396 else
2397 xassert (it->current.dpvec_index < 0);
2400 #define CHECK_IT(IT) check_it ((IT))
2402 #else /* not 0 */
2404 #define CHECK_IT(IT) (void) 0
2406 #endif /* not 0 */
2409 #if GLYPH_DEBUG
2411 /* Check that the window end of window W is what we expect it
2412 to be---the last row in the current matrix displaying text. */
2414 static void
2415 check_window_end (w)
2416 struct window *w;
2418 if (!MINI_WINDOW_P (w)
2419 && !NILP (w->window_end_valid))
2421 struct glyph_row *row;
2422 xassert ((row = MATRIX_ROW (w->current_matrix,
2423 XFASTINT (w->window_end_vpos)),
2424 !row->enabled_p
2425 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2426 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2430 #define CHECK_WINDOW_END(W) check_window_end ((W))
2432 #else /* not GLYPH_DEBUG */
2434 #define CHECK_WINDOW_END(W) (void) 0
2436 #endif /* not GLYPH_DEBUG */
2440 /***********************************************************************
2441 Iterator initialization
2442 ***********************************************************************/
2444 /* Initialize IT for displaying current_buffer in window W, starting
2445 at character position CHARPOS. CHARPOS < 0 means that no buffer
2446 position is specified which is useful when the iterator is assigned
2447 a position later. BYTEPOS is the byte position corresponding to
2448 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2450 If ROW is not null, calls to produce_glyphs with IT as parameter
2451 will produce glyphs in that row.
2453 BASE_FACE_ID is the id of a base face to use. It must be one of
2454 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2455 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2456 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2458 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2459 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2460 will be initialized to use the corresponding mode line glyph row of
2461 the desired matrix of W. */
2463 void
2464 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2465 struct it *it;
2466 struct window *w;
2467 int charpos, bytepos;
2468 struct glyph_row *row;
2469 enum face_id base_face_id;
2471 int highlight_region_p;
2473 /* Some precondition checks. */
2474 xassert (w != NULL && it != NULL);
2475 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2476 && charpos <= ZV));
2478 /* If face attributes have been changed since the last redisplay,
2479 free realized faces now because they depend on face definitions
2480 that might have changed. Don't free faces while there might be
2481 desired matrices pending which reference these faces. */
2482 if (face_change_count && !inhibit_free_realized_faces)
2484 face_change_count = 0;
2485 free_all_realized_faces (Qnil);
2488 /* Use one of the mode line rows of W's desired matrix if
2489 appropriate. */
2490 if (row == NULL)
2492 if (base_face_id == MODE_LINE_FACE_ID
2493 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2494 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2495 else if (base_face_id == HEADER_LINE_FACE_ID)
2496 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2499 /* Clear IT. */
2500 bzero (it, sizeof *it);
2501 it->current.overlay_string_index = -1;
2502 it->current.dpvec_index = -1;
2503 it->base_face_id = base_face_id;
2504 it->string = Qnil;
2505 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2507 /* The window in which we iterate over current_buffer: */
2508 XSETWINDOW (it->window, w);
2509 it->w = w;
2510 it->f = XFRAME (w->frame);
2512 /* Extra space between lines (on window systems only). */
2513 if (base_face_id == DEFAULT_FACE_ID
2514 && FRAME_WINDOW_P (it->f))
2516 if (NATNUMP (current_buffer->extra_line_spacing))
2517 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2518 else if (FLOATP (current_buffer->extra_line_spacing))
2519 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2520 * FRAME_LINE_HEIGHT (it->f));
2521 else if (it->f->extra_line_spacing > 0)
2522 it->extra_line_spacing = it->f->extra_line_spacing;
2523 it->max_extra_line_spacing = 0;
2526 /* If realized faces have been removed, e.g. because of face
2527 attribute changes of named faces, recompute them. When running
2528 in batch mode, the face cache of Vterminal_frame is null. If
2529 we happen to get called, make a dummy face cache. */
2530 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2531 init_frame_faces (it->f);
2532 if (FRAME_FACE_CACHE (it->f)->used == 0)
2533 recompute_basic_faces (it->f);
2535 /* Current value of the `slice', `space-width', and 'height' properties. */
2536 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2537 it->space_width = Qnil;
2538 it->font_height = Qnil;
2539 it->override_ascent = -1;
2541 /* Are control characters displayed as `^C'? */
2542 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2544 /* -1 means everything between a CR and the following line end
2545 is invisible. >0 means lines indented more than this value are
2546 invisible. */
2547 it->selective = (INTEGERP (current_buffer->selective_display)
2548 ? XFASTINT (current_buffer->selective_display)
2549 : (!NILP (current_buffer->selective_display)
2550 ? -1 : 0));
2551 it->selective_display_ellipsis_p
2552 = !NILP (current_buffer->selective_display_ellipses);
2554 /* Display table to use. */
2555 it->dp = window_display_table (w);
2557 /* Are multibyte characters enabled in current_buffer? */
2558 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2560 /* Non-zero if we should highlight the region. */
2561 highlight_region_p
2562 = (!NILP (Vtransient_mark_mode)
2563 && !NILP (current_buffer->mark_active)
2564 && XMARKER (current_buffer->mark)->buffer != 0);
2566 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2567 start and end of a visible region in window IT->w. Set both to
2568 -1 to indicate no region. */
2569 if (highlight_region_p
2570 /* Maybe highlight only in selected window. */
2571 && (/* Either show region everywhere. */
2572 highlight_nonselected_windows
2573 /* Or show region in the selected window. */
2574 || w == XWINDOW (selected_window)
2575 /* Or show the region if we are in the mini-buffer and W is
2576 the window the mini-buffer refers to. */
2577 || (MINI_WINDOW_P (XWINDOW (selected_window))
2578 && WINDOWP (minibuf_selected_window)
2579 && w == XWINDOW (minibuf_selected_window))))
2581 int charpos = marker_position (current_buffer->mark);
2582 it->region_beg_charpos = min (PT, charpos);
2583 it->region_end_charpos = max (PT, charpos);
2585 else
2586 it->region_beg_charpos = it->region_end_charpos = -1;
2588 /* Get the position at which the redisplay_end_trigger hook should
2589 be run, if it is to be run at all. */
2590 if (MARKERP (w->redisplay_end_trigger)
2591 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2592 it->redisplay_end_trigger_charpos
2593 = marker_position (w->redisplay_end_trigger);
2594 else if (INTEGERP (w->redisplay_end_trigger))
2595 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2597 /* Correct bogus values of tab_width. */
2598 it->tab_width = XINT (current_buffer->tab_width);
2599 if (it->tab_width <= 0 || it->tab_width > 1000)
2600 it->tab_width = 8;
2602 /* Are lines in the display truncated? */
2603 it->truncate_lines_p
2604 = (base_face_id != DEFAULT_FACE_ID
2605 || XINT (it->w->hscroll)
2606 || (truncate_partial_width_windows
2607 && !WINDOW_FULL_WIDTH_P (it->w))
2608 || !NILP (current_buffer->truncate_lines));
2610 /* Get dimensions of truncation and continuation glyphs. These are
2611 displayed as fringe bitmaps under X, so we don't need them for such
2612 frames. */
2613 if (!FRAME_WINDOW_P (it->f))
2615 if (it->truncate_lines_p)
2617 /* We will need the truncation glyph. */
2618 xassert (it->glyph_row == NULL);
2619 produce_special_glyphs (it, IT_TRUNCATION);
2620 it->truncation_pixel_width = it->pixel_width;
2622 else
2624 /* We will need the continuation glyph. */
2625 xassert (it->glyph_row == NULL);
2626 produce_special_glyphs (it, IT_CONTINUATION);
2627 it->continuation_pixel_width = it->pixel_width;
2630 /* Reset these values to zero because the produce_special_glyphs
2631 above has changed them. */
2632 it->pixel_width = it->ascent = it->descent = 0;
2633 it->phys_ascent = it->phys_descent = 0;
2636 /* Set this after getting the dimensions of truncation and
2637 continuation glyphs, so that we don't produce glyphs when calling
2638 produce_special_glyphs, above. */
2639 it->glyph_row = row;
2640 it->area = TEXT_AREA;
2642 /* Get the dimensions of the display area. The display area
2643 consists of the visible window area plus a horizontally scrolled
2644 part to the left of the window. All x-values are relative to the
2645 start of this total display area. */
2646 if (base_face_id != DEFAULT_FACE_ID)
2648 /* Mode lines, menu bar in terminal frames. */
2649 it->first_visible_x = 0;
2650 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2652 else
2654 it->first_visible_x
2655 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2656 it->last_visible_x = (it->first_visible_x
2657 + window_box_width (w, TEXT_AREA));
2659 /* If we truncate lines, leave room for the truncator glyph(s) at
2660 the right margin. Otherwise, leave room for the continuation
2661 glyph(s). Truncation and continuation glyphs are not inserted
2662 for window-based redisplay. */
2663 if (!FRAME_WINDOW_P (it->f))
2665 if (it->truncate_lines_p)
2666 it->last_visible_x -= it->truncation_pixel_width;
2667 else
2668 it->last_visible_x -= it->continuation_pixel_width;
2671 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2672 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2675 /* Leave room for a border glyph. */
2676 if (!FRAME_WINDOW_P (it->f)
2677 && !WINDOW_RIGHTMOST_P (it->w))
2678 it->last_visible_x -= 1;
2680 it->last_visible_y = window_text_bottom_y (w);
2682 /* For mode lines and alike, arrange for the first glyph having a
2683 left box line if the face specifies a box. */
2684 if (base_face_id != DEFAULT_FACE_ID)
2686 struct face *face;
2688 it->face_id = base_face_id;
2690 /* If we have a boxed mode line, make the first character appear
2691 with a left box line. */
2692 face = FACE_FROM_ID (it->f, base_face_id);
2693 if (face->box != FACE_NO_BOX)
2694 it->start_of_box_run_p = 1;
2697 /* If a buffer position was specified, set the iterator there,
2698 getting overlays and face properties from that position. */
2699 if (charpos >= BUF_BEG (current_buffer))
2701 it->end_charpos = ZV;
2702 it->face_id = -1;
2703 IT_CHARPOS (*it) = charpos;
2705 /* Compute byte position if not specified. */
2706 if (bytepos < charpos)
2707 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2708 else
2709 IT_BYTEPOS (*it) = bytepos;
2711 it->start = it->current;
2713 /* Compute faces etc. */
2714 reseat (it, it->current.pos, 1);
2717 CHECK_IT (it);
2721 /* Initialize IT for the display of window W with window start POS. */
2723 void
2724 start_display (it, w, pos)
2725 struct it *it;
2726 struct window *w;
2727 struct text_pos pos;
2729 struct glyph_row *row;
2730 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2732 row = w->desired_matrix->rows + first_vpos;
2733 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2734 it->first_vpos = first_vpos;
2736 /* Don't reseat to previous visible line start if current start
2737 position is in a string or image. */
2738 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2740 int start_at_line_beg_p;
2741 int first_y = it->current_y;
2743 /* If window start is not at a line start, skip forward to POS to
2744 get the correct continuation lines width. */
2745 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2746 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2747 if (!start_at_line_beg_p)
2749 int new_x;
2751 reseat_at_previous_visible_line_start (it);
2752 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2754 new_x = it->current_x + it->pixel_width;
2756 /* If lines are continued, this line may end in the middle
2757 of a multi-glyph character (e.g. a control character
2758 displayed as \003, or in the middle of an overlay
2759 string). In this case move_it_to above will not have
2760 taken us to the start of the continuation line but to the
2761 end of the continued line. */
2762 if (it->current_x > 0
2763 && !it->truncate_lines_p /* Lines are continued. */
2764 && (/* And glyph doesn't fit on the line. */
2765 new_x > it->last_visible_x
2766 /* Or it fits exactly and we're on a window
2767 system frame. */
2768 || (new_x == it->last_visible_x
2769 && FRAME_WINDOW_P (it->f))))
2771 if (it->current.dpvec_index >= 0
2772 || it->current.overlay_string_index >= 0)
2774 set_iterator_to_next (it, 1);
2775 move_it_in_display_line_to (it, -1, -1, 0);
2778 it->continuation_lines_width += it->current_x;
2781 /* We're starting a new display line, not affected by the
2782 height of the continued line, so clear the appropriate
2783 fields in the iterator structure. */
2784 it->max_ascent = it->max_descent = 0;
2785 it->max_phys_ascent = it->max_phys_descent = 0;
2787 it->current_y = first_y;
2788 it->vpos = 0;
2789 it->current_x = it->hpos = 0;
2793 #if 0 /* Don't assert the following because start_display is sometimes
2794 called intentionally with a window start that is not at a
2795 line start. Please leave this code in as a comment. */
2797 /* Window start should be on a line start, now. */
2798 xassert (it->continuation_lines_width
2799 || IT_CHARPOS (it) == BEGV
2800 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2801 #endif /* 0 */
2805 /* Return 1 if POS is a position in ellipses displayed for invisible
2806 text. W is the window we display, for text property lookup. */
2808 static int
2809 in_ellipses_for_invisible_text_p (pos, w)
2810 struct display_pos *pos;
2811 struct window *w;
2813 Lisp_Object prop, window;
2814 int ellipses_p = 0;
2815 int charpos = CHARPOS (pos->pos);
2817 /* If POS specifies a position in a display vector, this might
2818 be for an ellipsis displayed for invisible text. We won't
2819 get the iterator set up for delivering that ellipsis unless
2820 we make sure that it gets aware of the invisible text. */
2821 if (pos->dpvec_index >= 0
2822 && pos->overlay_string_index < 0
2823 && CHARPOS (pos->string_pos) < 0
2824 && charpos > BEGV
2825 && (XSETWINDOW (window, w),
2826 prop = Fget_char_property (make_number (charpos),
2827 Qinvisible, window),
2828 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2830 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2831 window);
2832 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2835 return ellipses_p;
2839 /* Initialize IT for stepping through current_buffer in window W,
2840 starting at position POS that includes overlay string and display
2841 vector/ control character translation position information. Value
2842 is zero if there are overlay strings with newlines at POS. */
2844 static int
2845 init_from_display_pos (it, w, pos)
2846 struct it *it;
2847 struct window *w;
2848 struct display_pos *pos;
2850 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2851 int i, overlay_strings_with_newlines = 0;
2853 /* If POS specifies a position in a display vector, this might
2854 be for an ellipsis displayed for invisible text. We won't
2855 get the iterator set up for delivering that ellipsis unless
2856 we make sure that it gets aware of the invisible text. */
2857 if (in_ellipses_for_invisible_text_p (pos, w))
2859 --charpos;
2860 bytepos = 0;
2863 /* Keep in mind: the call to reseat in init_iterator skips invisible
2864 text, so we might end up at a position different from POS. This
2865 is only a problem when POS is a row start after a newline and an
2866 overlay starts there with an after-string, and the overlay has an
2867 invisible property. Since we don't skip invisible text in
2868 display_line and elsewhere immediately after consuming the
2869 newline before the row start, such a POS will not be in a string,
2870 but the call to init_iterator below will move us to the
2871 after-string. */
2872 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2874 /* This only scans the current chunk -- it should scan all chunks.
2875 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2876 to 16 in 22.1 to make this a lesser problem. */
2877 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2879 const char *s = SDATA (it->overlay_strings[i]);
2880 const char *e = s + SBYTES (it->overlay_strings[i]);
2882 while (s < e && *s != '\n')
2883 ++s;
2885 if (s < e)
2887 overlay_strings_with_newlines = 1;
2888 break;
2892 /* If position is within an overlay string, set up IT to the right
2893 overlay string. */
2894 if (pos->overlay_string_index >= 0)
2896 int relative_index;
2898 /* If the first overlay string happens to have a `display'
2899 property for an image, the iterator will be set up for that
2900 image, and we have to undo that setup first before we can
2901 correct the overlay string index. */
2902 if (it->method == GET_FROM_IMAGE)
2903 pop_it (it);
2905 /* We already have the first chunk of overlay strings in
2906 IT->overlay_strings. Load more until the one for
2907 pos->overlay_string_index is in IT->overlay_strings. */
2908 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2910 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2911 it->current.overlay_string_index = 0;
2912 while (n--)
2914 load_overlay_strings (it, 0);
2915 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2919 it->current.overlay_string_index = pos->overlay_string_index;
2920 relative_index = (it->current.overlay_string_index
2921 % OVERLAY_STRING_CHUNK_SIZE);
2922 it->string = it->overlay_strings[relative_index];
2923 xassert (STRINGP (it->string));
2924 it->current.string_pos = pos->string_pos;
2925 it->method = GET_FROM_STRING;
2928 #if 0 /* This is bogus because POS not having an overlay string
2929 position does not mean it's after the string. Example: A
2930 line starting with a before-string and initialization of IT
2931 to the previous row's end position. */
2932 else if (it->current.overlay_string_index >= 0)
2934 /* If POS says we're already after an overlay string ending at
2935 POS, make sure to pop the iterator because it will be in
2936 front of that overlay string. When POS is ZV, we've thereby
2937 also ``processed'' overlay strings at ZV. */
2938 while (it->sp)
2939 pop_it (it);
2940 xassert (it->current.overlay_string_index == -1);
2941 xassert (it->method == GET_FROM_BUFFER);
2942 if (CHARPOS (pos->pos) == ZV)
2943 it->overlay_strings_at_end_processed_p = 1;
2945 #endif /* 0 */
2947 if (CHARPOS (pos->string_pos) >= 0)
2949 /* Recorded position is not in an overlay string, but in another
2950 string. This can only be a string from a `display' property.
2951 IT should already be filled with that string. */
2952 it->current.string_pos = pos->string_pos;
2953 xassert (STRINGP (it->string));
2956 /* Restore position in display vector translations, control
2957 character translations or ellipses. */
2958 if (pos->dpvec_index >= 0)
2960 if (it->dpvec == NULL)
2961 get_next_display_element (it);
2962 xassert (it->dpvec && it->current.dpvec_index == 0);
2963 it->current.dpvec_index = pos->dpvec_index;
2966 CHECK_IT (it);
2967 return !overlay_strings_with_newlines;
2971 /* Initialize IT for stepping through current_buffer in window W
2972 starting at ROW->start. */
2974 static void
2975 init_to_row_start (it, w, row)
2976 struct it *it;
2977 struct window *w;
2978 struct glyph_row *row;
2980 init_from_display_pos (it, w, &row->start);
2981 it->start = row->start;
2982 it->continuation_lines_width = row->continuation_lines_width;
2983 CHECK_IT (it);
2987 /* Initialize IT for stepping through current_buffer in window W
2988 starting in the line following ROW, i.e. starting at ROW->end.
2989 Value is zero if there are overlay strings with newlines at ROW's
2990 end position. */
2992 static int
2993 init_to_row_end (it, w, row)
2994 struct it *it;
2995 struct window *w;
2996 struct glyph_row *row;
2998 int success = 0;
3000 if (init_from_display_pos (it, w, &row->end))
3002 if (row->continued_p)
3003 it->continuation_lines_width
3004 = row->continuation_lines_width + row->pixel_width;
3005 CHECK_IT (it);
3006 success = 1;
3009 return success;
3015 /***********************************************************************
3016 Text properties
3017 ***********************************************************************/
3019 /* Called when IT reaches IT->stop_charpos. Handle text property and
3020 overlay changes. Set IT->stop_charpos to the next position where
3021 to stop. */
3023 static void
3024 handle_stop (it)
3025 struct it *it;
3027 enum prop_handled handled;
3028 int handle_overlay_change_p;
3029 struct props *p;
3031 it->dpvec = NULL;
3032 it->current.dpvec_index = -1;
3033 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3034 it->ignore_overlay_strings_at_pos_p = 0;
3036 /* Use face of preceding text for ellipsis (if invisible) */
3037 if (it->selective_display_ellipsis_p)
3038 it->saved_face_id = it->face_id;
3042 handled = HANDLED_NORMALLY;
3044 /* Call text property handlers. */
3045 for (p = it_props; p->handler; ++p)
3047 handled = p->handler (it);
3049 if (handled == HANDLED_RECOMPUTE_PROPS)
3050 break;
3051 else if (handled == HANDLED_RETURN)
3053 /* We still want to show before and after strings from
3054 overlays even if the actual buffer text is replaced. */
3055 if (!handle_overlay_change_p || it->sp > 1)
3056 return;
3057 if (!get_overlay_strings_1 (it, 0, 0))
3058 return;
3059 it->ignore_overlay_strings_at_pos_p = 1;
3060 it->string_from_display_prop_p = 0;
3061 handle_overlay_change_p = 0;
3062 handled = HANDLED_RECOMPUTE_PROPS;
3063 break;
3065 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3066 handle_overlay_change_p = 0;
3069 if (handled != HANDLED_RECOMPUTE_PROPS)
3071 /* Don't check for overlay strings below when set to deliver
3072 characters from a display vector. */
3073 if (it->method == GET_FROM_DISPLAY_VECTOR)
3074 handle_overlay_change_p = 0;
3076 /* Handle overlay changes. */
3077 if (handle_overlay_change_p)
3078 handled = handle_overlay_change (it);
3080 /* Determine where to stop next. */
3081 if (handled == HANDLED_NORMALLY)
3082 compute_stop_pos (it);
3085 while (handled == HANDLED_RECOMPUTE_PROPS);
3089 /* Compute IT->stop_charpos from text property and overlay change
3090 information for IT's current position. */
3092 static void
3093 compute_stop_pos (it)
3094 struct it *it;
3096 register INTERVAL iv, next_iv;
3097 Lisp_Object object, limit, position;
3099 /* If nowhere else, stop at the end. */
3100 it->stop_charpos = it->end_charpos;
3102 if (STRINGP (it->string))
3104 /* Strings are usually short, so don't limit the search for
3105 properties. */
3106 object = it->string;
3107 limit = Qnil;
3108 position = make_number (IT_STRING_CHARPOS (*it));
3110 else
3112 int charpos;
3114 /* If next overlay change is in front of the current stop pos
3115 (which is IT->end_charpos), stop there. Note: value of
3116 next_overlay_change is point-max if no overlay change
3117 follows. */
3118 charpos = next_overlay_change (IT_CHARPOS (*it));
3119 if (charpos < it->stop_charpos)
3120 it->stop_charpos = charpos;
3122 /* If showing the region, we have to stop at the region
3123 start or end because the face might change there. */
3124 if (it->region_beg_charpos > 0)
3126 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3127 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3128 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3129 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3132 /* Set up variables for computing the stop position from text
3133 property changes. */
3134 XSETBUFFER (object, current_buffer);
3135 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3136 position = make_number (IT_CHARPOS (*it));
3140 /* Get the interval containing IT's position. Value is a null
3141 interval if there isn't such an interval. */
3142 iv = validate_interval_range (object, &position, &position, 0);
3143 if (!NULL_INTERVAL_P (iv))
3145 Lisp_Object values_here[LAST_PROP_IDX];
3146 struct props *p;
3148 /* Get properties here. */
3149 for (p = it_props; p->handler; ++p)
3150 values_here[p->idx] = textget (iv->plist, *p->name);
3152 /* Look for an interval following iv that has different
3153 properties. */
3154 for (next_iv = next_interval (iv);
3155 (!NULL_INTERVAL_P (next_iv)
3156 && (NILP (limit)
3157 || XFASTINT (limit) > next_iv->position));
3158 next_iv = next_interval (next_iv))
3160 for (p = it_props; p->handler; ++p)
3162 Lisp_Object new_value;
3164 new_value = textget (next_iv->plist, *p->name);
3165 if (!EQ (values_here[p->idx], new_value))
3166 break;
3169 if (p->handler)
3170 break;
3173 if (!NULL_INTERVAL_P (next_iv))
3175 if (INTEGERP (limit)
3176 && next_iv->position >= XFASTINT (limit))
3177 /* No text property change up to limit. */
3178 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3179 else
3180 /* Text properties change in next_iv. */
3181 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3185 xassert (STRINGP (it->string)
3186 || (it->stop_charpos >= BEGV
3187 && it->stop_charpos >= IT_CHARPOS (*it)));
3191 /* Return the position of the next overlay change after POS in
3192 current_buffer. Value is point-max if no overlay change
3193 follows. This is like `next-overlay-change' but doesn't use
3194 xmalloc. */
3196 static int
3197 next_overlay_change (pos)
3198 int pos;
3200 int noverlays;
3201 int endpos;
3202 Lisp_Object *overlays;
3203 int i;
3205 /* Get all overlays at the given position. */
3206 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3208 /* If any of these overlays ends before endpos,
3209 use its ending point instead. */
3210 for (i = 0; i < noverlays; ++i)
3212 Lisp_Object oend;
3213 int oendpos;
3215 oend = OVERLAY_END (overlays[i]);
3216 oendpos = OVERLAY_POSITION (oend);
3217 endpos = min (endpos, oendpos);
3220 return endpos;
3225 /***********************************************************************
3226 Fontification
3227 ***********************************************************************/
3229 /* Handle changes in the `fontified' property of the current buffer by
3230 calling hook functions from Qfontification_functions to fontify
3231 regions of text. */
3233 static enum prop_handled
3234 handle_fontified_prop (it)
3235 struct it *it;
3237 Lisp_Object prop, pos;
3238 enum prop_handled handled = HANDLED_NORMALLY;
3240 if (!NILP (Vmemory_full))
3241 return handled;
3243 /* Get the value of the `fontified' property at IT's current buffer
3244 position. (The `fontified' property doesn't have a special
3245 meaning in strings.) If the value is nil, call functions from
3246 Qfontification_functions. */
3247 if (!STRINGP (it->string)
3248 && it->s == NULL
3249 && !NILP (Vfontification_functions)
3250 && !NILP (Vrun_hooks)
3251 && (pos = make_number (IT_CHARPOS (*it)),
3252 prop = Fget_char_property (pos, Qfontified, Qnil),
3253 /* Ignore the special cased nil value always present at EOB since
3254 no amount of fontifying will be able to change it. */
3255 NILP (prop) && IT_CHARPOS (*it) < Z))
3257 int count = SPECPDL_INDEX ();
3258 Lisp_Object val;
3260 val = Vfontification_functions;
3261 specbind (Qfontification_functions, Qnil);
3263 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3264 safe_call1 (val, pos);
3265 else
3267 Lisp_Object globals, fn;
3268 struct gcpro gcpro1, gcpro2;
3270 globals = Qnil;
3271 GCPRO2 (val, globals);
3273 for (; CONSP (val); val = XCDR (val))
3275 fn = XCAR (val);
3277 if (EQ (fn, Qt))
3279 /* A value of t indicates this hook has a local
3280 binding; it means to run the global binding too.
3281 In a global value, t should not occur. If it
3282 does, we must ignore it to avoid an endless
3283 loop. */
3284 for (globals = Fdefault_value (Qfontification_functions);
3285 CONSP (globals);
3286 globals = XCDR (globals))
3288 fn = XCAR (globals);
3289 if (!EQ (fn, Qt))
3290 safe_call1 (fn, pos);
3293 else
3294 safe_call1 (fn, pos);
3297 UNGCPRO;
3300 unbind_to (count, Qnil);
3302 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3303 something. This avoids an endless loop if they failed to
3304 fontify the text for which reason ever. */
3305 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3306 handled = HANDLED_RECOMPUTE_PROPS;
3309 return handled;
3314 /***********************************************************************
3315 Faces
3316 ***********************************************************************/
3318 /* Set up iterator IT from face properties at its current position.
3319 Called from handle_stop. */
3321 static enum prop_handled
3322 handle_face_prop (it)
3323 struct it *it;
3325 int new_face_id, next_stop;
3327 if (!STRINGP (it->string))
3329 new_face_id
3330 = face_at_buffer_position (it->w,
3331 IT_CHARPOS (*it),
3332 it->region_beg_charpos,
3333 it->region_end_charpos,
3334 &next_stop,
3335 (IT_CHARPOS (*it)
3336 + TEXT_PROP_DISTANCE_LIMIT),
3339 /* Is this a start of a run of characters with box face?
3340 Caveat: this can be called for a freshly initialized
3341 iterator; face_id is -1 in this case. We know that the new
3342 face will not change until limit, i.e. if the new face has a
3343 box, all characters up to limit will have one. But, as
3344 usual, we don't know whether limit is really the end. */
3345 if (new_face_id != it->face_id)
3347 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3349 /* If new face has a box but old face has not, this is
3350 the start of a run of characters with box, i.e. it has
3351 a shadow on the left side. The value of face_id of the
3352 iterator will be -1 if this is the initial call that gets
3353 the face. In this case, we have to look in front of IT's
3354 position and see whether there is a face != new_face_id. */
3355 it->start_of_box_run_p
3356 = (new_face->box != FACE_NO_BOX
3357 && (it->face_id >= 0
3358 || IT_CHARPOS (*it) == BEG
3359 || new_face_id != face_before_it_pos (it)));
3360 it->face_box_p = new_face->box != FACE_NO_BOX;
3363 else
3365 int base_face_id, bufpos;
3367 if (it->current.overlay_string_index >= 0)
3368 bufpos = IT_CHARPOS (*it);
3369 else
3370 bufpos = 0;
3372 /* For strings from a buffer, i.e. overlay strings or strings
3373 from a `display' property, use the face at IT's current
3374 buffer position as the base face to merge with, so that
3375 overlay strings appear in the same face as surrounding
3376 text, unless they specify their own faces. */
3377 base_face_id = underlying_face_id (it);
3379 new_face_id = face_at_string_position (it->w,
3380 it->string,
3381 IT_STRING_CHARPOS (*it),
3382 bufpos,
3383 it->region_beg_charpos,
3384 it->region_end_charpos,
3385 &next_stop,
3386 base_face_id, 0);
3388 #if 0 /* This shouldn't be neccessary. Let's check it. */
3389 /* If IT is used to display a mode line we would really like to
3390 use the mode line face instead of the frame's default face. */
3391 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3392 && new_face_id == DEFAULT_FACE_ID)
3393 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3394 #endif
3396 /* Is this a start of a run of characters with box? Caveat:
3397 this can be called for a freshly allocated iterator; face_id
3398 is -1 is this case. We know that the new face will not
3399 change until the next check pos, i.e. if the new face has a
3400 box, all characters up to that position will have a
3401 box. But, as usual, we don't know whether that position
3402 is really the end. */
3403 if (new_face_id != it->face_id)
3405 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3406 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3408 /* If new face has a box but old face hasn't, this is the
3409 start of a run of characters with box, i.e. it has a
3410 shadow on the left side. */
3411 it->start_of_box_run_p
3412 = new_face->box && (old_face == NULL || !old_face->box);
3413 it->face_box_p = new_face->box != FACE_NO_BOX;
3417 it->face_id = new_face_id;
3418 return HANDLED_NORMALLY;
3422 /* Return the ID of the face ``underlying'' IT's current position,
3423 which is in a string. If the iterator is associated with a
3424 buffer, return the face at IT's current buffer position.
3425 Otherwise, use the iterator's base_face_id. */
3427 static int
3428 underlying_face_id (it)
3429 struct it *it;
3431 int face_id = it->base_face_id, i;
3433 xassert (STRINGP (it->string));
3435 for (i = it->sp - 1; i >= 0; --i)
3436 if (NILP (it->stack[i].string))
3437 face_id = it->stack[i].face_id;
3439 return face_id;
3443 /* Compute the face one character before or after the current position
3444 of IT. BEFORE_P non-zero means get the face in front of IT's
3445 position. Value is the id of the face. */
3447 static int
3448 face_before_or_after_it_pos (it, before_p)
3449 struct it *it;
3450 int before_p;
3452 int face_id, limit;
3453 int next_check_charpos;
3454 struct text_pos pos;
3456 xassert (it->s == NULL);
3458 if (STRINGP (it->string))
3460 int bufpos, base_face_id;
3462 /* No face change past the end of the string (for the case
3463 we are padding with spaces). No face change before the
3464 string start. */
3465 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3466 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3467 return it->face_id;
3469 /* Set pos to the position before or after IT's current position. */
3470 if (before_p)
3471 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3472 else
3473 /* For composition, we must check the character after the
3474 composition. */
3475 pos = (it->what == IT_COMPOSITION
3476 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3477 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3479 if (it->current.overlay_string_index >= 0)
3480 bufpos = IT_CHARPOS (*it);
3481 else
3482 bufpos = 0;
3484 base_face_id = underlying_face_id (it);
3486 /* Get the face for ASCII, or unibyte. */
3487 face_id = face_at_string_position (it->w,
3488 it->string,
3489 CHARPOS (pos),
3490 bufpos,
3491 it->region_beg_charpos,
3492 it->region_end_charpos,
3493 &next_check_charpos,
3494 base_face_id, 0);
3496 /* Correct the face for charsets different from ASCII. Do it
3497 for the multibyte case only. The face returned above is
3498 suitable for unibyte text if IT->string is unibyte. */
3499 if (STRING_MULTIBYTE (it->string))
3501 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3502 int rest = SBYTES (it->string) - BYTEPOS (pos);
3503 int c, len;
3504 struct face *face = FACE_FROM_ID (it->f, face_id);
3506 c = string_char_and_length (p, rest, &len);
3507 face_id = FACE_FOR_CHAR (it->f, face, c);
3510 else
3512 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3513 || (IT_CHARPOS (*it) <= BEGV && before_p))
3514 return it->face_id;
3516 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3517 pos = it->current.pos;
3519 if (before_p)
3520 DEC_TEXT_POS (pos, it->multibyte_p);
3521 else
3523 if (it->what == IT_COMPOSITION)
3524 /* For composition, we must check the position after the
3525 composition. */
3526 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3527 else
3528 INC_TEXT_POS (pos, it->multibyte_p);
3531 /* Determine face for CHARSET_ASCII, or unibyte. */
3532 face_id = face_at_buffer_position (it->w,
3533 CHARPOS (pos),
3534 it->region_beg_charpos,
3535 it->region_end_charpos,
3536 &next_check_charpos,
3537 limit, 0);
3539 /* Correct the face for charsets different from ASCII. Do it
3540 for the multibyte case only. The face returned above is
3541 suitable for unibyte text if current_buffer is unibyte. */
3542 if (it->multibyte_p)
3544 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3545 struct face *face = FACE_FROM_ID (it->f, face_id);
3546 face_id = FACE_FOR_CHAR (it->f, face, c);
3550 return face_id;
3555 /***********************************************************************
3556 Invisible text
3557 ***********************************************************************/
3559 /* Set up iterator IT from invisible properties at its current
3560 position. Called from handle_stop. */
3562 static enum prop_handled
3563 handle_invisible_prop (it)
3564 struct it *it;
3566 enum prop_handled handled = HANDLED_NORMALLY;
3568 if (STRINGP (it->string))
3570 extern Lisp_Object Qinvisible;
3571 Lisp_Object prop, end_charpos, limit, charpos;
3573 /* Get the value of the invisible text property at the
3574 current position. Value will be nil if there is no such
3575 property. */
3576 charpos = make_number (IT_STRING_CHARPOS (*it));
3577 prop = Fget_text_property (charpos, Qinvisible, it->string);
3579 if (!NILP (prop)
3580 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3582 handled = HANDLED_RECOMPUTE_PROPS;
3584 /* Get the position at which the next change of the
3585 invisible text property can be found in IT->string.
3586 Value will be nil if the property value is the same for
3587 all the rest of IT->string. */
3588 XSETINT (limit, SCHARS (it->string));
3589 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3590 it->string, limit);
3592 /* Text at current position is invisible. The next
3593 change in the property is at position end_charpos.
3594 Move IT's current position to that position. */
3595 if (INTEGERP (end_charpos)
3596 && XFASTINT (end_charpos) < XFASTINT (limit))
3598 struct text_pos old;
3599 old = it->current.string_pos;
3600 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3601 compute_string_pos (&it->current.string_pos, old, it->string);
3603 else
3605 /* The rest of the string is invisible. If this is an
3606 overlay string, proceed with the next overlay string
3607 or whatever comes and return a character from there. */
3608 if (it->current.overlay_string_index >= 0)
3610 next_overlay_string (it);
3611 /* Don't check for overlay strings when we just
3612 finished processing them. */
3613 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3615 else
3617 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3618 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3623 else
3625 int invis_p, newpos, next_stop, start_charpos;
3626 Lisp_Object pos, prop, overlay;
3628 /* First of all, is there invisible text at this position? */
3629 start_charpos = IT_CHARPOS (*it);
3630 pos = make_number (IT_CHARPOS (*it));
3631 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3632 &overlay);
3633 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3635 /* If we are on invisible text, skip over it. */
3636 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3638 /* Record whether we have to display an ellipsis for the
3639 invisible text. */
3640 int display_ellipsis_p = invis_p == 2;
3642 handled = HANDLED_RECOMPUTE_PROPS;
3644 /* Loop skipping over invisible text. The loop is left at
3645 ZV or with IT on the first char being visible again. */
3648 /* Try to skip some invisible text. Return value is the
3649 position reached which can be equal to IT's position
3650 if there is nothing invisible here. This skips both
3651 over invisible text properties and overlays with
3652 invisible property. */
3653 newpos = skip_invisible (IT_CHARPOS (*it),
3654 &next_stop, ZV, it->window);
3656 /* If we skipped nothing at all we weren't at invisible
3657 text in the first place. If everything to the end of
3658 the buffer was skipped, end the loop. */
3659 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3660 invis_p = 0;
3661 else
3663 /* We skipped some characters but not necessarily
3664 all there are. Check if we ended up on visible
3665 text. Fget_char_property returns the property of
3666 the char before the given position, i.e. if we
3667 get invis_p = 0, this means that the char at
3668 newpos is visible. */
3669 pos = make_number (newpos);
3670 prop = Fget_char_property (pos, Qinvisible, it->window);
3671 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3674 /* If we ended up on invisible text, proceed to
3675 skip starting with next_stop. */
3676 if (invis_p)
3677 IT_CHARPOS (*it) = next_stop;
3679 /* If there are adjacent invisible texts, don't lose the
3680 second one's ellipsis. */
3681 if (invis_p == 2)
3682 display_ellipsis_p = 1;
3684 while (invis_p);
3686 /* The position newpos is now either ZV or on visible text. */
3687 IT_CHARPOS (*it) = newpos;
3688 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3690 /* If there are before-strings at the start of invisible
3691 text, and the text is invisible because of a text
3692 property, arrange to show before-strings because 20.x did
3693 it that way. (If the text is invisible because of an
3694 overlay property instead of a text property, this is
3695 already handled in the overlay code.) */
3696 if (NILP (overlay)
3697 && get_overlay_strings (it, start_charpos))
3699 handled = HANDLED_RECOMPUTE_PROPS;
3700 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3702 else if (display_ellipsis_p)
3704 /* Make sure that the glyphs of the ellipsis will get
3705 correct `charpos' values. If we would not update
3706 it->position here, the glyphs would belong to the
3707 last visible character _before_ the invisible
3708 text, which confuses `set_cursor_from_row'.
3710 We use the last invisible position instead of the
3711 first because this way the cursor is always drawn on
3712 the first "." of the ellipsis, whenever PT is inside
3713 the invisible text. Otherwise the cursor would be
3714 placed _after_ the ellipsis when the point is after the
3715 first invisible character. */
3716 if (!STRINGP (it->object))
3718 it->position.charpos = IT_CHARPOS (*it) - 1;
3719 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3721 setup_for_ellipsis (it, 0);
3726 return handled;
3730 /* Make iterator IT return `...' next.
3731 Replaces LEN characters from buffer. */
3733 static void
3734 setup_for_ellipsis (it, len)
3735 struct it *it;
3736 int len;
3738 /* Use the display table definition for `...'. Invalid glyphs
3739 will be handled by the method returning elements from dpvec. */
3740 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3742 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3743 it->dpvec = v->contents;
3744 it->dpend = v->contents + v->size;
3746 else
3748 /* Default `...'. */
3749 it->dpvec = default_invis_vector;
3750 it->dpend = default_invis_vector + 3;
3753 it->dpvec_char_len = len;
3754 it->current.dpvec_index = 0;
3755 it->dpvec_face_id = -1;
3757 /* Remember the current face id in case glyphs specify faces.
3758 IT's face is restored in set_iterator_to_next.
3759 saved_face_id was set to preceding char's face in handle_stop. */
3760 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3761 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3763 it->method = GET_FROM_DISPLAY_VECTOR;
3764 it->ellipsis_p = 1;
3769 /***********************************************************************
3770 'display' property
3771 ***********************************************************************/
3773 /* Set up iterator IT from `display' property at its current position.
3774 Called from handle_stop.
3775 We return HANDLED_RETURN if some part of the display property
3776 overrides the display of the buffer text itself.
3777 Otherwise we return HANDLED_NORMALLY. */
3779 static enum prop_handled
3780 handle_display_prop (it)
3781 struct it *it;
3783 Lisp_Object prop, object;
3784 struct text_pos *position;
3785 /* Nonzero if some property replaces the display of the text itself. */
3786 int display_replaced_p = 0;
3788 if (STRINGP (it->string))
3790 object = it->string;
3791 position = &it->current.string_pos;
3793 else
3795 XSETWINDOW (object, it->w);
3796 position = &it->current.pos;
3799 /* Reset those iterator values set from display property values. */
3800 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3801 it->space_width = Qnil;
3802 it->font_height = Qnil;
3803 it->voffset = 0;
3805 /* We don't support recursive `display' properties, i.e. string
3806 values that have a string `display' property, that have a string
3807 `display' property etc. */
3808 if (!it->string_from_display_prop_p)
3809 it->area = TEXT_AREA;
3811 prop = Fget_char_property (make_number (position->charpos),
3812 Qdisplay, object);
3813 if (NILP (prop))
3814 return HANDLED_NORMALLY;
3816 if (!STRINGP (it->string))
3817 object = it->w->buffer;
3819 if (CONSP (prop)
3820 /* Simple properties. */
3821 && !EQ (XCAR (prop), Qimage)
3822 && !EQ (XCAR (prop), Qspace)
3823 && !EQ (XCAR (prop), Qwhen)
3824 && !EQ (XCAR (prop), Qslice)
3825 && !EQ (XCAR (prop), Qspace_width)
3826 && !EQ (XCAR (prop), Qheight)
3827 && !EQ (XCAR (prop), Qraise)
3828 /* Marginal area specifications. */
3829 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3830 && !EQ (XCAR (prop), Qleft_fringe)
3831 && !EQ (XCAR (prop), Qright_fringe)
3832 && !NILP (XCAR (prop)))
3834 for (; CONSP (prop); prop = XCDR (prop))
3836 if (handle_single_display_spec (it, XCAR (prop), object,
3837 position, display_replaced_p))
3838 display_replaced_p = 1;
3841 else if (VECTORP (prop))
3843 int i;
3844 for (i = 0; i < ASIZE (prop); ++i)
3845 if (handle_single_display_spec (it, AREF (prop, i), object,
3846 position, display_replaced_p))
3847 display_replaced_p = 1;
3849 else
3851 int ret = handle_single_display_spec (it, prop, object, position, 0);
3852 if (ret < 0) /* Replaced by "", i.e. nothing. */
3853 return HANDLED_RECOMPUTE_PROPS;
3854 if (ret)
3855 display_replaced_p = 1;
3858 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3862 /* Value is the position of the end of the `display' property starting
3863 at START_POS in OBJECT. */
3865 static struct text_pos
3866 display_prop_end (it, object, start_pos)
3867 struct it *it;
3868 Lisp_Object object;
3869 struct text_pos start_pos;
3871 Lisp_Object end;
3872 struct text_pos end_pos;
3874 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3875 Qdisplay, object, Qnil);
3876 CHARPOS (end_pos) = XFASTINT (end);
3877 if (STRINGP (object))
3878 compute_string_pos (&end_pos, start_pos, it->string);
3879 else
3880 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3882 return end_pos;
3886 /* Set up IT from a single `display' specification PROP. OBJECT
3887 is the object in which the `display' property was found. *POSITION
3888 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3889 means that we previously saw a display specification which already
3890 replaced text display with something else, for example an image;
3891 we ignore such properties after the first one has been processed.
3893 If PROP is a `space' or `image' specification, and in some other
3894 cases too, set *POSITION to the position where the `display'
3895 property ends.
3897 Value is non-zero if something was found which replaces the display
3898 of buffer or string text. Specifically, the value is -1 if that
3899 "something" is "nothing". */
3901 static int
3902 handle_single_display_spec (it, spec, object, position,
3903 display_replaced_before_p)
3904 struct it *it;
3905 Lisp_Object spec;
3906 Lisp_Object object;
3907 struct text_pos *position;
3908 int display_replaced_before_p;
3910 Lisp_Object form;
3911 Lisp_Object location, value;
3912 struct text_pos start_pos, save_pos;
3913 int valid_p;
3915 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3916 If the result is non-nil, use VALUE instead of SPEC. */
3917 form = Qt;
3918 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3920 spec = XCDR (spec);
3921 if (!CONSP (spec))
3922 return 0;
3923 form = XCAR (spec);
3924 spec = XCDR (spec);
3927 if (!NILP (form) && !EQ (form, Qt))
3929 int count = SPECPDL_INDEX ();
3930 struct gcpro gcpro1;
3932 /* Bind `object' to the object having the `display' property, a
3933 buffer or string. Bind `position' to the position in the
3934 object where the property was found, and `buffer-position'
3935 to the current position in the buffer. */
3936 specbind (Qobject, object);
3937 specbind (Qposition, make_number (CHARPOS (*position)));
3938 specbind (Qbuffer_position,
3939 make_number (STRINGP (object)
3940 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3941 GCPRO1 (form);
3942 form = safe_eval (form);
3943 UNGCPRO;
3944 unbind_to (count, Qnil);
3947 if (NILP (form))
3948 return 0;
3950 /* Handle `(height HEIGHT)' specifications. */
3951 if (CONSP (spec)
3952 && EQ (XCAR (spec), Qheight)
3953 && CONSP (XCDR (spec)))
3955 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3956 return 0;
3958 it->font_height = XCAR (XCDR (spec));
3959 if (!NILP (it->font_height))
3961 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3962 int new_height = -1;
3964 if (CONSP (it->font_height)
3965 && (EQ (XCAR (it->font_height), Qplus)
3966 || EQ (XCAR (it->font_height), Qminus))
3967 && CONSP (XCDR (it->font_height))
3968 && INTEGERP (XCAR (XCDR (it->font_height))))
3970 /* `(+ N)' or `(- N)' where N is an integer. */
3971 int steps = XINT (XCAR (XCDR (it->font_height)));
3972 if (EQ (XCAR (it->font_height), Qplus))
3973 steps = - steps;
3974 it->face_id = smaller_face (it->f, it->face_id, steps);
3976 else if (FUNCTIONP (it->font_height))
3978 /* Call function with current height as argument.
3979 Value is the new height. */
3980 Lisp_Object height;
3981 height = safe_call1 (it->font_height,
3982 face->lface[LFACE_HEIGHT_INDEX]);
3983 if (NUMBERP (height))
3984 new_height = XFLOATINT (height);
3986 else if (NUMBERP (it->font_height))
3988 /* Value is a multiple of the canonical char height. */
3989 struct face *face;
3991 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3992 new_height = (XFLOATINT (it->font_height)
3993 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3995 else
3997 /* Evaluate IT->font_height with `height' bound to the
3998 current specified height to get the new height. */
3999 int count = SPECPDL_INDEX ();
4001 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4002 value = safe_eval (it->font_height);
4003 unbind_to (count, Qnil);
4005 if (NUMBERP (value))
4006 new_height = XFLOATINT (value);
4009 if (new_height > 0)
4010 it->face_id = face_with_height (it->f, it->face_id, new_height);
4013 return 0;
4016 /* Handle `(space_width WIDTH)'. */
4017 if (CONSP (spec)
4018 && EQ (XCAR (spec), Qspace_width)
4019 && CONSP (XCDR (spec)))
4021 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4022 return 0;
4024 value = XCAR (XCDR (spec));
4025 if (NUMBERP (value) && XFLOATINT (value) > 0)
4026 it->space_width = value;
4028 return 0;
4031 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4032 if (CONSP (spec)
4033 && EQ (XCAR (spec), Qslice))
4035 Lisp_Object tem;
4037 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4038 return 0;
4040 if (tem = XCDR (spec), CONSP (tem))
4042 it->slice.x = XCAR (tem);
4043 if (tem = XCDR (tem), CONSP (tem))
4045 it->slice.y = XCAR (tem);
4046 if (tem = XCDR (tem), CONSP (tem))
4048 it->slice.width = XCAR (tem);
4049 if (tem = XCDR (tem), CONSP (tem))
4050 it->slice.height = XCAR (tem);
4055 return 0;
4058 /* Handle `(raise FACTOR)'. */
4059 if (CONSP (spec)
4060 && EQ (XCAR (spec), Qraise)
4061 && CONSP (XCDR (spec)))
4063 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4064 return 0;
4066 #ifdef HAVE_WINDOW_SYSTEM
4067 value = XCAR (XCDR (spec));
4068 if (NUMBERP (value))
4070 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4071 it->voffset = - (XFLOATINT (value)
4072 * (FONT_HEIGHT (face->font)));
4074 #endif /* HAVE_WINDOW_SYSTEM */
4076 return 0;
4079 /* Don't handle the other kinds of display specifications
4080 inside a string that we got from a `display' property. */
4081 if (it->string_from_display_prop_p)
4082 return 0;
4084 /* Characters having this form of property are not displayed, so
4085 we have to find the end of the property. */
4086 start_pos = *position;
4087 *position = display_prop_end (it, object, start_pos);
4088 value = Qnil;
4090 /* Stop the scan at that end position--we assume that all
4091 text properties change there. */
4092 it->stop_charpos = position->charpos;
4094 /* Handle `(left-fringe BITMAP [FACE])'
4095 and `(right-fringe BITMAP [FACE])'. */
4096 if (CONSP (spec)
4097 && (EQ (XCAR (spec), Qleft_fringe)
4098 || EQ (XCAR (spec), Qright_fringe))
4099 && CONSP (XCDR (spec)))
4101 int face_id = DEFAULT_FACE_ID;
4102 int fringe_bitmap;
4104 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4105 /* If we return here, POSITION has been advanced
4106 across the text with this property. */
4107 return 0;
4109 #ifdef HAVE_WINDOW_SYSTEM
4110 value = XCAR (XCDR (spec));
4111 if (!SYMBOLP (value)
4112 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4113 /* If we return here, POSITION has been advanced
4114 across the text with this property. */
4115 return 0;
4117 if (CONSP (XCDR (XCDR (spec))))
4119 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4120 int face_id2 = lookup_derived_face (it->f, face_name,
4121 'A', FRINGE_FACE_ID, 0);
4122 if (face_id2 >= 0)
4123 face_id = face_id2;
4126 /* Save current settings of IT so that we can restore them
4127 when we are finished with the glyph property value. */
4129 save_pos = it->position;
4130 it->position = *position;
4131 push_it (it);
4132 it->position = save_pos;
4134 it->area = TEXT_AREA;
4135 it->what = IT_IMAGE;
4136 it->image_id = -1; /* no image */
4137 it->position = start_pos;
4138 it->object = NILP (object) ? it->w->buffer : object;
4139 it->method = GET_FROM_IMAGE;
4140 it->face_id = face_id;
4142 /* Say that we haven't consumed the characters with
4143 `display' property yet. The call to pop_it in
4144 set_iterator_to_next will clean this up. */
4145 *position = start_pos;
4147 if (EQ (XCAR (spec), Qleft_fringe))
4149 it->left_user_fringe_bitmap = fringe_bitmap;
4150 it->left_user_fringe_face_id = face_id;
4152 else
4154 it->right_user_fringe_bitmap = fringe_bitmap;
4155 it->right_user_fringe_face_id = face_id;
4157 #endif /* HAVE_WINDOW_SYSTEM */
4158 return 1;
4161 /* Prepare to handle `((margin left-margin) ...)',
4162 `((margin right-margin) ...)' and `((margin nil) ...)'
4163 prefixes for display specifications. */
4164 location = Qunbound;
4165 if (CONSP (spec) && CONSP (XCAR (spec)))
4167 Lisp_Object tem;
4169 value = XCDR (spec);
4170 if (CONSP (value))
4171 value = XCAR (value);
4173 tem = XCAR (spec);
4174 if (EQ (XCAR (tem), Qmargin)
4175 && (tem = XCDR (tem),
4176 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4177 (NILP (tem)
4178 || EQ (tem, Qleft_margin)
4179 || EQ (tem, Qright_margin))))
4180 location = tem;
4183 if (EQ (location, Qunbound))
4185 location = Qnil;
4186 value = spec;
4189 /* After this point, VALUE is the property after any
4190 margin prefix has been stripped. It must be a string,
4191 an image specification, or `(space ...)'.
4193 LOCATION specifies where to display: `left-margin',
4194 `right-margin' or nil. */
4196 valid_p = (STRINGP (value)
4197 #ifdef HAVE_WINDOW_SYSTEM
4198 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4199 #endif /* not HAVE_WINDOW_SYSTEM */
4200 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4202 if (valid_p && !display_replaced_before_p)
4204 /* Save current settings of IT so that we can restore them
4205 when we are finished with the glyph property value. */
4206 save_pos = it->position;
4207 it->position = *position;
4208 push_it (it);
4209 it->position = save_pos;
4211 if (NILP (location))
4212 it->area = TEXT_AREA;
4213 else if (EQ (location, Qleft_margin))
4214 it->area = LEFT_MARGIN_AREA;
4215 else
4216 it->area = RIGHT_MARGIN_AREA;
4218 if (STRINGP (value))
4220 if (SCHARS (value) == 0)
4222 pop_it (it);
4223 return -1; /* Replaced by "", i.e. nothing. */
4225 it->string = value;
4226 it->multibyte_p = STRING_MULTIBYTE (it->string);
4227 it->current.overlay_string_index = -1;
4228 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4229 it->end_charpos = it->string_nchars = SCHARS (it->string);
4230 it->method = GET_FROM_STRING;
4231 it->stop_charpos = 0;
4232 it->string_from_display_prop_p = 1;
4233 /* Say that we haven't consumed the characters with
4234 `display' property yet. The call to pop_it in
4235 set_iterator_to_next will clean this up. */
4236 *position = start_pos;
4238 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4240 it->method = GET_FROM_STRETCH;
4241 it->object = value;
4242 *position = it->position = start_pos;
4244 #ifdef HAVE_WINDOW_SYSTEM
4245 else
4247 it->what = IT_IMAGE;
4248 it->image_id = lookup_image (it->f, value);
4249 it->position = start_pos;
4250 it->object = NILP (object) ? it->w->buffer : object;
4251 it->method = GET_FROM_IMAGE;
4253 /* Say that we haven't consumed the characters with
4254 `display' property yet. The call to pop_it in
4255 set_iterator_to_next will clean this up. */
4256 *position = start_pos;
4258 #endif /* HAVE_WINDOW_SYSTEM */
4260 return 1;
4263 /* Invalid property or property not supported. Restore
4264 POSITION to what it was before. */
4265 *position = start_pos;
4266 return 0;
4270 /* Check if SPEC is a display specification value whose text should be
4271 treated as intangible. */
4273 static int
4274 single_display_spec_intangible_p (prop)
4275 Lisp_Object prop;
4277 /* Skip over `when FORM'. */
4278 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4280 prop = XCDR (prop);
4281 if (!CONSP (prop))
4282 return 0;
4283 prop = XCDR (prop);
4286 if (STRINGP (prop))
4287 return 1;
4289 if (!CONSP (prop))
4290 return 0;
4292 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4293 we don't need to treat text as intangible. */
4294 if (EQ (XCAR (prop), Qmargin))
4296 prop = XCDR (prop);
4297 if (!CONSP (prop))
4298 return 0;
4300 prop = XCDR (prop);
4301 if (!CONSP (prop)
4302 || EQ (XCAR (prop), Qleft_margin)
4303 || EQ (XCAR (prop), Qright_margin))
4304 return 0;
4307 return (CONSP (prop)
4308 && (EQ (XCAR (prop), Qimage)
4309 || EQ (XCAR (prop), Qspace)));
4313 /* Check if PROP is a display property value whose text should be
4314 treated as intangible. */
4317 display_prop_intangible_p (prop)
4318 Lisp_Object prop;
4320 if (CONSP (prop)
4321 && CONSP (XCAR (prop))
4322 && !EQ (Qmargin, XCAR (XCAR (prop))))
4324 /* A list of sub-properties. */
4325 while (CONSP (prop))
4327 if (single_display_spec_intangible_p (XCAR (prop)))
4328 return 1;
4329 prop = XCDR (prop);
4332 else if (VECTORP (prop))
4334 /* A vector of sub-properties. */
4335 int i;
4336 for (i = 0; i < ASIZE (prop); ++i)
4337 if (single_display_spec_intangible_p (AREF (prop, i)))
4338 return 1;
4340 else
4341 return single_display_spec_intangible_p (prop);
4343 return 0;
4347 /* Return 1 if PROP is a display sub-property value containing STRING. */
4349 static int
4350 single_display_spec_string_p (prop, string)
4351 Lisp_Object prop, string;
4353 if (EQ (string, prop))
4354 return 1;
4356 /* Skip over `when FORM'. */
4357 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4359 prop = XCDR (prop);
4360 if (!CONSP (prop))
4361 return 0;
4362 prop = XCDR (prop);
4365 if (CONSP (prop))
4366 /* Skip over `margin LOCATION'. */
4367 if (EQ (XCAR (prop), Qmargin))
4369 prop = XCDR (prop);
4370 if (!CONSP (prop))
4371 return 0;
4373 prop = XCDR (prop);
4374 if (!CONSP (prop))
4375 return 0;
4378 return CONSP (prop) && EQ (XCAR (prop), string);
4382 /* Return 1 if STRING appears in the `display' property PROP. */
4384 static int
4385 display_prop_string_p (prop, string)
4386 Lisp_Object prop, string;
4388 if (CONSP (prop)
4389 && CONSP (XCAR (prop))
4390 && !EQ (Qmargin, XCAR (XCAR (prop))))
4392 /* A list of sub-properties. */
4393 while (CONSP (prop))
4395 if (single_display_spec_string_p (XCAR (prop), string))
4396 return 1;
4397 prop = XCDR (prop);
4400 else if (VECTORP (prop))
4402 /* A vector of sub-properties. */
4403 int i;
4404 for (i = 0; i < ASIZE (prop); ++i)
4405 if (single_display_spec_string_p (AREF (prop, i), string))
4406 return 1;
4408 else
4409 return single_display_spec_string_p (prop, string);
4411 return 0;
4415 /* Determine from which buffer position in W's buffer STRING comes
4416 from. AROUND_CHARPOS is an approximate position where it could
4417 be from. Value is the buffer position or 0 if it couldn't be
4418 determined.
4420 W's buffer must be current.
4422 This function is necessary because we don't record buffer positions
4423 in glyphs generated from strings (to keep struct glyph small).
4424 This function may only use code that doesn't eval because it is
4425 called asynchronously from note_mouse_highlight. */
4428 string_buffer_position (w, string, around_charpos)
4429 struct window *w;
4430 Lisp_Object string;
4431 int around_charpos;
4433 Lisp_Object limit, prop, pos;
4434 const int MAX_DISTANCE = 1000;
4435 int found = 0;
4437 pos = make_number (around_charpos);
4438 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4439 while (!found && !EQ (pos, limit))
4441 prop = Fget_char_property (pos, Qdisplay, Qnil);
4442 if (!NILP (prop) && display_prop_string_p (prop, string))
4443 found = 1;
4444 else
4445 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4448 if (!found)
4450 pos = make_number (around_charpos);
4451 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4452 while (!found && !EQ (pos, limit))
4454 prop = Fget_char_property (pos, Qdisplay, Qnil);
4455 if (!NILP (prop) && display_prop_string_p (prop, string))
4456 found = 1;
4457 else
4458 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4459 limit);
4463 return found ? XINT (pos) : 0;
4468 /***********************************************************************
4469 `composition' property
4470 ***********************************************************************/
4472 /* Set up iterator IT from `composition' property at its current
4473 position. Called from handle_stop. */
4475 static enum prop_handled
4476 handle_composition_prop (it)
4477 struct it *it;
4479 Lisp_Object prop, string;
4480 int pos, pos_byte, end;
4481 enum prop_handled handled = HANDLED_NORMALLY;
4483 if (STRINGP (it->string))
4485 pos = IT_STRING_CHARPOS (*it);
4486 pos_byte = IT_STRING_BYTEPOS (*it);
4487 string = it->string;
4489 else
4491 pos = IT_CHARPOS (*it);
4492 pos_byte = IT_BYTEPOS (*it);
4493 string = Qnil;
4496 /* If there's a valid composition and point is not inside of the
4497 composition (in the case that the composition is from the current
4498 buffer), draw a glyph composed from the composition components. */
4499 if (find_composition (pos, -1, &pos, &end, &prop, string)
4500 && COMPOSITION_VALID_P (pos, end, prop)
4501 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4503 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4505 if (id >= 0)
4507 struct composition *cmp = composition_table[id];
4509 if (cmp->glyph_len == 0)
4511 /* No glyph. */
4512 if (STRINGP (it->string))
4514 IT_STRING_CHARPOS (*it) = end;
4515 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4516 end);
4518 else
4520 IT_CHARPOS (*it) = end;
4521 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4523 return HANDLED_RECOMPUTE_PROPS;
4526 it->stop_charpos = end;
4527 push_it (it);
4529 it->method = GET_FROM_COMPOSITION;
4530 it->cmp_id = id;
4531 it->cmp_len = COMPOSITION_LENGTH (prop);
4532 /* For a terminal, draw only the first character of the
4533 components. */
4534 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4535 it->len = (STRINGP (it->string)
4536 ? string_char_to_byte (it->string, end)
4537 : CHAR_TO_BYTE (end)) - pos_byte;
4538 handled = HANDLED_RETURN;
4542 return handled;
4547 /***********************************************************************
4548 Overlay strings
4549 ***********************************************************************/
4551 /* The following structure is used to record overlay strings for
4552 later sorting in load_overlay_strings. */
4554 struct overlay_entry
4556 Lisp_Object overlay;
4557 Lisp_Object string;
4558 int priority;
4559 int after_string_p;
4563 /* Set up iterator IT from overlay strings at its current position.
4564 Called from handle_stop. */
4566 static enum prop_handled
4567 handle_overlay_change (it)
4568 struct it *it;
4570 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4571 return HANDLED_RECOMPUTE_PROPS;
4572 else
4573 return HANDLED_NORMALLY;
4577 /* Set up the next overlay string for delivery by IT, if there is an
4578 overlay string to deliver. Called by set_iterator_to_next when the
4579 end of the current overlay string is reached. If there are more
4580 overlay strings to display, IT->string and
4581 IT->current.overlay_string_index are set appropriately here.
4582 Otherwise IT->string is set to nil. */
4584 static void
4585 next_overlay_string (it)
4586 struct it *it;
4588 ++it->current.overlay_string_index;
4589 if (it->current.overlay_string_index == it->n_overlay_strings)
4591 /* No more overlay strings. Restore IT's settings to what
4592 they were before overlay strings were processed, and
4593 continue to deliver from current_buffer. */
4594 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4596 pop_it (it);
4597 xassert (it->sp > 0
4598 || it->method == GET_FROM_COMPOSITION
4599 || (NILP (it->string)
4600 && it->method == GET_FROM_BUFFER
4601 && it->stop_charpos >= BEGV
4602 && it->stop_charpos <= it->end_charpos));
4603 it->current.overlay_string_index = -1;
4604 it->n_overlay_strings = 0;
4606 /* If we're at the end of the buffer, record that we have
4607 processed the overlay strings there already, so that
4608 next_element_from_buffer doesn't try it again. */
4609 if (IT_CHARPOS (*it) >= it->end_charpos)
4610 it->overlay_strings_at_end_processed_p = 1;
4612 /* If we have to display `...' for invisible text, set
4613 the iterator up for that. */
4614 if (display_ellipsis_p)
4615 setup_for_ellipsis (it, 0);
4617 else
4619 /* There are more overlay strings to process. If
4620 IT->current.overlay_string_index has advanced to a position
4621 where we must load IT->overlay_strings with more strings, do
4622 it. */
4623 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4625 if (it->current.overlay_string_index && i == 0)
4626 load_overlay_strings (it, 0);
4628 /* Initialize IT to deliver display elements from the overlay
4629 string. */
4630 it->string = it->overlay_strings[i];
4631 it->multibyte_p = STRING_MULTIBYTE (it->string);
4632 SET_TEXT_POS (it->current.string_pos, 0, 0);
4633 it->method = GET_FROM_STRING;
4634 it->stop_charpos = 0;
4637 CHECK_IT (it);
4641 /* Compare two overlay_entry structures E1 and E2. Used as a
4642 comparison function for qsort in load_overlay_strings. Overlay
4643 strings for the same position are sorted so that
4645 1. All after-strings come in front of before-strings, except
4646 when they come from the same overlay.
4648 2. Within after-strings, strings are sorted so that overlay strings
4649 from overlays with higher priorities come first.
4651 2. Within before-strings, strings are sorted so that overlay
4652 strings from overlays with higher priorities come last.
4654 Value is analogous to strcmp. */
4657 static int
4658 compare_overlay_entries (e1, e2)
4659 void *e1, *e2;
4661 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4662 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4663 int result;
4665 if (entry1->after_string_p != entry2->after_string_p)
4667 /* Let after-strings appear in front of before-strings if
4668 they come from different overlays. */
4669 if (EQ (entry1->overlay, entry2->overlay))
4670 result = entry1->after_string_p ? 1 : -1;
4671 else
4672 result = entry1->after_string_p ? -1 : 1;
4674 else if (entry1->after_string_p)
4675 /* After-strings sorted in order of decreasing priority. */
4676 result = entry2->priority - entry1->priority;
4677 else
4678 /* Before-strings sorted in order of increasing priority. */
4679 result = entry1->priority - entry2->priority;
4681 return result;
4685 /* Load the vector IT->overlay_strings with overlay strings from IT's
4686 current buffer position, or from CHARPOS if that is > 0. Set
4687 IT->n_overlays to the total number of overlay strings found.
4689 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4690 a time. On entry into load_overlay_strings,
4691 IT->current.overlay_string_index gives the number of overlay
4692 strings that have already been loaded by previous calls to this
4693 function.
4695 IT->add_overlay_start contains an additional overlay start
4696 position to consider for taking overlay strings from, if non-zero.
4697 This position comes into play when the overlay has an `invisible'
4698 property, and both before and after-strings. When we've skipped to
4699 the end of the overlay, because of its `invisible' property, we
4700 nevertheless want its before-string to appear.
4701 IT->add_overlay_start will contain the overlay start position
4702 in this case.
4704 Overlay strings are sorted so that after-string strings come in
4705 front of before-string strings. Within before and after-strings,
4706 strings are sorted by overlay priority. See also function
4707 compare_overlay_entries. */
4709 static void
4710 load_overlay_strings (it, charpos)
4711 struct it *it;
4712 int charpos;
4714 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4715 Lisp_Object overlay, window, str, invisible;
4716 struct Lisp_Overlay *ov;
4717 int start, end;
4718 int size = 20;
4719 int n = 0, i, j, invis_p;
4720 struct overlay_entry *entries
4721 = (struct overlay_entry *) alloca (size * sizeof *entries);
4723 if (charpos <= 0)
4724 charpos = IT_CHARPOS (*it);
4726 /* Append the overlay string STRING of overlay OVERLAY to vector
4727 `entries' which has size `size' and currently contains `n'
4728 elements. AFTER_P non-zero means STRING is an after-string of
4729 OVERLAY. */
4730 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4731 do \
4733 Lisp_Object priority; \
4735 if (n == size) \
4737 int new_size = 2 * size; \
4738 struct overlay_entry *old = entries; \
4739 entries = \
4740 (struct overlay_entry *) alloca (new_size \
4741 * sizeof *entries); \
4742 bcopy (old, entries, size * sizeof *entries); \
4743 size = new_size; \
4746 entries[n].string = (STRING); \
4747 entries[n].overlay = (OVERLAY); \
4748 priority = Foverlay_get ((OVERLAY), Qpriority); \
4749 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4750 entries[n].after_string_p = (AFTER_P); \
4751 ++n; \
4753 while (0)
4755 /* Process overlay before the overlay center. */
4756 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4758 XSETMISC (overlay, ov);
4759 xassert (OVERLAYP (overlay));
4760 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4761 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4763 if (end < charpos)
4764 break;
4766 /* Skip this overlay if it doesn't start or end at IT's current
4767 position. */
4768 if (end != charpos && start != charpos)
4769 continue;
4771 /* Skip this overlay if it doesn't apply to IT->w. */
4772 window = Foverlay_get (overlay, Qwindow);
4773 if (WINDOWP (window) && XWINDOW (window) != it->w)
4774 continue;
4776 /* If the text ``under'' the overlay is invisible, both before-
4777 and after-strings from this overlay are visible; start and
4778 end position are indistinguishable. */
4779 invisible = Foverlay_get (overlay, Qinvisible);
4780 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4782 /* If overlay has a non-empty before-string, record it. */
4783 if ((start == charpos || (end == charpos && invis_p))
4784 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4785 && SCHARS (str))
4786 RECORD_OVERLAY_STRING (overlay, str, 0);
4788 /* If overlay has a non-empty after-string, record it. */
4789 if ((end == charpos || (start == charpos && invis_p))
4790 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4791 && SCHARS (str))
4792 RECORD_OVERLAY_STRING (overlay, str, 1);
4795 /* Process overlays after the overlay center. */
4796 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4798 XSETMISC (overlay, ov);
4799 xassert (OVERLAYP (overlay));
4800 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4801 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4803 if (start > charpos)
4804 break;
4806 /* Skip this overlay if it doesn't start or end at IT's current
4807 position. */
4808 if (end != charpos && start != charpos)
4809 continue;
4811 /* Skip this overlay if it doesn't apply to IT->w. */
4812 window = Foverlay_get (overlay, Qwindow);
4813 if (WINDOWP (window) && XWINDOW (window) != it->w)
4814 continue;
4816 /* If the text ``under'' the overlay is invisible, it has a zero
4817 dimension, and both before- and after-strings apply. */
4818 invisible = Foverlay_get (overlay, Qinvisible);
4819 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4821 /* If overlay has a non-empty before-string, record it. */
4822 if ((start == charpos || (end == charpos && invis_p))
4823 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4824 && SCHARS (str))
4825 RECORD_OVERLAY_STRING (overlay, str, 0);
4827 /* If overlay has a non-empty after-string, record it. */
4828 if ((end == charpos || (start == charpos && invis_p))
4829 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4830 && SCHARS (str))
4831 RECORD_OVERLAY_STRING (overlay, str, 1);
4834 #undef RECORD_OVERLAY_STRING
4836 /* Sort entries. */
4837 if (n > 1)
4838 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4840 /* Record the total number of strings to process. */
4841 it->n_overlay_strings = n;
4843 /* IT->current.overlay_string_index is the number of overlay strings
4844 that have already been consumed by IT. Copy some of the
4845 remaining overlay strings to IT->overlay_strings. */
4846 i = 0;
4847 j = it->current.overlay_string_index;
4848 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4849 it->overlay_strings[i++] = entries[j++].string;
4851 CHECK_IT (it);
4855 /* Get the first chunk of overlay strings at IT's current buffer
4856 position, or at CHARPOS if that is > 0. Value is non-zero if at
4857 least one overlay string was found. */
4859 static int
4860 get_overlay_strings_1 (it, charpos, compute_stop_p)
4861 struct it *it;
4862 int charpos;
4864 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4865 process. This fills IT->overlay_strings with strings, and sets
4866 IT->n_overlay_strings to the total number of strings to process.
4867 IT->pos.overlay_string_index has to be set temporarily to zero
4868 because load_overlay_strings needs this; it must be set to -1
4869 when no overlay strings are found because a zero value would
4870 indicate a position in the first overlay string. */
4871 it->current.overlay_string_index = 0;
4872 load_overlay_strings (it, charpos);
4874 /* If we found overlay strings, set up IT to deliver display
4875 elements from the first one. Otherwise set up IT to deliver
4876 from current_buffer. */
4877 if (it->n_overlay_strings)
4879 /* Make sure we know settings in current_buffer, so that we can
4880 restore meaningful values when we're done with the overlay
4881 strings. */
4882 if (compute_stop_p)
4883 compute_stop_pos (it);
4884 xassert (it->face_id >= 0);
4886 /* Save IT's settings. They are restored after all overlay
4887 strings have been processed. */
4888 xassert (!compute_stop_p || it->sp == 0);
4889 push_it (it);
4891 /* Set up IT to deliver display elements from the first overlay
4892 string. */
4893 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4894 it->string = it->overlay_strings[0];
4895 it->stop_charpos = 0;
4896 xassert (STRINGP (it->string));
4897 it->end_charpos = SCHARS (it->string);
4898 it->multibyte_p = STRING_MULTIBYTE (it->string);
4899 it->method = GET_FROM_STRING;
4900 return 1;
4903 it->current.overlay_string_index = -1;
4904 return 0;
4907 static int
4908 get_overlay_strings (it, charpos)
4909 struct it *it;
4910 int charpos;
4912 it->string = Qnil;
4913 it->method = GET_FROM_BUFFER;
4915 (void) get_overlay_strings_1 (it, charpos, 1);
4917 CHECK_IT (it);
4919 /* Value is non-zero if we found at least one overlay string. */
4920 return STRINGP (it->string);
4925 /***********************************************************************
4926 Saving and restoring state
4927 ***********************************************************************/
4929 /* Save current settings of IT on IT->stack. Called, for example,
4930 before setting up IT for an overlay string, to be able to restore
4931 IT's settings to what they were after the overlay string has been
4932 processed. */
4934 static void
4935 push_it (it)
4936 struct it *it;
4938 struct iterator_stack_entry *p;
4940 xassert (it->sp < IT_STACK_SIZE);
4941 p = it->stack + it->sp;
4943 p->stop_charpos = it->stop_charpos;
4944 xassert (it->face_id >= 0);
4945 p->face_id = it->face_id;
4946 p->string = it->string;
4947 p->method = it->method;
4948 switch (p->method)
4950 case GET_FROM_IMAGE:
4951 p->u.image.object = it->object;
4952 p->u.image.image_id = it->image_id;
4953 p->u.image.slice = it->slice;
4954 break;
4955 case GET_FROM_COMPOSITION:
4956 p->u.comp.object = it->object;
4957 p->u.comp.c = it->c;
4958 p->u.comp.len = it->len;
4959 p->u.comp.cmp_id = it->cmp_id;
4960 p->u.comp.cmp_len = it->cmp_len;
4961 break;
4962 case GET_FROM_STRETCH:
4963 p->u.stretch.object = it->object;
4964 break;
4966 p->position = it->position;
4967 p->current = it->current;
4968 p->end_charpos = it->end_charpos;
4969 p->string_nchars = it->string_nchars;
4970 p->area = it->area;
4971 p->multibyte_p = it->multibyte_p;
4972 p->space_width = it->space_width;
4973 p->font_height = it->font_height;
4974 p->voffset = it->voffset;
4975 p->string_from_display_prop_p = it->string_from_display_prop_p;
4976 p->display_ellipsis_p = 0;
4977 ++it->sp;
4981 /* Restore IT's settings from IT->stack. Called, for example, when no
4982 more overlay strings must be processed, and we return to delivering
4983 display elements from a buffer, or when the end of a string from a
4984 `display' property is reached and we return to delivering display
4985 elements from an overlay string, or from a buffer. */
4987 static void
4988 pop_it (it)
4989 struct it *it;
4991 struct iterator_stack_entry *p;
4993 xassert (it->sp > 0);
4994 --it->sp;
4995 p = it->stack + it->sp;
4996 it->stop_charpos = p->stop_charpos;
4997 it->face_id = p->face_id;
4998 it->current = p->current;
4999 it->position = p->position;
5000 it->string = p->string;
5001 if (NILP (it->string))
5002 SET_TEXT_POS (it->current.string_pos, -1, -1);
5003 it->method = p->method;
5004 switch (it->method)
5006 case GET_FROM_IMAGE:
5007 it->image_id = p->u.image.image_id;
5008 it->object = p->u.image.object;
5009 it->slice = p->u.image.slice;
5010 break;
5011 case GET_FROM_COMPOSITION:
5012 it->object = p->u.comp.object;
5013 it->c = p->u.comp.c;
5014 it->len = p->u.comp.len;
5015 it->cmp_id = p->u.comp.cmp_id;
5016 it->cmp_len = p->u.comp.cmp_len;
5017 break;
5018 case GET_FROM_STRETCH:
5019 it->object = p->u.comp.object;
5020 break;
5021 case GET_FROM_BUFFER:
5022 it->object = it->w->buffer;
5023 break;
5024 case GET_FROM_STRING:
5025 it->object = it->string;
5026 break;
5028 it->end_charpos = p->end_charpos;
5029 it->string_nchars = p->string_nchars;
5030 it->area = p->area;
5031 it->multibyte_p = p->multibyte_p;
5032 it->space_width = p->space_width;
5033 it->font_height = p->font_height;
5034 it->voffset = p->voffset;
5035 it->string_from_display_prop_p = p->string_from_display_prop_p;
5040 /***********************************************************************
5041 Moving over lines
5042 ***********************************************************************/
5044 /* Set IT's current position to the previous line start. */
5046 static void
5047 back_to_previous_line_start (it)
5048 struct it *it;
5050 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5051 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5055 /* Move IT to the next line start.
5057 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5058 we skipped over part of the text (as opposed to moving the iterator
5059 continuously over the text). Otherwise, don't change the value
5060 of *SKIPPED_P.
5062 Newlines may come from buffer text, overlay strings, or strings
5063 displayed via the `display' property. That's the reason we can't
5064 simply use find_next_newline_no_quit.
5066 Note that this function may not skip over invisible text that is so
5067 because of text properties and immediately follows a newline. If
5068 it would, function reseat_at_next_visible_line_start, when called
5069 from set_iterator_to_next, would effectively make invisible
5070 characters following a newline part of the wrong glyph row, which
5071 leads to wrong cursor motion. */
5073 static int
5074 forward_to_next_line_start (it, skipped_p)
5075 struct it *it;
5076 int *skipped_p;
5078 int old_selective, newline_found_p, n;
5079 const int MAX_NEWLINE_DISTANCE = 500;
5081 /* If already on a newline, just consume it to avoid unintended
5082 skipping over invisible text below. */
5083 if (it->what == IT_CHARACTER
5084 && it->c == '\n'
5085 && CHARPOS (it->position) == IT_CHARPOS (*it))
5087 set_iterator_to_next (it, 0);
5088 it->c = 0;
5089 return 1;
5092 /* Don't handle selective display in the following. It's (a)
5093 unnecessary because it's done by the caller, and (b) leads to an
5094 infinite recursion because next_element_from_ellipsis indirectly
5095 calls this function. */
5096 old_selective = it->selective;
5097 it->selective = 0;
5099 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5100 from buffer text. */
5101 for (n = newline_found_p = 0;
5102 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5103 n += STRINGP (it->string) ? 0 : 1)
5105 if (!get_next_display_element (it))
5106 return 0;
5107 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5108 set_iterator_to_next (it, 0);
5111 /* If we didn't find a newline near enough, see if we can use a
5112 short-cut. */
5113 if (!newline_found_p)
5115 int start = IT_CHARPOS (*it);
5116 int limit = find_next_newline_no_quit (start, 1);
5117 Lisp_Object pos;
5119 xassert (!STRINGP (it->string));
5121 /* If there isn't any `display' property in sight, and no
5122 overlays, we can just use the position of the newline in
5123 buffer text. */
5124 if (it->stop_charpos >= limit
5125 || ((pos = Fnext_single_property_change (make_number (start),
5126 Qdisplay,
5127 Qnil, make_number (limit)),
5128 NILP (pos))
5129 && next_overlay_change (start) == ZV))
5131 IT_CHARPOS (*it) = limit;
5132 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5133 *skipped_p = newline_found_p = 1;
5135 else
5137 while (get_next_display_element (it)
5138 && !newline_found_p)
5140 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5141 set_iterator_to_next (it, 0);
5146 it->selective = old_selective;
5147 return newline_found_p;
5151 /* Set IT's current position to the previous visible line start. Skip
5152 invisible text that is so either due to text properties or due to
5153 selective display. Caution: this does not change IT->current_x and
5154 IT->hpos. */
5156 static void
5157 back_to_previous_visible_line_start (it)
5158 struct it *it;
5160 while (IT_CHARPOS (*it) > BEGV)
5162 back_to_previous_line_start (it);
5164 if (IT_CHARPOS (*it) <= BEGV)
5165 break;
5167 /* If selective > 0, then lines indented more than that values
5168 are invisible. */
5169 if (it->selective > 0
5170 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5171 (double) it->selective)) /* iftc */
5172 continue;
5174 /* Check the newline before point for invisibility. */
5176 Lisp_Object prop;
5177 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5178 Qinvisible, it->window);
5179 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5180 continue;
5183 if (IT_CHARPOS (*it) <= BEGV)
5184 break;
5187 struct it it2;
5188 int pos;
5189 int beg, end;
5190 Lisp_Object val, overlay;
5192 /* If newline is part of a composition, continue from start of composition */
5193 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5194 && beg < IT_CHARPOS (*it))
5195 goto replaced;
5197 /* If newline is replaced by a display property, find start of overlay
5198 or interval and continue search from that point. */
5199 it2 = *it;
5200 pos = --IT_CHARPOS (it2);
5201 --IT_BYTEPOS (it2);
5202 it2.sp = 0;
5203 if (handle_display_prop (&it2) == HANDLED_RETURN
5204 && !NILP (val = get_char_property_and_overlay
5205 (make_number (pos), Qdisplay, Qnil, &overlay))
5206 && (OVERLAYP (overlay)
5207 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5208 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5209 goto replaced;
5211 /* Newline is not replaced by anything -- so we are done. */
5212 break;
5214 replaced:
5215 if (beg < BEGV)
5216 beg = BEGV;
5217 IT_CHARPOS (*it) = beg;
5218 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5222 it->continuation_lines_width = 0;
5224 xassert (IT_CHARPOS (*it) >= BEGV);
5225 xassert (IT_CHARPOS (*it) == BEGV
5226 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5227 CHECK_IT (it);
5231 /* Reseat iterator IT at the previous visible line start. Skip
5232 invisible text that is so either due to text properties or due to
5233 selective display. At the end, update IT's overlay information,
5234 face information etc. */
5236 void
5237 reseat_at_previous_visible_line_start (it)
5238 struct it *it;
5240 back_to_previous_visible_line_start (it);
5241 reseat (it, it->current.pos, 1);
5242 CHECK_IT (it);
5246 /* Reseat iterator IT on the next visible line start in the current
5247 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5248 preceding the line start. Skip over invisible text that is so
5249 because of selective display. Compute faces, overlays etc at the
5250 new position. Note that this function does not skip over text that
5251 is invisible because of text properties. */
5253 static void
5254 reseat_at_next_visible_line_start (it, on_newline_p)
5255 struct it *it;
5256 int on_newline_p;
5258 int newline_found_p, skipped_p = 0;
5260 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5262 /* Skip over lines that are invisible because they are indented
5263 more than the value of IT->selective. */
5264 if (it->selective > 0)
5265 while (IT_CHARPOS (*it) < ZV
5266 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5267 (double) it->selective)) /* iftc */
5269 xassert (IT_BYTEPOS (*it) == BEGV
5270 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5271 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5274 /* Position on the newline if that's what's requested. */
5275 if (on_newline_p && newline_found_p)
5277 if (STRINGP (it->string))
5279 if (IT_STRING_CHARPOS (*it) > 0)
5281 --IT_STRING_CHARPOS (*it);
5282 --IT_STRING_BYTEPOS (*it);
5285 else if (IT_CHARPOS (*it) > BEGV)
5287 --IT_CHARPOS (*it);
5288 --IT_BYTEPOS (*it);
5289 reseat (it, it->current.pos, 0);
5292 else if (skipped_p)
5293 reseat (it, it->current.pos, 0);
5295 CHECK_IT (it);
5300 /***********************************************************************
5301 Changing an iterator's position
5302 ***********************************************************************/
5304 /* Change IT's current position to POS in current_buffer. If FORCE_P
5305 is non-zero, always check for text properties at the new position.
5306 Otherwise, text properties are only looked up if POS >=
5307 IT->check_charpos of a property. */
5309 static void
5310 reseat (it, pos, force_p)
5311 struct it *it;
5312 struct text_pos pos;
5313 int force_p;
5315 int original_pos = IT_CHARPOS (*it);
5317 reseat_1 (it, pos, 0);
5319 /* Determine where to check text properties. Avoid doing it
5320 where possible because text property lookup is very expensive. */
5321 if (force_p
5322 || CHARPOS (pos) > it->stop_charpos
5323 || CHARPOS (pos) < original_pos)
5324 handle_stop (it);
5326 CHECK_IT (it);
5330 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5331 IT->stop_pos to POS, also. */
5333 static void
5334 reseat_1 (it, pos, set_stop_p)
5335 struct it *it;
5336 struct text_pos pos;
5337 int set_stop_p;
5339 /* Don't call this function when scanning a C string. */
5340 xassert (it->s == NULL);
5342 /* POS must be a reasonable value. */
5343 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5345 it->current.pos = it->position = pos;
5346 it->end_charpos = ZV;
5347 it->dpvec = NULL;
5348 it->current.dpvec_index = -1;
5349 it->current.overlay_string_index = -1;
5350 IT_STRING_CHARPOS (*it) = -1;
5351 IT_STRING_BYTEPOS (*it) = -1;
5352 it->string = Qnil;
5353 it->method = GET_FROM_BUFFER;
5354 it->object = it->w->buffer;
5355 it->area = TEXT_AREA;
5356 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5357 it->sp = 0;
5358 it->string_from_display_prop_p = 0;
5359 it->face_before_selective_p = 0;
5361 if (set_stop_p)
5362 it->stop_charpos = CHARPOS (pos);
5366 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5367 If S is non-null, it is a C string to iterate over. Otherwise,
5368 STRING gives a Lisp string to iterate over.
5370 If PRECISION > 0, don't return more then PRECISION number of
5371 characters from the string.
5373 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5374 characters have been returned. FIELD_WIDTH < 0 means an infinite
5375 field width.
5377 MULTIBYTE = 0 means disable processing of multibyte characters,
5378 MULTIBYTE > 0 means enable it,
5379 MULTIBYTE < 0 means use IT->multibyte_p.
5381 IT must be initialized via a prior call to init_iterator before
5382 calling this function. */
5384 static void
5385 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5386 struct it *it;
5387 unsigned char *s;
5388 Lisp_Object string;
5389 int charpos;
5390 int precision, field_width, multibyte;
5392 /* No region in strings. */
5393 it->region_beg_charpos = it->region_end_charpos = -1;
5395 /* No text property checks performed by default, but see below. */
5396 it->stop_charpos = -1;
5398 /* Set iterator position and end position. */
5399 bzero (&it->current, sizeof it->current);
5400 it->current.overlay_string_index = -1;
5401 it->current.dpvec_index = -1;
5402 xassert (charpos >= 0);
5404 /* If STRING is specified, use its multibyteness, otherwise use the
5405 setting of MULTIBYTE, if specified. */
5406 if (multibyte >= 0)
5407 it->multibyte_p = multibyte > 0;
5409 if (s == NULL)
5411 xassert (STRINGP (string));
5412 it->string = string;
5413 it->s = NULL;
5414 it->end_charpos = it->string_nchars = SCHARS (string);
5415 it->method = GET_FROM_STRING;
5416 it->current.string_pos = string_pos (charpos, string);
5418 else
5420 it->s = s;
5421 it->string = Qnil;
5423 /* Note that we use IT->current.pos, not it->current.string_pos,
5424 for displaying C strings. */
5425 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5426 if (it->multibyte_p)
5428 it->current.pos = c_string_pos (charpos, s, 1);
5429 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5431 else
5433 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5434 it->end_charpos = it->string_nchars = strlen (s);
5437 it->method = GET_FROM_C_STRING;
5440 /* PRECISION > 0 means don't return more than PRECISION characters
5441 from the string. */
5442 if (precision > 0 && it->end_charpos - charpos > precision)
5443 it->end_charpos = it->string_nchars = charpos + precision;
5445 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5446 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5447 FIELD_WIDTH < 0 means infinite field width. This is useful for
5448 padding with `-' at the end of a mode line. */
5449 if (field_width < 0)
5450 field_width = INFINITY;
5451 if (field_width > it->end_charpos - charpos)
5452 it->end_charpos = charpos + field_width;
5454 /* Use the standard display table for displaying strings. */
5455 if (DISP_TABLE_P (Vstandard_display_table))
5456 it->dp = XCHAR_TABLE (Vstandard_display_table);
5458 it->stop_charpos = charpos;
5459 CHECK_IT (it);
5464 /***********************************************************************
5465 Iteration
5466 ***********************************************************************/
5468 /* Map enum it_method value to corresponding next_element_from_* function. */
5470 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5472 next_element_from_buffer,
5473 next_element_from_display_vector,
5474 next_element_from_composition,
5475 next_element_from_string,
5476 next_element_from_c_string,
5477 next_element_from_image,
5478 next_element_from_stretch
5482 /* Load IT's display element fields with information about the next
5483 display element from the current position of IT. Value is zero if
5484 end of buffer (or C string) is reached. */
5486 static struct frame *last_escape_glyph_frame = NULL;
5487 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5488 static int last_escape_glyph_merged_face_id = 0;
5491 get_next_display_element (it)
5492 struct it *it;
5494 /* Non-zero means that we found a display element. Zero means that
5495 we hit the end of what we iterate over. Performance note: the
5496 function pointer `method' used here turns out to be faster than
5497 using a sequence of if-statements. */
5498 int success_p;
5500 get_next:
5501 success_p = (*get_next_element[it->method]) (it);
5503 if (it->what == IT_CHARACTER)
5505 /* Map via display table or translate control characters.
5506 IT->c, IT->len etc. have been set to the next character by
5507 the function call above. If we have a display table, and it
5508 contains an entry for IT->c, translate it. Don't do this if
5509 IT->c itself comes from a display table, otherwise we could
5510 end up in an infinite recursion. (An alternative could be to
5511 count the recursion depth of this function and signal an
5512 error when a certain maximum depth is reached.) Is it worth
5513 it? */
5514 if (success_p && it->dpvec == NULL)
5516 Lisp_Object dv;
5518 if (it->dp
5519 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5520 VECTORP (dv)))
5522 struct Lisp_Vector *v = XVECTOR (dv);
5524 /* Return the first character from the display table
5525 entry, if not empty. If empty, don't display the
5526 current character. */
5527 if (v->size)
5529 it->dpvec_char_len = it->len;
5530 it->dpvec = v->contents;
5531 it->dpend = v->contents + v->size;
5532 it->current.dpvec_index = 0;
5533 it->dpvec_face_id = -1;
5534 it->saved_face_id = it->face_id;
5535 it->method = GET_FROM_DISPLAY_VECTOR;
5536 it->ellipsis_p = 0;
5538 else
5540 set_iterator_to_next (it, 0);
5542 goto get_next;
5545 /* Translate control characters into `\003' or `^C' form.
5546 Control characters coming from a display table entry are
5547 currently not translated because we use IT->dpvec to hold
5548 the translation. This could easily be changed but I
5549 don't believe that it is worth doing.
5551 If it->multibyte_p is nonzero, eight-bit characters and
5552 non-printable multibyte characters are also translated to
5553 octal form.
5555 If it->multibyte_p is zero, eight-bit characters that
5556 don't have corresponding multibyte char code are also
5557 translated to octal form. */
5558 else if ((it->c < ' '
5559 && (it->area != TEXT_AREA
5560 /* In mode line, treat \n like other crl chars. */
5561 || (it->c != '\t'
5562 && it->glyph_row && it->glyph_row->mode_line_p)
5563 || (it->c != '\n' && it->c != '\t')))
5564 || (it->multibyte_p
5565 ? ((it->c >= 127
5566 && it->len == 1)
5567 || !CHAR_PRINTABLE_P (it->c)
5568 || (!NILP (Vnobreak_char_display)
5569 && (it->c == 0x8a0 || it->c == 0x8ad
5570 || it->c == 0x920 || it->c == 0x92d
5571 || it->c == 0xe20 || it->c == 0xe2d
5572 || it->c == 0xf20 || it->c == 0xf2d)))
5573 : (it->c >= 127
5574 && (!unibyte_display_via_language_environment
5575 || it->c == unibyte_char_to_multibyte (it->c)))))
5577 /* IT->c is a control character which must be displayed
5578 either as '\003' or as `^C' where the '\\' and '^'
5579 can be defined in the display table. Fill
5580 IT->ctl_chars with glyphs for what we have to
5581 display. Then, set IT->dpvec to these glyphs. */
5582 GLYPH g;
5583 int ctl_len;
5584 int face_id, lface_id = 0 ;
5585 GLYPH escape_glyph;
5587 /* Handle control characters with ^. */
5589 if (it->c < 128 && it->ctl_arrow_p)
5591 g = '^'; /* default glyph for Control */
5592 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5593 if (it->dp
5594 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5595 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5597 g = XINT (DISP_CTRL_GLYPH (it->dp));
5598 lface_id = FAST_GLYPH_FACE (g);
5600 if (lface_id)
5602 g = FAST_GLYPH_CHAR (g);
5603 face_id = merge_faces (it->f, Qt, lface_id,
5604 it->face_id);
5606 else if (it->f == last_escape_glyph_frame
5607 && it->face_id == last_escape_glyph_face_id)
5609 face_id = last_escape_glyph_merged_face_id;
5611 else
5613 /* Merge the escape-glyph face into the current face. */
5614 face_id = merge_faces (it->f, Qescape_glyph, 0,
5615 it->face_id);
5616 last_escape_glyph_frame = it->f;
5617 last_escape_glyph_face_id = it->face_id;
5618 last_escape_glyph_merged_face_id = face_id;
5621 XSETINT (it->ctl_chars[0], g);
5622 g = it->c ^ 0100;
5623 XSETINT (it->ctl_chars[1], g);
5624 ctl_len = 2;
5625 goto display_control;
5628 /* Handle non-break space in the mode where it only gets
5629 highlighting. */
5631 if (EQ (Vnobreak_char_display, Qt)
5632 && (it->c == 0x8a0 || it->c == 0x920
5633 || it->c == 0xe20 || it->c == 0xf20))
5635 /* Merge the no-break-space face into the current face. */
5636 face_id = merge_faces (it->f, Qnobreak_space, 0,
5637 it->face_id);
5639 g = it->c = ' ';
5640 XSETINT (it->ctl_chars[0], g);
5641 ctl_len = 1;
5642 goto display_control;
5645 /* Handle sequences that start with the "escape glyph". */
5647 /* the default escape glyph is \. */
5648 escape_glyph = '\\';
5650 if (it->dp
5651 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5652 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5654 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5655 lface_id = FAST_GLYPH_FACE (escape_glyph);
5657 if (lface_id)
5659 /* The display table specified a face.
5660 Merge it into face_id and also into escape_glyph. */
5661 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5662 face_id = merge_faces (it->f, Qt, lface_id,
5663 it->face_id);
5665 else if (it->f == last_escape_glyph_frame
5666 && it->face_id == last_escape_glyph_face_id)
5668 face_id = last_escape_glyph_merged_face_id;
5670 else
5672 /* Merge the escape-glyph face into the current face. */
5673 face_id = merge_faces (it->f, Qescape_glyph, 0,
5674 it->face_id);
5675 last_escape_glyph_frame = it->f;
5676 last_escape_glyph_face_id = it->face_id;
5677 last_escape_glyph_merged_face_id = face_id;
5680 /* Handle soft hyphens in the mode where they only get
5681 highlighting. */
5683 if (EQ (Vnobreak_char_display, Qt)
5684 && (it->c == 0x8ad || it->c == 0x92d
5685 || it->c == 0xe2d || it->c == 0xf2d))
5687 g = it->c = '-';
5688 XSETINT (it->ctl_chars[0], g);
5689 ctl_len = 1;
5690 goto display_control;
5693 /* Handle non-break space and soft hyphen
5694 with the escape glyph. */
5696 if (it->c == 0x8a0 || it->c == 0x8ad
5697 || it->c == 0x920 || it->c == 0x92d
5698 || it->c == 0xe20 || it->c == 0xe2d
5699 || it->c == 0xf20 || it->c == 0xf2d)
5701 XSETINT (it->ctl_chars[0], escape_glyph);
5702 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5703 XSETINT (it->ctl_chars[1], g);
5704 ctl_len = 2;
5705 goto display_control;
5709 unsigned char str[MAX_MULTIBYTE_LENGTH];
5710 int len;
5711 int i;
5713 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5714 if (SINGLE_BYTE_CHAR_P (it->c))
5715 str[0] = it->c, len = 1;
5716 else
5718 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5719 if (len < 0)
5721 /* It's an invalid character, which shouldn't
5722 happen actually, but due to bugs it may
5723 happen. Let's print the char as is, there's
5724 not much meaningful we can do with it. */
5725 str[0] = it->c;
5726 str[1] = it->c >> 8;
5727 str[2] = it->c >> 16;
5728 str[3] = it->c >> 24;
5729 len = 4;
5733 for (i = 0; i < len; i++)
5735 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5736 /* Insert three more glyphs into IT->ctl_chars for
5737 the octal display of the character. */
5738 g = ((str[i] >> 6) & 7) + '0';
5739 XSETINT (it->ctl_chars[i * 4 + 1], g);
5740 g = ((str[i] >> 3) & 7) + '0';
5741 XSETINT (it->ctl_chars[i * 4 + 2], g);
5742 g = (str[i] & 7) + '0';
5743 XSETINT (it->ctl_chars[i * 4 + 3], g);
5745 ctl_len = len * 4;
5748 display_control:
5749 /* Set up IT->dpvec and return first character from it. */
5750 it->dpvec_char_len = it->len;
5751 it->dpvec = it->ctl_chars;
5752 it->dpend = it->dpvec + ctl_len;
5753 it->current.dpvec_index = 0;
5754 it->dpvec_face_id = face_id;
5755 it->saved_face_id = it->face_id;
5756 it->method = GET_FROM_DISPLAY_VECTOR;
5757 it->ellipsis_p = 0;
5758 goto get_next;
5762 /* Adjust face id for a multibyte character. There are no
5763 multibyte character in unibyte text. */
5764 if (it->multibyte_p
5765 && success_p
5766 && FRAME_WINDOW_P (it->f))
5768 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5769 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5773 /* Is this character the last one of a run of characters with
5774 box? If yes, set IT->end_of_box_run_p to 1. */
5775 if (it->face_box_p
5776 && it->s == NULL)
5778 int face_id;
5779 struct face *face;
5781 it->end_of_box_run_p
5782 = ((face_id = face_after_it_pos (it),
5783 face_id != it->face_id)
5784 && (face = FACE_FROM_ID (it->f, face_id),
5785 face->box == FACE_NO_BOX));
5788 /* Value is 0 if end of buffer or string reached. */
5789 return success_p;
5793 /* Move IT to the next display element.
5795 RESEAT_P non-zero means if called on a newline in buffer text,
5796 skip to the next visible line start.
5798 Functions get_next_display_element and set_iterator_to_next are
5799 separate because I find this arrangement easier to handle than a
5800 get_next_display_element function that also increments IT's
5801 position. The way it is we can first look at an iterator's current
5802 display element, decide whether it fits on a line, and if it does,
5803 increment the iterator position. The other way around we probably
5804 would either need a flag indicating whether the iterator has to be
5805 incremented the next time, or we would have to implement a
5806 decrement position function which would not be easy to write. */
5808 void
5809 set_iterator_to_next (it, reseat_p)
5810 struct it *it;
5811 int reseat_p;
5813 /* Reset flags indicating start and end of a sequence of characters
5814 with box. Reset them at the start of this function because
5815 moving the iterator to a new position might set them. */
5816 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5818 switch (it->method)
5820 case GET_FROM_BUFFER:
5821 /* The current display element of IT is a character from
5822 current_buffer. Advance in the buffer, and maybe skip over
5823 invisible lines that are so because of selective display. */
5824 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5825 reseat_at_next_visible_line_start (it, 0);
5826 else
5828 xassert (it->len != 0);
5829 IT_BYTEPOS (*it) += it->len;
5830 IT_CHARPOS (*it) += 1;
5831 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5833 break;
5835 case GET_FROM_COMPOSITION:
5836 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5837 xassert (it->sp > 0);
5838 pop_it (it);
5839 if (it->method == GET_FROM_STRING)
5841 IT_STRING_BYTEPOS (*it) += it->len;
5842 IT_STRING_CHARPOS (*it) += it->cmp_len;
5843 goto consider_string_end;
5845 else if (it->method == GET_FROM_BUFFER)
5847 IT_BYTEPOS (*it) += it->len;
5848 IT_CHARPOS (*it) += it->cmp_len;
5850 break;
5852 case GET_FROM_C_STRING:
5853 /* Current display element of IT is from a C string. */
5854 IT_BYTEPOS (*it) += it->len;
5855 IT_CHARPOS (*it) += 1;
5856 break;
5858 case GET_FROM_DISPLAY_VECTOR:
5859 /* Current display element of IT is from a display table entry.
5860 Advance in the display table definition. Reset it to null if
5861 end reached, and continue with characters from buffers/
5862 strings. */
5863 ++it->current.dpvec_index;
5865 /* Restore face of the iterator to what they were before the
5866 display vector entry (these entries may contain faces). */
5867 it->face_id = it->saved_face_id;
5869 if (it->dpvec + it->current.dpvec_index == it->dpend)
5871 int recheck_faces = it->ellipsis_p;
5873 if (it->s)
5874 it->method = GET_FROM_C_STRING;
5875 else if (STRINGP (it->string))
5876 it->method = GET_FROM_STRING;
5877 else
5879 it->method = GET_FROM_BUFFER;
5880 it->object = it->w->buffer;
5883 it->dpvec = NULL;
5884 it->current.dpvec_index = -1;
5886 /* Skip over characters which were displayed via IT->dpvec. */
5887 if (it->dpvec_char_len < 0)
5888 reseat_at_next_visible_line_start (it, 1);
5889 else if (it->dpvec_char_len > 0)
5891 if (it->method == GET_FROM_STRING
5892 && it->n_overlay_strings > 0)
5893 it->ignore_overlay_strings_at_pos_p = 1;
5894 it->len = it->dpvec_char_len;
5895 set_iterator_to_next (it, reseat_p);
5898 /* Maybe recheck faces after display vector */
5899 if (recheck_faces)
5900 it->stop_charpos = IT_CHARPOS (*it);
5902 break;
5904 case GET_FROM_STRING:
5905 /* Current display element is a character from a Lisp string. */
5906 xassert (it->s == NULL && STRINGP (it->string));
5907 IT_STRING_BYTEPOS (*it) += it->len;
5908 IT_STRING_CHARPOS (*it) += 1;
5910 consider_string_end:
5912 if (it->current.overlay_string_index >= 0)
5914 /* IT->string is an overlay string. Advance to the
5915 next, if there is one. */
5916 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5917 next_overlay_string (it);
5919 else
5921 /* IT->string is not an overlay string. If we reached
5922 its end, and there is something on IT->stack, proceed
5923 with what is on the stack. This can be either another
5924 string, this time an overlay string, or a buffer. */
5925 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5926 && it->sp > 0)
5928 pop_it (it);
5929 if (it->method == GET_FROM_STRING)
5930 goto consider_string_end;
5933 break;
5935 case GET_FROM_IMAGE:
5936 case GET_FROM_STRETCH:
5937 /* The position etc with which we have to proceed are on
5938 the stack. The position may be at the end of a string,
5939 if the `display' property takes up the whole string. */
5940 xassert (it->sp > 0);
5941 pop_it (it);
5942 if (it->method == GET_FROM_STRING)
5943 goto consider_string_end;
5944 break;
5946 default:
5947 /* There are no other methods defined, so this should be a bug. */
5948 abort ();
5951 xassert (it->method != GET_FROM_STRING
5952 || (STRINGP (it->string)
5953 && IT_STRING_CHARPOS (*it) >= 0));
5956 /* Load IT's display element fields with information about the next
5957 display element which comes from a display table entry or from the
5958 result of translating a control character to one of the forms `^C'
5959 or `\003'.
5961 IT->dpvec holds the glyphs to return as characters.
5962 IT->saved_face_id holds the face id before the display vector--
5963 it is restored into IT->face_idin set_iterator_to_next. */
5965 static int
5966 next_element_from_display_vector (it)
5967 struct it *it;
5969 /* Precondition. */
5970 xassert (it->dpvec && it->current.dpvec_index >= 0);
5972 it->face_id = it->saved_face_id;
5974 if (INTEGERP (*it->dpvec)
5975 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5977 GLYPH g;
5979 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5980 it->c = FAST_GLYPH_CHAR (g);
5981 it->len = CHAR_BYTES (it->c);
5983 /* The entry may contain a face id to use. Such a face id is
5984 the id of a Lisp face, not a realized face. A face id of
5985 zero means no face is specified. */
5986 if (it->dpvec_face_id >= 0)
5987 it->face_id = it->dpvec_face_id;
5988 else
5990 int lface_id = FAST_GLYPH_FACE (g);
5991 if (lface_id > 0)
5992 it->face_id = merge_faces (it->f, Qt, lface_id,
5993 it->saved_face_id);
5996 else
5997 /* Display table entry is invalid. Return a space. */
5998 it->c = ' ', it->len = 1;
6000 /* Don't change position and object of the iterator here. They are
6001 still the values of the character that had this display table
6002 entry or was translated, and that's what we want. */
6003 it->what = IT_CHARACTER;
6004 return 1;
6008 /* Load IT with the next display element from Lisp string IT->string.
6009 IT->current.string_pos is the current position within the string.
6010 If IT->current.overlay_string_index >= 0, the Lisp string is an
6011 overlay string. */
6013 static int
6014 next_element_from_string (it)
6015 struct it *it;
6017 struct text_pos position;
6019 xassert (STRINGP (it->string));
6020 xassert (IT_STRING_CHARPOS (*it) >= 0);
6021 position = it->current.string_pos;
6023 /* Time to check for invisible text? */
6024 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6025 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6027 handle_stop (it);
6029 /* Since a handler may have changed IT->method, we must
6030 recurse here. */
6031 return get_next_display_element (it);
6034 if (it->current.overlay_string_index >= 0)
6036 /* Get the next character from an overlay string. In overlay
6037 strings, There is no field width or padding with spaces to
6038 do. */
6039 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6041 it->what = IT_EOB;
6042 return 0;
6044 else if (STRING_MULTIBYTE (it->string))
6046 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6047 const unsigned char *s = (SDATA (it->string)
6048 + IT_STRING_BYTEPOS (*it));
6049 it->c = string_char_and_length (s, remaining, &it->len);
6051 else
6053 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6054 it->len = 1;
6057 else
6059 /* Get the next character from a Lisp string that is not an
6060 overlay string. Such strings come from the mode line, for
6061 example. We may have to pad with spaces, or truncate the
6062 string. See also next_element_from_c_string. */
6063 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6065 it->what = IT_EOB;
6066 return 0;
6068 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6070 /* Pad with spaces. */
6071 it->c = ' ', it->len = 1;
6072 CHARPOS (position) = BYTEPOS (position) = -1;
6074 else if (STRING_MULTIBYTE (it->string))
6076 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6077 const unsigned char *s = (SDATA (it->string)
6078 + IT_STRING_BYTEPOS (*it));
6079 it->c = string_char_and_length (s, maxlen, &it->len);
6081 else
6083 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6084 it->len = 1;
6088 /* Record what we have and where it came from. */
6089 it->what = IT_CHARACTER;
6090 it->object = it->string;
6091 it->position = position;
6092 return 1;
6096 /* Load IT with next display element from C string IT->s.
6097 IT->string_nchars is the maximum number of characters to return
6098 from the string. IT->end_charpos may be greater than
6099 IT->string_nchars when this function is called, in which case we
6100 may have to return padding spaces. Value is zero if end of string
6101 reached, including padding spaces. */
6103 static int
6104 next_element_from_c_string (it)
6105 struct it *it;
6107 int success_p = 1;
6109 xassert (it->s);
6110 it->what = IT_CHARACTER;
6111 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6112 it->object = Qnil;
6114 /* IT's position can be greater IT->string_nchars in case a field
6115 width or precision has been specified when the iterator was
6116 initialized. */
6117 if (IT_CHARPOS (*it) >= it->end_charpos)
6119 /* End of the game. */
6120 it->what = IT_EOB;
6121 success_p = 0;
6123 else if (IT_CHARPOS (*it) >= it->string_nchars)
6125 /* Pad with spaces. */
6126 it->c = ' ', it->len = 1;
6127 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6129 else if (it->multibyte_p)
6131 /* Implementation note: The calls to strlen apparently aren't a
6132 performance problem because there is no noticeable performance
6133 difference between Emacs running in unibyte or multibyte mode. */
6134 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6135 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6136 maxlen, &it->len);
6138 else
6139 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6141 return success_p;
6145 /* Set up IT to return characters from an ellipsis, if appropriate.
6146 The definition of the ellipsis glyphs may come from a display table
6147 entry. This function Fills IT with the first glyph from the
6148 ellipsis if an ellipsis is to be displayed. */
6150 static int
6151 next_element_from_ellipsis (it)
6152 struct it *it;
6154 if (it->selective_display_ellipsis_p)
6155 setup_for_ellipsis (it, it->len);
6156 else
6158 /* The face at the current position may be different from the
6159 face we find after the invisible text. Remember what it
6160 was in IT->saved_face_id, and signal that it's there by
6161 setting face_before_selective_p. */
6162 it->saved_face_id = it->face_id;
6163 it->method = GET_FROM_BUFFER;
6164 it->object = it->w->buffer;
6165 reseat_at_next_visible_line_start (it, 1);
6166 it->face_before_selective_p = 1;
6169 return get_next_display_element (it);
6173 /* Deliver an image display element. The iterator IT is already
6174 filled with image information (done in handle_display_prop). Value
6175 is always 1. */
6178 static int
6179 next_element_from_image (it)
6180 struct it *it;
6182 it->what = IT_IMAGE;
6183 return 1;
6187 /* Fill iterator IT with next display element from a stretch glyph
6188 property. IT->object is the value of the text property. Value is
6189 always 1. */
6191 static int
6192 next_element_from_stretch (it)
6193 struct it *it;
6195 it->what = IT_STRETCH;
6196 return 1;
6200 /* Load IT with the next display element from current_buffer. Value
6201 is zero if end of buffer reached. IT->stop_charpos is the next
6202 position at which to stop and check for text properties or buffer
6203 end. */
6205 static int
6206 next_element_from_buffer (it)
6207 struct it *it;
6209 int success_p = 1;
6211 /* Check this assumption, otherwise, we would never enter the
6212 if-statement, below. */
6213 xassert (IT_CHARPOS (*it) >= BEGV
6214 && IT_CHARPOS (*it) <= it->stop_charpos);
6216 if (IT_CHARPOS (*it) >= it->stop_charpos)
6218 if (IT_CHARPOS (*it) >= it->end_charpos)
6220 int overlay_strings_follow_p;
6222 /* End of the game, except when overlay strings follow that
6223 haven't been returned yet. */
6224 if (it->overlay_strings_at_end_processed_p)
6225 overlay_strings_follow_p = 0;
6226 else
6228 it->overlay_strings_at_end_processed_p = 1;
6229 overlay_strings_follow_p = get_overlay_strings (it, 0);
6232 if (overlay_strings_follow_p)
6233 success_p = get_next_display_element (it);
6234 else
6236 it->what = IT_EOB;
6237 it->position = it->current.pos;
6238 success_p = 0;
6241 else
6243 handle_stop (it);
6244 return get_next_display_element (it);
6247 else
6249 /* No face changes, overlays etc. in sight, so just return a
6250 character from current_buffer. */
6251 unsigned char *p;
6253 /* Maybe run the redisplay end trigger hook. Performance note:
6254 This doesn't seem to cost measurable time. */
6255 if (it->redisplay_end_trigger_charpos
6256 && it->glyph_row
6257 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6258 run_redisplay_end_trigger_hook (it);
6260 /* Get the next character, maybe multibyte. */
6261 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6262 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6264 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6265 - IT_BYTEPOS (*it));
6266 it->c = string_char_and_length (p, maxlen, &it->len);
6268 else
6269 it->c = *p, it->len = 1;
6271 /* Record what we have and where it came from. */
6272 it->what = IT_CHARACTER;
6273 it->object = it->w->buffer;
6274 it->position = it->current.pos;
6276 /* Normally we return the character found above, except when we
6277 really want to return an ellipsis for selective display. */
6278 if (it->selective)
6280 if (it->c == '\n')
6282 /* A value of selective > 0 means hide lines indented more
6283 than that number of columns. */
6284 if (it->selective > 0
6285 && IT_CHARPOS (*it) + 1 < ZV
6286 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6287 IT_BYTEPOS (*it) + 1,
6288 (double) it->selective)) /* iftc */
6290 success_p = next_element_from_ellipsis (it);
6291 it->dpvec_char_len = -1;
6294 else if (it->c == '\r' && it->selective == -1)
6296 /* A value of selective == -1 means that everything from the
6297 CR to the end of the line is invisible, with maybe an
6298 ellipsis displayed for it. */
6299 success_p = next_element_from_ellipsis (it);
6300 it->dpvec_char_len = -1;
6305 /* Value is zero if end of buffer reached. */
6306 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6307 return success_p;
6311 /* Run the redisplay end trigger hook for IT. */
6313 static void
6314 run_redisplay_end_trigger_hook (it)
6315 struct it *it;
6317 Lisp_Object args[3];
6319 /* IT->glyph_row should be non-null, i.e. we should be actually
6320 displaying something, or otherwise we should not run the hook. */
6321 xassert (it->glyph_row);
6323 /* Set up hook arguments. */
6324 args[0] = Qredisplay_end_trigger_functions;
6325 args[1] = it->window;
6326 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6327 it->redisplay_end_trigger_charpos = 0;
6329 /* Since we are *trying* to run these functions, don't try to run
6330 them again, even if they get an error. */
6331 it->w->redisplay_end_trigger = Qnil;
6332 Frun_hook_with_args (3, args);
6334 /* Notice if it changed the face of the character we are on. */
6335 handle_face_prop (it);
6339 /* Deliver a composition display element. The iterator IT is already
6340 filled with composition information (done in
6341 handle_composition_prop). Value is always 1. */
6343 static int
6344 next_element_from_composition (it)
6345 struct it *it;
6347 it->what = IT_COMPOSITION;
6348 it->position = (STRINGP (it->string)
6349 ? it->current.string_pos
6350 : it->current.pos);
6351 if (STRINGP (it->string))
6352 it->object = it->string;
6353 else
6354 it->object = it->w->buffer;
6355 return 1;
6360 /***********************************************************************
6361 Moving an iterator without producing glyphs
6362 ***********************************************************************/
6364 /* Check if iterator is at a position corresponding to a valid buffer
6365 position after some move_it_ call. */
6367 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6368 ((it)->method == GET_FROM_STRING \
6369 ? IT_STRING_CHARPOS (*it) == 0 \
6370 : 1)
6373 /* Move iterator IT to a specified buffer or X position within one
6374 line on the display without producing glyphs.
6376 OP should be a bit mask including some or all of these bits:
6377 MOVE_TO_X: Stop on reaching x-position TO_X.
6378 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6379 Regardless of OP's value, stop in reaching the end of the display line.
6381 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6382 This means, in particular, that TO_X includes window's horizontal
6383 scroll amount.
6385 The return value has several possible values that
6386 say what condition caused the scan to stop:
6388 MOVE_POS_MATCH_OR_ZV
6389 - when TO_POS or ZV was reached.
6391 MOVE_X_REACHED
6392 -when TO_X was reached before TO_POS or ZV were reached.
6394 MOVE_LINE_CONTINUED
6395 - when we reached the end of the display area and the line must
6396 be continued.
6398 MOVE_LINE_TRUNCATED
6399 - when we reached the end of the display area and the line is
6400 truncated.
6402 MOVE_NEWLINE_OR_CR
6403 - when we stopped at a line end, i.e. a newline or a CR and selective
6404 display is on. */
6406 static enum move_it_result
6407 move_it_in_display_line_to (it, to_charpos, to_x, op)
6408 struct it *it;
6409 int to_charpos, to_x, op;
6411 enum move_it_result result = MOVE_UNDEFINED;
6412 struct glyph_row *saved_glyph_row;
6414 /* Don't produce glyphs in produce_glyphs. */
6415 saved_glyph_row = it->glyph_row;
6416 it->glyph_row = NULL;
6418 #define BUFFER_POS_REACHED_P() \
6419 ((op & MOVE_TO_POS) != 0 \
6420 && BUFFERP (it->object) \
6421 && IT_CHARPOS (*it) >= to_charpos \
6422 && (it->method == GET_FROM_BUFFER \
6423 || (it->method == GET_FROM_DISPLAY_VECTOR \
6424 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6427 while (1)
6429 int x, i, ascent = 0, descent = 0;
6431 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6432 if ((op & MOVE_TO_POS) != 0
6433 && BUFFERP (it->object)
6434 && it->method == GET_FROM_BUFFER
6435 && IT_CHARPOS (*it) > to_charpos)
6437 result = MOVE_POS_MATCH_OR_ZV;
6438 break;
6441 /* Stop when ZV reached.
6442 We used to stop here when TO_CHARPOS reached as well, but that is
6443 too soon if this glyph does not fit on this line. So we handle it
6444 explicitly below. */
6445 if (!get_next_display_element (it)
6446 || (it->truncate_lines_p
6447 && BUFFER_POS_REACHED_P ()))
6449 result = MOVE_POS_MATCH_OR_ZV;
6450 break;
6453 /* The call to produce_glyphs will get the metrics of the
6454 display element IT is loaded with. We record in x the
6455 x-position before this display element in case it does not
6456 fit on the line. */
6457 x = it->current_x;
6459 /* Remember the line height so far in case the next element doesn't
6460 fit on the line. */
6461 if (!it->truncate_lines_p)
6463 ascent = it->max_ascent;
6464 descent = it->max_descent;
6467 PRODUCE_GLYPHS (it);
6469 if (it->area != TEXT_AREA)
6471 set_iterator_to_next (it, 1);
6472 continue;
6475 /* The number of glyphs we get back in IT->nglyphs will normally
6476 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6477 character on a terminal frame, or (iii) a line end. For the
6478 second case, IT->nglyphs - 1 padding glyphs will be present
6479 (on X frames, there is only one glyph produced for a
6480 composite character.
6482 The behavior implemented below means, for continuation lines,
6483 that as many spaces of a TAB as fit on the current line are
6484 displayed there. For terminal frames, as many glyphs of a
6485 multi-glyph character are displayed in the current line, too.
6486 This is what the old redisplay code did, and we keep it that
6487 way. Under X, the whole shape of a complex character must
6488 fit on the line or it will be completely displayed in the
6489 next line.
6491 Note that both for tabs and padding glyphs, all glyphs have
6492 the same width. */
6493 if (it->nglyphs)
6495 /* More than one glyph or glyph doesn't fit on line. All
6496 glyphs have the same width. */
6497 int single_glyph_width = it->pixel_width / it->nglyphs;
6498 int new_x;
6499 int x_before_this_char = x;
6500 int hpos_before_this_char = it->hpos;
6502 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6504 new_x = x + single_glyph_width;
6506 /* We want to leave anything reaching TO_X to the caller. */
6507 if ((op & MOVE_TO_X) && new_x > to_x)
6509 if (BUFFER_POS_REACHED_P ())
6510 goto buffer_pos_reached;
6511 it->current_x = x;
6512 result = MOVE_X_REACHED;
6513 break;
6515 else if (/* Lines are continued. */
6516 !it->truncate_lines_p
6517 && (/* And glyph doesn't fit on the line. */
6518 new_x > it->last_visible_x
6519 /* Or it fits exactly and we're on a window
6520 system frame. */
6521 || (new_x == it->last_visible_x
6522 && FRAME_WINDOW_P (it->f))))
6524 if (/* IT->hpos == 0 means the very first glyph
6525 doesn't fit on the line, e.g. a wide image. */
6526 it->hpos == 0
6527 || (new_x == it->last_visible_x
6528 && FRAME_WINDOW_P (it->f)))
6530 ++it->hpos;
6531 it->current_x = new_x;
6533 /* The character's last glyph just barely fits
6534 in this row. */
6535 if (i == it->nglyphs - 1)
6537 /* If this is the destination position,
6538 return a position *before* it in this row,
6539 now that we know it fits in this row. */
6540 if (BUFFER_POS_REACHED_P ())
6542 it->hpos = hpos_before_this_char;
6543 it->current_x = x_before_this_char;
6544 result = MOVE_POS_MATCH_OR_ZV;
6545 break;
6548 set_iterator_to_next (it, 1);
6549 #ifdef HAVE_WINDOW_SYSTEM
6550 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6552 if (!get_next_display_element (it))
6554 result = MOVE_POS_MATCH_OR_ZV;
6555 break;
6557 if (BUFFER_POS_REACHED_P ())
6559 if (ITERATOR_AT_END_OF_LINE_P (it))
6560 result = MOVE_POS_MATCH_OR_ZV;
6561 else
6562 result = MOVE_LINE_CONTINUED;
6563 break;
6565 if (ITERATOR_AT_END_OF_LINE_P (it))
6567 result = MOVE_NEWLINE_OR_CR;
6568 break;
6571 #endif /* HAVE_WINDOW_SYSTEM */
6574 else
6576 it->current_x = x;
6577 it->max_ascent = ascent;
6578 it->max_descent = descent;
6581 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6582 IT_CHARPOS (*it)));
6583 result = MOVE_LINE_CONTINUED;
6584 break;
6586 else if (BUFFER_POS_REACHED_P ())
6587 goto buffer_pos_reached;
6588 else if (new_x > it->first_visible_x)
6590 /* Glyph is visible. Increment number of glyphs that
6591 would be displayed. */
6592 ++it->hpos;
6594 else
6596 /* Glyph is completely off the left margin of the display
6597 area. Nothing to do. */
6601 if (result != MOVE_UNDEFINED)
6602 break;
6604 else if (BUFFER_POS_REACHED_P ())
6606 buffer_pos_reached:
6607 it->current_x = x;
6608 it->max_ascent = ascent;
6609 it->max_descent = descent;
6610 result = MOVE_POS_MATCH_OR_ZV;
6611 break;
6613 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6615 /* Stop when TO_X specified and reached. This check is
6616 necessary here because of lines consisting of a line end,
6617 only. The line end will not produce any glyphs and we
6618 would never get MOVE_X_REACHED. */
6619 xassert (it->nglyphs == 0);
6620 result = MOVE_X_REACHED;
6621 break;
6624 /* Is this a line end? If yes, we're done. */
6625 if (ITERATOR_AT_END_OF_LINE_P (it))
6627 result = MOVE_NEWLINE_OR_CR;
6628 break;
6631 /* The current display element has been consumed. Advance
6632 to the next. */
6633 set_iterator_to_next (it, 1);
6635 /* Stop if lines are truncated and IT's current x-position is
6636 past the right edge of the window now. */
6637 if (it->truncate_lines_p
6638 && it->current_x >= it->last_visible_x)
6640 #ifdef HAVE_WINDOW_SYSTEM
6641 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6643 if (!get_next_display_element (it)
6644 || BUFFER_POS_REACHED_P ())
6646 result = MOVE_POS_MATCH_OR_ZV;
6647 break;
6649 if (ITERATOR_AT_END_OF_LINE_P (it))
6651 result = MOVE_NEWLINE_OR_CR;
6652 break;
6655 #endif /* HAVE_WINDOW_SYSTEM */
6656 result = MOVE_LINE_TRUNCATED;
6657 break;
6661 #undef BUFFER_POS_REACHED_P
6663 /* Restore the iterator settings altered at the beginning of this
6664 function. */
6665 it->glyph_row = saved_glyph_row;
6666 return result;
6670 /* Move IT forward until it satisfies one or more of the criteria in
6671 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6673 OP is a bit-mask that specifies where to stop, and in particular,
6674 which of those four position arguments makes a difference. See the
6675 description of enum move_operation_enum.
6677 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6678 screen line, this function will set IT to the next position >
6679 TO_CHARPOS. */
6681 void
6682 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6683 struct it *it;
6684 int to_charpos, to_x, to_y, to_vpos;
6685 int op;
6687 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6688 int line_height;
6689 int reached = 0;
6691 for (;;)
6693 if (op & MOVE_TO_VPOS)
6695 /* If no TO_CHARPOS and no TO_X specified, stop at the
6696 start of the line TO_VPOS. */
6697 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6699 if (it->vpos == to_vpos)
6701 reached = 1;
6702 break;
6704 else
6705 skip = move_it_in_display_line_to (it, -1, -1, 0);
6707 else
6709 /* TO_VPOS >= 0 means stop at TO_X in the line at
6710 TO_VPOS, or at TO_POS, whichever comes first. */
6711 if (it->vpos == to_vpos)
6713 reached = 2;
6714 break;
6717 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6719 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6721 reached = 3;
6722 break;
6724 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6726 /* We have reached TO_X but not in the line we want. */
6727 skip = move_it_in_display_line_to (it, to_charpos,
6728 -1, MOVE_TO_POS);
6729 if (skip == MOVE_POS_MATCH_OR_ZV)
6731 reached = 4;
6732 break;
6737 else if (op & MOVE_TO_Y)
6739 struct it it_backup;
6741 /* TO_Y specified means stop at TO_X in the line containing
6742 TO_Y---or at TO_CHARPOS if this is reached first. The
6743 problem is that we can't really tell whether the line
6744 contains TO_Y before we have completely scanned it, and
6745 this may skip past TO_X. What we do is to first scan to
6746 TO_X.
6748 If TO_X is not specified, use a TO_X of zero. The reason
6749 is to make the outcome of this function more predictable.
6750 If we didn't use TO_X == 0, we would stop at the end of
6751 the line which is probably not what a caller would expect
6752 to happen. */
6753 skip = move_it_in_display_line_to (it, to_charpos,
6754 ((op & MOVE_TO_X)
6755 ? to_x : 0),
6756 (MOVE_TO_X
6757 | (op & MOVE_TO_POS)));
6759 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6760 if (skip == MOVE_POS_MATCH_OR_ZV)
6762 reached = 5;
6763 break;
6766 /* If TO_X was reached, we would like to know whether TO_Y
6767 is in the line. This can only be said if we know the
6768 total line height which requires us to scan the rest of
6769 the line. */
6770 if (skip == MOVE_X_REACHED)
6772 it_backup = *it;
6773 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6774 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6775 op & MOVE_TO_POS);
6776 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6779 /* Now, decide whether TO_Y is in this line. */
6780 line_height = it->max_ascent + it->max_descent;
6781 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6783 if (to_y >= it->current_y
6784 && to_y < it->current_y + line_height)
6786 if (skip == MOVE_X_REACHED)
6787 /* If TO_Y is in this line and TO_X was reached above,
6788 we scanned too far. We have to restore IT's settings
6789 to the ones before skipping. */
6790 *it = it_backup;
6791 reached = 6;
6793 else if (skip == MOVE_X_REACHED)
6795 skip = skip2;
6796 if (skip == MOVE_POS_MATCH_OR_ZV)
6797 reached = 7;
6800 if (reached)
6801 break;
6803 else if (BUFFERP (it->object)
6804 && it->method == GET_FROM_BUFFER
6805 && IT_CHARPOS (*it) >= to_charpos)
6806 skip = MOVE_POS_MATCH_OR_ZV;
6807 else
6808 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6810 switch (skip)
6812 case MOVE_POS_MATCH_OR_ZV:
6813 reached = 8;
6814 goto out;
6816 case MOVE_NEWLINE_OR_CR:
6817 set_iterator_to_next (it, 1);
6818 it->continuation_lines_width = 0;
6819 break;
6821 case MOVE_LINE_TRUNCATED:
6822 it->continuation_lines_width = 0;
6823 reseat_at_next_visible_line_start (it, 0);
6824 if ((op & MOVE_TO_POS) != 0
6825 && IT_CHARPOS (*it) > to_charpos)
6827 reached = 9;
6828 goto out;
6830 break;
6832 case MOVE_LINE_CONTINUED:
6833 /* For continued lines ending in a tab, some of the glyphs
6834 associated with the tab are displayed on the current
6835 line. Since it->current_x does not include these glyphs,
6836 we use it->last_visible_x instead. */
6837 it->continuation_lines_width +=
6838 (it->c == '\t') ? it->last_visible_x : it->current_x;
6839 break;
6841 default:
6842 abort ();
6845 /* Reset/increment for the next run. */
6846 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6847 it->current_x = it->hpos = 0;
6848 it->current_y += it->max_ascent + it->max_descent;
6849 ++it->vpos;
6850 last_height = it->max_ascent + it->max_descent;
6851 last_max_ascent = it->max_ascent;
6852 it->max_ascent = it->max_descent = 0;
6855 out:
6857 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6861 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6863 If DY > 0, move IT backward at least that many pixels. DY = 0
6864 means move IT backward to the preceding line start or BEGV. This
6865 function may move over more than DY pixels if IT->current_y - DY
6866 ends up in the middle of a line; in this case IT->current_y will be
6867 set to the top of the line moved to. */
6869 void
6870 move_it_vertically_backward (it, dy)
6871 struct it *it;
6872 int dy;
6874 int nlines, h;
6875 struct it it2, it3;
6876 int start_pos;
6878 move_further_back:
6879 xassert (dy >= 0);
6881 start_pos = IT_CHARPOS (*it);
6883 /* Estimate how many newlines we must move back. */
6884 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6886 /* Set the iterator's position that many lines back. */
6887 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6888 back_to_previous_visible_line_start (it);
6890 /* Reseat the iterator here. When moving backward, we don't want
6891 reseat to skip forward over invisible text, set up the iterator
6892 to deliver from overlay strings at the new position etc. So,
6893 use reseat_1 here. */
6894 reseat_1 (it, it->current.pos, 1);
6896 /* We are now surely at a line start. */
6897 it->current_x = it->hpos = 0;
6898 it->continuation_lines_width = 0;
6900 /* Move forward and see what y-distance we moved. First move to the
6901 start of the next line so that we get its height. We need this
6902 height to be able to tell whether we reached the specified
6903 y-distance. */
6904 it2 = *it;
6905 it2.max_ascent = it2.max_descent = 0;
6908 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6909 MOVE_TO_POS | MOVE_TO_VPOS);
6911 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6912 xassert (IT_CHARPOS (*it) >= BEGV);
6913 it3 = it2;
6915 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6916 xassert (IT_CHARPOS (*it) >= BEGV);
6917 /* H is the actual vertical distance from the position in *IT
6918 and the starting position. */
6919 h = it2.current_y - it->current_y;
6920 /* NLINES is the distance in number of lines. */
6921 nlines = it2.vpos - it->vpos;
6923 /* Correct IT's y and vpos position
6924 so that they are relative to the starting point. */
6925 it->vpos -= nlines;
6926 it->current_y -= h;
6928 if (dy == 0)
6930 /* DY == 0 means move to the start of the screen line. The
6931 value of nlines is > 0 if continuation lines were involved. */
6932 if (nlines > 0)
6933 move_it_by_lines (it, nlines, 1);
6934 #if 0
6935 /* I think this assert is bogus if buffer contains
6936 invisible text or images. KFS. */
6937 xassert (IT_CHARPOS (*it) <= start_pos);
6938 #endif
6940 else
6942 /* The y-position we try to reach, relative to *IT.
6943 Note that H has been subtracted in front of the if-statement. */
6944 int target_y = it->current_y + h - dy;
6945 int y0 = it3.current_y;
6946 int y1 = line_bottom_y (&it3);
6947 int line_height = y1 - y0;
6949 /* If we did not reach target_y, try to move further backward if
6950 we can. If we moved too far backward, try to move forward. */
6951 if (target_y < it->current_y
6952 /* This is heuristic. In a window that's 3 lines high, with
6953 a line height of 13 pixels each, recentering with point
6954 on the bottom line will try to move -39/2 = 19 pixels
6955 backward. Try to avoid moving into the first line. */
6956 && (it->current_y - target_y
6957 > min (window_box_height (it->w), line_height * 2 / 3))
6958 && IT_CHARPOS (*it) > BEGV)
6960 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6961 target_y - it->current_y));
6962 dy = it->current_y - target_y;
6963 goto move_further_back;
6965 else if (target_y >= it->current_y + line_height
6966 && IT_CHARPOS (*it) < ZV)
6968 /* Should move forward by at least one line, maybe more.
6970 Note: Calling move_it_by_lines can be expensive on
6971 terminal frames, where compute_motion is used (via
6972 vmotion) to do the job, when there are very long lines
6973 and truncate-lines is nil. That's the reason for
6974 treating terminal frames specially here. */
6976 if (!FRAME_WINDOW_P (it->f))
6977 move_it_vertically (it, target_y - (it->current_y + line_height));
6978 else
6982 move_it_by_lines (it, 1, 1);
6984 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6987 #if 0
6988 /* I think this assert is bogus if buffer contains
6989 invisible text or images. KFS. */
6990 xassert (IT_CHARPOS (*it) >= BEGV);
6991 #endif
6997 /* Move IT by a specified amount of pixel lines DY. DY negative means
6998 move backwards. DY = 0 means move to start of screen line. At the
6999 end, IT will be on the start of a screen line. */
7001 void
7002 move_it_vertically (it, dy)
7003 struct it *it;
7004 int dy;
7006 if (dy <= 0)
7007 move_it_vertically_backward (it, -dy);
7008 else
7010 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7011 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7012 MOVE_TO_POS | MOVE_TO_Y);
7013 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7015 /* If buffer ends in ZV without a newline, move to the start of
7016 the line to satisfy the post-condition. */
7017 if (IT_CHARPOS (*it) == ZV
7018 && ZV > BEGV
7019 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7020 move_it_by_lines (it, 0, 0);
7025 /* Move iterator IT past the end of the text line it is in. */
7027 void
7028 move_it_past_eol (it)
7029 struct it *it;
7031 enum move_it_result rc;
7033 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7034 if (rc == MOVE_NEWLINE_OR_CR)
7035 set_iterator_to_next (it, 0);
7039 #if 0 /* Currently not used. */
7041 /* Return non-zero if some text between buffer positions START_CHARPOS
7042 and END_CHARPOS is invisible. IT->window is the window for text
7043 property lookup. */
7045 static int
7046 invisible_text_between_p (it, start_charpos, end_charpos)
7047 struct it *it;
7048 int start_charpos, end_charpos;
7050 Lisp_Object prop, limit;
7051 int invisible_found_p;
7053 xassert (it != NULL && start_charpos <= end_charpos);
7055 /* Is text at START invisible? */
7056 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7057 it->window);
7058 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7059 invisible_found_p = 1;
7060 else
7062 limit = Fnext_single_char_property_change (make_number (start_charpos),
7063 Qinvisible, Qnil,
7064 make_number (end_charpos));
7065 invisible_found_p = XFASTINT (limit) < end_charpos;
7068 return invisible_found_p;
7071 #endif /* 0 */
7074 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7075 negative means move up. DVPOS == 0 means move to the start of the
7076 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7077 NEED_Y_P is zero, IT->current_y will be left unchanged.
7079 Further optimization ideas: If we would know that IT->f doesn't use
7080 a face with proportional font, we could be faster for
7081 truncate-lines nil. */
7083 void
7084 move_it_by_lines (it, dvpos, need_y_p)
7085 struct it *it;
7086 int dvpos, need_y_p;
7088 struct position pos;
7090 /* The commented-out optimization uses vmotion on terminals. This
7091 gives bad results, because elements like it->what, on which
7092 callers such as pos_visible_p rely, aren't updated. */
7093 /* if (!FRAME_WINDOW_P (it->f))
7095 struct text_pos textpos;
7097 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7098 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7099 reseat (it, textpos, 1);
7100 it->vpos += pos.vpos;
7101 it->current_y += pos.vpos;
7103 else */
7105 if (dvpos == 0)
7107 /* DVPOS == 0 means move to the start of the screen line. */
7108 move_it_vertically_backward (it, 0);
7109 xassert (it->current_x == 0 && it->hpos == 0);
7110 /* Let next call to line_bottom_y calculate real line height */
7111 last_height = 0;
7113 else if (dvpos > 0)
7115 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7116 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7117 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7119 else
7121 struct it it2;
7122 int start_charpos, i;
7124 /* Start at the beginning of the screen line containing IT's
7125 position. This may actually move vertically backwards,
7126 in case of overlays, so adjust dvpos accordingly. */
7127 dvpos += it->vpos;
7128 move_it_vertically_backward (it, 0);
7129 dvpos -= it->vpos;
7131 /* Go back -DVPOS visible lines and reseat the iterator there. */
7132 start_charpos = IT_CHARPOS (*it);
7133 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7134 back_to_previous_visible_line_start (it);
7135 reseat (it, it->current.pos, 1);
7137 /* Move further back if we end up in a string or an image. */
7138 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7140 /* First try to move to start of display line. */
7141 dvpos += it->vpos;
7142 move_it_vertically_backward (it, 0);
7143 dvpos -= it->vpos;
7144 if (IT_POS_VALID_AFTER_MOVE_P (it))
7145 break;
7146 /* If start of line is still in string or image,
7147 move further back. */
7148 back_to_previous_visible_line_start (it);
7149 reseat (it, it->current.pos, 1);
7150 dvpos--;
7153 it->current_x = it->hpos = 0;
7155 /* Above call may have moved too far if continuation lines
7156 are involved. Scan forward and see if it did. */
7157 it2 = *it;
7158 it2.vpos = it2.current_y = 0;
7159 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7160 it->vpos -= it2.vpos;
7161 it->current_y -= it2.current_y;
7162 it->current_x = it->hpos = 0;
7164 /* If we moved too far back, move IT some lines forward. */
7165 if (it2.vpos > -dvpos)
7167 int delta = it2.vpos + dvpos;
7168 it2 = *it;
7169 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7170 /* Move back again if we got too far ahead. */
7171 if (IT_CHARPOS (*it) >= start_charpos)
7172 *it = it2;
7177 /* Return 1 if IT points into the middle of a display vector. */
7180 in_display_vector_p (it)
7181 struct it *it;
7183 return (it->method == GET_FROM_DISPLAY_VECTOR
7184 && it->current.dpvec_index > 0
7185 && it->dpvec + it->current.dpvec_index != it->dpend);
7189 /***********************************************************************
7190 Messages
7191 ***********************************************************************/
7194 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7195 to *Messages*. */
7197 void
7198 add_to_log (format, arg1, arg2)
7199 char *format;
7200 Lisp_Object arg1, arg2;
7202 Lisp_Object args[3];
7203 Lisp_Object msg, fmt;
7204 char *buffer;
7205 int len;
7206 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7207 USE_SAFE_ALLOCA;
7209 /* Do nothing if called asynchronously. Inserting text into
7210 a buffer may call after-change-functions and alike and
7211 that would means running Lisp asynchronously. */
7212 if (handling_signal)
7213 return;
7215 fmt = msg = Qnil;
7216 GCPRO4 (fmt, msg, arg1, arg2);
7218 args[0] = fmt = build_string (format);
7219 args[1] = arg1;
7220 args[2] = arg2;
7221 msg = Fformat (3, args);
7223 len = SBYTES (msg) + 1;
7224 SAFE_ALLOCA (buffer, char *, len);
7225 bcopy (SDATA (msg), buffer, len);
7227 message_dolog (buffer, len - 1, 1, 0);
7228 SAFE_FREE ();
7230 UNGCPRO;
7234 /* Output a newline in the *Messages* buffer if "needs" one. */
7236 void
7237 message_log_maybe_newline ()
7239 if (message_log_need_newline)
7240 message_dolog ("", 0, 1, 0);
7244 /* Add a string M of length NBYTES to the message log, optionally
7245 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7246 nonzero, means interpret the contents of M as multibyte. This
7247 function calls low-level routines in order to bypass text property
7248 hooks, etc. which might not be safe to run.
7250 This may GC (insert may run before/after change hooks),
7251 so the buffer M must NOT point to a Lisp string. */
7253 void
7254 message_dolog (m, nbytes, nlflag, multibyte)
7255 const char *m;
7256 int nbytes, nlflag, multibyte;
7258 if (!NILP (Vmemory_full))
7259 return;
7261 if (!NILP (Vmessage_log_max))
7263 struct buffer *oldbuf;
7264 Lisp_Object oldpoint, oldbegv, oldzv;
7265 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7266 int point_at_end = 0;
7267 int zv_at_end = 0;
7268 Lisp_Object old_deactivate_mark, tem;
7269 struct gcpro gcpro1;
7271 old_deactivate_mark = Vdeactivate_mark;
7272 oldbuf = current_buffer;
7273 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7274 current_buffer->undo_list = Qt;
7276 oldpoint = message_dolog_marker1;
7277 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7278 oldbegv = message_dolog_marker2;
7279 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7280 oldzv = message_dolog_marker3;
7281 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7282 GCPRO1 (old_deactivate_mark);
7284 if (PT == Z)
7285 point_at_end = 1;
7286 if (ZV == Z)
7287 zv_at_end = 1;
7289 BEGV = BEG;
7290 BEGV_BYTE = BEG_BYTE;
7291 ZV = Z;
7292 ZV_BYTE = Z_BYTE;
7293 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7295 /* Insert the string--maybe converting multibyte to single byte
7296 or vice versa, so that all the text fits the buffer. */
7297 if (multibyte
7298 && NILP (current_buffer->enable_multibyte_characters))
7300 int i, c, char_bytes;
7301 unsigned char work[1];
7303 /* Convert a multibyte string to single-byte
7304 for the *Message* buffer. */
7305 for (i = 0; i < nbytes; i += char_bytes)
7307 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7308 work[0] = (SINGLE_BYTE_CHAR_P (c)
7310 : multibyte_char_to_unibyte (c, Qnil));
7311 insert_1_both (work, 1, 1, 1, 0, 0);
7314 else if (! multibyte
7315 && ! NILP (current_buffer->enable_multibyte_characters))
7317 int i, c, char_bytes;
7318 unsigned char *msg = (unsigned char *) m;
7319 unsigned char str[MAX_MULTIBYTE_LENGTH];
7320 /* Convert a single-byte string to multibyte
7321 for the *Message* buffer. */
7322 for (i = 0; i < nbytes; i++)
7324 c = unibyte_char_to_multibyte (msg[i]);
7325 char_bytes = CHAR_STRING (c, str);
7326 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7329 else if (nbytes)
7330 insert_1 (m, nbytes, 1, 0, 0);
7332 if (nlflag)
7334 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7335 insert_1 ("\n", 1, 1, 0, 0);
7337 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7338 this_bol = PT;
7339 this_bol_byte = PT_BYTE;
7341 /* See if this line duplicates the previous one.
7342 If so, combine duplicates. */
7343 if (this_bol > BEG)
7345 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7346 prev_bol = PT;
7347 prev_bol_byte = PT_BYTE;
7349 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7350 this_bol, this_bol_byte);
7351 if (dup)
7353 del_range_both (prev_bol, prev_bol_byte,
7354 this_bol, this_bol_byte, 0);
7355 if (dup > 1)
7357 char dupstr[40];
7358 int duplen;
7360 /* If you change this format, don't forget to also
7361 change message_log_check_duplicate. */
7362 sprintf (dupstr, " [%d times]", dup);
7363 duplen = strlen (dupstr);
7364 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7365 insert_1 (dupstr, duplen, 1, 0, 1);
7370 /* If we have more than the desired maximum number of lines
7371 in the *Messages* buffer now, delete the oldest ones.
7372 This is safe because we don't have undo in this buffer. */
7374 if (NATNUMP (Vmessage_log_max))
7376 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7377 -XFASTINT (Vmessage_log_max) - 1, 0);
7378 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7381 BEGV = XMARKER (oldbegv)->charpos;
7382 BEGV_BYTE = marker_byte_position (oldbegv);
7384 if (zv_at_end)
7386 ZV = Z;
7387 ZV_BYTE = Z_BYTE;
7389 else
7391 ZV = XMARKER (oldzv)->charpos;
7392 ZV_BYTE = marker_byte_position (oldzv);
7395 if (point_at_end)
7396 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7397 else
7398 /* We can't do Fgoto_char (oldpoint) because it will run some
7399 Lisp code. */
7400 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7401 XMARKER (oldpoint)->bytepos);
7403 UNGCPRO;
7404 unchain_marker (XMARKER (oldpoint));
7405 unchain_marker (XMARKER (oldbegv));
7406 unchain_marker (XMARKER (oldzv));
7408 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7409 set_buffer_internal (oldbuf);
7410 if (NILP (tem))
7411 windows_or_buffers_changed = old_windows_or_buffers_changed;
7412 message_log_need_newline = !nlflag;
7413 Vdeactivate_mark = old_deactivate_mark;
7418 /* We are at the end of the buffer after just having inserted a newline.
7419 (Note: We depend on the fact we won't be crossing the gap.)
7420 Check to see if the most recent message looks a lot like the previous one.
7421 Return 0 if different, 1 if the new one should just replace it, or a
7422 value N > 1 if we should also append " [N times]". */
7424 static int
7425 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7426 int prev_bol, this_bol;
7427 int prev_bol_byte, this_bol_byte;
7429 int i;
7430 int len = Z_BYTE - 1 - this_bol_byte;
7431 int seen_dots = 0;
7432 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7433 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7435 for (i = 0; i < len; i++)
7437 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7438 seen_dots = 1;
7439 if (p1[i] != p2[i])
7440 return seen_dots;
7442 p1 += len;
7443 if (*p1 == '\n')
7444 return 2;
7445 if (*p1++ == ' ' && *p1++ == '[')
7447 int n = 0;
7448 while (*p1 >= '0' && *p1 <= '9')
7449 n = n * 10 + *p1++ - '0';
7450 if (strncmp (p1, " times]\n", 8) == 0)
7451 return n+1;
7453 return 0;
7457 /* Display an echo area message M with a specified length of NBYTES
7458 bytes. The string may include null characters. If M is 0, clear
7459 out any existing message, and let the mini-buffer text show
7460 through.
7462 This may GC, so the buffer M must NOT point to a Lisp string. */
7464 void
7465 message2 (m, nbytes, multibyte)
7466 const char *m;
7467 int nbytes;
7468 int multibyte;
7470 /* First flush out any partial line written with print. */
7471 message_log_maybe_newline ();
7472 if (m)
7473 message_dolog (m, nbytes, 1, multibyte);
7474 message2_nolog (m, nbytes, multibyte);
7478 /* The non-logging counterpart of message2. */
7480 void
7481 message2_nolog (m, nbytes, multibyte)
7482 const char *m;
7483 int nbytes, multibyte;
7485 struct frame *sf = SELECTED_FRAME ();
7486 message_enable_multibyte = multibyte;
7488 if (noninteractive)
7490 if (noninteractive_need_newline)
7491 putc ('\n', stderr);
7492 noninteractive_need_newline = 0;
7493 if (m)
7494 fwrite (m, nbytes, 1, stderr);
7495 if (cursor_in_echo_area == 0)
7496 fprintf (stderr, "\n");
7497 fflush (stderr);
7499 /* A null message buffer means that the frame hasn't really been
7500 initialized yet. Error messages get reported properly by
7501 cmd_error, so this must be just an informative message; toss it. */
7502 else if (INTERACTIVE
7503 && sf->glyphs_initialized_p
7504 && FRAME_MESSAGE_BUF (sf))
7506 Lisp_Object mini_window;
7507 struct frame *f;
7509 /* Get the frame containing the mini-buffer
7510 that the selected frame is using. */
7511 mini_window = FRAME_MINIBUF_WINDOW (sf);
7512 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7514 FRAME_SAMPLE_VISIBILITY (f);
7515 if (FRAME_VISIBLE_P (sf)
7516 && ! FRAME_VISIBLE_P (f))
7517 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7519 if (m)
7521 set_message (m, Qnil, nbytes, multibyte);
7522 if (minibuffer_auto_raise)
7523 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7525 else
7526 clear_message (1, 1);
7528 do_pending_window_change (0);
7529 echo_area_display (1);
7530 do_pending_window_change (0);
7531 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7532 (*frame_up_to_date_hook) (f);
7537 /* Display an echo area message M with a specified length of NBYTES
7538 bytes. The string may include null characters. If M is not a
7539 string, clear out any existing message, and let the mini-buffer
7540 text show through.
7542 This function cancels echoing. */
7544 void
7545 message3 (m, nbytes, multibyte)
7546 Lisp_Object m;
7547 int nbytes;
7548 int multibyte;
7550 struct gcpro gcpro1;
7552 GCPRO1 (m);
7553 clear_message (1,1);
7554 cancel_echoing ();
7556 /* First flush out any partial line written with print. */
7557 message_log_maybe_newline ();
7558 if (STRINGP (m))
7560 char *buffer;
7561 USE_SAFE_ALLOCA;
7563 SAFE_ALLOCA (buffer, char *, nbytes);
7564 bcopy (SDATA (m), buffer, nbytes);
7565 message_dolog (buffer, nbytes, 1, multibyte);
7566 SAFE_FREE ();
7568 message3_nolog (m, nbytes, multibyte);
7570 UNGCPRO;
7574 /* The non-logging version of message3.
7575 This does not cancel echoing, because it is used for echoing.
7576 Perhaps we need to make a separate function for echoing
7577 and make this cancel echoing. */
7579 void
7580 message3_nolog (m, nbytes, multibyte)
7581 Lisp_Object m;
7582 int nbytes, multibyte;
7584 struct frame *sf = SELECTED_FRAME ();
7585 message_enable_multibyte = multibyte;
7587 if (noninteractive)
7589 if (noninteractive_need_newline)
7590 putc ('\n', stderr);
7591 noninteractive_need_newline = 0;
7592 if (STRINGP (m))
7593 fwrite (SDATA (m), nbytes, 1, stderr);
7594 if (cursor_in_echo_area == 0)
7595 fprintf (stderr, "\n");
7596 fflush (stderr);
7598 /* A null message buffer means that the frame hasn't really been
7599 initialized yet. Error messages get reported properly by
7600 cmd_error, so this must be just an informative message; toss it. */
7601 else if (INTERACTIVE
7602 && sf->glyphs_initialized_p
7603 && FRAME_MESSAGE_BUF (sf))
7605 Lisp_Object mini_window;
7606 Lisp_Object frame;
7607 struct frame *f;
7609 /* Get the frame containing the mini-buffer
7610 that the selected frame is using. */
7611 mini_window = FRAME_MINIBUF_WINDOW (sf);
7612 frame = XWINDOW (mini_window)->frame;
7613 f = XFRAME (frame);
7615 FRAME_SAMPLE_VISIBILITY (f);
7616 if (FRAME_VISIBLE_P (sf)
7617 && !FRAME_VISIBLE_P (f))
7618 Fmake_frame_visible (frame);
7620 if (STRINGP (m) && SCHARS (m) > 0)
7622 set_message (NULL, m, nbytes, multibyte);
7623 if (minibuffer_auto_raise)
7624 Fraise_frame (frame);
7625 /* Assume we are not echoing.
7626 (If we are, echo_now will override this.) */
7627 echo_message_buffer = Qnil;
7629 else
7630 clear_message (1, 1);
7632 do_pending_window_change (0);
7633 echo_area_display (1);
7634 do_pending_window_change (0);
7635 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7636 (*frame_up_to_date_hook) (f);
7641 /* Display a null-terminated echo area message M. If M is 0, clear
7642 out any existing message, and let the mini-buffer text show through.
7644 The buffer M must continue to exist until after the echo area gets
7645 cleared or some other message gets displayed there. Do not pass
7646 text that is stored in a Lisp string. Do not pass text in a buffer
7647 that was alloca'd. */
7649 void
7650 message1 (m)
7651 char *m;
7653 message2 (m, (m ? strlen (m) : 0), 0);
7657 /* The non-logging counterpart of message1. */
7659 void
7660 message1_nolog (m)
7661 char *m;
7663 message2_nolog (m, (m ? strlen (m) : 0), 0);
7666 /* Display a message M which contains a single %s
7667 which gets replaced with STRING. */
7669 void
7670 message_with_string (m, string, log)
7671 char *m;
7672 Lisp_Object string;
7673 int log;
7675 CHECK_STRING (string);
7677 if (noninteractive)
7679 if (m)
7681 if (noninteractive_need_newline)
7682 putc ('\n', stderr);
7683 noninteractive_need_newline = 0;
7684 fprintf (stderr, m, SDATA (string));
7685 if (cursor_in_echo_area == 0)
7686 fprintf (stderr, "\n");
7687 fflush (stderr);
7690 else if (INTERACTIVE)
7692 /* The frame whose minibuffer we're going to display the message on.
7693 It may be larger than the selected frame, so we need
7694 to use its buffer, not the selected frame's buffer. */
7695 Lisp_Object mini_window;
7696 struct frame *f, *sf = SELECTED_FRAME ();
7698 /* Get the frame containing the minibuffer
7699 that the selected frame is using. */
7700 mini_window = FRAME_MINIBUF_WINDOW (sf);
7701 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7703 /* A null message buffer means that the frame hasn't really been
7704 initialized yet. Error messages get reported properly by
7705 cmd_error, so this must be just an informative message; toss it. */
7706 if (FRAME_MESSAGE_BUF (f))
7708 Lisp_Object args[2], message;
7709 struct gcpro gcpro1, gcpro2;
7711 args[0] = build_string (m);
7712 args[1] = message = string;
7713 GCPRO2 (args[0], message);
7714 gcpro1.nvars = 2;
7716 message = Fformat (2, args);
7718 if (log)
7719 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7720 else
7721 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7723 UNGCPRO;
7725 /* Print should start at the beginning of the message
7726 buffer next time. */
7727 message_buf_print = 0;
7733 /* Dump an informative message to the minibuf. If M is 0, clear out
7734 any existing message, and let the mini-buffer text show through. */
7736 /* VARARGS 1 */
7737 void
7738 message (m, a1, a2, a3)
7739 char *m;
7740 EMACS_INT a1, a2, a3;
7742 if (noninteractive)
7744 if (m)
7746 if (noninteractive_need_newline)
7747 putc ('\n', stderr);
7748 noninteractive_need_newline = 0;
7749 fprintf (stderr, m, a1, a2, a3);
7750 if (cursor_in_echo_area == 0)
7751 fprintf (stderr, "\n");
7752 fflush (stderr);
7755 else if (INTERACTIVE)
7757 /* The frame whose mini-buffer we're going to display the message
7758 on. It may be larger than the selected frame, so we need to
7759 use its buffer, not the selected frame's buffer. */
7760 Lisp_Object mini_window;
7761 struct frame *f, *sf = SELECTED_FRAME ();
7763 /* Get the frame containing the mini-buffer
7764 that the selected frame is using. */
7765 mini_window = FRAME_MINIBUF_WINDOW (sf);
7766 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7768 /* A null message buffer means that the frame hasn't really been
7769 initialized yet. Error messages get reported properly by
7770 cmd_error, so this must be just an informative message; toss
7771 it. */
7772 if (FRAME_MESSAGE_BUF (f))
7774 if (m)
7776 int len;
7777 #ifdef NO_ARG_ARRAY
7778 char *a[3];
7779 a[0] = (char *) a1;
7780 a[1] = (char *) a2;
7781 a[2] = (char *) a3;
7783 len = doprnt (FRAME_MESSAGE_BUF (f),
7784 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7785 #else
7786 len = doprnt (FRAME_MESSAGE_BUF (f),
7787 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7788 (char **) &a1);
7789 #endif /* NO_ARG_ARRAY */
7791 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7793 else
7794 message1 (0);
7796 /* Print should start at the beginning of the message
7797 buffer next time. */
7798 message_buf_print = 0;
7804 /* The non-logging version of message. */
7806 void
7807 message_nolog (m, a1, a2, a3)
7808 char *m;
7809 EMACS_INT a1, a2, a3;
7811 Lisp_Object old_log_max;
7812 old_log_max = Vmessage_log_max;
7813 Vmessage_log_max = Qnil;
7814 message (m, a1, a2, a3);
7815 Vmessage_log_max = old_log_max;
7819 /* Display the current message in the current mini-buffer. This is
7820 only called from error handlers in process.c, and is not time
7821 critical. */
7823 void
7824 update_echo_area ()
7826 if (!NILP (echo_area_buffer[0]))
7828 Lisp_Object string;
7829 string = Fcurrent_message ();
7830 message3 (string, SBYTES (string),
7831 !NILP (current_buffer->enable_multibyte_characters));
7836 /* Make sure echo area buffers in `echo_buffers' are live.
7837 If they aren't, make new ones. */
7839 static void
7840 ensure_echo_area_buffers ()
7842 int i;
7844 for (i = 0; i < 2; ++i)
7845 if (!BUFFERP (echo_buffer[i])
7846 || NILP (XBUFFER (echo_buffer[i])->name))
7848 char name[30];
7849 Lisp_Object old_buffer;
7850 int j;
7852 old_buffer = echo_buffer[i];
7853 sprintf (name, " *Echo Area %d*", i);
7854 echo_buffer[i] = Fget_buffer_create (build_string (name));
7855 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7857 for (j = 0; j < 2; ++j)
7858 if (EQ (old_buffer, echo_area_buffer[j]))
7859 echo_area_buffer[j] = echo_buffer[i];
7864 /* Call FN with args A1..A4 with either the current or last displayed
7865 echo_area_buffer as current buffer.
7867 WHICH zero means use the current message buffer
7868 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7869 from echo_buffer[] and clear it.
7871 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7872 suitable buffer from echo_buffer[] and clear it.
7874 Value is what FN returns. */
7876 static int
7877 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7878 struct window *w;
7879 int which;
7880 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7881 EMACS_INT a1;
7882 Lisp_Object a2;
7883 EMACS_INT a3, a4;
7885 Lisp_Object buffer;
7886 int this_one, the_other, clear_buffer_p, rc;
7887 int count = SPECPDL_INDEX ();
7889 /* If buffers aren't live, make new ones. */
7890 ensure_echo_area_buffers ();
7892 clear_buffer_p = 0;
7894 if (which == 0)
7895 this_one = 0, the_other = 1;
7896 else if (which > 0)
7897 this_one = 1, the_other = 0;
7899 /* Choose a suitable buffer from echo_buffer[] is we don't
7900 have one. */
7901 if (NILP (echo_area_buffer[this_one]))
7903 echo_area_buffer[this_one]
7904 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7905 ? echo_buffer[the_other]
7906 : echo_buffer[this_one]);
7907 clear_buffer_p = 1;
7910 buffer = echo_area_buffer[this_one];
7912 /* Don't get confused by reusing the buffer used for echoing
7913 for a different purpose. */
7914 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7915 cancel_echoing ();
7917 record_unwind_protect (unwind_with_echo_area_buffer,
7918 with_echo_area_buffer_unwind_data (w));
7920 /* Make the echo area buffer current. Note that for display
7921 purposes, it is not necessary that the displayed window's buffer
7922 == current_buffer, except for text property lookup. So, let's
7923 only set that buffer temporarily here without doing a full
7924 Fset_window_buffer. We must also change w->pointm, though,
7925 because otherwise an assertions in unshow_buffer fails, and Emacs
7926 aborts. */
7927 set_buffer_internal_1 (XBUFFER (buffer));
7928 if (w)
7930 w->buffer = buffer;
7931 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7934 current_buffer->undo_list = Qt;
7935 current_buffer->read_only = Qnil;
7936 specbind (Qinhibit_read_only, Qt);
7937 specbind (Qinhibit_modification_hooks, Qt);
7939 if (clear_buffer_p && Z > BEG)
7940 del_range (BEG, Z);
7942 xassert (BEGV >= BEG);
7943 xassert (ZV <= Z && ZV >= BEGV);
7945 rc = fn (a1, a2, a3, a4);
7947 xassert (BEGV >= BEG);
7948 xassert (ZV <= Z && ZV >= BEGV);
7950 unbind_to (count, Qnil);
7951 return rc;
7955 /* Save state that should be preserved around the call to the function
7956 FN called in with_echo_area_buffer. */
7958 static Lisp_Object
7959 with_echo_area_buffer_unwind_data (w)
7960 struct window *w;
7962 int i = 0;
7963 Lisp_Object vector;
7965 /* Reduce consing by keeping one vector in
7966 Vwith_echo_area_save_vector. */
7967 vector = Vwith_echo_area_save_vector;
7968 Vwith_echo_area_save_vector = Qnil;
7970 if (NILP (vector))
7971 vector = Fmake_vector (make_number (7), Qnil);
7973 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7974 AREF (vector, i) = Vdeactivate_mark, ++i;
7975 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7977 if (w)
7979 XSETWINDOW (AREF (vector, i), w); ++i;
7980 AREF (vector, i) = w->buffer; ++i;
7981 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7982 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7984 else
7986 int end = i + 4;
7987 for (; i < end; ++i)
7988 AREF (vector, i) = Qnil;
7991 xassert (i == ASIZE (vector));
7992 return vector;
7996 /* Restore global state from VECTOR which was created by
7997 with_echo_area_buffer_unwind_data. */
7999 static Lisp_Object
8000 unwind_with_echo_area_buffer (vector)
8001 Lisp_Object vector;
8003 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8004 Vdeactivate_mark = AREF (vector, 1);
8005 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8007 if (WINDOWP (AREF (vector, 3)))
8009 struct window *w;
8010 Lisp_Object buffer, charpos, bytepos;
8012 w = XWINDOW (AREF (vector, 3));
8013 buffer = AREF (vector, 4);
8014 charpos = AREF (vector, 5);
8015 bytepos = AREF (vector, 6);
8017 w->buffer = buffer;
8018 set_marker_both (w->pointm, buffer,
8019 XFASTINT (charpos), XFASTINT (bytepos));
8022 Vwith_echo_area_save_vector = vector;
8023 return Qnil;
8027 /* Set up the echo area for use by print functions. MULTIBYTE_P
8028 non-zero means we will print multibyte. */
8030 void
8031 setup_echo_area_for_printing (multibyte_p)
8032 int multibyte_p;
8034 /* If we can't find an echo area any more, exit. */
8035 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8036 Fkill_emacs (Qnil);
8038 ensure_echo_area_buffers ();
8040 if (!message_buf_print)
8042 /* A message has been output since the last time we printed.
8043 Choose a fresh echo area buffer. */
8044 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8045 echo_area_buffer[0] = echo_buffer[1];
8046 else
8047 echo_area_buffer[0] = echo_buffer[0];
8049 /* Switch to that buffer and clear it. */
8050 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8051 current_buffer->truncate_lines = Qnil;
8053 if (Z > BEG)
8055 int count = SPECPDL_INDEX ();
8056 specbind (Qinhibit_read_only, Qt);
8057 /* Note that undo recording is always disabled. */
8058 del_range (BEG, Z);
8059 unbind_to (count, Qnil);
8061 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8063 /* Set up the buffer for the multibyteness we need. */
8064 if (multibyte_p
8065 != !NILP (current_buffer->enable_multibyte_characters))
8066 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8068 /* Raise the frame containing the echo area. */
8069 if (minibuffer_auto_raise)
8071 struct frame *sf = SELECTED_FRAME ();
8072 Lisp_Object mini_window;
8073 mini_window = FRAME_MINIBUF_WINDOW (sf);
8074 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8077 message_log_maybe_newline ();
8078 message_buf_print = 1;
8080 else
8082 if (NILP (echo_area_buffer[0]))
8084 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8085 echo_area_buffer[0] = echo_buffer[1];
8086 else
8087 echo_area_buffer[0] = echo_buffer[0];
8090 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8092 /* Someone switched buffers between print requests. */
8093 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8094 current_buffer->truncate_lines = Qnil;
8100 /* Display an echo area message in window W. Value is non-zero if W's
8101 height is changed. If display_last_displayed_message_p is
8102 non-zero, display the message that was last displayed, otherwise
8103 display the current message. */
8105 static int
8106 display_echo_area (w)
8107 struct window *w;
8109 int i, no_message_p, window_height_changed_p, count;
8111 /* Temporarily disable garbage collections while displaying the echo
8112 area. This is done because a GC can print a message itself.
8113 That message would modify the echo area buffer's contents while a
8114 redisplay of the buffer is going on, and seriously confuse
8115 redisplay. */
8116 count = inhibit_garbage_collection ();
8118 /* If there is no message, we must call display_echo_area_1
8119 nevertheless because it resizes the window. But we will have to
8120 reset the echo_area_buffer in question to nil at the end because
8121 with_echo_area_buffer will sets it to an empty buffer. */
8122 i = display_last_displayed_message_p ? 1 : 0;
8123 no_message_p = NILP (echo_area_buffer[i]);
8125 window_height_changed_p
8126 = with_echo_area_buffer (w, display_last_displayed_message_p,
8127 display_echo_area_1,
8128 (EMACS_INT) w, Qnil, 0, 0);
8130 if (no_message_p)
8131 echo_area_buffer[i] = Qnil;
8133 unbind_to (count, Qnil);
8134 return window_height_changed_p;
8138 /* Helper for display_echo_area. Display the current buffer which
8139 contains the current echo area message in window W, a mini-window,
8140 a pointer to which is passed in A1. A2..A4 are currently not used.
8141 Change the height of W so that all of the message is displayed.
8142 Value is non-zero if height of W was changed. */
8144 static int
8145 display_echo_area_1 (a1, a2, a3, a4)
8146 EMACS_INT a1;
8147 Lisp_Object a2;
8148 EMACS_INT a3, a4;
8150 struct window *w = (struct window *) a1;
8151 Lisp_Object window;
8152 struct text_pos start;
8153 int window_height_changed_p = 0;
8155 /* Do this before displaying, so that we have a large enough glyph
8156 matrix for the display. If we can't get enough space for the
8157 whole text, display the last N lines. That works by setting w->start. */
8158 window_height_changed_p = resize_mini_window (w, 0);
8160 /* Use the starting position chosen by resize_mini_window. */
8161 SET_TEXT_POS_FROM_MARKER (start, w->start);
8163 /* Display. */
8164 clear_glyph_matrix (w->desired_matrix);
8165 XSETWINDOW (window, w);
8166 try_window (window, start, 0);
8168 return window_height_changed_p;
8172 /* Resize the echo area window to exactly the size needed for the
8173 currently displayed message, if there is one. If a mini-buffer
8174 is active, don't shrink it. */
8176 void
8177 resize_echo_area_exactly ()
8179 if (BUFFERP (echo_area_buffer[0])
8180 && WINDOWP (echo_area_window))
8182 struct window *w = XWINDOW (echo_area_window);
8183 int resized_p;
8184 Lisp_Object resize_exactly;
8186 if (minibuf_level == 0)
8187 resize_exactly = Qt;
8188 else
8189 resize_exactly = Qnil;
8191 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8192 (EMACS_INT) w, resize_exactly, 0, 0);
8193 if (resized_p)
8195 ++windows_or_buffers_changed;
8196 ++update_mode_lines;
8197 redisplay_internal (0);
8203 /* Callback function for with_echo_area_buffer, when used from
8204 resize_echo_area_exactly. A1 contains a pointer to the window to
8205 resize, EXACTLY non-nil means resize the mini-window exactly to the
8206 size of the text displayed. A3 and A4 are not used. Value is what
8207 resize_mini_window returns. */
8209 static int
8210 resize_mini_window_1 (a1, exactly, a3, a4)
8211 EMACS_INT a1;
8212 Lisp_Object exactly;
8213 EMACS_INT a3, a4;
8215 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8219 /* Resize mini-window W to fit the size of its contents. EXACT:P
8220 means size the window exactly to the size needed. Otherwise, it's
8221 only enlarged until W's buffer is empty.
8223 Set W->start to the right place to begin display. If the whole
8224 contents fit, start at the beginning. Otherwise, start so as
8225 to make the end of the contents appear. This is particularly
8226 important for y-or-n-p, but seems desirable generally.
8228 Value is non-zero if the window height has been changed. */
8231 resize_mini_window (w, exact_p)
8232 struct window *w;
8233 int exact_p;
8235 struct frame *f = XFRAME (w->frame);
8236 int window_height_changed_p = 0;
8238 xassert (MINI_WINDOW_P (w));
8240 /* By default, start display at the beginning. */
8241 set_marker_both (w->start, w->buffer,
8242 BUF_BEGV (XBUFFER (w->buffer)),
8243 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8245 /* Don't resize windows while redisplaying a window; it would
8246 confuse redisplay functions when the size of the window they are
8247 displaying changes from under them. Such a resizing can happen,
8248 for instance, when which-func prints a long message while
8249 we are running fontification-functions. We're running these
8250 functions with safe_call which binds inhibit-redisplay to t. */
8251 if (!NILP (Vinhibit_redisplay))
8252 return 0;
8254 /* Nil means don't try to resize. */
8255 if (NILP (Vresize_mini_windows)
8256 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8257 return 0;
8259 if (!FRAME_MINIBUF_ONLY_P (f))
8261 struct it it;
8262 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8263 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8264 int height, max_height;
8265 int unit = FRAME_LINE_HEIGHT (f);
8266 struct text_pos start;
8267 struct buffer *old_current_buffer = NULL;
8269 if (current_buffer != XBUFFER (w->buffer))
8271 old_current_buffer = current_buffer;
8272 set_buffer_internal (XBUFFER (w->buffer));
8275 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8277 /* Compute the max. number of lines specified by the user. */
8278 if (FLOATP (Vmax_mini_window_height))
8279 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8280 else if (INTEGERP (Vmax_mini_window_height))
8281 max_height = XINT (Vmax_mini_window_height);
8282 else
8283 max_height = total_height / 4;
8285 /* Correct that max. height if it's bogus. */
8286 max_height = max (1, max_height);
8287 max_height = min (total_height, max_height);
8289 /* Find out the height of the text in the window. */
8290 if (it.truncate_lines_p)
8291 height = 1;
8292 else
8294 last_height = 0;
8295 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8296 if (it.max_ascent == 0 && it.max_descent == 0)
8297 height = it.current_y + last_height;
8298 else
8299 height = it.current_y + it.max_ascent + it.max_descent;
8300 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8301 height = (height + unit - 1) / unit;
8304 /* Compute a suitable window start. */
8305 if (height > max_height)
8307 height = max_height;
8308 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8309 move_it_vertically_backward (&it, (height - 1) * unit);
8310 start = it.current.pos;
8312 else
8313 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8314 SET_MARKER_FROM_TEXT_POS (w->start, start);
8316 if (EQ (Vresize_mini_windows, Qgrow_only))
8318 /* Let it grow only, until we display an empty message, in which
8319 case the window shrinks again. */
8320 if (height > WINDOW_TOTAL_LINES (w))
8322 int old_height = WINDOW_TOTAL_LINES (w);
8323 freeze_window_starts (f, 1);
8324 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8325 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8327 else if (height < WINDOW_TOTAL_LINES (w)
8328 && (exact_p || BEGV == ZV))
8330 int old_height = WINDOW_TOTAL_LINES (w);
8331 freeze_window_starts (f, 0);
8332 shrink_mini_window (w);
8333 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8336 else
8338 /* Always resize to exact size needed. */
8339 if (height > WINDOW_TOTAL_LINES (w))
8341 int old_height = WINDOW_TOTAL_LINES (w);
8342 freeze_window_starts (f, 1);
8343 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8344 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8346 else if (height < WINDOW_TOTAL_LINES (w))
8348 int old_height = WINDOW_TOTAL_LINES (w);
8349 freeze_window_starts (f, 0);
8350 shrink_mini_window (w);
8352 if (height)
8354 freeze_window_starts (f, 1);
8355 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8358 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8362 if (old_current_buffer)
8363 set_buffer_internal (old_current_buffer);
8366 return window_height_changed_p;
8370 /* Value is the current message, a string, or nil if there is no
8371 current message. */
8373 Lisp_Object
8374 current_message ()
8376 Lisp_Object msg;
8378 if (NILP (echo_area_buffer[0]))
8379 msg = Qnil;
8380 else
8382 with_echo_area_buffer (0, 0, current_message_1,
8383 (EMACS_INT) &msg, Qnil, 0, 0);
8384 if (NILP (msg))
8385 echo_area_buffer[0] = Qnil;
8388 return msg;
8392 static int
8393 current_message_1 (a1, a2, a3, a4)
8394 EMACS_INT a1;
8395 Lisp_Object a2;
8396 EMACS_INT a3, a4;
8398 Lisp_Object *msg = (Lisp_Object *) a1;
8400 if (Z > BEG)
8401 *msg = make_buffer_string (BEG, Z, 1);
8402 else
8403 *msg = Qnil;
8404 return 0;
8408 /* Push the current message on Vmessage_stack for later restauration
8409 by restore_message. Value is non-zero if the current message isn't
8410 empty. This is a relatively infrequent operation, so it's not
8411 worth optimizing. */
8414 push_message ()
8416 Lisp_Object msg;
8417 msg = current_message ();
8418 Vmessage_stack = Fcons (msg, Vmessage_stack);
8419 return STRINGP (msg);
8423 /* Restore message display from the top of Vmessage_stack. */
8425 void
8426 restore_message ()
8428 Lisp_Object msg;
8430 xassert (CONSP (Vmessage_stack));
8431 msg = XCAR (Vmessage_stack);
8432 if (STRINGP (msg))
8433 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8434 else
8435 message3_nolog (msg, 0, 0);
8439 /* Handler for record_unwind_protect calling pop_message. */
8441 Lisp_Object
8442 pop_message_unwind (dummy)
8443 Lisp_Object dummy;
8445 pop_message ();
8446 return Qnil;
8449 /* Pop the top-most entry off Vmessage_stack. */
8451 void
8452 pop_message ()
8454 xassert (CONSP (Vmessage_stack));
8455 Vmessage_stack = XCDR (Vmessage_stack);
8459 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8460 exits. If the stack is not empty, we have a missing pop_message
8461 somewhere. */
8463 void
8464 check_message_stack ()
8466 if (!NILP (Vmessage_stack))
8467 abort ();
8471 /* Truncate to NCHARS what will be displayed in the echo area the next
8472 time we display it---but don't redisplay it now. */
8474 void
8475 truncate_echo_area (nchars)
8476 int nchars;
8478 if (nchars == 0)
8479 echo_area_buffer[0] = Qnil;
8480 /* A null message buffer means that the frame hasn't really been
8481 initialized yet. Error messages get reported properly by
8482 cmd_error, so this must be just an informative message; toss it. */
8483 else if (!noninteractive
8484 && INTERACTIVE
8485 && !NILP (echo_area_buffer[0]))
8487 struct frame *sf = SELECTED_FRAME ();
8488 if (FRAME_MESSAGE_BUF (sf))
8489 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8494 /* Helper function for truncate_echo_area. Truncate the current
8495 message to at most NCHARS characters. */
8497 static int
8498 truncate_message_1 (nchars, a2, a3, a4)
8499 EMACS_INT nchars;
8500 Lisp_Object a2;
8501 EMACS_INT a3, a4;
8503 if (BEG + nchars < Z)
8504 del_range (BEG + nchars, Z);
8505 if (Z == BEG)
8506 echo_area_buffer[0] = Qnil;
8507 return 0;
8511 /* Set the current message to a substring of S or STRING.
8513 If STRING is a Lisp string, set the message to the first NBYTES
8514 bytes from STRING. NBYTES zero means use the whole string. If
8515 STRING is multibyte, the message will be displayed multibyte.
8517 If S is not null, set the message to the first LEN bytes of S. LEN
8518 zero means use the whole string. MULTIBYTE_P non-zero means S is
8519 multibyte. Display the message multibyte in that case.
8521 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8522 to t before calling set_message_1 (which calls insert).
8525 void
8526 set_message (s, string, nbytes, multibyte_p)
8527 const char *s;
8528 Lisp_Object string;
8529 int nbytes, multibyte_p;
8531 message_enable_multibyte
8532 = ((s && multibyte_p)
8533 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8535 with_echo_area_buffer (0, 0, set_message_1,
8536 (EMACS_INT) s, string, nbytes, multibyte_p);
8537 message_buf_print = 0;
8538 help_echo_showing_p = 0;
8542 /* Helper function for set_message. Arguments have the same meaning
8543 as there, with A1 corresponding to S and A2 corresponding to STRING
8544 This function is called with the echo area buffer being
8545 current. */
8547 static int
8548 set_message_1 (a1, a2, nbytes, multibyte_p)
8549 EMACS_INT a1;
8550 Lisp_Object a2;
8551 EMACS_INT nbytes, multibyte_p;
8553 const char *s = (const char *) a1;
8554 Lisp_Object string = a2;
8556 /* Change multibyteness of the echo buffer appropriately. */
8557 if (message_enable_multibyte
8558 != !NILP (current_buffer->enable_multibyte_characters))
8559 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8561 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8563 /* Insert new message at BEG. */
8564 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8565 Ferase_buffer ();
8567 if (STRINGP (string))
8569 int nchars;
8571 if (nbytes == 0)
8572 nbytes = SBYTES (string);
8573 nchars = string_byte_to_char (string, nbytes);
8575 /* This function takes care of single/multibyte conversion. We
8576 just have to ensure that the echo area buffer has the right
8577 setting of enable_multibyte_characters. */
8578 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8580 else if (s)
8582 if (nbytes == 0)
8583 nbytes = strlen (s);
8585 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8587 /* Convert from multi-byte to single-byte. */
8588 int i, c, n;
8589 unsigned char work[1];
8591 /* Convert a multibyte string to single-byte. */
8592 for (i = 0; i < nbytes; i += n)
8594 c = string_char_and_length (s + i, nbytes - i, &n);
8595 work[0] = (SINGLE_BYTE_CHAR_P (c)
8597 : multibyte_char_to_unibyte (c, Qnil));
8598 insert_1_both (work, 1, 1, 1, 0, 0);
8601 else if (!multibyte_p
8602 && !NILP (current_buffer->enable_multibyte_characters))
8604 /* Convert from single-byte to multi-byte. */
8605 int i, c, n;
8606 const unsigned char *msg = (const unsigned char *) s;
8607 unsigned char str[MAX_MULTIBYTE_LENGTH];
8609 /* Convert a single-byte string to multibyte. */
8610 for (i = 0; i < nbytes; i++)
8612 c = unibyte_char_to_multibyte (msg[i]);
8613 n = CHAR_STRING (c, str);
8614 insert_1_both (str, 1, n, 1, 0, 0);
8617 else
8618 insert_1 (s, nbytes, 1, 0, 0);
8621 return 0;
8625 /* Clear messages. CURRENT_P non-zero means clear the current
8626 message. LAST_DISPLAYED_P non-zero means clear the message
8627 last displayed. */
8629 void
8630 clear_message (current_p, last_displayed_p)
8631 int current_p, last_displayed_p;
8633 if (current_p)
8635 echo_area_buffer[0] = Qnil;
8636 message_cleared_p = 1;
8639 if (last_displayed_p)
8640 echo_area_buffer[1] = Qnil;
8642 message_buf_print = 0;
8645 /* Clear garbaged frames.
8647 This function is used where the old redisplay called
8648 redraw_garbaged_frames which in turn called redraw_frame which in
8649 turn called clear_frame. The call to clear_frame was a source of
8650 flickering. I believe a clear_frame is not necessary. It should
8651 suffice in the new redisplay to invalidate all current matrices,
8652 and ensure a complete redisplay of all windows. */
8654 static void
8655 clear_garbaged_frames ()
8657 if (frame_garbaged)
8659 Lisp_Object tail, frame;
8660 int changed_count = 0;
8662 FOR_EACH_FRAME (tail, frame)
8664 struct frame *f = XFRAME (frame);
8666 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8668 if (f->resized_p)
8670 Fredraw_frame (frame);
8671 f->force_flush_display_p = 1;
8673 clear_current_matrices (f);
8674 changed_count++;
8675 f->garbaged = 0;
8676 f->resized_p = 0;
8680 frame_garbaged = 0;
8681 if (changed_count)
8682 ++windows_or_buffers_changed;
8687 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8688 is non-zero update selected_frame. Value is non-zero if the
8689 mini-windows height has been changed. */
8691 static int
8692 echo_area_display (update_frame_p)
8693 int update_frame_p;
8695 Lisp_Object mini_window;
8696 struct window *w;
8697 struct frame *f;
8698 int window_height_changed_p = 0;
8699 struct frame *sf = SELECTED_FRAME ();
8701 mini_window = FRAME_MINIBUF_WINDOW (sf);
8702 w = XWINDOW (mini_window);
8703 f = XFRAME (WINDOW_FRAME (w));
8705 /* Don't display if frame is invisible or not yet initialized. */
8706 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8707 return 0;
8709 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8710 #ifndef MAC_OS8
8711 #ifdef HAVE_WINDOW_SYSTEM
8712 /* When Emacs starts, selected_frame may be a visible terminal
8713 frame, even if we run under a window system. If we let this
8714 through, a message would be displayed on the terminal. */
8715 if (EQ (selected_frame, Vterminal_frame)
8716 && !NILP (Vwindow_system))
8717 return 0;
8718 #endif /* HAVE_WINDOW_SYSTEM */
8719 #endif
8721 /* Redraw garbaged frames. */
8722 if (frame_garbaged)
8723 clear_garbaged_frames ();
8725 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8727 echo_area_window = mini_window;
8728 window_height_changed_p = display_echo_area (w);
8729 w->must_be_updated_p = 1;
8731 /* Update the display, unless called from redisplay_internal.
8732 Also don't update the screen during redisplay itself. The
8733 update will happen at the end of redisplay, and an update
8734 here could cause confusion. */
8735 if (update_frame_p && !redisplaying_p)
8737 int n = 0;
8739 /* If the display update has been interrupted by pending
8740 input, update mode lines in the frame. Due to the
8741 pending input, it might have been that redisplay hasn't
8742 been called, so that mode lines above the echo area are
8743 garbaged. This looks odd, so we prevent it here. */
8744 if (!display_completed)
8745 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8747 if (window_height_changed_p
8748 /* Don't do this if Emacs is shutting down. Redisplay
8749 needs to run hooks. */
8750 && !NILP (Vrun_hooks))
8752 /* Must update other windows. Likewise as in other
8753 cases, don't let this update be interrupted by
8754 pending input. */
8755 int count = SPECPDL_INDEX ();
8756 specbind (Qredisplay_dont_pause, Qt);
8757 windows_or_buffers_changed = 1;
8758 redisplay_internal (0);
8759 unbind_to (count, Qnil);
8761 else if (FRAME_WINDOW_P (f) && n == 0)
8763 /* Window configuration is the same as before.
8764 Can do with a display update of the echo area,
8765 unless we displayed some mode lines. */
8766 update_single_window (w, 1);
8767 rif->flush_display (f);
8769 else
8770 update_frame (f, 1, 1);
8772 /* If cursor is in the echo area, make sure that the next
8773 redisplay displays the minibuffer, so that the cursor will
8774 be replaced with what the minibuffer wants. */
8775 if (cursor_in_echo_area)
8776 ++windows_or_buffers_changed;
8779 else if (!EQ (mini_window, selected_window))
8780 windows_or_buffers_changed++;
8782 /* The current message is now also the last one displayed. */
8783 echo_area_buffer[1] = echo_area_buffer[0];
8785 /* Prevent redisplay optimization in redisplay_internal by resetting
8786 this_line_start_pos. This is done because the mini-buffer now
8787 displays the message instead of its buffer text. */
8788 if (EQ (mini_window, selected_window))
8789 CHARPOS (this_line_start_pos) = 0;
8791 return window_height_changed_p;
8796 /***********************************************************************
8797 Mode Lines and Frame Titles
8798 ***********************************************************************/
8800 /* A buffer for constructing non-propertized mode-line strings and
8801 frame titles in it; allocated from the heap in init_xdisp and
8802 resized as needed in store_mode_line_noprop_char. */
8804 static char *mode_line_noprop_buf;
8806 /* The buffer's end, and a current output position in it. */
8808 static char *mode_line_noprop_buf_end;
8809 static char *mode_line_noprop_ptr;
8811 #define MODE_LINE_NOPROP_LEN(start) \
8812 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8814 static enum {
8815 MODE_LINE_DISPLAY = 0,
8816 MODE_LINE_TITLE,
8817 MODE_LINE_NOPROP,
8818 MODE_LINE_STRING
8819 } mode_line_target;
8821 /* Alist that caches the results of :propertize.
8822 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8823 static Lisp_Object mode_line_proptrans_alist;
8825 /* List of strings making up the mode-line. */
8826 static Lisp_Object mode_line_string_list;
8828 /* Base face property when building propertized mode line string. */
8829 static Lisp_Object mode_line_string_face;
8830 static Lisp_Object mode_line_string_face_prop;
8833 /* Unwind data for mode line strings */
8835 static Lisp_Object Vmode_line_unwind_vector;
8837 static Lisp_Object
8838 format_mode_line_unwind_data (obuf, save_proptrans)
8839 struct buffer *obuf;
8841 Lisp_Object vector;
8843 /* Reduce consing by keeping one vector in
8844 Vwith_echo_area_save_vector. */
8845 vector = Vmode_line_unwind_vector;
8846 Vmode_line_unwind_vector = Qnil;
8848 if (NILP (vector))
8849 vector = Fmake_vector (make_number (7), Qnil);
8851 AREF (vector, 0) = make_number (mode_line_target);
8852 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8853 AREF (vector, 2) = mode_line_string_list;
8854 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8855 AREF (vector, 4) = mode_line_string_face;
8856 AREF (vector, 5) = mode_line_string_face_prop;
8858 if (obuf)
8859 XSETBUFFER (AREF (vector, 6), obuf);
8860 else
8861 AREF (vector, 6) = Qnil;
8863 return vector;
8866 static Lisp_Object
8867 unwind_format_mode_line (vector)
8868 Lisp_Object vector;
8870 mode_line_target = XINT (AREF (vector, 0));
8871 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8872 mode_line_string_list = AREF (vector, 2);
8873 if (! EQ (AREF (vector, 3), Qt))
8874 mode_line_proptrans_alist = AREF (vector, 3);
8875 mode_line_string_face = AREF (vector, 4);
8876 mode_line_string_face_prop = AREF (vector, 5);
8878 if (!NILP (AREF (vector, 6)))
8880 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8881 AREF (vector, 6) = Qnil;
8884 Vmode_line_unwind_vector = vector;
8885 return Qnil;
8889 /* Store a single character C for the frame title in mode_line_noprop_buf.
8890 Re-allocate mode_line_noprop_buf if necessary. */
8892 static void
8893 #ifdef PROTOTYPES
8894 store_mode_line_noprop_char (char c)
8895 #else
8896 store_mode_line_noprop_char (c)
8897 char c;
8898 #endif
8900 /* If output position has reached the end of the allocated buffer,
8901 double the buffer's size. */
8902 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8904 int len = MODE_LINE_NOPROP_LEN (0);
8905 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8906 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8907 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8908 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8911 *mode_line_noprop_ptr++ = c;
8915 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8916 mode_line_noprop_ptr. STR is the string to store. Do not copy
8917 characters that yield more columns than PRECISION; PRECISION <= 0
8918 means copy the whole string. Pad with spaces until FIELD_WIDTH
8919 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8920 pad. Called from display_mode_element when it is used to build a
8921 frame title. */
8923 static int
8924 store_mode_line_noprop (str, field_width, precision)
8925 const unsigned char *str;
8926 int field_width, precision;
8928 int n = 0;
8929 int dummy, nbytes;
8931 /* Copy at most PRECISION chars from STR. */
8932 nbytes = strlen (str);
8933 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8934 while (nbytes--)
8935 store_mode_line_noprop_char (*str++);
8937 /* Fill up with spaces until FIELD_WIDTH reached. */
8938 while (field_width > 0
8939 && n < field_width)
8941 store_mode_line_noprop_char (' ');
8942 ++n;
8945 return n;
8948 /***********************************************************************
8949 Frame Titles
8950 ***********************************************************************/
8952 #ifdef HAVE_WINDOW_SYSTEM
8954 /* Set the title of FRAME, if it has changed. The title format is
8955 Vicon_title_format if FRAME is iconified, otherwise it is
8956 frame_title_format. */
8958 static void
8959 x_consider_frame_title (frame)
8960 Lisp_Object frame;
8962 struct frame *f = XFRAME (frame);
8964 if (FRAME_WINDOW_P (f)
8965 || FRAME_MINIBUF_ONLY_P (f)
8966 || f->explicit_name)
8968 /* Do we have more than one visible frame on this X display? */
8969 Lisp_Object tail;
8970 Lisp_Object fmt;
8971 int title_start;
8972 char *title;
8973 int len;
8974 struct it it;
8975 int count = SPECPDL_INDEX ();
8977 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8979 Lisp_Object other_frame = XCAR (tail);
8980 struct frame *tf = XFRAME (other_frame);
8982 if (tf != f
8983 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8984 && !FRAME_MINIBUF_ONLY_P (tf)
8985 && !EQ (other_frame, tip_frame)
8986 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8987 break;
8990 /* Set global variable indicating that multiple frames exist. */
8991 multiple_frames = CONSP (tail);
8993 /* Switch to the buffer of selected window of the frame. Set up
8994 mode_line_target so that display_mode_element will output into
8995 mode_line_noprop_buf; then display the title. */
8996 record_unwind_protect (unwind_format_mode_line,
8997 format_mode_line_unwind_data (current_buffer, 0));
8999 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9000 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9002 mode_line_target = MODE_LINE_TITLE;
9003 title_start = MODE_LINE_NOPROP_LEN (0);
9004 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9005 NULL, DEFAULT_FACE_ID);
9006 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9007 len = MODE_LINE_NOPROP_LEN (title_start);
9008 title = mode_line_noprop_buf + title_start;
9009 unbind_to (count, Qnil);
9011 /* Set the title only if it's changed. This avoids consing in
9012 the common case where it hasn't. (If it turns out that we've
9013 already wasted too much time by walking through the list with
9014 display_mode_element, then we might need to optimize at a
9015 higher level than this.) */
9016 if (! STRINGP (f->name)
9017 || SBYTES (f->name) != len
9018 || bcmp (title, SDATA (f->name), len) != 0)
9019 x_implicitly_set_name (f, make_string (title, len), Qnil);
9023 #endif /* not HAVE_WINDOW_SYSTEM */
9028 /***********************************************************************
9029 Menu Bars
9030 ***********************************************************************/
9033 /* Prepare for redisplay by updating menu-bar item lists when
9034 appropriate. This can call eval. */
9036 void
9037 prepare_menu_bars ()
9039 int all_windows;
9040 struct gcpro gcpro1, gcpro2;
9041 struct frame *f;
9042 Lisp_Object tooltip_frame;
9044 #ifdef HAVE_WINDOW_SYSTEM
9045 tooltip_frame = tip_frame;
9046 #else
9047 tooltip_frame = Qnil;
9048 #endif
9050 /* Update all frame titles based on their buffer names, etc. We do
9051 this before the menu bars so that the buffer-menu will show the
9052 up-to-date frame titles. */
9053 #ifdef HAVE_WINDOW_SYSTEM
9054 if (windows_or_buffers_changed || update_mode_lines)
9056 Lisp_Object tail, frame;
9058 FOR_EACH_FRAME (tail, frame)
9060 f = XFRAME (frame);
9061 if (!EQ (frame, tooltip_frame)
9062 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9063 x_consider_frame_title (frame);
9066 #endif /* HAVE_WINDOW_SYSTEM */
9068 /* Update the menu bar item lists, if appropriate. This has to be
9069 done before any actual redisplay or generation of display lines. */
9070 all_windows = (update_mode_lines
9071 || buffer_shared > 1
9072 || windows_or_buffers_changed);
9073 if (all_windows)
9075 Lisp_Object tail, frame;
9076 int count = SPECPDL_INDEX ();
9077 /* 1 means that update_menu_bar has run its hooks
9078 so any further calls to update_menu_bar shouldn't do so again. */
9079 int menu_bar_hooks_run = 0;
9081 record_unwind_save_match_data ();
9083 FOR_EACH_FRAME (tail, frame)
9085 f = XFRAME (frame);
9087 /* Ignore tooltip frame. */
9088 if (EQ (frame, tooltip_frame))
9089 continue;
9091 /* If a window on this frame changed size, report that to
9092 the user and clear the size-change flag. */
9093 if (FRAME_WINDOW_SIZES_CHANGED (f))
9095 Lisp_Object functions;
9097 /* Clear flag first in case we get an error below. */
9098 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9099 functions = Vwindow_size_change_functions;
9100 GCPRO2 (tail, functions);
9102 while (CONSP (functions))
9104 call1 (XCAR (functions), frame);
9105 functions = XCDR (functions);
9107 UNGCPRO;
9110 GCPRO1 (tail);
9111 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9112 #ifdef HAVE_WINDOW_SYSTEM
9113 update_tool_bar (f, 0);
9114 #ifdef MAC_OS
9115 mac_update_title_bar (f, 0);
9116 #endif
9117 #endif
9118 UNGCPRO;
9121 unbind_to (count, Qnil);
9123 else
9125 struct frame *sf = SELECTED_FRAME ();
9126 update_menu_bar (sf, 1, 0);
9127 #ifdef HAVE_WINDOW_SYSTEM
9128 update_tool_bar (sf, 1);
9129 #ifdef MAC_OS
9130 mac_update_title_bar (sf, 1);
9131 #endif
9132 #endif
9135 /* Motif needs this. See comment in xmenu.c. Turn it off when
9136 pending_menu_activation is not defined. */
9137 #ifdef USE_X_TOOLKIT
9138 pending_menu_activation = 0;
9139 #endif
9143 /* Update the menu bar item list for frame F. This has to be done
9144 before we start to fill in any display lines, because it can call
9145 eval.
9147 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9149 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9150 already ran the menu bar hooks for this redisplay, so there
9151 is no need to run them again. The return value is the
9152 updated value of this flag, to pass to the next call. */
9154 static int
9155 update_menu_bar (f, save_match_data, hooks_run)
9156 struct frame *f;
9157 int save_match_data;
9158 int hooks_run;
9160 Lisp_Object window;
9161 register struct window *w;
9163 /* If called recursively during a menu update, do nothing. This can
9164 happen when, for instance, an activate-menubar-hook causes a
9165 redisplay. */
9166 if (inhibit_menubar_update)
9167 return hooks_run;
9169 window = FRAME_SELECTED_WINDOW (f);
9170 w = XWINDOW (window);
9172 #if 0 /* The if statement below this if statement used to include the
9173 condition !NILP (w->update_mode_line), rather than using
9174 update_mode_lines directly, and this if statement may have
9175 been added to make that condition work. Now the if
9176 statement below matches its comment, this isn't needed. */
9177 if (update_mode_lines)
9178 w->update_mode_line = Qt;
9179 #endif
9181 if (FRAME_WINDOW_P (f)
9183 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9184 || defined (USE_GTK)
9185 FRAME_EXTERNAL_MENU_BAR (f)
9186 #else
9187 FRAME_MENU_BAR_LINES (f) > 0
9188 #endif
9189 : FRAME_MENU_BAR_LINES (f) > 0)
9191 /* If the user has switched buffers or windows, we need to
9192 recompute to reflect the new bindings. But we'll
9193 recompute when update_mode_lines is set too; that means
9194 that people can use force-mode-line-update to request
9195 that the menu bar be recomputed. The adverse effect on
9196 the rest of the redisplay algorithm is about the same as
9197 windows_or_buffers_changed anyway. */
9198 if (windows_or_buffers_changed
9199 /* This used to test w->update_mode_line, but we believe
9200 there is no need to recompute the menu in that case. */
9201 || update_mode_lines
9202 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9203 < BUF_MODIFF (XBUFFER (w->buffer)))
9204 != !NILP (w->last_had_star))
9205 || ((!NILP (Vtransient_mark_mode)
9206 && !NILP (XBUFFER (w->buffer)->mark_active))
9207 != !NILP (w->region_showing)))
9209 struct buffer *prev = current_buffer;
9210 int count = SPECPDL_INDEX ();
9212 specbind (Qinhibit_menubar_update, Qt);
9214 set_buffer_internal_1 (XBUFFER (w->buffer));
9215 if (save_match_data)
9216 record_unwind_save_match_data ();
9217 if (NILP (Voverriding_local_map_menu_flag))
9219 specbind (Qoverriding_terminal_local_map, Qnil);
9220 specbind (Qoverriding_local_map, Qnil);
9223 if (!hooks_run)
9225 /* Run the Lucid hook. */
9226 safe_run_hooks (Qactivate_menubar_hook);
9228 /* If it has changed current-menubar from previous value,
9229 really recompute the menu-bar from the value. */
9230 if (! NILP (Vlucid_menu_bar_dirty_flag))
9231 call0 (Qrecompute_lucid_menubar);
9233 safe_run_hooks (Qmenu_bar_update_hook);
9235 hooks_run = 1;
9238 XSETFRAME (Vmenu_updating_frame, f);
9239 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9241 /* Redisplay the menu bar in case we changed it. */
9242 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9243 || defined (USE_GTK)
9244 if (FRAME_WINDOW_P (f))
9246 #ifdef MAC_OS
9247 /* All frames on Mac OS share the same menubar. So only
9248 the selected frame should be allowed to set it. */
9249 if (f == SELECTED_FRAME ())
9250 #endif
9251 set_frame_menubar (f, 0, 0);
9253 else
9254 /* On a terminal screen, the menu bar is an ordinary screen
9255 line, and this makes it get updated. */
9256 w->update_mode_line = Qt;
9257 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9258 /* In the non-toolkit version, the menu bar is an ordinary screen
9259 line, and this makes it get updated. */
9260 w->update_mode_line = Qt;
9261 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9263 unbind_to (count, Qnil);
9264 set_buffer_internal_1 (prev);
9268 return hooks_run;
9273 /***********************************************************************
9274 Output Cursor
9275 ***********************************************************************/
9277 #ifdef HAVE_WINDOW_SYSTEM
9279 /* EXPORT:
9280 Nominal cursor position -- where to draw output.
9281 HPOS and VPOS are window relative glyph matrix coordinates.
9282 X and Y are window relative pixel coordinates. */
9284 struct cursor_pos output_cursor;
9287 /* EXPORT:
9288 Set the global variable output_cursor to CURSOR. All cursor
9289 positions are relative to updated_window. */
9291 void
9292 set_output_cursor (cursor)
9293 struct cursor_pos *cursor;
9295 output_cursor.hpos = cursor->hpos;
9296 output_cursor.vpos = cursor->vpos;
9297 output_cursor.x = cursor->x;
9298 output_cursor.y = cursor->y;
9302 /* EXPORT for RIF:
9303 Set a nominal cursor position.
9305 HPOS and VPOS are column/row positions in a window glyph matrix. X
9306 and Y are window text area relative pixel positions.
9308 If this is done during an update, updated_window will contain the
9309 window that is being updated and the position is the future output
9310 cursor position for that window. If updated_window is null, use
9311 selected_window and display the cursor at the given position. */
9313 void
9314 x_cursor_to (vpos, hpos, y, x)
9315 int vpos, hpos, y, x;
9317 struct window *w;
9319 /* If updated_window is not set, work on selected_window. */
9320 if (updated_window)
9321 w = updated_window;
9322 else
9323 w = XWINDOW (selected_window);
9325 /* Set the output cursor. */
9326 output_cursor.hpos = hpos;
9327 output_cursor.vpos = vpos;
9328 output_cursor.x = x;
9329 output_cursor.y = y;
9331 /* If not called as part of an update, really display the cursor.
9332 This will also set the cursor position of W. */
9333 if (updated_window == NULL)
9335 BLOCK_INPUT;
9336 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9337 if (rif->flush_display_optional)
9338 rif->flush_display_optional (SELECTED_FRAME ());
9339 UNBLOCK_INPUT;
9343 #endif /* HAVE_WINDOW_SYSTEM */
9346 /***********************************************************************
9347 Tool-bars
9348 ***********************************************************************/
9350 #ifdef HAVE_WINDOW_SYSTEM
9352 /* Where the mouse was last time we reported a mouse event. */
9354 FRAME_PTR last_mouse_frame;
9356 /* Tool-bar item index of the item on which a mouse button was pressed
9357 or -1. */
9359 int last_tool_bar_item;
9362 /* Update the tool-bar item list for frame F. This has to be done
9363 before we start to fill in any display lines. Called from
9364 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9365 and restore it here. */
9367 static void
9368 update_tool_bar (f, save_match_data)
9369 struct frame *f;
9370 int save_match_data;
9372 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9373 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9374 #else
9375 int do_update = WINDOWP (f->tool_bar_window)
9376 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9377 #endif
9379 if (do_update)
9381 Lisp_Object window;
9382 struct window *w;
9384 window = FRAME_SELECTED_WINDOW (f);
9385 w = XWINDOW (window);
9387 /* If the user has switched buffers or windows, we need to
9388 recompute to reflect the new bindings. But we'll
9389 recompute when update_mode_lines is set too; that means
9390 that people can use force-mode-line-update to request
9391 that the menu bar be recomputed. The adverse effect on
9392 the rest of the redisplay algorithm is about the same as
9393 windows_or_buffers_changed anyway. */
9394 if (windows_or_buffers_changed
9395 || !NILP (w->update_mode_line)
9396 || update_mode_lines
9397 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9398 < BUF_MODIFF (XBUFFER (w->buffer)))
9399 != !NILP (w->last_had_star))
9400 || ((!NILP (Vtransient_mark_mode)
9401 && !NILP (XBUFFER (w->buffer)->mark_active))
9402 != !NILP (w->region_showing)))
9404 struct buffer *prev = current_buffer;
9405 int count = SPECPDL_INDEX ();
9406 Lisp_Object new_tool_bar;
9407 int new_n_tool_bar;
9408 struct gcpro gcpro1;
9410 /* Set current_buffer to the buffer of the selected
9411 window of the frame, so that we get the right local
9412 keymaps. */
9413 set_buffer_internal_1 (XBUFFER (w->buffer));
9415 /* Save match data, if we must. */
9416 if (save_match_data)
9417 record_unwind_save_match_data ();
9419 /* Make sure that we don't accidentally use bogus keymaps. */
9420 if (NILP (Voverriding_local_map_menu_flag))
9422 specbind (Qoverriding_terminal_local_map, Qnil);
9423 specbind (Qoverriding_local_map, Qnil);
9426 GCPRO1 (new_tool_bar);
9428 /* Build desired tool-bar items from keymaps. */
9429 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9430 &new_n_tool_bar);
9432 /* Redisplay the tool-bar if we changed it. */
9433 if (new_n_tool_bar != f->n_tool_bar_items
9434 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9436 /* Redisplay that happens asynchronously due to an expose event
9437 may access f->tool_bar_items. Make sure we update both
9438 variables within BLOCK_INPUT so no such event interrupts. */
9439 BLOCK_INPUT;
9440 f->tool_bar_items = new_tool_bar;
9441 f->n_tool_bar_items = new_n_tool_bar;
9442 w->update_mode_line = Qt;
9443 UNBLOCK_INPUT;
9446 UNGCPRO;
9448 unbind_to (count, Qnil);
9449 set_buffer_internal_1 (prev);
9455 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9456 F's desired tool-bar contents. F->tool_bar_items must have
9457 been set up previously by calling prepare_menu_bars. */
9459 static void
9460 build_desired_tool_bar_string (f)
9461 struct frame *f;
9463 int i, size, size_needed;
9464 struct gcpro gcpro1, gcpro2, gcpro3;
9465 Lisp_Object image, plist, props;
9467 image = plist = props = Qnil;
9468 GCPRO3 (image, plist, props);
9470 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9471 Otherwise, make a new string. */
9473 /* The size of the string we might be able to reuse. */
9474 size = (STRINGP (f->desired_tool_bar_string)
9475 ? SCHARS (f->desired_tool_bar_string)
9476 : 0);
9478 /* We need one space in the string for each image. */
9479 size_needed = f->n_tool_bar_items;
9481 /* Reuse f->desired_tool_bar_string, if possible. */
9482 if (size < size_needed || NILP (f->desired_tool_bar_string))
9483 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9484 make_number (' '));
9485 else
9487 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9488 Fremove_text_properties (make_number (0), make_number (size),
9489 props, f->desired_tool_bar_string);
9492 /* Put a `display' property on the string for the images to display,
9493 put a `menu_item' property on tool-bar items with a value that
9494 is the index of the item in F's tool-bar item vector. */
9495 for (i = 0; i < f->n_tool_bar_items; ++i)
9497 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9499 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9500 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9501 int hmargin, vmargin, relief, idx, end;
9502 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9504 /* If image is a vector, choose the image according to the
9505 button state. */
9506 image = PROP (TOOL_BAR_ITEM_IMAGES);
9507 if (VECTORP (image))
9509 if (enabled_p)
9510 idx = (selected_p
9511 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9512 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9513 else
9514 idx = (selected_p
9515 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9516 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9518 xassert (ASIZE (image) >= idx);
9519 image = AREF (image, idx);
9521 else
9522 idx = -1;
9524 /* Ignore invalid image specifications. */
9525 if (!valid_image_p (image))
9526 continue;
9528 /* Display the tool-bar button pressed, or depressed. */
9529 plist = Fcopy_sequence (XCDR (image));
9531 /* Compute margin and relief to draw. */
9532 relief = (tool_bar_button_relief >= 0
9533 ? tool_bar_button_relief
9534 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9535 hmargin = vmargin = relief;
9537 if (INTEGERP (Vtool_bar_button_margin)
9538 && XINT (Vtool_bar_button_margin) > 0)
9540 hmargin += XFASTINT (Vtool_bar_button_margin);
9541 vmargin += XFASTINT (Vtool_bar_button_margin);
9543 else if (CONSP (Vtool_bar_button_margin))
9545 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9546 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9547 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9549 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9550 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9551 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9554 if (auto_raise_tool_bar_buttons_p)
9556 /* Add a `:relief' property to the image spec if the item is
9557 selected. */
9558 if (selected_p)
9560 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9561 hmargin -= relief;
9562 vmargin -= relief;
9565 else
9567 /* If image is selected, display it pressed, i.e. with a
9568 negative relief. If it's not selected, display it with a
9569 raised relief. */
9570 plist = Fplist_put (plist, QCrelief,
9571 (selected_p
9572 ? make_number (-relief)
9573 : make_number (relief)));
9574 hmargin -= relief;
9575 vmargin -= relief;
9578 /* Put a margin around the image. */
9579 if (hmargin || vmargin)
9581 if (hmargin == vmargin)
9582 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9583 else
9584 plist = Fplist_put (plist, QCmargin,
9585 Fcons (make_number (hmargin),
9586 make_number (vmargin)));
9589 /* If button is not enabled, and we don't have special images
9590 for the disabled state, make the image appear disabled by
9591 applying an appropriate algorithm to it. */
9592 if (!enabled_p && idx < 0)
9593 plist = Fplist_put (plist, QCconversion, Qdisabled);
9595 /* Put a `display' text property on the string for the image to
9596 display. Put a `menu-item' property on the string that gives
9597 the start of this item's properties in the tool-bar items
9598 vector. */
9599 image = Fcons (Qimage, plist);
9600 props = list4 (Qdisplay, image,
9601 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9603 /* Let the last image hide all remaining spaces in the tool bar
9604 string. The string can be longer than needed when we reuse a
9605 previous string. */
9606 if (i + 1 == f->n_tool_bar_items)
9607 end = SCHARS (f->desired_tool_bar_string);
9608 else
9609 end = i + 1;
9610 Fadd_text_properties (make_number (i), make_number (end),
9611 props, f->desired_tool_bar_string);
9612 #undef PROP
9615 UNGCPRO;
9619 /* Display one line of the tool-bar of frame IT->f.
9621 HEIGHT specifies the desired height of the tool-bar line.
9622 If the actual height of the glyph row is less than HEIGHT, the
9623 row's height is increased to HEIGHT, and the icons are centered
9624 vertically in the new height.
9626 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9627 count a final empty row in case the tool-bar width exactly matches
9628 the window width.
9631 static void
9632 display_tool_bar_line (it, height)
9633 struct it *it;
9634 int height;
9636 struct glyph_row *row = it->glyph_row;
9637 int max_x = it->last_visible_x;
9638 struct glyph *last;
9640 prepare_desired_row (row);
9641 row->y = it->current_y;
9643 /* Note that this isn't made use of if the face hasn't a box,
9644 so there's no need to check the face here. */
9645 it->start_of_box_run_p = 1;
9647 while (it->current_x < max_x)
9649 int x, n_glyphs_before, i, nglyphs;
9650 struct it it_before;
9652 /* Get the next display element. */
9653 if (!get_next_display_element (it))
9655 /* Don't count empty row if we are counting needed tool-bar lines. */
9656 if (height < 0 && !it->hpos)
9657 return;
9658 break;
9661 /* Produce glyphs. */
9662 n_glyphs_before = row->used[TEXT_AREA];
9663 it_before = *it;
9665 PRODUCE_GLYPHS (it);
9667 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9668 i = 0;
9669 x = it_before.current_x;
9670 while (i < nglyphs)
9672 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9674 if (x + glyph->pixel_width > max_x)
9676 /* Glyph doesn't fit on line. Backtrack. */
9677 row->used[TEXT_AREA] = n_glyphs_before;
9678 *it = it_before;
9679 /* If this is the only glyph on this line, it will never fit on the
9680 toolbar, so skip it. But ensure there is at least one glyph,
9681 so we don't accidentally disable the tool-bar. */
9682 if (n_glyphs_before == 0
9683 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9684 break;
9685 goto out;
9688 ++it->hpos;
9689 x += glyph->pixel_width;
9690 ++i;
9693 /* Stop at line ends. */
9694 if (ITERATOR_AT_END_OF_LINE_P (it))
9695 break;
9697 set_iterator_to_next (it, 1);
9700 out:;
9702 row->displays_text_p = row->used[TEXT_AREA] != 0;
9704 /* Use default face for the border below the tool bar.
9706 FIXME: When auto-resize-tool-bars is grow-only, there is
9707 no additional border below the possibly empty tool-bar lines.
9708 So to make the extra empty lines look "normal", we have to
9709 use the tool-bar face for the border too. */
9710 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9711 it->face_id = DEFAULT_FACE_ID;
9713 extend_face_to_end_of_line (it);
9714 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9715 last->right_box_line_p = 1;
9716 if (last == row->glyphs[TEXT_AREA])
9717 last->left_box_line_p = 1;
9719 /* Make line the desired height and center it vertically. */
9720 if ((height -= it->max_ascent + it->max_descent) > 0)
9722 /* Don't add more than one line height. */
9723 height %= FRAME_LINE_HEIGHT (it->f);
9724 it->max_ascent += height / 2;
9725 it->max_descent += (height + 1) / 2;
9728 compute_line_metrics (it);
9730 /* If line is empty, make it occupy the rest of the tool-bar. */
9731 if (!row->displays_text_p)
9733 row->height = row->phys_height = it->last_visible_y - row->y;
9734 row->visible_height = row->height;
9735 row->ascent = row->phys_ascent = 0;
9736 row->extra_line_spacing = 0;
9739 row->full_width_p = 1;
9740 row->continued_p = 0;
9741 row->truncated_on_left_p = 0;
9742 row->truncated_on_right_p = 0;
9744 it->current_x = it->hpos = 0;
9745 it->current_y += row->height;
9746 ++it->vpos;
9747 ++it->glyph_row;
9751 /* Max tool-bar height. */
9753 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9754 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9756 /* Value is the number of screen lines needed to make all tool-bar
9757 items of frame F visible. The number of actual rows needed is
9758 returned in *N_ROWS if non-NULL. */
9760 static int
9761 tool_bar_lines_needed (f, n_rows)
9762 struct frame *f;
9763 int *n_rows;
9765 struct window *w = XWINDOW (f->tool_bar_window);
9766 struct it it;
9767 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9768 the desired matrix, so use (unused) mode-line row as temporary row to
9769 avoid destroying the first tool-bar row. */
9770 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9772 /* Initialize an iterator for iteration over
9773 F->desired_tool_bar_string in the tool-bar window of frame F. */
9774 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9775 it.first_visible_x = 0;
9776 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9777 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9779 while (!ITERATOR_AT_END_P (&it))
9781 clear_glyph_row (temp_row);
9782 it.glyph_row = temp_row;
9783 display_tool_bar_line (&it, -1);
9785 clear_glyph_row (temp_row);
9787 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9788 if (n_rows)
9789 *n_rows = it.vpos > 0 ? it.vpos : -1;
9791 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9795 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9796 0, 1, 0,
9797 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9798 (frame)
9799 Lisp_Object frame;
9801 struct frame *f;
9802 struct window *w;
9803 int nlines = 0;
9805 if (NILP (frame))
9806 frame = selected_frame;
9807 else
9808 CHECK_FRAME (frame);
9809 f = XFRAME (frame);
9811 if (WINDOWP (f->tool_bar_window)
9812 || (w = XWINDOW (f->tool_bar_window),
9813 WINDOW_TOTAL_LINES (w) > 0))
9815 update_tool_bar (f, 1);
9816 if (f->n_tool_bar_items)
9818 build_desired_tool_bar_string (f);
9819 nlines = tool_bar_lines_needed (f, NULL);
9823 return make_number (nlines);
9827 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9828 height should be changed. */
9830 static int
9831 redisplay_tool_bar (f)
9832 struct frame *f;
9834 struct window *w;
9835 struct it it;
9836 struct glyph_row *row;
9838 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9839 if (FRAME_EXTERNAL_TOOL_BAR (f))
9840 update_frame_tool_bar (f);
9841 return 0;
9842 #endif
9844 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9845 do anything. This means you must start with tool-bar-lines
9846 non-zero to get the auto-sizing effect. Or in other words, you
9847 can turn off tool-bars by specifying tool-bar-lines zero. */
9848 if (!WINDOWP (f->tool_bar_window)
9849 || (w = XWINDOW (f->tool_bar_window),
9850 WINDOW_TOTAL_LINES (w) == 0))
9851 return 0;
9853 /* Set up an iterator for the tool-bar window. */
9854 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9855 it.first_visible_x = 0;
9856 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9857 row = it.glyph_row;
9859 /* Build a string that represents the contents of the tool-bar. */
9860 build_desired_tool_bar_string (f);
9861 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9863 if (f->n_tool_bar_rows == 0)
9865 int nlines;
9867 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9868 nlines != WINDOW_TOTAL_LINES (w)))
9870 extern Lisp_Object Qtool_bar_lines;
9871 Lisp_Object frame;
9872 int old_height = WINDOW_TOTAL_LINES (w);
9874 XSETFRAME (frame, f);
9875 Fmodify_frame_parameters (frame,
9876 Fcons (Fcons (Qtool_bar_lines,
9877 make_number (nlines)),
9878 Qnil));
9879 if (WINDOW_TOTAL_LINES (w) != old_height)
9881 clear_glyph_matrix (w->desired_matrix);
9882 fonts_changed_p = 1;
9883 return 1;
9888 /* Display as many lines as needed to display all tool-bar items. */
9890 if (f->n_tool_bar_rows > 0)
9892 int border, rows, height, extra;
9894 if (INTEGERP (Vtool_bar_border))
9895 border = XINT (Vtool_bar_border);
9896 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9897 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9898 else if (EQ (Vtool_bar_border, Qborder_width))
9899 border = f->border_width;
9900 else
9901 border = 0;
9902 if (border < 0)
9903 border = 0;
9905 rows = f->n_tool_bar_rows;
9906 height = max (1, (it.last_visible_y - border) / rows);
9907 extra = it.last_visible_y - border - height * rows;
9909 while (it.current_y < it.last_visible_y)
9911 int h = 0;
9912 if (extra > 0 && rows-- > 0)
9914 h = (extra + rows - 1) / rows;
9915 extra -= h;
9917 display_tool_bar_line (&it, height + h);
9920 else
9922 while (it.current_y < it.last_visible_y)
9923 display_tool_bar_line (&it, 0);
9926 /* It doesn't make much sense to try scrolling in the tool-bar
9927 window, so don't do it. */
9928 w->desired_matrix->no_scrolling_p = 1;
9929 w->must_be_updated_p = 1;
9931 if (!NILP (Vauto_resize_tool_bars))
9933 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9934 int change_height_p = 0;
9936 /* If we couldn't display everything, change the tool-bar's
9937 height if there is room for more. */
9938 if (IT_STRING_CHARPOS (it) < it.end_charpos
9939 && it.current_y < max_tool_bar_height)
9940 change_height_p = 1;
9942 row = it.glyph_row - 1;
9944 /* If there are blank lines at the end, except for a partially
9945 visible blank line at the end that is smaller than
9946 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9947 if (!row->displays_text_p
9948 && row->height >= FRAME_LINE_HEIGHT (f))
9949 change_height_p = 1;
9951 /* If row displays tool-bar items, but is partially visible,
9952 change the tool-bar's height. */
9953 if (row->displays_text_p
9954 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9955 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9956 change_height_p = 1;
9958 /* Resize windows as needed by changing the `tool-bar-lines'
9959 frame parameter. */
9960 if (change_height_p)
9962 extern Lisp_Object Qtool_bar_lines;
9963 Lisp_Object frame;
9964 int old_height = WINDOW_TOTAL_LINES (w);
9965 int nrows;
9966 int nlines = tool_bar_lines_needed (f, &nrows);
9968 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
9969 && !f->minimize_tool_bar_window_p)
9970 ? (nlines > old_height)
9971 : (nlines != old_height));
9972 f->minimize_tool_bar_window_p = 0;
9974 if (change_height_p)
9976 XSETFRAME (frame, f);
9977 Fmodify_frame_parameters (frame,
9978 Fcons (Fcons (Qtool_bar_lines,
9979 make_number (nlines)),
9980 Qnil));
9981 if (WINDOW_TOTAL_LINES (w) != old_height)
9983 clear_glyph_matrix (w->desired_matrix);
9984 f->n_tool_bar_rows = nrows;
9985 fonts_changed_p = 1;
9986 return 1;
9992 f->minimize_tool_bar_window_p = 0;
9993 return 0;
9997 /* Get information about the tool-bar item which is displayed in GLYPH
9998 on frame F. Return in *PROP_IDX the index where tool-bar item
9999 properties start in F->tool_bar_items. Value is zero if
10000 GLYPH doesn't display a tool-bar item. */
10002 static int
10003 tool_bar_item_info (f, glyph, prop_idx)
10004 struct frame *f;
10005 struct glyph *glyph;
10006 int *prop_idx;
10008 Lisp_Object prop;
10009 int success_p;
10010 int charpos;
10012 /* This function can be called asynchronously, which means we must
10013 exclude any possibility that Fget_text_property signals an
10014 error. */
10015 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10016 charpos = max (0, charpos);
10018 /* Get the text property `menu-item' at pos. The value of that
10019 property is the start index of this item's properties in
10020 F->tool_bar_items. */
10021 prop = Fget_text_property (make_number (charpos),
10022 Qmenu_item, f->current_tool_bar_string);
10023 if (INTEGERP (prop))
10025 *prop_idx = XINT (prop);
10026 success_p = 1;
10028 else
10029 success_p = 0;
10031 return success_p;
10035 /* Get information about the tool-bar item at position X/Y on frame F.
10036 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10037 the current matrix of the tool-bar window of F, or NULL if not
10038 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10039 item in F->tool_bar_items. Value is
10041 -1 if X/Y is not on a tool-bar item
10042 0 if X/Y is on the same item that was highlighted before.
10043 1 otherwise. */
10045 static int
10046 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10047 struct frame *f;
10048 int x, y;
10049 struct glyph **glyph;
10050 int *hpos, *vpos, *prop_idx;
10052 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10053 struct window *w = XWINDOW (f->tool_bar_window);
10054 int area;
10056 /* Find the glyph under X/Y. */
10057 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10058 if (*glyph == NULL)
10059 return -1;
10061 /* Get the start of this tool-bar item's properties in
10062 f->tool_bar_items. */
10063 if (!tool_bar_item_info (f, *glyph, prop_idx))
10064 return -1;
10066 /* Is mouse on the highlighted item? */
10067 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10068 && *vpos >= dpyinfo->mouse_face_beg_row
10069 && *vpos <= dpyinfo->mouse_face_end_row
10070 && (*vpos > dpyinfo->mouse_face_beg_row
10071 || *hpos >= dpyinfo->mouse_face_beg_col)
10072 && (*vpos < dpyinfo->mouse_face_end_row
10073 || *hpos < dpyinfo->mouse_face_end_col
10074 || dpyinfo->mouse_face_past_end))
10075 return 0;
10077 return 1;
10081 /* EXPORT:
10082 Handle mouse button event on the tool-bar of frame F, at
10083 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10084 0 for button release. MODIFIERS is event modifiers for button
10085 release. */
10087 void
10088 handle_tool_bar_click (f, x, y, down_p, modifiers)
10089 struct frame *f;
10090 int x, y, down_p;
10091 unsigned int modifiers;
10093 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10094 struct window *w = XWINDOW (f->tool_bar_window);
10095 int hpos, vpos, prop_idx;
10096 struct glyph *glyph;
10097 Lisp_Object enabled_p;
10099 /* If not on the highlighted tool-bar item, return. */
10100 frame_to_window_pixel_xy (w, &x, &y);
10101 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10102 return;
10104 /* If item is disabled, do nothing. */
10105 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10106 if (NILP (enabled_p))
10107 return;
10109 if (down_p)
10111 /* Show item in pressed state. */
10112 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10113 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10114 last_tool_bar_item = prop_idx;
10116 else
10118 Lisp_Object key, frame;
10119 struct input_event event;
10120 EVENT_INIT (event);
10122 /* Show item in released state. */
10123 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10124 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10126 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10128 XSETFRAME (frame, f);
10129 event.kind = TOOL_BAR_EVENT;
10130 event.frame_or_window = frame;
10131 event.arg = frame;
10132 kbd_buffer_store_event (&event);
10134 event.kind = TOOL_BAR_EVENT;
10135 event.frame_or_window = frame;
10136 event.arg = key;
10137 event.modifiers = modifiers;
10138 kbd_buffer_store_event (&event);
10139 last_tool_bar_item = -1;
10144 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10145 tool-bar window-relative coordinates X/Y. Called from
10146 note_mouse_highlight. */
10148 static void
10149 note_tool_bar_highlight (f, x, y)
10150 struct frame *f;
10151 int x, y;
10153 Lisp_Object window = f->tool_bar_window;
10154 struct window *w = XWINDOW (window);
10155 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10156 int hpos, vpos;
10157 struct glyph *glyph;
10158 struct glyph_row *row;
10159 int i;
10160 Lisp_Object enabled_p;
10161 int prop_idx;
10162 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10163 int mouse_down_p, rc;
10165 /* Function note_mouse_highlight is called with negative x(y
10166 values when mouse moves outside of the frame. */
10167 if (x <= 0 || y <= 0)
10169 clear_mouse_face (dpyinfo);
10170 return;
10173 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10174 if (rc < 0)
10176 /* Not on tool-bar item. */
10177 clear_mouse_face (dpyinfo);
10178 return;
10180 else if (rc == 0)
10181 /* On same tool-bar item as before. */
10182 goto set_help_echo;
10184 clear_mouse_face (dpyinfo);
10186 /* Mouse is down, but on different tool-bar item? */
10187 mouse_down_p = (dpyinfo->grabbed
10188 && f == last_mouse_frame
10189 && FRAME_LIVE_P (f));
10190 if (mouse_down_p
10191 && last_tool_bar_item != prop_idx)
10192 return;
10194 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10195 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10197 /* If tool-bar item is not enabled, don't highlight it. */
10198 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10199 if (!NILP (enabled_p))
10201 /* Compute the x-position of the glyph. In front and past the
10202 image is a space. We include this in the highlighted area. */
10203 row = MATRIX_ROW (w->current_matrix, vpos);
10204 for (i = x = 0; i < hpos; ++i)
10205 x += row->glyphs[TEXT_AREA][i].pixel_width;
10207 /* Record this as the current active region. */
10208 dpyinfo->mouse_face_beg_col = hpos;
10209 dpyinfo->mouse_face_beg_row = vpos;
10210 dpyinfo->mouse_face_beg_x = x;
10211 dpyinfo->mouse_face_beg_y = row->y;
10212 dpyinfo->mouse_face_past_end = 0;
10214 dpyinfo->mouse_face_end_col = hpos + 1;
10215 dpyinfo->mouse_face_end_row = vpos;
10216 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10217 dpyinfo->mouse_face_end_y = row->y;
10218 dpyinfo->mouse_face_window = window;
10219 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10221 /* Display it as active. */
10222 show_mouse_face (dpyinfo, draw);
10223 dpyinfo->mouse_face_image_state = draw;
10226 set_help_echo:
10228 /* Set help_echo_string to a help string to display for this tool-bar item.
10229 XTread_socket does the rest. */
10230 help_echo_object = help_echo_window = Qnil;
10231 help_echo_pos = -1;
10232 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10233 if (NILP (help_echo_string))
10234 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10237 #endif /* HAVE_WINDOW_SYSTEM */
10241 /************************************************************************
10242 Horizontal scrolling
10243 ************************************************************************/
10245 static int hscroll_window_tree P_ ((Lisp_Object));
10246 static int hscroll_windows P_ ((Lisp_Object));
10248 /* For all leaf windows in the window tree rooted at WINDOW, set their
10249 hscroll value so that PT is (i) visible in the window, and (ii) so
10250 that it is not within a certain margin at the window's left and
10251 right border. Value is non-zero if any window's hscroll has been
10252 changed. */
10254 static int
10255 hscroll_window_tree (window)
10256 Lisp_Object window;
10258 int hscrolled_p = 0;
10259 int hscroll_relative_p = FLOATP (Vhscroll_step);
10260 int hscroll_step_abs = 0;
10261 double hscroll_step_rel = 0;
10263 if (hscroll_relative_p)
10265 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10266 if (hscroll_step_rel < 0)
10268 hscroll_relative_p = 0;
10269 hscroll_step_abs = 0;
10272 else if (INTEGERP (Vhscroll_step))
10274 hscroll_step_abs = XINT (Vhscroll_step);
10275 if (hscroll_step_abs < 0)
10276 hscroll_step_abs = 0;
10278 else
10279 hscroll_step_abs = 0;
10281 while (WINDOWP (window))
10283 struct window *w = XWINDOW (window);
10285 if (WINDOWP (w->hchild))
10286 hscrolled_p |= hscroll_window_tree (w->hchild);
10287 else if (WINDOWP (w->vchild))
10288 hscrolled_p |= hscroll_window_tree (w->vchild);
10289 else if (w->cursor.vpos >= 0)
10291 int h_margin;
10292 int text_area_width;
10293 struct glyph_row *current_cursor_row
10294 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10295 struct glyph_row *desired_cursor_row
10296 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10297 struct glyph_row *cursor_row
10298 = (desired_cursor_row->enabled_p
10299 ? desired_cursor_row
10300 : current_cursor_row);
10302 text_area_width = window_box_width (w, TEXT_AREA);
10304 /* Scroll when cursor is inside this scroll margin. */
10305 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10307 if ((XFASTINT (w->hscroll)
10308 && w->cursor.x <= h_margin)
10309 || (cursor_row->enabled_p
10310 && cursor_row->truncated_on_right_p
10311 && (w->cursor.x >= text_area_width - h_margin)))
10313 struct it it;
10314 int hscroll;
10315 struct buffer *saved_current_buffer;
10316 int pt;
10317 int wanted_x;
10319 /* Find point in a display of infinite width. */
10320 saved_current_buffer = current_buffer;
10321 current_buffer = XBUFFER (w->buffer);
10323 if (w == XWINDOW (selected_window))
10324 pt = BUF_PT (current_buffer);
10325 else
10327 pt = marker_position (w->pointm);
10328 pt = max (BEGV, pt);
10329 pt = min (ZV, pt);
10332 /* Move iterator to pt starting at cursor_row->start in
10333 a line with infinite width. */
10334 init_to_row_start (&it, w, cursor_row);
10335 it.last_visible_x = INFINITY;
10336 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10337 current_buffer = saved_current_buffer;
10339 /* Position cursor in window. */
10340 if (!hscroll_relative_p && hscroll_step_abs == 0)
10341 hscroll = max (0, (it.current_x
10342 - (ITERATOR_AT_END_OF_LINE_P (&it)
10343 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10344 : (text_area_width / 2))))
10345 / FRAME_COLUMN_WIDTH (it.f);
10346 else if (w->cursor.x >= text_area_width - h_margin)
10348 if (hscroll_relative_p)
10349 wanted_x = text_area_width * (1 - hscroll_step_rel)
10350 - h_margin;
10351 else
10352 wanted_x = text_area_width
10353 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10354 - h_margin;
10355 hscroll
10356 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10358 else
10360 if (hscroll_relative_p)
10361 wanted_x = text_area_width * hscroll_step_rel
10362 + h_margin;
10363 else
10364 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10365 + h_margin;
10366 hscroll
10367 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10369 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10371 /* Don't call Fset_window_hscroll if value hasn't
10372 changed because it will prevent redisplay
10373 optimizations. */
10374 if (XFASTINT (w->hscroll) != hscroll)
10376 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10377 w->hscroll = make_number (hscroll);
10378 hscrolled_p = 1;
10383 window = w->next;
10386 /* Value is non-zero if hscroll of any leaf window has been changed. */
10387 return hscrolled_p;
10391 /* Set hscroll so that cursor is visible and not inside horizontal
10392 scroll margins for all windows in the tree rooted at WINDOW. See
10393 also hscroll_window_tree above. Value is non-zero if any window's
10394 hscroll has been changed. If it has, desired matrices on the frame
10395 of WINDOW are cleared. */
10397 static int
10398 hscroll_windows (window)
10399 Lisp_Object window;
10401 int hscrolled_p;
10403 if (automatic_hscrolling_p)
10405 hscrolled_p = hscroll_window_tree (window);
10406 if (hscrolled_p)
10407 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10409 else
10410 hscrolled_p = 0;
10411 return hscrolled_p;
10416 /************************************************************************
10417 Redisplay
10418 ************************************************************************/
10420 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10421 to a non-zero value. This is sometimes handy to have in a debugger
10422 session. */
10424 #if GLYPH_DEBUG
10426 /* First and last unchanged row for try_window_id. */
10428 int debug_first_unchanged_at_end_vpos;
10429 int debug_last_unchanged_at_beg_vpos;
10431 /* Delta vpos and y. */
10433 int debug_dvpos, debug_dy;
10435 /* Delta in characters and bytes for try_window_id. */
10437 int debug_delta, debug_delta_bytes;
10439 /* Values of window_end_pos and window_end_vpos at the end of
10440 try_window_id. */
10442 EMACS_INT debug_end_pos, debug_end_vpos;
10444 /* Append a string to W->desired_matrix->method. FMT is a printf
10445 format string. A1...A9 are a supplement for a variable-length
10446 argument list. If trace_redisplay_p is non-zero also printf the
10447 resulting string to stderr. */
10449 static void
10450 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10451 struct window *w;
10452 char *fmt;
10453 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10455 char buffer[512];
10456 char *method = w->desired_matrix->method;
10457 int len = strlen (method);
10458 int size = sizeof w->desired_matrix->method;
10459 int remaining = size - len - 1;
10461 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10462 if (len && remaining)
10464 method[len] = '|';
10465 --remaining, ++len;
10468 strncpy (method + len, buffer, remaining);
10470 if (trace_redisplay_p)
10471 fprintf (stderr, "%p (%s): %s\n",
10473 ((BUFFERP (w->buffer)
10474 && STRINGP (XBUFFER (w->buffer)->name))
10475 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10476 : "no buffer"),
10477 buffer);
10480 #endif /* GLYPH_DEBUG */
10483 /* Value is non-zero if all changes in window W, which displays
10484 current_buffer, are in the text between START and END. START is a
10485 buffer position, END is given as a distance from Z. Used in
10486 redisplay_internal for display optimization. */
10488 static INLINE int
10489 text_outside_line_unchanged_p (w, start, end)
10490 struct window *w;
10491 int start, end;
10493 int unchanged_p = 1;
10495 /* If text or overlays have changed, see where. */
10496 if (XFASTINT (w->last_modified) < MODIFF
10497 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10499 /* Gap in the line? */
10500 if (GPT < start || Z - GPT < end)
10501 unchanged_p = 0;
10503 /* Changes start in front of the line, or end after it? */
10504 if (unchanged_p
10505 && (BEG_UNCHANGED < start - 1
10506 || END_UNCHANGED < end))
10507 unchanged_p = 0;
10509 /* If selective display, can't optimize if changes start at the
10510 beginning of the line. */
10511 if (unchanged_p
10512 && INTEGERP (current_buffer->selective_display)
10513 && XINT (current_buffer->selective_display) > 0
10514 && (BEG_UNCHANGED < start || GPT <= start))
10515 unchanged_p = 0;
10517 /* If there are overlays at the start or end of the line, these
10518 may have overlay strings with newlines in them. A change at
10519 START, for instance, may actually concern the display of such
10520 overlay strings as well, and they are displayed on different
10521 lines. So, quickly rule out this case. (For the future, it
10522 might be desirable to implement something more telling than
10523 just BEG/END_UNCHANGED.) */
10524 if (unchanged_p)
10526 if (BEG + BEG_UNCHANGED == start
10527 && overlay_touches_p (start))
10528 unchanged_p = 0;
10529 if (END_UNCHANGED == end
10530 && overlay_touches_p (Z - end))
10531 unchanged_p = 0;
10535 return unchanged_p;
10539 /* Do a frame update, taking possible shortcuts into account. This is
10540 the main external entry point for redisplay.
10542 If the last redisplay displayed an echo area message and that message
10543 is no longer requested, we clear the echo area or bring back the
10544 mini-buffer if that is in use. */
10546 void
10547 redisplay ()
10549 redisplay_internal (0);
10553 static Lisp_Object
10554 overlay_arrow_string_or_property (var)
10555 Lisp_Object var;
10557 Lisp_Object val;
10559 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10560 return val;
10562 return Voverlay_arrow_string;
10565 /* Return 1 if there are any overlay-arrows in current_buffer. */
10566 static int
10567 overlay_arrow_in_current_buffer_p ()
10569 Lisp_Object vlist;
10571 for (vlist = Voverlay_arrow_variable_list;
10572 CONSP (vlist);
10573 vlist = XCDR (vlist))
10575 Lisp_Object var = XCAR (vlist);
10576 Lisp_Object val;
10578 if (!SYMBOLP (var))
10579 continue;
10580 val = find_symbol_value (var);
10581 if (MARKERP (val)
10582 && current_buffer == XMARKER (val)->buffer)
10583 return 1;
10585 return 0;
10589 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10590 has changed. */
10592 static int
10593 overlay_arrows_changed_p ()
10595 Lisp_Object vlist;
10597 for (vlist = Voverlay_arrow_variable_list;
10598 CONSP (vlist);
10599 vlist = XCDR (vlist))
10601 Lisp_Object var = XCAR (vlist);
10602 Lisp_Object val, pstr;
10604 if (!SYMBOLP (var))
10605 continue;
10606 val = find_symbol_value (var);
10607 if (!MARKERP (val))
10608 continue;
10609 if (! EQ (COERCE_MARKER (val),
10610 Fget (var, Qlast_arrow_position))
10611 || ! (pstr = overlay_arrow_string_or_property (var),
10612 EQ (pstr, Fget (var, Qlast_arrow_string))))
10613 return 1;
10615 return 0;
10618 /* Mark overlay arrows to be updated on next redisplay. */
10620 static void
10621 update_overlay_arrows (up_to_date)
10622 int up_to_date;
10624 Lisp_Object vlist;
10626 for (vlist = Voverlay_arrow_variable_list;
10627 CONSP (vlist);
10628 vlist = XCDR (vlist))
10630 Lisp_Object var = XCAR (vlist);
10632 if (!SYMBOLP (var))
10633 continue;
10635 if (up_to_date > 0)
10637 Lisp_Object val = find_symbol_value (var);
10638 Fput (var, Qlast_arrow_position,
10639 COERCE_MARKER (val));
10640 Fput (var, Qlast_arrow_string,
10641 overlay_arrow_string_or_property (var));
10643 else if (up_to_date < 0
10644 || !NILP (Fget (var, Qlast_arrow_position)))
10646 Fput (var, Qlast_arrow_position, Qt);
10647 Fput (var, Qlast_arrow_string, Qt);
10653 /* Return overlay arrow string to display at row.
10654 Return integer (bitmap number) for arrow bitmap in left fringe.
10655 Return nil if no overlay arrow. */
10657 static Lisp_Object
10658 overlay_arrow_at_row (it, row)
10659 struct it *it;
10660 struct glyph_row *row;
10662 Lisp_Object vlist;
10664 for (vlist = Voverlay_arrow_variable_list;
10665 CONSP (vlist);
10666 vlist = XCDR (vlist))
10668 Lisp_Object var = XCAR (vlist);
10669 Lisp_Object val;
10671 if (!SYMBOLP (var))
10672 continue;
10674 val = find_symbol_value (var);
10676 if (MARKERP (val)
10677 && current_buffer == XMARKER (val)->buffer
10678 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10680 if (FRAME_WINDOW_P (it->f)
10681 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10683 #ifdef HAVE_WINDOW_SYSTEM
10684 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10686 int fringe_bitmap;
10687 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10688 return make_number (fringe_bitmap);
10690 #endif
10691 return make_number (-1); /* Use default arrow bitmap */
10693 return overlay_arrow_string_or_property (var);
10697 return Qnil;
10700 /* Return 1 if point moved out of or into a composition. Otherwise
10701 return 0. PREV_BUF and PREV_PT are the last point buffer and
10702 position. BUF and PT are the current point buffer and position. */
10705 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10706 struct buffer *prev_buf, *buf;
10707 int prev_pt, pt;
10709 int start, end;
10710 Lisp_Object prop;
10711 Lisp_Object buffer;
10713 XSETBUFFER (buffer, buf);
10714 /* Check a composition at the last point if point moved within the
10715 same buffer. */
10716 if (prev_buf == buf)
10718 if (prev_pt == pt)
10719 /* Point didn't move. */
10720 return 0;
10722 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10723 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10724 && COMPOSITION_VALID_P (start, end, prop)
10725 && start < prev_pt && end > prev_pt)
10726 /* The last point was within the composition. Return 1 iff
10727 point moved out of the composition. */
10728 return (pt <= start || pt >= end);
10731 /* Check a composition at the current point. */
10732 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10733 && find_composition (pt, -1, &start, &end, &prop, buffer)
10734 && COMPOSITION_VALID_P (start, end, prop)
10735 && start < pt && end > pt);
10739 /* Reconsider the setting of B->clip_changed which is displayed
10740 in window W. */
10742 static INLINE void
10743 reconsider_clip_changes (w, b)
10744 struct window *w;
10745 struct buffer *b;
10747 if (b->clip_changed
10748 && !NILP (w->window_end_valid)
10749 && w->current_matrix->buffer == b
10750 && w->current_matrix->zv == BUF_ZV (b)
10751 && w->current_matrix->begv == BUF_BEGV (b))
10752 b->clip_changed = 0;
10754 /* If display wasn't paused, and W is not a tool bar window, see if
10755 point has been moved into or out of a composition. In that case,
10756 we set b->clip_changed to 1 to force updating the screen. If
10757 b->clip_changed has already been set to 1, we can skip this
10758 check. */
10759 if (!b->clip_changed
10760 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10762 int pt;
10764 if (w == XWINDOW (selected_window))
10765 pt = BUF_PT (current_buffer);
10766 else
10767 pt = marker_position (w->pointm);
10769 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10770 || pt != XINT (w->last_point))
10771 && check_point_in_composition (w->current_matrix->buffer,
10772 XINT (w->last_point),
10773 XBUFFER (w->buffer), pt))
10774 b->clip_changed = 1;
10779 /* Select FRAME to forward the values of frame-local variables into C
10780 variables so that the redisplay routines can access those values
10781 directly. */
10783 static void
10784 select_frame_for_redisplay (frame)
10785 Lisp_Object frame;
10787 Lisp_Object tail, sym, val;
10788 Lisp_Object old = selected_frame;
10790 selected_frame = frame;
10792 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10793 if (CONSP (XCAR (tail))
10794 && (sym = XCAR (XCAR (tail)),
10795 SYMBOLP (sym))
10796 && (sym = indirect_variable (sym),
10797 val = SYMBOL_VALUE (sym),
10798 (BUFFER_LOCAL_VALUEP (val)
10799 || SOME_BUFFER_LOCAL_VALUEP (val)))
10800 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10801 /* Use find_symbol_value rather than Fsymbol_value
10802 to avoid an error if it is void. */
10803 find_symbol_value (sym);
10805 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10806 if (CONSP (XCAR (tail))
10807 && (sym = XCAR (XCAR (tail)),
10808 SYMBOLP (sym))
10809 && (sym = indirect_variable (sym),
10810 val = SYMBOL_VALUE (sym),
10811 (BUFFER_LOCAL_VALUEP (val)
10812 || SOME_BUFFER_LOCAL_VALUEP (val)))
10813 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10814 find_symbol_value (sym);
10818 #define STOP_POLLING \
10819 do { if (! polling_stopped_here) stop_polling (); \
10820 polling_stopped_here = 1; } while (0)
10822 #define RESUME_POLLING \
10823 do { if (polling_stopped_here) start_polling (); \
10824 polling_stopped_here = 0; } while (0)
10827 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10828 response to any user action; therefore, we should preserve the echo
10829 area. (Actually, our caller does that job.) Perhaps in the future
10830 avoid recentering windows if it is not necessary; currently that
10831 causes some problems. */
10833 static void
10834 redisplay_internal (preserve_echo_area)
10835 int preserve_echo_area;
10837 struct window *w = XWINDOW (selected_window);
10838 struct frame *f;
10839 int pause;
10840 int must_finish = 0;
10841 struct text_pos tlbufpos, tlendpos;
10842 int number_of_visible_frames;
10843 int count, count1;
10844 struct frame *sf;
10845 int polling_stopped_here = 0;
10847 /* Non-zero means redisplay has to consider all windows on all
10848 frames. Zero means, only selected_window is considered. */
10849 int consider_all_windows_p;
10851 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10853 /* No redisplay if running in batch mode or frame is not yet fully
10854 initialized, or redisplay is explicitly turned off by setting
10855 Vinhibit_redisplay. */
10856 if (noninteractive
10857 || !NILP (Vinhibit_redisplay))
10858 return;
10860 /* Don't examine these until after testing Vinhibit_redisplay.
10861 When Emacs is shutting down, perhaps because its connection to
10862 X has dropped, we should not look at them at all. */
10863 f = XFRAME (w->frame);
10864 sf = SELECTED_FRAME ();
10866 if (!f->glyphs_initialized_p)
10867 return;
10869 /* The flag redisplay_performed_directly_p is set by
10870 direct_output_for_insert when it already did the whole screen
10871 update necessary. */
10872 if (redisplay_performed_directly_p)
10874 redisplay_performed_directly_p = 0;
10875 if (!hscroll_windows (selected_window))
10876 return;
10879 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10880 if (popup_activated ())
10881 return;
10882 #endif
10884 /* I don't think this happens but let's be paranoid. */
10885 if (redisplaying_p)
10886 return;
10888 /* Record a function that resets redisplaying_p to its old value
10889 when we leave this function. */
10890 count = SPECPDL_INDEX ();
10891 record_unwind_protect (unwind_redisplay,
10892 Fcons (make_number (redisplaying_p), selected_frame));
10893 ++redisplaying_p;
10894 specbind (Qinhibit_free_realized_faces, Qnil);
10897 Lisp_Object tail, frame;
10899 FOR_EACH_FRAME (tail, frame)
10901 struct frame *f = XFRAME (frame);
10902 f->already_hscrolled_p = 0;
10906 retry:
10907 pause = 0;
10908 reconsider_clip_changes (w, current_buffer);
10909 last_escape_glyph_frame = NULL;
10910 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10912 /* If new fonts have been loaded that make a glyph matrix adjustment
10913 necessary, do it. */
10914 if (fonts_changed_p)
10916 adjust_glyphs (NULL);
10917 ++windows_or_buffers_changed;
10918 fonts_changed_p = 0;
10921 /* If face_change_count is non-zero, init_iterator will free all
10922 realized faces, which includes the faces referenced from current
10923 matrices. So, we can't reuse current matrices in this case. */
10924 if (face_change_count)
10925 ++windows_or_buffers_changed;
10927 if (! FRAME_WINDOW_P (sf)
10928 && previous_terminal_frame != sf)
10930 /* Since frames on an ASCII terminal share the same display
10931 area, displaying a different frame means redisplay the whole
10932 thing. */
10933 windows_or_buffers_changed++;
10934 SET_FRAME_GARBAGED (sf);
10935 XSETFRAME (Vterminal_frame, sf);
10937 previous_terminal_frame = sf;
10939 /* Set the visible flags for all frames. Do this before checking
10940 for resized or garbaged frames; they want to know if their frames
10941 are visible. See the comment in frame.h for
10942 FRAME_SAMPLE_VISIBILITY. */
10944 Lisp_Object tail, frame;
10946 number_of_visible_frames = 0;
10948 FOR_EACH_FRAME (tail, frame)
10950 struct frame *f = XFRAME (frame);
10952 FRAME_SAMPLE_VISIBILITY (f);
10953 if (FRAME_VISIBLE_P (f))
10954 ++number_of_visible_frames;
10955 clear_desired_matrices (f);
10959 /* Notice any pending interrupt request to change frame size. */
10960 do_pending_window_change (1);
10962 /* Clear frames marked as garbaged. */
10963 if (frame_garbaged)
10964 clear_garbaged_frames ();
10966 /* Build menubar and tool-bar items. */
10967 if (NILP (Vmemory_full))
10968 prepare_menu_bars ();
10970 if (windows_or_buffers_changed)
10971 update_mode_lines++;
10973 /* Detect case that we need to write or remove a star in the mode line. */
10974 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10976 w->update_mode_line = Qt;
10977 if (buffer_shared > 1)
10978 update_mode_lines++;
10981 /* Avoid invocation of point motion hooks by `current_column' below. */
10982 count1 = SPECPDL_INDEX ();
10983 specbind (Qinhibit_point_motion_hooks, Qt);
10985 /* If %c is in the mode line, update it if needed. */
10986 if (!NILP (w->column_number_displayed)
10987 /* This alternative quickly identifies a common case
10988 where no change is needed. */
10989 && !(PT == XFASTINT (w->last_point)
10990 && XFASTINT (w->last_modified) >= MODIFF
10991 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10992 && (XFASTINT (w->column_number_displayed)
10993 != (int) current_column ())) /* iftc */
10994 w->update_mode_line = Qt;
10996 unbind_to (count1, Qnil);
10998 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11000 /* The variable buffer_shared is set in redisplay_window and
11001 indicates that we redisplay a buffer in different windows. See
11002 there. */
11003 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11004 || cursor_type_changed);
11006 /* If specs for an arrow have changed, do thorough redisplay
11007 to ensure we remove any arrow that should no longer exist. */
11008 if (overlay_arrows_changed_p ())
11009 consider_all_windows_p = windows_or_buffers_changed = 1;
11011 /* Normally the message* functions will have already displayed and
11012 updated the echo area, but the frame may have been trashed, or
11013 the update may have been preempted, so display the echo area
11014 again here. Checking message_cleared_p captures the case that
11015 the echo area should be cleared. */
11016 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11017 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11018 || (message_cleared_p
11019 && minibuf_level == 0
11020 /* If the mini-window is currently selected, this means the
11021 echo-area doesn't show through. */
11022 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11024 int window_height_changed_p = echo_area_display (0);
11025 must_finish = 1;
11027 /* If we don't display the current message, don't clear the
11028 message_cleared_p flag, because, if we did, we wouldn't clear
11029 the echo area in the next redisplay which doesn't preserve
11030 the echo area. */
11031 if (!display_last_displayed_message_p)
11032 message_cleared_p = 0;
11034 if (fonts_changed_p)
11035 goto retry;
11036 else if (window_height_changed_p)
11038 consider_all_windows_p = 1;
11039 ++update_mode_lines;
11040 ++windows_or_buffers_changed;
11042 /* If window configuration was changed, frames may have been
11043 marked garbaged. Clear them or we will experience
11044 surprises wrt scrolling. */
11045 if (frame_garbaged)
11046 clear_garbaged_frames ();
11049 else if (EQ (selected_window, minibuf_window)
11050 && (current_buffer->clip_changed
11051 || XFASTINT (w->last_modified) < MODIFF
11052 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11053 && resize_mini_window (w, 0))
11055 /* Resized active mini-window to fit the size of what it is
11056 showing if its contents might have changed. */
11057 must_finish = 1;
11058 consider_all_windows_p = 1;
11059 ++windows_or_buffers_changed;
11060 ++update_mode_lines;
11062 /* If window configuration was changed, frames may have been
11063 marked garbaged. Clear them or we will experience
11064 surprises wrt scrolling. */
11065 if (frame_garbaged)
11066 clear_garbaged_frames ();
11070 /* If showing the region, and mark has changed, we must redisplay
11071 the whole window. The assignment to this_line_start_pos prevents
11072 the optimization directly below this if-statement. */
11073 if (((!NILP (Vtransient_mark_mode)
11074 && !NILP (XBUFFER (w->buffer)->mark_active))
11075 != !NILP (w->region_showing))
11076 || (!NILP (w->region_showing)
11077 && !EQ (w->region_showing,
11078 Fmarker_position (XBUFFER (w->buffer)->mark))))
11079 CHARPOS (this_line_start_pos) = 0;
11081 /* Optimize the case that only the line containing the cursor in the
11082 selected window has changed. Variables starting with this_ are
11083 set in display_line and record information about the line
11084 containing the cursor. */
11085 tlbufpos = this_line_start_pos;
11086 tlendpos = this_line_end_pos;
11087 if (!consider_all_windows_p
11088 && CHARPOS (tlbufpos) > 0
11089 && NILP (w->update_mode_line)
11090 && !current_buffer->clip_changed
11091 && !current_buffer->prevent_redisplay_optimizations_p
11092 && FRAME_VISIBLE_P (XFRAME (w->frame))
11093 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11094 /* Make sure recorded data applies to current buffer, etc. */
11095 && this_line_buffer == current_buffer
11096 && current_buffer == XBUFFER (w->buffer)
11097 && NILP (w->force_start)
11098 && NILP (w->optional_new_start)
11099 /* Point must be on the line that we have info recorded about. */
11100 && PT >= CHARPOS (tlbufpos)
11101 && PT <= Z - CHARPOS (tlendpos)
11102 /* All text outside that line, including its final newline,
11103 must be unchanged */
11104 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11105 CHARPOS (tlendpos)))
11107 if (CHARPOS (tlbufpos) > BEGV
11108 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11109 && (CHARPOS (tlbufpos) == ZV
11110 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11111 /* Former continuation line has disappeared by becoming empty */
11112 goto cancel;
11113 else if (XFASTINT (w->last_modified) < MODIFF
11114 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11115 || MINI_WINDOW_P (w))
11117 /* We have to handle the case of continuation around a
11118 wide-column character (See the comment in indent.c around
11119 line 885).
11121 For instance, in the following case:
11123 -------- Insert --------
11124 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11125 J_I_ ==> J_I_ `^^' are cursors.
11126 ^^ ^^
11127 -------- --------
11129 As we have to redraw the line above, we should goto cancel. */
11131 struct it it;
11132 int line_height_before = this_line_pixel_height;
11134 /* Note that start_display will handle the case that the
11135 line starting at tlbufpos is a continuation lines. */
11136 start_display (&it, w, tlbufpos);
11138 /* Implementation note: It this still necessary? */
11139 if (it.current_x != this_line_start_x)
11140 goto cancel;
11142 TRACE ((stderr, "trying display optimization 1\n"));
11143 w->cursor.vpos = -1;
11144 overlay_arrow_seen = 0;
11145 it.vpos = this_line_vpos;
11146 it.current_y = this_line_y;
11147 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11148 display_line (&it);
11150 /* If line contains point, is not continued,
11151 and ends at same distance from eob as before, we win */
11152 if (w->cursor.vpos >= 0
11153 /* Line is not continued, otherwise this_line_start_pos
11154 would have been set to 0 in display_line. */
11155 && CHARPOS (this_line_start_pos)
11156 /* Line ends as before. */
11157 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11158 /* Line has same height as before. Otherwise other lines
11159 would have to be shifted up or down. */
11160 && this_line_pixel_height == line_height_before)
11162 /* If this is not the window's last line, we must adjust
11163 the charstarts of the lines below. */
11164 if (it.current_y < it.last_visible_y)
11166 struct glyph_row *row
11167 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11168 int delta, delta_bytes;
11170 if (Z - CHARPOS (tlendpos) == ZV)
11172 /* This line ends at end of (accessible part of)
11173 buffer. There is no newline to count. */
11174 delta = (Z
11175 - CHARPOS (tlendpos)
11176 - MATRIX_ROW_START_CHARPOS (row));
11177 delta_bytes = (Z_BYTE
11178 - BYTEPOS (tlendpos)
11179 - MATRIX_ROW_START_BYTEPOS (row));
11181 else
11183 /* This line ends in a newline. Must take
11184 account of the newline and the rest of the
11185 text that follows. */
11186 delta = (Z
11187 - CHARPOS (tlendpos)
11188 - MATRIX_ROW_START_CHARPOS (row));
11189 delta_bytes = (Z_BYTE
11190 - BYTEPOS (tlendpos)
11191 - MATRIX_ROW_START_BYTEPOS (row));
11194 increment_matrix_positions (w->current_matrix,
11195 this_line_vpos + 1,
11196 w->current_matrix->nrows,
11197 delta, delta_bytes);
11200 /* If this row displays text now but previously didn't,
11201 or vice versa, w->window_end_vpos may have to be
11202 adjusted. */
11203 if ((it.glyph_row - 1)->displays_text_p)
11205 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11206 XSETINT (w->window_end_vpos, this_line_vpos);
11208 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11209 && this_line_vpos > 0)
11210 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11211 w->window_end_valid = Qnil;
11213 /* Update hint: No need to try to scroll in update_window. */
11214 w->desired_matrix->no_scrolling_p = 1;
11216 #if GLYPH_DEBUG
11217 *w->desired_matrix->method = 0;
11218 debug_method_add (w, "optimization 1");
11219 #endif
11220 #ifdef HAVE_WINDOW_SYSTEM
11221 update_window_fringes (w, 0);
11222 #endif
11223 goto update;
11225 else
11226 goto cancel;
11228 else if (/* Cursor position hasn't changed. */
11229 PT == XFASTINT (w->last_point)
11230 /* Make sure the cursor was last displayed
11231 in this window. Otherwise we have to reposition it. */
11232 && 0 <= w->cursor.vpos
11233 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11235 if (!must_finish)
11237 do_pending_window_change (1);
11239 /* We used to always goto end_of_redisplay here, but this
11240 isn't enough if we have a blinking cursor. */
11241 if (w->cursor_off_p == w->last_cursor_off_p)
11242 goto end_of_redisplay;
11244 goto update;
11246 /* If highlighting the region, or if the cursor is in the echo area,
11247 then we can't just move the cursor. */
11248 else if (! (!NILP (Vtransient_mark_mode)
11249 && !NILP (current_buffer->mark_active))
11250 && (EQ (selected_window, current_buffer->last_selected_window)
11251 || highlight_nonselected_windows)
11252 && NILP (w->region_showing)
11253 && NILP (Vshow_trailing_whitespace)
11254 && !cursor_in_echo_area)
11256 struct it it;
11257 struct glyph_row *row;
11259 /* Skip from tlbufpos to PT and see where it is. Note that
11260 PT may be in invisible text. If so, we will end at the
11261 next visible position. */
11262 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11263 NULL, DEFAULT_FACE_ID);
11264 it.current_x = this_line_start_x;
11265 it.current_y = this_line_y;
11266 it.vpos = this_line_vpos;
11268 /* The call to move_it_to stops in front of PT, but
11269 moves over before-strings. */
11270 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11272 if (it.vpos == this_line_vpos
11273 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11274 row->enabled_p))
11276 xassert (this_line_vpos == it.vpos);
11277 xassert (this_line_y == it.current_y);
11278 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11279 #if GLYPH_DEBUG
11280 *w->desired_matrix->method = 0;
11281 debug_method_add (w, "optimization 3");
11282 #endif
11283 goto update;
11285 else
11286 goto cancel;
11289 cancel:
11290 /* Text changed drastically or point moved off of line. */
11291 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11294 CHARPOS (this_line_start_pos) = 0;
11295 consider_all_windows_p |= buffer_shared > 1;
11296 ++clear_face_cache_count;
11297 #ifdef HAVE_WINDOW_SYSTEM
11298 ++clear_image_cache_count;
11299 #endif
11301 /* Build desired matrices, and update the display. If
11302 consider_all_windows_p is non-zero, do it for all windows on all
11303 frames. Otherwise do it for selected_window, only. */
11305 if (consider_all_windows_p)
11307 Lisp_Object tail, frame;
11309 FOR_EACH_FRAME (tail, frame)
11310 XFRAME (frame)->updated_p = 0;
11312 /* Recompute # windows showing selected buffer. This will be
11313 incremented each time such a window is displayed. */
11314 buffer_shared = 0;
11316 FOR_EACH_FRAME (tail, frame)
11318 struct frame *f = XFRAME (frame);
11320 if (FRAME_WINDOW_P (f) || f == sf)
11322 if (! EQ (frame, selected_frame))
11323 /* Select the frame, for the sake of frame-local
11324 variables. */
11325 select_frame_for_redisplay (frame);
11327 /* Mark all the scroll bars to be removed; we'll redeem
11328 the ones we want when we redisplay their windows. */
11329 if (condemn_scroll_bars_hook)
11330 condemn_scroll_bars_hook (f);
11332 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11333 redisplay_windows (FRAME_ROOT_WINDOW (f));
11335 /* Any scroll bars which redisplay_windows should have
11336 nuked should now go away. */
11337 if (judge_scroll_bars_hook)
11338 judge_scroll_bars_hook (f);
11340 /* If fonts changed, display again. */
11341 /* ??? rms: I suspect it is a mistake to jump all the way
11342 back to retry here. It should just retry this frame. */
11343 if (fonts_changed_p)
11344 goto retry;
11346 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11348 /* See if we have to hscroll. */
11349 if (!f->already_hscrolled_p)
11351 f->already_hscrolled_p = 1;
11352 if (hscroll_windows (f->root_window))
11353 goto retry;
11356 /* Prevent various kinds of signals during display
11357 update. stdio is not robust about handling
11358 signals, which can cause an apparent I/O
11359 error. */
11360 if (interrupt_input)
11361 unrequest_sigio ();
11362 STOP_POLLING;
11364 /* Update the display. */
11365 set_window_update_flags (XWINDOW (f->root_window), 1);
11366 pause |= update_frame (f, 0, 0);
11367 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11368 if (pause)
11369 break;
11370 #endif
11372 f->updated_p = 1;
11377 if (!pause)
11379 /* Do the mark_window_display_accurate after all windows have
11380 been redisplayed because this call resets flags in buffers
11381 which are needed for proper redisplay. */
11382 FOR_EACH_FRAME (tail, frame)
11384 struct frame *f = XFRAME (frame);
11385 if (f->updated_p)
11387 mark_window_display_accurate (f->root_window, 1);
11388 if (frame_up_to_date_hook)
11389 frame_up_to_date_hook (f);
11394 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11396 Lisp_Object mini_window;
11397 struct frame *mini_frame;
11399 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11400 /* Use list_of_error, not Qerror, so that
11401 we catch only errors and don't run the debugger. */
11402 internal_condition_case_1 (redisplay_window_1, selected_window,
11403 list_of_error,
11404 redisplay_window_error);
11406 /* Compare desired and current matrices, perform output. */
11408 update:
11409 /* If fonts changed, display again. */
11410 if (fonts_changed_p)
11411 goto retry;
11413 /* Prevent various kinds of signals during display update.
11414 stdio is not robust about handling signals,
11415 which can cause an apparent I/O error. */
11416 if (interrupt_input)
11417 unrequest_sigio ();
11418 STOP_POLLING;
11420 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11422 if (hscroll_windows (selected_window))
11423 goto retry;
11425 XWINDOW (selected_window)->must_be_updated_p = 1;
11426 pause = update_frame (sf, 0, 0);
11429 /* We may have called echo_area_display at the top of this
11430 function. If the echo area is on another frame, that may
11431 have put text on a frame other than the selected one, so the
11432 above call to update_frame would not have caught it. Catch
11433 it here. */
11434 mini_window = FRAME_MINIBUF_WINDOW (sf);
11435 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11437 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11439 XWINDOW (mini_window)->must_be_updated_p = 1;
11440 pause |= update_frame (mini_frame, 0, 0);
11441 if (!pause && hscroll_windows (mini_window))
11442 goto retry;
11446 /* If display was paused because of pending input, make sure we do a
11447 thorough update the next time. */
11448 if (pause)
11450 /* Prevent the optimization at the beginning of
11451 redisplay_internal that tries a single-line update of the
11452 line containing the cursor in the selected window. */
11453 CHARPOS (this_line_start_pos) = 0;
11455 /* Let the overlay arrow be updated the next time. */
11456 update_overlay_arrows (0);
11458 /* If we pause after scrolling, some rows in the current
11459 matrices of some windows are not valid. */
11460 if (!WINDOW_FULL_WIDTH_P (w)
11461 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11462 update_mode_lines = 1;
11464 else
11466 if (!consider_all_windows_p)
11468 /* This has already been done above if
11469 consider_all_windows_p is set. */
11470 mark_window_display_accurate_1 (w, 1);
11472 /* Say overlay arrows are up to date. */
11473 update_overlay_arrows (1);
11475 if (frame_up_to_date_hook != 0)
11476 frame_up_to_date_hook (sf);
11479 update_mode_lines = 0;
11480 windows_or_buffers_changed = 0;
11481 cursor_type_changed = 0;
11484 /* Start SIGIO interrupts coming again. Having them off during the
11485 code above makes it less likely one will discard output, but not
11486 impossible, since there might be stuff in the system buffer here.
11487 But it is much hairier to try to do anything about that. */
11488 if (interrupt_input)
11489 request_sigio ();
11490 RESUME_POLLING;
11492 /* If a frame has become visible which was not before, redisplay
11493 again, so that we display it. Expose events for such a frame
11494 (which it gets when becoming visible) don't call the parts of
11495 redisplay constructing glyphs, so simply exposing a frame won't
11496 display anything in this case. So, we have to display these
11497 frames here explicitly. */
11498 if (!pause)
11500 Lisp_Object tail, frame;
11501 int new_count = 0;
11503 FOR_EACH_FRAME (tail, frame)
11505 int this_is_visible = 0;
11507 if (XFRAME (frame)->visible)
11508 this_is_visible = 1;
11509 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11510 if (XFRAME (frame)->visible)
11511 this_is_visible = 1;
11513 if (this_is_visible)
11514 new_count++;
11517 if (new_count != number_of_visible_frames)
11518 windows_or_buffers_changed++;
11521 /* Change frame size now if a change is pending. */
11522 do_pending_window_change (1);
11524 /* If we just did a pending size change, or have additional
11525 visible frames, redisplay again. */
11526 if (windows_or_buffers_changed && !pause)
11527 goto retry;
11529 /* Clear the face cache eventually. */
11530 if (consider_all_windows_p)
11532 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11534 clear_face_cache (0);
11535 clear_face_cache_count = 0;
11537 #ifdef HAVE_WINDOW_SYSTEM
11538 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11540 Lisp_Object tail, frame;
11541 FOR_EACH_FRAME (tail, frame)
11543 struct frame *f = XFRAME (frame);
11544 if (FRAME_WINDOW_P (f))
11545 clear_image_cache (f, 0);
11547 clear_image_cache_count = 0;
11549 #endif /* HAVE_WINDOW_SYSTEM */
11552 end_of_redisplay:
11553 unbind_to (count, Qnil);
11554 RESUME_POLLING;
11558 /* Redisplay, but leave alone any recent echo area message unless
11559 another message has been requested in its place.
11561 This is useful in situations where you need to redisplay but no
11562 user action has occurred, making it inappropriate for the message
11563 area to be cleared. See tracking_off and
11564 wait_reading_process_output for examples of these situations.
11566 FROM_WHERE is an integer saying from where this function was
11567 called. This is useful for debugging. */
11569 void
11570 redisplay_preserve_echo_area (from_where)
11571 int from_where;
11573 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11575 if (!NILP (echo_area_buffer[1]))
11577 /* We have a previously displayed message, but no current
11578 message. Redisplay the previous message. */
11579 display_last_displayed_message_p = 1;
11580 redisplay_internal (1);
11581 display_last_displayed_message_p = 0;
11583 else
11584 redisplay_internal (1);
11586 if (rif != NULL && rif->flush_display_optional)
11587 rif->flush_display_optional (NULL);
11591 /* Function registered with record_unwind_protect in
11592 redisplay_internal. Reset redisplaying_p to the value it had
11593 before redisplay_internal was called, and clear
11594 prevent_freeing_realized_faces_p. It also selects the previously
11595 selected frame. */
11597 static Lisp_Object
11598 unwind_redisplay (val)
11599 Lisp_Object val;
11601 Lisp_Object old_redisplaying_p, old_frame;
11603 old_redisplaying_p = XCAR (val);
11604 redisplaying_p = XFASTINT (old_redisplaying_p);
11605 old_frame = XCDR (val);
11606 if (! EQ (old_frame, selected_frame))
11607 select_frame_for_redisplay (old_frame);
11608 return Qnil;
11612 /* Mark the display of window W as accurate or inaccurate. If
11613 ACCURATE_P is non-zero mark display of W as accurate. If
11614 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11615 redisplay_internal is called. */
11617 static void
11618 mark_window_display_accurate_1 (w, accurate_p)
11619 struct window *w;
11620 int accurate_p;
11622 if (BUFFERP (w->buffer))
11624 struct buffer *b = XBUFFER (w->buffer);
11626 w->last_modified
11627 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11628 w->last_overlay_modified
11629 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11630 w->last_had_star
11631 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11633 if (accurate_p)
11635 b->clip_changed = 0;
11636 b->prevent_redisplay_optimizations_p = 0;
11638 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11639 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11640 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11641 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11643 w->current_matrix->buffer = b;
11644 w->current_matrix->begv = BUF_BEGV (b);
11645 w->current_matrix->zv = BUF_ZV (b);
11647 w->last_cursor = w->cursor;
11648 w->last_cursor_off_p = w->cursor_off_p;
11650 if (w == XWINDOW (selected_window))
11651 w->last_point = make_number (BUF_PT (b));
11652 else
11653 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11657 if (accurate_p)
11659 w->window_end_valid = w->buffer;
11660 #if 0 /* This is incorrect with variable-height lines. */
11661 xassert (XINT (w->window_end_vpos)
11662 < (WINDOW_TOTAL_LINES (w)
11663 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11664 #endif
11665 w->update_mode_line = Qnil;
11670 /* Mark the display of windows in the window tree rooted at WINDOW as
11671 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11672 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11673 be redisplayed the next time redisplay_internal is called. */
11675 void
11676 mark_window_display_accurate (window, accurate_p)
11677 Lisp_Object window;
11678 int accurate_p;
11680 struct window *w;
11682 for (; !NILP (window); window = w->next)
11684 w = XWINDOW (window);
11685 mark_window_display_accurate_1 (w, accurate_p);
11687 if (!NILP (w->vchild))
11688 mark_window_display_accurate (w->vchild, accurate_p);
11689 if (!NILP (w->hchild))
11690 mark_window_display_accurate (w->hchild, accurate_p);
11693 if (accurate_p)
11695 update_overlay_arrows (1);
11697 else
11699 /* Force a thorough redisplay the next time by setting
11700 last_arrow_position and last_arrow_string to t, which is
11701 unequal to any useful value of Voverlay_arrow_... */
11702 update_overlay_arrows (-1);
11707 /* Return value in display table DP (Lisp_Char_Table *) for character
11708 C. Since a display table doesn't have any parent, we don't have to
11709 follow parent. Do not call this function directly but use the
11710 macro DISP_CHAR_VECTOR. */
11712 Lisp_Object
11713 disp_char_vector (dp, c)
11714 struct Lisp_Char_Table *dp;
11715 int c;
11717 int code[4], i;
11718 Lisp_Object val;
11720 if (SINGLE_BYTE_CHAR_P (c))
11721 return (dp->contents[c]);
11723 SPLIT_CHAR (c, code[0], code[1], code[2]);
11724 if (code[1] < 32)
11725 code[1] = -1;
11726 else if (code[2] < 32)
11727 code[2] = -1;
11729 /* Here, the possible range of code[0] (== charset ID) is
11730 128..max_charset. Since the top level char table contains data
11731 for multibyte characters after 256th element, we must increment
11732 code[0] by 128 to get a correct index. */
11733 code[0] += 128;
11734 code[3] = -1; /* anchor */
11736 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11738 val = dp->contents[code[i]];
11739 if (!SUB_CHAR_TABLE_P (val))
11740 return (NILP (val) ? dp->defalt : val);
11743 /* Here, val is a sub char table. We return the default value of
11744 it. */
11745 return (dp->defalt);
11750 /***********************************************************************
11751 Window Redisplay
11752 ***********************************************************************/
11754 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11756 static void
11757 redisplay_windows (window)
11758 Lisp_Object window;
11760 while (!NILP (window))
11762 struct window *w = XWINDOW (window);
11764 if (!NILP (w->hchild))
11765 redisplay_windows (w->hchild);
11766 else if (!NILP (w->vchild))
11767 redisplay_windows (w->vchild);
11768 else
11770 displayed_buffer = XBUFFER (w->buffer);
11771 /* Use list_of_error, not Qerror, so that
11772 we catch only errors and don't run the debugger. */
11773 internal_condition_case_1 (redisplay_window_0, window,
11774 list_of_error,
11775 redisplay_window_error);
11778 window = w->next;
11782 static Lisp_Object
11783 redisplay_window_error ()
11785 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11786 return Qnil;
11789 static Lisp_Object
11790 redisplay_window_0 (window)
11791 Lisp_Object window;
11793 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11794 redisplay_window (window, 0);
11795 return Qnil;
11798 static Lisp_Object
11799 redisplay_window_1 (window)
11800 Lisp_Object window;
11802 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11803 redisplay_window (window, 1);
11804 return Qnil;
11808 /* Increment GLYPH until it reaches END or CONDITION fails while
11809 adding (GLYPH)->pixel_width to X. */
11811 #define SKIP_GLYPHS(glyph, end, x, condition) \
11812 do \
11814 (x) += (glyph)->pixel_width; \
11815 ++(glyph); \
11817 while ((glyph) < (end) && (condition))
11820 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11821 DELTA is the number of bytes by which positions recorded in ROW
11822 differ from current buffer positions.
11824 Return 0 if cursor is not on this row. 1 otherwise. */
11827 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11828 struct window *w;
11829 struct glyph_row *row;
11830 struct glyph_matrix *matrix;
11831 int delta, delta_bytes, dy, dvpos;
11833 struct glyph *glyph = row->glyphs[TEXT_AREA];
11834 struct glyph *end = glyph + row->used[TEXT_AREA];
11835 struct glyph *cursor = NULL;
11836 /* The first glyph that starts a sequence of glyphs from string. */
11837 struct glyph *string_start;
11838 /* The X coordinate of string_start. */
11839 int string_start_x;
11840 /* The last known character position. */
11841 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11842 /* The last known character position before string_start. */
11843 int string_before_pos;
11844 int x = row->x;
11845 int cursor_x = x;
11846 int cursor_from_overlay_pos = 0;
11847 int pt_old = PT - delta;
11849 /* Skip over glyphs not having an object at the start of the row.
11850 These are special glyphs like truncation marks on terminal
11851 frames. */
11852 if (row->displays_text_p)
11853 while (glyph < end
11854 && INTEGERP (glyph->object)
11855 && glyph->charpos < 0)
11857 x += glyph->pixel_width;
11858 ++glyph;
11861 string_start = NULL;
11862 while (glyph < end
11863 && !INTEGERP (glyph->object)
11864 && (!BUFFERP (glyph->object)
11865 || (last_pos = glyph->charpos) < pt_old))
11867 if (! STRINGP (glyph->object))
11869 string_start = NULL;
11870 x += glyph->pixel_width;
11871 ++glyph;
11872 if (cursor_from_overlay_pos
11873 && last_pos >= cursor_from_overlay_pos)
11875 cursor_from_overlay_pos = 0;
11876 cursor = 0;
11879 else
11881 if (string_start == NULL)
11883 string_before_pos = last_pos;
11884 string_start = glyph;
11885 string_start_x = x;
11887 /* Skip all glyphs from string. */
11890 Lisp_Object cprop;
11891 int pos;
11892 if ((cursor == NULL || glyph > cursor)
11893 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11894 Qcursor, (glyph)->object),
11895 !NILP (cprop))
11896 && (pos = string_buffer_position (w, glyph->object,
11897 string_before_pos),
11898 (pos == 0 /* From overlay */
11899 || pos == pt_old)))
11901 /* Estimate overlay buffer position from the buffer
11902 positions of the glyphs before and after the overlay.
11903 Add 1 to last_pos so that if point corresponds to the
11904 glyph right after the overlay, we still use a 'cursor'
11905 property found in that overlay. */
11906 cursor_from_overlay_pos = (pos ? 0 : last_pos
11907 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11908 cursor = glyph;
11909 cursor_x = x;
11911 x += glyph->pixel_width;
11912 ++glyph;
11914 while (glyph < end && EQ (glyph->object, string_start->object));
11918 if (cursor != NULL)
11920 glyph = cursor;
11921 x = cursor_x;
11923 else if (row->ends_in_ellipsis_p && glyph == end)
11925 /* Scan back over the ellipsis glyphs, decrementing positions. */
11926 while (glyph > row->glyphs[TEXT_AREA]
11927 && (glyph - 1)->charpos == last_pos)
11928 glyph--, x -= glyph->pixel_width;
11929 /* That loop always goes one position too far,
11930 including the glyph before the ellipsis.
11931 So scan forward over that one. */
11932 x += glyph->pixel_width;
11933 glyph++;
11935 else if (string_start
11936 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11938 /* We may have skipped over point because the previous glyphs
11939 are from string. As there's no easy way to know the
11940 character position of the current glyph, find the correct
11941 glyph on point by scanning from string_start again. */
11942 Lisp_Object limit;
11943 Lisp_Object string;
11944 struct glyph *stop = glyph;
11945 int pos;
11947 limit = make_number (pt_old + 1);
11948 glyph = string_start;
11949 x = string_start_x;
11950 string = glyph->object;
11951 pos = string_buffer_position (w, string, string_before_pos);
11952 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11953 because we always put cursor after overlay strings. */
11954 while (pos == 0 && glyph < stop)
11956 string = glyph->object;
11957 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11958 if (glyph < stop)
11959 pos = string_buffer_position (w, glyph->object, string_before_pos);
11962 while (glyph < stop)
11964 pos = XINT (Fnext_single_char_property_change
11965 (make_number (pos), Qdisplay, Qnil, limit));
11966 if (pos > pt_old)
11967 break;
11968 /* Skip glyphs from the same string. */
11969 string = glyph->object;
11970 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11971 /* Skip glyphs from an overlay. */
11972 while (glyph < stop
11973 && ! string_buffer_position (w, glyph->object, pos))
11975 string = glyph->object;
11976 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11980 /* If we reached the end of the line, and end was from a string,
11981 cursor is not on this line. */
11982 if (glyph == end && row->continued_p)
11983 return 0;
11986 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11987 w->cursor.x = x;
11988 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11989 w->cursor.y = row->y + dy;
11991 if (w == XWINDOW (selected_window))
11993 if (!row->continued_p
11994 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11995 && row->x == 0)
11997 this_line_buffer = XBUFFER (w->buffer);
11999 CHARPOS (this_line_start_pos)
12000 = MATRIX_ROW_START_CHARPOS (row) + delta;
12001 BYTEPOS (this_line_start_pos)
12002 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12004 CHARPOS (this_line_end_pos)
12005 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12006 BYTEPOS (this_line_end_pos)
12007 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12009 this_line_y = w->cursor.y;
12010 this_line_pixel_height = row->height;
12011 this_line_vpos = w->cursor.vpos;
12012 this_line_start_x = row->x;
12014 else
12015 CHARPOS (this_line_start_pos) = 0;
12018 return 1;
12022 /* Run window scroll functions, if any, for WINDOW with new window
12023 start STARTP. Sets the window start of WINDOW to that position.
12025 We assume that the window's buffer is really current. */
12027 static INLINE struct text_pos
12028 run_window_scroll_functions (window, startp)
12029 Lisp_Object window;
12030 struct text_pos startp;
12032 struct window *w = XWINDOW (window);
12033 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12035 if (current_buffer != XBUFFER (w->buffer))
12036 abort ();
12038 if (!NILP (Vwindow_scroll_functions))
12040 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12041 make_number (CHARPOS (startp)));
12042 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12043 /* In case the hook functions switch buffers. */
12044 if (current_buffer != XBUFFER (w->buffer))
12045 set_buffer_internal_1 (XBUFFER (w->buffer));
12048 return startp;
12052 /* Make sure the line containing the cursor is fully visible.
12053 A value of 1 means there is nothing to be done.
12054 (Either the line is fully visible, or it cannot be made so,
12055 or we cannot tell.)
12057 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12058 is higher than window.
12060 A value of 0 means the caller should do scrolling
12061 as if point had gone off the screen. */
12063 static int
12064 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12065 struct window *w;
12066 int force_p;
12067 int current_matrix_p;
12069 struct glyph_matrix *matrix;
12070 struct glyph_row *row;
12071 int window_height;
12073 if (!make_cursor_line_fully_visible_p)
12074 return 1;
12076 /* It's not always possible to find the cursor, e.g, when a window
12077 is full of overlay strings. Don't do anything in that case. */
12078 if (w->cursor.vpos < 0)
12079 return 1;
12081 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12082 row = MATRIX_ROW (matrix, w->cursor.vpos);
12084 /* If the cursor row is not partially visible, there's nothing to do. */
12085 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12086 return 1;
12088 /* If the row the cursor is in is taller than the window's height,
12089 it's not clear what to do, so do nothing. */
12090 window_height = window_box_height (w);
12091 if (row->height >= window_height)
12093 if (!force_p || MINI_WINDOW_P (w)
12094 || w->vscroll || w->cursor.vpos == 0)
12095 return 1;
12097 return 0;
12099 #if 0
12100 /* This code used to try to scroll the window just enough to make
12101 the line visible. It returned 0 to say that the caller should
12102 allocate larger glyph matrices. */
12104 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12106 int dy = row->height - row->visible_height;
12107 w->vscroll = 0;
12108 w->cursor.y += dy;
12109 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12111 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12113 int dy = - (row->height - row->visible_height);
12114 w->vscroll = dy;
12115 w->cursor.y += dy;
12116 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12119 /* When we change the cursor y-position of the selected window,
12120 change this_line_y as well so that the display optimization for
12121 the cursor line of the selected window in redisplay_internal uses
12122 the correct y-position. */
12123 if (w == XWINDOW (selected_window))
12124 this_line_y = w->cursor.y;
12126 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12127 redisplay with larger matrices. */
12128 if (matrix->nrows < required_matrix_height (w))
12130 fonts_changed_p = 1;
12131 return 0;
12134 return 1;
12135 #endif /* 0 */
12139 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12140 non-zero means only WINDOW is redisplayed in redisplay_internal.
12141 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12142 in redisplay_window to bring a partially visible line into view in
12143 the case that only the cursor has moved.
12145 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12146 last screen line's vertical height extends past the end of the screen.
12148 Value is
12150 1 if scrolling succeeded
12152 0 if scrolling didn't find point.
12154 -1 if new fonts have been loaded so that we must interrupt
12155 redisplay, adjust glyph matrices, and try again. */
12157 enum
12159 SCROLLING_SUCCESS,
12160 SCROLLING_FAILED,
12161 SCROLLING_NEED_LARGER_MATRICES
12164 static int
12165 try_scrolling (window, just_this_one_p, scroll_conservatively,
12166 scroll_step, temp_scroll_step, last_line_misfit)
12167 Lisp_Object window;
12168 int just_this_one_p;
12169 EMACS_INT scroll_conservatively, scroll_step;
12170 int temp_scroll_step;
12171 int last_line_misfit;
12173 struct window *w = XWINDOW (window);
12174 struct frame *f = XFRAME (w->frame);
12175 struct text_pos scroll_margin_pos;
12176 struct text_pos pos;
12177 struct text_pos startp;
12178 struct it it;
12179 Lisp_Object window_end;
12180 int this_scroll_margin;
12181 int dy = 0;
12182 int scroll_max;
12183 int rc;
12184 int amount_to_scroll = 0;
12185 Lisp_Object aggressive;
12186 int height;
12187 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12189 #if GLYPH_DEBUG
12190 debug_method_add (w, "try_scrolling");
12191 #endif
12193 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12195 /* Compute scroll margin height in pixels. We scroll when point is
12196 within this distance from the top or bottom of the window. */
12197 if (scroll_margin > 0)
12199 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12200 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12202 else
12203 this_scroll_margin = 0;
12205 /* Force scroll_conservatively to have a reasonable value so it doesn't
12206 cause an overflow while computing how much to scroll. */
12207 if (scroll_conservatively)
12208 scroll_conservatively = min (scroll_conservatively,
12209 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12211 /* Compute how much we should try to scroll maximally to bring point
12212 into view. */
12213 if (scroll_step || scroll_conservatively || temp_scroll_step)
12214 scroll_max = max (scroll_step,
12215 max (scroll_conservatively, temp_scroll_step));
12216 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12217 || NUMBERP (current_buffer->scroll_up_aggressively))
12218 /* We're trying to scroll because of aggressive scrolling
12219 but no scroll_step is set. Choose an arbitrary one. Maybe
12220 there should be a variable for this. */
12221 scroll_max = 10;
12222 else
12223 scroll_max = 0;
12224 scroll_max *= FRAME_LINE_HEIGHT (f);
12226 /* Decide whether we have to scroll down. Start at the window end
12227 and move this_scroll_margin up to find the position of the scroll
12228 margin. */
12229 window_end = Fwindow_end (window, Qt);
12231 too_near_end:
12233 CHARPOS (scroll_margin_pos) = XINT (window_end);
12234 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12236 if (this_scroll_margin || extra_scroll_margin_lines)
12238 start_display (&it, w, scroll_margin_pos);
12239 if (this_scroll_margin)
12240 move_it_vertically_backward (&it, this_scroll_margin);
12241 if (extra_scroll_margin_lines)
12242 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12243 scroll_margin_pos = it.current.pos;
12246 if (PT >= CHARPOS (scroll_margin_pos))
12248 int y0;
12250 /* Point is in the scroll margin at the bottom of the window, or
12251 below. Compute a new window start that makes point visible. */
12253 /* Compute the distance from the scroll margin to PT.
12254 Give up if the distance is greater than scroll_max. */
12255 start_display (&it, w, scroll_margin_pos);
12256 y0 = it.current_y;
12257 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12258 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12260 /* To make point visible, we have to move the window start
12261 down so that the line the cursor is in is visible, which
12262 means we have to add in the height of the cursor line. */
12263 dy = line_bottom_y (&it) - y0;
12265 if (dy > scroll_max)
12266 return SCROLLING_FAILED;
12268 /* Move the window start down. If scrolling conservatively,
12269 move it just enough down to make point visible. If
12270 scroll_step is set, move it down by scroll_step. */
12271 start_display (&it, w, startp);
12273 if (scroll_conservatively)
12274 /* Set AMOUNT_TO_SCROLL to at least one line,
12275 and at most scroll_conservatively lines. */
12276 amount_to_scroll
12277 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12278 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12279 else if (scroll_step || temp_scroll_step)
12280 amount_to_scroll = scroll_max;
12281 else
12283 aggressive = current_buffer->scroll_up_aggressively;
12284 height = WINDOW_BOX_TEXT_HEIGHT (w);
12285 if (NUMBERP (aggressive))
12287 double float_amount = XFLOATINT (aggressive) * height;
12288 amount_to_scroll = float_amount;
12289 if (amount_to_scroll == 0 && float_amount > 0)
12290 amount_to_scroll = 1;
12294 if (amount_to_scroll <= 0)
12295 return SCROLLING_FAILED;
12297 /* If moving by amount_to_scroll leaves STARTP unchanged,
12298 move it down one screen line. */
12300 move_it_vertically (&it, amount_to_scroll);
12301 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12302 move_it_by_lines (&it, 1, 1);
12303 startp = it.current.pos;
12305 else
12307 /* See if point is inside the scroll margin at the top of the
12308 window. */
12309 scroll_margin_pos = startp;
12310 if (this_scroll_margin)
12312 start_display (&it, w, startp);
12313 move_it_vertically (&it, this_scroll_margin);
12314 scroll_margin_pos = it.current.pos;
12317 if (PT < CHARPOS (scroll_margin_pos))
12319 /* Point is in the scroll margin at the top of the window or
12320 above what is displayed in the window. */
12321 int y0;
12323 /* Compute the vertical distance from PT to the scroll
12324 margin position. Give up if distance is greater than
12325 scroll_max. */
12326 SET_TEXT_POS (pos, PT, PT_BYTE);
12327 start_display (&it, w, pos);
12328 y0 = it.current_y;
12329 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12330 it.last_visible_y, -1,
12331 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12332 dy = it.current_y - y0;
12333 if (dy > scroll_max)
12334 return SCROLLING_FAILED;
12336 /* Compute new window start. */
12337 start_display (&it, w, startp);
12339 if (scroll_conservatively)
12340 amount_to_scroll
12341 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12342 else if (scroll_step || temp_scroll_step)
12343 amount_to_scroll = scroll_max;
12344 else
12346 aggressive = current_buffer->scroll_down_aggressively;
12347 height = WINDOW_BOX_TEXT_HEIGHT (w);
12348 if (NUMBERP (aggressive))
12350 double float_amount = XFLOATINT (aggressive) * height;
12351 amount_to_scroll = float_amount;
12352 if (amount_to_scroll == 0 && float_amount > 0)
12353 amount_to_scroll = 1;
12357 if (amount_to_scroll <= 0)
12358 return SCROLLING_FAILED;
12360 move_it_vertically_backward (&it, amount_to_scroll);
12361 startp = it.current.pos;
12365 /* Run window scroll functions. */
12366 startp = run_window_scroll_functions (window, startp);
12368 /* Display the window. Give up if new fonts are loaded, or if point
12369 doesn't appear. */
12370 if (!try_window (window, startp, 0))
12371 rc = SCROLLING_NEED_LARGER_MATRICES;
12372 else if (w->cursor.vpos < 0)
12374 clear_glyph_matrix (w->desired_matrix);
12375 rc = SCROLLING_FAILED;
12377 else
12379 /* Maybe forget recorded base line for line number display. */
12380 if (!just_this_one_p
12381 || current_buffer->clip_changed
12382 || BEG_UNCHANGED < CHARPOS (startp))
12383 w->base_line_number = Qnil;
12385 /* If cursor ends up on a partially visible line,
12386 treat that as being off the bottom of the screen. */
12387 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12389 clear_glyph_matrix (w->desired_matrix);
12390 ++extra_scroll_margin_lines;
12391 goto too_near_end;
12393 rc = SCROLLING_SUCCESS;
12396 return rc;
12400 /* Compute a suitable window start for window W if display of W starts
12401 on a continuation line. Value is non-zero if a new window start
12402 was computed.
12404 The new window start will be computed, based on W's width, starting
12405 from the start of the continued line. It is the start of the
12406 screen line with the minimum distance from the old start W->start. */
12408 static int
12409 compute_window_start_on_continuation_line (w)
12410 struct window *w;
12412 struct text_pos pos, start_pos;
12413 int window_start_changed_p = 0;
12415 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12417 /* If window start is on a continuation line... Window start may be
12418 < BEGV in case there's invisible text at the start of the
12419 buffer (M-x rmail, for example). */
12420 if (CHARPOS (start_pos) > BEGV
12421 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12423 struct it it;
12424 struct glyph_row *row;
12426 /* Handle the case that the window start is out of range. */
12427 if (CHARPOS (start_pos) < BEGV)
12428 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12429 else if (CHARPOS (start_pos) > ZV)
12430 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12432 /* Find the start of the continued line. This should be fast
12433 because scan_buffer is fast (newline cache). */
12434 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12435 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12436 row, DEFAULT_FACE_ID);
12437 reseat_at_previous_visible_line_start (&it);
12439 /* If the line start is "too far" away from the window start,
12440 say it takes too much time to compute a new window start. */
12441 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12442 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12444 int min_distance, distance;
12446 /* Move forward by display lines to find the new window
12447 start. If window width was enlarged, the new start can
12448 be expected to be > the old start. If window width was
12449 decreased, the new window start will be < the old start.
12450 So, we're looking for the display line start with the
12451 minimum distance from the old window start. */
12452 pos = it.current.pos;
12453 min_distance = INFINITY;
12454 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12455 distance < min_distance)
12457 min_distance = distance;
12458 pos = it.current.pos;
12459 move_it_by_lines (&it, 1, 0);
12462 /* Set the window start there. */
12463 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12464 window_start_changed_p = 1;
12468 return window_start_changed_p;
12472 /* Try cursor movement in case text has not changed in window WINDOW,
12473 with window start STARTP. Value is
12475 CURSOR_MOVEMENT_SUCCESS if successful
12477 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12479 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12480 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12481 we want to scroll as if scroll-step were set to 1. See the code.
12483 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12484 which case we have to abort this redisplay, and adjust matrices
12485 first. */
12487 enum
12489 CURSOR_MOVEMENT_SUCCESS,
12490 CURSOR_MOVEMENT_CANNOT_BE_USED,
12491 CURSOR_MOVEMENT_MUST_SCROLL,
12492 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12495 static int
12496 try_cursor_movement (window, startp, scroll_step)
12497 Lisp_Object window;
12498 struct text_pos startp;
12499 int *scroll_step;
12501 struct window *w = XWINDOW (window);
12502 struct frame *f = XFRAME (w->frame);
12503 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12505 #if GLYPH_DEBUG
12506 if (inhibit_try_cursor_movement)
12507 return rc;
12508 #endif
12510 /* Handle case where text has not changed, only point, and it has
12511 not moved off the frame. */
12512 if (/* Point may be in this window. */
12513 PT >= CHARPOS (startp)
12514 /* Selective display hasn't changed. */
12515 && !current_buffer->clip_changed
12516 /* Function force-mode-line-update is used to force a thorough
12517 redisplay. It sets either windows_or_buffers_changed or
12518 update_mode_lines. So don't take a shortcut here for these
12519 cases. */
12520 && !update_mode_lines
12521 && !windows_or_buffers_changed
12522 && !cursor_type_changed
12523 /* Can't use this case if highlighting a region. When a
12524 region exists, cursor movement has to do more than just
12525 set the cursor. */
12526 && !(!NILP (Vtransient_mark_mode)
12527 && !NILP (current_buffer->mark_active))
12528 && NILP (w->region_showing)
12529 && NILP (Vshow_trailing_whitespace)
12530 /* Right after splitting windows, last_point may be nil. */
12531 && INTEGERP (w->last_point)
12532 /* This code is not used for mini-buffer for the sake of the case
12533 of redisplaying to replace an echo area message; since in
12534 that case the mini-buffer contents per se are usually
12535 unchanged. This code is of no real use in the mini-buffer
12536 since the handling of this_line_start_pos, etc., in redisplay
12537 handles the same cases. */
12538 && !EQ (window, minibuf_window)
12539 /* When splitting windows or for new windows, it happens that
12540 redisplay is called with a nil window_end_vpos or one being
12541 larger than the window. This should really be fixed in
12542 window.c. I don't have this on my list, now, so we do
12543 approximately the same as the old redisplay code. --gerd. */
12544 && INTEGERP (w->window_end_vpos)
12545 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12546 && (FRAME_WINDOW_P (f)
12547 || !overlay_arrow_in_current_buffer_p ()))
12549 int this_scroll_margin, top_scroll_margin;
12550 struct glyph_row *row = NULL;
12552 #if GLYPH_DEBUG
12553 debug_method_add (w, "cursor movement");
12554 #endif
12556 /* Scroll if point within this distance from the top or bottom
12557 of the window. This is a pixel value. */
12558 this_scroll_margin = max (0, scroll_margin);
12559 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12560 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12562 top_scroll_margin = this_scroll_margin;
12563 if (WINDOW_WANTS_HEADER_LINE_P (w))
12564 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12566 /* Start with the row the cursor was displayed during the last
12567 not paused redisplay. Give up if that row is not valid. */
12568 if (w->last_cursor.vpos < 0
12569 || w->last_cursor.vpos >= w->current_matrix->nrows)
12570 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12571 else
12573 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12574 if (row->mode_line_p)
12575 ++row;
12576 if (!row->enabled_p)
12577 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12580 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12582 int scroll_p = 0;
12583 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12585 if (PT > XFASTINT (w->last_point))
12587 /* Point has moved forward. */
12588 while (MATRIX_ROW_END_CHARPOS (row) < PT
12589 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12591 xassert (row->enabled_p);
12592 ++row;
12595 /* The end position of a row equals the start position
12596 of the next row. If PT is there, we would rather
12597 display it in the next line. */
12598 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12599 && MATRIX_ROW_END_CHARPOS (row) == PT
12600 && !cursor_row_p (w, row))
12601 ++row;
12603 /* If within the scroll margin, scroll. Note that
12604 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12605 the next line would be drawn, and that
12606 this_scroll_margin can be zero. */
12607 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12608 || PT > MATRIX_ROW_END_CHARPOS (row)
12609 /* Line is completely visible last line in window
12610 and PT is to be set in the next line. */
12611 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12612 && PT == MATRIX_ROW_END_CHARPOS (row)
12613 && !row->ends_at_zv_p
12614 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12615 scroll_p = 1;
12617 else if (PT < XFASTINT (w->last_point))
12619 /* Cursor has to be moved backward. Note that PT >=
12620 CHARPOS (startp) because of the outer if-statement. */
12621 while (!row->mode_line_p
12622 && (MATRIX_ROW_START_CHARPOS (row) > PT
12623 || (MATRIX_ROW_START_CHARPOS (row) == PT
12624 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12625 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12626 row > w->current_matrix->rows
12627 && (row-1)->ends_in_newline_from_string_p))))
12628 && (row->y > top_scroll_margin
12629 || CHARPOS (startp) == BEGV))
12631 xassert (row->enabled_p);
12632 --row;
12635 /* Consider the following case: Window starts at BEGV,
12636 there is invisible, intangible text at BEGV, so that
12637 display starts at some point START > BEGV. It can
12638 happen that we are called with PT somewhere between
12639 BEGV and START. Try to handle that case. */
12640 if (row < w->current_matrix->rows
12641 || row->mode_line_p)
12643 row = w->current_matrix->rows;
12644 if (row->mode_line_p)
12645 ++row;
12648 /* Due to newlines in overlay strings, we may have to
12649 skip forward over overlay strings. */
12650 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12651 && MATRIX_ROW_END_CHARPOS (row) == PT
12652 && !cursor_row_p (w, row))
12653 ++row;
12655 /* If within the scroll margin, scroll. */
12656 if (row->y < top_scroll_margin
12657 && CHARPOS (startp) != BEGV)
12658 scroll_p = 1;
12660 else
12662 /* Cursor did not move. So don't scroll even if cursor line
12663 is partially visible, as it was so before. */
12664 rc = CURSOR_MOVEMENT_SUCCESS;
12667 if (PT < MATRIX_ROW_START_CHARPOS (row)
12668 || PT > MATRIX_ROW_END_CHARPOS (row))
12670 /* if PT is not in the glyph row, give up. */
12671 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12673 else if (rc != CURSOR_MOVEMENT_SUCCESS
12674 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12675 && make_cursor_line_fully_visible_p)
12677 if (PT == MATRIX_ROW_END_CHARPOS (row)
12678 && !row->ends_at_zv_p
12679 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12680 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12681 else if (row->height > window_box_height (w))
12683 /* If we end up in a partially visible line, let's
12684 make it fully visible, except when it's taller
12685 than the window, in which case we can't do much
12686 about it. */
12687 *scroll_step = 1;
12688 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12690 else
12692 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12693 if (!cursor_row_fully_visible_p (w, 0, 1))
12694 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12695 else
12696 rc = CURSOR_MOVEMENT_SUCCESS;
12699 else if (scroll_p)
12700 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12701 else
12705 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12707 rc = CURSOR_MOVEMENT_SUCCESS;
12708 break;
12710 ++row;
12712 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12713 && MATRIX_ROW_START_CHARPOS (row) == PT
12714 && cursor_row_p (w, row));
12719 return rc;
12722 void
12723 set_vertical_scroll_bar (w)
12724 struct window *w;
12726 int start, end, whole;
12728 /* Calculate the start and end positions for the current window.
12729 At some point, it would be nice to choose between scrollbars
12730 which reflect the whole buffer size, with special markers
12731 indicating narrowing, and scrollbars which reflect only the
12732 visible region.
12734 Note that mini-buffers sometimes aren't displaying any text. */
12735 if (!MINI_WINDOW_P (w)
12736 || (w == XWINDOW (minibuf_window)
12737 && NILP (echo_area_buffer[0])))
12739 struct buffer *buf = XBUFFER (w->buffer);
12740 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12741 start = marker_position (w->start) - BUF_BEGV (buf);
12742 /* I don't think this is guaranteed to be right. For the
12743 moment, we'll pretend it is. */
12744 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12746 if (end < start)
12747 end = start;
12748 if (whole < (end - start))
12749 whole = end - start;
12751 else
12752 start = end = whole = 0;
12754 /* Indicate what this scroll bar ought to be displaying now. */
12755 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12759 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12760 selected_window is redisplayed.
12762 We can return without actually redisplaying the window if
12763 fonts_changed_p is nonzero. In that case, redisplay_internal will
12764 retry. */
12766 static void
12767 redisplay_window (window, just_this_one_p)
12768 Lisp_Object window;
12769 int just_this_one_p;
12771 struct window *w = XWINDOW (window);
12772 struct frame *f = XFRAME (w->frame);
12773 struct buffer *buffer = XBUFFER (w->buffer);
12774 struct buffer *old = current_buffer;
12775 struct text_pos lpoint, opoint, startp;
12776 int update_mode_line;
12777 int tem;
12778 struct it it;
12779 /* Record it now because it's overwritten. */
12780 int current_matrix_up_to_date_p = 0;
12781 int used_current_matrix_p = 0;
12782 /* This is less strict than current_matrix_up_to_date_p.
12783 It indictes that the buffer contents and narrowing are unchanged. */
12784 int buffer_unchanged_p = 0;
12785 int temp_scroll_step = 0;
12786 int count = SPECPDL_INDEX ();
12787 int rc;
12788 int centering_position = -1;
12789 int last_line_misfit = 0;
12790 int beg_unchanged, end_unchanged;
12792 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12793 opoint = lpoint;
12795 /* W must be a leaf window here. */
12796 xassert (!NILP (w->buffer));
12797 #if GLYPH_DEBUG
12798 *w->desired_matrix->method = 0;
12799 #endif
12801 specbind (Qinhibit_point_motion_hooks, Qt);
12803 reconsider_clip_changes (w, buffer);
12805 /* Has the mode line to be updated? */
12806 update_mode_line = (!NILP (w->update_mode_line)
12807 || update_mode_lines
12808 || buffer->clip_changed
12809 || buffer->prevent_redisplay_optimizations_p);
12811 if (MINI_WINDOW_P (w))
12813 if (w == XWINDOW (echo_area_window)
12814 && !NILP (echo_area_buffer[0]))
12816 if (update_mode_line)
12817 /* We may have to update a tty frame's menu bar or a
12818 tool-bar. Example `M-x C-h C-h C-g'. */
12819 goto finish_menu_bars;
12820 else
12821 /* We've already displayed the echo area glyphs in this window. */
12822 goto finish_scroll_bars;
12824 else if ((w != XWINDOW (minibuf_window)
12825 || minibuf_level == 0)
12826 /* When buffer is nonempty, redisplay window normally. */
12827 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12828 /* Quail displays non-mini buffers in minibuffer window.
12829 In that case, redisplay the window normally. */
12830 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12832 /* W is a mini-buffer window, but it's not active, so clear
12833 it. */
12834 int yb = window_text_bottom_y (w);
12835 struct glyph_row *row;
12836 int y;
12838 for (y = 0, row = w->desired_matrix->rows;
12839 y < yb;
12840 y += row->height, ++row)
12841 blank_row (w, row, y);
12842 goto finish_scroll_bars;
12845 clear_glyph_matrix (w->desired_matrix);
12848 /* Otherwise set up data on this window; select its buffer and point
12849 value. */
12850 /* Really select the buffer, for the sake of buffer-local
12851 variables. */
12852 set_buffer_internal_1 (XBUFFER (w->buffer));
12853 SET_TEXT_POS (opoint, PT, PT_BYTE);
12855 beg_unchanged = BEG_UNCHANGED;
12856 end_unchanged = END_UNCHANGED;
12858 current_matrix_up_to_date_p
12859 = (!NILP (w->window_end_valid)
12860 && !current_buffer->clip_changed
12861 && !current_buffer->prevent_redisplay_optimizations_p
12862 && XFASTINT (w->last_modified) >= MODIFF
12863 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12865 buffer_unchanged_p
12866 = (!NILP (w->window_end_valid)
12867 && !current_buffer->clip_changed
12868 && XFASTINT (w->last_modified) >= MODIFF
12869 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12871 /* When windows_or_buffers_changed is non-zero, we can't rely on
12872 the window end being valid, so set it to nil there. */
12873 if (windows_or_buffers_changed)
12875 /* If window starts on a continuation line, maybe adjust the
12876 window start in case the window's width changed. */
12877 if (XMARKER (w->start)->buffer == current_buffer)
12878 compute_window_start_on_continuation_line (w);
12880 w->window_end_valid = Qnil;
12883 /* Some sanity checks. */
12884 CHECK_WINDOW_END (w);
12885 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12886 abort ();
12887 if (BYTEPOS (opoint) < CHARPOS (opoint))
12888 abort ();
12890 /* If %c is in mode line, update it if needed. */
12891 if (!NILP (w->column_number_displayed)
12892 /* This alternative quickly identifies a common case
12893 where no change is needed. */
12894 && !(PT == XFASTINT (w->last_point)
12895 && XFASTINT (w->last_modified) >= MODIFF
12896 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12897 && (XFASTINT (w->column_number_displayed)
12898 != (int) current_column ())) /* iftc */
12899 update_mode_line = 1;
12901 /* Count number of windows showing the selected buffer. An indirect
12902 buffer counts as its base buffer. */
12903 if (!just_this_one_p)
12905 struct buffer *current_base, *window_base;
12906 current_base = current_buffer;
12907 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12908 if (current_base->base_buffer)
12909 current_base = current_base->base_buffer;
12910 if (window_base->base_buffer)
12911 window_base = window_base->base_buffer;
12912 if (current_base == window_base)
12913 buffer_shared++;
12916 /* Point refers normally to the selected window. For any other
12917 window, set up appropriate value. */
12918 if (!EQ (window, selected_window))
12920 int new_pt = XMARKER (w->pointm)->charpos;
12921 int new_pt_byte = marker_byte_position (w->pointm);
12922 if (new_pt < BEGV)
12924 new_pt = BEGV;
12925 new_pt_byte = BEGV_BYTE;
12926 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12928 else if (new_pt > (ZV - 1))
12930 new_pt = ZV;
12931 new_pt_byte = ZV_BYTE;
12932 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12935 /* We don't use SET_PT so that the point-motion hooks don't run. */
12936 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12939 /* If any of the character widths specified in the display table
12940 have changed, invalidate the width run cache. It's true that
12941 this may be a bit late to catch such changes, but the rest of
12942 redisplay goes (non-fatally) haywire when the display table is
12943 changed, so why should we worry about doing any better? */
12944 if (current_buffer->width_run_cache)
12946 struct Lisp_Char_Table *disptab = buffer_display_table ();
12948 if (! disptab_matches_widthtab (disptab,
12949 XVECTOR (current_buffer->width_table)))
12951 invalidate_region_cache (current_buffer,
12952 current_buffer->width_run_cache,
12953 BEG, Z);
12954 recompute_width_table (current_buffer, disptab);
12958 /* If window-start is screwed up, choose a new one. */
12959 if (XMARKER (w->start)->buffer != current_buffer)
12960 goto recenter;
12962 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12964 /* If someone specified a new starting point but did not insist,
12965 check whether it can be used. */
12966 if (!NILP (w->optional_new_start)
12967 && CHARPOS (startp) >= BEGV
12968 && CHARPOS (startp) <= ZV)
12970 w->optional_new_start = Qnil;
12971 start_display (&it, w, startp);
12972 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12973 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12974 if (IT_CHARPOS (it) == PT)
12975 w->force_start = Qt;
12976 /* IT may overshoot PT if text at PT is invisible. */
12977 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12978 w->force_start = Qt;
12981 force_start:
12983 /* Handle case where place to start displaying has been specified,
12984 unless the specified location is outside the accessible range. */
12985 if (!NILP (w->force_start)
12986 || w->frozen_window_start_p)
12988 /* We set this later on if we have to adjust point. */
12989 int new_vpos = -1;
12990 int val;
12992 w->force_start = Qnil;
12993 w->vscroll = 0;
12994 w->window_end_valid = Qnil;
12996 /* Forget any recorded base line for line number display. */
12997 if (!buffer_unchanged_p)
12998 w->base_line_number = Qnil;
13000 /* Redisplay the mode line. Select the buffer properly for that.
13001 Also, run the hook window-scroll-functions
13002 because we have scrolled. */
13003 /* Note, we do this after clearing force_start because
13004 if there's an error, it is better to forget about force_start
13005 than to get into an infinite loop calling the hook functions
13006 and having them get more errors. */
13007 if (!update_mode_line
13008 || ! NILP (Vwindow_scroll_functions))
13010 update_mode_line = 1;
13011 w->update_mode_line = Qt;
13012 startp = run_window_scroll_functions (window, startp);
13015 w->last_modified = make_number (0);
13016 w->last_overlay_modified = make_number (0);
13017 if (CHARPOS (startp) < BEGV)
13018 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13019 else if (CHARPOS (startp) > ZV)
13020 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13022 /* Redisplay, then check if cursor has been set during the
13023 redisplay. Give up if new fonts were loaded. */
13024 val = try_window (window, startp, 1);
13025 if (!val)
13027 w->force_start = Qt;
13028 clear_glyph_matrix (w->desired_matrix);
13029 goto need_larger_matrices;
13031 /* Point was outside the scroll margins. */
13032 if (val < 0)
13033 new_vpos = window_box_height (w) / 2;
13035 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13037 /* If point does not appear, try to move point so it does
13038 appear. The desired matrix has been built above, so we
13039 can use it here. */
13040 new_vpos = window_box_height (w) / 2;
13043 if (!cursor_row_fully_visible_p (w, 0, 0))
13045 /* Point does appear, but on a line partly visible at end of window.
13046 Move it back to a fully-visible line. */
13047 new_vpos = window_box_height (w);
13050 /* If we need to move point for either of the above reasons,
13051 now actually do it. */
13052 if (new_vpos >= 0)
13054 struct glyph_row *row;
13056 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13057 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13058 ++row;
13060 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13061 MATRIX_ROW_START_BYTEPOS (row));
13063 if (w != XWINDOW (selected_window))
13064 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13065 else if (current_buffer == old)
13066 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13068 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13070 /* If we are highlighting the region, then we just changed
13071 the region, so redisplay to show it. */
13072 if (!NILP (Vtransient_mark_mode)
13073 && !NILP (current_buffer->mark_active))
13075 clear_glyph_matrix (w->desired_matrix);
13076 if (!try_window (window, startp, 0))
13077 goto need_larger_matrices;
13081 #if GLYPH_DEBUG
13082 debug_method_add (w, "forced window start");
13083 #endif
13084 goto done;
13087 /* Handle case where text has not changed, only point, and it has
13088 not moved off the frame, and we are not retrying after hscroll.
13089 (current_matrix_up_to_date_p is nonzero when retrying.) */
13090 if (current_matrix_up_to_date_p
13091 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13092 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13094 switch (rc)
13096 case CURSOR_MOVEMENT_SUCCESS:
13097 used_current_matrix_p = 1;
13098 goto done;
13100 #if 0 /* try_cursor_movement never returns this value. */
13101 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13102 goto need_larger_matrices;
13103 #endif
13105 case CURSOR_MOVEMENT_MUST_SCROLL:
13106 goto try_to_scroll;
13108 default:
13109 abort ();
13112 /* If current starting point was originally the beginning of a line
13113 but no longer is, find a new starting point. */
13114 else if (!NILP (w->start_at_line_beg)
13115 && !(CHARPOS (startp) <= BEGV
13116 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13118 #if GLYPH_DEBUG
13119 debug_method_add (w, "recenter 1");
13120 #endif
13121 goto recenter;
13124 /* Try scrolling with try_window_id. Value is > 0 if update has
13125 been done, it is -1 if we know that the same window start will
13126 not work. It is 0 if unsuccessful for some other reason. */
13127 else if ((tem = try_window_id (w)) != 0)
13129 #if GLYPH_DEBUG
13130 debug_method_add (w, "try_window_id %d", tem);
13131 #endif
13133 if (fonts_changed_p)
13134 goto need_larger_matrices;
13135 if (tem > 0)
13136 goto done;
13138 /* Otherwise try_window_id has returned -1 which means that we
13139 don't want the alternative below this comment to execute. */
13141 else if (CHARPOS (startp) >= BEGV
13142 && CHARPOS (startp) <= ZV
13143 && PT >= CHARPOS (startp)
13144 && (CHARPOS (startp) < ZV
13145 /* Avoid starting at end of buffer. */
13146 || CHARPOS (startp) == BEGV
13147 || (XFASTINT (w->last_modified) >= MODIFF
13148 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13151 /* If first window line is a continuation line, and window start
13152 is inside the modified region, but the first change is before
13153 current window start, we must select a new window start.
13155 However, if this is the result of a down-mouse event (e.g. by
13156 extending the mouse-drag-overlay), we don't want to select a
13157 new window start, since that would change the position under
13158 the mouse, resulting in an unwanted mouse-movement rather
13159 than a simple mouse-click. */
13160 if (NILP (w->start_at_line_beg)
13161 && NILP (do_mouse_tracking)
13162 && CHARPOS (startp) > BEGV
13163 && CHARPOS (startp) > BEG + beg_unchanged
13164 && CHARPOS (startp) <= Z - end_unchanged)
13166 w->force_start = Qt;
13167 if (XMARKER (w->start)->buffer == current_buffer)
13168 compute_window_start_on_continuation_line (w);
13169 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13170 goto force_start;
13173 #if GLYPH_DEBUG
13174 debug_method_add (w, "same window start");
13175 #endif
13177 /* Try to redisplay starting at same place as before.
13178 If point has not moved off frame, accept the results. */
13179 if (!current_matrix_up_to_date_p
13180 /* Don't use try_window_reusing_current_matrix in this case
13181 because a window scroll function can have changed the
13182 buffer. */
13183 || !NILP (Vwindow_scroll_functions)
13184 || MINI_WINDOW_P (w)
13185 || !(used_current_matrix_p
13186 = try_window_reusing_current_matrix (w)))
13188 IF_DEBUG (debug_method_add (w, "1"));
13189 if (try_window (window, startp, 1) < 0)
13190 /* -1 means we need to scroll.
13191 0 means we need new matrices, but fonts_changed_p
13192 is set in that case, so we will detect it below. */
13193 goto try_to_scroll;
13196 if (fonts_changed_p)
13197 goto need_larger_matrices;
13199 if (w->cursor.vpos >= 0)
13201 if (!just_this_one_p
13202 || current_buffer->clip_changed
13203 || BEG_UNCHANGED < CHARPOS (startp))
13204 /* Forget any recorded base line for line number display. */
13205 w->base_line_number = Qnil;
13207 if (!cursor_row_fully_visible_p (w, 1, 0))
13209 clear_glyph_matrix (w->desired_matrix);
13210 last_line_misfit = 1;
13212 /* Drop through and scroll. */
13213 else
13214 goto done;
13216 else
13217 clear_glyph_matrix (w->desired_matrix);
13220 try_to_scroll:
13222 w->last_modified = make_number (0);
13223 w->last_overlay_modified = make_number (0);
13225 /* Redisplay the mode line. Select the buffer properly for that. */
13226 if (!update_mode_line)
13228 update_mode_line = 1;
13229 w->update_mode_line = Qt;
13232 /* Try to scroll by specified few lines. */
13233 if ((scroll_conservatively
13234 || scroll_step
13235 || temp_scroll_step
13236 || NUMBERP (current_buffer->scroll_up_aggressively)
13237 || NUMBERP (current_buffer->scroll_down_aggressively))
13238 && !current_buffer->clip_changed
13239 && CHARPOS (startp) >= BEGV
13240 && CHARPOS (startp) <= ZV)
13242 /* The function returns -1 if new fonts were loaded, 1 if
13243 successful, 0 if not successful. */
13244 int rc = try_scrolling (window, just_this_one_p,
13245 scroll_conservatively,
13246 scroll_step,
13247 temp_scroll_step, last_line_misfit);
13248 switch (rc)
13250 case SCROLLING_SUCCESS:
13251 goto done;
13253 case SCROLLING_NEED_LARGER_MATRICES:
13254 goto need_larger_matrices;
13256 case SCROLLING_FAILED:
13257 break;
13259 default:
13260 abort ();
13264 /* Finally, just choose place to start which centers point */
13266 recenter:
13267 if (centering_position < 0)
13268 centering_position = window_box_height (w) / 2;
13270 #if GLYPH_DEBUG
13271 debug_method_add (w, "recenter");
13272 #endif
13274 /* w->vscroll = 0; */
13276 /* Forget any previously recorded base line for line number display. */
13277 if (!buffer_unchanged_p)
13278 w->base_line_number = Qnil;
13280 /* Move backward half the height of the window. */
13281 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13282 it.current_y = it.last_visible_y;
13283 move_it_vertically_backward (&it, centering_position);
13284 xassert (IT_CHARPOS (it) >= BEGV);
13286 /* The function move_it_vertically_backward may move over more
13287 than the specified y-distance. If it->w is small, e.g. a
13288 mini-buffer window, we may end up in front of the window's
13289 display area. Start displaying at the start of the line
13290 containing PT in this case. */
13291 if (it.current_y <= 0)
13293 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13294 move_it_vertically_backward (&it, 0);
13295 #if 0
13296 /* I think this assert is bogus if buffer contains
13297 invisible text or images. KFS. */
13298 xassert (IT_CHARPOS (it) <= PT);
13299 #endif
13300 it.current_y = 0;
13303 it.current_x = it.hpos = 0;
13305 /* Set startp here explicitly in case that helps avoid an infinite loop
13306 in case the window-scroll-functions functions get errors. */
13307 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13309 /* Run scroll hooks. */
13310 startp = run_window_scroll_functions (window, it.current.pos);
13312 /* Redisplay the window. */
13313 if (!current_matrix_up_to_date_p
13314 || windows_or_buffers_changed
13315 || cursor_type_changed
13316 /* Don't use try_window_reusing_current_matrix in this case
13317 because it can have changed the buffer. */
13318 || !NILP (Vwindow_scroll_functions)
13319 || !just_this_one_p
13320 || MINI_WINDOW_P (w)
13321 || !(used_current_matrix_p
13322 = try_window_reusing_current_matrix (w)))
13323 try_window (window, startp, 0);
13325 /* If new fonts have been loaded (due to fontsets), give up. We
13326 have to start a new redisplay since we need to re-adjust glyph
13327 matrices. */
13328 if (fonts_changed_p)
13329 goto need_larger_matrices;
13331 /* If cursor did not appear assume that the middle of the window is
13332 in the first line of the window. Do it again with the next line.
13333 (Imagine a window of height 100, displaying two lines of height
13334 60. Moving back 50 from it->last_visible_y will end in the first
13335 line.) */
13336 if (w->cursor.vpos < 0)
13338 if (!NILP (w->window_end_valid)
13339 && PT >= Z - XFASTINT (w->window_end_pos))
13341 clear_glyph_matrix (w->desired_matrix);
13342 move_it_by_lines (&it, 1, 0);
13343 try_window (window, it.current.pos, 0);
13345 else if (PT < IT_CHARPOS (it))
13347 clear_glyph_matrix (w->desired_matrix);
13348 move_it_by_lines (&it, -1, 0);
13349 try_window (window, it.current.pos, 0);
13351 else
13353 /* Not much we can do about it. */
13357 /* Consider the following case: Window starts at BEGV, there is
13358 invisible, intangible text at BEGV, so that display starts at
13359 some point START > BEGV. It can happen that we are called with
13360 PT somewhere between BEGV and START. Try to handle that case. */
13361 if (w->cursor.vpos < 0)
13363 struct glyph_row *row = w->current_matrix->rows;
13364 if (row->mode_line_p)
13365 ++row;
13366 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13369 if (!cursor_row_fully_visible_p (w, 0, 0))
13371 /* If vscroll is enabled, disable it and try again. */
13372 if (w->vscroll)
13374 w->vscroll = 0;
13375 clear_glyph_matrix (w->desired_matrix);
13376 goto recenter;
13379 /* If centering point failed to make the whole line visible,
13380 put point at the top instead. That has to make the whole line
13381 visible, if it can be done. */
13382 if (centering_position == 0)
13383 goto done;
13385 clear_glyph_matrix (w->desired_matrix);
13386 centering_position = 0;
13387 goto recenter;
13390 done:
13392 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13393 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13394 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13395 ? Qt : Qnil);
13397 /* Display the mode line, if we must. */
13398 if ((update_mode_line
13399 /* If window not full width, must redo its mode line
13400 if (a) the window to its side is being redone and
13401 (b) we do a frame-based redisplay. This is a consequence
13402 of how inverted lines are drawn in frame-based redisplay. */
13403 || (!just_this_one_p
13404 && !FRAME_WINDOW_P (f)
13405 && !WINDOW_FULL_WIDTH_P (w))
13406 /* Line number to display. */
13407 || INTEGERP (w->base_line_pos)
13408 /* Column number is displayed and different from the one displayed. */
13409 || (!NILP (w->column_number_displayed)
13410 && (XFASTINT (w->column_number_displayed)
13411 != (int) current_column ()))) /* iftc */
13412 /* This means that the window has a mode line. */
13413 && (WINDOW_WANTS_MODELINE_P (w)
13414 || WINDOW_WANTS_HEADER_LINE_P (w)))
13416 display_mode_lines (w);
13418 /* If mode line height has changed, arrange for a thorough
13419 immediate redisplay using the correct mode line height. */
13420 if (WINDOW_WANTS_MODELINE_P (w)
13421 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13423 fonts_changed_p = 1;
13424 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13425 = DESIRED_MODE_LINE_HEIGHT (w);
13428 /* If top line height has changed, arrange for a thorough
13429 immediate redisplay using the correct mode line height. */
13430 if (WINDOW_WANTS_HEADER_LINE_P (w)
13431 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13433 fonts_changed_p = 1;
13434 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13435 = DESIRED_HEADER_LINE_HEIGHT (w);
13438 if (fonts_changed_p)
13439 goto need_larger_matrices;
13442 if (!line_number_displayed
13443 && !BUFFERP (w->base_line_pos))
13445 w->base_line_pos = Qnil;
13446 w->base_line_number = Qnil;
13449 finish_menu_bars:
13451 /* When we reach a frame's selected window, redo the frame's menu bar. */
13452 if (update_mode_line
13453 && EQ (FRAME_SELECTED_WINDOW (f), window))
13455 int redisplay_menu_p = 0;
13456 int redisplay_tool_bar_p = 0;
13458 if (FRAME_WINDOW_P (f))
13460 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13461 || defined (USE_GTK)
13462 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13463 #else
13464 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13465 #endif
13467 else
13468 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13470 if (redisplay_menu_p)
13471 display_menu_bar (w);
13473 #ifdef HAVE_WINDOW_SYSTEM
13474 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13475 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13476 #else
13477 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13478 && (FRAME_TOOL_BAR_LINES (f) > 0
13479 || !NILP (Vauto_resize_tool_bars));
13481 #endif
13483 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13485 extern int ignore_mouse_drag_p;
13486 ignore_mouse_drag_p = 1;
13488 #endif
13491 #ifdef HAVE_WINDOW_SYSTEM
13492 if (FRAME_WINDOW_P (f)
13493 && update_window_fringes (w, (just_this_one_p
13494 || (!used_current_matrix_p && !overlay_arrow_seen)
13495 || w->pseudo_window_p)))
13497 update_begin (f);
13498 BLOCK_INPUT;
13499 if (draw_window_fringes (w, 1))
13500 x_draw_vertical_border (w);
13501 UNBLOCK_INPUT;
13502 update_end (f);
13504 #endif /* HAVE_WINDOW_SYSTEM */
13506 /* We go to this label, with fonts_changed_p nonzero,
13507 if it is necessary to try again using larger glyph matrices.
13508 We have to redeem the scroll bar even in this case,
13509 because the loop in redisplay_internal expects that. */
13510 need_larger_matrices:
13512 finish_scroll_bars:
13514 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13516 /* Set the thumb's position and size. */
13517 set_vertical_scroll_bar (w);
13519 /* Note that we actually used the scroll bar attached to this
13520 window, so it shouldn't be deleted at the end of redisplay. */
13521 redeem_scroll_bar_hook (w);
13524 /* Restore current_buffer and value of point in it. */
13525 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13526 set_buffer_internal_1 (old);
13527 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13528 shorter. This can be caused by log truncation in *Messages*. */
13529 if (CHARPOS (lpoint) <= ZV)
13530 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13532 unbind_to (count, Qnil);
13536 /* Build the complete desired matrix of WINDOW with a window start
13537 buffer position POS.
13539 Value is 1 if successful. It is zero if fonts were loaded during
13540 redisplay which makes re-adjusting glyph matrices necessary, and -1
13541 if point would appear in the scroll margins.
13542 (We check that only if CHECK_MARGINS is nonzero. */
13545 try_window (window, pos, check_margins)
13546 Lisp_Object window;
13547 struct text_pos pos;
13548 int check_margins;
13550 struct window *w = XWINDOW (window);
13551 struct it it;
13552 struct glyph_row *last_text_row = NULL;
13553 struct frame *f = XFRAME (w->frame);
13555 /* Make POS the new window start. */
13556 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13558 /* Mark cursor position as unknown. No overlay arrow seen. */
13559 w->cursor.vpos = -1;
13560 overlay_arrow_seen = 0;
13562 /* Initialize iterator and info to start at POS. */
13563 start_display (&it, w, pos);
13565 /* Display all lines of W. */
13566 while (it.current_y < it.last_visible_y)
13568 if (display_line (&it))
13569 last_text_row = it.glyph_row - 1;
13570 if (fonts_changed_p)
13571 return 0;
13574 /* Don't let the cursor end in the scroll margins. */
13575 if (check_margins
13576 && !MINI_WINDOW_P (w))
13578 int this_scroll_margin;
13580 this_scroll_margin = max (0, scroll_margin);
13581 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13582 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13584 if ((w->cursor.y >= 0 /* not vscrolled */
13585 && w->cursor.y < this_scroll_margin
13586 && CHARPOS (pos) > BEGV
13587 && IT_CHARPOS (it) < ZV)
13588 /* rms: considering make_cursor_line_fully_visible_p here
13589 seems to give wrong results. We don't want to recenter
13590 when the last line is partly visible, we want to allow
13591 that case to be handled in the usual way. */
13592 || (w->cursor.y + 1) > it.last_visible_y)
13594 w->cursor.vpos = -1;
13595 clear_glyph_matrix (w->desired_matrix);
13596 return -1;
13600 /* If bottom moved off end of frame, change mode line percentage. */
13601 if (XFASTINT (w->window_end_pos) <= 0
13602 && Z != IT_CHARPOS (it))
13603 w->update_mode_line = Qt;
13605 /* Set window_end_pos to the offset of the last character displayed
13606 on the window from the end of current_buffer. Set
13607 window_end_vpos to its row number. */
13608 if (last_text_row)
13610 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13611 w->window_end_bytepos
13612 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13613 w->window_end_pos
13614 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13615 w->window_end_vpos
13616 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13617 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13618 ->displays_text_p);
13620 else
13622 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13623 w->window_end_pos = make_number (Z - ZV);
13624 w->window_end_vpos = make_number (0);
13627 /* But that is not valid info until redisplay finishes. */
13628 w->window_end_valid = Qnil;
13629 return 1;
13634 /************************************************************************
13635 Window redisplay reusing current matrix when buffer has not changed
13636 ************************************************************************/
13638 /* Try redisplay of window W showing an unchanged buffer with a
13639 different window start than the last time it was displayed by
13640 reusing its current matrix. Value is non-zero if successful.
13641 W->start is the new window start. */
13643 static int
13644 try_window_reusing_current_matrix (w)
13645 struct window *w;
13647 struct frame *f = XFRAME (w->frame);
13648 struct glyph_row *row, *bottom_row;
13649 struct it it;
13650 struct run run;
13651 struct text_pos start, new_start;
13652 int nrows_scrolled, i;
13653 struct glyph_row *last_text_row;
13654 struct glyph_row *last_reused_text_row;
13655 struct glyph_row *start_row;
13656 int start_vpos, min_y, max_y;
13658 #if GLYPH_DEBUG
13659 if (inhibit_try_window_reusing)
13660 return 0;
13661 #endif
13663 if (/* This function doesn't handle terminal frames. */
13664 !FRAME_WINDOW_P (f)
13665 /* Don't try to reuse the display if windows have been split
13666 or such. */
13667 || windows_or_buffers_changed
13668 || cursor_type_changed)
13669 return 0;
13671 /* Can't do this if region may have changed. */
13672 if ((!NILP (Vtransient_mark_mode)
13673 && !NILP (current_buffer->mark_active))
13674 || !NILP (w->region_showing)
13675 || !NILP (Vshow_trailing_whitespace))
13676 return 0;
13678 /* If top-line visibility has changed, give up. */
13679 if (WINDOW_WANTS_HEADER_LINE_P (w)
13680 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13681 return 0;
13683 /* Give up if old or new display is scrolled vertically. We could
13684 make this function handle this, but right now it doesn't. */
13685 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13686 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13687 return 0;
13689 /* The variable new_start now holds the new window start. The old
13690 start `start' can be determined from the current matrix. */
13691 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13692 start = start_row->start.pos;
13693 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13695 /* Clear the desired matrix for the display below. */
13696 clear_glyph_matrix (w->desired_matrix);
13698 if (CHARPOS (new_start) <= CHARPOS (start))
13700 int first_row_y;
13702 /* Don't use this method if the display starts with an ellipsis
13703 displayed for invisible text. It's not easy to handle that case
13704 below, and it's certainly not worth the effort since this is
13705 not a frequent case. */
13706 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13707 return 0;
13709 IF_DEBUG (debug_method_add (w, "twu1"));
13711 /* Display up to a row that can be reused. The variable
13712 last_text_row is set to the last row displayed that displays
13713 text. Note that it.vpos == 0 if or if not there is a
13714 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13715 start_display (&it, w, new_start);
13716 first_row_y = it.current_y;
13717 w->cursor.vpos = -1;
13718 last_text_row = last_reused_text_row = NULL;
13720 while (it.current_y < it.last_visible_y
13721 && !fonts_changed_p)
13723 /* If we have reached into the characters in the START row,
13724 that means the line boundaries have changed. So we
13725 can't start copying with the row START. Maybe it will
13726 work to start copying with the following row. */
13727 while (IT_CHARPOS (it) > CHARPOS (start))
13729 /* Advance to the next row as the "start". */
13730 start_row++;
13731 start = start_row->start.pos;
13732 /* If there are no more rows to try, or just one, give up. */
13733 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13734 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13735 || CHARPOS (start) == ZV)
13737 clear_glyph_matrix (w->desired_matrix);
13738 return 0;
13741 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13743 /* If we have reached alignment,
13744 we can copy the rest of the rows. */
13745 if (IT_CHARPOS (it) == CHARPOS (start))
13746 break;
13748 if (display_line (&it))
13749 last_text_row = it.glyph_row - 1;
13752 /* A value of current_y < last_visible_y means that we stopped
13753 at the previous window start, which in turn means that we
13754 have at least one reusable row. */
13755 if (it.current_y < it.last_visible_y)
13757 /* IT.vpos always starts from 0; it counts text lines. */
13758 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13760 /* Find PT if not already found in the lines displayed. */
13761 if (w->cursor.vpos < 0)
13763 int dy = it.current_y - start_row->y;
13765 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13766 row = row_containing_pos (w, PT, row, NULL, dy);
13767 if (row)
13768 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13769 dy, nrows_scrolled);
13770 else
13772 clear_glyph_matrix (w->desired_matrix);
13773 return 0;
13777 /* Scroll the display. Do it before the current matrix is
13778 changed. The problem here is that update has not yet
13779 run, i.e. part of the current matrix is not up to date.
13780 scroll_run_hook will clear the cursor, and use the
13781 current matrix to get the height of the row the cursor is
13782 in. */
13783 run.current_y = start_row->y;
13784 run.desired_y = it.current_y;
13785 run.height = it.last_visible_y - it.current_y;
13787 if (run.height > 0 && run.current_y != run.desired_y)
13789 update_begin (f);
13790 rif->update_window_begin_hook (w);
13791 rif->clear_window_mouse_face (w);
13792 rif->scroll_run_hook (w, &run);
13793 rif->update_window_end_hook (w, 0, 0);
13794 update_end (f);
13797 /* Shift current matrix down by nrows_scrolled lines. */
13798 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13799 rotate_matrix (w->current_matrix,
13800 start_vpos,
13801 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13802 nrows_scrolled);
13804 /* Disable lines that must be updated. */
13805 for (i = 0; i < nrows_scrolled; ++i)
13806 (start_row + i)->enabled_p = 0;
13808 /* Re-compute Y positions. */
13809 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13810 max_y = it.last_visible_y;
13811 for (row = start_row + nrows_scrolled;
13812 row < bottom_row;
13813 ++row)
13815 row->y = it.current_y;
13816 row->visible_height = row->height;
13818 if (row->y < min_y)
13819 row->visible_height -= min_y - row->y;
13820 if (row->y + row->height > max_y)
13821 row->visible_height -= row->y + row->height - max_y;
13822 row->redraw_fringe_bitmaps_p = 1;
13824 it.current_y += row->height;
13826 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13827 last_reused_text_row = row;
13828 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13829 break;
13832 /* Disable lines in the current matrix which are now
13833 below the window. */
13834 for (++row; row < bottom_row; ++row)
13835 row->enabled_p = row->mode_line_p = 0;
13838 /* Update window_end_pos etc.; last_reused_text_row is the last
13839 reused row from the current matrix containing text, if any.
13840 The value of last_text_row is the last displayed line
13841 containing text. */
13842 if (last_reused_text_row)
13844 w->window_end_bytepos
13845 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13846 w->window_end_pos
13847 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13848 w->window_end_vpos
13849 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13850 w->current_matrix));
13852 else if (last_text_row)
13854 w->window_end_bytepos
13855 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13856 w->window_end_pos
13857 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13858 w->window_end_vpos
13859 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13861 else
13863 /* This window must be completely empty. */
13864 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13865 w->window_end_pos = make_number (Z - ZV);
13866 w->window_end_vpos = make_number (0);
13868 w->window_end_valid = Qnil;
13870 /* Update hint: don't try scrolling again in update_window. */
13871 w->desired_matrix->no_scrolling_p = 1;
13873 #if GLYPH_DEBUG
13874 debug_method_add (w, "try_window_reusing_current_matrix 1");
13875 #endif
13876 return 1;
13878 else if (CHARPOS (new_start) > CHARPOS (start))
13880 struct glyph_row *pt_row, *row;
13881 struct glyph_row *first_reusable_row;
13882 struct glyph_row *first_row_to_display;
13883 int dy;
13884 int yb = window_text_bottom_y (w);
13886 /* Find the row starting at new_start, if there is one. Don't
13887 reuse a partially visible line at the end. */
13888 first_reusable_row = start_row;
13889 while (first_reusable_row->enabled_p
13890 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13891 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13892 < CHARPOS (new_start)))
13893 ++first_reusable_row;
13895 /* Give up if there is no row to reuse. */
13896 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13897 || !first_reusable_row->enabled_p
13898 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13899 != CHARPOS (new_start)))
13900 return 0;
13902 /* We can reuse fully visible rows beginning with
13903 first_reusable_row to the end of the window. Set
13904 first_row_to_display to the first row that cannot be reused.
13905 Set pt_row to the row containing point, if there is any. */
13906 pt_row = NULL;
13907 for (first_row_to_display = first_reusable_row;
13908 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13909 ++first_row_to_display)
13911 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13912 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13913 pt_row = first_row_to_display;
13916 /* Start displaying at the start of first_row_to_display. */
13917 xassert (first_row_to_display->y < yb);
13918 init_to_row_start (&it, w, first_row_to_display);
13920 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13921 - start_vpos);
13922 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13923 - nrows_scrolled);
13924 it.current_y = (first_row_to_display->y - first_reusable_row->y
13925 + WINDOW_HEADER_LINE_HEIGHT (w));
13927 /* Display lines beginning with first_row_to_display in the
13928 desired matrix. Set last_text_row to the last row displayed
13929 that displays text. */
13930 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13931 if (pt_row == NULL)
13932 w->cursor.vpos = -1;
13933 last_text_row = NULL;
13934 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13935 if (display_line (&it))
13936 last_text_row = it.glyph_row - 1;
13938 /* Give up If point isn't in a row displayed or reused. */
13939 if (w->cursor.vpos < 0)
13941 clear_glyph_matrix (w->desired_matrix);
13942 return 0;
13945 /* If point is in a reused row, adjust y and vpos of the cursor
13946 position. */
13947 if (pt_row)
13949 w->cursor.vpos -= nrows_scrolled;
13950 w->cursor.y -= first_reusable_row->y - start_row->y;
13953 /* Scroll the display. */
13954 run.current_y = first_reusable_row->y;
13955 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13956 run.height = it.last_visible_y - run.current_y;
13957 dy = run.current_y - run.desired_y;
13959 if (run.height)
13961 update_begin (f);
13962 rif->update_window_begin_hook (w);
13963 rif->clear_window_mouse_face (w);
13964 rif->scroll_run_hook (w, &run);
13965 rif->update_window_end_hook (w, 0, 0);
13966 update_end (f);
13969 /* Adjust Y positions of reused rows. */
13970 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13971 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13972 max_y = it.last_visible_y;
13973 for (row = first_reusable_row; row < first_row_to_display; ++row)
13975 row->y -= dy;
13976 row->visible_height = row->height;
13977 if (row->y < min_y)
13978 row->visible_height -= min_y - row->y;
13979 if (row->y + row->height > max_y)
13980 row->visible_height -= row->y + row->height - max_y;
13981 row->redraw_fringe_bitmaps_p = 1;
13984 /* Scroll the current matrix. */
13985 xassert (nrows_scrolled > 0);
13986 rotate_matrix (w->current_matrix,
13987 start_vpos,
13988 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13989 -nrows_scrolled);
13991 /* Disable rows not reused. */
13992 for (row -= nrows_scrolled; row < bottom_row; ++row)
13993 row->enabled_p = 0;
13995 /* Point may have moved to a different line, so we cannot assume that
13996 the previous cursor position is valid; locate the correct row. */
13997 if (pt_row)
13999 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14000 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14001 row++)
14003 w->cursor.vpos++;
14004 w->cursor.y = row->y;
14006 if (row < bottom_row)
14008 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14009 while (glyph->charpos < PT)
14011 w->cursor.hpos++;
14012 w->cursor.x += glyph->pixel_width;
14013 glyph++;
14018 /* Adjust window end. A null value of last_text_row means that
14019 the window end is in reused rows which in turn means that
14020 only its vpos can have changed. */
14021 if (last_text_row)
14023 w->window_end_bytepos
14024 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14025 w->window_end_pos
14026 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14027 w->window_end_vpos
14028 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14030 else
14032 w->window_end_vpos
14033 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14036 w->window_end_valid = Qnil;
14037 w->desired_matrix->no_scrolling_p = 1;
14039 #if GLYPH_DEBUG
14040 debug_method_add (w, "try_window_reusing_current_matrix 2");
14041 #endif
14042 return 1;
14045 return 0;
14050 /************************************************************************
14051 Window redisplay reusing current matrix when buffer has changed
14052 ************************************************************************/
14054 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14055 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14056 int *, int *));
14057 static struct glyph_row *
14058 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14059 struct glyph_row *));
14062 /* Return the last row in MATRIX displaying text. If row START is
14063 non-null, start searching with that row. IT gives the dimensions
14064 of the display. Value is null if matrix is empty; otherwise it is
14065 a pointer to the row found. */
14067 static struct glyph_row *
14068 find_last_row_displaying_text (matrix, it, start)
14069 struct glyph_matrix *matrix;
14070 struct it *it;
14071 struct glyph_row *start;
14073 struct glyph_row *row, *row_found;
14075 /* Set row_found to the last row in IT->w's current matrix
14076 displaying text. The loop looks funny but think of partially
14077 visible lines. */
14078 row_found = NULL;
14079 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14080 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14082 xassert (row->enabled_p);
14083 row_found = row;
14084 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14085 break;
14086 ++row;
14089 return row_found;
14093 /* Return the last row in the current matrix of W that is not affected
14094 by changes at the start of current_buffer that occurred since W's
14095 current matrix was built. Value is null if no such row exists.
14097 BEG_UNCHANGED us the number of characters unchanged at the start of
14098 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14099 first changed character in current_buffer. Characters at positions <
14100 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14101 when the current matrix was built. */
14103 static struct glyph_row *
14104 find_last_unchanged_at_beg_row (w)
14105 struct window *w;
14107 int first_changed_pos = BEG + BEG_UNCHANGED;
14108 struct glyph_row *row;
14109 struct glyph_row *row_found = NULL;
14110 int yb = window_text_bottom_y (w);
14112 /* Find the last row displaying unchanged text. */
14113 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14114 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14115 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14117 if (/* If row ends before first_changed_pos, it is unchanged,
14118 except in some case. */
14119 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14120 /* When row ends in ZV and we write at ZV it is not
14121 unchanged. */
14122 && !row->ends_at_zv_p
14123 /* When first_changed_pos is the end of a continued line,
14124 row is not unchanged because it may be no longer
14125 continued. */
14126 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14127 && (row->continued_p
14128 || row->exact_window_width_line_p)))
14129 row_found = row;
14131 /* Stop if last visible row. */
14132 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14133 break;
14135 ++row;
14138 return row_found;
14142 /* Find the first glyph row in the current matrix of W that is not
14143 affected by changes at the end of current_buffer since the
14144 time W's current matrix was built.
14146 Return in *DELTA the number of chars by which buffer positions in
14147 unchanged text at the end of current_buffer must be adjusted.
14149 Return in *DELTA_BYTES the corresponding number of bytes.
14151 Value is null if no such row exists, i.e. all rows are affected by
14152 changes. */
14154 static struct glyph_row *
14155 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14156 struct window *w;
14157 int *delta, *delta_bytes;
14159 struct glyph_row *row;
14160 struct glyph_row *row_found = NULL;
14162 *delta = *delta_bytes = 0;
14164 /* Display must not have been paused, otherwise the current matrix
14165 is not up to date. */
14166 if (NILP (w->window_end_valid))
14167 abort ();
14169 /* A value of window_end_pos >= END_UNCHANGED means that the window
14170 end is in the range of changed text. If so, there is no
14171 unchanged row at the end of W's current matrix. */
14172 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14173 return NULL;
14175 /* Set row to the last row in W's current matrix displaying text. */
14176 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14178 /* If matrix is entirely empty, no unchanged row exists. */
14179 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14181 /* The value of row is the last glyph row in the matrix having a
14182 meaningful buffer position in it. The end position of row
14183 corresponds to window_end_pos. This allows us to translate
14184 buffer positions in the current matrix to current buffer
14185 positions for characters not in changed text. */
14186 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14187 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14188 int last_unchanged_pos, last_unchanged_pos_old;
14189 struct glyph_row *first_text_row
14190 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14192 *delta = Z - Z_old;
14193 *delta_bytes = Z_BYTE - Z_BYTE_old;
14195 /* Set last_unchanged_pos to the buffer position of the last
14196 character in the buffer that has not been changed. Z is the
14197 index + 1 of the last character in current_buffer, i.e. by
14198 subtracting END_UNCHANGED we get the index of the last
14199 unchanged character, and we have to add BEG to get its buffer
14200 position. */
14201 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14202 last_unchanged_pos_old = last_unchanged_pos - *delta;
14204 /* Search backward from ROW for a row displaying a line that
14205 starts at a minimum position >= last_unchanged_pos_old. */
14206 for (; row > first_text_row; --row)
14208 /* This used to abort, but it can happen.
14209 It is ok to just stop the search instead here. KFS. */
14210 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14211 break;
14213 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14214 row_found = row;
14218 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14219 abort ();
14221 return row_found;
14225 /* Make sure that glyph rows in the current matrix of window W
14226 reference the same glyph memory as corresponding rows in the
14227 frame's frame matrix. This function is called after scrolling W's
14228 current matrix on a terminal frame in try_window_id and
14229 try_window_reusing_current_matrix. */
14231 static void
14232 sync_frame_with_window_matrix_rows (w)
14233 struct window *w;
14235 struct frame *f = XFRAME (w->frame);
14236 struct glyph_row *window_row, *window_row_end, *frame_row;
14238 /* Preconditions: W must be a leaf window and full-width. Its frame
14239 must have a frame matrix. */
14240 xassert (NILP (w->hchild) && NILP (w->vchild));
14241 xassert (WINDOW_FULL_WIDTH_P (w));
14242 xassert (!FRAME_WINDOW_P (f));
14244 /* If W is a full-width window, glyph pointers in W's current matrix
14245 have, by definition, to be the same as glyph pointers in the
14246 corresponding frame matrix. Note that frame matrices have no
14247 marginal areas (see build_frame_matrix). */
14248 window_row = w->current_matrix->rows;
14249 window_row_end = window_row + w->current_matrix->nrows;
14250 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14251 while (window_row < window_row_end)
14253 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14254 struct glyph *end = window_row->glyphs[LAST_AREA];
14256 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14257 frame_row->glyphs[TEXT_AREA] = start;
14258 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14259 frame_row->glyphs[LAST_AREA] = end;
14261 /* Disable frame rows whose corresponding window rows have
14262 been disabled in try_window_id. */
14263 if (!window_row->enabled_p)
14264 frame_row->enabled_p = 0;
14266 ++window_row, ++frame_row;
14271 /* Find the glyph row in window W containing CHARPOS. Consider all
14272 rows between START and END (not inclusive). END null means search
14273 all rows to the end of the display area of W. Value is the row
14274 containing CHARPOS or null. */
14276 struct glyph_row *
14277 row_containing_pos (w, charpos, start, end, dy)
14278 struct window *w;
14279 int charpos;
14280 struct glyph_row *start, *end;
14281 int dy;
14283 struct glyph_row *row = start;
14284 int last_y;
14286 /* If we happen to start on a header-line, skip that. */
14287 if (row->mode_line_p)
14288 ++row;
14290 if ((end && row >= end) || !row->enabled_p)
14291 return NULL;
14293 last_y = window_text_bottom_y (w) - dy;
14295 while (1)
14297 /* Give up if we have gone too far. */
14298 if (end && row >= end)
14299 return NULL;
14300 /* This formerly returned if they were equal.
14301 I think that both quantities are of a "last plus one" type;
14302 if so, when they are equal, the row is within the screen. -- rms. */
14303 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14304 return NULL;
14306 /* If it is in this row, return this row. */
14307 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14308 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14309 /* The end position of a row equals the start
14310 position of the next row. If CHARPOS is there, we
14311 would rather display it in the next line, except
14312 when this line ends in ZV. */
14313 && !row->ends_at_zv_p
14314 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14315 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14316 return row;
14317 ++row;
14322 /* Try to redisplay window W by reusing its existing display. W's
14323 current matrix must be up to date when this function is called,
14324 i.e. window_end_valid must not be nil.
14326 Value is
14328 1 if display has been updated
14329 0 if otherwise unsuccessful
14330 -1 if redisplay with same window start is known not to succeed
14332 The following steps are performed:
14334 1. Find the last row in the current matrix of W that is not
14335 affected by changes at the start of current_buffer. If no such row
14336 is found, give up.
14338 2. Find the first row in W's current matrix that is not affected by
14339 changes at the end of current_buffer. Maybe there is no such row.
14341 3. Display lines beginning with the row + 1 found in step 1 to the
14342 row found in step 2 or, if step 2 didn't find a row, to the end of
14343 the window.
14345 4. If cursor is not known to appear on the window, give up.
14347 5. If display stopped at the row found in step 2, scroll the
14348 display and current matrix as needed.
14350 6. Maybe display some lines at the end of W, if we must. This can
14351 happen under various circumstances, like a partially visible line
14352 becoming fully visible, or because newly displayed lines are displayed
14353 in smaller font sizes.
14355 7. Update W's window end information. */
14357 static int
14358 try_window_id (w)
14359 struct window *w;
14361 struct frame *f = XFRAME (w->frame);
14362 struct glyph_matrix *current_matrix = w->current_matrix;
14363 struct glyph_matrix *desired_matrix = w->desired_matrix;
14364 struct glyph_row *last_unchanged_at_beg_row;
14365 struct glyph_row *first_unchanged_at_end_row;
14366 struct glyph_row *row;
14367 struct glyph_row *bottom_row;
14368 int bottom_vpos;
14369 struct it it;
14370 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14371 struct text_pos start_pos;
14372 struct run run;
14373 int first_unchanged_at_end_vpos = 0;
14374 struct glyph_row *last_text_row, *last_text_row_at_end;
14375 struct text_pos start;
14376 int first_changed_charpos, last_changed_charpos;
14378 #if GLYPH_DEBUG
14379 if (inhibit_try_window_id)
14380 return 0;
14381 #endif
14383 /* This is handy for debugging. */
14384 #if 0
14385 #define GIVE_UP(X) \
14386 do { \
14387 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14388 return 0; \
14389 } while (0)
14390 #else
14391 #define GIVE_UP(X) return 0
14392 #endif
14394 SET_TEXT_POS_FROM_MARKER (start, w->start);
14396 /* Don't use this for mini-windows because these can show
14397 messages and mini-buffers, and we don't handle that here. */
14398 if (MINI_WINDOW_P (w))
14399 GIVE_UP (1);
14401 /* This flag is used to prevent redisplay optimizations. */
14402 if (windows_or_buffers_changed || cursor_type_changed)
14403 GIVE_UP (2);
14405 /* Verify that narrowing has not changed.
14406 Also verify that we were not told to prevent redisplay optimizations.
14407 It would be nice to further
14408 reduce the number of cases where this prevents try_window_id. */
14409 if (current_buffer->clip_changed
14410 || current_buffer->prevent_redisplay_optimizations_p)
14411 GIVE_UP (3);
14413 /* Window must either use window-based redisplay or be full width. */
14414 if (!FRAME_WINDOW_P (f)
14415 && (!line_ins_del_ok
14416 || !WINDOW_FULL_WIDTH_P (w)))
14417 GIVE_UP (4);
14419 /* Give up if point is not known NOT to appear in W. */
14420 if (PT < CHARPOS (start))
14421 GIVE_UP (5);
14423 /* Another way to prevent redisplay optimizations. */
14424 if (XFASTINT (w->last_modified) == 0)
14425 GIVE_UP (6);
14427 /* Verify that window is not hscrolled. */
14428 if (XFASTINT (w->hscroll) != 0)
14429 GIVE_UP (7);
14431 /* Verify that display wasn't paused. */
14432 if (NILP (w->window_end_valid))
14433 GIVE_UP (8);
14435 /* Can't use this if highlighting a region because a cursor movement
14436 will do more than just set the cursor. */
14437 if (!NILP (Vtransient_mark_mode)
14438 && !NILP (current_buffer->mark_active))
14439 GIVE_UP (9);
14441 /* Likewise if highlighting trailing whitespace. */
14442 if (!NILP (Vshow_trailing_whitespace))
14443 GIVE_UP (11);
14445 /* Likewise if showing a region. */
14446 if (!NILP (w->region_showing))
14447 GIVE_UP (10);
14449 /* Can use this if overlay arrow position and or string have changed. */
14450 if (overlay_arrows_changed_p ())
14451 GIVE_UP (12);
14454 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14455 only if buffer has really changed. The reason is that the gap is
14456 initially at Z for freshly visited files. The code below would
14457 set end_unchanged to 0 in that case. */
14458 if (MODIFF > SAVE_MODIFF
14459 /* This seems to happen sometimes after saving a buffer. */
14460 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14462 if (GPT - BEG < BEG_UNCHANGED)
14463 BEG_UNCHANGED = GPT - BEG;
14464 if (Z - GPT < END_UNCHANGED)
14465 END_UNCHANGED = Z - GPT;
14468 /* The position of the first and last character that has been changed. */
14469 first_changed_charpos = BEG + BEG_UNCHANGED;
14470 last_changed_charpos = Z - END_UNCHANGED;
14472 /* If window starts after a line end, and the last change is in
14473 front of that newline, then changes don't affect the display.
14474 This case happens with stealth-fontification. Note that although
14475 the display is unchanged, glyph positions in the matrix have to
14476 be adjusted, of course. */
14477 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14478 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14479 && ((last_changed_charpos < CHARPOS (start)
14480 && CHARPOS (start) == BEGV)
14481 || (last_changed_charpos < CHARPOS (start) - 1
14482 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14484 int Z_old, delta, Z_BYTE_old, delta_bytes;
14485 struct glyph_row *r0;
14487 /* Compute how many chars/bytes have been added to or removed
14488 from the buffer. */
14489 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14490 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14491 delta = Z - Z_old;
14492 delta_bytes = Z_BYTE - Z_BYTE_old;
14494 /* Give up if PT is not in the window. Note that it already has
14495 been checked at the start of try_window_id that PT is not in
14496 front of the window start. */
14497 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14498 GIVE_UP (13);
14500 /* If window start is unchanged, we can reuse the whole matrix
14501 as is, after adjusting glyph positions. No need to compute
14502 the window end again, since its offset from Z hasn't changed. */
14503 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14504 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14505 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14506 /* PT must not be in a partially visible line. */
14507 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14508 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14510 /* Adjust positions in the glyph matrix. */
14511 if (delta || delta_bytes)
14513 struct glyph_row *r1
14514 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14515 increment_matrix_positions (w->current_matrix,
14516 MATRIX_ROW_VPOS (r0, current_matrix),
14517 MATRIX_ROW_VPOS (r1, current_matrix),
14518 delta, delta_bytes);
14521 /* Set the cursor. */
14522 row = row_containing_pos (w, PT, r0, NULL, 0);
14523 if (row)
14524 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14525 else
14526 abort ();
14527 return 1;
14531 /* Handle the case that changes are all below what is displayed in
14532 the window, and that PT is in the window. This shortcut cannot
14533 be taken if ZV is visible in the window, and text has been added
14534 there that is visible in the window. */
14535 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14536 /* ZV is not visible in the window, or there are no
14537 changes at ZV, actually. */
14538 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14539 || first_changed_charpos == last_changed_charpos))
14541 struct glyph_row *r0;
14543 /* Give up if PT is not in the window. Note that it already has
14544 been checked at the start of try_window_id that PT is not in
14545 front of the window start. */
14546 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14547 GIVE_UP (14);
14549 /* If window start is unchanged, we can reuse the whole matrix
14550 as is, without changing glyph positions since no text has
14551 been added/removed in front of the window end. */
14552 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14553 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14554 /* PT must not be in a partially visible line. */
14555 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14556 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14558 /* We have to compute the window end anew since text
14559 can have been added/removed after it. */
14560 w->window_end_pos
14561 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14562 w->window_end_bytepos
14563 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14565 /* Set the cursor. */
14566 row = row_containing_pos (w, PT, r0, NULL, 0);
14567 if (row)
14568 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14569 else
14570 abort ();
14571 return 2;
14575 /* Give up if window start is in the changed area.
14577 The condition used to read
14579 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14581 but why that was tested escapes me at the moment. */
14582 if (CHARPOS (start) >= first_changed_charpos
14583 && CHARPOS (start) <= last_changed_charpos)
14584 GIVE_UP (15);
14586 /* Check that window start agrees with the start of the first glyph
14587 row in its current matrix. Check this after we know the window
14588 start is not in changed text, otherwise positions would not be
14589 comparable. */
14590 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14591 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14592 GIVE_UP (16);
14594 /* Give up if the window ends in strings. Overlay strings
14595 at the end are difficult to handle, so don't try. */
14596 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14597 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14598 GIVE_UP (20);
14600 /* Compute the position at which we have to start displaying new
14601 lines. Some of the lines at the top of the window might be
14602 reusable because they are not displaying changed text. Find the
14603 last row in W's current matrix not affected by changes at the
14604 start of current_buffer. Value is null if changes start in the
14605 first line of window. */
14606 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14607 if (last_unchanged_at_beg_row)
14609 /* Avoid starting to display in the moddle of a character, a TAB
14610 for instance. This is easier than to set up the iterator
14611 exactly, and it's not a frequent case, so the additional
14612 effort wouldn't really pay off. */
14613 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14614 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14615 && last_unchanged_at_beg_row > w->current_matrix->rows)
14616 --last_unchanged_at_beg_row;
14618 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14619 GIVE_UP (17);
14621 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14622 GIVE_UP (18);
14623 start_pos = it.current.pos;
14625 /* Start displaying new lines in the desired matrix at the same
14626 vpos we would use in the current matrix, i.e. below
14627 last_unchanged_at_beg_row. */
14628 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14629 current_matrix);
14630 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14631 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14633 xassert (it.hpos == 0 && it.current_x == 0);
14635 else
14637 /* There are no reusable lines at the start of the window.
14638 Start displaying in the first text line. */
14639 start_display (&it, w, start);
14640 it.vpos = it.first_vpos;
14641 start_pos = it.current.pos;
14644 /* Find the first row that is not affected by changes at the end of
14645 the buffer. Value will be null if there is no unchanged row, in
14646 which case we must redisplay to the end of the window. delta
14647 will be set to the value by which buffer positions beginning with
14648 first_unchanged_at_end_row have to be adjusted due to text
14649 changes. */
14650 first_unchanged_at_end_row
14651 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14652 IF_DEBUG (debug_delta = delta);
14653 IF_DEBUG (debug_delta_bytes = delta_bytes);
14655 /* Set stop_pos to the buffer position up to which we will have to
14656 display new lines. If first_unchanged_at_end_row != NULL, this
14657 is the buffer position of the start of the line displayed in that
14658 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14659 that we don't stop at a buffer position. */
14660 stop_pos = 0;
14661 if (first_unchanged_at_end_row)
14663 xassert (last_unchanged_at_beg_row == NULL
14664 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14666 /* If this is a continuation line, move forward to the next one
14667 that isn't. Changes in lines above affect this line.
14668 Caution: this may move first_unchanged_at_end_row to a row
14669 not displaying text. */
14670 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14671 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14672 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14673 < it.last_visible_y))
14674 ++first_unchanged_at_end_row;
14676 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14677 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14678 >= it.last_visible_y))
14679 first_unchanged_at_end_row = NULL;
14680 else
14682 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14683 + delta);
14684 first_unchanged_at_end_vpos
14685 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14686 xassert (stop_pos >= Z - END_UNCHANGED);
14689 else if (last_unchanged_at_beg_row == NULL)
14690 GIVE_UP (19);
14693 #if GLYPH_DEBUG
14695 /* Either there is no unchanged row at the end, or the one we have
14696 now displays text. This is a necessary condition for the window
14697 end pos calculation at the end of this function. */
14698 xassert (first_unchanged_at_end_row == NULL
14699 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14701 debug_last_unchanged_at_beg_vpos
14702 = (last_unchanged_at_beg_row
14703 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14704 : -1);
14705 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14707 #endif /* GLYPH_DEBUG != 0 */
14710 /* Display new lines. Set last_text_row to the last new line
14711 displayed which has text on it, i.e. might end up as being the
14712 line where the window_end_vpos is. */
14713 w->cursor.vpos = -1;
14714 last_text_row = NULL;
14715 overlay_arrow_seen = 0;
14716 while (it.current_y < it.last_visible_y
14717 && !fonts_changed_p
14718 && (first_unchanged_at_end_row == NULL
14719 || IT_CHARPOS (it) < stop_pos))
14721 if (display_line (&it))
14722 last_text_row = it.glyph_row - 1;
14725 if (fonts_changed_p)
14726 return -1;
14729 /* Compute differences in buffer positions, y-positions etc. for
14730 lines reused at the bottom of the window. Compute what we can
14731 scroll. */
14732 if (first_unchanged_at_end_row
14733 /* No lines reused because we displayed everything up to the
14734 bottom of the window. */
14735 && it.current_y < it.last_visible_y)
14737 dvpos = (it.vpos
14738 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14739 current_matrix));
14740 dy = it.current_y - first_unchanged_at_end_row->y;
14741 run.current_y = first_unchanged_at_end_row->y;
14742 run.desired_y = run.current_y + dy;
14743 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14745 else
14747 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14748 first_unchanged_at_end_row = NULL;
14750 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14753 /* Find the cursor if not already found. We have to decide whether
14754 PT will appear on this window (it sometimes doesn't, but this is
14755 not a very frequent case.) This decision has to be made before
14756 the current matrix is altered. A value of cursor.vpos < 0 means
14757 that PT is either in one of the lines beginning at
14758 first_unchanged_at_end_row or below the window. Don't care for
14759 lines that might be displayed later at the window end; as
14760 mentioned, this is not a frequent case. */
14761 if (w->cursor.vpos < 0)
14763 /* Cursor in unchanged rows at the top? */
14764 if (PT < CHARPOS (start_pos)
14765 && last_unchanged_at_beg_row)
14767 row = row_containing_pos (w, PT,
14768 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14769 last_unchanged_at_beg_row + 1, 0);
14770 if (row)
14771 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14774 /* Start from first_unchanged_at_end_row looking for PT. */
14775 else if (first_unchanged_at_end_row)
14777 row = row_containing_pos (w, PT - delta,
14778 first_unchanged_at_end_row, NULL, 0);
14779 if (row)
14780 set_cursor_from_row (w, row, w->current_matrix, delta,
14781 delta_bytes, dy, dvpos);
14784 /* Give up if cursor was not found. */
14785 if (w->cursor.vpos < 0)
14787 clear_glyph_matrix (w->desired_matrix);
14788 return -1;
14792 /* Don't let the cursor end in the scroll margins. */
14794 int this_scroll_margin, cursor_height;
14796 this_scroll_margin = max (0, scroll_margin);
14797 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14798 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14799 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14801 if ((w->cursor.y < this_scroll_margin
14802 && CHARPOS (start) > BEGV)
14803 /* Old redisplay didn't take scroll margin into account at the bottom,
14804 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14805 || (w->cursor.y + (make_cursor_line_fully_visible_p
14806 ? cursor_height + this_scroll_margin
14807 : 1)) > it.last_visible_y)
14809 w->cursor.vpos = -1;
14810 clear_glyph_matrix (w->desired_matrix);
14811 return -1;
14815 /* Scroll the display. Do it before changing the current matrix so
14816 that xterm.c doesn't get confused about where the cursor glyph is
14817 found. */
14818 if (dy && run.height)
14820 update_begin (f);
14822 if (FRAME_WINDOW_P (f))
14824 rif->update_window_begin_hook (w);
14825 rif->clear_window_mouse_face (w);
14826 rif->scroll_run_hook (w, &run);
14827 rif->update_window_end_hook (w, 0, 0);
14829 else
14831 /* Terminal frame. In this case, dvpos gives the number of
14832 lines to scroll by; dvpos < 0 means scroll up. */
14833 int first_unchanged_at_end_vpos
14834 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14835 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14836 int end = (WINDOW_TOP_EDGE_LINE (w)
14837 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14838 + window_internal_height (w));
14840 /* Perform the operation on the screen. */
14841 if (dvpos > 0)
14843 /* Scroll last_unchanged_at_beg_row to the end of the
14844 window down dvpos lines. */
14845 set_terminal_window (end);
14847 /* On dumb terminals delete dvpos lines at the end
14848 before inserting dvpos empty lines. */
14849 if (!scroll_region_ok)
14850 ins_del_lines (end - dvpos, -dvpos);
14852 /* Insert dvpos empty lines in front of
14853 last_unchanged_at_beg_row. */
14854 ins_del_lines (from, dvpos);
14856 else if (dvpos < 0)
14858 /* Scroll up last_unchanged_at_beg_vpos to the end of
14859 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14860 set_terminal_window (end);
14862 /* Delete dvpos lines in front of
14863 last_unchanged_at_beg_vpos. ins_del_lines will set
14864 the cursor to the given vpos and emit |dvpos| delete
14865 line sequences. */
14866 ins_del_lines (from + dvpos, dvpos);
14868 /* On a dumb terminal insert dvpos empty lines at the
14869 end. */
14870 if (!scroll_region_ok)
14871 ins_del_lines (end + dvpos, -dvpos);
14874 set_terminal_window (0);
14877 update_end (f);
14880 /* Shift reused rows of the current matrix to the right position.
14881 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14882 text. */
14883 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14884 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14885 if (dvpos < 0)
14887 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14888 bottom_vpos, dvpos);
14889 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14890 bottom_vpos, 0);
14892 else if (dvpos > 0)
14894 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14895 bottom_vpos, dvpos);
14896 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14897 first_unchanged_at_end_vpos + dvpos, 0);
14900 /* For frame-based redisplay, make sure that current frame and window
14901 matrix are in sync with respect to glyph memory. */
14902 if (!FRAME_WINDOW_P (f))
14903 sync_frame_with_window_matrix_rows (w);
14905 /* Adjust buffer positions in reused rows. */
14906 if (delta || delta_bytes)
14907 increment_matrix_positions (current_matrix,
14908 first_unchanged_at_end_vpos + dvpos,
14909 bottom_vpos, delta, delta_bytes);
14911 /* Adjust Y positions. */
14912 if (dy)
14913 shift_glyph_matrix (w, current_matrix,
14914 first_unchanged_at_end_vpos + dvpos,
14915 bottom_vpos, dy);
14917 if (first_unchanged_at_end_row)
14919 first_unchanged_at_end_row += dvpos;
14920 if (first_unchanged_at_end_row->y >= it.last_visible_y
14921 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14922 first_unchanged_at_end_row = NULL;
14925 /* If scrolling up, there may be some lines to display at the end of
14926 the window. */
14927 last_text_row_at_end = NULL;
14928 if (dy < 0)
14930 /* Scrolling up can leave for example a partially visible line
14931 at the end of the window to be redisplayed. */
14932 /* Set last_row to the glyph row in the current matrix where the
14933 window end line is found. It has been moved up or down in
14934 the matrix by dvpos. */
14935 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14936 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14938 /* If last_row is the window end line, it should display text. */
14939 xassert (last_row->displays_text_p);
14941 /* If window end line was partially visible before, begin
14942 displaying at that line. Otherwise begin displaying with the
14943 line following it. */
14944 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14946 init_to_row_start (&it, w, last_row);
14947 it.vpos = last_vpos;
14948 it.current_y = last_row->y;
14950 else
14952 init_to_row_end (&it, w, last_row);
14953 it.vpos = 1 + last_vpos;
14954 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14955 ++last_row;
14958 /* We may start in a continuation line. If so, we have to
14959 get the right continuation_lines_width and current_x. */
14960 it.continuation_lines_width = last_row->continuation_lines_width;
14961 it.hpos = it.current_x = 0;
14963 /* Display the rest of the lines at the window end. */
14964 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14965 while (it.current_y < it.last_visible_y
14966 && !fonts_changed_p)
14968 /* Is it always sure that the display agrees with lines in
14969 the current matrix? I don't think so, so we mark rows
14970 displayed invalid in the current matrix by setting their
14971 enabled_p flag to zero. */
14972 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14973 if (display_line (&it))
14974 last_text_row_at_end = it.glyph_row - 1;
14978 /* Update window_end_pos and window_end_vpos. */
14979 if (first_unchanged_at_end_row
14980 && !last_text_row_at_end)
14982 /* Window end line if one of the preserved rows from the current
14983 matrix. Set row to the last row displaying text in current
14984 matrix starting at first_unchanged_at_end_row, after
14985 scrolling. */
14986 xassert (first_unchanged_at_end_row->displays_text_p);
14987 row = find_last_row_displaying_text (w->current_matrix, &it,
14988 first_unchanged_at_end_row);
14989 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14991 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14992 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14993 w->window_end_vpos
14994 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14995 xassert (w->window_end_bytepos >= 0);
14996 IF_DEBUG (debug_method_add (w, "A"));
14998 else if (last_text_row_at_end)
15000 w->window_end_pos
15001 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15002 w->window_end_bytepos
15003 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15004 w->window_end_vpos
15005 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15006 xassert (w->window_end_bytepos >= 0);
15007 IF_DEBUG (debug_method_add (w, "B"));
15009 else if (last_text_row)
15011 /* We have displayed either to the end of the window or at the
15012 end of the window, i.e. the last row with text is to be found
15013 in the desired matrix. */
15014 w->window_end_pos
15015 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15016 w->window_end_bytepos
15017 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15018 w->window_end_vpos
15019 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15020 xassert (w->window_end_bytepos >= 0);
15022 else if (first_unchanged_at_end_row == NULL
15023 && last_text_row == NULL
15024 && last_text_row_at_end == NULL)
15026 /* Displayed to end of window, but no line containing text was
15027 displayed. Lines were deleted at the end of the window. */
15028 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15029 int vpos = XFASTINT (w->window_end_vpos);
15030 struct glyph_row *current_row = current_matrix->rows + vpos;
15031 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15033 for (row = NULL;
15034 row == NULL && vpos >= first_vpos;
15035 --vpos, --current_row, --desired_row)
15037 if (desired_row->enabled_p)
15039 if (desired_row->displays_text_p)
15040 row = desired_row;
15042 else if (current_row->displays_text_p)
15043 row = current_row;
15046 xassert (row != NULL);
15047 w->window_end_vpos = make_number (vpos + 1);
15048 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15049 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15050 xassert (w->window_end_bytepos >= 0);
15051 IF_DEBUG (debug_method_add (w, "C"));
15053 else
15054 abort ();
15056 #if 0 /* This leads to problems, for instance when the cursor is
15057 at ZV, and the cursor line displays no text. */
15058 /* Disable rows below what's displayed in the window. This makes
15059 debugging easier. */
15060 enable_glyph_matrix_rows (current_matrix,
15061 XFASTINT (w->window_end_vpos) + 1,
15062 bottom_vpos, 0);
15063 #endif
15065 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15066 debug_end_vpos = XFASTINT (w->window_end_vpos));
15068 /* Record that display has not been completed. */
15069 w->window_end_valid = Qnil;
15070 w->desired_matrix->no_scrolling_p = 1;
15071 return 3;
15073 #undef GIVE_UP
15078 /***********************************************************************
15079 More debugging support
15080 ***********************************************************************/
15082 #if GLYPH_DEBUG
15084 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15085 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15086 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15089 /* Dump the contents of glyph matrix MATRIX on stderr.
15091 GLYPHS 0 means don't show glyph contents.
15092 GLYPHS 1 means show glyphs in short form
15093 GLYPHS > 1 means show glyphs in long form. */
15095 void
15096 dump_glyph_matrix (matrix, glyphs)
15097 struct glyph_matrix *matrix;
15098 int glyphs;
15100 int i;
15101 for (i = 0; i < matrix->nrows; ++i)
15102 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15106 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15107 the glyph row and area where the glyph comes from. */
15109 void
15110 dump_glyph (row, glyph, area)
15111 struct glyph_row *row;
15112 struct glyph *glyph;
15113 int area;
15115 if (glyph->type == CHAR_GLYPH)
15117 fprintf (stderr,
15118 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15119 glyph - row->glyphs[TEXT_AREA],
15120 'C',
15121 glyph->charpos,
15122 (BUFFERP (glyph->object)
15123 ? 'B'
15124 : (STRINGP (glyph->object)
15125 ? 'S'
15126 : '-')),
15127 glyph->pixel_width,
15128 glyph->u.ch,
15129 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15130 ? glyph->u.ch
15131 : '.'),
15132 glyph->face_id,
15133 glyph->left_box_line_p,
15134 glyph->right_box_line_p);
15136 else if (glyph->type == STRETCH_GLYPH)
15138 fprintf (stderr,
15139 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15140 glyph - row->glyphs[TEXT_AREA],
15141 'S',
15142 glyph->charpos,
15143 (BUFFERP (glyph->object)
15144 ? 'B'
15145 : (STRINGP (glyph->object)
15146 ? 'S'
15147 : '-')),
15148 glyph->pixel_width,
15150 '.',
15151 glyph->face_id,
15152 glyph->left_box_line_p,
15153 glyph->right_box_line_p);
15155 else if (glyph->type == IMAGE_GLYPH)
15157 fprintf (stderr,
15158 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15159 glyph - row->glyphs[TEXT_AREA],
15160 'I',
15161 glyph->charpos,
15162 (BUFFERP (glyph->object)
15163 ? 'B'
15164 : (STRINGP (glyph->object)
15165 ? 'S'
15166 : '-')),
15167 glyph->pixel_width,
15168 glyph->u.img_id,
15169 '.',
15170 glyph->face_id,
15171 glyph->left_box_line_p,
15172 glyph->right_box_line_p);
15174 else if (glyph->type == COMPOSITE_GLYPH)
15176 fprintf (stderr,
15177 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15178 glyph - row->glyphs[TEXT_AREA],
15179 '+',
15180 glyph->charpos,
15181 (BUFFERP (glyph->object)
15182 ? 'B'
15183 : (STRINGP (glyph->object)
15184 ? 'S'
15185 : '-')),
15186 glyph->pixel_width,
15187 glyph->u.cmp_id,
15188 '.',
15189 glyph->face_id,
15190 glyph->left_box_line_p,
15191 glyph->right_box_line_p);
15196 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15197 GLYPHS 0 means don't show glyph contents.
15198 GLYPHS 1 means show glyphs in short form
15199 GLYPHS > 1 means show glyphs in long form. */
15201 void
15202 dump_glyph_row (row, vpos, glyphs)
15203 struct glyph_row *row;
15204 int vpos, glyphs;
15206 if (glyphs != 1)
15208 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15209 fprintf (stderr, "======================================================================\n");
15211 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15212 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15213 vpos,
15214 MATRIX_ROW_START_CHARPOS (row),
15215 MATRIX_ROW_END_CHARPOS (row),
15216 row->used[TEXT_AREA],
15217 row->contains_overlapping_glyphs_p,
15218 row->enabled_p,
15219 row->truncated_on_left_p,
15220 row->truncated_on_right_p,
15221 row->continued_p,
15222 MATRIX_ROW_CONTINUATION_LINE_P (row),
15223 row->displays_text_p,
15224 row->ends_at_zv_p,
15225 row->fill_line_p,
15226 row->ends_in_middle_of_char_p,
15227 row->starts_in_middle_of_char_p,
15228 row->mouse_face_p,
15229 row->x,
15230 row->y,
15231 row->pixel_width,
15232 row->height,
15233 row->visible_height,
15234 row->ascent,
15235 row->phys_ascent);
15236 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15237 row->end.overlay_string_index,
15238 row->continuation_lines_width);
15239 fprintf (stderr, "%9d %5d\n",
15240 CHARPOS (row->start.string_pos),
15241 CHARPOS (row->end.string_pos));
15242 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15243 row->end.dpvec_index);
15246 if (glyphs > 1)
15248 int area;
15250 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15252 struct glyph *glyph = row->glyphs[area];
15253 struct glyph *glyph_end = glyph + row->used[area];
15255 /* Glyph for a line end in text. */
15256 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15257 ++glyph_end;
15259 if (glyph < glyph_end)
15260 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15262 for (; glyph < glyph_end; ++glyph)
15263 dump_glyph (row, glyph, area);
15266 else if (glyphs == 1)
15268 int area;
15270 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15272 char *s = (char *) alloca (row->used[area] + 1);
15273 int i;
15275 for (i = 0; i < row->used[area]; ++i)
15277 struct glyph *glyph = row->glyphs[area] + i;
15278 if (glyph->type == CHAR_GLYPH
15279 && glyph->u.ch < 0x80
15280 && glyph->u.ch >= ' ')
15281 s[i] = glyph->u.ch;
15282 else
15283 s[i] = '.';
15286 s[i] = '\0';
15287 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15293 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15294 Sdump_glyph_matrix, 0, 1, "p",
15295 doc: /* Dump the current matrix of the selected window to stderr.
15296 Shows contents of glyph row structures. With non-nil
15297 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15298 glyphs in short form, otherwise show glyphs in long form. */)
15299 (glyphs)
15300 Lisp_Object glyphs;
15302 struct window *w = XWINDOW (selected_window);
15303 struct buffer *buffer = XBUFFER (w->buffer);
15305 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15306 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15307 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15308 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15309 fprintf (stderr, "=============================================\n");
15310 dump_glyph_matrix (w->current_matrix,
15311 NILP (glyphs) ? 0 : XINT (glyphs));
15312 return Qnil;
15316 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15317 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15320 struct frame *f = XFRAME (selected_frame);
15321 dump_glyph_matrix (f->current_matrix, 1);
15322 return Qnil;
15326 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15327 doc: /* Dump glyph row ROW to stderr.
15328 GLYPH 0 means don't dump glyphs.
15329 GLYPH 1 means dump glyphs in short form.
15330 GLYPH > 1 or omitted means dump glyphs in long form. */)
15331 (row, glyphs)
15332 Lisp_Object row, glyphs;
15334 struct glyph_matrix *matrix;
15335 int vpos;
15337 CHECK_NUMBER (row);
15338 matrix = XWINDOW (selected_window)->current_matrix;
15339 vpos = XINT (row);
15340 if (vpos >= 0 && vpos < matrix->nrows)
15341 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15342 vpos,
15343 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15344 return Qnil;
15348 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15349 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15350 GLYPH 0 means don't dump glyphs.
15351 GLYPH 1 means dump glyphs in short form.
15352 GLYPH > 1 or omitted means dump glyphs in long form. */)
15353 (row, glyphs)
15354 Lisp_Object row, glyphs;
15356 struct frame *sf = SELECTED_FRAME ();
15357 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15358 int vpos;
15360 CHECK_NUMBER (row);
15361 vpos = XINT (row);
15362 if (vpos >= 0 && vpos < m->nrows)
15363 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15364 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15365 return Qnil;
15369 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15370 doc: /* Toggle tracing of redisplay.
15371 With ARG, turn tracing on if and only if ARG is positive. */)
15372 (arg)
15373 Lisp_Object arg;
15375 if (NILP (arg))
15376 trace_redisplay_p = !trace_redisplay_p;
15377 else
15379 arg = Fprefix_numeric_value (arg);
15380 trace_redisplay_p = XINT (arg) > 0;
15383 return Qnil;
15387 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15388 doc: /* Like `format', but print result to stderr.
15389 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15390 (nargs, args)
15391 int nargs;
15392 Lisp_Object *args;
15394 Lisp_Object s = Fformat (nargs, args);
15395 fprintf (stderr, "%s", SDATA (s));
15396 return Qnil;
15399 #endif /* GLYPH_DEBUG */
15403 /***********************************************************************
15404 Building Desired Matrix Rows
15405 ***********************************************************************/
15407 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15408 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15410 static struct glyph_row *
15411 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15412 struct window *w;
15413 Lisp_Object overlay_arrow_string;
15415 struct frame *f = XFRAME (WINDOW_FRAME (w));
15416 struct buffer *buffer = XBUFFER (w->buffer);
15417 struct buffer *old = current_buffer;
15418 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15419 int arrow_len = SCHARS (overlay_arrow_string);
15420 const unsigned char *arrow_end = arrow_string + arrow_len;
15421 const unsigned char *p;
15422 struct it it;
15423 int multibyte_p;
15424 int n_glyphs_before;
15426 set_buffer_temp (buffer);
15427 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15428 it.glyph_row->used[TEXT_AREA] = 0;
15429 SET_TEXT_POS (it.position, 0, 0);
15431 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15432 p = arrow_string;
15433 while (p < arrow_end)
15435 Lisp_Object face, ilisp;
15437 /* Get the next character. */
15438 if (multibyte_p)
15439 it.c = string_char_and_length (p, arrow_len, &it.len);
15440 else
15441 it.c = *p, it.len = 1;
15442 p += it.len;
15444 /* Get its face. */
15445 ilisp = make_number (p - arrow_string);
15446 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15447 it.face_id = compute_char_face (f, it.c, face);
15449 /* Compute its width, get its glyphs. */
15450 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15451 SET_TEXT_POS (it.position, -1, -1);
15452 PRODUCE_GLYPHS (&it);
15454 /* If this character doesn't fit any more in the line, we have
15455 to remove some glyphs. */
15456 if (it.current_x > it.last_visible_x)
15458 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15459 break;
15463 set_buffer_temp (old);
15464 return it.glyph_row;
15468 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15469 glyphs are only inserted for terminal frames since we can't really
15470 win with truncation glyphs when partially visible glyphs are
15471 involved. Which glyphs to insert is determined by
15472 produce_special_glyphs. */
15474 static void
15475 insert_left_trunc_glyphs (it)
15476 struct it *it;
15478 struct it truncate_it;
15479 struct glyph *from, *end, *to, *toend;
15481 xassert (!FRAME_WINDOW_P (it->f));
15483 /* Get the truncation glyphs. */
15484 truncate_it = *it;
15485 truncate_it.current_x = 0;
15486 truncate_it.face_id = DEFAULT_FACE_ID;
15487 truncate_it.glyph_row = &scratch_glyph_row;
15488 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15489 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15490 truncate_it.object = make_number (0);
15491 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15493 /* Overwrite glyphs from IT with truncation glyphs. */
15494 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15495 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15496 to = it->glyph_row->glyphs[TEXT_AREA];
15497 toend = to + it->glyph_row->used[TEXT_AREA];
15499 while (from < end)
15500 *to++ = *from++;
15502 /* There may be padding glyphs left over. Overwrite them too. */
15503 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15505 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15506 while (from < end)
15507 *to++ = *from++;
15510 if (to > toend)
15511 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15515 /* Compute the pixel height and width of IT->glyph_row.
15517 Most of the time, ascent and height of a display line will be equal
15518 to the max_ascent and max_height values of the display iterator
15519 structure. This is not the case if
15521 1. We hit ZV without displaying anything. In this case, max_ascent
15522 and max_height will be zero.
15524 2. We have some glyphs that don't contribute to the line height.
15525 (The glyph row flag contributes_to_line_height_p is for future
15526 pixmap extensions).
15528 The first case is easily covered by using default values because in
15529 these cases, the line height does not really matter, except that it
15530 must not be zero. */
15532 static void
15533 compute_line_metrics (it)
15534 struct it *it;
15536 struct glyph_row *row = it->glyph_row;
15537 int area, i;
15539 if (FRAME_WINDOW_P (it->f))
15541 int i, min_y, max_y;
15543 /* The line may consist of one space only, that was added to
15544 place the cursor on it. If so, the row's height hasn't been
15545 computed yet. */
15546 if (row->height == 0)
15548 if (it->max_ascent + it->max_descent == 0)
15549 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15550 row->ascent = it->max_ascent;
15551 row->height = it->max_ascent + it->max_descent;
15552 row->phys_ascent = it->max_phys_ascent;
15553 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15554 row->extra_line_spacing = it->max_extra_line_spacing;
15557 /* Compute the width of this line. */
15558 row->pixel_width = row->x;
15559 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15560 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15562 xassert (row->pixel_width >= 0);
15563 xassert (row->ascent >= 0 && row->height > 0);
15565 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15566 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15568 /* If first line's physical ascent is larger than its logical
15569 ascent, use the physical ascent, and make the row taller.
15570 This makes accented characters fully visible. */
15571 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15572 && row->phys_ascent > row->ascent)
15574 row->height += row->phys_ascent - row->ascent;
15575 row->ascent = row->phys_ascent;
15578 /* Compute how much of the line is visible. */
15579 row->visible_height = row->height;
15581 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15582 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15584 if (row->y < min_y)
15585 row->visible_height -= min_y - row->y;
15586 if (row->y + row->height > max_y)
15587 row->visible_height -= row->y + row->height - max_y;
15589 else
15591 row->pixel_width = row->used[TEXT_AREA];
15592 if (row->continued_p)
15593 row->pixel_width -= it->continuation_pixel_width;
15594 else if (row->truncated_on_right_p)
15595 row->pixel_width -= it->truncation_pixel_width;
15596 row->ascent = row->phys_ascent = 0;
15597 row->height = row->phys_height = row->visible_height = 1;
15598 row->extra_line_spacing = 0;
15601 /* Compute a hash code for this row. */
15602 row->hash = 0;
15603 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15604 for (i = 0; i < row->used[area]; ++i)
15605 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15606 + row->glyphs[area][i].u.val
15607 + row->glyphs[area][i].face_id
15608 + row->glyphs[area][i].padding_p
15609 + (row->glyphs[area][i].type << 2));
15611 it->max_ascent = it->max_descent = 0;
15612 it->max_phys_ascent = it->max_phys_descent = 0;
15616 /* Append one space to the glyph row of iterator IT if doing a
15617 window-based redisplay. The space has the same face as
15618 IT->face_id. Value is non-zero if a space was added.
15620 This function is called to make sure that there is always one glyph
15621 at the end of a glyph row that the cursor can be set on under
15622 window-systems. (If there weren't such a glyph we would not know
15623 how wide and tall a box cursor should be displayed).
15625 At the same time this space let's a nicely handle clearing to the
15626 end of the line if the row ends in italic text. */
15628 static int
15629 append_space_for_newline (it, default_face_p)
15630 struct it *it;
15631 int default_face_p;
15633 if (FRAME_WINDOW_P (it->f))
15635 int n = it->glyph_row->used[TEXT_AREA];
15637 if (it->glyph_row->glyphs[TEXT_AREA] + n
15638 < it->glyph_row->glyphs[1 + TEXT_AREA])
15640 /* Save some values that must not be changed.
15641 Must save IT->c and IT->len because otherwise
15642 ITERATOR_AT_END_P wouldn't work anymore after
15643 append_space_for_newline has been called. */
15644 enum display_element_type saved_what = it->what;
15645 int saved_c = it->c, saved_len = it->len;
15646 int saved_x = it->current_x;
15647 int saved_face_id = it->face_id;
15648 struct text_pos saved_pos;
15649 Lisp_Object saved_object;
15650 struct face *face;
15652 saved_object = it->object;
15653 saved_pos = it->position;
15655 it->what = IT_CHARACTER;
15656 bzero (&it->position, sizeof it->position);
15657 it->object = make_number (0);
15658 it->c = ' ';
15659 it->len = 1;
15661 if (default_face_p)
15662 it->face_id = DEFAULT_FACE_ID;
15663 else if (it->face_before_selective_p)
15664 it->face_id = it->saved_face_id;
15665 face = FACE_FROM_ID (it->f, it->face_id);
15666 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15668 PRODUCE_GLYPHS (it);
15670 it->override_ascent = -1;
15671 it->constrain_row_ascent_descent_p = 0;
15672 it->current_x = saved_x;
15673 it->object = saved_object;
15674 it->position = saved_pos;
15675 it->what = saved_what;
15676 it->face_id = saved_face_id;
15677 it->len = saved_len;
15678 it->c = saved_c;
15679 return 1;
15683 return 0;
15687 /* Extend the face of the last glyph in the text area of IT->glyph_row
15688 to the end of the display line. Called from display_line.
15689 If the glyph row is empty, add a space glyph to it so that we
15690 know the face to draw. Set the glyph row flag fill_line_p. */
15692 static void
15693 extend_face_to_end_of_line (it)
15694 struct it *it;
15696 struct face *face;
15697 struct frame *f = it->f;
15699 /* If line is already filled, do nothing. */
15700 if (it->current_x >= it->last_visible_x)
15701 return;
15703 /* Face extension extends the background and box of IT->face_id
15704 to the end of the line. If the background equals the background
15705 of the frame, we don't have to do anything. */
15706 if (it->face_before_selective_p)
15707 face = FACE_FROM_ID (it->f, it->saved_face_id);
15708 else
15709 face = FACE_FROM_ID (f, it->face_id);
15711 if (FRAME_WINDOW_P (f)
15712 && it->glyph_row->displays_text_p
15713 && face->box == FACE_NO_BOX
15714 && face->background == FRAME_BACKGROUND_PIXEL (f)
15715 && !face->stipple)
15716 return;
15718 /* Set the glyph row flag indicating that the face of the last glyph
15719 in the text area has to be drawn to the end of the text area. */
15720 it->glyph_row->fill_line_p = 1;
15722 /* If current character of IT is not ASCII, make sure we have the
15723 ASCII face. This will be automatically undone the next time
15724 get_next_display_element returns a multibyte character. Note
15725 that the character will always be single byte in unibyte text. */
15726 if (!SINGLE_BYTE_CHAR_P (it->c))
15728 it->face_id = FACE_FOR_CHAR (f, face, 0);
15731 if (FRAME_WINDOW_P (f))
15733 /* If the row is empty, add a space with the current face of IT,
15734 so that we know which face to draw. */
15735 if (it->glyph_row->used[TEXT_AREA] == 0)
15737 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15738 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15739 it->glyph_row->used[TEXT_AREA] = 1;
15742 else
15744 /* Save some values that must not be changed. */
15745 int saved_x = it->current_x;
15746 struct text_pos saved_pos;
15747 Lisp_Object saved_object;
15748 enum display_element_type saved_what = it->what;
15749 int saved_face_id = it->face_id;
15751 saved_object = it->object;
15752 saved_pos = it->position;
15754 it->what = IT_CHARACTER;
15755 bzero (&it->position, sizeof it->position);
15756 it->object = make_number (0);
15757 it->c = ' ';
15758 it->len = 1;
15759 it->face_id = face->id;
15761 PRODUCE_GLYPHS (it);
15763 while (it->current_x <= it->last_visible_x)
15764 PRODUCE_GLYPHS (it);
15766 /* Don't count these blanks really. It would let us insert a left
15767 truncation glyph below and make us set the cursor on them, maybe. */
15768 it->current_x = saved_x;
15769 it->object = saved_object;
15770 it->position = saved_pos;
15771 it->what = saved_what;
15772 it->face_id = saved_face_id;
15777 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15778 trailing whitespace. */
15780 static int
15781 trailing_whitespace_p (charpos)
15782 int charpos;
15784 int bytepos = CHAR_TO_BYTE (charpos);
15785 int c = 0;
15787 while (bytepos < ZV_BYTE
15788 && (c = FETCH_CHAR (bytepos),
15789 c == ' ' || c == '\t'))
15790 ++bytepos;
15792 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15794 if (bytepos != PT_BYTE)
15795 return 1;
15797 return 0;
15801 /* Highlight trailing whitespace, if any, in ROW. */
15803 void
15804 highlight_trailing_whitespace (f, row)
15805 struct frame *f;
15806 struct glyph_row *row;
15808 int used = row->used[TEXT_AREA];
15810 if (used)
15812 struct glyph *start = row->glyphs[TEXT_AREA];
15813 struct glyph *glyph = start + used - 1;
15815 /* Skip over glyphs inserted to display the cursor at the
15816 end of a line, for extending the face of the last glyph
15817 to the end of the line on terminals, and for truncation
15818 and continuation glyphs. */
15819 while (glyph >= start
15820 && glyph->type == CHAR_GLYPH
15821 && INTEGERP (glyph->object))
15822 --glyph;
15824 /* If last glyph is a space or stretch, and it's trailing
15825 whitespace, set the face of all trailing whitespace glyphs in
15826 IT->glyph_row to `trailing-whitespace'. */
15827 if (glyph >= start
15828 && BUFFERP (glyph->object)
15829 && (glyph->type == STRETCH_GLYPH
15830 || (glyph->type == CHAR_GLYPH
15831 && glyph->u.ch == ' '))
15832 && trailing_whitespace_p (glyph->charpos))
15834 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15835 if (face_id < 0)
15836 return;
15838 while (glyph >= start
15839 && BUFFERP (glyph->object)
15840 && (glyph->type == STRETCH_GLYPH
15841 || (glyph->type == CHAR_GLYPH
15842 && glyph->u.ch == ' ')))
15843 (glyph--)->face_id = face_id;
15849 /* Value is non-zero if glyph row ROW in window W should be
15850 used to hold the cursor. */
15852 static int
15853 cursor_row_p (w, row)
15854 struct window *w;
15855 struct glyph_row *row;
15857 int cursor_row_p = 1;
15859 if (PT == MATRIX_ROW_END_CHARPOS (row))
15861 /* Suppose the row ends on a string.
15862 Unless the row is continued, that means it ends on a newline
15863 in the string. If it's anything other than a display string
15864 (e.g. a before-string from an overlay), we don't want the
15865 cursor there. (This heuristic seems to give the optimal
15866 behavior for the various types of multi-line strings.) */
15867 if (CHARPOS (row->end.string_pos) >= 0)
15869 if (row->continued_p)
15870 cursor_row_p = 1;
15871 else
15873 /* Check for `display' property. */
15874 struct glyph *beg = row->glyphs[TEXT_AREA];
15875 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15876 struct glyph *glyph;
15878 cursor_row_p = 0;
15879 for (glyph = end; glyph >= beg; --glyph)
15880 if (STRINGP (glyph->object))
15882 Lisp_Object prop
15883 = Fget_char_property (make_number (PT),
15884 Qdisplay, Qnil);
15885 cursor_row_p =
15886 (!NILP (prop)
15887 && display_prop_string_p (prop, glyph->object));
15888 break;
15892 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15894 /* If the row ends in middle of a real character,
15895 and the line is continued, we want the cursor here.
15896 That's because MATRIX_ROW_END_CHARPOS would equal
15897 PT if PT is before the character. */
15898 if (!row->ends_in_ellipsis_p)
15899 cursor_row_p = row->continued_p;
15900 else
15901 /* If the row ends in an ellipsis, then
15902 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15903 We want that position to be displayed after the ellipsis. */
15904 cursor_row_p = 0;
15906 /* If the row ends at ZV, display the cursor at the end of that
15907 row instead of at the start of the row below. */
15908 else if (row->ends_at_zv_p)
15909 cursor_row_p = 1;
15910 else
15911 cursor_row_p = 0;
15914 return cursor_row_p;
15918 /* Construct the glyph row IT->glyph_row in the desired matrix of
15919 IT->w from text at the current position of IT. See dispextern.h
15920 for an overview of struct it. Value is non-zero if
15921 IT->glyph_row displays text, as opposed to a line displaying ZV
15922 only. */
15924 static int
15925 display_line (it)
15926 struct it *it;
15928 struct glyph_row *row = it->glyph_row;
15929 Lisp_Object overlay_arrow_string;
15931 /* We always start displaying at hpos zero even if hscrolled. */
15932 xassert (it->hpos == 0 && it->current_x == 0);
15934 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15935 >= it->w->desired_matrix->nrows)
15937 it->w->nrows_scale_factor++;
15938 fonts_changed_p = 1;
15939 return 0;
15942 /* Is IT->w showing the region? */
15943 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15945 /* Clear the result glyph row and enable it. */
15946 prepare_desired_row (row);
15948 row->y = it->current_y;
15949 row->start = it->start;
15950 row->continuation_lines_width = it->continuation_lines_width;
15951 row->displays_text_p = 1;
15952 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15953 it->starts_in_middle_of_char_p = 0;
15955 /* Arrange the overlays nicely for our purposes. Usually, we call
15956 display_line on only one line at a time, in which case this
15957 can't really hurt too much, or we call it on lines which appear
15958 one after another in the buffer, in which case all calls to
15959 recenter_overlay_lists but the first will be pretty cheap. */
15960 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15962 /* Move over display elements that are not visible because we are
15963 hscrolled. This may stop at an x-position < IT->first_visible_x
15964 if the first glyph is partially visible or if we hit a line end. */
15965 if (it->current_x < it->first_visible_x)
15967 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15968 MOVE_TO_POS | MOVE_TO_X);
15971 /* Get the initial row height. This is either the height of the
15972 text hscrolled, if there is any, or zero. */
15973 row->ascent = it->max_ascent;
15974 row->height = it->max_ascent + it->max_descent;
15975 row->phys_ascent = it->max_phys_ascent;
15976 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15977 row->extra_line_spacing = it->max_extra_line_spacing;
15979 /* Loop generating characters. The loop is left with IT on the next
15980 character to display. */
15981 while (1)
15983 int n_glyphs_before, hpos_before, x_before;
15984 int x, i, nglyphs;
15985 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15987 /* Retrieve the next thing to display. Value is zero if end of
15988 buffer reached. */
15989 if (!get_next_display_element (it))
15991 /* Maybe add a space at the end of this line that is used to
15992 display the cursor there under X. Set the charpos of the
15993 first glyph of blank lines not corresponding to any text
15994 to -1. */
15995 #ifdef HAVE_WINDOW_SYSTEM
15996 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15997 row->exact_window_width_line_p = 1;
15998 else
15999 #endif /* HAVE_WINDOW_SYSTEM */
16000 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16001 || row->used[TEXT_AREA] == 0)
16003 row->glyphs[TEXT_AREA]->charpos = -1;
16004 row->displays_text_p = 0;
16006 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16007 && (!MINI_WINDOW_P (it->w)
16008 || (minibuf_level && EQ (it->window, minibuf_window))))
16009 row->indicate_empty_line_p = 1;
16012 it->continuation_lines_width = 0;
16013 row->ends_at_zv_p = 1;
16014 break;
16017 /* Now, get the metrics of what we want to display. This also
16018 generates glyphs in `row' (which is IT->glyph_row). */
16019 n_glyphs_before = row->used[TEXT_AREA];
16020 x = it->current_x;
16022 /* Remember the line height so far in case the next element doesn't
16023 fit on the line. */
16024 if (!it->truncate_lines_p)
16026 ascent = it->max_ascent;
16027 descent = it->max_descent;
16028 phys_ascent = it->max_phys_ascent;
16029 phys_descent = it->max_phys_descent;
16032 PRODUCE_GLYPHS (it);
16034 /* If this display element was in marginal areas, continue with
16035 the next one. */
16036 if (it->area != TEXT_AREA)
16038 row->ascent = max (row->ascent, it->max_ascent);
16039 row->height = max (row->height, it->max_ascent + it->max_descent);
16040 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16041 row->phys_height = max (row->phys_height,
16042 it->max_phys_ascent + it->max_phys_descent);
16043 row->extra_line_spacing = max (row->extra_line_spacing,
16044 it->max_extra_line_spacing);
16045 set_iterator_to_next (it, 1);
16046 continue;
16049 /* Does the display element fit on the line? If we truncate
16050 lines, we should draw past the right edge of the window. If
16051 we don't truncate, we want to stop so that we can display the
16052 continuation glyph before the right margin. If lines are
16053 continued, there are two possible strategies for characters
16054 resulting in more than 1 glyph (e.g. tabs): Display as many
16055 glyphs as possible in this line and leave the rest for the
16056 continuation line, or display the whole element in the next
16057 line. Original redisplay did the former, so we do it also. */
16058 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16059 hpos_before = it->hpos;
16060 x_before = x;
16062 if (/* Not a newline. */
16063 nglyphs > 0
16064 /* Glyphs produced fit entirely in the line. */
16065 && it->current_x < it->last_visible_x)
16067 it->hpos += nglyphs;
16068 row->ascent = max (row->ascent, it->max_ascent);
16069 row->height = max (row->height, it->max_ascent + it->max_descent);
16070 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16071 row->phys_height = max (row->phys_height,
16072 it->max_phys_ascent + it->max_phys_descent);
16073 row->extra_line_spacing = max (row->extra_line_spacing,
16074 it->max_extra_line_spacing);
16075 if (it->current_x - it->pixel_width < it->first_visible_x)
16076 row->x = x - it->first_visible_x;
16078 else
16080 int new_x;
16081 struct glyph *glyph;
16083 for (i = 0; i < nglyphs; ++i, x = new_x)
16085 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16086 new_x = x + glyph->pixel_width;
16088 if (/* Lines are continued. */
16089 !it->truncate_lines_p
16090 && (/* Glyph doesn't fit on the line. */
16091 new_x > it->last_visible_x
16092 /* Or it fits exactly on a window system frame. */
16093 || (new_x == it->last_visible_x
16094 && FRAME_WINDOW_P (it->f))))
16096 /* End of a continued line. */
16098 if (it->hpos == 0
16099 || (new_x == it->last_visible_x
16100 && FRAME_WINDOW_P (it->f)))
16102 /* Current glyph is the only one on the line or
16103 fits exactly on the line. We must continue
16104 the line because we can't draw the cursor
16105 after the glyph. */
16106 row->continued_p = 1;
16107 it->current_x = new_x;
16108 it->continuation_lines_width += new_x;
16109 ++it->hpos;
16110 if (i == nglyphs - 1)
16112 set_iterator_to_next (it, 1);
16113 #ifdef HAVE_WINDOW_SYSTEM
16114 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16116 if (!get_next_display_element (it))
16118 row->exact_window_width_line_p = 1;
16119 it->continuation_lines_width = 0;
16120 row->continued_p = 0;
16121 row->ends_at_zv_p = 1;
16123 else if (ITERATOR_AT_END_OF_LINE_P (it))
16125 row->continued_p = 0;
16126 row->exact_window_width_line_p = 1;
16129 #endif /* HAVE_WINDOW_SYSTEM */
16132 else if (CHAR_GLYPH_PADDING_P (*glyph)
16133 && !FRAME_WINDOW_P (it->f))
16135 /* A padding glyph that doesn't fit on this line.
16136 This means the whole character doesn't fit
16137 on the line. */
16138 row->used[TEXT_AREA] = n_glyphs_before;
16140 /* Fill the rest of the row with continuation
16141 glyphs like in 20.x. */
16142 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16143 < row->glyphs[1 + TEXT_AREA])
16144 produce_special_glyphs (it, IT_CONTINUATION);
16146 row->continued_p = 1;
16147 it->current_x = x_before;
16148 it->continuation_lines_width += x_before;
16150 /* Restore the height to what it was before the
16151 element not fitting on the line. */
16152 it->max_ascent = ascent;
16153 it->max_descent = descent;
16154 it->max_phys_ascent = phys_ascent;
16155 it->max_phys_descent = phys_descent;
16157 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16159 /* A TAB that extends past the right edge of the
16160 window. This produces a single glyph on
16161 window system frames. We leave the glyph in
16162 this row and let it fill the row, but don't
16163 consume the TAB. */
16164 it->continuation_lines_width += it->last_visible_x;
16165 row->ends_in_middle_of_char_p = 1;
16166 row->continued_p = 1;
16167 glyph->pixel_width = it->last_visible_x - x;
16168 it->starts_in_middle_of_char_p = 1;
16170 else
16172 /* Something other than a TAB that draws past
16173 the right edge of the window. Restore
16174 positions to values before the element. */
16175 row->used[TEXT_AREA] = n_glyphs_before + i;
16177 /* Display continuation glyphs. */
16178 if (!FRAME_WINDOW_P (it->f))
16179 produce_special_glyphs (it, IT_CONTINUATION);
16180 row->continued_p = 1;
16182 it->current_x = x_before;
16183 it->continuation_lines_width += x;
16184 extend_face_to_end_of_line (it);
16186 if (nglyphs > 1 && i > 0)
16188 row->ends_in_middle_of_char_p = 1;
16189 it->starts_in_middle_of_char_p = 1;
16192 /* Restore the height to what it was before the
16193 element not fitting on the line. */
16194 it->max_ascent = ascent;
16195 it->max_descent = descent;
16196 it->max_phys_ascent = phys_ascent;
16197 it->max_phys_descent = phys_descent;
16200 break;
16202 else if (new_x > it->first_visible_x)
16204 /* Increment number of glyphs actually displayed. */
16205 ++it->hpos;
16207 if (x < it->first_visible_x)
16208 /* Glyph is partially visible, i.e. row starts at
16209 negative X position. */
16210 row->x = x - it->first_visible_x;
16212 else
16214 /* Glyph is completely off the left margin of the
16215 window. This should not happen because of the
16216 move_it_in_display_line at the start of this
16217 function, unless the text display area of the
16218 window is empty. */
16219 xassert (it->first_visible_x <= it->last_visible_x);
16223 row->ascent = max (row->ascent, it->max_ascent);
16224 row->height = max (row->height, it->max_ascent + it->max_descent);
16225 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16226 row->phys_height = max (row->phys_height,
16227 it->max_phys_ascent + it->max_phys_descent);
16228 row->extra_line_spacing = max (row->extra_line_spacing,
16229 it->max_extra_line_spacing);
16231 /* End of this display line if row is continued. */
16232 if (row->continued_p || row->ends_at_zv_p)
16233 break;
16236 at_end_of_line:
16237 /* Is this a line end? If yes, we're also done, after making
16238 sure that a non-default face is extended up to the right
16239 margin of the window. */
16240 if (ITERATOR_AT_END_OF_LINE_P (it))
16242 int used_before = row->used[TEXT_AREA];
16244 row->ends_in_newline_from_string_p = STRINGP (it->object);
16246 #ifdef HAVE_WINDOW_SYSTEM
16247 /* Add a space at the end of the line that is used to
16248 display the cursor there. */
16249 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16250 append_space_for_newline (it, 0);
16251 #endif /* HAVE_WINDOW_SYSTEM */
16253 /* Extend the face to the end of the line. */
16254 extend_face_to_end_of_line (it);
16256 /* Make sure we have the position. */
16257 if (used_before == 0)
16258 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16260 /* Consume the line end. This skips over invisible lines. */
16261 set_iterator_to_next (it, 1);
16262 it->continuation_lines_width = 0;
16263 break;
16266 /* Proceed with next display element. Note that this skips
16267 over lines invisible because of selective display. */
16268 set_iterator_to_next (it, 1);
16270 /* If we truncate lines, we are done when the last displayed
16271 glyphs reach past the right margin of the window. */
16272 if (it->truncate_lines_p
16273 && (FRAME_WINDOW_P (it->f)
16274 ? (it->current_x >= it->last_visible_x)
16275 : (it->current_x > it->last_visible_x)))
16277 /* Maybe add truncation glyphs. */
16278 if (!FRAME_WINDOW_P (it->f))
16280 int i, n;
16282 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16283 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16284 break;
16286 for (n = row->used[TEXT_AREA]; i < n; ++i)
16288 row->used[TEXT_AREA] = i;
16289 produce_special_glyphs (it, IT_TRUNCATION);
16292 #ifdef HAVE_WINDOW_SYSTEM
16293 else
16295 /* Don't truncate if we can overflow newline into fringe. */
16296 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16298 if (!get_next_display_element (it))
16300 it->continuation_lines_width = 0;
16301 row->ends_at_zv_p = 1;
16302 row->exact_window_width_line_p = 1;
16303 break;
16305 if (ITERATOR_AT_END_OF_LINE_P (it))
16307 row->exact_window_width_line_p = 1;
16308 goto at_end_of_line;
16312 #endif /* HAVE_WINDOW_SYSTEM */
16314 row->truncated_on_right_p = 1;
16315 it->continuation_lines_width = 0;
16316 reseat_at_next_visible_line_start (it, 0);
16317 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16318 it->hpos = hpos_before;
16319 it->current_x = x_before;
16320 break;
16324 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16325 at the left window margin. */
16326 if (it->first_visible_x
16327 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16329 if (!FRAME_WINDOW_P (it->f))
16330 insert_left_trunc_glyphs (it);
16331 row->truncated_on_left_p = 1;
16334 /* If the start of this line is the overlay arrow-position, then
16335 mark this glyph row as the one containing the overlay arrow.
16336 This is clearly a mess with variable size fonts. It would be
16337 better to let it be displayed like cursors under X. */
16338 if ((row->displays_text_p || !overlay_arrow_seen)
16339 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16340 !NILP (overlay_arrow_string)))
16342 /* Overlay arrow in window redisplay is a fringe bitmap. */
16343 if (STRINGP (overlay_arrow_string))
16345 struct glyph_row *arrow_row
16346 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16347 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16348 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16349 struct glyph *p = row->glyphs[TEXT_AREA];
16350 struct glyph *p2, *end;
16352 /* Copy the arrow glyphs. */
16353 while (glyph < arrow_end)
16354 *p++ = *glyph++;
16356 /* Throw away padding glyphs. */
16357 p2 = p;
16358 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16359 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16360 ++p2;
16361 if (p2 > p)
16363 while (p2 < end)
16364 *p++ = *p2++;
16365 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16368 else
16370 xassert (INTEGERP (overlay_arrow_string));
16371 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16373 overlay_arrow_seen = 1;
16376 /* Compute pixel dimensions of this line. */
16377 compute_line_metrics (it);
16379 /* Remember the position at which this line ends. */
16380 row->end = it->current;
16382 /* Record whether this row ends inside an ellipsis. */
16383 row->ends_in_ellipsis_p
16384 = (it->method == GET_FROM_DISPLAY_VECTOR
16385 && it->ellipsis_p);
16387 /* Save fringe bitmaps in this row. */
16388 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16389 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16390 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16391 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16393 it->left_user_fringe_bitmap = 0;
16394 it->left_user_fringe_face_id = 0;
16395 it->right_user_fringe_bitmap = 0;
16396 it->right_user_fringe_face_id = 0;
16398 /* Maybe set the cursor. */
16399 if (it->w->cursor.vpos < 0
16400 && PT >= MATRIX_ROW_START_CHARPOS (row)
16401 && PT <= MATRIX_ROW_END_CHARPOS (row)
16402 && cursor_row_p (it->w, row))
16403 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16405 /* Highlight trailing whitespace. */
16406 if (!NILP (Vshow_trailing_whitespace))
16407 highlight_trailing_whitespace (it->f, it->glyph_row);
16409 /* Prepare for the next line. This line starts horizontally at (X
16410 HPOS) = (0 0). Vertical positions are incremented. As a
16411 convenience for the caller, IT->glyph_row is set to the next
16412 row to be used. */
16413 it->current_x = it->hpos = 0;
16414 it->current_y += row->height;
16415 ++it->vpos;
16416 ++it->glyph_row;
16417 it->start = it->current;
16418 return row->displays_text_p;
16423 /***********************************************************************
16424 Menu Bar
16425 ***********************************************************************/
16427 /* Redisplay the menu bar in the frame for window W.
16429 The menu bar of X frames that don't have X toolkit support is
16430 displayed in a special window W->frame->menu_bar_window.
16432 The menu bar of terminal frames is treated specially as far as
16433 glyph matrices are concerned. Menu bar lines are not part of
16434 windows, so the update is done directly on the frame matrix rows
16435 for the menu bar. */
16437 static void
16438 display_menu_bar (w)
16439 struct window *w;
16441 struct frame *f = XFRAME (WINDOW_FRAME (w));
16442 struct it it;
16443 Lisp_Object items;
16444 int i;
16446 /* Don't do all this for graphical frames. */
16447 #ifdef HAVE_NTGUI
16448 if (!NILP (Vwindow_system))
16449 return;
16450 #endif
16451 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16452 if (FRAME_X_P (f))
16453 return;
16454 #endif
16455 #ifdef MAC_OS
16456 if (FRAME_MAC_P (f))
16457 return;
16458 #endif
16460 #ifdef USE_X_TOOLKIT
16461 xassert (!FRAME_WINDOW_P (f));
16462 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16463 it.first_visible_x = 0;
16464 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16465 #else /* not USE_X_TOOLKIT */
16466 if (FRAME_WINDOW_P (f))
16468 /* Menu bar lines are displayed in the desired matrix of the
16469 dummy window menu_bar_window. */
16470 struct window *menu_w;
16471 xassert (WINDOWP (f->menu_bar_window));
16472 menu_w = XWINDOW (f->menu_bar_window);
16473 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16474 MENU_FACE_ID);
16475 it.first_visible_x = 0;
16476 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16478 else
16480 /* This is a TTY frame, i.e. character hpos/vpos are used as
16481 pixel x/y. */
16482 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16483 MENU_FACE_ID);
16484 it.first_visible_x = 0;
16485 it.last_visible_x = FRAME_COLS (f);
16487 #endif /* not USE_X_TOOLKIT */
16489 if (! mode_line_inverse_video)
16490 /* Force the menu-bar to be displayed in the default face. */
16491 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16493 /* Clear all rows of the menu bar. */
16494 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16496 struct glyph_row *row = it.glyph_row + i;
16497 clear_glyph_row (row);
16498 row->enabled_p = 1;
16499 row->full_width_p = 1;
16502 /* Display all items of the menu bar. */
16503 items = FRAME_MENU_BAR_ITEMS (it.f);
16504 for (i = 0; i < XVECTOR (items)->size; i += 4)
16506 Lisp_Object string;
16508 /* Stop at nil string. */
16509 string = AREF (items, i + 1);
16510 if (NILP (string))
16511 break;
16513 /* Remember where item was displayed. */
16514 AREF (items, i + 3) = make_number (it.hpos);
16516 /* Display the item, pad with one space. */
16517 if (it.current_x < it.last_visible_x)
16518 display_string (NULL, string, Qnil, 0, 0, &it,
16519 SCHARS (string) + 1, 0, 0, -1);
16522 /* Fill out the line with spaces. */
16523 if (it.current_x < it.last_visible_x)
16524 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16526 /* Compute the total height of the lines. */
16527 compute_line_metrics (&it);
16532 /***********************************************************************
16533 Mode Line
16534 ***********************************************************************/
16536 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16537 FORCE is non-zero, redisplay mode lines unconditionally.
16538 Otherwise, redisplay only mode lines that are garbaged. Value is
16539 the number of windows whose mode lines were redisplayed. */
16541 static int
16542 redisplay_mode_lines (window, force)
16543 Lisp_Object window;
16544 int force;
16546 int nwindows = 0;
16548 while (!NILP (window))
16550 struct window *w = XWINDOW (window);
16552 if (WINDOWP (w->hchild))
16553 nwindows += redisplay_mode_lines (w->hchild, force);
16554 else if (WINDOWP (w->vchild))
16555 nwindows += redisplay_mode_lines (w->vchild, force);
16556 else if (force
16557 || FRAME_GARBAGED_P (XFRAME (w->frame))
16558 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16560 struct text_pos lpoint;
16561 struct buffer *old = current_buffer;
16563 /* Set the window's buffer for the mode line display. */
16564 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16565 set_buffer_internal_1 (XBUFFER (w->buffer));
16567 /* Point refers normally to the selected window. For any
16568 other window, set up appropriate value. */
16569 if (!EQ (window, selected_window))
16571 struct text_pos pt;
16573 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16574 if (CHARPOS (pt) < BEGV)
16575 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16576 else if (CHARPOS (pt) > (ZV - 1))
16577 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16578 else
16579 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16582 /* Display mode lines. */
16583 clear_glyph_matrix (w->desired_matrix);
16584 if (display_mode_lines (w))
16586 ++nwindows;
16587 w->must_be_updated_p = 1;
16590 /* Restore old settings. */
16591 set_buffer_internal_1 (old);
16592 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16595 window = w->next;
16598 return nwindows;
16602 /* Display the mode and/or top line of window W. Value is the number
16603 of mode lines displayed. */
16605 static int
16606 display_mode_lines (w)
16607 struct window *w;
16609 Lisp_Object old_selected_window, old_selected_frame;
16610 int n = 0;
16612 old_selected_frame = selected_frame;
16613 selected_frame = w->frame;
16614 old_selected_window = selected_window;
16615 XSETWINDOW (selected_window, w);
16617 /* These will be set while the mode line specs are processed. */
16618 line_number_displayed = 0;
16619 w->column_number_displayed = Qnil;
16621 if (WINDOW_WANTS_MODELINE_P (w))
16623 struct window *sel_w = XWINDOW (old_selected_window);
16625 /* Select mode line face based on the real selected window. */
16626 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16627 current_buffer->mode_line_format);
16628 ++n;
16631 if (WINDOW_WANTS_HEADER_LINE_P (w))
16633 display_mode_line (w, HEADER_LINE_FACE_ID,
16634 current_buffer->header_line_format);
16635 ++n;
16638 selected_frame = old_selected_frame;
16639 selected_window = old_selected_window;
16640 return n;
16644 /* Display mode or top line of window W. FACE_ID specifies which line
16645 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16646 FORMAT is the mode line format to display. Value is the pixel
16647 height of the mode line displayed. */
16649 static int
16650 display_mode_line (w, face_id, format)
16651 struct window *w;
16652 enum face_id face_id;
16653 Lisp_Object format;
16655 struct it it;
16656 struct face *face;
16657 int count = SPECPDL_INDEX ();
16659 init_iterator (&it, w, -1, -1, NULL, face_id);
16660 /* Don't extend on a previously drawn mode-line.
16661 This may happen if called from pos_visible_p. */
16662 it.glyph_row->enabled_p = 0;
16663 prepare_desired_row (it.glyph_row);
16665 it.glyph_row->mode_line_p = 1;
16667 if (! mode_line_inverse_video)
16668 /* Force the mode-line to be displayed in the default face. */
16669 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16671 record_unwind_protect (unwind_format_mode_line,
16672 format_mode_line_unwind_data (NULL, 0));
16674 mode_line_target = MODE_LINE_DISPLAY;
16676 /* Temporarily make frame's keyboard the current kboard so that
16677 kboard-local variables in the mode_line_format will get the right
16678 values. */
16679 push_frame_kboard (it.f);
16680 record_unwind_save_match_data ();
16681 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16682 pop_frame_kboard ();
16684 unbind_to (count, Qnil);
16686 /* Fill up with spaces. */
16687 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16689 compute_line_metrics (&it);
16690 it.glyph_row->full_width_p = 1;
16691 it.glyph_row->continued_p = 0;
16692 it.glyph_row->truncated_on_left_p = 0;
16693 it.glyph_row->truncated_on_right_p = 0;
16695 /* Make a 3D mode-line have a shadow at its right end. */
16696 face = FACE_FROM_ID (it.f, face_id);
16697 extend_face_to_end_of_line (&it);
16698 if (face->box != FACE_NO_BOX)
16700 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16701 + it.glyph_row->used[TEXT_AREA] - 1);
16702 last->right_box_line_p = 1;
16705 return it.glyph_row->height;
16708 /* Move element ELT in LIST to the front of LIST.
16709 Return the updated list. */
16711 static Lisp_Object
16712 move_elt_to_front (elt, list)
16713 Lisp_Object elt, list;
16715 register Lisp_Object tail, prev;
16716 register Lisp_Object tem;
16718 tail = list;
16719 prev = Qnil;
16720 while (CONSP (tail))
16722 tem = XCAR (tail);
16724 if (EQ (elt, tem))
16726 /* Splice out the link TAIL. */
16727 if (NILP (prev))
16728 list = XCDR (tail);
16729 else
16730 Fsetcdr (prev, XCDR (tail));
16732 /* Now make it the first. */
16733 Fsetcdr (tail, list);
16734 return tail;
16736 else
16737 prev = tail;
16738 tail = XCDR (tail);
16739 QUIT;
16742 /* Not found--return unchanged LIST. */
16743 return list;
16746 /* Contribute ELT to the mode line for window IT->w. How it
16747 translates into text depends on its data type.
16749 IT describes the display environment in which we display, as usual.
16751 DEPTH is the depth in recursion. It is used to prevent
16752 infinite recursion here.
16754 FIELD_WIDTH is the number of characters the display of ELT should
16755 occupy in the mode line, and PRECISION is the maximum number of
16756 characters to display from ELT's representation. See
16757 display_string for details.
16759 Returns the hpos of the end of the text generated by ELT.
16761 PROPS is a property list to add to any string we encounter.
16763 If RISKY is nonzero, remove (disregard) any properties in any string
16764 we encounter, and ignore :eval and :propertize.
16766 The global variable `mode_line_target' determines whether the
16767 output is passed to `store_mode_line_noprop',
16768 `store_mode_line_string', or `display_string'. */
16770 static int
16771 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16772 struct it *it;
16773 int depth;
16774 int field_width, precision;
16775 Lisp_Object elt, props;
16776 int risky;
16778 int n = 0, field, prec;
16779 int literal = 0;
16781 tail_recurse:
16782 if (depth > 100)
16783 elt = build_string ("*too-deep*");
16785 depth++;
16787 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16789 case Lisp_String:
16791 /* A string: output it and check for %-constructs within it. */
16792 unsigned char c;
16793 int offset = 0;
16795 if (SCHARS (elt) > 0
16796 && (!NILP (props) || risky))
16798 Lisp_Object oprops, aelt;
16799 oprops = Ftext_properties_at (make_number (0), elt);
16801 /* If the starting string's properties are not what
16802 we want, translate the string. Also, if the string
16803 is risky, do that anyway. */
16805 if (NILP (Fequal (props, oprops)) || risky)
16807 /* If the starting string has properties,
16808 merge the specified ones onto the existing ones. */
16809 if (! NILP (oprops) && !risky)
16811 Lisp_Object tem;
16813 oprops = Fcopy_sequence (oprops);
16814 tem = props;
16815 while (CONSP (tem))
16817 oprops = Fplist_put (oprops, XCAR (tem),
16818 XCAR (XCDR (tem)));
16819 tem = XCDR (XCDR (tem));
16821 props = oprops;
16824 aelt = Fassoc (elt, mode_line_proptrans_alist);
16825 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16827 /* AELT is what we want. Move it to the front
16828 without consing. */
16829 elt = XCAR (aelt);
16830 mode_line_proptrans_alist
16831 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16833 else
16835 Lisp_Object tem;
16837 /* If AELT has the wrong props, it is useless.
16838 so get rid of it. */
16839 if (! NILP (aelt))
16840 mode_line_proptrans_alist
16841 = Fdelq (aelt, mode_line_proptrans_alist);
16843 elt = Fcopy_sequence (elt);
16844 Fset_text_properties (make_number (0), Flength (elt),
16845 props, elt);
16846 /* Add this item to mode_line_proptrans_alist. */
16847 mode_line_proptrans_alist
16848 = Fcons (Fcons (elt, props),
16849 mode_line_proptrans_alist);
16850 /* Truncate mode_line_proptrans_alist
16851 to at most 50 elements. */
16852 tem = Fnthcdr (make_number (50),
16853 mode_line_proptrans_alist);
16854 if (! NILP (tem))
16855 XSETCDR (tem, Qnil);
16860 offset = 0;
16862 if (literal)
16864 prec = precision - n;
16865 switch (mode_line_target)
16867 case MODE_LINE_NOPROP:
16868 case MODE_LINE_TITLE:
16869 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16870 break;
16871 case MODE_LINE_STRING:
16872 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16873 break;
16874 case MODE_LINE_DISPLAY:
16875 n += display_string (NULL, elt, Qnil, 0, 0, it,
16876 0, prec, 0, STRING_MULTIBYTE (elt));
16877 break;
16880 break;
16883 /* Handle the non-literal case. */
16885 while ((precision <= 0 || n < precision)
16886 && SREF (elt, offset) != 0
16887 && (mode_line_target != MODE_LINE_DISPLAY
16888 || it->current_x < it->last_visible_x))
16890 int last_offset = offset;
16892 /* Advance to end of string or next format specifier. */
16893 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16896 if (offset - 1 != last_offset)
16898 int nchars, nbytes;
16900 /* Output to end of string or up to '%'. Field width
16901 is length of string. Don't output more than
16902 PRECISION allows us. */
16903 offset--;
16905 prec = c_string_width (SDATA (elt) + last_offset,
16906 offset - last_offset, precision - n,
16907 &nchars, &nbytes);
16909 switch (mode_line_target)
16911 case MODE_LINE_NOPROP:
16912 case MODE_LINE_TITLE:
16913 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16914 break;
16915 case MODE_LINE_STRING:
16917 int bytepos = last_offset;
16918 int charpos = string_byte_to_char (elt, bytepos);
16919 int endpos = (precision <= 0
16920 ? string_byte_to_char (elt, offset)
16921 : charpos + nchars);
16923 n += store_mode_line_string (NULL,
16924 Fsubstring (elt, make_number (charpos),
16925 make_number (endpos)),
16926 0, 0, 0, Qnil);
16928 break;
16929 case MODE_LINE_DISPLAY:
16931 int bytepos = last_offset;
16932 int charpos = string_byte_to_char (elt, bytepos);
16934 if (precision <= 0)
16935 nchars = string_byte_to_char (elt, offset) - charpos;
16936 n += display_string (NULL, elt, Qnil, 0, charpos,
16937 it, 0, nchars, 0,
16938 STRING_MULTIBYTE (elt));
16940 break;
16943 else /* c == '%' */
16945 int percent_position = offset;
16947 /* Get the specified minimum width. Zero means
16948 don't pad. */
16949 field = 0;
16950 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16951 field = field * 10 + c - '0';
16953 /* Don't pad beyond the total padding allowed. */
16954 if (field_width - n > 0 && field > field_width - n)
16955 field = field_width - n;
16957 /* Note that either PRECISION <= 0 or N < PRECISION. */
16958 prec = precision - n;
16960 if (c == 'M')
16961 n += display_mode_element (it, depth, field, prec,
16962 Vglobal_mode_string, props,
16963 risky);
16964 else if (c != 0)
16966 int multibyte;
16967 int bytepos, charpos;
16968 unsigned char *spec;
16970 bytepos = percent_position;
16971 charpos = (STRING_MULTIBYTE (elt)
16972 ? string_byte_to_char (elt, bytepos)
16973 : bytepos);
16975 spec
16976 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16978 switch (mode_line_target)
16980 case MODE_LINE_NOPROP:
16981 case MODE_LINE_TITLE:
16982 n += store_mode_line_noprop (spec, field, prec);
16983 break;
16984 case MODE_LINE_STRING:
16986 int len = strlen (spec);
16987 Lisp_Object tem = make_string (spec, len);
16988 props = Ftext_properties_at (make_number (charpos), elt);
16989 /* Should only keep face property in props */
16990 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16992 break;
16993 case MODE_LINE_DISPLAY:
16995 int nglyphs_before, nwritten;
16997 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16998 nwritten = display_string (spec, Qnil, elt,
16999 charpos, 0, it,
17000 field, prec, 0,
17001 multibyte);
17003 /* Assign to the glyphs written above the
17004 string where the `%x' came from, position
17005 of the `%'. */
17006 if (nwritten > 0)
17008 struct glyph *glyph
17009 = (it->glyph_row->glyphs[TEXT_AREA]
17010 + nglyphs_before);
17011 int i;
17013 for (i = 0; i < nwritten; ++i)
17015 glyph[i].object = elt;
17016 glyph[i].charpos = charpos;
17019 n += nwritten;
17022 break;
17025 else /* c == 0 */
17026 break;
17030 break;
17032 case Lisp_Symbol:
17033 /* A symbol: process the value of the symbol recursively
17034 as if it appeared here directly. Avoid error if symbol void.
17035 Special case: if value of symbol is a string, output the string
17036 literally. */
17038 register Lisp_Object tem;
17040 /* If the variable is not marked as risky to set
17041 then its contents are risky to use. */
17042 if (NILP (Fget (elt, Qrisky_local_variable)))
17043 risky = 1;
17045 tem = Fboundp (elt);
17046 if (!NILP (tem))
17048 tem = Fsymbol_value (elt);
17049 /* If value is a string, output that string literally:
17050 don't check for % within it. */
17051 if (STRINGP (tem))
17052 literal = 1;
17054 if (!EQ (tem, elt))
17056 /* Give up right away for nil or t. */
17057 elt = tem;
17058 goto tail_recurse;
17062 break;
17064 case Lisp_Cons:
17066 register Lisp_Object car, tem;
17068 /* A cons cell: five distinct cases.
17069 If first element is :eval or :propertize, do something special.
17070 If first element is a string or a cons, process all the elements
17071 and effectively concatenate them.
17072 If first element is a negative number, truncate displaying cdr to
17073 at most that many characters. If positive, pad (with spaces)
17074 to at least that many characters.
17075 If first element is a symbol, process the cadr or caddr recursively
17076 according to whether the symbol's value is non-nil or nil. */
17077 car = XCAR (elt);
17078 if (EQ (car, QCeval))
17080 /* An element of the form (:eval FORM) means evaluate FORM
17081 and use the result as mode line elements. */
17083 if (risky)
17084 break;
17086 if (CONSP (XCDR (elt)))
17088 Lisp_Object spec;
17089 spec = safe_eval (XCAR (XCDR (elt)));
17090 n += display_mode_element (it, depth, field_width - n,
17091 precision - n, spec, props,
17092 risky);
17095 else if (EQ (car, QCpropertize))
17097 /* An element of the form (:propertize ELT PROPS...)
17098 means display ELT but applying properties PROPS. */
17100 if (risky)
17101 break;
17103 if (CONSP (XCDR (elt)))
17104 n += display_mode_element (it, depth, field_width - n,
17105 precision - n, XCAR (XCDR (elt)),
17106 XCDR (XCDR (elt)), risky);
17108 else if (SYMBOLP (car))
17110 tem = Fboundp (car);
17111 elt = XCDR (elt);
17112 if (!CONSP (elt))
17113 goto invalid;
17114 /* elt is now the cdr, and we know it is a cons cell.
17115 Use its car if CAR has a non-nil value. */
17116 if (!NILP (tem))
17118 tem = Fsymbol_value (car);
17119 if (!NILP (tem))
17121 elt = XCAR (elt);
17122 goto tail_recurse;
17125 /* Symbol's value is nil (or symbol is unbound)
17126 Get the cddr of the original list
17127 and if possible find the caddr and use that. */
17128 elt = XCDR (elt);
17129 if (NILP (elt))
17130 break;
17131 else if (!CONSP (elt))
17132 goto invalid;
17133 elt = XCAR (elt);
17134 goto tail_recurse;
17136 else if (INTEGERP (car))
17138 register int lim = XINT (car);
17139 elt = XCDR (elt);
17140 if (lim < 0)
17142 /* Negative int means reduce maximum width. */
17143 if (precision <= 0)
17144 precision = -lim;
17145 else
17146 precision = min (precision, -lim);
17148 else if (lim > 0)
17150 /* Padding specified. Don't let it be more than
17151 current maximum. */
17152 if (precision > 0)
17153 lim = min (precision, lim);
17155 /* If that's more padding than already wanted, queue it.
17156 But don't reduce padding already specified even if
17157 that is beyond the current truncation point. */
17158 field_width = max (lim, field_width);
17160 goto tail_recurse;
17162 else if (STRINGP (car) || CONSP (car))
17164 register int limit = 50;
17165 /* Limit is to protect against circular lists. */
17166 while (CONSP (elt)
17167 && --limit > 0
17168 && (precision <= 0 || n < precision))
17170 n += display_mode_element (it, depth,
17171 /* Do padding only after the last
17172 element in the list. */
17173 (! CONSP (XCDR (elt))
17174 ? field_width - n
17175 : 0),
17176 precision - n, XCAR (elt),
17177 props, risky);
17178 elt = XCDR (elt);
17182 break;
17184 default:
17185 invalid:
17186 elt = build_string ("*invalid*");
17187 goto tail_recurse;
17190 /* Pad to FIELD_WIDTH. */
17191 if (field_width > 0 && n < field_width)
17193 switch (mode_line_target)
17195 case MODE_LINE_NOPROP:
17196 case MODE_LINE_TITLE:
17197 n += store_mode_line_noprop ("", field_width - n, 0);
17198 break;
17199 case MODE_LINE_STRING:
17200 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17201 break;
17202 case MODE_LINE_DISPLAY:
17203 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17204 0, 0, 0);
17205 break;
17209 return n;
17212 /* Store a mode-line string element in mode_line_string_list.
17214 If STRING is non-null, display that C string. Otherwise, the Lisp
17215 string LISP_STRING is displayed.
17217 FIELD_WIDTH is the minimum number of output glyphs to produce.
17218 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17219 with spaces. FIELD_WIDTH <= 0 means don't pad.
17221 PRECISION is the maximum number of characters to output from
17222 STRING. PRECISION <= 0 means don't truncate the string.
17224 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17225 properties to the string.
17227 PROPS are the properties to add to the string.
17228 The mode_line_string_face face property is always added to the string.
17231 static int
17232 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17233 char *string;
17234 Lisp_Object lisp_string;
17235 int copy_string;
17236 int field_width;
17237 int precision;
17238 Lisp_Object props;
17240 int len;
17241 int n = 0;
17243 if (string != NULL)
17245 len = strlen (string);
17246 if (precision > 0 && len > precision)
17247 len = precision;
17248 lisp_string = make_string (string, len);
17249 if (NILP (props))
17250 props = mode_line_string_face_prop;
17251 else if (!NILP (mode_line_string_face))
17253 Lisp_Object face = Fplist_get (props, Qface);
17254 props = Fcopy_sequence (props);
17255 if (NILP (face))
17256 face = mode_line_string_face;
17257 else
17258 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17259 props = Fplist_put (props, Qface, face);
17261 Fadd_text_properties (make_number (0), make_number (len),
17262 props, lisp_string);
17264 else
17266 len = XFASTINT (Flength (lisp_string));
17267 if (precision > 0 && len > precision)
17269 len = precision;
17270 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17271 precision = -1;
17273 if (!NILP (mode_line_string_face))
17275 Lisp_Object face;
17276 if (NILP (props))
17277 props = Ftext_properties_at (make_number (0), lisp_string);
17278 face = Fplist_get (props, Qface);
17279 if (NILP (face))
17280 face = mode_line_string_face;
17281 else
17282 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17283 props = Fcons (Qface, Fcons (face, Qnil));
17284 if (copy_string)
17285 lisp_string = Fcopy_sequence (lisp_string);
17287 if (!NILP (props))
17288 Fadd_text_properties (make_number (0), make_number (len),
17289 props, lisp_string);
17292 if (len > 0)
17294 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17295 n += len;
17298 if (field_width > len)
17300 field_width -= len;
17301 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17302 if (!NILP (props))
17303 Fadd_text_properties (make_number (0), make_number (field_width),
17304 props, lisp_string);
17305 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17306 n += field_width;
17309 return n;
17313 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17314 1, 4, 0,
17315 doc: /* Format a string out of a mode line format specification.
17316 First arg FORMAT specifies the mode line format (see `mode-line-format'
17317 for details) to use.
17319 Optional second arg FACE specifies the face property to put
17320 on all characters for which no face is specified.
17321 The value t means whatever face the window's mode line currently uses
17322 \(either `mode-line' or `mode-line-inactive', depending).
17323 A value of nil means the default is no face property.
17324 If FACE is an integer, the value string has no text properties.
17326 Optional third and fourth args WINDOW and BUFFER specify the window
17327 and buffer to use as the context for the formatting (defaults
17328 are the selected window and the window's buffer). */)
17329 (format, face, window, buffer)
17330 Lisp_Object format, face, window, buffer;
17332 struct it it;
17333 int len;
17334 struct window *w;
17335 struct buffer *old_buffer = NULL;
17336 int face_id = -1;
17337 int no_props = INTEGERP (face);
17338 int count = SPECPDL_INDEX ();
17339 Lisp_Object str;
17340 int string_start = 0;
17342 if (NILP (window))
17343 window = selected_window;
17344 CHECK_WINDOW (window);
17345 w = XWINDOW (window);
17347 if (NILP (buffer))
17348 buffer = w->buffer;
17349 CHECK_BUFFER (buffer);
17351 if (NILP (format))
17352 return empty_unibyte_string;
17354 if (no_props)
17355 face = Qnil;
17357 if (!NILP (face))
17359 if (EQ (face, Qt))
17360 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17361 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17364 if (face_id < 0)
17365 face_id = DEFAULT_FACE_ID;
17367 if (XBUFFER (buffer) != current_buffer)
17368 old_buffer = current_buffer;
17370 /* Save things including mode_line_proptrans_alist,
17371 and set that to nil so that we don't alter the outer value. */
17372 record_unwind_protect (unwind_format_mode_line,
17373 format_mode_line_unwind_data (old_buffer, 1));
17374 mode_line_proptrans_alist = Qnil;
17376 if (old_buffer)
17377 set_buffer_internal_1 (XBUFFER (buffer));
17379 init_iterator (&it, w, -1, -1, NULL, face_id);
17381 if (no_props)
17383 mode_line_target = MODE_LINE_NOPROP;
17384 mode_line_string_face_prop = Qnil;
17385 mode_line_string_list = Qnil;
17386 string_start = MODE_LINE_NOPROP_LEN (0);
17388 else
17390 mode_line_target = MODE_LINE_STRING;
17391 mode_line_string_list = Qnil;
17392 mode_line_string_face = face;
17393 mode_line_string_face_prop
17394 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17397 push_frame_kboard (it.f);
17398 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17399 pop_frame_kboard ();
17401 if (no_props)
17403 len = MODE_LINE_NOPROP_LEN (string_start);
17404 str = make_string (mode_line_noprop_buf + string_start, len);
17406 else
17408 mode_line_string_list = Fnreverse (mode_line_string_list);
17409 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17410 empty_unibyte_string);
17413 unbind_to (count, Qnil);
17414 return str;
17417 /* Write a null-terminated, right justified decimal representation of
17418 the positive integer D to BUF using a minimal field width WIDTH. */
17420 static void
17421 pint2str (buf, width, d)
17422 register char *buf;
17423 register int width;
17424 register int d;
17426 register char *p = buf;
17428 if (d <= 0)
17429 *p++ = '0';
17430 else
17432 while (d > 0)
17434 *p++ = d % 10 + '0';
17435 d /= 10;
17439 for (width -= (int) (p - buf); width > 0; --width)
17440 *p++ = ' ';
17441 *p-- = '\0';
17442 while (p > buf)
17444 d = *buf;
17445 *buf++ = *p;
17446 *p-- = d;
17450 /* Write a null-terminated, right justified decimal and "human
17451 readable" representation of the nonnegative integer D to BUF using
17452 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17454 static const char power_letter[] =
17456 0, /* not used */
17457 'k', /* kilo */
17458 'M', /* mega */
17459 'G', /* giga */
17460 'T', /* tera */
17461 'P', /* peta */
17462 'E', /* exa */
17463 'Z', /* zetta */
17464 'Y' /* yotta */
17467 static void
17468 pint2hrstr (buf, width, d)
17469 char *buf;
17470 int width;
17471 int d;
17473 /* We aim to represent the nonnegative integer D as
17474 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17475 int quotient = d;
17476 int remainder = 0;
17477 /* -1 means: do not use TENTHS. */
17478 int tenths = -1;
17479 int exponent = 0;
17481 /* Length of QUOTIENT.TENTHS as a string. */
17482 int length;
17484 char * psuffix;
17485 char * p;
17487 if (1000 <= quotient)
17489 /* Scale to the appropriate EXPONENT. */
17492 remainder = quotient % 1000;
17493 quotient /= 1000;
17494 exponent++;
17496 while (1000 <= quotient);
17498 /* Round to nearest and decide whether to use TENTHS or not. */
17499 if (quotient <= 9)
17501 tenths = remainder / 100;
17502 if (50 <= remainder % 100)
17504 if (tenths < 9)
17505 tenths++;
17506 else
17508 quotient++;
17509 if (quotient == 10)
17510 tenths = -1;
17511 else
17512 tenths = 0;
17516 else
17517 if (500 <= remainder)
17519 if (quotient < 999)
17520 quotient++;
17521 else
17523 quotient = 1;
17524 exponent++;
17525 tenths = 0;
17530 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17531 if (tenths == -1 && quotient <= 99)
17532 if (quotient <= 9)
17533 length = 1;
17534 else
17535 length = 2;
17536 else
17537 length = 3;
17538 p = psuffix = buf + max (width, length);
17540 /* Print EXPONENT. */
17541 if (exponent)
17542 *psuffix++ = power_letter[exponent];
17543 *psuffix = '\0';
17545 /* Print TENTHS. */
17546 if (tenths >= 0)
17548 *--p = '0' + tenths;
17549 *--p = '.';
17552 /* Print QUOTIENT. */
17555 int digit = quotient % 10;
17556 *--p = '0' + digit;
17558 while ((quotient /= 10) != 0);
17560 /* Print leading spaces. */
17561 while (buf < p)
17562 *--p = ' ';
17565 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17566 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17567 type of CODING_SYSTEM. Return updated pointer into BUF. */
17569 static unsigned char invalid_eol_type[] = "(*invalid*)";
17571 static char *
17572 decode_mode_spec_coding (coding_system, buf, eol_flag)
17573 Lisp_Object coding_system;
17574 register char *buf;
17575 int eol_flag;
17577 Lisp_Object val;
17578 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17579 const unsigned char *eol_str;
17580 int eol_str_len;
17581 /* The EOL conversion we are using. */
17582 Lisp_Object eoltype;
17584 val = Fget (coding_system, Qcoding_system);
17585 eoltype = Qnil;
17587 if (!VECTORP (val)) /* Not yet decided. */
17589 if (multibyte)
17590 *buf++ = '-';
17591 if (eol_flag)
17592 eoltype = eol_mnemonic_undecided;
17593 /* Don't mention EOL conversion if it isn't decided. */
17595 else
17597 Lisp_Object eolvalue;
17599 eolvalue = Fget (coding_system, Qeol_type);
17601 if (multibyte)
17602 *buf++ = XFASTINT (AREF (val, 1));
17604 if (eol_flag)
17606 /* The EOL conversion that is normal on this system. */
17608 if (NILP (eolvalue)) /* Not yet decided. */
17609 eoltype = eol_mnemonic_undecided;
17610 else if (VECTORP (eolvalue)) /* Not yet decided. */
17611 eoltype = eol_mnemonic_undecided;
17612 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17613 eoltype = (XFASTINT (eolvalue) == 0
17614 ? eol_mnemonic_unix
17615 : (XFASTINT (eolvalue) == 1
17616 ? eol_mnemonic_dos : eol_mnemonic_mac));
17620 if (eol_flag)
17622 /* Mention the EOL conversion if it is not the usual one. */
17623 if (STRINGP (eoltype))
17625 eol_str = SDATA (eoltype);
17626 eol_str_len = SBYTES (eoltype);
17628 else if (INTEGERP (eoltype)
17629 && CHAR_VALID_P (XINT (eoltype), 0))
17631 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17632 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17633 eol_str = tmp;
17635 else
17637 eol_str = invalid_eol_type;
17638 eol_str_len = sizeof (invalid_eol_type) - 1;
17640 bcopy (eol_str, buf, eol_str_len);
17641 buf += eol_str_len;
17644 return buf;
17647 /* Return a string for the output of a mode line %-spec for window W,
17648 generated by character C. PRECISION >= 0 means don't return a
17649 string longer than that value. FIELD_WIDTH > 0 means pad the
17650 string returned with spaces to that value. Return 1 in *MULTIBYTE
17651 if the result is multibyte text.
17653 Note we operate on the current buffer for most purposes,
17654 the exception being w->base_line_pos. */
17656 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17658 static char *
17659 decode_mode_spec (w, c, field_width, precision, multibyte)
17660 struct window *w;
17661 register int c;
17662 int field_width, precision;
17663 int *multibyte;
17665 Lisp_Object obj;
17666 struct frame *f = XFRAME (WINDOW_FRAME (w));
17667 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17668 struct buffer *b = current_buffer;
17670 obj = Qnil;
17671 *multibyte = 0;
17673 switch (c)
17675 case '*':
17676 if (!NILP (b->read_only))
17677 return "%";
17678 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17679 return "*";
17680 return "-";
17682 case '+':
17683 /* This differs from %* only for a modified read-only buffer. */
17684 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17685 return "*";
17686 if (!NILP (b->read_only))
17687 return "%";
17688 return "-";
17690 case '&':
17691 /* This differs from %* in ignoring read-only-ness. */
17692 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17693 return "*";
17694 return "-";
17696 case '%':
17697 return "%";
17699 case '[':
17701 int i;
17702 char *p;
17704 if (command_loop_level > 5)
17705 return "[[[... ";
17706 p = decode_mode_spec_buf;
17707 for (i = 0; i < command_loop_level; i++)
17708 *p++ = '[';
17709 *p = 0;
17710 return decode_mode_spec_buf;
17713 case ']':
17715 int i;
17716 char *p;
17718 if (command_loop_level > 5)
17719 return " ...]]]";
17720 p = decode_mode_spec_buf;
17721 for (i = 0; i < command_loop_level; i++)
17722 *p++ = ']';
17723 *p = 0;
17724 return decode_mode_spec_buf;
17727 case '-':
17729 register int i;
17731 /* Let lots_of_dashes be a string of infinite length. */
17732 if (mode_line_target == MODE_LINE_NOPROP ||
17733 mode_line_target == MODE_LINE_STRING)
17734 return "--";
17735 if (field_width <= 0
17736 || field_width > sizeof (lots_of_dashes))
17738 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17739 decode_mode_spec_buf[i] = '-';
17740 decode_mode_spec_buf[i] = '\0';
17741 return decode_mode_spec_buf;
17743 else
17744 return lots_of_dashes;
17747 case 'b':
17748 obj = b->name;
17749 break;
17751 case 'c':
17752 /* %c and %l are ignored in `frame-title-format'.
17753 (In redisplay_internal, the frame title is drawn _before_ the
17754 windows are updated, so the stuff which depends on actual
17755 window contents (such as %l) may fail to render properly, or
17756 even crash emacs.) */
17757 if (mode_line_target == MODE_LINE_TITLE)
17758 return "";
17759 else
17761 int col = (int) current_column (); /* iftc */
17762 w->column_number_displayed = make_number (col);
17763 pint2str (decode_mode_spec_buf, field_width, col);
17764 return decode_mode_spec_buf;
17767 case 'e':
17768 #ifndef SYSTEM_MALLOC
17770 if (NILP (Vmemory_full))
17771 return "";
17772 else
17773 return "!MEM FULL! ";
17775 #else
17776 return "";
17777 #endif
17779 case 'F':
17780 /* %F displays the frame name. */
17781 if (!NILP (f->title))
17782 return (char *) SDATA (f->title);
17783 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17784 return (char *) SDATA (f->name);
17785 return "Emacs";
17787 case 'f':
17788 obj = b->filename;
17789 break;
17791 case 'i':
17793 int size = ZV - BEGV;
17794 pint2str (decode_mode_spec_buf, field_width, size);
17795 return decode_mode_spec_buf;
17798 case 'I':
17800 int size = ZV - BEGV;
17801 pint2hrstr (decode_mode_spec_buf, field_width, size);
17802 return decode_mode_spec_buf;
17805 case 'l':
17807 int startpos, startpos_byte, line, linepos, linepos_byte;
17808 int topline, nlines, junk, height;
17810 /* %c and %l are ignored in `frame-title-format'. */
17811 if (mode_line_target == MODE_LINE_TITLE)
17812 return "";
17814 startpos = XMARKER (w->start)->charpos;
17815 startpos_byte = marker_byte_position (w->start);
17816 height = WINDOW_TOTAL_LINES (w);
17818 /* If we decided that this buffer isn't suitable for line numbers,
17819 don't forget that too fast. */
17820 if (EQ (w->base_line_pos, w->buffer))
17821 goto no_value;
17822 /* But do forget it, if the window shows a different buffer now. */
17823 else if (BUFFERP (w->base_line_pos))
17824 w->base_line_pos = Qnil;
17826 /* If the buffer is very big, don't waste time. */
17827 if (INTEGERP (Vline_number_display_limit)
17828 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17830 w->base_line_pos = Qnil;
17831 w->base_line_number = Qnil;
17832 goto no_value;
17835 if (!NILP (w->base_line_number)
17836 && !NILP (w->base_line_pos)
17837 && XFASTINT (w->base_line_pos) <= startpos)
17839 line = XFASTINT (w->base_line_number);
17840 linepos = XFASTINT (w->base_line_pos);
17841 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17843 else
17845 line = 1;
17846 linepos = BUF_BEGV (b);
17847 linepos_byte = BUF_BEGV_BYTE (b);
17850 /* Count lines from base line to window start position. */
17851 nlines = display_count_lines (linepos, linepos_byte,
17852 startpos_byte,
17853 startpos, &junk);
17855 topline = nlines + line;
17857 /* Determine a new base line, if the old one is too close
17858 or too far away, or if we did not have one.
17859 "Too close" means it's plausible a scroll-down would
17860 go back past it. */
17861 if (startpos == BUF_BEGV (b))
17863 w->base_line_number = make_number (topline);
17864 w->base_line_pos = make_number (BUF_BEGV (b));
17866 else if (nlines < height + 25 || nlines > height * 3 + 50
17867 || linepos == BUF_BEGV (b))
17869 int limit = BUF_BEGV (b);
17870 int limit_byte = BUF_BEGV_BYTE (b);
17871 int position;
17872 int distance = (height * 2 + 30) * line_number_display_limit_width;
17874 if (startpos - distance > limit)
17876 limit = startpos - distance;
17877 limit_byte = CHAR_TO_BYTE (limit);
17880 nlines = display_count_lines (startpos, startpos_byte,
17881 limit_byte,
17882 - (height * 2 + 30),
17883 &position);
17884 /* If we couldn't find the lines we wanted within
17885 line_number_display_limit_width chars per line,
17886 give up on line numbers for this window. */
17887 if (position == limit_byte && limit == startpos - distance)
17889 w->base_line_pos = w->buffer;
17890 w->base_line_number = Qnil;
17891 goto no_value;
17894 w->base_line_number = make_number (topline - nlines);
17895 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17898 /* Now count lines from the start pos to point. */
17899 nlines = display_count_lines (startpos, startpos_byte,
17900 PT_BYTE, PT, &junk);
17902 /* Record that we did display the line number. */
17903 line_number_displayed = 1;
17905 /* Make the string to show. */
17906 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17907 return decode_mode_spec_buf;
17908 no_value:
17910 char* p = decode_mode_spec_buf;
17911 int pad = field_width - 2;
17912 while (pad-- > 0)
17913 *p++ = ' ';
17914 *p++ = '?';
17915 *p++ = '?';
17916 *p = '\0';
17917 return decode_mode_spec_buf;
17920 break;
17922 case 'm':
17923 obj = b->mode_name;
17924 break;
17926 case 'n':
17927 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17928 return " Narrow";
17929 break;
17931 case 'p':
17933 int pos = marker_position (w->start);
17934 int total = BUF_ZV (b) - BUF_BEGV (b);
17936 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17938 if (pos <= BUF_BEGV (b))
17939 return "All";
17940 else
17941 return "Bottom";
17943 else if (pos <= BUF_BEGV (b))
17944 return "Top";
17945 else
17947 if (total > 1000000)
17948 /* Do it differently for a large value, to avoid overflow. */
17949 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17950 else
17951 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17952 /* We can't normally display a 3-digit number,
17953 so get us a 2-digit number that is close. */
17954 if (total == 100)
17955 total = 99;
17956 sprintf (decode_mode_spec_buf, "%2d%%", total);
17957 return decode_mode_spec_buf;
17961 /* Display percentage of size above the bottom of the screen. */
17962 case 'P':
17964 int toppos = marker_position (w->start);
17965 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17966 int total = BUF_ZV (b) - BUF_BEGV (b);
17968 if (botpos >= BUF_ZV (b))
17970 if (toppos <= BUF_BEGV (b))
17971 return "All";
17972 else
17973 return "Bottom";
17975 else
17977 if (total > 1000000)
17978 /* Do it differently for a large value, to avoid overflow. */
17979 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17980 else
17981 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17982 /* We can't normally display a 3-digit number,
17983 so get us a 2-digit number that is close. */
17984 if (total == 100)
17985 total = 99;
17986 if (toppos <= BUF_BEGV (b))
17987 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17988 else
17989 sprintf (decode_mode_spec_buf, "%2d%%", total);
17990 return decode_mode_spec_buf;
17994 case 's':
17995 /* status of process */
17996 obj = Fget_buffer_process (Fcurrent_buffer ());
17997 if (NILP (obj))
17998 return "no process";
17999 #ifdef subprocesses
18000 obj = Fsymbol_name (Fprocess_status (obj));
18001 #endif
18002 break;
18004 case '@':
18006 Lisp_Object val;
18007 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18008 if (NILP (val))
18009 return "-";
18010 else
18011 return "@";
18014 case 't': /* indicate TEXT or BINARY */
18015 #ifdef MODE_LINE_BINARY_TEXT
18016 return MODE_LINE_BINARY_TEXT (b);
18017 #else
18018 return "T";
18019 #endif
18021 case 'z':
18022 /* coding-system (not including end-of-line format) */
18023 case 'Z':
18024 /* coding-system (including end-of-line type) */
18026 int eol_flag = (c == 'Z');
18027 char *p = decode_mode_spec_buf;
18029 if (! FRAME_WINDOW_P (f))
18031 /* No need to mention EOL here--the terminal never needs
18032 to do EOL conversion. */
18033 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
18034 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
18036 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18037 p, eol_flag);
18039 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18040 #ifdef subprocesses
18041 obj = Fget_buffer_process (Fcurrent_buffer ());
18042 if (PROCESSP (obj))
18044 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18045 p, eol_flag);
18046 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18047 p, eol_flag);
18049 #endif /* subprocesses */
18050 #endif /* 0 */
18051 *p = 0;
18052 return decode_mode_spec_buf;
18056 if (STRINGP (obj))
18058 *multibyte = STRING_MULTIBYTE (obj);
18059 return (char *) SDATA (obj);
18061 else
18062 return "";
18066 /* Count up to COUNT lines starting from START / START_BYTE.
18067 But don't go beyond LIMIT_BYTE.
18068 Return the number of lines thus found (always nonnegative).
18070 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18072 static int
18073 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18074 int start, start_byte, limit_byte, count;
18075 int *byte_pos_ptr;
18077 register unsigned char *cursor;
18078 unsigned char *base;
18080 register int ceiling;
18081 register unsigned char *ceiling_addr;
18082 int orig_count = count;
18084 /* If we are not in selective display mode,
18085 check only for newlines. */
18086 int selective_display = (!NILP (current_buffer->selective_display)
18087 && !INTEGERP (current_buffer->selective_display));
18089 if (count > 0)
18091 while (start_byte < limit_byte)
18093 ceiling = BUFFER_CEILING_OF (start_byte);
18094 ceiling = min (limit_byte - 1, ceiling);
18095 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18096 base = (cursor = BYTE_POS_ADDR (start_byte));
18097 while (1)
18099 if (selective_display)
18100 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18102 else
18103 while (*cursor != '\n' && ++cursor != ceiling_addr)
18106 if (cursor != ceiling_addr)
18108 if (--count == 0)
18110 start_byte += cursor - base + 1;
18111 *byte_pos_ptr = start_byte;
18112 return orig_count;
18114 else
18115 if (++cursor == ceiling_addr)
18116 break;
18118 else
18119 break;
18121 start_byte += cursor - base;
18124 else
18126 while (start_byte > limit_byte)
18128 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18129 ceiling = max (limit_byte, ceiling);
18130 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18131 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18132 while (1)
18134 if (selective_display)
18135 while (--cursor != ceiling_addr
18136 && *cursor != '\n' && *cursor != 015)
18138 else
18139 while (--cursor != ceiling_addr && *cursor != '\n')
18142 if (cursor != ceiling_addr)
18144 if (++count == 0)
18146 start_byte += cursor - base + 1;
18147 *byte_pos_ptr = start_byte;
18148 /* When scanning backwards, we should
18149 not count the newline posterior to which we stop. */
18150 return - orig_count - 1;
18153 else
18154 break;
18156 /* Here we add 1 to compensate for the last decrement
18157 of CURSOR, which took it past the valid range. */
18158 start_byte += cursor - base + 1;
18162 *byte_pos_ptr = limit_byte;
18164 if (count < 0)
18165 return - orig_count + count;
18166 return orig_count - count;
18172 /***********************************************************************
18173 Displaying strings
18174 ***********************************************************************/
18176 /* Display a NUL-terminated string, starting with index START.
18178 If STRING is non-null, display that C string. Otherwise, the Lisp
18179 string LISP_STRING is displayed.
18181 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18182 FACE_STRING. Display STRING or LISP_STRING with the face at
18183 FACE_STRING_POS in FACE_STRING:
18185 Display the string in the environment given by IT, but use the
18186 standard display table, temporarily.
18188 FIELD_WIDTH is the minimum number of output glyphs to produce.
18189 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18190 with spaces. If STRING has more characters, more than FIELD_WIDTH
18191 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18193 PRECISION is the maximum number of characters to output from
18194 STRING. PRECISION < 0 means don't truncate the string.
18196 This is roughly equivalent to printf format specifiers:
18198 FIELD_WIDTH PRECISION PRINTF
18199 ----------------------------------------
18200 -1 -1 %s
18201 -1 10 %.10s
18202 10 -1 %10s
18203 20 10 %20.10s
18205 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18206 display them, and < 0 means obey the current buffer's value of
18207 enable_multibyte_characters.
18209 Value is the number of columns displayed. */
18211 static int
18212 display_string (string, lisp_string, face_string, face_string_pos,
18213 start, it, field_width, precision, max_x, multibyte)
18214 unsigned char *string;
18215 Lisp_Object lisp_string;
18216 Lisp_Object face_string;
18217 int face_string_pos;
18218 int start;
18219 struct it *it;
18220 int field_width, precision, max_x;
18221 int multibyte;
18223 int hpos_at_start = it->hpos;
18224 int saved_face_id = it->face_id;
18225 struct glyph_row *row = it->glyph_row;
18227 /* Initialize the iterator IT for iteration over STRING beginning
18228 with index START. */
18229 reseat_to_string (it, string, lisp_string, start,
18230 precision, field_width, multibyte);
18232 /* If displaying STRING, set up the face of the iterator
18233 from LISP_STRING, if that's given. */
18234 if (STRINGP (face_string))
18236 int endptr;
18237 struct face *face;
18239 it->face_id
18240 = face_at_string_position (it->w, face_string, face_string_pos,
18241 0, it->region_beg_charpos,
18242 it->region_end_charpos,
18243 &endptr, it->base_face_id, 0);
18244 face = FACE_FROM_ID (it->f, it->face_id);
18245 it->face_box_p = face->box != FACE_NO_BOX;
18248 /* Set max_x to the maximum allowed X position. Don't let it go
18249 beyond the right edge of the window. */
18250 if (max_x <= 0)
18251 max_x = it->last_visible_x;
18252 else
18253 max_x = min (max_x, it->last_visible_x);
18255 /* Skip over display elements that are not visible. because IT->w is
18256 hscrolled. */
18257 if (it->current_x < it->first_visible_x)
18258 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18259 MOVE_TO_POS | MOVE_TO_X);
18261 row->ascent = it->max_ascent;
18262 row->height = it->max_ascent + it->max_descent;
18263 row->phys_ascent = it->max_phys_ascent;
18264 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18265 row->extra_line_spacing = it->max_extra_line_spacing;
18267 /* This condition is for the case that we are called with current_x
18268 past last_visible_x. */
18269 while (it->current_x < max_x)
18271 int x_before, x, n_glyphs_before, i, nglyphs;
18273 /* Get the next display element. */
18274 if (!get_next_display_element (it))
18275 break;
18277 /* Produce glyphs. */
18278 x_before = it->current_x;
18279 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18280 PRODUCE_GLYPHS (it);
18282 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18283 i = 0;
18284 x = x_before;
18285 while (i < nglyphs)
18287 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18289 if (!it->truncate_lines_p
18290 && x + glyph->pixel_width > max_x)
18292 /* End of continued line or max_x reached. */
18293 if (CHAR_GLYPH_PADDING_P (*glyph))
18295 /* A wide character is unbreakable. */
18296 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18297 it->current_x = x_before;
18299 else
18301 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18302 it->current_x = x;
18304 break;
18306 else if (x + glyph->pixel_width > it->first_visible_x)
18308 /* Glyph is at least partially visible. */
18309 ++it->hpos;
18310 if (x < it->first_visible_x)
18311 it->glyph_row->x = x - it->first_visible_x;
18313 else
18315 /* Glyph is off the left margin of the display area.
18316 Should not happen. */
18317 abort ();
18320 row->ascent = max (row->ascent, it->max_ascent);
18321 row->height = max (row->height, it->max_ascent + it->max_descent);
18322 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18323 row->phys_height = max (row->phys_height,
18324 it->max_phys_ascent + it->max_phys_descent);
18325 row->extra_line_spacing = max (row->extra_line_spacing,
18326 it->max_extra_line_spacing);
18327 x += glyph->pixel_width;
18328 ++i;
18331 /* Stop if max_x reached. */
18332 if (i < nglyphs)
18333 break;
18335 /* Stop at line ends. */
18336 if (ITERATOR_AT_END_OF_LINE_P (it))
18338 it->continuation_lines_width = 0;
18339 break;
18342 set_iterator_to_next (it, 1);
18344 /* Stop if truncating at the right edge. */
18345 if (it->truncate_lines_p
18346 && it->current_x >= it->last_visible_x)
18348 /* Add truncation mark, but don't do it if the line is
18349 truncated at a padding space. */
18350 if (IT_CHARPOS (*it) < it->string_nchars)
18352 if (!FRAME_WINDOW_P (it->f))
18354 int i, n;
18356 if (it->current_x > it->last_visible_x)
18358 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18359 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18360 break;
18361 for (n = row->used[TEXT_AREA]; i < n; ++i)
18363 row->used[TEXT_AREA] = i;
18364 produce_special_glyphs (it, IT_TRUNCATION);
18367 produce_special_glyphs (it, IT_TRUNCATION);
18369 it->glyph_row->truncated_on_right_p = 1;
18371 break;
18375 /* Maybe insert a truncation at the left. */
18376 if (it->first_visible_x
18377 && IT_CHARPOS (*it) > 0)
18379 if (!FRAME_WINDOW_P (it->f))
18380 insert_left_trunc_glyphs (it);
18381 it->glyph_row->truncated_on_left_p = 1;
18384 it->face_id = saved_face_id;
18386 /* Value is number of columns displayed. */
18387 return it->hpos - hpos_at_start;
18392 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18393 appears as an element of LIST or as the car of an element of LIST.
18394 If PROPVAL is a list, compare each element against LIST in that
18395 way, and return 1/2 if any element of PROPVAL is found in LIST.
18396 Otherwise return 0. This function cannot quit.
18397 The return value is 2 if the text is invisible but with an ellipsis
18398 and 1 if it's invisible and without an ellipsis. */
18401 invisible_p (propval, list)
18402 register Lisp_Object propval;
18403 Lisp_Object list;
18405 register Lisp_Object tail, proptail;
18407 for (tail = list; CONSP (tail); tail = XCDR (tail))
18409 register Lisp_Object tem;
18410 tem = XCAR (tail);
18411 if (EQ (propval, tem))
18412 return 1;
18413 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18414 return NILP (XCDR (tem)) ? 1 : 2;
18417 if (CONSP (propval))
18419 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18421 Lisp_Object propelt;
18422 propelt = XCAR (proptail);
18423 for (tail = list; CONSP (tail); tail = XCDR (tail))
18425 register Lisp_Object tem;
18426 tem = XCAR (tail);
18427 if (EQ (propelt, tem))
18428 return 1;
18429 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18430 return NILP (XCDR (tem)) ? 1 : 2;
18435 return 0;
18438 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18439 doc: /* Non-nil if the property makes the text invisible.
18440 POS-OR-PROP can be a marker or number, in which case it is taken to be
18441 a position in the current buffer and the value of the `invisible' property
18442 is checked; or it can be some other value, which is then presumed to be the
18443 value of the `invisible' property of the text of interest.
18444 The non-nil value returned can be t for truly invisible text or something
18445 else if the text is replaced by an ellipsis. */)
18446 (pos_or_prop)
18447 Lisp_Object pos_or_prop;
18449 Lisp_Object prop
18450 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18451 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18452 : pos_or_prop);
18453 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18454 return (invis == 0 ? Qnil
18455 : invis == 1 ? Qt
18456 : make_number (invis));
18459 /* Calculate a width or height in pixels from a specification using
18460 the following elements:
18462 SPEC ::=
18463 NUM - a (fractional) multiple of the default font width/height
18464 (NUM) - specifies exactly NUM pixels
18465 UNIT - a fixed number of pixels, see below.
18466 ELEMENT - size of a display element in pixels, see below.
18467 (NUM . SPEC) - equals NUM * SPEC
18468 (+ SPEC SPEC ...) - add pixel values
18469 (- SPEC SPEC ...) - subtract pixel values
18470 (- SPEC) - negate pixel value
18472 NUM ::=
18473 INT or FLOAT - a number constant
18474 SYMBOL - use symbol's (buffer local) variable binding.
18476 UNIT ::=
18477 in - pixels per inch *)
18478 mm - pixels per 1/1000 meter *)
18479 cm - pixels per 1/100 meter *)
18480 width - width of current font in pixels.
18481 height - height of current font in pixels.
18483 *) using the ratio(s) defined in display-pixels-per-inch.
18485 ELEMENT ::=
18487 left-fringe - left fringe width in pixels
18488 right-fringe - right fringe width in pixels
18490 left-margin - left margin width in pixels
18491 right-margin - right margin width in pixels
18493 scroll-bar - scroll-bar area width in pixels
18495 Examples:
18497 Pixels corresponding to 5 inches:
18498 (5 . in)
18500 Total width of non-text areas on left side of window (if scroll-bar is on left):
18501 '(space :width (+ left-fringe left-margin scroll-bar))
18503 Align to first text column (in header line):
18504 '(space :align-to 0)
18506 Align to middle of text area minus half the width of variable `my-image'
18507 containing a loaded image:
18508 '(space :align-to (0.5 . (- text my-image)))
18510 Width of left margin minus width of 1 character in the default font:
18511 '(space :width (- left-margin 1))
18513 Width of left margin minus width of 2 characters in the current font:
18514 '(space :width (- left-margin (2 . width)))
18516 Center 1 character over left-margin (in header line):
18517 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18519 Different ways to express width of left fringe plus left margin minus one pixel:
18520 '(space :width (- (+ left-fringe left-margin) (1)))
18521 '(space :width (+ left-fringe left-margin (- (1))))
18522 '(space :width (+ left-fringe left-margin (-1)))
18526 #define NUMVAL(X) \
18527 ((INTEGERP (X) || FLOATP (X)) \
18528 ? XFLOATINT (X) \
18529 : - 1)
18532 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18533 double *res;
18534 struct it *it;
18535 Lisp_Object prop;
18536 void *font;
18537 int width_p, *align_to;
18539 double pixels;
18541 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18542 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18544 if (NILP (prop))
18545 return OK_PIXELS (0);
18547 if (SYMBOLP (prop))
18549 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18551 char *unit = SDATA (SYMBOL_NAME (prop));
18553 if (unit[0] == 'i' && unit[1] == 'n')
18554 pixels = 1.0;
18555 else if (unit[0] == 'm' && unit[1] == 'm')
18556 pixels = 25.4;
18557 else if (unit[0] == 'c' && unit[1] == 'm')
18558 pixels = 2.54;
18559 else
18560 pixels = 0;
18561 if (pixels > 0)
18563 double ppi;
18564 #ifdef HAVE_WINDOW_SYSTEM
18565 if (FRAME_WINDOW_P (it->f)
18566 && (ppi = (width_p
18567 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18568 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18569 ppi > 0))
18570 return OK_PIXELS (ppi / pixels);
18571 #endif
18573 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18574 || (CONSP (Vdisplay_pixels_per_inch)
18575 && (ppi = (width_p
18576 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18577 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18578 ppi > 0)))
18579 return OK_PIXELS (ppi / pixels);
18581 return 0;
18585 #ifdef HAVE_WINDOW_SYSTEM
18586 if (EQ (prop, Qheight))
18587 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18588 if (EQ (prop, Qwidth))
18589 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18590 #else
18591 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18592 return OK_PIXELS (1);
18593 #endif
18595 if (EQ (prop, Qtext))
18596 return OK_PIXELS (width_p
18597 ? window_box_width (it->w, TEXT_AREA)
18598 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18600 if (align_to && *align_to < 0)
18602 *res = 0;
18603 if (EQ (prop, Qleft))
18604 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18605 if (EQ (prop, Qright))
18606 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18607 if (EQ (prop, Qcenter))
18608 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18609 + window_box_width (it->w, TEXT_AREA) / 2);
18610 if (EQ (prop, Qleft_fringe))
18611 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18612 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18613 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18614 if (EQ (prop, Qright_fringe))
18615 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18616 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18617 : window_box_right_offset (it->w, TEXT_AREA));
18618 if (EQ (prop, Qleft_margin))
18619 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18620 if (EQ (prop, Qright_margin))
18621 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18622 if (EQ (prop, Qscroll_bar))
18623 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18625 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18626 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18627 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18628 : 0)));
18630 else
18632 if (EQ (prop, Qleft_fringe))
18633 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18634 if (EQ (prop, Qright_fringe))
18635 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18636 if (EQ (prop, Qleft_margin))
18637 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18638 if (EQ (prop, Qright_margin))
18639 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18640 if (EQ (prop, Qscroll_bar))
18641 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18644 prop = Fbuffer_local_value (prop, it->w->buffer);
18647 if (INTEGERP (prop) || FLOATP (prop))
18649 int base_unit = (width_p
18650 ? FRAME_COLUMN_WIDTH (it->f)
18651 : FRAME_LINE_HEIGHT (it->f));
18652 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18655 if (CONSP (prop))
18657 Lisp_Object car = XCAR (prop);
18658 Lisp_Object cdr = XCDR (prop);
18660 if (SYMBOLP (car))
18662 #ifdef HAVE_WINDOW_SYSTEM
18663 if (valid_image_p (prop))
18665 int id = lookup_image (it->f, prop);
18666 struct image *img = IMAGE_FROM_ID (it->f, id);
18668 return OK_PIXELS (width_p ? img->width : img->height);
18670 #endif
18671 if (EQ (car, Qplus) || EQ (car, Qminus))
18673 int first = 1;
18674 double px;
18676 pixels = 0;
18677 while (CONSP (cdr))
18679 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18680 font, width_p, align_to))
18681 return 0;
18682 if (first)
18683 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18684 else
18685 pixels += px;
18686 cdr = XCDR (cdr);
18688 if (EQ (car, Qminus))
18689 pixels = -pixels;
18690 return OK_PIXELS (pixels);
18693 car = Fbuffer_local_value (car, it->w->buffer);
18696 if (INTEGERP (car) || FLOATP (car))
18698 double fact;
18699 pixels = XFLOATINT (car);
18700 if (NILP (cdr))
18701 return OK_PIXELS (pixels);
18702 if (calc_pixel_width_or_height (&fact, it, cdr,
18703 font, width_p, align_to))
18704 return OK_PIXELS (pixels * fact);
18705 return 0;
18708 return 0;
18711 return 0;
18715 /***********************************************************************
18716 Glyph Display
18717 ***********************************************************************/
18719 #ifdef HAVE_WINDOW_SYSTEM
18721 #if GLYPH_DEBUG
18723 void
18724 dump_glyph_string (s)
18725 struct glyph_string *s;
18727 fprintf (stderr, "glyph string\n");
18728 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18729 s->x, s->y, s->width, s->height);
18730 fprintf (stderr, " ybase = %d\n", s->ybase);
18731 fprintf (stderr, " hl = %d\n", s->hl);
18732 fprintf (stderr, " left overhang = %d, right = %d\n",
18733 s->left_overhang, s->right_overhang);
18734 fprintf (stderr, " nchars = %d\n", s->nchars);
18735 fprintf (stderr, " extends to end of line = %d\n",
18736 s->extends_to_end_of_line_p);
18737 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18738 fprintf (stderr, " bg width = %d\n", s->background_width);
18741 #endif /* GLYPH_DEBUG */
18743 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18744 of XChar2b structures for S; it can't be allocated in
18745 init_glyph_string because it must be allocated via `alloca'. W
18746 is the window on which S is drawn. ROW and AREA are the glyph row
18747 and area within the row from which S is constructed. START is the
18748 index of the first glyph structure covered by S. HL is a
18749 face-override for drawing S. */
18751 #ifdef HAVE_NTGUI
18752 #define OPTIONAL_HDC(hdc) hdc,
18753 #define DECLARE_HDC(hdc) HDC hdc;
18754 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18755 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18756 #endif
18758 #ifndef OPTIONAL_HDC
18759 #define OPTIONAL_HDC(hdc)
18760 #define DECLARE_HDC(hdc)
18761 #define ALLOCATE_HDC(hdc, f)
18762 #define RELEASE_HDC(hdc, f)
18763 #endif
18765 static void
18766 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18767 struct glyph_string *s;
18768 DECLARE_HDC (hdc)
18769 XChar2b *char2b;
18770 struct window *w;
18771 struct glyph_row *row;
18772 enum glyph_row_area area;
18773 int start;
18774 enum draw_glyphs_face hl;
18776 bzero (s, sizeof *s);
18777 s->w = w;
18778 s->f = XFRAME (w->frame);
18779 #ifdef HAVE_NTGUI
18780 s->hdc = hdc;
18781 #endif
18782 s->display = FRAME_X_DISPLAY (s->f);
18783 s->window = FRAME_X_WINDOW (s->f);
18784 s->char2b = char2b;
18785 s->hl = hl;
18786 s->row = row;
18787 s->area = area;
18788 s->first_glyph = row->glyphs[area] + start;
18789 s->height = row->height;
18790 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18792 /* Display the internal border below the tool-bar window. */
18793 if (WINDOWP (s->f->tool_bar_window)
18794 && s->w == XWINDOW (s->f->tool_bar_window))
18795 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18797 s->ybase = s->y + row->ascent;
18801 /* Append the list of glyph strings with head H and tail T to the list
18802 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18804 static INLINE void
18805 append_glyph_string_lists (head, tail, h, t)
18806 struct glyph_string **head, **tail;
18807 struct glyph_string *h, *t;
18809 if (h)
18811 if (*head)
18812 (*tail)->next = h;
18813 else
18814 *head = h;
18815 h->prev = *tail;
18816 *tail = t;
18821 /* Prepend the list of glyph strings with head H and tail T to the
18822 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18823 result. */
18825 static INLINE void
18826 prepend_glyph_string_lists (head, tail, h, t)
18827 struct glyph_string **head, **tail;
18828 struct glyph_string *h, *t;
18830 if (h)
18832 if (*head)
18833 (*head)->prev = t;
18834 else
18835 *tail = t;
18836 t->next = *head;
18837 *head = h;
18842 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18843 Set *HEAD and *TAIL to the resulting list. */
18845 static INLINE void
18846 append_glyph_string (head, tail, s)
18847 struct glyph_string **head, **tail;
18848 struct glyph_string *s;
18850 s->next = s->prev = NULL;
18851 append_glyph_string_lists (head, tail, s, s);
18855 /* Get face and two-byte form of character glyph GLYPH on frame F.
18856 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18857 a pointer to a realized face that is ready for display. */
18859 static INLINE struct face *
18860 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18861 struct frame *f;
18862 struct glyph *glyph;
18863 XChar2b *char2b;
18864 int *two_byte_p;
18866 struct face *face;
18868 xassert (glyph->type == CHAR_GLYPH);
18869 face = FACE_FROM_ID (f, glyph->face_id);
18871 if (two_byte_p)
18872 *two_byte_p = 0;
18874 if (!glyph->multibyte_p)
18876 /* Unibyte case. We don't have to encode, but we have to make
18877 sure to use a face suitable for unibyte. */
18878 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18880 else if (glyph->u.ch < 128)
18882 /* Case of ASCII in a face known to fit ASCII. */
18883 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18885 else
18887 int c1, c2, charset;
18889 /* Split characters into bytes. If c2 is -1 afterwards, C is
18890 really a one-byte character so that byte1 is zero. */
18891 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18892 if (c2 > 0)
18893 STORE_XCHAR2B (char2b, c1, c2);
18894 else
18895 STORE_XCHAR2B (char2b, 0, c1);
18897 /* Maybe encode the character in *CHAR2B. */
18898 if (charset != CHARSET_ASCII)
18900 struct font_info *font_info
18901 = FONT_INFO_FROM_ID (f, face->font_info_id);
18902 if (font_info)
18903 glyph->font_type
18904 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18908 /* Make sure X resources of the face are allocated. */
18909 xassert (face != NULL);
18910 PREPARE_FACE_FOR_DISPLAY (f, face);
18911 return face;
18915 /* Fill glyph string S with composition components specified by S->cmp.
18917 FACES is an array of faces for all components of this composition.
18918 S->gidx is the index of the first component for S.
18920 OVERLAPS non-zero means S should draw the foreground only, and use
18921 its physical height for clipping. See also draw_glyphs.
18923 Value is the index of a component not in S. */
18925 static int
18926 fill_composite_glyph_string (s, faces, overlaps)
18927 struct glyph_string *s;
18928 struct face **faces;
18929 int overlaps;
18931 int i;
18933 xassert (s);
18935 s->for_overlaps = overlaps;
18937 s->face = faces[s->gidx];
18938 s->font = s->face->font;
18939 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18941 /* For all glyphs of this composition, starting at the offset
18942 S->gidx, until we reach the end of the definition or encounter a
18943 glyph that requires the different face, add it to S. */
18944 ++s->nchars;
18945 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18946 ++s->nchars;
18948 /* All glyph strings for the same composition has the same width,
18949 i.e. the width set for the first component of the composition. */
18951 s->width = s->first_glyph->pixel_width;
18953 /* If the specified font could not be loaded, use the frame's
18954 default font, but record the fact that we couldn't load it in
18955 the glyph string so that we can draw rectangles for the
18956 characters of the glyph string. */
18957 if (s->font == NULL)
18959 s->font_not_found_p = 1;
18960 s->font = FRAME_FONT (s->f);
18963 /* Adjust base line for subscript/superscript text. */
18964 s->ybase += s->first_glyph->voffset;
18966 xassert (s->face && s->face->gc);
18968 /* This glyph string must always be drawn with 16-bit functions. */
18969 s->two_byte_p = 1;
18971 return s->gidx + s->nchars;
18975 /* Fill glyph string S from a sequence of character glyphs.
18977 FACE_ID is the face id of the string. START is the index of the
18978 first glyph to consider, END is the index of the last + 1.
18979 OVERLAPS non-zero means S should draw the foreground only, and use
18980 its physical height for clipping. See also draw_glyphs.
18982 Value is the index of the first glyph not in S. */
18984 static int
18985 fill_glyph_string (s, face_id, start, end, overlaps)
18986 struct glyph_string *s;
18987 int face_id;
18988 int start, end, overlaps;
18990 struct glyph *glyph, *last;
18991 int voffset;
18992 int glyph_not_available_p;
18994 xassert (s->f == XFRAME (s->w->frame));
18995 xassert (s->nchars == 0);
18996 xassert (start >= 0 && end > start);
18998 s->for_overlaps = overlaps,
18999 glyph = s->row->glyphs[s->area] + start;
19000 last = s->row->glyphs[s->area] + end;
19001 voffset = glyph->voffset;
19003 glyph_not_available_p = glyph->glyph_not_available_p;
19005 while (glyph < last
19006 && glyph->type == CHAR_GLYPH
19007 && glyph->voffset == voffset
19008 /* Same face id implies same font, nowadays. */
19009 && glyph->face_id == face_id
19010 && glyph->glyph_not_available_p == glyph_not_available_p)
19012 int two_byte_p;
19014 s->face = get_glyph_face_and_encoding (s->f, glyph,
19015 s->char2b + s->nchars,
19016 &two_byte_p);
19017 s->two_byte_p = two_byte_p;
19018 ++s->nchars;
19019 xassert (s->nchars <= end - start);
19020 s->width += glyph->pixel_width;
19021 ++glyph;
19024 s->font = s->face->font;
19025 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19027 /* If the specified font could not be loaded, use the frame's font,
19028 but record the fact that we couldn't load it in
19029 S->font_not_found_p so that we can draw rectangles for the
19030 characters of the glyph string. */
19031 if (s->font == NULL || glyph_not_available_p)
19033 s->font_not_found_p = 1;
19034 s->font = FRAME_FONT (s->f);
19037 /* Adjust base line for subscript/superscript text. */
19038 s->ybase += voffset;
19040 xassert (s->face && s->face->gc);
19041 return glyph - s->row->glyphs[s->area];
19045 /* Fill glyph string S from image glyph S->first_glyph. */
19047 static void
19048 fill_image_glyph_string (s)
19049 struct glyph_string *s;
19051 xassert (s->first_glyph->type == IMAGE_GLYPH);
19052 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19053 xassert (s->img);
19054 s->slice = s->first_glyph->slice;
19055 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19056 s->font = s->face->font;
19057 s->width = s->first_glyph->pixel_width;
19059 /* Adjust base line for subscript/superscript text. */
19060 s->ybase += s->first_glyph->voffset;
19064 /* Fill glyph string S from a sequence of stretch glyphs.
19066 ROW is the glyph row in which the glyphs are found, AREA is the
19067 area within the row. START is the index of the first glyph to
19068 consider, END is the index of the last + 1.
19070 Value is the index of the first glyph not in S. */
19072 static int
19073 fill_stretch_glyph_string (s, row, area, start, end)
19074 struct glyph_string *s;
19075 struct glyph_row *row;
19076 enum glyph_row_area area;
19077 int start, end;
19079 struct glyph *glyph, *last;
19080 int voffset, face_id;
19082 xassert (s->first_glyph->type == STRETCH_GLYPH);
19084 glyph = s->row->glyphs[s->area] + start;
19085 last = s->row->glyphs[s->area] + end;
19086 face_id = glyph->face_id;
19087 s->face = FACE_FROM_ID (s->f, face_id);
19088 s->font = s->face->font;
19089 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19090 s->width = glyph->pixel_width;
19091 s->nchars = 1;
19092 voffset = glyph->voffset;
19094 for (++glyph;
19095 (glyph < last
19096 && glyph->type == STRETCH_GLYPH
19097 && glyph->voffset == voffset
19098 && glyph->face_id == face_id);
19099 ++glyph)
19100 s->width += glyph->pixel_width;
19102 /* Adjust base line for subscript/superscript text. */
19103 s->ybase += voffset;
19105 /* The case that face->gc == 0 is handled when drawing the glyph
19106 string by calling PREPARE_FACE_FOR_DISPLAY. */
19107 xassert (s->face);
19108 return glyph - s->row->glyphs[s->area];
19112 /* EXPORT for RIF:
19113 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19114 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19115 assumed to be zero. */
19117 void
19118 x_get_glyph_overhangs (glyph, f, left, right)
19119 struct glyph *glyph;
19120 struct frame *f;
19121 int *left, *right;
19123 *left = *right = 0;
19125 if (glyph->type == CHAR_GLYPH)
19127 XFontStruct *font;
19128 struct face *face;
19129 struct font_info *font_info;
19130 XChar2b char2b;
19131 XCharStruct *pcm;
19133 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19134 font = face->font;
19135 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19136 if (font /* ++KFS: Should this be font_info ? */
19137 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19139 if (pcm->rbearing > pcm->width)
19140 *right = pcm->rbearing - pcm->width;
19141 if (pcm->lbearing < 0)
19142 *left = -pcm->lbearing;
19148 /* Return the index of the first glyph preceding glyph string S that
19149 is overwritten by S because of S's left overhang. Value is -1
19150 if no glyphs are overwritten. */
19152 static int
19153 left_overwritten (s)
19154 struct glyph_string *s;
19156 int k;
19158 if (s->left_overhang)
19160 int x = 0, i;
19161 struct glyph *glyphs = s->row->glyphs[s->area];
19162 int first = s->first_glyph - glyphs;
19164 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19165 x -= glyphs[i].pixel_width;
19167 k = i + 1;
19169 else
19170 k = -1;
19172 return k;
19176 /* Return the index of the first glyph preceding glyph string S that
19177 is overwriting S because of its right overhang. Value is -1 if no
19178 glyph in front of S overwrites S. */
19180 static int
19181 left_overwriting (s)
19182 struct glyph_string *s;
19184 int i, k, x;
19185 struct glyph *glyphs = s->row->glyphs[s->area];
19186 int first = s->first_glyph - glyphs;
19188 k = -1;
19189 x = 0;
19190 for (i = first - 1; i >= 0; --i)
19192 int left, right;
19193 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19194 if (x + right > 0)
19195 k = i;
19196 x -= glyphs[i].pixel_width;
19199 return k;
19203 /* Return the index of the last glyph following glyph string S that is
19204 not overwritten by S because of S's right overhang. Value is -1 if
19205 no such glyph is found. */
19207 static int
19208 right_overwritten (s)
19209 struct glyph_string *s;
19211 int k = -1;
19213 if (s->right_overhang)
19215 int x = 0, i;
19216 struct glyph *glyphs = s->row->glyphs[s->area];
19217 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19218 int end = s->row->used[s->area];
19220 for (i = first; i < end && s->right_overhang > x; ++i)
19221 x += glyphs[i].pixel_width;
19223 k = i;
19226 return k;
19230 /* Return the index of the last glyph following glyph string S that
19231 overwrites S because of its left overhang. Value is negative
19232 if no such glyph is found. */
19234 static int
19235 right_overwriting (s)
19236 struct glyph_string *s;
19238 int i, k, x;
19239 int end = s->row->used[s->area];
19240 struct glyph *glyphs = s->row->glyphs[s->area];
19241 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19243 k = -1;
19244 x = 0;
19245 for (i = first; i < end; ++i)
19247 int left, right;
19248 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19249 if (x - left < 0)
19250 k = i;
19251 x += glyphs[i].pixel_width;
19254 return k;
19258 /* Get face and two-byte form of character C in face FACE_ID on frame
19259 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19260 means we want to display multibyte text. DISPLAY_P non-zero means
19261 make sure that X resources for the face returned are allocated.
19262 Value is a pointer to a realized face that is ready for display if
19263 DISPLAY_P is non-zero. */
19265 static INLINE struct face *
19266 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19267 struct frame *f;
19268 int c, face_id;
19269 XChar2b *char2b;
19270 int multibyte_p, display_p;
19272 struct face *face = FACE_FROM_ID (f, face_id);
19274 if (!multibyte_p)
19276 /* Unibyte case. We don't have to encode, but we have to make
19277 sure to use a face suitable for unibyte. */
19278 STORE_XCHAR2B (char2b, 0, c);
19279 face_id = FACE_FOR_CHAR (f, face, c);
19280 face = FACE_FROM_ID (f, face_id);
19282 else if (c < 128)
19284 /* Case of ASCII in a face known to fit ASCII. */
19285 STORE_XCHAR2B (char2b, 0, c);
19287 else
19289 int c1, c2, charset;
19291 /* Split characters into bytes. If c2 is -1 afterwards, C is
19292 really a one-byte character so that byte1 is zero. */
19293 SPLIT_CHAR (c, charset, c1, c2);
19294 if (c2 > 0)
19295 STORE_XCHAR2B (char2b, c1, c2);
19296 else
19297 STORE_XCHAR2B (char2b, 0, c1);
19299 /* Maybe encode the character in *CHAR2B. */
19300 if (face->font != NULL)
19302 struct font_info *font_info
19303 = FONT_INFO_FROM_ID (f, face->font_info_id);
19304 if (font_info)
19305 rif->encode_char (c, char2b, font_info, 0);
19309 /* Make sure X resources of the face are allocated. */
19310 #ifdef HAVE_X_WINDOWS
19311 if (display_p)
19312 #endif
19314 xassert (face != NULL);
19315 PREPARE_FACE_FOR_DISPLAY (f, face);
19318 return face;
19322 /* Set background width of glyph string S. START is the index of the
19323 first glyph following S. LAST_X is the right-most x-position + 1
19324 in the drawing area. */
19326 static INLINE void
19327 set_glyph_string_background_width (s, start, last_x)
19328 struct glyph_string *s;
19329 int start;
19330 int last_x;
19332 /* If the face of this glyph string has to be drawn to the end of
19333 the drawing area, set S->extends_to_end_of_line_p. */
19335 if (start == s->row->used[s->area]
19336 && s->area == TEXT_AREA
19337 && ((s->row->fill_line_p
19338 && (s->hl == DRAW_NORMAL_TEXT
19339 || s->hl == DRAW_IMAGE_RAISED
19340 || s->hl == DRAW_IMAGE_SUNKEN))
19341 || s->hl == DRAW_MOUSE_FACE))
19342 s->extends_to_end_of_line_p = 1;
19344 /* If S extends its face to the end of the line, set its
19345 background_width to the distance to the right edge of the drawing
19346 area. */
19347 if (s->extends_to_end_of_line_p)
19348 s->background_width = last_x - s->x + 1;
19349 else
19350 s->background_width = s->width;
19354 /* Compute overhangs and x-positions for glyph string S and its
19355 predecessors, or successors. X is the starting x-position for S.
19356 BACKWARD_P non-zero means process predecessors. */
19358 static void
19359 compute_overhangs_and_x (s, x, backward_p)
19360 struct glyph_string *s;
19361 int x;
19362 int backward_p;
19364 if (backward_p)
19366 while (s)
19368 if (rif->compute_glyph_string_overhangs)
19369 rif->compute_glyph_string_overhangs (s);
19370 x -= s->width;
19371 s->x = x;
19372 s = s->prev;
19375 else
19377 while (s)
19379 if (rif->compute_glyph_string_overhangs)
19380 rif->compute_glyph_string_overhangs (s);
19381 s->x = x;
19382 x += s->width;
19383 s = s->next;
19390 /* The following macros are only called from draw_glyphs below.
19391 They reference the following parameters of that function directly:
19392 `w', `row', `area', and `overlap_p'
19393 as well as the following local variables:
19394 `s', `f', and `hdc' (in W32) */
19396 #ifdef HAVE_NTGUI
19397 /* On W32, silently add local `hdc' variable to argument list of
19398 init_glyph_string. */
19399 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19400 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19401 #else
19402 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19403 init_glyph_string (s, char2b, w, row, area, start, hl)
19404 #endif
19406 /* Add a glyph string for a stretch glyph to the list of strings
19407 between HEAD and TAIL. START is the index of the stretch glyph in
19408 row area AREA of glyph row ROW. END is the index of the last glyph
19409 in that glyph row area. X is the current output position assigned
19410 to the new glyph string constructed. HL overrides that face of the
19411 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19412 is the right-most x-position of the drawing area. */
19414 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19415 and below -- keep them on one line. */
19416 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19417 do \
19419 s = (struct glyph_string *) alloca (sizeof *s); \
19420 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19421 START = fill_stretch_glyph_string (s, row, area, START, END); \
19422 append_glyph_string (&HEAD, &TAIL, s); \
19423 s->x = (X); \
19425 while (0)
19428 /* Add a glyph string for an image glyph to the list of strings
19429 between HEAD and TAIL. START is the index of the image glyph in
19430 row area AREA of glyph row ROW. END is the index of the last glyph
19431 in that glyph row area. X is the current output position assigned
19432 to the new glyph string constructed. HL overrides that face of the
19433 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19434 is the right-most x-position of the drawing area. */
19436 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19437 do \
19439 s = (struct glyph_string *) alloca (sizeof *s); \
19440 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19441 fill_image_glyph_string (s); \
19442 append_glyph_string (&HEAD, &TAIL, s); \
19443 ++START; \
19444 s->x = (X); \
19446 while (0)
19449 /* Add a glyph string for a sequence of character glyphs to the list
19450 of strings between HEAD and TAIL. START is the index of the first
19451 glyph in row area AREA of glyph row ROW that is part of the new
19452 glyph string. END is the index of the last glyph in that glyph row
19453 area. X is the current output position assigned to the new glyph
19454 string constructed. HL overrides that face of the glyph; e.g. it
19455 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19456 right-most x-position of the drawing area. */
19458 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19459 do \
19461 int c, face_id; \
19462 XChar2b *char2b; \
19464 c = (row)->glyphs[area][START].u.ch; \
19465 face_id = (row)->glyphs[area][START].face_id; \
19467 s = (struct glyph_string *) alloca (sizeof *s); \
19468 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19469 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19470 append_glyph_string (&HEAD, &TAIL, s); \
19471 s->x = (X); \
19472 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19474 while (0)
19477 /* Add a glyph string for a composite sequence to the list of strings
19478 between HEAD and TAIL. START is the index of the first glyph in
19479 row area AREA of glyph row ROW that is part of the new glyph
19480 string. END is the index of the last glyph in that glyph row area.
19481 X is the current output position assigned to the new glyph string
19482 constructed. HL overrides that face of the glyph; e.g. it is
19483 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19484 x-position of the drawing area. */
19486 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19487 do { \
19488 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19489 int face_id = (row)->glyphs[area][START].face_id; \
19490 struct face *base_face = FACE_FROM_ID (f, face_id); \
19491 struct composition *cmp = composition_table[cmp_id]; \
19492 int glyph_len = cmp->glyph_len; \
19493 XChar2b *char2b; \
19494 struct face **faces; \
19495 struct glyph_string *first_s = NULL; \
19496 int n; \
19498 base_face = base_face->ascii_face; \
19499 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19500 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19501 /* At first, fill in `char2b' and `faces'. */ \
19502 for (n = 0; n < glyph_len; n++) \
19504 int c = COMPOSITION_GLYPH (cmp, n); \
19505 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19506 faces[n] = FACE_FROM_ID (f, this_face_id); \
19507 get_char_face_and_encoding (f, c, this_face_id, \
19508 char2b + n, 1, 1); \
19511 /* Make glyph_strings for each glyph sequence that is drawable by \
19512 the same face, and append them to HEAD/TAIL. */ \
19513 for (n = 0; n < cmp->glyph_len;) \
19515 s = (struct glyph_string *) alloca (sizeof *s); \
19516 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19517 append_glyph_string (&(HEAD), &(TAIL), s); \
19518 s->cmp = cmp; \
19519 s->gidx = n; \
19520 s->x = (X); \
19522 if (n == 0) \
19523 first_s = s; \
19525 n = fill_composite_glyph_string (s, faces, overlaps); \
19528 ++START; \
19529 s = first_s; \
19530 } while (0)
19533 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19534 of AREA of glyph row ROW on window W between indices START and END.
19535 HL overrides the face for drawing glyph strings, e.g. it is
19536 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19537 x-positions of the drawing area.
19539 This is an ugly monster macro construct because we must use alloca
19540 to allocate glyph strings (because draw_glyphs can be called
19541 asynchronously). */
19543 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19544 do \
19546 HEAD = TAIL = NULL; \
19547 while (START < END) \
19549 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19550 switch (first_glyph->type) \
19552 case CHAR_GLYPH: \
19553 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19554 HL, X, LAST_X); \
19555 break; \
19557 case COMPOSITE_GLYPH: \
19558 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19559 HL, X, LAST_X); \
19560 break; \
19562 case STRETCH_GLYPH: \
19563 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19564 HL, X, LAST_X); \
19565 break; \
19567 case IMAGE_GLYPH: \
19568 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19569 HL, X, LAST_X); \
19570 break; \
19572 default: \
19573 abort (); \
19576 set_glyph_string_background_width (s, START, LAST_X); \
19577 (X) += s->width; \
19580 while (0)
19583 /* Draw glyphs between START and END in AREA of ROW on window W,
19584 starting at x-position X. X is relative to AREA in W. HL is a
19585 face-override with the following meaning:
19587 DRAW_NORMAL_TEXT draw normally
19588 DRAW_CURSOR draw in cursor face
19589 DRAW_MOUSE_FACE draw in mouse face.
19590 DRAW_INVERSE_VIDEO draw in mode line face
19591 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19592 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19594 If OVERLAPS is non-zero, draw only the foreground of characters and
19595 clip to the physical height of ROW. Non-zero value also defines
19596 the overlapping part to be drawn:
19598 OVERLAPS_PRED overlap with preceding rows
19599 OVERLAPS_SUCC overlap with succeeding rows
19600 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19601 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19603 Value is the x-position reached, relative to AREA of W. */
19605 static int
19606 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19607 struct window *w;
19608 int x;
19609 struct glyph_row *row;
19610 enum glyph_row_area area;
19611 int start, end;
19612 enum draw_glyphs_face hl;
19613 int overlaps;
19615 struct glyph_string *head, *tail;
19616 struct glyph_string *s;
19617 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19618 int last_x, area_width;
19619 int x_reached;
19620 int i, j;
19621 struct frame *f = XFRAME (WINDOW_FRAME (w));
19622 DECLARE_HDC (hdc);
19624 ALLOCATE_HDC (hdc, f);
19626 /* Let's rather be paranoid than getting a SEGV. */
19627 end = min (end, row->used[area]);
19628 start = max (0, start);
19629 start = min (end, start);
19631 /* Translate X to frame coordinates. Set last_x to the right
19632 end of the drawing area. */
19633 if (row->full_width_p)
19635 /* X is relative to the left edge of W, without scroll bars
19636 or fringes. */
19637 x += WINDOW_LEFT_EDGE_X (w);
19638 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19640 else
19642 int area_left = window_box_left (w, area);
19643 x += area_left;
19644 area_width = window_box_width (w, area);
19645 last_x = area_left + area_width;
19648 /* Build a doubly-linked list of glyph_string structures between
19649 head and tail from what we have to draw. Note that the macro
19650 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19651 the reason we use a separate variable `i'. */
19652 i = start;
19653 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19654 if (tail)
19655 x_reached = tail->x + tail->background_width;
19656 else
19657 x_reached = x;
19659 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19660 the row, redraw some glyphs in front or following the glyph
19661 strings built above. */
19662 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19664 int dummy_x = 0;
19665 struct glyph_string *h, *t;
19667 /* Compute overhangs for all glyph strings. */
19668 if (rif->compute_glyph_string_overhangs)
19669 for (s = head; s; s = s->next)
19670 rif->compute_glyph_string_overhangs (s);
19672 /* Prepend glyph strings for glyphs in front of the first glyph
19673 string that are overwritten because of the first glyph
19674 string's left overhang. The background of all strings
19675 prepended must be drawn because the first glyph string
19676 draws over it. */
19677 i = left_overwritten (head);
19678 if (i >= 0)
19680 j = i;
19681 BUILD_GLYPH_STRINGS (j, start, h, t,
19682 DRAW_NORMAL_TEXT, dummy_x, last_x);
19683 start = i;
19684 compute_overhangs_and_x (t, head->x, 1);
19685 prepend_glyph_string_lists (&head, &tail, h, t);
19686 clip_head = head;
19689 /* Prepend glyph strings for glyphs in front of the first glyph
19690 string that overwrite that glyph string because of their
19691 right overhang. For these strings, only the foreground must
19692 be drawn, because it draws over the glyph string at `head'.
19693 The background must not be drawn because this would overwrite
19694 right overhangs of preceding glyphs for which no glyph
19695 strings exist. */
19696 i = left_overwriting (head);
19697 if (i >= 0)
19699 clip_head = head;
19700 BUILD_GLYPH_STRINGS (i, start, h, t,
19701 DRAW_NORMAL_TEXT, dummy_x, last_x);
19702 for (s = h; s; s = s->next)
19703 s->background_filled_p = 1;
19704 compute_overhangs_and_x (t, head->x, 1);
19705 prepend_glyph_string_lists (&head, &tail, h, t);
19708 /* Append glyphs strings for glyphs following the last glyph
19709 string tail that are overwritten by tail. The background of
19710 these strings has to be drawn because tail's foreground draws
19711 over it. */
19712 i = right_overwritten (tail);
19713 if (i >= 0)
19715 BUILD_GLYPH_STRINGS (end, i, h, t,
19716 DRAW_NORMAL_TEXT, x, last_x);
19717 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19718 append_glyph_string_lists (&head, &tail, h, t);
19719 clip_tail = tail;
19722 /* Append glyph strings for glyphs following the last glyph
19723 string tail that overwrite tail. The foreground of such
19724 glyphs has to be drawn because it writes into the background
19725 of tail. The background must not be drawn because it could
19726 paint over the foreground of following glyphs. */
19727 i = right_overwriting (tail);
19728 if (i >= 0)
19730 clip_tail = tail;
19731 BUILD_GLYPH_STRINGS (end, i, h, t,
19732 DRAW_NORMAL_TEXT, x, last_x);
19733 for (s = h; s; s = s->next)
19734 s->background_filled_p = 1;
19735 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19736 append_glyph_string_lists (&head, &tail, h, t);
19738 if (clip_head || clip_tail)
19739 for (s = head; s; s = s->next)
19741 s->clip_head = clip_head;
19742 s->clip_tail = clip_tail;
19746 /* Draw all strings. */
19747 for (s = head; s; s = s->next)
19748 rif->draw_glyph_string (s);
19750 if (area == TEXT_AREA
19751 && !row->full_width_p
19752 /* When drawing overlapping rows, only the glyph strings'
19753 foreground is drawn, which doesn't erase a cursor
19754 completely. */
19755 && !overlaps)
19757 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19758 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19759 : (tail ? tail->x + tail->background_width : x));
19761 int text_left = window_box_left (w, TEXT_AREA);
19762 x0 -= text_left;
19763 x1 -= text_left;
19765 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19766 row->y, MATRIX_ROW_BOTTOM_Y (row));
19769 /* Value is the x-position up to which drawn, relative to AREA of W.
19770 This doesn't include parts drawn because of overhangs. */
19771 if (row->full_width_p)
19772 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19773 else
19774 x_reached -= window_box_left (w, area);
19776 RELEASE_HDC (hdc, f);
19778 return x_reached;
19781 /* Expand row matrix if too narrow. Don't expand if area
19782 is not present. */
19784 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19786 if (!fonts_changed_p \
19787 && (it->glyph_row->glyphs[area] \
19788 < it->glyph_row->glyphs[area + 1])) \
19790 it->w->ncols_scale_factor++; \
19791 fonts_changed_p = 1; \
19795 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19796 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19798 static INLINE void
19799 append_glyph (it)
19800 struct it *it;
19802 struct glyph *glyph;
19803 enum glyph_row_area area = it->area;
19805 xassert (it->glyph_row);
19806 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19808 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19809 if (glyph < it->glyph_row->glyphs[area + 1])
19811 glyph->charpos = CHARPOS (it->position);
19812 glyph->object = it->object;
19813 glyph->pixel_width = it->pixel_width;
19814 glyph->ascent = it->ascent;
19815 glyph->descent = it->descent;
19816 glyph->voffset = it->voffset;
19817 glyph->type = CHAR_GLYPH;
19818 glyph->multibyte_p = it->multibyte_p;
19819 glyph->left_box_line_p = it->start_of_box_run_p;
19820 glyph->right_box_line_p = it->end_of_box_run_p;
19821 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19822 || it->phys_descent > it->descent);
19823 glyph->padding_p = 0;
19824 glyph->glyph_not_available_p = it->glyph_not_available_p;
19825 glyph->face_id = it->face_id;
19826 glyph->u.ch = it->char_to_display;
19827 glyph->slice = null_glyph_slice;
19828 glyph->font_type = FONT_TYPE_UNKNOWN;
19829 ++it->glyph_row->used[area];
19831 else
19832 IT_EXPAND_MATRIX_WIDTH (it, area);
19835 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19836 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19838 static INLINE void
19839 append_composite_glyph (it)
19840 struct it *it;
19842 struct glyph *glyph;
19843 enum glyph_row_area area = it->area;
19845 xassert (it->glyph_row);
19847 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19848 if (glyph < it->glyph_row->glyphs[area + 1])
19850 glyph->charpos = CHARPOS (it->position);
19851 glyph->object = it->object;
19852 glyph->pixel_width = it->pixel_width;
19853 glyph->ascent = it->ascent;
19854 glyph->descent = it->descent;
19855 glyph->voffset = it->voffset;
19856 glyph->type = COMPOSITE_GLYPH;
19857 glyph->multibyte_p = it->multibyte_p;
19858 glyph->left_box_line_p = it->start_of_box_run_p;
19859 glyph->right_box_line_p = it->end_of_box_run_p;
19860 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19861 || it->phys_descent > it->descent);
19862 glyph->padding_p = 0;
19863 glyph->glyph_not_available_p = 0;
19864 glyph->face_id = it->face_id;
19865 glyph->u.cmp_id = it->cmp_id;
19866 glyph->slice = null_glyph_slice;
19867 glyph->font_type = FONT_TYPE_UNKNOWN;
19868 ++it->glyph_row->used[area];
19870 else
19871 IT_EXPAND_MATRIX_WIDTH (it, area);
19875 /* Change IT->ascent and IT->height according to the setting of
19876 IT->voffset. */
19878 static INLINE void
19879 take_vertical_position_into_account (it)
19880 struct it *it;
19882 if (it->voffset)
19884 if (it->voffset < 0)
19885 /* Increase the ascent so that we can display the text higher
19886 in the line. */
19887 it->ascent -= it->voffset;
19888 else
19889 /* Increase the descent so that we can display the text lower
19890 in the line. */
19891 it->descent += it->voffset;
19896 /* Produce glyphs/get display metrics for the image IT is loaded with.
19897 See the description of struct display_iterator in dispextern.h for
19898 an overview of struct display_iterator. */
19900 static void
19901 produce_image_glyph (it)
19902 struct it *it;
19904 struct image *img;
19905 struct face *face;
19906 int glyph_ascent, crop;
19907 struct glyph_slice slice;
19909 xassert (it->what == IT_IMAGE);
19911 face = FACE_FROM_ID (it->f, it->face_id);
19912 xassert (face);
19913 /* Make sure X resources of the face is loaded. */
19914 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19916 if (it->image_id < 0)
19918 /* Fringe bitmap. */
19919 it->ascent = it->phys_ascent = 0;
19920 it->descent = it->phys_descent = 0;
19921 it->pixel_width = 0;
19922 it->nglyphs = 0;
19923 return;
19926 img = IMAGE_FROM_ID (it->f, it->image_id);
19927 xassert (img);
19928 /* Make sure X resources of the image is loaded. */
19929 prepare_image_for_display (it->f, img);
19931 slice.x = slice.y = 0;
19932 slice.width = img->width;
19933 slice.height = img->height;
19935 if (INTEGERP (it->slice.x))
19936 slice.x = XINT (it->slice.x);
19937 else if (FLOATP (it->slice.x))
19938 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19940 if (INTEGERP (it->slice.y))
19941 slice.y = XINT (it->slice.y);
19942 else if (FLOATP (it->slice.y))
19943 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19945 if (INTEGERP (it->slice.width))
19946 slice.width = XINT (it->slice.width);
19947 else if (FLOATP (it->slice.width))
19948 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19950 if (INTEGERP (it->slice.height))
19951 slice.height = XINT (it->slice.height);
19952 else if (FLOATP (it->slice.height))
19953 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19955 if (slice.x >= img->width)
19956 slice.x = img->width;
19957 if (slice.y >= img->height)
19958 slice.y = img->height;
19959 if (slice.x + slice.width >= img->width)
19960 slice.width = img->width - slice.x;
19961 if (slice.y + slice.height > img->height)
19962 slice.height = img->height - slice.y;
19964 if (slice.width == 0 || slice.height == 0)
19965 return;
19967 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19969 it->descent = slice.height - glyph_ascent;
19970 if (slice.y == 0)
19971 it->descent += img->vmargin;
19972 if (slice.y + slice.height == img->height)
19973 it->descent += img->vmargin;
19974 it->phys_descent = it->descent;
19976 it->pixel_width = slice.width;
19977 if (slice.x == 0)
19978 it->pixel_width += img->hmargin;
19979 if (slice.x + slice.width == img->width)
19980 it->pixel_width += img->hmargin;
19982 /* It's quite possible for images to have an ascent greater than
19983 their height, so don't get confused in that case. */
19984 if (it->descent < 0)
19985 it->descent = 0;
19987 #if 0 /* this breaks image tiling */
19988 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19989 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19990 if (face_ascent > it->ascent)
19991 it->ascent = it->phys_ascent = face_ascent;
19992 #endif
19994 it->nglyphs = 1;
19996 if (face->box != FACE_NO_BOX)
19998 if (face->box_line_width > 0)
20000 if (slice.y == 0)
20001 it->ascent += face->box_line_width;
20002 if (slice.y + slice.height == img->height)
20003 it->descent += face->box_line_width;
20006 if (it->start_of_box_run_p && slice.x == 0)
20007 it->pixel_width += abs (face->box_line_width);
20008 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20009 it->pixel_width += abs (face->box_line_width);
20012 take_vertical_position_into_account (it);
20014 /* Automatically crop wide image glyphs at right edge so we can
20015 draw the cursor on same display row. */
20016 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20017 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20019 it->pixel_width -= crop;
20020 slice.width -= crop;
20023 if (it->glyph_row)
20025 struct glyph *glyph;
20026 enum glyph_row_area area = it->area;
20028 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20029 if (glyph < it->glyph_row->glyphs[area + 1])
20031 glyph->charpos = CHARPOS (it->position);
20032 glyph->object = it->object;
20033 glyph->pixel_width = it->pixel_width;
20034 glyph->ascent = glyph_ascent;
20035 glyph->descent = it->descent;
20036 glyph->voffset = it->voffset;
20037 glyph->type = IMAGE_GLYPH;
20038 glyph->multibyte_p = it->multibyte_p;
20039 glyph->left_box_line_p = it->start_of_box_run_p;
20040 glyph->right_box_line_p = it->end_of_box_run_p;
20041 glyph->overlaps_vertically_p = 0;
20042 glyph->padding_p = 0;
20043 glyph->glyph_not_available_p = 0;
20044 glyph->face_id = it->face_id;
20045 glyph->u.img_id = img->id;
20046 glyph->slice = slice;
20047 glyph->font_type = FONT_TYPE_UNKNOWN;
20048 ++it->glyph_row->used[area];
20050 else
20051 IT_EXPAND_MATRIX_WIDTH (it, area);
20056 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20057 of the glyph, WIDTH and HEIGHT are the width and height of the
20058 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20060 static void
20061 append_stretch_glyph (it, object, width, height, ascent)
20062 struct it *it;
20063 Lisp_Object object;
20064 int width, height;
20065 int ascent;
20067 struct glyph *glyph;
20068 enum glyph_row_area area = it->area;
20070 xassert (ascent >= 0 && ascent <= height);
20072 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20073 if (glyph < it->glyph_row->glyphs[area + 1])
20075 glyph->charpos = CHARPOS (it->position);
20076 glyph->object = object;
20077 glyph->pixel_width = width;
20078 glyph->ascent = ascent;
20079 glyph->descent = height - ascent;
20080 glyph->voffset = it->voffset;
20081 glyph->type = STRETCH_GLYPH;
20082 glyph->multibyte_p = it->multibyte_p;
20083 glyph->left_box_line_p = it->start_of_box_run_p;
20084 glyph->right_box_line_p = it->end_of_box_run_p;
20085 glyph->overlaps_vertically_p = 0;
20086 glyph->padding_p = 0;
20087 glyph->glyph_not_available_p = 0;
20088 glyph->face_id = it->face_id;
20089 glyph->u.stretch.ascent = ascent;
20090 glyph->u.stretch.height = height;
20091 glyph->slice = null_glyph_slice;
20092 glyph->font_type = FONT_TYPE_UNKNOWN;
20093 ++it->glyph_row->used[area];
20095 else
20096 IT_EXPAND_MATRIX_WIDTH (it, area);
20100 /* Produce a stretch glyph for iterator IT. IT->object is the value
20101 of the glyph property displayed. The value must be a list
20102 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20103 being recognized:
20105 1. `:width WIDTH' specifies that the space should be WIDTH *
20106 canonical char width wide. WIDTH may be an integer or floating
20107 point number.
20109 2. `:relative-width FACTOR' specifies that the width of the stretch
20110 should be computed from the width of the first character having the
20111 `glyph' property, and should be FACTOR times that width.
20113 3. `:align-to HPOS' specifies that the space should be wide enough
20114 to reach HPOS, a value in canonical character units.
20116 Exactly one of the above pairs must be present.
20118 4. `:height HEIGHT' specifies that the height of the stretch produced
20119 should be HEIGHT, measured in canonical character units.
20121 5. `:relative-height FACTOR' specifies that the height of the
20122 stretch should be FACTOR times the height of the characters having
20123 the glyph property.
20125 Either none or exactly one of 4 or 5 must be present.
20127 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20128 of the stretch should be used for the ascent of the stretch.
20129 ASCENT must be in the range 0 <= ASCENT <= 100. */
20131 static void
20132 produce_stretch_glyph (it)
20133 struct it *it;
20135 /* (space :width WIDTH :height HEIGHT ...) */
20136 Lisp_Object prop, plist;
20137 int width = 0, height = 0, align_to = -1;
20138 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20139 int ascent = 0;
20140 double tem;
20141 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20142 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20144 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20146 /* List should start with `space'. */
20147 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20148 plist = XCDR (it->object);
20150 /* Compute the width of the stretch. */
20151 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20152 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20154 /* Absolute width `:width WIDTH' specified and valid. */
20155 zero_width_ok_p = 1;
20156 width = (int)tem;
20158 else if (prop = Fplist_get (plist, QCrelative_width),
20159 NUMVAL (prop) > 0)
20161 /* Relative width `:relative-width FACTOR' specified and valid.
20162 Compute the width of the characters having the `glyph'
20163 property. */
20164 struct it it2;
20165 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20167 it2 = *it;
20168 if (it->multibyte_p)
20170 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20171 - IT_BYTEPOS (*it));
20172 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20174 else
20175 it2.c = *p, it2.len = 1;
20177 it2.glyph_row = NULL;
20178 it2.what = IT_CHARACTER;
20179 x_produce_glyphs (&it2);
20180 width = NUMVAL (prop) * it2.pixel_width;
20182 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20183 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20185 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20186 align_to = (align_to < 0
20188 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20189 else if (align_to < 0)
20190 align_to = window_box_left_offset (it->w, TEXT_AREA);
20191 width = max (0, (int)tem + align_to - it->current_x);
20192 zero_width_ok_p = 1;
20194 else
20195 /* Nothing specified -> width defaults to canonical char width. */
20196 width = FRAME_COLUMN_WIDTH (it->f);
20198 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20199 width = 1;
20201 /* Compute height. */
20202 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20203 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20205 height = (int)tem;
20206 zero_height_ok_p = 1;
20208 else if (prop = Fplist_get (plist, QCrelative_height),
20209 NUMVAL (prop) > 0)
20210 height = FONT_HEIGHT (font) * NUMVAL (prop);
20211 else
20212 height = FONT_HEIGHT (font);
20214 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20215 height = 1;
20217 /* Compute percentage of height used for ascent. If
20218 `:ascent ASCENT' is present and valid, use that. Otherwise,
20219 derive the ascent from the font in use. */
20220 if (prop = Fplist_get (plist, QCascent),
20221 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20222 ascent = height * NUMVAL (prop) / 100.0;
20223 else if (!NILP (prop)
20224 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20225 ascent = min (max (0, (int)tem), height);
20226 else
20227 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20229 if (width > 0 && !it->truncate_lines_p
20230 && it->current_x + width > it->last_visible_x)
20231 width = it->last_visible_x - it->current_x - 1;
20233 if (width > 0 && height > 0 && it->glyph_row)
20235 Lisp_Object object = it->stack[it->sp - 1].string;
20236 if (!STRINGP (object))
20237 object = it->w->buffer;
20238 append_stretch_glyph (it, object, width, height, ascent);
20241 it->pixel_width = width;
20242 it->ascent = it->phys_ascent = ascent;
20243 it->descent = it->phys_descent = height - it->ascent;
20244 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20246 take_vertical_position_into_account (it);
20249 /* Get line-height and line-spacing property at point.
20250 If line-height has format (HEIGHT TOTAL), return TOTAL
20251 in TOTAL_HEIGHT. */
20253 static Lisp_Object
20254 get_line_height_property (it, prop)
20255 struct it *it;
20256 Lisp_Object prop;
20258 Lisp_Object position;
20260 if (STRINGP (it->object))
20261 position = make_number (IT_STRING_CHARPOS (*it));
20262 else if (BUFFERP (it->object))
20263 position = make_number (IT_CHARPOS (*it));
20264 else
20265 return Qnil;
20267 return Fget_char_property (position, prop, it->object);
20270 /* Calculate line-height and line-spacing properties.
20271 An integer value specifies explicit pixel value.
20272 A float value specifies relative value to current face height.
20273 A cons (float . face-name) specifies relative value to
20274 height of specified face font.
20276 Returns height in pixels, or nil. */
20279 static Lisp_Object
20280 calc_line_height_property (it, val, font, boff, override)
20281 struct it *it;
20282 Lisp_Object val;
20283 XFontStruct *font;
20284 int boff, override;
20286 Lisp_Object face_name = Qnil;
20287 int ascent, descent, height;
20289 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20290 return val;
20292 if (CONSP (val))
20294 face_name = XCAR (val);
20295 val = XCDR (val);
20296 if (!NUMBERP (val))
20297 val = make_number (1);
20298 if (NILP (face_name))
20300 height = it->ascent + it->descent;
20301 goto scale;
20305 if (NILP (face_name))
20307 font = FRAME_FONT (it->f);
20308 boff = FRAME_BASELINE_OFFSET (it->f);
20310 else if (EQ (face_name, Qt))
20312 override = 0;
20314 else
20316 int face_id;
20317 struct face *face;
20318 struct font_info *font_info;
20320 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20321 if (face_id < 0)
20322 return make_number (-1);
20324 face = FACE_FROM_ID (it->f, face_id);
20325 font = face->font;
20326 if (font == NULL)
20327 return make_number (-1);
20329 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20330 boff = font_info->baseline_offset;
20331 if (font_info->vertical_centering)
20332 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20335 ascent = FONT_BASE (font) + boff;
20336 descent = FONT_DESCENT (font) - boff;
20338 if (override)
20340 it->override_ascent = ascent;
20341 it->override_descent = descent;
20342 it->override_boff = boff;
20345 height = ascent + descent;
20347 scale:
20348 if (FLOATP (val))
20349 height = (int)(XFLOAT_DATA (val) * height);
20350 else if (INTEGERP (val))
20351 height *= XINT (val);
20353 return make_number (height);
20357 /* RIF:
20358 Produce glyphs/get display metrics for the display element IT is
20359 loaded with. See the description of struct it in dispextern.h
20360 for an overview of struct it. */
20362 void
20363 x_produce_glyphs (it)
20364 struct it *it;
20366 int extra_line_spacing = it->extra_line_spacing;
20368 it->glyph_not_available_p = 0;
20370 if (it->what == IT_CHARACTER)
20372 XChar2b char2b;
20373 XFontStruct *font;
20374 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20375 XCharStruct *pcm;
20376 int font_not_found_p;
20377 struct font_info *font_info;
20378 int boff; /* baseline offset */
20379 /* We may change it->multibyte_p upon unibyte<->multibyte
20380 conversion. So, save the current value now and restore it
20381 later.
20383 Note: It seems that we don't have to record multibyte_p in
20384 struct glyph because the character code itself tells if or
20385 not the character is multibyte. Thus, in the future, we must
20386 consider eliminating the field `multibyte_p' in the struct
20387 glyph. */
20388 int saved_multibyte_p = it->multibyte_p;
20390 /* Maybe translate single-byte characters to multibyte, or the
20391 other way. */
20392 it->char_to_display = it->c;
20393 if (!ASCII_BYTE_P (it->c))
20395 if (unibyte_display_via_language_environment
20396 && SINGLE_BYTE_CHAR_P (it->c)
20397 && (it->c >= 0240
20398 || !NILP (Vnonascii_translation_table)))
20400 it->char_to_display = unibyte_char_to_multibyte (it->c);
20401 it->multibyte_p = 1;
20402 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20403 face = FACE_FROM_ID (it->f, it->face_id);
20405 else if (!SINGLE_BYTE_CHAR_P (it->c)
20406 && !it->multibyte_p)
20408 it->multibyte_p = 1;
20409 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20410 face = FACE_FROM_ID (it->f, it->face_id);
20414 /* Get font to use. Encode IT->char_to_display. */
20415 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20416 &char2b, it->multibyte_p, 0);
20417 font = face->font;
20419 /* When no suitable font found, use the default font. */
20420 font_not_found_p = font == NULL;
20421 if (font_not_found_p)
20423 font = FRAME_FONT (it->f);
20424 boff = FRAME_BASELINE_OFFSET (it->f);
20425 font_info = NULL;
20427 else
20429 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20430 boff = font_info->baseline_offset;
20431 if (font_info->vertical_centering)
20432 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20435 if (it->char_to_display >= ' '
20436 && (!it->multibyte_p || it->char_to_display < 128))
20438 /* Either unibyte or ASCII. */
20439 int stretched_p;
20441 it->nglyphs = 1;
20443 pcm = rif->per_char_metric (font, &char2b,
20444 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20446 if (it->override_ascent >= 0)
20448 it->ascent = it->override_ascent;
20449 it->descent = it->override_descent;
20450 boff = it->override_boff;
20452 else
20454 it->ascent = FONT_BASE (font) + boff;
20455 it->descent = FONT_DESCENT (font) - boff;
20458 if (pcm)
20460 it->phys_ascent = pcm->ascent + boff;
20461 it->phys_descent = pcm->descent - boff;
20462 it->pixel_width = pcm->width;
20464 else
20466 it->glyph_not_available_p = 1;
20467 it->phys_ascent = it->ascent;
20468 it->phys_descent = it->descent;
20469 it->pixel_width = FONT_WIDTH (font);
20472 if (it->constrain_row_ascent_descent_p)
20474 if (it->descent > it->max_descent)
20476 it->ascent += it->descent - it->max_descent;
20477 it->descent = it->max_descent;
20479 if (it->ascent > it->max_ascent)
20481 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20482 it->ascent = it->max_ascent;
20484 it->phys_ascent = min (it->phys_ascent, it->ascent);
20485 it->phys_descent = min (it->phys_descent, it->descent);
20486 extra_line_spacing = 0;
20489 /* If this is a space inside a region of text with
20490 `space-width' property, change its width. */
20491 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20492 if (stretched_p)
20493 it->pixel_width *= XFLOATINT (it->space_width);
20495 /* If face has a box, add the box thickness to the character
20496 height. If character has a box line to the left and/or
20497 right, add the box line width to the character's width. */
20498 if (face->box != FACE_NO_BOX)
20500 int thick = face->box_line_width;
20502 if (thick > 0)
20504 it->ascent += thick;
20505 it->descent += thick;
20507 else
20508 thick = -thick;
20510 if (it->start_of_box_run_p)
20511 it->pixel_width += thick;
20512 if (it->end_of_box_run_p)
20513 it->pixel_width += thick;
20516 /* If face has an overline, add the height of the overline
20517 (1 pixel) and a 1 pixel margin to the character height. */
20518 if (face->overline_p)
20519 it->ascent += overline_margin;
20521 if (it->constrain_row_ascent_descent_p)
20523 if (it->ascent > it->max_ascent)
20524 it->ascent = it->max_ascent;
20525 if (it->descent > it->max_descent)
20526 it->descent = it->max_descent;
20529 take_vertical_position_into_account (it);
20531 /* If we have to actually produce glyphs, do it. */
20532 if (it->glyph_row)
20534 if (stretched_p)
20536 /* Translate a space with a `space-width' property
20537 into a stretch glyph. */
20538 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20539 / FONT_HEIGHT (font));
20540 append_stretch_glyph (it, it->object, it->pixel_width,
20541 it->ascent + it->descent, ascent);
20543 else
20544 append_glyph (it);
20546 /* If characters with lbearing or rbearing are displayed
20547 in this line, record that fact in a flag of the
20548 glyph row. This is used to optimize X output code. */
20549 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20550 it->glyph_row->contains_overlapping_glyphs_p = 1;
20553 else if (it->char_to_display == '\n')
20555 /* A newline has no width but we need the height of the line.
20556 But if previous part of the line set a height, don't
20557 increase that height */
20559 Lisp_Object height;
20560 Lisp_Object total_height = Qnil;
20562 it->override_ascent = -1;
20563 it->pixel_width = 0;
20564 it->nglyphs = 0;
20566 height = get_line_height_property(it, Qline_height);
20567 /* Split (line-height total-height) list */
20568 if (CONSP (height)
20569 && CONSP (XCDR (height))
20570 && NILP (XCDR (XCDR (height))))
20572 total_height = XCAR (XCDR (height));
20573 height = XCAR (height);
20575 height = calc_line_height_property(it, height, font, boff, 1);
20577 if (it->override_ascent >= 0)
20579 it->ascent = it->override_ascent;
20580 it->descent = it->override_descent;
20581 boff = it->override_boff;
20583 else
20585 it->ascent = FONT_BASE (font) + boff;
20586 it->descent = FONT_DESCENT (font) - boff;
20589 if (EQ (height, Qt))
20591 if (it->descent > it->max_descent)
20593 it->ascent += it->descent - it->max_descent;
20594 it->descent = it->max_descent;
20596 if (it->ascent > it->max_ascent)
20598 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20599 it->ascent = it->max_ascent;
20601 it->phys_ascent = min (it->phys_ascent, it->ascent);
20602 it->phys_descent = min (it->phys_descent, it->descent);
20603 it->constrain_row_ascent_descent_p = 1;
20604 extra_line_spacing = 0;
20606 else
20608 Lisp_Object spacing;
20610 it->phys_ascent = it->ascent;
20611 it->phys_descent = it->descent;
20613 if ((it->max_ascent > 0 || it->max_descent > 0)
20614 && face->box != FACE_NO_BOX
20615 && face->box_line_width > 0)
20617 it->ascent += face->box_line_width;
20618 it->descent += face->box_line_width;
20620 if (!NILP (height)
20621 && XINT (height) > it->ascent + it->descent)
20622 it->ascent = XINT (height) - it->descent;
20624 if (!NILP (total_height))
20625 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20626 else
20628 spacing = get_line_height_property(it, Qline_spacing);
20629 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20631 if (INTEGERP (spacing))
20633 extra_line_spacing = XINT (spacing);
20634 if (!NILP (total_height))
20635 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20639 else if (it->char_to_display == '\t')
20641 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20642 int x = it->current_x + it->continuation_lines_width;
20643 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20645 /* If the distance from the current position to the next tab
20646 stop is less than a space character width, use the
20647 tab stop after that. */
20648 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20649 next_tab_x += tab_width;
20651 it->pixel_width = next_tab_x - x;
20652 it->nglyphs = 1;
20653 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20654 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20656 if (it->glyph_row)
20658 append_stretch_glyph (it, it->object, it->pixel_width,
20659 it->ascent + it->descent, it->ascent);
20662 else
20664 /* A multi-byte character. Assume that the display width of the
20665 character is the width of the character multiplied by the
20666 width of the font. */
20668 /* If we found a font, this font should give us the right
20669 metrics. If we didn't find a font, use the frame's
20670 default font and calculate the width of the character
20671 from the charset width; this is what old redisplay code
20672 did. */
20674 pcm = rif->per_char_metric (font, &char2b,
20675 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20677 if (font_not_found_p || !pcm)
20679 int charset = CHAR_CHARSET (it->char_to_display);
20681 it->glyph_not_available_p = 1;
20682 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20683 * CHARSET_WIDTH (charset));
20684 it->phys_ascent = FONT_BASE (font) + boff;
20685 it->phys_descent = FONT_DESCENT (font) - boff;
20687 else
20689 it->pixel_width = pcm->width;
20690 it->phys_ascent = pcm->ascent + boff;
20691 it->phys_descent = pcm->descent - boff;
20692 if (it->glyph_row
20693 && (pcm->lbearing < 0
20694 || pcm->rbearing > pcm->width))
20695 it->glyph_row->contains_overlapping_glyphs_p = 1;
20697 it->nglyphs = 1;
20698 it->ascent = FONT_BASE (font) + boff;
20699 it->descent = FONT_DESCENT (font) - boff;
20700 if (face->box != FACE_NO_BOX)
20702 int thick = face->box_line_width;
20704 if (thick > 0)
20706 it->ascent += thick;
20707 it->descent += thick;
20709 else
20710 thick = - thick;
20712 if (it->start_of_box_run_p)
20713 it->pixel_width += thick;
20714 if (it->end_of_box_run_p)
20715 it->pixel_width += thick;
20718 /* If face has an overline, add the height of the overline
20719 (1 pixel) and a 1 pixel margin to the character height. */
20720 if (face->overline_p)
20721 it->ascent += overline_margin;
20723 take_vertical_position_into_account (it);
20725 if (it->glyph_row)
20726 append_glyph (it);
20728 it->multibyte_p = saved_multibyte_p;
20730 else if (it->what == IT_COMPOSITION)
20732 /* Note: A composition is represented as one glyph in the
20733 glyph matrix. There are no padding glyphs. */
20734 XChar2b char2b;
20735 XFontStruct *font;
20736 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20737 XCharStruct *pcm;
20738 int font_not_found_p;
20739 struct font_info *font_info;
20740 int boff; /* baseline offset */
20741 struct composition *cmp = composition_table[it->cmp_id];
20743 /* Maybe translate single-byte characters to multibyte. */
20744 it->char_to_display = it->c;
20745 if (unibyte_display_via_language_environment
20746 && SINGLE_BYTE_CHAR_P (it->c)
20747 && (it->c >= 0240
20748 || (it->c >= 0200
20749 && !NILP (Vnonascii_translation_table))))
20751 it->char_to_display = unibyte_char_to_multibyte (it->c);
20754 /* Get face and font to use. Encode IT->char_to_display. */
20755 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20756 face = FACE_FROM_ID (it->f, it->face_id);
20757 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20758 &char2b, it->multibyte_p, 0);
20759 font = face->font;
20761 /* When no suitable font found, use the default font. */
20762 font_not_found_p = font == NULL;
20763 if (font_not_found_p)
20765 font = FRAME_FONT (it->f);
20766 boff = FRAME_BASELINE_OFFSET (it->f);
20767 font_info = NULL;
20769 else
20771 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20772 boff = font_info->baseline_offset;
20773 if (font_info->vertical_centering)
20774 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20777 /* There are no padding glyphs, so there is only one glyph to
20778 produce for the composition. Important is that pixel_width,
20779 ascent and descent are the values of what is drawn by
20780 draw_glyphs (i.e. the values of the overall glyphs composed). */
20781 it->nglyphs = 1;
20783 /* If we have not yet calculated pixel size data of glyphs of
20784 the composition for the current face font, calculate them
20785 now. Theoretically, we have to check all fonts for the
20786 glyphs, but that requires much time and memory space. So,
20787 here we check only the font of the first glyph. This leads
20788 to incorrect display very rarely, and C-l (recenter) can
20789 correct the display anyway. */
20790 if (cmp->font != (void *) font)
20792 /* Ascent and descent of the font of the first character of
20793 this composition (adjusted by baseline offset). Ascent
20794 and descent of overall glyphs should not be less than
20795 them respectively. */
20796 int font_ascent = FONT_BASE (font) + boff;
20797 int font_descent = FONT_DESCENT (font) - boff;
20798 /* Bounding box of the overall glyphs. */
20799 int leftmost, rightmost, lowest, highest;
20800 int i, width, ascent, descent;
20802 cmp->font = (void *) font;
20804 /* Initialize the bounding box. */
20805 if (font_info
20806 && (pcm = rif->per_char_metric (font, &char2b,
20807 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20809 width = pcm->width;
20810 ascent = pcm->ascent;
20811 descent = pcm->descent;
20813 else
20815 width = FONT_WIDTH (font);
20816 ascent = FONT_BASE (font);
20817 descent = FONT_DESCENT (font);
20820 rightmost = width;
20821 lowest = - descent + boff;
20822 highest = ascent + boff;
20823 leftmost = 0;
20825 if (font_info
20826 && font_info->default_ascent
20827 && CHAR_TABLE_P (Vuse_default_ascent)
20828 && !NILP (Faref (Vuse_default_ascent,
20829 make_number (it->char_to_display))))
20830 highest = font_info->default_ascent + boff;
20832 /* Draw the first glyph at the normal position. It may be
20833 shifted to right later if some other glyphs are drawn at
20834 the left. */
20835 cmp->offsets[0] = 0;
20836 cmp->offsets[1] = boff;
20838 /* Set cmp->offsets for the remaining glyphs. */
20839 for (i = 1; i < cmp->glyph_len; i++)
20841 int left, right, btm, top;
20842 int ch = COMPOSITION_GLYPH (cmp, i);
20843 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20845 face = FACE_FROM_ID (it->f, face_id);
20846 get_char_face_and_encoding (it->f, ch, face->id,
20847 &char2b, it->multibyte_p, 0);
20848 font = face->font;
20849 if (font == NULL)
20851 font = FRAME_FONT (it->f);
20852 boff = FRAME_BASELINE_OFFSET (it->f);
20853 font_info = NULL;
20855 else
20857 font_info
20858 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20859 boff = font_info->baseline_offset;
20860 if (font_info->vertical_centering)
20861 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20864 if (font_info
20865 && (pcm = rif->per_char_metric (font, &char2b,
20866 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20868 width = pcm->width;
20869 ascent = pcm->ascent;
20870 descent = pcm->descent;
20872 else
20874 width = FONT_WIDTH (font);
20875 ascent = 1;
20876 descent = 0;
20879 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20881 /* Relative composition with or without
20882 alternate chars. */
20883 left = (leftmost + rightmost - width) / 2;
20884 btm = - descent + boff;
20885 if (font_info && font_info->relative_compose
20886 && (! CHAR_TABLE_P (Vignore_relative_composition)
20887 || NILP (Faref (Vignore_relative_composition,
20888 make_number (ch)))))
20891 if (- descent >= font_info->relative_compose)
20892 /* One extra pixel between two glyphs. */
20893 btm = highest + 1;
20894 else if (ascent <= 0)
20895 /* One extra pixel between two glyphs. */
20896 btm = lowest - 1 - ascent - descent;
20899 else
20901 /* A composition rule is specified by an integer
20902 value that encodes global and new reference
20903 points (GREF and NREF). GREF and NREF are
20904 specified by numbers as below:
20906 0---1---2 -- ascent
20910 9--10--11 -- center
20912 ---3---4---5--- baseline
20914 6---7---8 -- descent
20916 int rule = COMPOSITION_RULE (cmp, i);
20917 int gref, nref, grefx, grefy, nrefx, nrefy;
20919 COMPOSITION_DECODE_RULE (rule, gref, nref);
20920 grefx = gref % 3, nrefx = nref % 3;
20921 grefy = gref / 3, nrefy = nref / 3;
20923 left = (leftmost
20924 + grefx * (rightmost - leftmost) / 2
20925 - nrefx * width / 2);
20926 btm = ((grefy == 0 ? highest
20927 : grefy == 1 ? 0
20928 : grefy == 2 ? lowest
20929 : (highest + lowest) / 2)
20930 - (nrefy == 0 ? ascent + descent
20931 : nrefy == 1 ? descent - boff
20932 : nrefy == 2 ? 0
20933 : (ascent + descent) / 2));
20936 cmp->offsets[i * 2] = left;
20937 cmp->offsets[i * 2 + 1] = btm + descent;
20939 /* Update the bounding box of the overall glyphs. */
20940 right = left + width;
20941 top = btm + descent + ascent;
20942 if (left < leftmost)
20943 leftmost = left;
20944 if (right > rightmost)
20945 rightmost = right;
20946 if (top > highest)
20947 highest = top;
20948 if (btm < lowest)
20949 lowest = btm;
20952 /* If there are glyphs whose x-offsets are negative,
20953 shift all glyphs to the right and make all x-offsets
20954 non-negative. */
20955 if (leftmost < 0)
20957 for (i = 0; i < cmp->glyph_len; i++)
20958 cmp->offsets[i * 2] -= leftmost;
20959 rightmost -= leftmost;
20962 cmp->pixel_width = rightmost;
20963 cmp->ascent = highest;
20964 cmp->descent = - lowest;
20965 if (cmp->ascent < font_ascent)
20966 cmp->ascent = font_ascent;
20967 if (cmp->descent < font_descent)
20968 cmp->descent = font_descent;
20971 it->pixel_width = cmp->pixel_width;
20972 it->ascent = it->phys_ascent = cmp->ascent;
20973 it->descent = it->phys_descent = cmp->descent;
20975 if (face->box != FACE_NO_BOX)
20977 int thick = face->box_line_width;
20979 if (thick > 0)
20981 it->ascent += thick;
20982 it->descent += thick;
20984 else
20985 thick = - thick;
20987 if (it->start_of_box_run_p)
20988 it->pixel_width += thick;
20989 if (it->end_of_box_run_p)
20990 it->pixel_width += thick;
20993 /* If face has an overline, add the height of the overline
20994 (1 pixel) and a 1 pixel margin to the character height. */
20995 if (face->overline_p)
20996 it->ascent += overline_margin;
20998 take_vertical_position_into_account (it);
21000 if (it->glyph_row)
21001 append_composite_glyph (it);
21003 else if (it->what == IT_IMAGE)
21004 produce_image_glyph (it);
21005 else if (it->what == IT_STRETCH)
21006 produce_stretch_glyph (it);
21008 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21009 because this isn't true for images with `:ascent 100'. */
21010 xassert (it->ascent >= 0 && it->descent >= 0);
21011 if (it->area == TEXT_AREA)
21012 it->current_x += it->pixel_width;
21014 if (extra_line_spacing > 0)
21016 it->descent += extra_line_spacing;
21017 if (extra_line_spacing > it->max_extra_line_spacing)
21018 it->max_extra_line_spacing = extra_line_spacing;
21021 it->max_ascent = max (it->max_ascent, it->ascent);
21022 it->max_descent = max (it->max_descent, it->descent);
21023 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21024 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21027 /* EXPORT for RIF:
21028 Output LEN glyphs starting at START at the nominal cursor position.
21029 Advance the nominal cursor over the text. The global variable
21030 updated_window contains the window being updated, updated_row is
21031 the glyph row being updated, and updated_area is the area of that
21032 row being updated. */
21034 void
21035 x_write_glyphs (start, len)
21036 struct glyph *start;
21037 int len;
21039 int x, hpos;
21041 xassert (updated_window && updated_row);
21042 BLOCK_INPUT;
21044 /* Write glyphs. */
21046 hpos = start - updated_row->glyphs[updated_area];
21047 x = draw_glyphs (updated_window, output_cursor.x,
21048 updated_row, updated_area,
21049 hpos, hpos + len,
21050 DRAW_NORMAL_TEXT, 0);
21052 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21053 if (updated_area == TEXT_AREA
21054 && updated_window->phys_cursor_on_p
21055 && updated_window->phys_cursor.vpos == output_cursor.vpos
21056 && updated_window->phys_cursor.hpos >= hpos
21057 && updated_window->phys_cursor.hpos < hpos + len)
21058 updated_window->phys_cursor_on_p = 0;
21060 UNBLOCK_INPUT;
21062 /* Advance the output cursor. */
21063 output_cursor.hpos += len;
21064 output_cursor.x = x;
21068 /* EXPORT for RIF:
21069 Insert LEN glyphs from START at the nominal cursor position. */
21071 void
21072 x_insert_glyphs (start, len)
21073 struct glyph *start;
21074 int len;
21076 struct frame *f;
21077 struct window *w;
21078 int line_height, shift_by_width, shifted_region_width;
21079 struct glyph_row *row;
21080 struct glyph *glyph;
21081 int frame_x, frame_y, hpos;
21083 xassert (updated_window && updated_row);
21084 BLOCK_INPUT;
21085 w = updated_window;
21086 f = XFRAME (WINDOW_FRAME (w));
21088 /* Get the height of the line we are in. */
21089 row = updated_row;
21090 line_height = row->height;
21092 /* Get the width of the glyphs to insert. */
21093 shift_by_width = 0;
21094 for (glyph = start; glyph < start + len; ++glyph)
21095 shift_by_width += glyph->pixel_width;
21097 /* Get the width of the region to shift right. */
21098 shifted_region_width = (window_box_width (w, updated_area)
21099 - output_cursor.x
21100 - shift_by_width);
21102 /* Shift right. */
21103 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21104 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21106 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21107 line_height, shift_by_width);
21109 /* Write the glyphs. */
21110 hpos = start - row->glyphs[updated_area];
21111 draw_glyphs (w, output_cursor.x, row, updated_area,
21112 hpos, hpos + len,
21113 DRAW_NORMAL_TEXT, 0);
21115 /* Advance the output cursor. */
21116 output_cursor.hpos += len;
21117 output_cursor.x += shift_by_width;
21118 UNBLOCK_INPUT;
21122 /* EXPORT for RIF:
21123 Erase the current text line from the nominal cursor position
21124 (inclusive) to pixel column TO_X (exclusive). The idea is that
21125 everything from TO_X onward is already erased.
21127 TO_X is a pixel position relative to updated_area of
21128 updated_window. TO_X == -1 means clear to the end of this area. */
21130 void
21131 x_clear_end_of_line (to_x)
21132 int to_x;
21134 struct frame *f;
21135 struct window *w = updated_window;
21136 int max_x, min_y, max_y;
21137 int from_x, from_y, to_y;
21139 xassert (updated_window && updated_row);
21140 f = XFRAME (w->frame);
21142 if (updated_row->full_width_p)
21143 max_x = WINDOW_TOTAL_WIDTH (w);
21144 else
21145 max_x = window_box_width (w, updated_area);
21146 max_y = window_text_bottom_y (w);
21148 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21149 of window. For TO_X > 0, truncate to end of drawing area. */
21150 if (to_x == 0)
21151 return;
21152 else if (to_x < 0)
21153 to_x = max_x;
21154 else
21155 to_x = min (to_x, max_x);
21157 to_y = min (max_y, output_cursor.y + updated_row->height);
21159 /* Notice if the cursor will be cleared by this operation. */
21160 if (!updated_row->full_width_p)
21161 notice_overwritten_cursor (w, updated_area,
21162 output_cursor.x, -1,
21163 updated_row->y,
21164 MATRIX_ROW_BOTTOM_Y (updated_row));
21166 from_x = output_cursor.x;
21168 /* Translate to frame coordinates. */
21169 if (updated_row->full_width_p)
21171 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21172 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21174 else
21176 int area_left = window_box_left (w, updated_area);
21177 from_x += area_left;
21178 to_x += area_left;
21181 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21182 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21183 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21185 /* Prevent inadvertently clearing to end of the X window. */
21186 if (to_x > from_x && to_y > from_y)
21188 BLOCK_INPUT;
21189 rif->clear_frame_area (f, from_x, from_y,
21190 to_x - from_x, to_y - from_y);
21191 UNBLOCK_INPUT;
21195 #endif /* HAVE_WINDOW_SYSTEM */
21199 /***********************************************************************
21200 Cursor types
21201 ***********************************************************************/
21203 /* Value is the internal representation of the specified cursor type
21204 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21205 of the bar cursor. */
21207 static enum text_cursor_kinds
21208 get_specified_cursor_type (arg, width)
21209 Lisp_Object arg;
21210 int *width;
21212 enum text_cursor_kinds type;
21214 if (NILP (arg))
21215 return NO_CURSOR;
21217 if (EQ (arg, Qbox))
21218 return FILLED_BOX_CURSOR;
21220 if (EQ (arg, Qhollow))
21221 return HOLLOW_BOX_CURSOR;
21223 if (EQ (arg, Qbar))
21225 *width = 2;
21226 return BAR_CURSOR;
21229 if (CONSP (arg)
21230 && EQ (XCAR (arg), Qbar)
21231 && INTEGERP (XCDR (arg))
21232 && XINT (XCDR (arg)) >= 0)
21234 *width = XINT (XCDR (arg));
21235 return BAR_CURSOR;
21238 if (EQ (arg, Qhbar))
21240 *width = 2;
21241 return HBAR_CURSOR;
21244 if (CONSP (arg)
21245 && EQ (XCAR (arg), Qhbar)
21246 && INTEGERP (XCDR (arg))
21247 && XINT (XCDR (arg)) >= 0)
21249 *width = XINT (XCDR (arg));
21250 return HBAR_CURSOR;
21253 /* Treat anything unknown as "hollow box cursor".
21254 It was bad to signal an error; people have trouble fixing
21255 .Xdefaults with Emacs, when it has something bad in it. */
21256 type = HOLLOW_BOX_CURSOR;
21258 return type;
21261 /* Set the default cursor types for specified frame. */
21262 void
21263 set_frame_cursor_types (f, arg)
21264 struct frame *f;
21265 Lisp_Object arg;
21267 int width;
21268 Lisp_Object tem;
21270 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21271 FRAME_CURSOR_WIDTH (f) = width;
21273 /* By default, set up the blink-off state depending on the on-state. */
21275 tem = Fassoc (arg, Vblink_cursor_alist);
21276 if (!NILP (tem))
21278 FRAME_BLINK_OFF_CURSOR (f)
21279 = get_specified_cursor_type (XCDR (tem), &width);
21280 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21282 else
21283 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21287 /* Return the cursor we want to be displayed in window W. Return
21288 width of bar/hbar cursor through WIDTH arg. Return with
21289 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21290 (i.e. if the `system caret' should track this cursor).
21292 In a mini-buffer window, we want the cursor only to appear if we
21293 are reading input from this window. For the selected window, we
21294 want the cursor type given by the frame parameter or buffer local
21295 setting of cursor-type. If explicitly marked off, draw no cursor.
21296 In all other cases, we want a hollow box cursor. */
21298 static enum text_cursor_kinds
21299 get_window_cursor_type (w, glyph, width, active_cursor)
21300 struct window *w;
21301 struct glyph *glyph;
21302 int *width;
21303 int *active_cursor;
21305 struct frame *f = XFRAME (w->frame);
21306 struct buffer *b = XBUFFER (w->buffer);
21307 int cursor_type = DEFAULT_CURSOR;
21308 Lisp_Object alt_cursor;
21309 int non_selected = 0;
21311 *active_cursor = 1;
21313 /* Echo area */
21314 if (cursor_in_echo_area
21315 && FRAME_HAS_MINIBUF_P (f)
21316 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21318 if (w == XWINDOW (echo_area_window))
21320 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21322 *width = FRAME_CURSOR_WIDTH (f);
21323 return FRAME_DESIRED_CURSOR (f);
21325 else
21326 return get_specified_cursor_type (b->cursor_type, width);
21329 *active_cursor = 0;
21330 non_selected = 1;
21333 /* Nonselected window or nonselected frame. */
21334 else if (w != XWINDOW (f->selected_window)
21335 #ifdef HAVE_WINDOW_SYSTEM
21336 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21337 #endif
21340 *active_cursor = 0;
21342 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21343 return NO_CURSOR;
21345 non_selected = 1;
21348 /* Never display a cursor in a window in which cursor-type is nil. */
21349 if (NILP (b->cursor_type))
21350 return NO_CURSOR;
21352 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21353 if (non_selected)
21355 alt_cursor = b->cursor_in_non_selected_windows;
21356 return get_specified_cursor_type (alt_cursor, width);
21359 /* Get the normal cursor type for this window. */
21360 if (EQ (b->cursor_type, Qt))
21362 cursor_type = FRAME_DESIRED_CURSOR (f);
21363 *width = FRAME_CURSOR_WIDTH (f);
21365 else
21366 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21368 /* Use normal cursor if not blinked off. */
21369 if (!w->cursor_off_p)
21371 #ifdef HAVE_WINDOW_SYSTEM
21372 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21374 if (cursor_type == FILLED_BOX_CURSOR)
21376 /* Using a block cursor on large images can be very annoying.
21377 So use a hollow cursor for "large" images.
21378 If image is not transparent (no mask), also use hollow cursor. */
21379 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21380 if (img != NULL && IMAGEP (img->spec))
21382 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21383 where N = size of default frame font size.
21384 This should cover most of the "tiny" icons people may use. */
21385 if (!img->mask
21386 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21387 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21388 cursor_type = HOLLOW_BOX_CURSOR;
21391 else if (cursor_type != NO_CURSOR)
21393 /* Display current only supports BOX and HOLLOW cursors for images.
21394 So for now, unconditionally use a HOLLOW cursor when cursor is
21395 not a solid box cursor. */
21396 cursor_type = HOLLOW_BOX_CURSOR;
21399 #endif
21400 return cursor_type;
21403 /* Cursor is blinked off, so determine how to "toggle" it. */
21405 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21406 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21407 return get_specified_cursor_type (XCDR (alt_cursor), width);
21409 /* Then see if frame has specified a specific blink off cursor type. */
21410 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21412 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21413 return FRAME_BLINK_OFF_CURSOR (f);
21416 #if 0
21417 /* Some people liked having a permanently visible blinking cursor,
21418 while others had very strong opinions against it. So it was
21419 decided to remove it. KFS 2003-09-03 */
21421 /* Finally perform built-in cursor blinking:
21422 filled box <-> hollow box
21423 wide [h]bar <-> narrow [h]bar
21424 narrow [h]bar <-> no cursor
21425 other type <-> no cursor */
21427 if (cursor_type == FILLED_BOX_CURSOR)
21428 return HOLLOW_BOX_CURSOR;
21430 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21432 *width = 1;
21433 return cursor_type;
21435 #endif
21437 return NO_CURSOR;
21441 #ifdef HAVE_WINDOW_SYSTEM
21443 /* Notice when the text cursor of window W has been completely
21444 overwritten by a drawing operation that outputs glyphs in AREA
21445 starting at X0 and ending at X1 in the line starting at Y0 and
21446 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21447 the rest of the line after X0 has been written. Y coordinates
21448 are window-relative. */
21450 static void
21451 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21452 struct window *w;
21453 enum glyph_row_area area;
21454 int x0, y0, x1, y1;
21456 int cx0, cx1, cy0, cy1;
21457 struct glyph_row *row;
21459 if (!w->phys_cursor_on_p)
21460 return;
21461 if (area != TEXT_AREA)
21462 return;
21464 if (w->phys_cursor.vpos < 0
21465 || w->phys_cursor.vpos >= w->current_matrix->nrows
21466 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21467 !(row->enabled_p && row->displays_text_p)))
21468 return;
21470 if (row->cursor_in_fringe_p)
21472 row->cursor_in_fringe_p = 0;
21473 draw_fringe_bitmap (w, row, 0);
21474 w->phys_cursor_on_p = 0;
21475 return;
21478 cx0 = w->phys_cursor.x;
21479 cx1 = cx0 + w->phys_cursor_width;
21480 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21481 return;
21483 /* The cursor image will be completely removed from the
21484 screen if the output area intersects the cursor area in
21485 y-direction. When we draw in [y0 y1[, and some part of
21486 the cursor is at y < y0, that part must have been drawn
21487 before. When scrolling, the cursor is erased before
21488 actually scrolling, so we don't come here. When not
21489 scrolling, the rows above the old cursor row must have
21490 changed, and in this case these rows must have written
21491 over the cursor image.
21493 Likewise if part of the cursor is below y1, with the
21494 exception of the cursor being in the first blank row at
21495 the buffer and window end because update_text_area
21496 doesn't draw that row. (Except when it does, but
21497 that's handled in update_text_area.) */
21499 cy0 = w->phys_cursor.y;
21500 cy1 = cy0 + w->phys_cursor_height;
21501 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21502 return;
21504 w->phys_cursor_on_p = 0;
21507 #endif /* HAVE_WINDOW_SYSTEM */
21510 /************************************************************************
21511 Mouse Face
21512 ************************************************************************/
21514 #ifdef HAVE_WINDOW_SYSTEM
21516 /* EXPORT for RIF:
21517 Fix the display of area AREA of overlapping row ROW in window W
21518 with respect to the overlapping part OVERLAPS. */
21520 void
21521 x_fix_overlapping_area (w, row, area, overlaps)
21522 struct window *w;
21523 struct glyph_row *row;
21524 enum glyph_row_area area;
21525 int overlaps;
21527 int i, x;
21529 BLOCK_INPUT;
21531 x = 0;
21532 for (i = 0; i < row->used[area];)
21534 if (row->glyphs[area][i].overlaps_vertically_p)
21536 int start = i, start_x = x;
21540 x += row->glyphs[area][i].pixel_width;
21541 ++i;
21543 while (i < row->used[area]
21544 && row->glyphs[area][i].overlaps_vertically_p);
21546 draw_glyphs (w, start_x, row, area,
21547 start, i,
21548 DRAW_NORMAL_TEXT, overlaps);
21550 else
21552 x += row->glyphs[area][i].pixel_width;
21553 ++i;
21557 UNBLOCK_INPUT;
21561 /* EXPORT:
21562 Draw the cursor glyph of window W in glyph row ROW. See the
21563 comment of draw_glyphs for the meaning of HL. */
21565 void
21566 draw_phys_cursor_glyph (w, row, hl)
21567 struct window *w;
21568 struct glyph_row *row;
21569 enum draw_glyphs_face hl;
21571 /* If cursor hpos is out of bounds, don't draw garbage. This can
21572 happen in mini-buffer windows when switching between echo area
21573 glyphs and mini-buffer. */
21574 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21576 int on_p = w->phys_cursor_on_p;
21577 int x1;
21578 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21579 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21580 hl, 0);
21581 w->phys_cursor_on_p = on_p;
21583 if (hl == DRAW_CURSOR)
21584 w->phys_cursor_width = x1 - w->phys_cursor.x;
21585 /* When we erase the cursor, and ROW is overlapped by other
21586 rows, make sure that these overlapping parts of other rows
21587 are redrawn. */
21588 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21590 w->phys_cursor_width = x1 - w->phys_cursor.x;
21592 if (row > w->current_matrix->rows
21593 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21594 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21595 OVERLAPS_ERASED_CURSOR);
21597 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21598 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21599 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21600 OVERLAPS_ERASED_CURSOR);
21606 /* EXPORT:
21607 Erase the image of a cursor of window W from the screen. */
21609 void
21610 erase_phys_cursor (w)
21611 struct window *w;
21613 struct frame *f = XFRAME (w->frame);
21614 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21615 int hpos = w->phys_cursor.hpos;
21616 int vpos = w->phys_cursor.vpos;
21617 int mouse_face_here_p = 0;
21618 struct glyph_matrix *active_glyphs = w->current_matrix;
21619 struct glyph_row *cursor_row;
21620 struct glyph *cursor_glyph;
21621 enum draw_glyphs_face hl;
21623 /* No cursor displayed or row invalidated => nothing to do on the
21624 screen. */
21625 if (w->phys_cursor_type == NO_CURSOR)
21626 goto mark_cursor_off;
21628 /* VPOS >= active_glyphs->nrows means that window has been resized.
21629 Don't bother to erase the cursor. */
21630 if (vpos >= active_glyphs->nrows)
21631 goto mark_cursor_off;
21633 /* If row containing cursor is marked invalid, there is nothing we
21634 can do. */
21635 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21636 if (!cursor_row->enabled_p)
21637 goto mark_cursor_off;
21639 /* If line spacing is > 0, old cursor may only be partially visible in
21640 window after split-window. So adjust visible height. */
21641 cursor_row->visible_height = min (cursor_row->visible_height,
21642 window_text_bottom_y (w) - cursor_row->y);
21644 /* If row is completely invisible, don't attempt to delete a cursor which
21645 isn't there. This can happen if cursor is at top of a window, and
21646 we switch to a buffer with a header line in that window. */
21647 if (cursor_row->visible_height <= 0)
21648 goto mark_cursor_off;
21650 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21651 if (cursor_row->cursor_in_fringe_p)
21653 cursor_row->cursor_in_fringe_p = 0;
21654 draw_fringe_bitmap (w, cursor_row, 0);
21655 goto mark_cursor_off;
21658 /* This can happen when the new row is shorter than the old one.
21659 In this case, either draw_glyphs or clear_end_of_line
21660 should have cleared the cursor. Note that we wouldn't be
21661 able to erase the cursor in this case because we don't have a
21662 cursor glyph at hand. */
21663 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21664 goto mark_cursor_off;
21666 /* If the cursor is in the mouse face area, redisplay that when
21667 we clear the cursor. */
21668 if (! NILP (dpyinfo->mouse_face_window)
21669 && w == XWINDOW (dpyinfo->mouse_face_window)
21670 && (vpos > dpyinfo->mouse_face_beg_row
21671 || (vpos == dpyinfo->mouse_face_beg_row
21672 && hpos >= dpyinfo->mouse_face_beg_col))
21673 && (vpos < dpyinfo->mouse_face_end_row
21674 || (vpos == dpyinfo->mouse_face_end_row
21675 && hpos < dpyinfo->mouse_face_end_col))
21676 /* Don't redraw the cursor's spot in mouse face if it is at the
21677 end of a line (on a newline). The cursor appears there, but
21678 mouse highlighting does not. */
21679 && cursor_row->used[TEXT_AREA] > hpos)
21680 mouse_face_here_p = 1;
21682 /* Maybe clear the display under the cursor. */
21683 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21685 int x, y, left_x;
21686 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21687 int width;
21689 cursor_glyph = get_phys_cursor_glyph (w);
21690 if (cursor_glyph == NULL)
21691 goto mark_cursor_off;
21693 width = cursor_glyph->pixel_width;
21694 left_x = window_box_left_offset (w, TEXT_AREA);
21695 x = w->phys_cursor.x;
21696 if (x < left_x)
21697 width -= left_x - x;
21698 width = min (width, window_box_width (w, TEXT_AREA) - x);
21699 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21700 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21702 if (width > 0)
21703 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21706 /* Erase the cursor by redrawing the character underneath it. */
21707 if (mouse_face_here_p)
21708 hl = DRAW_MOUSE_FACE;
21709 else
21710 hl = DRAW_NORMAL_TEXT;
21711 draw_phys_cursor_glyph (w, cursor_row, hl);
21713 mark_cursor_off:
21714 w->phys_cursor_on_p = 0;
21715 w->phys_cursor_type = NO_CURSOR;
21719 /* EXPORT:
21720 Display or clear cursor of window W. If ON is zero, clear the
21721 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21722 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21724 void
21725 display_and_set_cursor (w, on, hpos, vpos, x, y)
21726 struct window *w;
21727 int on, hpos, vpos, x, y;
21729 struct frame *f = XFRAME (w->frame);
21730 int new_cursor_type;
21731 int new_cursor_width;
21732 int active_cursor;
21733 struct glyph_row *glyph_row;
21734 struct glyph *glyph;
21736 /* This is pointless on invisible frames, and dangerous on garbaged
21737 windows and frames; in the latter case, the frame or window may
21738 be in the midst of changing its size, and x and y may be off the
21739 window. */
21740 if (! FRAME_VISIBLE_P (f)
21741 || FRAME_GARBAGED_P (f)
21742 || vpos >= w->current_matrix->nrows
21743 || hpos >= w->current_matrix->matrix_w)
21744 return;
21746 /* If cursor is off and we want it off, return quickly. */
21747 if (!on && !w->phys_cursor_on_p)
21748 return;
21750 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21751 /* If cursor row is not enabled, we don't really know where to
21752 display the cursor. */
21753 if (!glyph_row->enabled_p)
21755 w->phys_cursor_on_p = 0;
21756 return;
21759 glyph = NULL;
21760 if (!glyph_row->exact_window_width_line_p
21761 || hpos < glyph_row->used[TEXT_AREA])
21762 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21764 xassert (interrupt_input_blocked);
21766 /* Set new_cursor_type to the cursor we want to be displayed. */
21767 new_cursor_type = get_window_cursor_type (w, glyph,
21768 &new_cursor_width, &active_cursor);
21770 /* If cursor is currently being shown and we don't want it to be or
21771 it is in the wrong place, or the cursor type is not what we want,
21772 erase it. */
21773 if (w->phys_cursor_on_p
21774 && (!on
21775 || w->phys_cursor.x != x
21776 || w->phys_cursor.y != y
21777 || new_cursor_type != w->phys_cursor_type
21778 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21779 && new_cursor_width != w->phys_cursor_width)))
21780 erase_phys_cursor (w);
21782 /* Don't check phys_cursor_on_p here because that flag is only set
21783 to zero in some cases where we know that the cursor has been
21784 completely erased, to avoid the extra work of erasing the cursor
21785 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21786 still not be visible, or it has only been partly erased. */
21787 if (on)
21789 w->phys_cursor_ascent = glyph_row->ascent;
21790 w->phys_cursor_height = glyph_row->height;
21792 /* Set phys_cursor_.* before x_draw_.* is called because some
21793 of them may need the information. */
21794 w->phys_cursor.x = x;
21795 w->phys_cursor.y = glyph_row->y;
21796 w->phys_cursor.hpos = hpos;
21797 w->phys_cursor.vpos = vpos;
21800 rif->draw_window_cursor (w, glyph_row, x, y,
21801 new_cursor_type, new_cursor_width,
21802 on, active_cursor);
21806 /* Switch the display of W's cursor on or off, according to the value
21807 of ON. */
21809 static void
21810 update_window_cursor (w, on)
21811 struct window *w;
21812 int on;
21814 /* Don't update cursor in windows whose frame is in the process
21815 of being deleted. */
21816 if (w->current_matrix)
21818 BLOCK_INPUT;
21819 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21820 w->phys_cursor.x, w->phys_cursor.y);
21821 UNBLOCK_INPUT;
21826 /* Call update_window_cursor with parameter ON_P on all leaf windows
21827 in the window tree rooted at W. */
21829 static void
21830 update_cursor_in_window_tree (w, on_p)
21831 struct window *w;
21832 int on_p;
21834 while (w)
21836 if (!NILP (w->hchild))
21837 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21838 else if (!NILP (w->vchild))
21839 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21840 else
21841 update_window_cursor (w, on_p);
21843 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21848 /* EXPORT:
21849 Display the cursor on window W, or clear it, according to ON_P.
21850 Don't change the cursor's position. */
21852 void
21853 x_update_cursor (f, on_p)
21854 struct frame *f;
21855 int on_p;
21857 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21861 /* EXPORT:
21862 Clear the cursor of window W to background color, and mark the
21863 cursor as not shown. This is used when the text where the cursor
21864 is is about to be rewritten. */
21866 void
21867 x_clear_cursor (w)
21868 struct window *w;
21870 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21871 update_window_cursor (w, 0);
21875 /* EXPORT:
21876 Display the active region described by mouse_face_* according to DRAW. */
21878 void
21879 show_mouse_face (dpyinfo, draw)
21880 Display_Info *dpyinfo;
21881 enum draw_glyphs_face draw;
21883 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21884 struct frame *f = XFRAME (WINDOW_FRAME (w));
21886 if (/* If window is in the process of being destroyed, don't bother
21887 to do anything. */
21888 w->current_matrix != NULL
21889 /* Don't update mouse highlight if hidden */
21890 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21891 /* Recognize when we are called to operate on rows that don't exist
21892 anymore. This can happen when a window is split. */
21893 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21895 int phys_cursor_on_p = w->phys_cursor_on_p;
21896 struct glyph_row *row, *first, *last;
21898 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21899 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21901 for (row = first; row <= last && row->enabled_p; ++row)
21903 int start_hpos, end_hpos, start_x;
21905 /* For all but the first row, the highlight starts at column 0. */
21906 if (row == first)
21908 start_hpos = dpyinfo->mouse_face_beg_col;
21909 start_x = dpyinfo->mouse_face_beg_x;
21911 else
21913 start_hpos = 0;
21914 start_x = 0;
21917 if (row == last)
21918 end_hpos = dpyinfo->mouse_face_end_col;
21919 else
21921 end_hpos = row->used[TEXT_AREA];
21922 if (draw == DRAW_NORMAL_TEXT)
21923 row->fill_line_p = 1; /* Clear to end of line */
21926 if (end_hpos > start_hpos)
21928 draw_glyphs (w, start_x, row, TEXT_AREA,
21929 start_hpos, end_hpos,
21930 draw, 0);
21932 row->mouse_face_p
21933 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21937 /* When we've written over the cursor, arrange for it to
21938 be displayed again. */
21939 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21941 BLOCK_INPUT;
21942 display_and_set_cursor (w, 1,
21943 w->phys_cursor.hpos, w->phys_cursor.vpos,
21944 w->phys_cursor.x, w->phys_cursor.y);
21945 UNBLOCK_INPUT;
21949 /* Change the mouse cursor. */
21950 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
21951 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21952 else if (draw == DRAW_MOUSE_FACE)
21953 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21954 else
21955 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21958 /* EXPORT:
21959 Clear out the mouse-highlighted active region.
21960 Redraw it un-highlighted first. Value is non-zero if mouse
21961 face was actually drawn unhighlighted. */
21964 clear_mouse_face (dpyinfo)
21965 Display_Info *dpyinfo;
21967 int cleared = 0;
21969 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21971 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21972 cleared = 1;
21975 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21976 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21977 dpyinfo->mouse_face_window = Qnil;
21978 dpyinfo->mouse_face_overlay = Qnil;
21979 return cleared;
21983 /* EXPORT:
21984 Non-zero if physical cursor of window W is within mouse face. */
21987 cursor_in_mouse_face_p (w)
21988 struct window *w;
21990 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21991 int in_mouse_face = 0;
21993 if (WINDOWP (dpyinfo->mouse_face_window)
21994 && XWINDOW (dpyinfo->mouse_face_window) == w)
21996 int hpos = w->phys_cursor.hpos;
21997 int vpos = w->phys_cursor.vpos;
21999 if (vpos >= dpyinfo->mouse_face_beg_row
22000 && vpos <= dpyinfo->mouse_face_end_row
22001 && (vpos > dpyinfo->mouse_face_beg_row
22002 || hpos >= dpyinfo->mouse_face_beg_col)
22003 && (vpos < dpyinfo->mouse_face_end_row
22004 || hpos < dpyinfo->mouse_face_end_col
22005 || dpyinfo->mouse_face_past_end))
22006 in_mouse_face = 1;
22009 return in_mouse_face;
22015 /* Find the glyph matrix position of buffer position CHARPOS in window
22016 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22017 current glyphs must be up to date. If CHARPOS is above window
22018 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22019 of last line in W. In the row containing CHARPOS, stop before glyphs
22020 having STOP as object. */
22022 #if 1 /* This is a version of fast_find_position that's more correct
22023 in the presence of hscrolling, for example. I didn't install
22024 it right away because the problem fixed is minor, it failed
22025 in 20.x as well, and I think it's too risky to install
22026 so near the release of 21.1. 2001-09-25 gerd. */
22028 static int
22029 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22030 struct window *w;
22031 int charpos;
22032 int *hpos, *vpos, *x, *y;
22033 Lisp_Object stop;
22035 struct glyph_row *row, *first;
22036 struct glyph *glyph, *end;
22037 int past_end = 0;
22039 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22040 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22042 *x = first->x;
22043 *y = first->y;
22044 *hpos = 0;
22045 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22046 return 1;
22049 row = row_containing_pos (w, charpos, first, NULL, 0);
22050 if (row == NULL)
22052 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22053 past_end = 1;
22056 /* If whole rows or last part of a row came from a display overlay,
22057 row_containing_pos will skip over such rows because their end pos
22058 equals the start pos of the overlay or interval.
22060 Move back if we have a STOP object and previous row's
22061 end glyph came from STOP. */
22062 if (!NILP (stop))
22064 struct glyph_row *prev;
22065 while ((prev = row - 1, prev >= first)
22066 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22067 && prev->used[TEXT_AREA] > 0)
22069 struct glyph *beg = prev->glyphs[TEXT_AREA];
22070 glyph = beg + prev->used[TEXT_AREA];
22071 while (--glyph >= beg
22072 && INTEGERP (glyph->object));
22073 if (glyph < beg
22074 || !EQ (stop, glyph->object))
22075 break;
22076 row = prev;
22080 *x = row->x;
22081 *y = row->y;
22082 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22084 glyph = row->glyphs[TEXT_AREA];
22085 end = glyph + row->used[TEXT_AREA];
22087 /* Skip over glyphs not having an object at the start of the row.
22088 These are special glyphs like truncation marks on terminal
22089 frames. */
22090 if (row->displays_text_p)
22091 while (glyph < end
22092 && INTEGERP (glyph->object)
22093 && !EQ (stop, glyph->object)
22094 && glyph->charpos < 0)
22096 *x += glyph->pixel_width;
22097 ++glyph;
22100 while (glyph < end
22101 && !INTEGERP (glyph->object)
22102 && !EQ (stop, glyph->object)
22103 && (!BUFFERP (glyph->object)
22104 || glyph->charpos < charpos))
22106 *x += glyph->pixel_width;
22107 ++glyph;
22110 *hpos = glyph - row->glyphs[TEXT_AREA];
22111 return !past_end;
22114 #else /* not 1 */
22116 static int
22117 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22118 struct window *w;
22119 int pos;
22120 int *hpos, *vpos, *x, *y;
22121 Lisp_Object stop;
22123 int i;
22124 int lastcol;
22125 int maybe_next_line_p = 0;
22126 int line_start_position;
22127 int yb = window_text_bottom_y (w);
22128 struct glyph_row *row, *best_row;
22129 int row_vpos, best_row_vpos;
22130 int current_x;
22132 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22133 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22135 while (row->y < yb)
22137 if (row->used[TEXT_AREA])
22138 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22139 else
22140 line_start_position = 0;
22142 if (line_start_position > pos)
22143 break;
22144 /* If the position sought is the end of the buffer,
22145 don't include the blank lines at the bottom of the window. */
22146 else if (line_start_position == pos
22147 && pos == BUF_ZV (XBUFFER (w->buffer)))
22149 maybe_next_line_p = 1;
22150 break;
22152 else if (line_start_position > 0)
22154 best_row = row;
22155 best_row_vpos = row_vpos;
22158 if (row->y + row->height >= yb)
22159 break;
22161 ++row;
22162 ++row_vpos;
22165 /* Find the right column within BEST_ROW. */
22166 lastcol = 0;
22167 current_x = best_row->x;
22168 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22170 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22171 int charpos = glyph->charpos;
22173 if (BUFFERP (glyph->object))
22175 if (charpos == pos)
22177 *hpos = i;
22178 *vpos = best_row_vpos;
22179 *x = current_x;
22180 *y = best_row->y;
22181 return 1;
22183 else if (charpos > pos)
22184 break;
22186 else if (EQ (glyph->object, stop))
22187 break;
22189 if (charpos > 0)
22190 lastcol = i;
22191 current_x += glyph->pixel_width;
22194 /* If we're looking for the end of the buffer,
22195 and we didn't find it in the line we scanned,
22196 use the start of the following line. */
22197 if (maybe_next_line_p)
22199 ++best_row;
22200 ++best_row_vpos;
22201 lastcol = 0;
22202 current_x = best_row->x;
22205 *vpos = best_row_vpos;
22206 *hpos = lastcol + 1;
22207 *x = current_x;
22208 *y = best_row->y;
22209 return 0;
22212 #endif /* not 1 */
22215 /* Find the position of the glyph for position POS in OBJECT in
22216 window W's current matrix, and return in *X, *Y the pixel
22217 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22219 RIGHT_P non-zero means return the position of the right edge of the
22220 glyph, RIGHT_P zero means return the left edge position.
22222 If no glyph for POS exists in the matrix, return the position of
22223 the glyph with the next smaller position that is in the matrix, if
22224 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22225 exists in the matrix, return the position of the glyph with the
22226 next larger position in OBJECT.
22228 Value is non-zero if a glyph was found. */
22230 static int
22231 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22232 struct window *w;
22233 int pos;
22234 Lisp_Object object;
22235 int *hpos, *vpos, *x, *y;
22236 int right_p;
22238 int yb = window_text_bottom_y (w);
22239 struct glyph_row *r;
22240 struct glyph *best_glyph = NULL;
22241 struct glyph_row *best_row = NULL;
22242 int best_x = 0;
22244 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22245 r->enabled_p && r->y < yb;
22246 ++r)
22248 struct glyph *g = r->glyphs[TEXT_AREA];
22249 struct glyph *e = g + r->used[TEXT_AREA];
22250 int gx;
22252 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22253 if (EQ (g->object, object))
22255 if (g->charpos == pos)
22257 best_glyph = g;
22258 best_x = gx;
22259 best_row = r;
22260 goto found;
22262 else if (best_glyph == NULL
22263 || ((abs (g->charpos - pos)
22264 < abs (best_glyph->charpos - pos))
22265 && (right_p
22266 ? g->charpos < pos
22267 : g->charpos > pos)))
22269 best_glyph = g;
22270 best_x = gx;
22271 best_row = r;
22276 found:
22278 if (best_glyph)
22280 *x = best_x;
22281 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22283 if (right_p)
22285 *x += best_glyph->pixel_width;
22286 ++*hpos;
22289 *y = best_row->y;
22290 *vpos = best_row - w->current_matrix->rows;
22293 return best_glyph != NULL;
22297 /* See if position X, Y is within a hot-spot of an image. */
22299 static int
22300 on_hot_spot_p (hot_spot, x, y)
22301 Lisp_Object hot_spot;
22302 int x, y;
22304 if (!CONSP (hot_spot))
22305 return 0;
22307 if (EQ (XCAR (hot_spot), Qrect))
22309 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22310 Lisp_Object rect = XCDR (hot_spot);
22311 Lisp_Object tem;
22312 if (!CONSP (rect))
22313 return 0;
22314 if (!CONSP (XCAR (rect)))
22315 return 0;
22316 if (!CONSP (XCDR (rect)))
22317 return 0;
22318 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22319 return 0;
22320 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22321 return 0;
22322 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22323 return 0;
22324 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22325 return 0;
22326 return 1;
22328 else if (EQ (XCAR (hot_spot), Qcircle))
22330 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22331 Lisp_Object circ = XCDR (hot_spot);
22332 Lisp_Object lr, lx0, ly0;
22333 if (CONSP (circ)
22334 && CONSP (XCAR (circ))
22335 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22336 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22337 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22339 double r = XFLOATINT (lr);
22340 double dx = XINT (lx0) - x;
22341 double dy = XINT (ly0) - y;
22342 return (dx * dx + dy * dy <= r * r);
22345 else if (EQ (XCAR (hot_spot), Qpoly))
22347 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22348 if (VECTORP (XCDR (hot_spot)))
22350 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22351 Lisp_Object *poly = v->contents;
22352 int n = v->size;
22353 int i;
22354 int inside = 0;
22355 Lisp_Object lx, ly;
22356 int x0, y0;
22358 /* Need an even number of coordinates, and at least 3 edges. */
22359 if (n < 6 || n & 1)
22360 return 0;
22362 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22363 If count is odd, we are inside polygon. Pixels on edges
22364 may or may not be included depending on actual geometry of the
22365 polygon. */
22366 if ((lx = poly[n-2], !INTEGERP (lx))
22367 || (ly = poly[n-1], !INTEGERP (lx)))
22368 return 0;
22369 x0 = XINT (lx), y0 = XINT (ly);
22370 for (i = 0; i < n; i += 2)
22372 int x1 = x0, y1 = y0;
22373 if ((lx = poly[i], !INTEGERP (lx))
22374 || (ly = poly[i+1], !INTEGERP (ly)))
22375 return 0;
22376 x0 = XINT (lx), y0 = XINT (ly);
22378 /* Does this segment cross the X line? */
22379 if (x0 >= x)
22381 if (x1 >= x)
22382 continue;
22384 else if (x1 < x)
22385 continue;
22386 if (y > y0 && y > y1)
22387 continue;
22388 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22389 inside = !inside;
22391 return inside;
22394 /* If we don't understand the format, pretend we're not in the hot-spot. */
22395 return 0;
22398 Lisp_Object
22399 find_hot_spot (map, x, y)
22400 Lisp_Object map;
22401 int x, y;
22403 while (CONSP (map))
22405 if (CONSP (XCAR (map))
22406 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22407 return XCAR (map);
22408 map = XCDR (map);
22411 return Qnil;
22414 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22415 3, 3, 0,
22416 doc: /* Lookup in image map MAP coordinates X and Y.
22417 An image map is an alist where each element has the format (AREA ID PLIST).
22418 An AREA is specified as either a rectangle, a circle, or a polygon:
22419 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22420 pixel coordinates of the upper left and bottom right corners.
22421 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22422 and the radius of the circle; r may be a float or integer.
22423 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22424 vector describes one corner in the polygon.
22425 Returns the alist element for the first matching AREA in MAP. */)
22426 (map, x, y)
22427 Lisp_Object map;
22428 Lisp_Object x, y;
22430 if (NILP (map))
22431 return Qnil;
22433 CHECK_NUMBER (x);
22434 CHECK_NUMBER (y);
22436 return find_hot_spot (map, XINT (x), XINT (y));
22440 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22441 static void
22442 define_frame_cursor1 (f, cursor, pointer)
22443 struct frame *f;
22444 Cursor cursor;
22445 Lisp_Object pointer;
22447 /* Do not change cursor shape while dragging mouse. */
22448 if (!NILP (do_mouse_tracking))
22449 return;
22451 if (!NILP (pointer))
22453 if (EQ (pointer, Qarrow))
22454 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22455 else if (EQ (pointer, Qhand))
22456 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22457 else if (EQ (pointer, Qtext))
22458 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22459 else if (EQ (pointer, intern ("hdrag")))
22460 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22461 #ifdef HAVE_X_WINDOWS
22462 else if (EQ (pointer, intern ("vdrag")))
22463 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22464 #endif
22465 else if (EQ (pointer, intern ("hourglass")))
22466 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22467 else if (EQ (pointer, Qmodeline))
22468 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22469 else
22470 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22473 if (cursor != No_Cursor)
22474 rif->define_frame_cursor (f, cursor);
22477 /* Take proper action when mouse has moved to the mode or header line
22478 or marginal area AREA of window W, x-position X and y-position Y.
22479 X is relative to the start of the text display area of W, so the
22480 width of bitmap areas and scroll bars must be subtracted to get a
22481 position relative to the start of the mode line. */
22483 static void
22484 note_mode_line_or_margin_highlight (window, x, y, area)
22485 Lisp_Object window;
22486 int x, y;
22487 enum window_part area;
22489 struct window *w = XWINDOW (window);
22490 struct frame *f = XFRAME (w->frame);
22491 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22492 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22493 Lisp_Object pointer = Qnil;
22494 int charpos, dx, dy, width, height;
22495 Lisp_Object string, object = Qnil;
22496 Lisp_Object pos, help;
22498 Lisp_Object mouse_face;
22499 int original_x_pixel = x;
22500 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22501 struct glyph_row *row;
22503 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22505 int x0;
22506 struct glyph *end;
22508 string = mode_line_string (w, area, &x, &y, &charpos,
22509 &object, &dx, &dy, &width, &height);
22511 row = (area == ON_MODE_LINE
22512 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22513 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22515 /* Find glyph */
22516 if (row->mode_line_p && row->enabled_p)
22518 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22519 end = glyph + row->used[TEXT_AREA];
22521 for (x0 = original_x_pixel;
22522 glyph < end && x0 >= glyph->pixel_width;
22523 ++glyph)
22524 x0 -= glyph->pixel_width;
22526 if (glyph >= end)
22527 glyph = NULL;
22530 else
22532 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22533 string = marginal_area_string (w, area, &x, &y, &charpos,
22534 &object, &dx, &dy, &width, &height);
22537 help = Qnil;
22539 if (IMAGEP (object))
22541 Lisp_Object image_map, hotspot;
22542 if ((image_map = Fplist_get (XCDR (object), QCmap),
22543 !NILP (image_map))
22544 && (hotspot = find_hot_spot (image_map, dx, dy),
22545 CONSP (hotspot))
22546 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22548 Lisp_Object area_id, plist;
22550 area_id = XCAR (hotspot);
22551 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22552 If so, we could look for mouse-enter, mouse-leave
22553 properties in PLIST (and do something...). */
22554 hotspot = XCDR (hotspot);
22555 if (CONSP (hotspot)
22556 && (plist = XCAR (hotspot), CONSP (plist)))
22558 pointer = Fplist_get (plist, Qpointer);
22559 if (NILP (pointer))
22560 pointer = Qhand;
22561 help = Fplist_get (plist, Qhelp_echo);
22562 if (!NILP (help))
22564 help_echo_string = help;
22565 /* Is this correct? ++kfs */
22566 XSETWINDOW (help_echo_window, w);
22567 help_echo_object = w->buffer;
22568 help_echo_pos = charpos;
22572 if (NILP (pointer))
22573 pointer = Fplist_get (XCDR (object), QCpointer);
22576 if (STRINGP (string))
22578 pos = make_number (charpos);
22579 /* If we're on a string with `help-echo' text property, arrange
22580 for the help to be displayed. This is done by setting the
22581 global variable help_echo_string to the help string. */
22582 if (NILP (help))
22584 help = Fget_text_property (pos, Qhelp_echo, string);
22585 if (!NILP (help))
22587 help_echo_string = help;
22588 XSETWINDOW (help_echo_window, w);
22589 help_echo_object = string;
22590 help_echo_pos = charpos;
22594 if (NILP (pointer))
22595 pointer = Fget_text_property (pos, Qpointer, string);
22597 /* Change the mouse pointer according to what is under X/Y. */
22598 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22600 Lisp_Object map;
22601 map = Fget_text_property (pos, Qlocal_map, string);
22602 if (!KEYMAPP (map))
22603 map = Fget_text_property (pos, Qkeymap, string);
22604 if (!KEYMAPP (map))
22605 cursor = dpyinfo->vertical_scroll_bar_cursor;
22608 /* Change the mouse face according to what is under X/Y. */
22609 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22610 if (!NILP (mouse_face)
22611 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22612 && glyph)
22614 Lisp_Object b, e;
22616 struct glyph * tmp_glyph;
22618 int gpos;
22619 int gseq_length;
22620 int total_pixel_width;
22621 int ignore;
22623 int vpos, hpos;
22625 b = Fprevious_single_property_change (make_number (charpos + 1),
22626 Qmouse_face, string, Qnil);
22627 if (NILP (b))
22628 b = make_number (0);
22630 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22631 if (NILP (e))
22632 e = make_number (SCHARS (string));
22634 /* Calculate the position(glyph position: GPOS) of GLYPH in
22635 displayed string. GPOS is different from CHARPOS.
22637 CHARPOS is the position of glyph in internal string
22638 object. A mode line string format has structures which
22639 is converted to a flatten by emacs lisp interpreter.
22640 The internal string is an element of the structures.
22641 The displayed string is the flatten string. */
22642 gpos = 0;
22643 if (glyph > row_start_glyph)
22645 tmp_glyph = glyph - 1;
22646 while (tmp_glyph >= row_start_glyph
22647 && tmp_glyph->charpos >= XINT (b)
22648 && EQ (tmp_glyph->object, glyph->object))
22650 tmp_glyph--;
22651 gpos++;
22655 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22656 displayed string holding GLYPH.
22658 GSEQ_LENGTH is different from SCHARS (STRING).
22659 SCHARS (STRING) returns the length of the internal string. */
22660 for (tmp_glyph = glyph, gseq_length = gpos;
22661 tmp_glyph->charpos < XINT (e);
22662 tmp_glyph++, gseq_length++)
22664 if (!EQ (tmp_glyph->object, glyph->object))
22665 break;
22668 total_pixel_width = 0;
22669 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22670 total_pixel_width += tmp_glyph->pixel_width;
22672 /* Pre calculation of re-rendering position */
22673 vpos = (x - gpos);
22674 hpos = (area == ON_MODE_LINE
22675 ? (w->current_matrix)->nrows - 1
22676 : 0);
22678 /* If the re-rendering position is included in the last
22679 re-rendering area, we should do nothing. */
22680 if ( EQ (window, dpyinfo->mouse_face_window)
22681 && dpyinfo->mouse_face_beg_col <= vpos
22682 && vpos < dpyinfo->mouse_face_end_col
22683 && dpyinfo->mouse_face_beg_row == hpos )
22684 return;
22686 if (clear_mouse_face (dpyinfo))
22687 cursor = No_Cursor;
22689 dpyinfo->mouse_face_beg_col = vpos;
22690 dpyinfo->mouse_face_beg_row = hpos;
22692 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22693 dpyinfo->mouse_face_beg_y = 0;
22695 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22696 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22698 dpyinfo->mouse_face_end_x = 0;
22699 dpyinfo->mouse_face_end_y = 0;
22701 dpyinfo->mouse_face_past_end = 0;
22702 dpyinfo->mouse_face_window = window;
22704 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22705 charpos,
22706 0, 0, 0, &ignore,
22707 glyph->face_id, 1);
22708 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22710 if (NILP (pointer))
22711 pointer = Qhand;
22713 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22714 clear_mouse_face (dpyinfo);
22716 define_frame_cursor1 (f, cursor, pointer);
22720 /* EXPORT:
22721 Take proper action when the mouse has moved to position X, Y on
22722 frame F as regards highlighting characters that have mouse-face
22723 properties. Also de-highlighting chars where the mouse was before.
22724 X and Y can be negative or out of range. */
22726 void
22727 note_mouse_highlight (f, x, y)
22728 struct frame *f;
22729 int x, y;
22731 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22732 enum window_part part;
22733 Lisp_Object window;
22734 struct window *w;
22735 Cursor cursor = No_Cursor;
22736 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22737 struct buffer *b;
22739 /* When a menu is active, don't highlight because this looks odd. */
22740 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22741 if (popup_activated ())
22742 return;
22743 #endif
22745 if (NILP (Vmouse_highlight)
22746 || !f->glyphs_initialized_p)
22747 return;
22749 dpyinfo->mouse_face_mouse_x = x;
22750 dpyinfo->mouse_face_mouse_y = y;
22751 dpyinfo->mouse_face_mouse_frame = f;
22753 if (dpyinfo->mouse_face_defer)
22754 return;
22756 if (gc_in_progress)
22758 dpyinfo->mouse_face_deferred_gc = 1;
22759 return;
22762 /* Which window is that in? */
22763 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22765 /* If we were displaying active text in another window, clear that.
22766 Also clear if we move out of text area in same window. */
22767 if (! EQ (window, dpyinfo->mouse_face_window)
22768 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22769 && !NILP (dpyinfo->mouse_face_window)))
22770 clear_mouse_face (dpyinfo);
22772 /* Not on a window -> return. */
22773 if (!WINDOWP (window))
22774 return;
22776 /* Reset help_echo_string. It will get recomputed below. */
22777 help_echo_string = Qnil;
22779 /* Convert to window-relative pixel coordinates. */
22780 w = XWINDOW (window);
22781 frame_to_window_pixel_xy (w, &x, &y);
22783 /* Handle tool-bar window differently since it doesn't display a
22784 buffer. */
22785 if (EQ (window, f->tool_bar_window))
22787 note_tool_bar_highlight (f, x, y);
22788 return;
22791 /* Mouse is on the mode, header line or margin? */
22792 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22793 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22795 note_mode_line_or_margin_highlight (window, x, y, part);
22796 return;
22799 if (part == ON_VERTICAL_BORDER)
22801 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22802 help_echo_string = build_string ("drag-mouse-1: resize");
22804 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22805 || part == ON_SCROLL_BAR)
22806 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22807 else
22808 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22810 /* Are we in a window whose display is up to date?
22811 And verify the buffer's text has not changed. */
22812 b = XBUFFER (w->buffer);
22813 if (part == ON_TEXT
22814 && EQ (w->window_end_valid, w->buffer)
22815 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22816 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22818 int hpos, vpos, pos, i, dx, dy, area;
22819 struct glyph *glyph;
22820 Lisp_Object object;
22821 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22822 Lisp_Object *overlay_vec = NULL;
22823 int noverlays;
22824 struct buffer *obuf;
22825 int obegv, ozv, same_region;
22827 /* Find the glyph under X/Y. */
22828 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22830 /* Look for :pointer property on image. */
22831 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22833 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22834 if (img != NULL && IMAGEP (img->spec))
22836 Lisp_Object image_map, hotspot;
22837 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22838 !NILP (image_map))
22839 && (hotspot = find_hot_spot (image_map,
22840 glyph->slice.x + dx,
22841 glyph->slice.y + dy),
22842 CONSP (hotspot))
22843 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22845 Lisp_Object area_id, plist;
22847 area_id = XCAR (hotspot);
22848 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22849 If so, we could look for mouse-enter, mouse-leave
22850 properties in PLIST (and do something...). */
22851 hotspot = XCDR (hotspot);
22852 if (CONSP (hotspot)
22853 && (plist = XCAR (hotspot), CONSP (plist)))
22855 pointer = Fplist_get (plist, Qpointer);
22856 if (NILP (pointer))
22857 pointer = Qhand;
22858 help_echo_string = Fplist_get (plist, Qhelp_echo);
22859 if (!NILP (help_echo_string))
22861 help_echo_window = window;
22862 help_echo_object = glyph->object;
22863 help_echo_pos = glyph->charpos;
22867 if (NILP (pointer))
22868 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22872 /* Clear mouse face if X/Y not over text. */
22873 if (glyph == NULL
22874 || area != TEXT_AREA
22875 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22877 if (clear_mouse_face (dpyinfo))
22878 cursor = No_Cursor;
22879 if (NILP (pointer))
22881 if (area != TEXT_AREA)
22882 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22883 else
22884 pointer = Vvoid_text_area_pointer;
22886 goto set_cursor;
22889 pos = glyph->charpos;
22890 object = glyph->object;
22891 if (!STRINGP (object) && !BUFFERP (object))
22892 goto set_cursor;
22894 /* If we get an out-of-range value, return now; avoid an error. */
22895 if (BUFFERP (object) && pos > BUF_Z (b))
22896 goto set_cursor;
22898 /* Make the window's buffer temporarily current for
22899 overlays_at and compute_char_face. */
22900 obuf = current_buffer;
22901 current_buffer = b;
22902 obegv = BEGV;
22903 ozv = ZV;
22904 BEGV = BEG;
22905 ZV = Z;
22907 /* Is this char mouse-active or does it have help-echo? */
22908 position = make_number (pos);
22910 if (BUFFERP (object))
22912 /* Put all the overlays we want in a vector in overlay_vec. */
22913 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22914 /* Sort overlays into increasing priority order. */
22915 noverlays = sort_overlays (overlay_vec, noverlays, w);
22917 else
22918 noverlays = 0;
22920 same_region = (EQ (window, dpyinfo->mouse_face_window)
22921 && vpos >= dpyinfo->mouse_face_beg_row
22922 && vpos <= dpyinfo->mouse_face_end_row
22923 && (vpos > dpyinfo->mouse_face_beg_row
22924 || hpos >= dpyinfo->mouse_face_beg_col)
22925 && (vpos < dpyinfo->mouse_face_end_row
22926 || hpos < dpyinfo->mouse_face_end_col
22927 || dpyinfo->mouse_face_past_end));
22929 if (same_region)
22930 cursor = No_Cursor;
22932 /* Check mouse-face highlighting. */
22933 if (! same_region
22934 /* If there exists an overlay with mouse-face overlapping
22935 the one we are currently highlighting, we have to
22936 check if we enter the overlapping overlay, and then
22937 highlight only that. */
22938 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22939 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22941 /* Find the highest priority overlay that has a mouse-face
22942 property. */
22943 overlay = Qnil;
22944 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22946 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22947 if (!NILP (mouse_face))
22948 overlay = overlay_vec[i];
22951 /* If we're actually highlighting the same overlay as
22952 before, there's no need to do that again. */
22953 if (!NILP (overlay)
22954 && EQ (overlay, dpyinfo->mouse_face_overlay))
22955 goto check_help_echo;
22957 dpyinfo->mouse_face_overlay = overlay;
22959 /* Clear the display of the old active region, if any. */
22960 if (clear_mouse_face (dpyinfo))
22961 cursor = No_Cursor;
22963 /* If no overlay applies, get a text property. */
22964 if (NILP (overlay))
22965 mouse_face = Fget_text_property (position, Qmouse_face, object);
22967 /* Handle the overlay case. */
22968 if (!NILP (overlay))
22970 /* Find the range of text around this char that
22971 should be active. */
22972 Lisp_Object before, after;
22973 int ignore;
22975 before = Foverlay_start (overlay);
22976 after = Foverlay_end (overlay);
22977 /* Record this as the current active region. */
22978 fast_find_position (w, XFASTINT (before),
22979 &dpyinfo->mouse_face_beg_col,
22980 &dpyinfo->mouse_face_beg_row,
22981 &dpyinfo->mouse_face_beg_x,
22982 &dpyinfo->mouse_face_beg_y, Qnil);
22984 dpyinfo->mouse_face_past_end
22985 = !fast_find_position (w, XFASTINT (after),
22986 &dpyinfo->mouse_face_end_col,
22987 &dpyinfo->mouse_face_end_row,
22988 &dpyinfo->mouse_face_end_x,
22989 &dpyinfo->mouse_face_end_y, Qnil);
22990 dpyinfo->mouse_face_window = window;
22992 dpyinfo->mouse_face_face_id
22993 = face_at_buffer_position (w, pos, 0, 0,
22994 &ignore, pos + 1,
22995 !dpyinfo->mouse_face_hidden);
22997 /* Display it as active. */
22998 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22999 cursor = No_Cursor;
23001 /* Handle the text property case. */
23002 else if (!NILP (mouse_face) && BUFFERP (object))
23004 /* Find the range of text around this char that
23005 should be active. */
23006 Lisp_Object before, after, beginning, end;
23007 int ignore;
23009 beginning = Fmarker_position (w->start);
23010 end = make_number (BUF_Z (XBUFFER (object))
23011 - XFASTINT (w->window_end_pos));
23012 before
23013 = Fprevious_single_property_change (make_number (pos + 1),
23014 Qmouse_face,
23015 object, beginning);
23016 after
23017 = Fnext_single_property_change (position, Qmouse_face,
23018 object, end);
23020 /* Record this as the current active region. */
23021 fast_find_position (w, XFASTINT (before),
23022 &dpyinfo->mouse_face_beg_col,
23023 &dpyinfo->mouse_face_beg_row,
23024 &dpyinfo->mouse_face_beg_x,
23025 &dpyinfo->mouse_face_beg_y, Qnil);
23026 dpyinfo->mouse_face_past_end
23027 = !fast_find_position (w, XFASTINT (after),
23028 &dpyinfo->mouse_face_end_col,
23029 &dpyinfo->mouse_face_end_row,
23030 &dpyinfo->mouse_face_end_x,
23031 &dpyinfo->mouse_face_end_y, Qnil);
23032 dpyinfo->mouse_face_window = window;
23034 if (BUFFERP (object))
23035 dpyinfo->mouse_face_face_id
23036 = face_at_buffer_position (w, pos, 0, 0,
23037 &ignore, pos + 1,
23038 !dpyinfo->mouse_face_hidden);
23040 /* Display it as active. */
23041 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23042 cursor = No_Cursor;
23044 else if (!NILP (mouse_face) && STRINGP (object))
23046 Lisp_Object b, e;
23047 int ignore;
23049 b = Fprevious_single_property_change (make_number (pos + 1),
23050 Qmouse_face,
23051 object, Qnil);
23052 e = Fnext_single_property_change (position, Qmouse_face,
23053 object, Qnil);
23054 if (NILP (b))
23055 b = make_number (0);
23056 if (NILP (e))
23057 e = make_number (SCHARS (object) - 1);
23059 fast_find_string_pos (w, XINT (b), object,
23060 &dpyinfo->mouse_face_beg_col,
23061 &dpyinfo->mouse_face_beg_row,
23062 &dpyinfo->mouse_face_beg_x,
23063 &dpyinfo->mouse_face_beg_y, 0);
23064 fast_find_string_pos (w, XINT (e), object,
23065 &dpyinfo->mouse_face_end_col,
23066 &dpyinfo->mouse_face_end_row,
23067 &dpyinfo->mouse_face_end_x,
23068 &dpyinfo->mouse_face_end_y, 1);
23069 dpyinfo->mouse_face_past_end = 0;
23070 dpyinfo->mouse_face_window = window;
23071 dpyinfo->mouse_face_face_id
23072 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23073 glyph->face_id, 1);
23074 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23075 cursor = No_Cursor;
23077 else if (STRINGP (object) && NILP (mouse_face))
23079 /* A string which doesn't have mouse-face, but
23080 the text ``under'' it might have. */
23081 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23082 int start = MATRIX_ROW_START_CHARPOS (r);
23084 pos = string_buffer_position (w, object, start);
23085 if (pos > 0)
23086 mouse_face = get_char_property_and_overlay (make_number (pos),
23087 Qmouse_face,
23088 w->buffer,
23089 &overlay);
23090 if (!NILP (mouse_face) && !NILP (overlay))
23092 Lisp_Object before = Foverlay_start (overlay);
23093 Lisp_Object after = Foverlay_end (overlay);
23094 int ignore;
23096 /* Note that we might not be able to find position
23097 BEFORE in the glyph matrix if the overlay is
23098 entirely covered by a `display' property. In
23099 this case, we overshoot. So let's stop in
23100 the glyph matrix before glyphs for OBJECT. */
23101 fast_find_position (w, XFASTINT (before),
23102 &dpyinfo->mouse_face_beg_col,
23103 &dpyinfo->mouse_face_beg_row,
23104 &dpyinfo->mouse_face_beg_x,
23105 &dpyinfo->mouse_face_beg_y,
23106 object);
23108 dpyinfo->mouse_face_past_end
23109 = !fast_find_position (w, XFASTINT (after),
23110 &dpyinfo->mouse_face_end_col,
23111 &dpyinfo->mouse_face_end_row,
23112 &dpyinfo->mouse_face_end_x,
23113 &dpyinfo->mouse_face_end_y,
23114 Qnil);
23115 dpyinfo->mouse_face_window = window;
23116 dpyinfo->mouse_face_face_id
23117 = face_at_buffer_position (w, pos, 0, 0,
23118 &ignore, pos + 1,
23119 !dpyinfo->mouse_face_hidden);
23121 /* Display it as active. */
23122 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23123 cursor = No_Cursor;
23128 check_help_echo:
23130 /* Look for a `help-echo' property. */
23131 if (NILP (help_echo_string)) {
23132 Lisp_Object help, overlay;
23134 /* Check overlays first. */
23135 help = overlay = Qnil;
23136 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23138 overlay = overlay_vec[i];
23139 help = Foverlay_get (overlay, Qhelp_echo);
23142 if (!NILP (help))
23144 help_echo_string = help;
23145 help_echo_window = window;
23146 help_echo_object = overlay;
23147 help_echo_pos = pos;
23149 else
23151 Lisp_Object object = glyph->object;
23152 int charpos = glyph->charpos;
23154 /* Try text properties. */
23155 if (STRINGP (object)
23156 && charpos >= 0
23157 && charpos < SCHARS (object))
23159 help = Fget_text_property (make_number (charpos),
23160 Qhelp_echo, object);
23161 if (NILP (help))
23163 /* If the string itself doesn't specify a help-echo,
23164 see if the buffer text ``under'' it does. */
23165 struct glyph_row *r
23166 = MATRIX_ROW (w->current_matrix, vpos);
23167 int start = MATRIX_ROW_START_CHARPOS (r);
23168 int pos = string_buffer_position (w, object, start);
23169 if (pos > 0)
23171 help = Fget_char_property (make_number (pos),
23172 Qhelp_echo, w->buffer);
23173 if (!NILP (help))
23175 charpos = pos;
23176 object = w->buffer;
23181 else if (BUFFERP (object)
23182 && charpos >= BEGV
23183 && charpos < ZV)
23184 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23185 object);
23187 if (!NILP (help))
23189 help_echo_string = help;
23190 help_echo_window = window;
23191 help_echo_object = object;
23192 help_echo_pos = charpos;
23197 /* Look for a `pointer' property. */
23198 if (NILP (pointer))
23200 /* Check overlays first. */
23201 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23202 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23204 if (NILP (pointer))
23206 Lisp_Object object = glyph->object;
23207 int charpos = glyph->charpos;
23209 /* Try text properties. */
23210 if (STRINGP (object)
23211 && charpos >= 0
23212 && charpos < SCHARS (object))
23214 pointer = Fget_text_property (make_number (charpos),
23215 Qpointer, object);
23216 if (NILP (pointer))
23218 /* If the string itself doesn't specify a pointer,
23219 see if the buffer text ``under'' it does. */
23220 struct glyph_row *r
23221 = MATRIX_ROW (w->current_matrix, vpos);
23222 int start = MATRIX_ROW_START_CHARPOS (r);
23223 int pos = string_buffer_position (w, object, start);
23224 if (pos > 0)
23225 pointer = Fget_char_property (make_number (pos),
23226 Qpointer, w->buffer);
23229 else if (BUFFERP (object)
23230 && charpos >= BEGV
23231 && charpos < ZV)
23232 pointer = Fget_text_property (make_number (charpos),
23233 Qpointer, object);
23237 BEGV = obegv;
23238 ZV = ozv;
23239 current_buffer = obuf;
23242 set_cursor:
23244 define_frame_cursor1 (f, cursor, pointer);
23248 /* EXPORT for RIF:
23249 Clear any mouse-face on window W. This function is part of the
23250 redisplay interface, and is called from try_window_id and similar
23251 functions to ensure the mouse-highlight is off. */
23253 void
23254 x_clear_window_mouse_face (w)
23255 struct window *w;
23257 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23258 Lisp_Object window;
23260 BLOCK_INPUT;
23261 XSETWINDOW (window, w);
23262 if (EQ (window, dpyinfo->mouse_face_window))
23263 clear_mouse_face (dpyinfo);
23264 UNBLOCK_INPUT;
23268 /* EXPORT:
23269 Just discard the mouse face information for frame F, if any.
23270 This is used when the size of F is changed. */
23272 void
23273 cancel_mouse_face (f)
23274 struct frame *f;
23276 Lisp_Object window;
23277 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23279 window = dpyinfo->mouse_face_window;
23280 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23282 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23283 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23284 dpyinfo->mouse_face_window = Qnil;
23289 #endif /* HAVE_WINDOW_SYSTEM */
23292 /***********************************************************************
23293 Exposure Events
23294 ***********************************************************************/
23296 #ifdef HAVE_WINDOW_SYSTEM
23298 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23299 which intersects rectangle R. R is in window-relative coordinates. */
23301 static void
23302 expose_area (w, row, r, area)
23303 struct window *w;
23304 struct glyph_row *row;
23305 XRectangle *r;
23306 enum glyph_row_area area;
23308 struct glyph *first = row->glyphs[area];
23309 struct glyph *end = row->glyphs[area] + row->used[area];
23310 struct glyph *last;
23311 int first_x, start_x, x;
23313 if (area == TEXT_AREA && row->fill_line_p)
23314 /* If row extends face to end of line write the whole line. */
23315 draw_glyphs (w, 0, row, area,
23316 0, row->used[area],
23317 DRAW_NORMAL_TEXT, 0);
23318 else
23320 /* Set START_X to the window-relative start position for drawing glyphs of
23321 AREA. The first glyph of the text area can be partially visible.
23322 The first glyphs of other areas cannot. */
23323 start_x = window_box_left_offset (w, area);
23324 x = start_x;
23325 if (area == TEXT_AREA)
23326 x += row->x;
23328 /* Find the first glyph that must be redrawn. */
23329 while (first < end
23330 && x + first->pixel_width < r->x)
23332 x += first->pixel_width;
23333 ++first;
23336 /* Find the last one. */
23337 last = first;
23338 first_x = x;
23339 while (last < end
23340 && x < r->x + r->width)
23342 x += last->pixel_width;
23343 ++last;
23346 /* Repaint. */
23347 if (last > first)
23348 draw_glyphs (w, first_x - start_x, row, area,
23349 first - row->glyphs[area], last - row->glyphs[area],
23350 DRAW_NORMAL_TEXT, 0);
23355 /* Redraw the parts of the glyph row ROW on window W intersecting
23356 rectangle R. R is in window-relative coordinates. Value is
23357 non-zero if mouse-face was overwritten. */
23359 static int
23360 expose_line (w, row, r)
23361 struct window *w;
23362 struct glyph_row *row;
23363 XRectangle *r;
23365 xassert (row->enabled_p);
23367 if (row->mode_line_p || w->pseudo_window_p)
23368 draw_glyphs (w, 0, row, TEXT_AREA,
23369 0, row->used[TEXT_AREA],
23370 DRAW_NORMAL_TEXT, 0);
23371 else
23373 if (row->used[LEFT_MARGIN_AREA])
23374 expose_area (w, row, r, LEFT_MARGIN_AREA);
23375 if (row->used[TEXT_AREA])
23376 expose_area (w, row, r, TEXT_AREA);
23377 if (row->used[RIGHT_MARGIN_AREA])
23378 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23379 draw_row_fringe_bitmaps (w, row);
23382 return row->mouse_face_p;
23386 /* Redraw those parts of glyphs rows during expose event handling that
23387 overlap other rows. Redrawing of an exposed line writes over parts
23388 of lines overlapping that exposed line; this function fixes that.
23390 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23391 row in W's current matrix that is exposed and overlaps other rows.
23392 LAST_OVERLAPPING_ROW is the last such row. */
23394 static void
23395 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23396 struct window *w;
23397 struct glyph_row *first_overlapping_row;
23398 struct glyph_row *last_overlapping_row;
23400 struct glyph_row *row;
23402 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23403 if (row->overlapping_p)
23405 xassert (row->enabled_p && !row->mode_line_p);
23407 if (row->used[LEFT_MARGIN_AREA])
23408 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23410 if (row->used[TEXT_AREA])
23411 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23413 if (row->used[RIGHT_MARGIN_AREA])
23414 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23419 /* Return non-zero if W's cursor intersects rectangle R. */
23421 static int
23422 phys_cursor_in_rect_p (w, r)
23423 struct window *w;
23424 XRectangle *r;
23426 XRectangle cr, result;
23427 struct glyph *cursor_glyph;
23429 cursor_glyph = get_phys_cursor_glyph (w);
23430 if (cursor_glyph)
23432 /* r is relative to W's box, but w->phys_cursor.x is relative
23433 to left edge of W's TEXT area. Adjust it. */
23434 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23435 cr.y = w->phys_cursor.y;
23436 cr.width = cursor_glyph->pixel_width;
23437 cr.height = w->phys_cursor_height;
23438 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23439 I assume the effect is the same -- and this is portable. */
23440 return x_intersect_rectangles (&cr, r, &result);
23442 else
23443 return 0;
23447 /* EXPORT:
23448 Draw a vertical window border to the right of window W if W doesn't
23449 have vertical scroll bars. */
23451 void
23452 x_draw_vertical_border (w)
23453 struct window *w;
23455 /* We could do better, if we knew what type of scroll-bar the adjacent
23456 windows (on either side) have... But we don't :-(
23457 However, I think this works ok. ++KFS 2003-04-25 */
23459 /* Redraw borders between horizontally adjacent windows. Don't
23460 do it for frames with vertical scroll bars because either the
23461 right scroll bar of a window, or the left scroll bar of its
23462 neighbor will suffice as a border. */
23463 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23464 return;
23466 if (!WINDOW_RIGHTMOST_P (w)
23467 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23469 int x0, x1, y0, y1;
23471 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23472 y1 -= 1;
23474 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23475 x1 -= 1;
23477 rif->draw_vertical_window_border (w, x1, y0, y1);
23479 else if (!WINDOW_LEFTMOST_P (w)
23480 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23482 int x0, x1, y0, y1;
23484 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23485 y1 -= 1;
23487 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23488 x0 -= 1;
23490 rif->draw_vertical_window_border (w, x0, y0, y1);
23495 /* Redraw the part of window W intersection rectangle FR. Pixel
23496 coordinates in FR are frame-relative. Call this function with
23497 input blocked. Value is non-zero if the exposure overwrites
23498 mouse-face. */
23500 static int
23501 expose_window (w, fr)
23502 struct window *w;
23503 XRectangle *fr;
23505 struct frame *f = XFRAME (w->frame);
23506 XRectangle wr, r;
23507 int mouse_face_overwritten_p = 0;
23509 /* If window is not yet fully initialized, do nothing. This can
23510 happen when toolkit scroll bars are used and a window is split.
23511 Reconfiguring the scroll bar will generate an expose for a newly
23512 created window. */
23513 if (w->current_matrix == NULL)
23514 return 0;
23516 /* When we're currently updating the window, display and current
23517 matrix usually don't agree. Arrange for a thorough display
23518 later. */
23519 if (w == updated_window)
23521 SET_FRAME_GARBAGED (f);
23522 return 0;
23525 /* Frame-relative pixel rectangle of W. */
23526 wr.x = WINDOW_LEFT_EDGE_X (w);
23527 wr.y = WINDOW_TOP_EDGE_Y (w);
23528 wr.width = WINDOW_TOTAL_WIDTH (w);
23529 wr.height = WINDOW_TOTAL_HEIGHT (w);
23531 if (x_intersect_rectangles (fr, &wr, &r))
23533 int yb = window_text_bottom_y (w);
23534 struct glyph_row *row;
23535 int cursor_cleared_p;
23536 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23538 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23539 r.x, r.y, r.width, r.height));
23541 /* Convert to window coordinates. */
23542 r.x -= WINDOW_LEFT_EDGE_X (w);
23543 r.y -= WINDOW_TOP_EDGE_Y (w);
23545 /* Turn off the cursor. */
23546 if (!w->pseudo_window_p
23547 && phys_cursor_in_rect_p (w, &r))
23549 x_clear_cursor (w);
23550 cursor_cleared_p = 1;
23552 else
23553 cursor_cleared_p = 0;
23555 /* Update lines intersecting rectangle R. */
23556 first_overlapping_row = last_overlapping_row = NULL;
23557 for (row = w->current_matrix->rows;
23558 row->enabled_p;
23559 ++row)
23561 int y0 = row->y;
23562 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23564 if ((y0 >= r.y && y0 < r.y + r.height)
23565 || (y1 > r.y && y1 < r.y + r.height)
23566 || (r.y >= y0 && r.y < y1)
23567 || (r.y + r.height > y0 && r.y + r.height < y1))
23569 /* A header line may be overlapping, but there is no need
23570 to fix overlapping areas for them. KFS 2005-02-12 */
23571 if (row->overlapping_p && !row->mode_line_p)
23573 if (first_overlapping_row == NULL)
23574 first_overlapping_row = row;
23575 last_overlapping_row = row;
23578 if (expose_line (w, row, &r))
23579 mouse_face_overwritten_p = 1;
23582 if (y1 >= yb)
23583 break;
23586 /* Display the mode line if there is one. */
23587 if (WINDOW_WANTS_MODELINE_P (w)
23588 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23589 row->enabled_p)
23590 && row->y < r.y + r.height)
23592 if (expose_line (w, row, &r))
23593 mouse_face_overwritten_p = 1;
23596 if (!w->pseudo_window_p)
23598 /* Fix the display of overlapping rows. */
23599 if (first_overlapping_row)
23600 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23602 /* Draw border between windows. */
23603 x_draw_vertical_border (w);
23605 /* Turn the cursor on again. */
23606 if (cursor_cleared_p)
23607 update_window_cursor (w, 1);
23611 return mouse_face_overwritten_p;
23616 /* Redraw (parts) of all windows in the window tree rooted at W that
23617 intersect R. R contains frame pixel coordinates. Value is
23618 non-zero if the exposure overwrites mouse-face. */
23620 static int
23621 expose_window_tree (w, r)
23622 struct window *w;
23623 XRectangle *r;
23625 struct frame *f = XFRAME (w->frame);
23626 int mouse_face_overwritten_p = 0;
23628 while (w && !FRAME_GARBAGED_P (f))
23630 if (!NILP (w->hchild))
23631 mouse_face_overwritten_p
23632 |= expose_window_tree (XWINDOW (w->hchild), r);
23633 else if (!NILP (w->vchild))
23634 mouse_face_overwritten_p
23635 |= expose_window_tree (XWINDOW (w->vchild), r);
23636 else
23637 mouse_face_overwritten_p |= expose_window (w, r);
23639 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23642 return mouse_face_overwritten_p;
23646 /* EXPORT:
23647 Redisplay an exposed area of frame F. X and Y are the upper-left
23648 corner of the exposed rectangle. W and H are width and height of
23649 the exposed area. All are pixel values. W or H zero means redraw
23650 the entire frame. */
23652 void
23653 expose_frame (f, x, y, w, h)
23654 struct frame *f;
23655 int x, y, w, h;
23657 XRectangle r;
23658 int mouse_face_overwritten_p = 0;
23660 TRACE ((stderr, "expose_frame "));
23662 /* No need to redraw if frame will be redrawn soon. */
23663 if (FRAME_GARBAGED_P (f))
23665 TRACE ((stderr, " garbaged\n"));
23666 return;
23669 /* If basic faces haven't been realized yet, there is no point in
23670 trying to redraw anything. This can happen when we get an expose
23671 event while Emacs is starting, e.g. by moving another window. */
23672 if (FRAME_FACE_CACHE (f) == NULL
23673 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23675 TRACE ((stderr, " no faces\n"));
23676 return;
23679 if (w == 0 || h == 0)
23681 r.x = r.y = 0;
23682 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23683 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23685 else
23687 r.x = x;
23688 r.y = y;
23689 r.width = w;
23690 r.height = h;
23693 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23694 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23696 if (WINDOWP (f->tool_bar_window))
23697 mouse_face_overwritten_p
23698 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23700 #ifdef HAVE_X_WINDOWS
23701 #ifndef MSDOS
23702 #ifndef USE_X_TOOLKIT
23703 if (WINDOWP (f->menu_bar_window))
23704 mouse_face_overwritten_p
23705 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23706 #endif /* not USE_X_TOOLKIT */
23707 #endif
23708 #endif
23710 /* Some window managers support a focus-follows-mouse style with
23711 delayed raising of frames. Imagine a partially obscured frame,
23712 and moving the mouse into partially obscured mouse-face on that
23713 frame. The visible part of the mouse-face will be highlighted,
23714 then the WM raises the obscured frame. With at least one WM, KDE
23715 2.1, Emacs is not getting any event for the raising of the frame
23716 (even tried with SubstructureRedirectMask), only Expose events.
23717 These expose events will draw text normally, i.e. not
23718 highlighted. Which means we must redo the highlight here.
23719 Subsume it under ``we love X''. --gerd 2001-08-15 */
23720 /* Included in Windows version because Windows most likely does not
23721 do the right thing if any third party tool offers
23722 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23723 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23725 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23726 if (f == dpyinfo->mouse_face_mouse_frame)
23728 int x = dpyinfo->mouse_face_mouse_x;
23729 int y = dpyinfo->mouse_face_mouse_y;
23730 clear_mouse_face (dpyinfo);
23731 note_mouse_highlight (f, x, y);
23737 /* EXPORT:
23738 Determine the intersection of two rectangles R1 and R2. Return
23739 the intersection in *RESULT. Value is non-zero if RESULT is not
23740 empty. */
23743 x_intersect_rectangles (r1, r2, result)
23744 XRectangle *r1, *r2, *result;
23746 XRectangle *left, *right;
23747 XRectangle *upper, *lower;
23748 int intersection_p = 0;
23750 /* Rearrange so that R1 is the left-most rectangle. */
23751 if (r1->x < r2->x)
23752 left = r1, right = r2;
23753 else
23754 left = r2, right = r1;
23756 /* X0 of the intersection is right.x0, if this is inside R1,
23757 otherwise there is no intersection. */
23758 if (right->x <= left->x + left->width)
23760 result->x = right->x;
23762 /* The right end of the intersection is the minimum of the
23763 the right ends of left and right. */
23764 result->width = (min (left->x + left->width, right->x + right->width)
23765 - result->x);
23767 /* Same game for Y. */
23768 if (r1->y < r2->y)
23769 upper = r1, lower = r2;
23770 else
23771 upper = r2, lower = r1;
23773 /* The upper end of the intersection is lower.y0, if this is inside
23774 of upper. Otherwise, there is no intersection. */
23775 if (lower->y <= upper->y + upper->height)
23777 result->y = lower->y;
23779 /* The lower end of the intersection is the minimum of the lower
23780 ends of upper and lower. */
23781 result->height = (min (lower->y + lower->height,
23782 upper->y + upper->height)
23783 - result->y);
23784 intersection_p = 1;
23788 return intersection_p;
23791 #endif /* HAVE_WINDOW_SYSTEM */
23794 /***********************************************************************
23795 Initialization
23796 ***********************************************************************/
23798 void
23799 syms_of_xdisp ()
23801 Vwith_echo_area_save_vector = Qnil;
23802 staticpro (&Vwith_echo_area_save_vector);
23804 Vmessage_stack = Qnil;
23805 staticpro (&Vmessage_stack);
23807 Qinhibit_redisplay = intern ("inhibit-redisplay");
23808 staticpro (&Qinhibit_redisplay);
23810 message_dolog_marker1 = Fmake_marker ();
23811 staticpro (&message_dolog_marker1);
23812 message_dolog_marker2 = Fmake_marker ();
23813 staticpro (&message_dolog_marker2);
23814 message_dolog_marker3 = Fmake_marker ();
23815 staticpro (&message_dolog_marker3);
23817 #if GLYPH_DEBUG
23818 defsubr (&Sdump_frame_glyph_matrix);
23819 defsubr (&Sdump_glyph_matrix);
23820 defsubr (&Sdump_glyph_row);
23821 defsubr (&Sdump_tool_bar_row);
23822 defsubr (&Strace_redisplay);
23823 defsubr (&Strace_to_stderr);
23824 #endif
23825 #ifdef HAVE_WINDOW_SYSTEM
23826 defsubr (&Stool_bar_lines_needed);
23827 defsubr (&Slookup_image_map);
23828 #endif
23829 defsubr (&Sformat_mode_line);
23830 defsubr (&Sinvisible_p);
23832 staticpro (&Qmenu_bar_update_hook);
23833 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23835 staticpro (&Qoverriding_terminal_local_map);
23836 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23838 staticpro (&Qoverriding_local_map);
23839 Qoverriding_local_map = intern ("overriding-local-map");
23841 staticpro (&Qwindow_scroll_functions);
23842 Qwindow_scroll_functions = intern ("window-scroll-functions");
23844 staticpro (&Qredisplay_end_trigger_functions);
23845 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23847 staticpro (&Qinhibit_point_motion_hooks);
23848 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23850 QCdata = intern (":data");
23851 staticpro (&QCdata);
23852 Qdisplay = intern ("display");
23853 staticpro (&Qdisplay);
23854 Qspace_width = intern ("space-width");
23855 staticpro (&Qspace_width);
23856 Qraise = intern ("raise");
23857 staticpro (&Qraise);
23858 Qslice = intern ("slice");
23859 staticpro (&Qslice);
23860 Qspace = intern ("space");
23861 staticpro (&Qspace);
23862 Qmargin = intern ("margin");
23863 staticpro (&Qmargin);
23864 Qpointer = intern ("pointer");
23865 staticpro (&Qpointer);
23866 Qleft_margin = intern ("left-margin");
23867 staticpro (&Qleft_margin);
23868 Qright_margin = intern ("right-margin");
23869 staticpro (&Qright_margin);
23870 Qcenter = intern ("center");
23871 staticpro (&Qcenter);
23872 Qline_height = intern ("line-height");
23873 staticpro (&Qline_height);
23874 QCalign_to = intern (":align-to");
23875 staticpro (&QCalign_to);
23876 QCrelative_width = intern (":relative-width");
23877 staticpro (&QCrelative_width);
23878 QCrelative_height = intern (":relative-height");
23879 staticpro (&QCrelative_height);
23880 QCeval = intern (":eval");
23881 staticpro (&QCeval);
23882 QCpropertize = intern (":propertize");
23883 staticpro (&QCpropertize);
23884 QCfile = intern (":file");
23885 staticpro (&QCfile);
23886 Qfontified = intern ("fontified");
23887 staticpro (&Qfontified);
23888 Qfontification_functions = intern ("fontification-functions");
23889 staticpro (&Qfontification_functions);
23890 Qtrailing_whitespace = intern ("trailing-whitespace");
23891 staticpro (&Qtrailing_whitespace);
23892 Qescape_glyph = intern ("escape-glyph");
23893 staticpro (&Qescape_glyph);
23894 Qnobreak_space = intern ("nobreak-space");
23895 staticpro (&Qnobreak_space);
23896 Qimage = intern ("image");
23897 staticpro (&Qimage);
23898 QCmap = intern (":map");
23899 staticpro (&QCmap);
23900 QCpointer = intern (":pointer");
23901 staticpro (&QCpointer);
23902 Qrect = intern ("rect");
23903 staticpro (&Qrect);
23904 Qcircle = intern ("circle");
23905 staticpro (&Qcircle);
23906 Qpoly = intern ("poly");
23907 staticpro (&Qpoly);
23908 Qmessage_truncate_lines = intern ("message-truncate-lines");
23909 staticpro (&Qmessage_truncate_lines);
23910 Qgrow_only = intern ("grow-only");
23911 staticpro (&Qgrow_only);
23912 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23913 staticpro (&Qinhibit_menubar_update);
23914 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23915 staticpro (&Qinhibit_eval_during_redisplay);
23916 Qposition = intern ("position");
23917 staticpro (&Qposition);
23918 Qbuffer_position = intern ("buffer-position");
23919 staticpro (&Qbuffer_position);
23920 Qobject = intern ("object");
23921 staticpro (&Qobject);
23922 Qbar = intern ("bar");
23923 staticpro (&Qbar);
23924 Qhbar = intern ("hbar");
23925 staticpro (&Qhbar);
23926 Qbox = intern ("box");
23927 staticpro (&Qbox);
23928 Qhollow = intern ("hollow");
23929 staticpro (&Qhollow);
23930 Qhand = intern ("hand");
23931 staticpro (&Qhand);
23932 Qarrow = intern ("arrow");
23933 staticpro (&Qarrow);
23934 Qtext = intern ("text");
23935 staticpro (&Qtext);
23936 Qrisky_local_variable = intern ("risky-local-variable");
23937 staticpro (&Qrisky_local_variable);
23938 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23939 staticpro (&Qinhibit_free_realized_faces);
23941 list_of_error = Fcons (Fcons (intern ("error"),
23942 Fcons (intern ("void-variable"), Qnil)),
23943 Qnil);
23944 staticpro (&list_of_error);
23946 Qlast_arrow_position = intern ("last-arrow-position");
23947 staticpro (&Qlast_arrow_position);
23948 Qlast_arrow_string = intern ("last-arrow-string");
23949 staticpro (&Qlast_arrow_string);
23951 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23952 staticpro (&Qoverlay_arrow_string);
23953 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23954 staticpro (&Qoverlay_arrow_bitmap);
23956 echo_buffer[0] = echo_buffer[1] = Qnil;
23957 staticpro (&echo_buffer[0]);
23958 staticpro (&echo_buffer[1]);
23960 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23961 staticpro (&echo_area_buffer[0]);
23962 staticpro (&echo_area_buffer[1]);
23964 Vmessages_buffer_name = build_string ("*Messages*");
23965 staticpro (&Vmessages_buffer_name);
23967 mode_line_proptrans_alist = Qnil;
23968 staticpro (&mode_line_proptrans_alist);
23969 mode_line_string_list = Qnil;
23970 staticpro (&mode_line_string_list);
23971 mode_line_string_face = Qnil;
23972 staticpro (&mode_line_string_face);
23973 mode_line_string_face_prop = Qnil;
23974 staticpro (&mode_line_string_face_prop);
23975 Vmode_line_unwind_vector = Qnil;
23976 staticpro (&Vmode_line_unwind_vector);
23978 help_echo_string = Qnil;
23979 staticpro (&help_echo_string);
23980 help_echo_object = Qnil;
23981 staticpro (&help_echo_object);
23982 help_echo_window = Qnil;
23983 staticpro (&help_echo_window);
23984 previous_help_echo_string = Qnil;
23985 staticpro (&previous_help_echo_string);
23986 help_echo_pos = -1;
23988 #ifdef HAVE_WINDOW_SYSTEM
23989 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23990 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23991 For example, if a block cursor is over a tab, it will be drawn as
23992 wide as that tab on the display. */);
23993 x_stretch_cursor_p = 0;
23994 #endif
23996 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23997 doc: /* *Non-nil means highlight trailing whitespace.
23998 The face used for trailing whitespace is `trailing-whitespace'. */);
23999 Vshow_trailing_whitespace = Qnil;
24001 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24002 doc: /* *Control highlighting of nobreak space and soft hyphen.
24003 A value of t means highlight the character itself (for nobreak space,
24004 use face `nobreak-space').
24005 A value of nil means no highlighting.
24006 Other values mean display the escape glyph followed by an ordinary
24007 space or ordinary hyphen. */);
24008 Vnobreak_char_display = Qt;
24010 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24011 doc: /* *The pointer shape to show in void text areas.
24012 A value of nil means to show the text pointer. Other options are `arrow',
24013 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24014 Vvoid_text_area_pointer = Qarrow;
24016 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24017 doc: /* Non-nil means don't actually do any redisplay.
24018 This is used for internal purposes. */);
24019 Vinhibit_redisplay = Qnil;
24021 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24022 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24023 Vglobal_mode_string = Qnil;
24025 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24026 doc: /* Marker for where to display an arrow on top of the buffer text.
24027 This must be the beginning of a line in order to work.
24028 See also `overlay-arrow-string'. */);
24029 Voverlay_arrow_position = Qnil;
24031 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24032 doc: /* String to display as an arrow in non-window frames.
24033 See also `overlay-arrow-position'. */);
24034 Voverlay_arrow_string = build_string ("=>");
24036 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24037 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24038 The symbols on this list are examined during redisplay to determine
24039 where to display overlay arrows. */);
24040 Voverlay_arrow_variable_list
24041 = Fcons (intern ("overlay-arrow-position"), Qnil);
24043 DEFVAR_INT ("scroll-step", &scroll_step,
24044 doc: /* *The number of lines to try scrolling a window by when point moves out.
24045 If that fails to bring point back on frame, point is centered instead.
24046 If this is zero, point is always centered after it moves off frame.
24047 If you want scrolling to always be a line at a time, you should set
24048 `scroll-conservatively' to a large value rather than set this to 1. */);
24050 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24051 doc: /* *Scroll up to this many lines, to bring point back on screen.
24052 A value of zero means to scroll the text to center point vertically
24053 in the window. */);
24054 scroll_conservatively = 0;
24056 DEFVAR_INT ("scroll-margin", &scroll_margin,
24057 doc: /* *Number of lines of margin at the top and bottom of a window.
24058 Recenter the window whenever point gets within this many lines
24059 of the top or bottom of the window. */);
24060 scroll_margin = 0;
24062 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24063 doc: /* Pixels per inch value for non-window system displays.
24064 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24065 Vdisplay_pixels_per_inch = make_float (72.0);
24067 #if GLYPH_DEBUG
24068 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24069 #endif
24071 DEFVAR_BOOL ("truncate-partial-width-windows",
24072 &truncate_partial_width_windows,
24073 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24074 truncate_partial_width_windows = 1;
24076 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24077 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24078 Any other value means to use the appropriate face, `mode-line',
24079 `header-line', or `menu' respectively. */);
24080 mode_line_inverse_video = 1;
24082 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24083 doc: /* *Maximum buffer size for which line number should be displayed.
24084 If the buffer is bigger than this, the line number does not appear
24085 in the mode line. A value of nil means no limit. */);
24086 Vline_number_display_limit = Qnil;
24088 DEFVAR_INT ("line-number-display-limit-width",
24089 &line_number_display_limit_width,
24090 doc: /* *Maximum line width (in characters) for line number display.
24091 If the average length of the lines near point is bigger than this, then the
24092 line number may be omitted from the mode line. */);
24093 line_number_display_limit_width = 200;
24095 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24096 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24097 highlight_nonselected_windows = 0;
24099 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24100 doc: /* Non-nil if more than one frame is visible on this display.
24101 Minibuffer-only frames don't count, but iconified frames do.
24102 This variable is not guaranteed to be accurate except while processing
24103 `frame-title-format' and `icon-title-format'. */);
24105 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24106 doc: /* Template for displaying the title bar of visible frames.
24107 \(Assuming the window manager supports this feature.)
24109 This variable has the same structure as `mode-line-format', except that
24110 the %c and %l constructs are ignored. It is used only on frames for
24111 which no explicit name has been set \(see `modify-frame-parameters'). */);
24113 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24114 doc: /* Template for displaying the title bar of an iconified frame.
24115 \(Assuming the window manager supports this feature.)
24116 This variable has the same structure as `mode-line-format' (which see),
24117 and is used only on frames for which no explicit name has been set
24118 \(see `modify-frame-parameters'). */);
24119 Vicon_title_format
24120 = Vframe_title_format
24121 = Fcons (intern ("multiple-frames"),
24122 Fcons (build_string ("%b"),
24123 Fcons (Fcons (empty_unibyte_string,
24124 Fcons (intern ("invocation-name"),
24125 Fcons (build_string ("@"),
24126 Fcons (intern ("system-name"),
24127 Qnil)))),
24128 Qnil)));
24130 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24131 doc: /* Maximum number of lines to keep in the message log buffer.
24132 If nil, disable message logging. If t, log messages but don't truncate
24133 the buffer when it becomes large. */);
24134 Vmessage_log_max = make_number (100);
24136 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24137 doc: /* Functions called before redisplay, if window sizes have changed.
24138 The value should be a list of functions that take one argument.
24139 Just before redisplay, for each frame, if any of its windows have changed
24140 size since the last redisplay, or have been split or deleted,
24141 all the functions in the list are called, with the frame as argument. */);
24142 Vwindow_size_change_functions = Qnil;
24144 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24145 doc: /* List of functions to call before redisplaying a window with scrolling.
24146 Each function is called with two arguments, the window
24147 and its new display-start position. Note that the value of `window-end'
24148 is not valid when these functions are called. */);
24149 Vwindow_scroll_functions = Qnil;
24151 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24152 doc: /* Functions called when redisplay of a window reaches the end trigger.
24153 Each function is called with two arguments, the window and the end trigger value.
24154 See `set-window-redisplay-end-trigger'. */);
24155 Vredisplay_end_trigger_functions = Qnil;
24157 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24158 doc: /* *Non-nil means autoselect window with mouse pointer.
24159 If nil, do not autoselect windows.
24160 A positive number means delay autoselection by that many seconds: a
24161 window is autoselected only after the mouse has remained in that
24162 window for the duration of the delay.
24163 A negative number has a similar effect, but causes windows to be
24164 autoselected only after the mouse has stopped moving. \(Because of
24165 the way Emacs compares mouse events, you will occasionally wait twice
24166 that time before the window gets selected.\)
24167 Any other value means to autoselect window instantaneously when the
24168 mouse pointer enters it.
24170 Autoselection selects the minibuffer only if it is active, and never
24171 unselects the minibuffer if it is active. */);
24172 Vmouse_autoselect_window = Qnil;
24174 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24175 doc: /* *Non-nil means automatically resize tool-bars.
24176 This dynamically changes the tool-bar's height to the minimum height
24177 that is needed to make all tool-bar items visible.
24178 If value is `grow-only', the tool-bar's height is only increased
24179 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24180 Vauto_resize_tool_bars = Qt;
24182 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24183 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24184 auto_raise_tool_bar_buttons_p = 1;
24186 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24187 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24188 make_cursor_line_fully_visible_p = 1;
24190 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24191 doc: /* *Border below tool-bar in pixels.
24192 If an integer, use it as the height of the border.
24193 If it is one of `internal-border-width' or `border-width', use the
24194 value of the corresponding frame parameter.
24195 Otherwise, no border is added below the tool-bar. */);
24196 Vtool_bar_border = Qinternal_border_width;
24198 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24199 doc: /* *Margin around tool-bar buttons in pixels.
24200 If an integer, use that for both horizontal and vertical margins.
24201 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24202 HORZ specifying the horizontal margin, and VERT specifying the
24203 vertical margin. */);
24204 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24206 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24207 doc: /* *Relief thickness of tool-bar buttons. */);
24208 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24210 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24211 doc: /* List of functions to call to fontify regions of text.
24212 Each function is called with one argument POS. Functions must
24213 fontify a region starting at POS in the current buffer, and give
24214 fontified regions the property `fontified'. */);
24215 Vfontification_functions = Qnil;
24216 Fmake_variable_buffer_local (Qfontification_functions);
24218 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24219 &unibyte_display_via_language_environment,
24220 doc: /* *Non-nil means display unibyte text according to language environment.
24221 Specifically this means that unibyte non-ASCII characters
24222 are displayed by converting them to the equivalent multibyte characters
24223 according to the current language environment. As a result, they are
24224 displayed according to the current fontset. */);
24225 unibyte_display_via_language_environment = 0;
24227 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24228 doc: /* *Maximum height for resizing mini-windows.
24229 If a float, it specifies a fraction of the mini-window frame's height.
24230 If an integer, it specifies a number of lines. */);
24231 Vmax_mini_window_height = make_float (0.25);
24233 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24234 doc: /* *How to resize mini-windows.
24235 A value of nil means don't automatically resize mini-windows.
24236 A value of t means resize them to fit the text displayed in them.
24237 A value of `grow-only', the default, means let mini-windows grow
24238 only, until their display becomes empty, at which point the windows
24239 go back to their normal size. */);
24240 Vresize_mini_windows = Qgrow_only;
24242 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24243 doc: /* Alist specifying how to blink the cursor off.
24244 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24245 `cursor-type' frame-parameter or variable equals ON-STATE,
24246 comparing using `equal', Emacs uses OFF-STATE to specify
24247 how to blink it off. ON-STATE and OFF-STATE are values for
24248 the `cursor-type' frame parameter.
24250 If a frame's ON-STATE has no entry in this list,
24251 the frame's other specifications determine how to blink the cursor off. */);
24252 Vblink_cursor_alist = Qnil;
24254 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24255 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24256 automatic_hscrolling_p = 1;
24258 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24259 doc: /* *How many columns away from the window edge point is allowed to get
24260 before automatic hscrolling will horizontally scroll the window. */);
24261 hscroll_margin = 5;
24263 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24264 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24265 When point is less than `hscroll-margin' columns from the window
24266 edge, automatic hscrolling will scroll the window by the amount of columns
24267 determined by this variable. If its value is a positive integer, scroll that
24268 many columns. If it's a positive floating-point number, it specifies the
24269 fraction of the window's width to scroll. If it's nil or zero, point will be
24270 centered horizontally after the scroll. Any other value, including negative
24271 numbers, are treated as if the value were zero.
24273 Automatic hscrolling always moves point outside the scroll margin, so if
24274 point was more than scroll step columns inside the margin, the window will
24275 scroll more than the value given by the scroll step.
24277 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24278 and `scroll-right' overrides this variable's effect. */);
24279 Vhscroll_step = make_number (0);
24281 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24282 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24283 Bind this around calls to `message' to let it take effect. */);
24284 message_truncate_lines = 0;
24286 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24287 doc: /* Normal hook run to update the menu bar definitions.
24288 Redisplay runs this hook before it redisplays the menu bar.
24289 This is used to update submenus such as Buffers,
24290 whose contents depend on various data. */);
24291 Vmenu_bar_update_hook = Qnil;
24293 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24294 doc: /* Frame for which we are updating a menu.
24295 The enable predicate for a menu binding should check this variable. */);
24296 Vmenu_updating_frame = Qnil;
24298 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24299 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24300 inhibit_menubar_update = 0;
24302 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24303 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24304 inhibit_eval_during_redisplay = 0;
24306 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24307 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24308 inhibit_free_realized_faces = 0;
24310 #if GLYPH_DEBUG
24311 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24312 doc: /* Inhibit try_window_id display optimization. */);
24313 inhibit_try_window_id = 0;
24315 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24316 doc: /* Inhibit try_window_reusing display optimization. */);
24317 inhibit_try_window_reusing = 0;
24319 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24320 doc: /* Inhibit try_cursor_movement display optimization. */);
24321 inhibit_try_cursor_movement = 0;
24322 #endif /* GLYPH_DEBUG */
24324 DEFVAR_INT ("overline-margin", &overline_margin,
24325 doc: /* *Space between overline and text, in pixels.
24326 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24327 margin to the caracter height. */);
24328 overline_margin = 2;
24332 /* Initialize this module when Emacs starts. */
24334 void
24335 init_xdisp ()
24337 Lisp_Object root_window;
24338 struct window *mini_w;
24340 current_header_line_height = current_mode_line_height = -1;
24342 CHARPOS (this_line_start_pos) = 0;
24344 mini_w = XWINDOW (minibuf_window);
24345 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24347 if (!noninteractive)
24349 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24350 int i;
24352 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24353 set_window_height (root_window,
24354 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24356 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24357 set_window_height (minibuf_window, 1, 0);
24359 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24360 mini_w->total_cols = make_number (FRAME_COLS (f));
24362 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24363 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24364 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24366 /* The default ellipsis glyphs `...'. */
24367 for (i = 0; i < 3; ++i)
24368 default_invis_vector[i] = make_number ('.');
24372 /* Allocate the buffer for frame titles.
24373 Also used for `format-mode-line'. */
24374 int size = 100;
24375 mode_line_noprop_buf = (char *) xmalloc (size);
24376 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24377 mode_line_noprop_ptr = mode_line_noprop_buf;
24378 mode_line_target = MODE_LINE_DISPLAY;
24381 help_echo_showing_p = 0;
24385 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24386 (do not change this comment) */