Correct typo.
[emacs.git] / src / xdisp.c
blob92c0af8c5ed1bf495d798e2118af6d65f6fd6c81
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);
3722 /* Let the ellipsis display before
3723 considering any properties of the following char.
3724 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3725 handled = HANDLED_RETURN;
3730 return handled;
3734 /* Make iterator IT return `...' next.
3735 Replaces LEN characters from buffer. */
3737 static void
3738 setup_for_ellipsis (it, len)
3739 struct it *it;
3740 int len;
3742 /* Use the display table definition for `...'. Invalid glyphs
3743 will be handled by the method returning elements from dpvec. */
3744 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3746 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3747 it->dpvec = v->contents;
3748 it->dpend = v->contents + v->size;
3750 else
3752 /* Default `...'. */
3753 it->dpvec = default_invis_vector;
3754 it->dpend = default_invis_vector + 3;
3757 it->dpvec_char_len = len;
3758 it->current.dpvec_index = 0;
3759 it->dpvec_face_id = -1;
3761 /* Remember the current face id in case glyphs specify faces.
3762 IT's face is restored in set_iterator_to_next.
3763 saved_face_id was set to preceding char's face in handle_stop. */
3764 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3765 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3767 it->method = GET_FROM_DISPLAY_VECTOR;
3768 it->ellipsis_p = 1;
3773 /***********************************************************************
3774 'display' property
3775 ***********************************************************************/
3777 /* Set up iterator IT from `display' property at its current position.
3778 Called from handle_stop.
3779 We return HANDLED_RETURN if some part of the display property
3780 overrides the display of the buffer text itself.
3781 Otherwise we return HANDLED_NORMALLY. */
3783 static enum prop_handled
3784 handle_display_prop (it)
3785 struct it *it;
3787 Lisp_Object prop, object;
3788 struct text_pos *position;
3789 /* Nonzero if some property replaces the display of the text itself. */
3790 int display_replaced_p = 0;
3792 if (STRINGP (it->string))
3794 object = it->string;
3795 position = &it->current.string_pos;
3797 else
3799 XSETWINDOW (object, it->w);
3800 position = &it->current.pos;
3803 /* Reset those iterator values set from display property values. */
3804 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3805 it->space_width = Qnil;
3806 it->font_height = Qnil;
3807 it->voffset = 0;
3809 /* We don't support recursive `display' properties, i.e. string
3810 values that have a string `display' property, that have a string
3811 `display' property etc. */
3812 if (!it->string_from_display_prop_p)
3813 it->area = TEXT_AREA;
3815 prop = Fget_char_property (make_number (position->charpos),
3816 Qdisplay, object);
3817 if (NILP (prop))
3818 return HANDLED_NORMALLY;
3820 if (!STRINGP (it->string))
3821 object = it->w->buffer;
3823 if (CONSP (prop)
3824 /* Simple properties. */
3825 && !EQ (XCAR (prop), Qimage)
3826 && !EQ (XCAR (prop), Qspace)
3827 && !EQ (XCAR (prop), Qwhen)
3828 && !EQ (XCAR (prop), Qslice)
3829 && !EQ (XCAR (prop), Qspace_width)
3830 && !EQ (XCAR (prop), Qheight)
3831 && !EQ (XCAR (prop), Qraise)
3832 /* Marginal area specifications. */
3833 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3834 && !EQ (XCAR (prop), Qleft_fringe)
3835 && !EQ (XCAR (prop), Qright_fringe)
3836 && !NILP (XCAR (prop)))
3838 for (; CONSP (prop); prop = XCDR (prop))
3840 if (handle_single_display_spec (it, XCAR (prop), object,
3841 position, display_replaced_p))
3843 display_replaced_p = 1;
3844 /* If some text in a string is replaced, `position' no
3845 longer points to the position of `object'. */
3846 if (STRINGP (object))
3847 break;
3851 else if (VECTORP (prop))
3853 int i;
3854 for (i = 0; i < ASIZE (prop); ++i)
3855 if (handle_single_display_spec (it, AREF (prop, i), object,
3856 position, display_replaced_p))
3858 display_replaced_p = 1;
3859 /* If some text in a string is replaced, `position' no
3860 longer points to the position of `object'. */
3861 if (STRINGP (object))
3862 break;
3865 else
3867 int ret = handle_single_display_spec (it, prop, object, position, 0);
3868 if (ret < 0) /* Replaced by "", i.e. nothing. */
3869 return HANDLED_RECOMPUTE_PROPS;
3870 if (ret)
3871 display_replaced_p = 1;
3874 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3878 /* Value is the position of the end of the `display' property starting
3879 at START_POS in OBJECT. */
3881 static struct text_pos
3882 display_prop_end (it, object, start_pos)
3883 struct it *it;
3884 Lisp_Object object;
3885 struct text_pos start_pos;
3887 Lisp_Object end;
3888 struct text_pos end_pos;
3890 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3891 Qdisplay, object, Qnil);
3892 CHARPOS (end_pos) = XFASTINT (end);
3893 if (STRINGP (object))
3894 compute_string_pos (&end_pos, start_pos, it->string);
3895 else
3896 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3898 return end_pos;
3902 /* Set up IT from a single `display' specification PROP. OBJECT
3903 is the object in which the `display' property was found. *POSITION
3904 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3905 means that we previously saw a display specification which already
3906 replaced text display with something else, for example an image;
3907 we ignore such properties after the first one has been processed.
3909 If PROP is a `space' or `image' specification, and in some other
3910 cases too, set *POSITION to the position where the `display'
3911 property ends.
3913 Value is non-zero if something was found which replaces the display
3914 of buffer or string text. Specifically, the value is -1 if that
3915 "something" is "nothing". */
3917 static int
3918 handle_single_display_spec (it, spec, object, position,
3919 display_replaced_before_p)
3920 struct it *it;
3921 Lisp_Object spec;
3922 Lisp_Object object;
3923 struct text_pos *position;
3924 int display_replaced_before_p;
3926 Lisp_Object form;
3927 Lisp_Object location, value;
3928 struct text_pos start_pos, save_pos;
3929 int valid_p;
3931 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3932 If the result is non-nil, use VALUE instead of SPEC. */
3933 form = Qt;
3934 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3936 spec = XCDR (spec);
3937 if (!CONSP (spec))
3938 return 0;
3939 form = XCAR (spec);
3940 spec = XCDR (spec);
3943 if (!NILP (form) && !EQ (form, Qt))
3945 int count = SPECPDL_INDEX ();
3946 struct gcpro gcpro1;
3948 /* Bind `object' to the object having the `display' property, a
3949 buffer or string. Bind `position' to the position in the
3950 object where the property was found, and `buffer-position'
3951 to the current position in the buffer. */
3952 specbind (Qobject, object);
3953 specbind (Qposition, make_number (CHARPOS (*position)));
3954 specbind (Qbuffer_position,
3955 make_number (STRINGP (object)
3956 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3957 GCPRO1 (form);
3958 form = safe_eval (form);
3959 UNGCPRO;
3960 unbind_to (count, Qnil);
3963 if (NILP (form))
3964 return 0;
3966 /* Handle `(height HEIGHT)' specifications. */
3967 if (CONSP (spec)
3968 && EQ (XCAR (spec), Qheight)
3969 && CONSP (XCDR (spec)))
3971 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3972 return 0;
3974 it->font_height = XCAR (XCDR (spec));
3975 if (!NILP (it->font_height))
3977 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3978 int new_height = -1;
3980 if (CONSP (it->font_height)
3981 && (EQ (XCAR (it->font_height), Qplus)
3982 || EQ (XCAR (it->font_height), Qminus))
3983 && CONSP (XCDR (it->font_height))
3984 && INTEGERP (XCAR (XCDR (it->font_height))))
3986 /* `(+ N)' or `(- N)' where N is an integer. */
3987 int steps = XINT (XCAR (XCDR (it->font_height)));
3988 if (EQ (XCAR (it->font_height), Qplus))
3989 steps = - steps;
3990 it->face_id = smaller_face (it->f, it->face_id, steps);
3992 else if (FUNCTIONP (it->font_height))
3994 /* Call function with current height as argument.
3995 Value is the new height. */
3996 Lisp_Object height;
3997 height = safe_call1 (it->font_height,
3998 face->lface[LFACE_HEIGHT_INDEX]);
3999 if (NUMBERP (height))
4000 new_height = XFLOATINT (height);
4002 else if (NUMBERP (it->font_height))
4004 /* Value is a multiple of the canonical char height. */
4005 struct face *face;
4007 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4008 new_height = (XFLOATINT (it->font_height)
4009 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4011 else
4013 /* Evaluate IT->font_height with `height' bound to the
4014 current specified height to get the new height. */
4015 int count = SPECPDL_INDEX ();
4017 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4018 value = safe_eval (it->font_height);
4019 unbind_to (count, Qnil);
4021 if (NUMBERP (value))
4022 new_height = XFLOATINT (value);
4025 if (new_height > 0)
4026 it->face_id = face_with_height (it->f, it->face_id, new_height);
4029 return 0;
4032 /* Handle `(space_width WIDTH)'. */
4033 if (CONSP (spec)
4034 && EQ (XCAR (spec), Qspace_width)
4035 && CONSP (XCDR (spec)))
4037 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4038 return 0;
4040 value = XCAR (XCDR (spec));
4041 if (NUMBERP (value) && XFLOATINT (value) > 0)
4042 it->space_width = value;
4044 return 0;
4047 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4048 if (CONSP (spec)
4049 && EQ (XCAR (spec), Qslice))
4051 Lisp_Object tem;
4053 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4054 return 0;
4056 if (tem = XCDR (spec), CONSP (tem))
4058 it->slice.x = XCAR (tem);
4059 if (tem = XCDR (tem), CONSP (tem))
4061 it->slice.y = XCAR (tem);
4062 if (tem = XCDR (tem), CONSP (tem))
4064 it->slice.width = XCAR (tem);
4065 if (tem = XCDR (tem), CONSP (tem))
4066 it->slice.height = XCAR (tem);
4071 return 0;
4074 /* Handle `(raise FACTOR)'. */
4075 if (CONSP (spec)
4076 && EQ (XCAR (spec), Qraise)
4077 && CONSP (XCDR (spec)))
4079 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4080 return 0;
4082 #ifdef HAVE_WINDOW_SYSTEM
4083 value = XCAR (XCDR (spec));
4084 if (NUMBERP (value))
4086 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4087 it->voffset = - (XFLOATINT (value)
4088 * (FONT_HEIGHT (face->font)));
4090 #endif /* HAVE_WINDOW_SYSTEM */
4092 return 0;
4095 /* Don't handle the other kinds of display specifications
4096 inside a string that we got from a `display' property. */
4097 if (it->string_from_display_prop_p)
4098 return 0;
4100 /* Characters having this form of property are not displayed, so
4101 we have to find the end of the property. */
4102 start_pos = *position;
4103 *position = display_prop_end (it, object, start_pos);
4104 value = Qnil;
4106 /* Stop the scan at that end position--we assume that all
4107 text properties change there. */
4108 it->stop_charpos = position->charpos;
4110 /* Handle `(left-fringe BITMAP [FACE])'
4111 and `(right-fringe BITMAP [FACE])'. */
4112 if (CONSP (spec)
4113 && (EQ (XCAR (spec), Qleft_fringe)
4114 || EQ (XCAR (spec), Qright_fringe))
4115 && CONSP (XCDR (spec)))
4117 int face_id = DEFAULT_FACE_ID;
4118 int fringe_bitmap;
4120 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4121 /* If we return here, POSITION has been advanced
4122 across the text with this property. */
4123 return 0;
4125 #ifdef HAVE_WINDOW_SYSTEM
4126 value = XCAR (XCDR (spec));
4127 if (!SYMBOLP (value)
4128 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4129 /* If we return here, POSITION has been advanced
4130 across the text with this property. */
4131 return 0;
4133 if (CONSP (XCDR (XCDR (spec))))
4135 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4136 int face_id2 = lookup_derived_face (it->f, face_name,
4137 'A', FRINGE_FACE_ID, 0);
4138 if (face_id2 >= 0)
4139 face_id = face_id2;
4142 /* Save current settings of IT so that we can restore them
4143 when we are finished with the glyph property value. */
4145 save_pos = it->position;
4146 it->position = *position;
4147 push_it (it);
4148 it->position = save_pos;
4150 it->area = TEXT_AREA;
4151 it->what = IT_IMAGE;
4152 it->image_id = -1; /* no image */
4153 it->position = start_pos;
4154 it->object = NILP (object) ? it->w->buffer : object;
4155 it->method = GET_FROM_IMAGE;
4156 it->face_id = face_id;
4158 /* Say that we haven't consumed the characters with
4159 `display' property yet. The call to pop_it in
4160 set_iterator_to_next will clean this up. */
4161 *position = start_pos;
4163 if (EQ (XCAR (spec), Qleft_fringe))
4165 it->left_user_fringe_bitmap = fringe_bitmap;
4166 it->left_user_fringe_face_id = face_id;
4168 else
4170 it->right_user_fringe_bitmap = fringe_bitmap;
4171 it->right_user_fringe_face_id = face_id;
4173 #endif /* HAVE_WINDOW_SYSTEM */
4174 return 1;
4177 /* Prepare to handle `((margin left-margin) ...)',
4178 `((margin right-margin) ...)' and `((margin nil) ...)'
4179 prefixes for display specifications. */
4180 location = Qunbound;
4181 if (CONSP (spec) && CONSP (XCAR (spec)))
4183 Lisp_Object tem;
4185 value = XCDR (spec);
4186 if (CONSP (value))
4187 value = XCAR (value);
4189 tem = XCAR (spec);
4190 if (EQ (XCAR (tem), Qmargin)
4191 && (tem = XCDR (tem),
4192 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4193 (NILP (tem)
4194 || EQ (tem, Qleft_margin)
4195 || EQ (tem, Qright_margin))))
4196 location = tem;
4199 if (EQ (location, Qunbound))
4201 location = Qnil;
4202 value = spec;
4205 /* After this point, VALUE is the property after any
4206 margin prefix has been stripped. It must be a string,
4207 an image specification, or `(space ...)'.
4209 LOCATION specifies where to display: `left-margin',
4210 `right-margin' or nil. */
4212 valid_p = (STRINGP (value)
4213 #ifdef HAVE_WINDOW_SYSTEM
4214 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4215 #endif /* not HAVE_WINDOW_SYSTEM */
4216 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4218 if (valid_p && !display_replaced_before_p)
4220 /* Save current settings of IT so that we can restore them
4221 when we are finished with the glyph property value. */
4222 save_pos = it->position;
4223 it->position = *position;
4224 push_it (it);
4225 it->position = save_pos;
4227 if (NILP (location))
4228 it->area = TEXT_AREA;
4229 else if (EQ (location, Qleft_margin))
4230 it->area = LEFT_MARGIN_AREA;
4231 else
4232 it->area = RIGHT_MARGIN_AREA;
4234 if (STRINGP (value))
4236 if (SCHARS (value) == 0)
4238 pop_it (it);
4239 return -1; /* Replaced by "", i.e. nothing. */
4241 it->string = value;
4242 it->multibyte_p = STRING_MULTIBYTE (it->string);
4243 it->current.overlay_string_index = -1;
4244 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4245 it->end_charpos = it->string_nchars = SCHARS (it->string);
4246 it->method = GET_FROM_STRING;
4247 it->stop_charpos = 0;
4248 it->string_from_display_prop_p = 1;
4249 /* Say that we haven't consumed the characters with
4250 `display' property yet. The call to pop_it in
4251 set_iterator_to_next will clean this up. */
4252 if (BUFFERP (object))
4253 it->current.pos = start_pos;
4255 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4257 it->method = GET_FROM_STRETCH;
4258 it->object = value;
4259 it->position = start_pos;
4260 if (BUFFERP (object))
4261 it->current.pos = start_pos;
4263 #ifdef HAVE_WINDOW_SYSTEM
4264 else
4266 it->what = IT_IMAGE;
4267 it->image_id = lookup_image (it->f, value);
4268 it->position = start_pos;
4269 it->object = NILP (object) ? it->w->buffer : object;
4270 it->method = GET_FROM_IMAGE;
4272 /* Say that we haven't consumed the characters with
4273 `display' property yet. The call to pop_it in
4274 set_iterator_to_next will clean this up. */
4275 if (BUFFERP (object))
4276 it->current.pos = start_pos;
4278 #endif /* HAVE_WINDOW_SYSTEM */
4280 return 1;
4283 /* Invalid property or property not supported. Restore
4284 POSITION to what it was before. */
4285 *position = start_pos;
4286 return 0;
4290 /* Check if SPEC is a display specification value whose text should be
4291 treated as intangible. */
4293 static int
4294 single_display_spec_intangible_p (prop)
4295 Lisp_Object prop;
4297 /* Skip over `when FORM'. */
4298 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4300 prop = XCDR (prop);
4301 if (!CONSP (prop))
4302 return 0;
4303 prop = XCDR (prop);
4306 if (STRINGP (prop))
4307 return 1;
4309 if (!CONSP (prop))
4310 return 0;
4312 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4313 we don't need to treat text as intangible. */
4314 if (EQ (XCAR (prop), Qmargin))
4316 prop = XCDR (prop);
4317 if (!CONSP (prop))
4318 return 0;
4320 prop = XCDR (prop);
4321 if (!CONSP (prop)
4322 || EQ (XCAR (prop), Qleft_margin)
4323 || EQ (XCAR (prop), Qright_margin))
4324 return 0;
4327 return (CONSP (prop)
4328 && (EQ (XCAR (prop), Qimage)
4329 || EQ (XCAR (prop), Qspace)));
4333 /* Check if PROP is a display property value whose text should be
4334 treated as intangible. */
4337 display_prop_intangible_p (prop)
4338 Lisp_Object prop;
4340 if (CONSP (prop)
4341 && CONSP (XCAR (prop))
4342 && !EQ (Qmargin, XCAR (XCAR (prop))))
4344 /* A list of sub-properties. */
4345 while (CONSP (prop))
4347 if (single_display_spec_intangible_p (XCAR (prop)))
4348 return 1;
4349 prop = XCDR (prop);
4352 else if (VECTORP (prop))
4354 /* A vector of sub-properties. */
4355 int i;
4356 for (i = 0; i < ASIZE (prop); ++i)
4357 if (single_display_spec_intangible_p (AREF (prop, i)))
4358 return 1;
4360 else
4361 return single_display_spec_intangible_p (prop);
4363 return 0;
4367 /* Return 1 if PROP is a display sub-property value containing STRING. */
4369 static int
4370 single_display_spec_string_p (prop, string)
4371 Lisp_Object prop, string;
4373 if (EQ (string, prop))
4374 return 1;
4376 /* Skip over `when FORM'. */
4377 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4379 prop = XCDR (prop);
4380 if (!CONSP (prop))
4381 return 0;
4382 prop = XCDR (prop);
4385 if (CONSP (prop))
4386 /* Skip over `margin LOCATION'. */
4387 if (EQ (XCAR (prop), Qmargin))
4389 prop = XCDR (prop);
4390 if (!CONSP (prop))
4391 return 0;
4393 prop = XCDR (prop);
4394 if (!CONSP (prop))
4395 return 0;
4398 return CONSP (prop) && EQ (XCAR (prop), string);
4402 /* Return 1 if STRING appears in the `display' property PROP. */
4404 static int
4405 display_prop_string_p (prop, string)
4406 Lisp_Object prop, string;
4408 if (CONSP (prop)
4409 && CONSP (XCAR (prop))
4410 && !EQ (Qmargin, XCAR (XCAR (prop))))
4412 /* A list of sub-properties. */
4413 while (CONSP (prop))
4415 if (single_display_spec_string_p (XCAR (prop), string))
4416 return 1;
4417 prop = XCDR (prop);
4420 else if (VECTORP (prop))
4422 /* A vector of sub-properties. */
4423 int i;
4424 for (i = 0; i < ASIZE (prop); ++i)
4425 if (single_display_spec_string_p (AREF (prop, i), string))
4426 return 1;
4428 else
4429 return single_display_spec_string_p (prop, string);
4431 return 0;
4435 /* Determine from which buffer position in W's buffer STRING comes
4436 from. AROUND_CHARPOS is an approximate position where it could
4437 be from. Value is the buffer position or 0 if it couldn't be
4438 determined.
4440 W's buffer must be current.
4442 This function is necessary because we don't record buffer positions
4443 in glyphs generated from strings (to keep struct glyph small).
4444 This function may only use code that doesn't eval because it is
4445 called asynchronously from note_mouse_highlight. */
4448 string_buffer_position (w, string, around_charpos)
4449 struct window *w;
4450 Lisp_Object string;
4451 int around_charpos;
4453 Lisp_Object limit, prop, pos;
4454 const int MAX_DISTANCE = 1000;
4455 int found = 0;
4457 pos = make_number (around_charpos);
4458 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4459 while (!found && !EQ (pos, limit))
4461 prop = Fget_char_property (pos, Qdisplay, Qnil);
4462 if (!NILP (prop) && display_prop_string_p (prop, string))
4463 found = 1;
4464 else
4465 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4468 if (!found)
4470 pos = make_number (around_charpos);
4471 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4472 while (!found && !EQ (pos, limit))
4474 prop = Fget_char_property (pos, Qdisplay, Qnil);
4475 if (!NILP (prop) && display_prop_string_p (prop, string))
4476 found = 1;
4477 else
4478 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4479 limit);
4483 return found ? XINT (pos) : 0;
4488 /***********************************************************************
4489 `composition' property
4490 ***********************************************************************/
4492 /* Set up iterator IT from `composition' property at its current
4493 position. Called from handle_stop. */
4495 static enum prop_handled
4496 handle_composition_prop (it)
4497 struct it *it;
4499 Lisp_Object prop, string;
4500 int pos, pos_byte, end;
4501 enum prop_handled handled = HANDLED_NORMALLY;
4503 if (STRINGP (it->string))
4505 pos = IT_STRING_CHARPOS (*it);
4506 pos_byte = IT_STRING_BYTEPOS (*it);
4507 string = it->string;
4509 else
4511 pos = IT_CHARPOS (*it);
4512 pos_byte = IT_BYTEPOS (*it);
4513 string = Qnil;
4516 /* If there's a valid composition and point is not inside of the
4517 composition (in the case that the composition is from the current
4518 buffer), draw a glyph composed from the composition components. */
4519 if (find_composition (pos, -1, &pos, &end, &prop, string)
4520 && COMPOSITION_VALID_P (pos, end, prop)
4521 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4523 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4525 if (id >= 0)
4527 struct composition *cmp = composition_table[id];
4529 if (cmp->glyph_len == 0)
4531 /* No glyph. */
4532 if (STRINGP (it->string))
4534 IT_STRING_CHARPOS (*it) = end;
4535 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4536 end);
4538 else
4540 IT_CHARPOS (*it) = end;
4541 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4543 return HANDLED_RECOMPUTE_PROPS;
4546 it->stop_charpos = end;
4547 push_it (it);
4549 it->method = GET_FROM_COMPOSITION;
4550 it->cmp_id = id;
4551 it->cmp_len = COMPOSITION_LENGTH (prop);
4552 /* For a terminal, draw only the first character of the
4553 components. */
4554 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4555 it->len = (STRINGP (it->string)
4556 ? string_char_to_byte (it->string, end)
4557 : CHAR_TO_BYTE (end)) - pos_byte;
4558 handled = HANDLED_RETURN;
4562 return handled;
4567 /***********************************************************************
4568 Overlay strings
4569 ***********************************************************************/
4571 /* The following structure is used to record overlay strings for
4572 later sorting in load_overlay_strings. */
4574 struct overlay_entry
4576 Lisp_Object overlay;
4577 Lisp_Object string;
4578 int priority;
4579 int after_string_p;
4583 /* Set up iterator IT from overlay strings at its current position.
4584 Called from handle_stop. */
4586 static enum prop_handled
4587 handle_overlay_change (it)
4588 struct it *it;
4590 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4591 return HANDLED_RECOMPUTE_PROPS;
4592 else
4593 return HANDLED_NORMALLY;
4597 /* Set up the next overlay string for delivery by IT, if there is an
4598 overlay string to deliver. Called by set_iterator_to_next when the
4599 end of the current overlay string is reached. If there are more
4600 overlay strings to display, IT->string and
4601 IT->current.overlay_string_index are set appropriately here.
4602 Otherwise IT->string is set to nil. */
4604 static void
4605 next_overlay_string (it)
4606 struct it *it;
4608 ++it->current.overlay_string_index;
4609 if (it->current.overlay_string_index == it->n_overlay_strings)
4611 /* No more overlay strings. Restore IT's settings to what
4612 they were before overlay strings were processed, and
4613 continue to deliver from current_buffer. */
4614 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4616 pop_it (it);
4617 xassert (it->sp > 0
4618 || it->method == GET_FROM_COMPOSITION
4619 || (NILP (it->string)
4620 && it->method == GET_FROM_BUFFER
4621 && it->stop_charpos >= BEGV
4622 && it->stop_charpos <= it->end_charpos));
4623 it->current.overlay_string_index = -1;
4624 it->n_overlay_strings = 0;
4626 /* If we're at the end of the buffer, record that we have
4627 processed the overlay strings there already, so that
4628 next_element_from_buffer doesn't try it again. */
4629 if (IT_CHARPOS (*it) >= it->end_charpos)
4630 it->overlay_strings_at_end_processed_p = 1;
4632 /* If we have to display `...' for invisible text, set
4633 the iterator up for that. */
4634 if (display_ellipsis_p)
4635 setup_for_ellipsis (it, 0);
4637 else
4639 /* There are more overlay strings to process. If
4640 IT->current.overlay_string_index has advanced to a position
4641 where we must load IT->overlay_strings with more strings, do
4642 it. */
4643 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4645 if (it->current.overlay_string_index && i == 0)
4646 load_overlay_strings (it, 0);
4648 /* Initialize IT to deliver display elements from the overlay
4649 string. */
4650 it->string = it->overlay_strings[i];
4651 it->multibyte_p = STRING_MULTIBYTE (it->string);
4652 SET_TEXT_POS (it->current.string_pos, 0, 0);
4653 it->method = GET_FROM_STRING;
4654 it->stop_charpos = 0;
4657 CHECK_IT (it);
4661 /* Compare two overlay_entry structures E1 and E2. Used as a
4662 comparison function for qsort in load_overlay_strings. Overlay
4663 strings for the same position are sorted so that
4665 1. All after-strings come in front of before-strings, except
4666 when they come from the same overlay.
4668 2. Within after-strings, strings are sorted so that overlay strings
4669 from overlays with higher priorities come first.
4671 2. Within before-strings, strings are sorted so that overlay
4672 strings from overlays with higher priorities come last.
4674 Value is analogous to strcmp. */
4677 static int
4678 compare_overlay_entries (e1, e2)
4679 void *e1, *e2;
4681 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4682 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4683 int result;
4685 if (entry1->after_string_p != entry2->after_string_p)
4687 /* Let after-strings appear in front of before-strings if
4688 they come from different overlays. */
4689 if (EQ (entry1->overlay, entry2->overlay))
4690 result = entry1->after_string_p ? 1 : -1;
4691 else
4692 result = entry1->after_string_p ? -1 : 1;
4694 else if (entry1->after_string_p)
4695 /* After-strings sorted in order of decreasing priority. */
4696 result = entry2->priority - entry1->priority;
4697 else
4698 /* Before-strings sorted in order of increasing priority. */
4699 result = entry1->priority - entry2->priority;
4701 return result;
4705 /* Load the vector IT->overlay_strings with overlay strings from IT's
4706 current buffer position, or from CHARPOS if that is > 0. Set
4707 IT->n_overlays to the total number of overlay strings found.
4709 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4710 a time. On entry into load_overlay_strings,
4711 IT->current.overlay_string_index gives the number of overlay
4712 strings that have already been loaded by previous calls to this
4713 function.
4715 IT->add_overlay_start contains an additional overlay start
4716 position to consider for taking overlay strings from, if non-zero.
4717 This position comes into play when the overlay has an `invisible'
4718 property, and both before and after-strings. When we've skipped to
4719 the end of the overlay, because of its `invisible' property, we
4720 nevertheless want its before-string to appear.
4721 IT->add_overlay_start will contain the overlay start position
4722 in this case.
4724 Overlay strings are sorted so that after-string strings come in
4725 front of before-string strings. Within before and after-strings,
4726 strings are sorted by overlay priority. See also function
4727 compare_overlay_entries. */
4729 static void
4730 load_overlay_strings (it, charpos)
4731 struct it *it;
4732 int charpos;
4734 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4735 Lisp_Object overlay, window, str, invisible;
4736 struct Lisp_Overlay *ov;
4737 int start, end;
4738 int size = 20;
4739 int n = 0, i, j, invis_p;
4740 struct overlay_entry *entries
4741 = (struct overlay_entry *) alloca (size * sizeof *entries);
4743 if (charpos <= 0)
4744 charpos = IT_CHARPOS (*it);
4746 /* Append the overlay string STRING of overlay OVERLAY to vector
4747 `entries' which has size `size' and currently contains `n'
4748 elements. AFTER_P non-zero means STRING is an after-string of
4749 OVERLAY. */
4750 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4751 do \
4753 Lisp_Object priority; \
4755 if (n == size) \
4757 int new_size = 2 * size; \
4758 struct overlay_entry *old = entries; \
4759 entries = \
4760 (struct overlay_entry *) alloca (new_size \
4761 * sizeof *entries); \
4762 bcopy (old, entries, size * sizeof *entries); \
4763 size = new_size; \
4766 entries[n].string = (STRING); \
4767 entries[n].overlay = (OVERLAY); \
4768 priority = Foverlay_get ((OVERLAY), Qpriority); \
4769 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4770 entries[n].after_string_p = (AFTER_P); \
4771 ++n; \
4773 while (0)
4775 /* Process overlay before the overlay center. */
4776 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4778 XSETMISC (overlay, ov);
4779 xassert (OVERLAYP (overlay));
4780 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4781 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4783 if (end < charpos)
4784 break;
4786 /* Skip this overlay if it doesn't start or end at IT's current
4787 position. */
4788 if (end != charpos && start != charpos)
4789 continue;
4791 /* Skip this overlay if it doesn't apply to IT->w. */
4792 window = Foverlay_get (overlay, Qwindow);
4793 if (WINDOWP (window) && XWINDOW (window) != it->w)
4794 continue;
4796 /* If the text ``under'' the overlay is invisible, both before-
4797 and after-strings from this overlay are visible; start and
4798 end position are indistinguishable. */
4799 invisible = Foverlay_get (overlay, Qinvisible);
4800 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4802 /* If overlay has a non-empty before-string, record it. */
4803 if ((start == charpos || (end == charpos && invis_p))
4804 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4805 && SCHARS (str))
4806 RECORD_OVERLAY_STRING (overlay, str, 0);
4808 /* If overlay has a non-empty after-string, record it. */
4809 if ((end == charpos || (start == charpos && invis_p))
4810 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4811 && SCHARS (str))
4812 RECORD_OVERLAY_STRING (overlay, str, 1);
4815 /* Process overlays after the overlay center. */
4816 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4818 XSETMISC (overlay, ov);
4819 xassert (OVERLAYP (overlay));
4820 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4821 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4823 if (start > charpos)
4824 break;
4826 /* Skip this overlay if it doesn't start or end at IT's current
4827 position. */
4828 if (end != charpos && start != charpos)
4829 continue;
4831 /* Skip this overlay if it doesn't apply to IT->w. */
4832 window = Foverlay_get (overlay, Qwindow);
4833 if (WINDOWP (window) && XWINDOW (window) != it->w)
4834 continue;
4836 /* If the text ``under'' the overlay is invisible, it has a zero
4837 dimension, and both before- and after-strings apply. */
4838 invisible = Foverlay_get (overlay, Qinvisible);
4839 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4841 /* If overlay has a non-empty before-string, record it. */
4842 if ((start == charpos || (end == charpos && invis_p))
4843 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4844 && SCHARS (str))
4845 RECORD_OVERLAY_STRING (overlay, str, 0);
4847 /* If overlay has a non-empty after-string, record it. */
4848 if ((end == charpos || (start == charpos && invis_p))
4849 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4850 && SCHARS (str))
4851 RECORD_OVERLAY_STRING (overlay, str, 1);
4854 #undef RECORD_OVERLAY_STRING
4856 /* Sort entries. */
4857 if (n > 1)
4858 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4860 /* Record the total number of strings to process. */
4861 it->n_overlay_strings = n;
4863 /* IT->current.overlay_string_index is the number of overlay strings
4864 that have already been consumed by IT. Copy some of the
4865 remaining overlay strings to IT->overlay_strings. */
4866 i = 0;
4867 j = it->current.overlay_string_index;
4868 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4869 it->overlay_strings[i++] = entries[j++].string;
4871 CHECK_IT (it);
4875 /* Get the first chunk of overlay strings at IT's current buffer
4876 position, or at CHARPOS if that is > 0. Value is non-zero if at
4877 least one overlay string was found. */
4879 static int
4880 get_overlay_strings_1 (it, charpos, compute_stop_p)
4881 struct it *it;
4882 int charpos;
4884 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4885 process. This fills IT->overlay_strings with strings, and sets
4886 IT->n_overlay_strings to the total number of strings to process.
4887 IT->pos.overlay_string_index has to be set temporarily to zero
4888 because load_overlay_strings needs this; it must be set to -1
4889 when no overlay strings are found because a zero value would
4890 indicate a position in the first overlay string. */
4891 it->current.overlay_string_index = 0;
4892 load_overlay_strings (it, charpos);
4894 /* If we found overlay strings, set up IT to deliver display
4895 elements from the first one. Otherwise set up IT to deliver
4896 from current_buffer. */
4897 if (it->n_overlay_strings)
4899 /* Make sure we know settings in current_buffer, so that we can
4900 restore meaningful values when we're done with the overlay
4901 strings. */
4902 if (compute_stop_p)
4903 compute_stop_pos (it);
4904 xassert (it->face_id >= 0);
4906 /* Save IT's settings. They are restored after all overlay
4907 strings have been processed. */
4908 xassert (!compute_stop_p || it->sp == 0);
4909 push_it (it);
4911 /* Set up IT to deliver display elements from the first overlay
4912 string. */
4913 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4914 it->string = it->overlay_strings[0];
4915 it->stop_charpos = 0;
4916 xassert (STRINGP (it->string));
4917 it->end_charpos = SCHARS (it->string);
4918 it->multibyte_p = STRING_MULTIBYTE (it->string);
4919 it->method = GET_FROM_STRING;
4920 return 1;
4923 it->current.overlay_string_index = -1;
4924 return 0;
4927 static int
4928 get_overlay_strings (it, charpos)
4929 struct it *it;
4930 int charpos;
4932 it->string = Qnil;
4933 it->method = GET_FROM_BUFFER;
4935 (void) get_overlay_strings_1 (it, charpos, 1);
4937 CHECK_IT (it);
4939 /* Value is non-zero if we found at least one overlay string. */
4940 return STRINGP (it->string);
4945 /***********************************************************************
4946 Saving and restoring state
4947 ***********************************************************************/
4949 /* Save current settings of IT on IT->stack. Called, for example,
4950 before setting up IT for an overlay string, to be able to restore
4951 IT's settings to what they were after the overlay string has been
4952 processed. */
4954 static void
4955 push_it (it)
4956 struct it *it;
4958 struct iterator_stack_entry *p;
4960 xassert (it->sp < IT_STACK_SIZE);
4961 p = it->stack + it->sp;
4963 p->stop_charpos = it->stop_charpos;
4964 xassert (it->face_id >= 0);
4965 p->face_id = it->face_id;
4966 p->string = it->string;
4967 p->method = it->method;
4968 switch (p->method)
4970 case GET_FROM_IMAGE:
4971 p->u.image.object = it->object;
4972 p->u.image.image_id = it->image_id;
4973 p->u.image.slice = it->slice;
4974 break;
4975 case GET_FROM_COMPOSITION:
4976 p->u.comp.object = it->object;
4977 p->u.comp.c = it->c;
4978 p->u.comp.len = it->len;
4979 p->u.comp.cmp_id = it->cmp_id;
4980 p->u.comp.cmp_len = it->cmp_len;
4981 break;
4982 case GET_FROM_STRETCH:
4983 p->u.stretch.object = it->object;
4984 break;
4986 p->position = it->position;
4987 p->current = it->current;
4988 p->end_charpos = it->end_charpos;
4989 p->string_nchars = it->string_nchars;
4990 p->area = it->area;
4991 p->multibyte_p = it->multibyte_p;
4992 p->space_width = it->space_width;
4993 p->font_height = it->font_height;
4994 p->voffset = it->voffset;
4995 p->string_from_display_prop_p = it->string_from_display_prop_p;
4996 p->display_ellipsis_p = 0;
4997 ++it->sp;
5001 /* Restore IT's settings from IT->stack. Called, for example, when no
5002 more overlay strings must be processed, and we return to delivering
5003 display elements from a buffer, or when the end of a string from a
5004 `display' property is reached and we return to delivering display
5005 elements from an overlay string, or from a buffer. */
5007 static void
5008 pop_it (it)
5009 struct it *it;
5011 struct iterator_stack_entry *p;
5013 xassert (it->sp > 0);
5014 --it->sp;
5015 p = it->stack + it->sp;
5016 it->stop_charpos = p->stop_charpos;
5017 it->face_id = p->face_id;
5018 it->current = p->current;
5019 it->position = p->position;
5020 it->string = p->string;
5021 if (NILP (it->string))
5022 SET_TEXT_POS (it->current.string_pos, -1, -1);
5023 it->method = p->method;
5024 switch (it->method)
5026 case GET_FROM_IMAGE:
5027 it->image_id = p->u.image.image_id;
5028 it->object = p->u.image.object;
5029 it->slice = p->u.image.slice;
5030 break;
5031 case GET_FROM_COMPOSITION:
5032 it->object = p->u.comp.object;
5033 it->c = p->u.comp.c;
5034 it->len = p->u.comp.len;
5035 it->cmp_id = p->u.comp.cmp_id;
5036 it->cmp_len = p->u.comp.cmp_len;
5037 break;
5038 case GET_FROM_STRETCH:
5039 it->object = p->u.comp.object;
5040 break;
5041 case GET_FROM_BUFFER:
5042 it->object = it->w->buffer;
5043 break;
5044 case GET_FROM_STRING:
5045 it->object = it->string;
5046 break;
5048 it->end_charpos = p->end_charpos;
5049 it->string_nchars = p->string_nchars;
5050 it->area = p->area;
5051 it->multibyte_p = p->multibyte_p;
5052 it->space_width = p->space_width;
5053 it->font_height = p->font_height;
5054 it->voffset = p->voffset;
5055 it->string_from_display_prop_p = p->string_from_display_prop_p;
5060 /***********************************************************************
5061 Moving over lines
5062 ***********************************************************************/
5064 /* Set IT's current position to the previous line start. */
5066 static void
5067 back_to_previous_line_start (it)
5068 struct it *it;
5070 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5071 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5075 /* Move IT to the next line start.
5077 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5078 we skipped over part of the text (as opposed to moving the iterator
5079 continuously over the text). Otherwise, don't change the value
5080 of *SKIPPED_P.
5082 Newlines may come from buffer text, overlay strings, or strings
5083 displayed via the `display' property. That's the reason we can't
5084 simply use find_next_newline_no_quit.
5086 Note that this function may not skip over invisible text that is so
5087 because of text properties and immediately follows a newline. If
5088 it would, function reseat_at_next_visible_line_start, when called
5089 from set_iterator_to_next, would effectively make invisible
5090 characters following a newline part of the wrong glyph row, which
5091 leads to wrong cursor motion. */
5093 static int
5094 forward_to_next_line_start (it, skipped_p)
5095 struct it *it;
5096 int *skipped_p;
5098 int old_selective, newline_found_p, n;
5099 const int MAX_NEWLINE_DISTANCE = 500;
5101 /* If already on a newline, just consume it to avoid unintended
5102 skipping over invisible text below. */
5103 if (it->what == IT_CHARACTER
5104 && it->c == '\n'
5105 && CHARPOS (it->position) == IT_CHARPOS (*it))
5107 set_iterator_to_next (it, 0);
5108 it->c = 0;
5109 return 1;
5112 /* Don't handle selective display in the following. It's (a)
5113 unnecessary because it's done by the caller, and (b) leads to an
5114 infinite recursion because next_element_from_ellipsis indirectly
5115 calls this function. */
5116 old_selective = it->selective;
5117 it->selective = 0;
5119 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5120 from buffer text. */
5121 for (n = newline_found_p = 0;
5122 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5123 n += STRINGP (it->string) ? 0 : 1)
5125 if (!get_next_display_element (it))
5126 return 0;
5127 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5128 set_iterator_to_next (it, 0);
5131 /* If we didn't find a newline near enough, see if we can use a
5132 short-cut. */
5133 if (!newline_found_p)
5135 int start = IT_CHARPOS (*it);
5136 int limit = find_next_newline_no_quit (start, 1);
5137 Lisp_Object pos;
5139 xassert (!STRINGP (it->string));
5141 /* If there isn't any `display' property in sight, and no
5142 overlays, we can just use the position of the newline in
5143 buffer text. */
5144 if (it->stop_charpos >= limit
5145 || ((pos = Fnext_single_property_change (make_number (start),
5146 Qdisplay,
5147 Qnil, make_number (limit)),
5148 NILP (pos))
5149 && next_overlay_change (start) == ZV))
5151 IT_CHARPOS (*it) = limit;
5152 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5153 *skipped_p = newline_found_p = 1;
5155 else
5157 while (get_next_display_element (it)
5158 && !newline_found_p)
5160 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5161 set_iterator_to_next (it, 0);
5166 it->selective = old_selective;
5167 return newline_found_p;
5171 /* Set IT's current position to the previous visible line start. Skip
5172 invisible text that is so either due to text properties or due to
5173 selective display. Caution: this does not change IT->current_x and
5174 IT->hpos. */
5176 static void
5177 back_to_previous_visible_line_start (it)
5178 struct it *it;
5180 while (IT_CHARPOS (*it) > BEGV)
5182 back_to_previous_line_start (it);
5184 if (IT_CHARPOS (*it) <= BEGV)
5185 break;
5187 /* If selective > 0, then lines indented more than that values
5188 are invisible. */
5189 if (it->selective > 0
5190 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5191 (double) it->selective)) /* iftc */
5192 continue;
5194 /* Check the newline before point for invisibility. */
5196 Lisp_Object prop;
5197 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5198 Qinvisible, it->window);
5199 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5200 continue;
5203 if (IT_CHARPOS (*it) <= BEGV)
5204 break;
5207 struct it it2;
5208 int pos;
5209 int beg, end;
5210 Lisp_Object val, overlay;
5212 /* If newline is part of a composition, continue from start of composition */
5213 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5214 && beg < IT_CHARPOS (*it))
5215 goto replaced;
5217 /* If newline is replaced by a display property, find start of overlay
5218 or interval and continue search from that point. */
5219 it2 = *it;
5220 pos = --IT_CHARPOS (it2);
5221 --IT_BYTEPOS (it2);
5222 it2.sp = 0;
5223 if (handle_display_prop (&it2) == HANDLED_RETURN
5224 && !NILP (val = get_char_property_and_overlay
5225 (make_number (pos), Qdisplay, Qnil, &overlay))
5226 && (OVERLAYP (overlay)
5227 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5228 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5229 goto replaced;
5231 /* Newline is not replaced by anything -- so we are done. */
5232 break;
5234 replaced:
5235 if (beg < BEGV)
5236 beg = BEGV;
5237 IT_CHARPOS (*it) = beg;
5238 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5242 it->continuation_lines_width = 0;
5244 xassert (IT_CHARPOS (*it) >= BEGV);
5245 xassert (IT_CHARPOS (*it) == BEGV
5246 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5247 CHECK_IT (it);
5251 /* Reseat iterator IT at the previous visible line start. Skip
5252 invisible text that is so either due to text properties or due to
5253 selective display. At the end, update IT's overlay information,
5254 face information etc. */
5256 void
5257 reseat_at_previous_visible_line_start (it)
5258 struct it *it;
5260 back_to_previous_visible_line_start (it);
5261 reseat (it, it->current.pos, 1);
5262 CHECK_IT (it);
5266 /* Reseat iterator IT on the next visible line start in the current
5267 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5268 preceding the line start. Skip over invisible text that is so
5269 because of selective display. Compute faces, overlays etc at the
5270 new position. Note that this function does not skip over text that
5271 is invisible because of text properties. */
5273 static void
5274 reseat_at_next_visible_line_start (it, on_newline_p)
5275 struct it *it;
5276 int on_newline_p;
5278 int newline_found_p, skipped_p = 0;
5280 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5282 /* Skip over lines that are invisible because they are indented
5283 more than the value of IT->selective. */
5284 if (it->selective > 0)
5285 while (IT_CHARPOS (*it) < ZV
5286 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5287 (double) it->selective)) /* iftc */
5289 xassert (IT_BYTEPOS (*it) == BEGV
5290 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5291 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5294 /* Position on the newline if that's what's requested. */
5295 if (on_newline_p && newline_found_p)
5297 if (STRINGP (it->string))
5299 if (IT_STRING_CHARPOS (*it) > 0)
5301 --IT_STRING_CHARPOS (*it);
5302 --IT_STRING_BYTEPOS (*it);
5305 else if (IT_CHARPOS (*it) > BEGV)
5307 --IT_CHARPOS (*it);
5308 --IT_BYTEPOS (*it);
5309 reseat (it, it->current.pos, 0);
5312 else if (skipped_p)
5313 reseat (it, it->current.pos, 0);
5315 CHECK_IT (it);
5320 /***********************************************************************
5321 Changing an iterator's position
5322 ***********************************************************************/
5324 /* Change IT's current position to POS in current_buffer. If FORCE_P
5325 is non-zero, always check for text properties at the new position.
5326 Otherwise, text properties are only looked up if POS >=
5327 IT->check_charpos of a property. */
5329 static void
5330 reseat (it, pos, force_p)
5331 struct it *it;
5332 struct text_pos pos;
5333 int force_p;
5335 int original_pos = IT_CHARPOS (*it);
5337 reseat_1 (it, pos, 0);
5339 /* Determine where to check text properties. Avoid doing it
5340 where possible because text property lookup is very expensive. */
5341 if (force_p
5342 || CHARPOS (pos) > it->stop_charpos
5343 || CHARPOS (pos) < original_pos)
5344 handle_stop (it);
5346 CHECK_IT (it);
5350 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5351 IT->stop_pos to POS, also. */
5353 static void
5354 reseat_1 (it, pos, set_stop_p)
5355 struct it *it;
5356 struct text_pos pos;
5357 int set_stop_p;
5359 /* Don't call this function when scanning a C string. */
5360 xassert (it->s == NULL);
5362 /* POS must be a reasonable value. */
5363 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5365 it->current.pos = it->position = pos;
5366 it->end_charpos = ZV;
5367 it->dpvec = NULL;
5368 it->current.dpvec_index = -1;
5369 it->current.overlay_string_index = -1;
5370 IT_STRING_CHARPOS (*it) = -1;
5371 IT_STRING_BYTEPOS (*it) = -1;
5372 it->string = Qnil;
5373 it->method = GET_FROM_BUFFER;
5374 it->object = it->w->buffer;
5375 it->area = TEXT_AREA;
5376 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5377 it->sp = 0;
5378 it->string_from_display_prop_p = 0;
5379 it->face_before_selective_p = 0;
5381 if (set_stop_p)
5382 it->stop_charpos = CHARPOS (pos);
5386 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5387 If S is non-null, it is a C string to iterate over. Otherwise,
5388 STRING gives a Lisp string to iterate over.
5390 If PRECISION > 0, don't return more then PRECISION number of
5391 characters from the string.
5393 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5394 characters have been returned. FIELD_WIDTH < 0 means an infinite
5395 field width.
5397 MULTIBYTE = 0 means disable processing of multibyte characters,
5398 MULTIBYTE > 0 means enable it,
5399 MULTIBYTE < 0 means use IT->multibyte_p.
5401 IT must be initialized via a prior call to init_iterator before
5402 calling this function. */
5404 static void
5405 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5406 struct it *it;
5407 unsigned char *s;
5408 Lisp_Object string;
5409 int charpos;
5410 int precision, field_width, multibyte;
5412 /* No region in strings. */
5413 it->region_beg_charpos = it->region_end_charpos = -1;
5415 /* No text property checks performed by default, but see below. */
5416 it->stop_charpos = -1;
5418 /* Set iterator position and end position. */
5419 bzero (&it->current, sizeof it->current);
5420 it->current.overlay_string_index = -1;
5421 it->current.dpvec_index = -1;
5422 xassert (charpos >= 0);
5424 /* If STRING is specified, use its multibyteness, otherwise use the
5425 setting of MULTIBYTE, if specified. */
5426 if (multibyte >= 0)
5427 it->multibyte_p = multibyte > 0;
5429 if (s == NULL)
5431 xassert (STRINGP (string));
5432 it->string = string;
5433 it->s = NULL;
5434 it->end_charpos = it->string_nchars = SCHARS (string);
5435 it->method = GET_FROM_STRING;
5436 it->current.string_pos = string_pos (charpos, string);
5438 else
5440 it->s = s;
5441 it->string = Qnil;
5443 /* Note that we use IT->current.pos, not it->current.string_pos,
5444 for displaying C strings. */
5445 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5446 if (it->multibyte_p)
5448 it->current.pos = c_string_pos (charpos, s, 1);
5449 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5451 else
5453 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5454 it->end_charpos = it->string_nchars = strlen (s);
5457 it->method = GET_FROM_C_STRING;
5460 /* PRECISION > 0 means don't return more than PRECISION characters
5461 from the string. */
5462 if (precision > 0 && it->end_charpos - charpos > precision)
5463 it->end_charpos = it->string_nchars = charpos + precision;
5465 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5466 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5467 FIELD_WIDTH < 0 means infinite field width. This is useful for
5468 padding with `-' at the end of a mode line. */
5469 if (field_width < 0)
5470 field_width = INFINITY;
5471 if (field_width > it->end_charpos - charpos)
5472 it->end_charpos = charpos + field_width;
5474 /* Use the standard display table for displaying strings. */
5475 if (DISP_TABLE_P (Vstandard_display_table))
5476 it->dp = XCHAR_TABLE (Vstandard_display_table);
5478 it->stop_charpos = charpos;
5479 CHECK_IT (it);
5484 /***********************************************************************
5485 Iteration
5486 ***********************************************************************/
5488 /* Map enum it_method value to corresponding next_element_from_* function. */
5490 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5492 next_element_from_buffer,
5493 next_element_from_display_vector,
5494 next_element_from_composition,
5495 next_element_from_string,
5496 next_element_from_c_string,
5497 next_element_from_image,
5498 next_element_from_stretch
5502 /* Load IT's display element fields with information about the next
5503 display element from the current position of IT. Value is zero if
5504 end of buffer (or C string) is reached. */
5506 static struct frame *last_escape_glyph_frame = NULL;
5507 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5508 static int last_escape_glyph_merged_face_id = 0;
5511 get_next_display_element (it)
5512 struct it *it;
5514 /* Non-zero means that we found a display element. Zero means that
5515 we hit the end of what we iterate over. Performance note: the
5516 function pointer `method' used here turns out to be faster than
5517 using a sequence of if-statements. */
5518 int success_p;
5520 get_next:
5521 success_p = (*get_next_element[it->method]) (it);
5523 if (it->what == IT_CHARACTER)
5525 /* Map via display table or translate control characters.
5526 IT->c, IT->len etc. have been set to the next character by
5527 the function call above. If we have a display table, and it
5528 contains an entry for IT->c, translate it. Don't do this if
5529 IT->c itself comes from a display table, otherwise we could
5530 end up in an infinite recursion. (An alternative could be to
5531 count the recursion depth of this function and signal an
5532 error when a certain maximum depth is reached.) Is it worth
5533 it? */
5534 if (success_p && it->dpvec == NULL)
5536 Lisp_Object dv;
5538 if (it->dp
5539 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5540 VECTORP (dv)))
5542 struct Lisp_Vector *v = XVECTOR (dv);
5544 /* Return the first character from the display table
5545 entry, if not empty. If empty, don't display the
5546 current character. */
5547 if (v->size)
5549 it->dpvec_char_len = it->len;
5550 it->dpvec = v->contents;
5551 it->dpend = v->contents + v->size;
5552 it->current.dpvec_index = 0;
5553 it->dpvec_face_id = -1;
5554 it->saved_face_id = it->face_id;
5555 it->method = GET_FROM_DISPLAY_VECTOR;
5556 it->ellipsis_p = 0;
5558 else
5560 set_iterator_to_next (it, 0);
5562 goto get_next;
5565 /* Translate control characters into `\003' or `^C' form.
5566 Control characters coming from a display table entry are
5567 currently not translated because we use IT->dpvec to hold
5568 the translation. This could easily be changed but I
5569 don't believe that it is worth doing.
5571 If it->multibyte_p is nonzero, eight-bit characters and
5572 non-printable multibyte characters are also translated to
5573 octal form.
5575 If it->multibyte_p is zero, eight-bit characters that
5576 don't have corresponding multibyte char code are also
5577 translated to octal form. */
5578 else if ((it->c < ' '
5579 && (it->area != TEXT_AREA
5580 /* In mode line, treat \n like other crl chars. */
5581 || (it->c != '\t'
5582 && it->glyph_row && it->glyph_row->mode_line_p)
5583 || (it->c != '\n' && it->c != '\t')))
5584 || (it->multibyte_p
5585 ? ((it->c >= 127
5586 && it->len == 1)
5587 || !CHAR_PRINTABLE_P (it->c)
5588 || (!NILP (Vnobreak_char_display)
5589 && (it->c == 0x8a0 || it->c == 0x8ad
5590 || it->c == 0x920 || it->c == 0x92d
5591 || it->c == 0xe20 || it->c == 0xe2d
5592 || it->c == 0xf20 || it->c == 0xf2d)))
5593 : (it->c >= 127
5594 && (!unibyte_display_via_language_environment
5595 || it->c == unibyte_char_to_multibyte (it->c)))))
5597 /* IT->c is a control character which must be displayed
5598 either as '\003' or as `^C' where the '\\' and '^'
5599 can be defined in the display table. Fill
5600 IT->ctl_chars with glyphs for what we have to
5601 display. Then, set IT->dpvec to these glyphs. */
5602 GLYPH g;
5603 int ctl_len;
5604 int face_id, lface_id = 0 ;
5605 GLYPH escape_glyph;
5607 /* Handle control characters with ^. */
5609 if (it->c < 128 && it->ctl_arrow_p)
5611 g = '^'; /* default glyph for Control */
5612 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5613 if (it->dp
5614 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5615 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5617 g = XINT (DISP_CTRL_GLYPH (it->dp));
5618 lface_id = FAST_GLYPH_FACE (g);
5620 if (lface_id)
5622 g = FAST_GLYPH_CHAR (g);
5623 face_id = merge_faces (it->f, Qt, lface_id,
5624 it->face_id);
5626 else if (it->f == last_escape_glyph_frame
5627 && it->face_id == last_escape_glyph_face_id)
5629 face_id = last_escape_glyph_merged_face_id;
5631 else
5633 /* Merge the escape-glyph face into the current face. */
5634 face_id = merge_faces (it->f, Qescape_glyph, 0,
5635 it->face_id);
5636 last_escape_glyph_frame = it->f;
5637 last_escape_glyph_face_id = it->face_id;
5638 last_escape_glyph_merged_face_id = face_id;
5641 XSETINT (it->ctl_chars[0], g);
5642 g = it->c ^ 0100;
5643 XSETINT (it->ctl_chars[1], g);
5644 ctl_len = 2;
5645 goto display_control;
5648 /* Handle non-break space in the mode where it only gets
5649 highlighting. */
5651 if (EQ (Vnobreak_char_display, Qt)
5652 && (it->c == 0x8a0 || it->c == 0x920
5653 || it->c == 0xe20 || it->c == 0xf20))
5655 /* Merge the no-break-space face into the current face. */
5656 face_id = merge_faces (it->f, Qnobreak_space, 0,
5657 it->face_id);
5659 g = it->c = ' ';
5660 XSETINT (it->ctl_chars[0], g);
5661 ctl_len = 1;
5662 goto display_control;
5665 /* Handle sequences that start with the "escape glyph". */
5667 /* the default escape glyph is \. */
5668 escape_glyph = '\\';
5670 if (it->dp
5671 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5672 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5674 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5675 lface_id = FAST_GLYPH_FACE (escape_glyph);
5677 if (lface_id)
5679 /* The display table specified a face.
5680 Merge it into face_id and also into escape_glyph. */
5681 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5682 face_id = merge_faces (it->f, Qt, lface_id,
5683 it->face_id);
5685 else if (it->f == last_escape_glyph_frame
5686 && it->face_id == last_escape_glyph_face_id)
5688 face_id = last_escape_glyph_merged_face_id;
5690 else
5692 /* Merge the escape-glyph face into the current face. */
5693 face_id = merge_faces (it->f, Qescape_glyph, 0,
5694 it->face_id);
5695 last_escape_glyph_frame = it->f;
5696 last_escape_glyph_face_id = it->face_id;
5697 last_escape_glyph_merged_face_id = face_id;
5700 /* Handle soft hyphens in the mode where they only get
5701 highlighting. */
5703 if (EQ (Vnobreak_char_display, Qt)
5704 && (it->c == 0x8ad || it->c == 0x92d
5705 || it->c == 0xe2d || it->c == 0xf2d))
5707 g = it->c = '-';
5708 XSETINT (it->ctl_chars[0], g);
5709 ctl_len = 1;
5710 goto display_control;
5713 /* Handle non-break space and soft hyphen
5714 with the escape glyph. */
5716 if (it->c == 0x8a0 || it->c == 0x8ad
5717 || it->c == 0x920 || it->c == 0x92d
5718 || it->c == 0xe20 || it->c == 0xe2d
5719 || it->c == 0xf20 || it->c == 0xf2d)
5721 XSETINT (it->ctl_chars[0], escape_glyph);
5722 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5723 XSETINT (it->ctl_chars[1], g);
5724 ctl_len = 2;
5725 goto display_control;
5729 unsigned char str[MAX_MULTIBYTE_LENGTH];
5730 int len;
5731 int i;
5733 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5734 if (SINGLE_BYTE_CHAR_P (it->c))
5735 str[0] = it->c, len = 1;
5736 else
5738 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5739 if (len < 0)
5741 /* It's an invalid character, which shouldn't
5742 happen actually, but due to bugs it may
5743 happen. Let's print the char as is, there's
5744 not much meaningful we can do with it. */
5745 str[0] = it->c;
5746 str[1] = it->c >> 8;
5747 str[2] = it->c >> 16;
5748 str[3] = it->c >> 24;
5749 len = 4;
5753 for (i = 0; i < len; i++)
5755 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5756 /* Insert three more glyphs into IT->ctl_chars for
5757 the octal display of the character. */
5758 g = ((str[i] >> 6) & 7) + '0';
5759 XSETINT (it->ctl_chars[i * 4 + 1], g);
5760 g = ((str[i] >> 3) & 7) + '0';
5761 XSETINT (it->ctl_chars[i * 4 + 2], g);
5762 g = (str[i] & 7) + '0';
5763 XSETINT (it->ctl_chars[i * 4 + 3], g);
5765 ctl_len = len * 4;
5768 display_control:
5769 /* Set up IT->dpvec and return first character from it. */
5770 it->dpvec_char_len = it->len;
5771 it->dpvec = it->ctl_chars;
5772 it->dpend = it->dpvec + ctl_len;
5773 it->current.dpvec_index = 0;
5774 it->dpvec_face_id = face_id;
5775 it->saved_face_id = it->face_id;
5776 it->method = GET_FROM_DISPLAY_VECTOR;
5777 it->ellipsis_p = 0;
5778 goto get_next;
5782 /* Adjust face id for a multibyte character. There are no
5783 multibyte character in unibyte text. */
5784 if (it->multibyte_p
5785 && success_p
5786 && FRAME_WINDOW_P (it->f))
5788 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5789 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5793 /* Is this character the last one of a run of characters with
5794 box? If yes, set IT->end_of_box_run_p to 1. */
5795 if (it->face_box_p
5796 && it->s == NULL)
5798 int face_id;
5799 struct face *face;
5801 it->end_of_box_run_p
5802 = ((face_id = face_after_it_pos (it),
5803 face_id != it->face_id)
5804 && (face = FACE_FROM_ID (it->f, face_id),
5805 face->box == FACE_NO_BOX));
5808 /* Value is 0 if end of buffer or string reached. */
5809 return success_p;
5813 /* Move IT to the next display element.
5815 RESEAT_P non-zero means if called on a newline in buffer text,
5816 skip to the next visible line start.
5818 Functions get_next_display_element and set_iterator_to_next are
5819 separate because I find this arrangement easier to handle than a
5820 get_next_display_element function that also increments IT's
5821 position. The way it is we can first look at an iterator's current
5822 display element, decide whether it fits on a line, and if it does,
5823 increment the iterator position. The other way around we probably
5824 would either need a flag indicating whether the iterator has to be
5825 incremented the next time, or we would have to implement a
5826 decrement position function which would not be easy to write. */
5828 void
5829 set_iterator_to_next (it, reseat_p)
5830 struct it *it;
5831 int reseat_p;
5833 /* Reset flags indicating start and end of a sequence of characters
5834 with box. Reset them at the start of this function because
5835 moving the iterator to a new position might set them. */
5836 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5838 switch (it->method)
5840 case GET_FROM_BUFFER:
5841 /* The current display element of IT is a character from
5842 current_buffer. Advance in the buffer, and maybe skip over
5843 invisible lines that are so because of selective display. */
5844 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5845 reseat_at_next_visible_line_start (it, 0);
5846 else
5848 xassert (it->len != 0);
5849 IT_BYTEPOS (*it) += it->len;
5850 IT_CHARPOS (*it) += 1;
5851 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5853 break;
5855 case GET_FROM_COMPOSITION:
5856 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5857 xassert (it->sp > 0);
5858 pop_it (it);
5859 if (it->method == GET_FROM_STRING)
5861 IT_STRING_BYTEPOS (*it) += it->len;
5862 IT_STRING_CHARPOS (*it) += it->cmp_len;
5863 goto consider_string_end;
5865 else if (it->method == GET_FROM_BUFFER)
5867 IT_BYTEPOS (*it) += it->len;
5868 IT_CHARPOS (*it) += it->cmp_len;
5870 break;
5872 case GET_FROM_C_STRING:
5873 /* Current display element of IT is from a C string. */
5874 IT_BYTEPOS (*it) += it->len;
5875 IT_CHARPOS (*it) += 1;
5876 break;
5878 case GET_FROM_DISPLAY_VECTOR:
5879 /* Current display element of IT is from a display table entry.
5880 Advance in the display table definition. Reset it to null if
5881 end reached, and continue with characters from buffers/
5882 strings. */
5883 ++it->current.dpvec_index;
5885 /* Restore face of the iterator to what they were before the
5886 display vector entry (these entries may contain faces). */
5887 it->face_id = it->saved_face_id;
5889 if (it->dpvec + it->current.dpvec_index == it->dpend)
5891 int recheck_faces = it->ellipsis_p;
5893 if (it->s)
5894 it->method = GET_FROM_C_STRING;
5895 else if (STRINGP (it->string))
5896 it->method = GET_FROM_STRING;
5897 else
5899 it->method = GET_FROM_BUFFER;
5900 it->object = it->w->buffer;
5903 it->dpvec = NULL;
5904 it->current.dpvec_index = -1;
5906 /* Skip over characters which were displayed via IT->dpvec. */
5907 if (it->dpvec_char_len < 0)
5908 reseat_at_next_visible_line_start (it, 1);
5909 else if (it->dpvec_char_len > 0)
5911 if (it->method == GET_FROM_STRING
5912 && it->n_overlay_strings > 0)
5913 it->ignore_overlay_strings_at_pos_p = 1;
5914 it->len = it->dpvec_char_len;
5915 set_iterator_to_next (it, reseat_p);
5918 /* Maybe recheck faces after display vector */
5919 if (recheck_faces)
5920 it->stop_charpos = IT_CHARPOS (*it);
5922 break;
5924 case GET_FROM_STRING:
5925 /* Current display element is a character from a Lisp string. */
5926 xassert (it->s == NULL && STRINGP (it->string));
5927 IT_STRING_BYTEPOS (*it) += it->len;
5928 IT_STRING_CHARPOS (*it) += 1;
5930 consider_string_end:
5932 if (it->current.overlay_string_index >= 0)
5934 /* IT->string is an overlay string. Advance to the
5935 next, if there is one. */
5936 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5937 next_overlay_string (it);
5939 else
5941 /* IT->string is not an overlay string. If we reached
5942 its end, and there is something on IT->stack, proceed
5943 with what is on the stack. This can be either another
5944 string, this time an overlay string, or a buffer. */
5945 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5946 && it->sp > 0)
5948 pop_it (it);
5949 if (it->method == GET_FROM_STRING)
5950 goto consider_string_end;
5953 break;
5955 case GET_FROM_IMAGE:
5956 case GET_FROM_STRETCH:
5957 /* The position etc with which we have to proceed are on
5958 the stack. The position may be at the end of a string,
5959 if the `display' property takes up the whole string. */
5960 xassert (it->sp > 0);
5961 pop_it (it);
5962 if (it->method == GET_FROM_STRING)
5963 goto consider_string_end;
5964 break;
5966 default:
5967 /* There are no other methods defined, so this should be a bug. */
5968 abort ();
5971 xassert (it->method != GET_FROM_STRING
5972 || (STRINGP (it->string)
5973 && IT_STRING_CHARPOS (*it) >= 0));
5976 /* Load IT's display element fields with information about the next
5977 display element which comes from a display table entry or from the
5978 result of translating a control character to one of the forms `^C'
5979 or `\003'.
5981 IT->dpvec holds the glyphs to return as characters.
5982 IT->saved_face_id holds the face id before the display vector--
5983 it is restored into IT->face_idin set_iterator_to_next. */
5985 static int
5986 next_element_from_display_vector (it)
5987 struct it *it;
5989 /* Precondition. */
5990 xassert (it->dpvec && it->current.dpvec_index >= 0);
5992 it->face_id = it->saved_face_id;
5994 if (INTEGERP (*it->dpvec)
5995 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5997 GLYPH g;
5999 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6000 it->c = FAST_GLYPH_CHAR (g);
6001 it->len = CHAR_BYTES (it->c);
6003 /* The entry may contain a face id to use. Such a face id is
6004 the id of a Lisp face, not a realized face. A face id of
6005 zero means no face is specified. */
6006 if (it->dpvec_face_id >= 0)
6007 it->face_id = it->dpvec_face_id;
6008 else
6010 int lface_id = FAST_GLYPH_FACE (g);
6011 if (lface_id > 0)
6012 it->face_id = merge_faces (it->f, Qt, lface_id,
6013 it->saved_face_id);
6016 else
6017 /* Display table entry is invalid. Return a space. */
6018 it->c = ' ', it->len = 1;
6020 /* Don't change position and object of the iterator here. They are
6021 still the values of the character that had this display table
6022 entry or was translated, and that's what we want. */
6023 it->what = IT_CHARACTER;
6024 return 1;
6028 /* Load IT with the next display element from Lisp string IT->string.
6029 IT->current.string_pos is the current position within the string.
6030 If IT->current.overlay_string_index >= 0, the Lisp string is an
6031 overlay string. */
6033 static int
6034 next_element_from_string (it)
6035 struct it *it;
6037 struct text_pos position;
6039 xassert (STRINGP (it->string));
6040 xassert (IT_STRING_CHARPOS (*it) >= 0);
6041 position = it->current.string_pos;
6043 /* Time to check for invisible text? */
6044 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6045 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6047 handle_stop (it);
6049 /* Since a handler may have changed IT->method, we must
6050 recurse here. */
6051 return get_next_display_element (it);
6054 if (it->current.overlay_string_index >= 0)
6056 /* Get the next character from an overlay string. In overlay
6057 strings, There is no field width or padding with spaces to
6058 do. */
6059 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6061 it->what = IT_EOB;
6062 return 0;
6064 else if (STRING_MULTIBYTE (it->string))
6066 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6067 const unsigned char *s = (SDATA (it->string)
6068 + IT_STRING_BYTEPOS (*it));
6069 it->c = string_char_and_length (s, remaining, &it->len);
6071 else
6073 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6074 it->len = 1;
6077 else
6079 /* Get the next character from a Lisp string that is not an
6080 overlay string. Such strings come from the mode line, for
6081 example. We may have to pad with spaces, or truncate the
6082 string. See also next_element_from_c_string. */
6083 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6085 it->what = IT_EOB;
6086 return 0;
6088 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6090 /* Pad with spaces. */
6091 it->c = ' ', it->len = 1;
6092 CHARPOS (position) = BYTEPOS (position) = -1;
6094 else if (STRING_MULTIBYTE (it->string))
6096 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6097 const unsigned char *s = (SDATA (it->string)
6098 + IT_STRING_BYTEPOS (*it));
6099 it->c = string_char_and_length (s, maxlen, &it->len);
6101 else
6103 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6104 it->len = 1;
6108 /* Record what we have and where it came from. */
6109 it->what = IT_CHARACTER;
6110 it->object = it->string;
6111 it->position = position;
6112 return 1;
6116 /* Load IT with next display element from C string IT->s.
6117 IT->string_nchars is the maximum number of characters to return
6118 from the string. IT->end_charpos may be greater than
6119 IT->string_nchars when this function is called, in which case we
6120 may have to return padding spaces. Value is zero if end of string
6121 reached, including padding spaces. */
6123 static int
6124 next_element_from_c_string (it)
6125 struct it *it;
6127 int success_p = 1;
6129 xassert (it->s);
6130 it->what = IT_CHARACTER;
6131 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6132 it->object = Qnil;
6134 /* IT's position can be greater IT->string_nchars in case a field
6135 width or precision has been specified when the iterator was
6136 initialized. */
6137 if (IT_CHARPOS (*it) >= it->end_charpos)
6139 /* End of the game. */
6140 it->what = IT_EOB;
6141 success_p = 0;
6143 else if (IT_CHARPOS (*it) >= it->string_nchars)
6145 /* Pad with spaces. */
6146 it->c = ' ', it->len = 1;
6147 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6149 else if (it->multibyte_p)
6151 /* Implementation note: The calls to strlen apparently aren't a
6152 performance problem because there is no noticeable performance
6153 difference between Emacs running in unibyte or multibyte mode. */
6154 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6155 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6156 maxlen, &it->len);
6158 else
6159 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6161 return success_p;
6165 /* Set up IT to return characters from an ellipsis, if appropriate.
6166 The definition of the ellipsis glyphs may come from a display table
6167 entry. This function Fills IT with the first glyph from the
6168 ellipsis if an ellipsis is to be displayed. */
6170 static int
6171 next_element_from_ellipsis (it)
6172 struct it *it;
6174 if (it->selective_display_ellipsis_p)
6175 setup_for_ellipsis (it, it->len);
6176 else
6178 /* The face at the current position may be different from the
6179 face we find after the invisible text. Remember what it
6180 was in IT->saved_face_id, and signal that it's there by
6181 setting face_before_selective_p. */
6182 it->saved_face_id = it->face_id;
6183 it->method = GET_FROM_BUFFER;
6184 it->object = it->w->buffer;
6185 reseat_at_next_visible_line_start (it, 1);
6186 it->face_before_selective_p = 1;
6189 return get_next_display_element (it);
6193 /* Deliver an image display element. The iterator IT is already
6194 filled with image information (done in handle_display_prop). Value
6195 is always 1. */
6198 static int
6199 next_element_from_image (it)
6200 struct it *it;
6202 it->what = IT_IMAGE;
6203 return 1;
6207 /* Fill iterator IT with next display element from a stretch glyph
6208 property. IT->object is the value of the text property. Value is
6209 always 1. */
6211 static int
6212 next_element_from_stretch (it)
6213 struct it *it;
6215 it->what = IT_STRETCH;
6216 return 1;
6220 /* Load IT with the next display element from current_buffer. Value
6221 is zero if end of buffer reached. IT->stop_charpos is the next
6222 position at which to stop and check for text properties or buffer
6223 end. */
6225 static int
6226 next_element_from_buffer (it)
6227 struct it *it;
6229 int success_p = 1;
6231 /* Check this assumption, otherwise, we would never enter the
6232 if-statement, below. */
6233 xassert (IT_CHARPOS (*it) >= BEGV
6234 && IT_CHARPOS (*it) <= it->stop_charpos);
6236 if (IT_CHARPOS (*it) >= it->stop_charpos)
6238 if (IT_CHARPOS (*it) >= it->end_charpos)
6240 int overlay_strings_follow_p;
6242 /* End of the game, except when overlay strings follow that
6243 haven't been returned yet. */
6244 if (it->overlay_strings_at_end_processed_p)
6245 overlay_strings_follow_p = 0;
6246 else
6248 it->overlay_strings_at_end_processed_p = 1;
6249 overlay_strings_follow_p = get_overlay_strings (it, 0);
6252 if (overlay_strings_follow_p)
6253 success_p = get_next_display_element (it);
6254 else
6256 it->what = IT_EOB;
6257 it->position = it->current.pos;
6258 success_p = 0;
6261 else
6263 handle_stop (it);
6264 return get_next_display_element (it);
6267 else
6269 /* No face changes, overlays etc. in sight, so just return a
6270 character from current_buffer. */
6271 unsigned char *p;
6273 /* Maybe run the redisplay end trigger hook. Performance note:
6274 This doesn't seem to cost measurable time. */
6275 if (it->redisplay_end_trigger_charpos
6276 && it->glyph_row
6277 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6278 run_redisplay_end_trigger_hook (it);
6280 /* Get the next character, maybe multibyte. */
6281 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6282 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6284 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6285 - IT_BYTEPOS (*it));
6286 it->c = string_char_and_length (p, maxlen, &it->len);
6288 else
6289 it->c = *p, it->len = 1;
6291 /* Record what we have and where it came from. */
6292 it->what = IT_CHARACTER;
6293 it->object = it->w->buffer;
6294 it->position = it->current.pos;
6296 /* Normally we return the character found above, except when we
6297 really want to return an ellipsis for selective display. */
6298 if (it->selective)
6300 if (it->c == '\n')
6302 /* A value of selective > 0 means hide lines indented more
6303 than that number of columns. */
6304 if (it->selective > 0
6305 && IT_CHARPOS (*it) + 1 < ZV
6306 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6307 IT_BYTEPOS (*it) + 1,
6308 (double) it->selective)) /* iftc */
6310 success_p = next_element_from_ellipsis (it);
6311 it->dpvec_char_len = -1;
6314 else if (it->c == '\r' && it->selective == -1)
6316 /* A value of selective == -1 means that everything from the
6317 CR to the end of the line is invisible, with maybe an
6318 ellipsis displayed for it. */
6319 success_p = next_element_from_ellipsis (it);
6320 it->dpvec_char_len = -1;
6325 /* Value is zero if end of buffer reached. */
6326 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6327 return success_p;
6331 /* Run the redisplay end trigger hook for IT. */
6333 static void
6334 run_redisplay_end_trigger_hook (it)
6335 struct it *it;
6337 Lisp_Object args[3];
6339 /* IT->glyph_row should be non-null, i.e. we should be actually
6340 displaying something, or otherwise we should not run the hook. */
6341 xassert (it->glyph_row);
6343 /* Set up hook arguments. */
6344 args[0] = Qredisplay_end_trigger_functions;
6345 args[1] = it->window;
6346 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6347 it->redisplay_end_trigger_charpos = 0;
6349 /* Since we are *trying* to run these functions, don't try to run
6350 them again, even if they get an error. */
6351 it->w->redisplay_end_trigger = Qnil;
6352 Frun_hook_with_args (3, args);
6354 /* Notice if it changed the face of the character we are on. */
6355 handle_face_prop (it);
6359 /* Deliver a composition display element. The iterator IT is already
6360 filled with composition information (done in
6361 handle_composition_prop). Value is always 1. */
6363 static int
6364 next_element_from_composition (it)
6365 struct it *it;
6367 it->what = IT_COMPOSITION;
6368 it->position = (STRINGP (it->string)
6369 ? it->current.string_pos
6370 : it->current.pos);
6371 if (STRINGP (it->string))
6372 it->object = it->string;
6373 else
6374 it->object = it->w->buffer;
6375 return 1;
6380 /***********************************************************************
6381 Moving an iterator without producing glyphs
6382 ***********************************************************************/
6384 /* Check if iterator is at a position corresponding to a valid buffer
6385 position after some move_it_ call. */
6387 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6388 ((it)->method == GET_FROM_STRING \
6389 ? IT_STRING_CHARPOS (*it) == 0 \
6390 : 1)
6393 /* Move iterator IT to a specified buffer or X position within one
6394 line on the display without producing glyphs.
6396 OP should be a bit mask including some or all of these bits:
6397 MOVE_TO_X: Stop on reaching x-position TO_X.
6398 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6399 Regardless of OP's value, stop in reaching the end of the display line.
6401 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6402 This means, in particular, that TO_X includes window's horizontal
6403 scroll amount.
6405 The return value has several possible values that
6406 say what condition caused the scan to stop:
6408 MOVE_POS_MATCH_OR_ZV
6409 - when TO_POS or ZV was reached.
6411 MOVE_X_REACHED
6412 -when TO_X was reached before TO_POS or ZV were reached.
6414 MOVE_LINE_CONTINUED
6415 - when we reached the end of the display area and the line must
6416 be continued.
6418 MOVE_LINE_TRUNCATED
6419 - when we reached the end of the display area and the line is
6420 truncated.
6422 MOVE_NEWLINE_OR_CR
6423 - when we stopped at a line end, i.e. a newline or a CR and selective
6424 display is on. */
6426 static enum move_it_result
6427 move_it_in_display_line_to (it, to_charpos, to_x, op)
6428 struct it *it;
6429 int to_charpos, to_x, op;
6431 enum move_it_result result = MOVE_UNDEFINED;
6432 struct glyph_row *saved_glyph_row;
6434 /* Don't produce glyphs in produce_glyphs. */
6435 saved_glyph_row = it->glyph_row;
6436 it->glyph_row = NULL;
6438 #define BUFFER_POS_REACHED_P() \
6439 ((op & MOVE_TO_POS) != 0 \
6440 && BUFFERP (it->object) \
6441 && IT_CHARPOS (*it) >= to_charpos \
6442 && (it->method == GET_FROM_BUFFER \
6443 || (it->method == GET_FROM_DISPLAY_VECTOR \
6444 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6447 while (1)
6449 int x, i, ascent = 0, descent = 0;
6451 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6452 if ((op & MOVE_TO_POS) != 0
6453 && BUFFERP (it->object)
6454 && it->method == GET_FROM_BUFFER
6455 && IT_CHARPOS (*it) > to_charpos)
6457 result = MOVE_POS_MATCH_OR_ZV;
6458 break;
6461 /* Stop when ZV reached.
6462 We used to stop here when TO_CHARPOS reached as well, but that is
6463 too soon if this glyph does not fit on this line. So we handle it
6464 explicitly below. */
6465 if (!get_next_display_element (it)
6466 || (it->truncate_lines_p
6467 && BUFFER_POS_REACHED_P ()))
6469 result = MOVE_POS_MATCH_OR_ZV;
6470 break;
6473 /* The call to produce_glyphs will get the metrics of the
6474 display element IT is loaded with. We record in x the
6475 x-position before this display element in case it does not
6476 fit on the line. */
6477 x = it->current_x;
6479 /* Remember the line height so far in case the next element doesn't
6480 fit on the line. */
6481 if (!it->truncate_lines_p)
6483 ascent = it->max_ascent;
6484 descent = it->max_descent;
6487 PRODUCE_GLYPHS (it);
6489 if (it->area != TEXT_AREA)
6491 set_iterator_to_next (it, 1);
6492 continue;
6495 /* The number of glyphs we get back in IT->nglyphs will normally
6496 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6497 character on a terminal frame, or (iii) a line end. For the
6498 second case, IT->nglyphs - 1 padding glyphs will be present
6499 (on X frames, there is only one glyph produced for a
6500 composite character.
6502 The behavior implemented below means, for continuation lines,
6503 that as many spaces of a TAB as fit on the current line are
6504 displayed there. For terminal frames, as many glyphs of a
6505 multi-glyph character are displayed in the current line, too.
6506 This is what the old redisplay code did, and we keep it that
6507 way. Under X, the whole shape of a complex character must
6508 fit on the line or it will be completely displayed in the
6509 next line.
6511 Note that both for tabs and padding glyphs, all glyphs have
6512 the same width. */
6513 if (it->nglyphs)
6515 /* More than one glyph or glyph doesn't fit on line. All
6516 glyphs have the same width. */
6517 int single_glyph_width = it->pixel_width / it->nglyphs;
6518 int new_x;
6519 int x_before_this_char = x;
6520 int hpos_before_this_char = it->hpos;
6522 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6524 new_x = x + single_glyph_width;
6526 /* We want to leave anything reaching TO_X to the caller. */
6527 if ((op & MOVE_TO_X) && new_x > to_x)
6529 if (BUFFER_POS_REACHED_P ())
6530 goto buffer_pos_reached;
6531 it->current_x = x;
6532 result = MOVE_X_REACHED;
6533 break;
6535 else if (/* Lines are continued. */
6536 !it->truncate_lines_p
6537 && (/* And glyph doesn't fit on the line. */
6538 new_x > it->last_visible_x
6539 /* Or it fits exactly and we're on a window
6540 system frame. */
6541 || (new_x == it->last_visible_x
6542 && FRAME_WINDOW_P (it->f))))
6544 if (/* IT->hpos == 0 means the very first glyph
6545 doesn't fit on the line, e.g. a wide image. */
6546 it->hpos == 0
6547 || (new_x == it->last_visible_x
6548 && FRAME_WINDOW_P (it->f)))
6550 ++it->hpos;
6551 it->current_x = new_x;
6553 /* The character's last glyph just barely fits
6554 in this row. */
6555 if (i == it->nglyphs - 1)
6557 /* If this is the destination position,
6558 return a position *before* it in this row,
6559 now that we know it fits in this row. */
6560 if (BUFFER_POS_REACHED_P ())
6562 it->hpos = hpos_before_this_char;
6563 it->current_x = x_before_this_char;
6564 result = MOVE_POS_MATCH_OR_ZV;
6565 break;
6568 set_iterator_to_next (it, 1);
6569 #ifdef HAVE_WINDOW_SYSTEM
6570 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6572 if (!get_next_display_element (it))
6574 result = MOVE_POS_MATCH_OR_ZV;
6575 break;
6577 if (BUFFER_POS_REACHED_P ())
6579 if (ITERATOR_AT_END_OF_LINE_P (it))
6580 result = MOVE_POS_MATCH_OR_ZV;
6581 else
6582 result = MOVE_LINE_CONTINUED;
6583 break;
6585 if (ITERATOR_AT_END_OF_LINE_P (it))
6587 result = MOVE_NEWLINE_OR_CR;
6588 break;
6591 #endif /* HAVE_WINDOW_SYSTEM */
6594 else
6596 it->current_x = x;
6597 it->max_ascent = ascent;
6598 it->max_descent = descent;
6601 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6602 IT_CHARPOS (*it)));
6603 result = MOVE_LINE_CONTINUED;
6604 break;
6606 else if (BUFFER_POS_REACHED_P ())
6607 goto buffer_pos_reached;
6608 else if (new_x > it->first_visible_x)
6610 /* Glyph is visible. Increment number of glyphs that
6611 would be displayed. */
6612 ++it->hpos;
6614 else
6616 /* Glyph is completely off the left margin of the display
6617 area. Nothing to do. */
6621 if (result != MOVE_UNDEFINED)
6622 break;
6624 else if (BUFFER_POS_REACHED_P ())
6626 buffer_pos_reached:
6627 it->current_x = x;
6628 it->max_ascent = ascent;
6629 it->max_descent = descent;
6630 result = MOVE_POS_MATCH_OR_ZV;
6631 break;
6633 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6635 /* Stop when TO_X specified and reached. This check is
6636 necessary here because of lines consisting of a line end,
6637 only. The line end will not produce any glyphs and we
6638 would never get MOVE_X_REACHED. */
6639 xassert (it->nglyphs == 0);
6640 result = MOVE_X_REACHED;
6641 break;
6644 /* Is this a line end? If yes, we're done. */
6645 if (ITERATOR_AT_END_OF_LINE_P (it))
6647 result = MOVE_NEWLINE_OR_CR;
6648 break;
6651 /* The current display element has been consumed. Advance
6652 to the next. */
6653 set_iterator_to_next (it, 1);
6655 /* Stop if lines are truncated and IT's current x-position is
6656 past the right edge of the window now. */
6657 if (it->truncate_lines_p
6658 && it->current_x >= it->last_visible_x)
6660 #ifdef HAVE_WINDOW_SYSTEM
6661 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6663 if (!get_next_display_element (it)
6664 || BUFFER_POS_REACHED_P ())
6666 result = MOVE_POS_MATCH_OR_ZV;
6667 break;
6669 if (ITERATOR_AT_END_OF_LINE_P (it))
6671 result = MOVE_NEWLINE_OR_CR;
6672 break;
6675 #endif /* HAVE_WINDOW_SYSTEM */
6676 result = MOVE_LINE_TRUNCATED;
6677 break;
6681 #undef BUFFER_POS_REACHED_P
6683 /* Restore the iterator settings altered at the beginning of this
6684 function. */
6685 it->glyph_row = saved_glyph_row;
6686 return result;
6690 /* Move IT forward until it satisfies one or more of the criteria in
6691 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6693 OP is a bit-mask that specifies where to stop, and in particular,
6694 which of those four position arguments makes a difference. See the
6695 description of enum move_operation_enum.
6697 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6698 screen line, this function will set IT to the next position >
6699 TO_CHARPOS. */
6701 void
6702 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6703 struct it *it;
6704 int to_charpos, to_x, to_y, to_vpos;
6705 int op;
6707 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6708 int line_height;
6709 int reached = 0;
6711 for (;;)
6713 if (op & MOVE_TO_VPOS)
6715 /* If no TO_CHARPOS and no TO_X specified, stop at the
6716 start of the line TO_VPOS. */
6717 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6719 if (it->vpos == to_vpos)
6721 reached = 1;
6722 break;
6724 else
6725 skip = move_it_in_display_line_to (it, -1, -1, 0);
6727 else
6729 /* TO_VPOS >= 0 means stop at TO_X in the line at
6730 TO_VPOS, or at TO_POS, whichever comes first. */
6731 if (it->vpos == to_vpos)
6733 reached = 2;
6734 break;
6737 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6739 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6741 reached = 3;
6742 break;
6744 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6746 /* We have reached TO_X but not in the line we want. */
6747 skip = move_it_in_display_line_to (it, to_charpos,
6748 -1, MOVE_TO_POS);
6749 if (skip == MOVE_POS_MATCH_OR_ZV)
6751 reached = 4;
6752 break;
6757 else if (op & MOVE_TO_Y)
6759 struct it it_backup;
6761 /* TO_Y specified means stop at TO_X in the line containing
6762 TO_Y---or at TO_CHARPOS if this is reached first. The
6763 problem is that we can't really tell whether the line
6764 contains TO_Y before we have completely scanned it, and
6765 this may skip past TO_X. What we do is to first scan to
6766 TO_X.
6768 If TO_X is not specified, use a TO_X of zero. The reason
6769 is to make the outcome of this function more predictable.
6770 If we didn't use TO_X == 0, we would stop at the end of
6771 the line which is probably not what a caller would expect
6772 to happen. */
6773 skip = move_it_in_display_line_to (it, to_charpos,
6774 ((op & MOVE_TO_X)
6775 ? to_x : 0),
6776 (MOVE_TO_X
6777 | (op & MOVE_TO_POS)));
6779 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6780 if (skip == MOVE_POS_MATCH_OR_ZV)
6782 reached = 5;
6783 break;
6786 /* If TO_X was reached, we would like to know whether TO_Y
6787 is in the line. This can only be said if we know the
6788 total line height which requires us to scan the rest of
6789 the line. */
6790 if (skip == MOVE_X_REACHED)
6792 it_backup = *it;
6793 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6794 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6795 op & MOVE_TO_POS);
6796 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6799 /* Now, decide whether TO_Y is in this line. */
6800 line_height = it->max_ascent + it->max_descent;
6801 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6803 if (to_y >= it->current_y
6804 && to_y < it->current_y + line_height)
6806 if (skip == MOVE_X_REACHED)
6807 /* If TO_Y is in this line and TO_X was reached above,
6808 we scanned too far. We have to restore IT's settings
6809 to the ones before skipping. */
6810 *it = it_backup;
6811 reached = 6;
6813 else if (skip == MOVE_X_REACHED)
6815 skip = skip2;
6816 if (skip == MOVE_POS_MATCH_OR_ZV)
6817 reached = 7;
6820 if (reached)
6821 break;
6823 else if (BUFFERP (it->object)
6824 && it->method == GET_FROM_BUFFER
6825 && IT_CHARPOS (*it) >= to_charpos)
6826 skip = MOVE_POS_MATCH_OR_ZV;
6827 else
6828 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6830 switch (skip)
6832 case MOVE_POS_MATCH_OR_ZV:
6833 reached = 8;
6834 goto out;
6836 case MOVE_NEWLINE_OR_CR:
6837 set_iterator_to_next (it, 1);
6838 it->continuation_lines_width = 0;
6839 break;
6841 case MOVE_LINE_TRUNCATED:
6842 it->continuation_lines_width = 0;
6843 reseat_at_next_visible_line_start (it, 0);
6844 if ((op & MOVE_TO_POS) != 0
6845 && IT_CHARPOS (*it) > to_charpos)
6847 reached = 9;
6848 goto out;
6850 break;
6852 case MOVE_LINE_CONTINUED:
6853 /* For continued lines ending in a tab, some of the glyphs
6854 associated with the tab are displayed on the current
6855 line. Since it->current_x does not include these glyphs,
6856 we use it->last_visible_x instead. */
6857 it->continuation_lines_width +=
6858 (it->c == '\t') ? it->last_visible_x : it->current_x;
6859 break;
6861 default:
6862 abort ();
6865 /* Reset/increment for the next run. */
6866 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6867 it->current_x = it->hpos = 0;
6868 it->current_y += it->max_ascent + it->max_descent;
6869 ++it->vpos;
6870 last_height = it->max_ascent + it->max_descent;
6871 last_max_ascent = it->max_ascent;
6872 it->max_ascent = it->max_descent = 0;
6875 out:
6877 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6881 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6883 If DY > 0, move IT backward at least that many pixels. DY = 0
6884 means move IT backward to the preceding line start or BEGV. This
6885 function may move over more than DY pixels if IT->current_y - DY
6886 ends up in the middle of a line; in this case IT->current_y will be
6887 set to the top of the line moved to. */
6889 void
6890 move_it_vertically_backward (it, dy)
6891 struct it *it;
6892 int dy;
6894 int nlines, h;
6895 struct it it2, it3;
6896 int start_pos;
6898 move_further_back:
6899 xassert (dy >= 0);
6901 start_pos = IT_CHARPOS (*it);
6903 /* Estimate how many newlines we must move back. */
6904 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6906 /* Set the iterator's position that many lines back. */
6907 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6908 back_to_previous_visible_line_start (it);
6910 /* Reseat the iterator here. When moving backward, we don't want
6911 reseat to skip forward over invisible text, set up the iterator
6912 to deliver from overlay strings at the new position etc. So,
6913 use reseat_1 here. */
6914 reseat_1 (it, it->current.pos, 1);
6916 /* We are now surely at a line start. */
6917 it->current_x = it->hpos = 0;
6918 it->continuation_lines_width = 0;
6920 /* Move forward and see what y-distance we moved. First move to the
6921 start of the next line so that we get its height. We need this
6922 height to be able to tell whether we reached the specified
6923 y-distance. */
6924 it2 = *it;
6925 it2.max_ascent = it2.max_descent = 0;
6928 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6929 MOVE_TO_POS | MOVE_TO_VPOS);
6931 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6932 xassert (IT_CHARPOS (*it) >= BEGV);
6933 it3 = it2;
6935 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6936 xassert (IT_CHARPOS (*it) >= BEGV);
6937 /* H is the actual vertical distance from the position in *IT
6938 and the starting position. */
6939 h = it2.current_y - it->current_y;
6940 /* NLINES is the distance in number of lines. */
6941 nlines = it2.vpos - it->vpos;
6943 /* Correct IT's y and vpos position
6944 so that they are relative to the starting point. */
6945 it->vpos -= nlines;
6946 it->current_y -= h;
6948 if (dy == 0)
6950 /* DY == 0 means move to the start of the screen line. The
6951 value of nlines is > 0 if continuation lines were involved. */
6952 if (nlines > 0)
6953 move_it_by_lines (it, nlines, 1);
6954 #if 0
6955 /* I think this assert is bogus if buffer contains
6956 invisible text or images. KFS. */
6957 xassert (IT_CHARPOS (*it) <= start_pos);
6958 #endif
6960 else
6962 /* The y-position we try to reach, relative to *IT.
6963 Note that H has been subtracted in front of the if-statement. */
6964 int target_y = it->current_y + h - dy;
6965 int y0 = it3.current_y;
6966 int y1 = line_bottom_y (&it3);
6967 int line_height = y1 - y0;
6969 /* If we did not reach target_y, try to move further backward if
6970 we can. If we moved too far backward, try to move forward. */
6971 if (target_y < it->current_y
6972 /* This is heuristic. In a window that's 3 lines high, with
6973 a line height of 13 pixels each, recentering with point
6974 on the bottom line will try to move -39/2 = 19 pixels
6975 backward. Try to avoid moving into the first line. */
6976 && (it->current_y - target_y
6977 > min (window_box_height (it->w), line_height * 2 / 3))
6978 && IT_CHARPOS (*it) > BEGV)
6980 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6981 target_y - it->current_y));
6982 dy = it->current_y - target_y;
6983 goto move_further_back;
6985 else if (target_y >= it->current_y + line_height
6986 && IT_CHARPOS (*it) < ZV)
6988 /* Should move forward by at least one line, maybe more.
6990 Note: Calling move_it_by_lines can be expensive on
6991 terminal frames, where compute_motion is used (via
6992 vmotion) to do the job, when there are very long lines
6993 and truncate-lines is nil. That's the reason for
6994 treating terminal frames specially here. */
6996 if (!FRAME_WINDOW_P (it->f))
6997 move_it_vertically (it, target_y - (it->current_y + line_height));
6998 else
7002 move_it_by_lines (it, 1, 1);
7004 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7007 #if 0
7008 /* I think this assert is bogus if buffer contains
7009 invisible text or images. KFS. */
7010 xassert (IT_CHARPOS (*it) >= BEGV);
7011 #endif
7017 /* Move IT by a specified amount of pixel lines DY. DY negative means
7018 move backwards. DY = 0 means move to start of screen line. At the
7019 end, IT will be on the start of a screen line. */
7021 void
7022 move_it_vertically (it, dy)
7023 struct it *it;
7024 int dy;
7026 if (dy <= 0)
7027 move_it_vertically_backward (it, -dy);
7028 else
7030 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7031 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7032 MOVE_TO_POS | MOVE_TO_Y);
7033 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7035 /* If buffer ends in ZV without a newline, move to the start of
7036 the line to satisfy the post-condition. */
7037 if (IT_CHARPOS (*it) == ZV
7038 && ZV > BEGV
7039 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7040 move_it_by_lines (it, 0, 0);
7045 /* Move iterator IT past the end of the text line it is in. */
7047 void
7048 move_it_past_eol (it)
7049 struct it *it;
7051 enum move_it_result rc;
7053 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7054 if (rc == MOVE_NEWLINE_OR_CR)
7055 set_iterator_to_next (it, 0);
7059 #if 0 /* Currently not used. */
7061 /* Return non-zero if some text between buffer positions START_CHARPOS
7062 and END_CHARPOS is invisible. IT->window is the window for text
7063 property lookup. */
7065 static int
7066 invisible_text_between_p (it, start_charpos, end_charpos)
7067 struct it *it;
7068 int start_charpos, end_charpos;
7070 Lisp_Object prop, limit;
7071 int invisible_found_p;
7073 xassert (it != NULL && start_charpos <= end_charpos);
7075 /* Is text at START invisible? */
7076 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7077 it->window);
7078 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7079 invisible_found_p = 1;
7080 else
7082 limit = Fnext_single_char_property_change (make_number (start_charpos),
7083 Qinvisible, Qnil,
7084 make_number (end_charpos));
7085 invisible_found_p = XFASTINT (limit) < end_charpos;
7088 return invisible_found_p;
7091 #endif /* 0 */
7094 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7095 negative means move up. DVPOS == 0 means move to the start of the
7096 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7097 NEED_Y_P is zero, IT->current_y will be left unchanged.
7099 Further optimization ideas: If we would know that IT->f doesn't use
7100 a face with proportional font, we could be faster for
7101 truncate-lines nil. */
7103 void
7104 move_it_by_lines (it, dvpos, need_y_p)
7105 struct it *it;
7106 int dvpos, need_y_p;
7108 struct position pos;
7110 /* The commented-out optimization uses vmotion on terminals. This
7111 gives bad results, because elements like it->what, on which
7112 callers such as pos_visible_p rely, aren't updated. */
7113 /* if (!FRAME_WINDOW_P (it->f))
7115 struct text_pos textpos;
7117 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7118 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7119 reseat (it, textpos, 1);
7120 it->vpos += pos.vpos;
7121 it->current_y += pos.vpos;
7123 else */
7125 if (dvpos == 0)
7127 /* DVPOS == 0 means move to the start of the screen line. */
7128 move_it_vertically_backward (it, 0);
7129 xassert (it->current_x == 0 && it->hpos == 0);
7130 /* Let next call to line_bottom_y calculate real line height */
7131 last_height = 0;
7133 else if (dvpos > 0)
7135 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7136 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7137 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7139 else
7141 struct it it2;
7142 int start_charpos, i;
7144 /* Start at the beginning of the screen line containing IT's
7145 position. This may actually move vertically backwards,
7146 in case of overlays, so adjust dvpos accordingly. */
7147 dvpos += it->vpos;
7148 move_it_vertically_backward (it, 0);
7149 dvpos -= it->vpos;
7151 /* Go back -DVPOS visible lines and reseat the iterator there. */
7152 start_charpos = IT_CHARPOS (*it);
7153 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7154 back_to_previous_visible_line_start (it);
7155 reseat (it, it->current.pos, 1);
7157 /* Move further back if we end up in a string or an image. */
7158 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7160 /* First try to move to start of display line. */
7161 dvpos += it->vpos;
7162 move_it_vertically_backward (it, 0);
7163 dvpos -= it->vpos;
7164 if (IT_POS_VALID_AFTER_MOVE_P (it))
7165 break;
7166 /* If start of line is still in string or image,
7167 move further back. */
7168 back_to_previous_visible_line_start (it);
7169 reseat (it, it->current.pos, 1);
7170 dvpos--;
7173 it->current_x = it->hpos = 0;
7175 /* Above call may have moved too far if continuation lines
7176 are involved. Scan forward and see if it did. */
7177 it2 = *it;
7178 it2.vpos = it2.current_y = 0;
7179 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7180 it->vpos -= it2.vpos;
7181 it->current_y -= it2.current_y;
7182 it->current_x = it->hpos = 0;
7184 /* If we moved too far back, move IT some lines forward. */
7185 if (it2.vpos > -dvpos)
7187 int delta = it2.vpos + dvpos;
7188 it2 = *it;
7189 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7190 /* Move back again if we got too far ahead. */
7191 if (IT_CHARPOS (*it) >= start_charpos)
7192 *it = it2;
7197 /* Return 1 if IT points into the middle of a display vector. */
7200 in_display_vector_p (it)
7201 struct it *it;
7203 return (it->method == GET_FROM_DISPLAY_VECTOR
7204 && it->current.dpvec_index > 0
7205 && it->dpvec + it->current.dpvec_index != it->dpend);
7209 /***********************************************************************
7210 Messages
7211 ***********************************************************************/
7214 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7215 to *Messages*. */
7217 void
7218 add_to_log (format, arg1, arg2)
7219 char *format;
7220 Lisp_Object arg1, arg2;
7222 Lisp_Object args[3];
7223 Lisp_Object msg, fmt;
7224 char *buffer;
7225 int len;
7226 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7227 USE_SAFE_ALLOCA;
7229 /* Do nothing if called asynchronously. Inserting text into
7230 a buffer may call after-change-functions and alike and
7231 that would means running Lisp asynchronously. */
7232 if (handling_signal)
7233 return;
7235 fmt = msg = Qnil;
7236 GCPRO4 (fmt, msg, arg1, arg2);
7238 args[0] = fmt = build_string (format);
7239 args[1] = arg1;
7240 args[2] = arg2;
7241 msg = Fformat (3, args);
7243 len = SBYTES (msg) + 1;
7244 SAFE_ALLOCA (buffer, char *, len);
7245 bcopy (SDATA (msg), buffer, len);
7247 message_dolog (buffer, len - 1, 1, 0);
7248 SAFE_FREE ();
7250 UNGCPRO;
7254 /* Output a newline in the *Messages* buffer if "needs" one. */
7256 void
7257 message_log_maybe_newline ()
7259 if (message_log_need_newline)
7260 message_dolog ("", 0, 1, 0);
7264 /* Add a string M of length NBYTES to the message log, optionally
7265 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7266 nonzero, means interpret the contents of M as multibyte. This
7267 function calls low-level routines in order to bypass text property
7268 hooks, etc. which might not be safe to run.
7270 This may GC (insert may run before/after change hooks),
7271 so the buffer M must NOT point to a Lisp string. */
7273 void
7274 message_dolog (m, nbytes, nlflag, multibyte)
7275 const char *m;
7276 int nbytes, nlflag, multibyte;
7278 if (!NILP (Vmemory_full))
7279 return;
7281 if (!NILP (Vmessage_log_max))
7283 struct buffer *oldbuf;
7284 Lisp_Object oldpoint, oldbegv, oldzv;
7285 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7286 int point_at_end = 0;
7287 int zv_at_end = 0;
7288 Lisp_Object old_deactivate_mark, tem;
7289 struct gcpro gcpro1;
7291 old_deactivate_mark = Vdeactivate_mark;
7292 oldbuf = current_buffer;
7293 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7294 current_buffer->undo_list = Qt;
7296 oldpoint = message_dolog_marker1;
7297 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7298 oldbegv = message_dolog_marker2;
7299 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7300 oldzv = message_dolog_marker3;
7301 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7302 GCPRO1 (old_deactivate_mark);
7304 if (PT == Z)
7305 point_at_end = 1;
7306 if (ZV == Z)
7307 zv_at_end = 1;
7309 BEGV = BEG;
7310 BEGV_BYTE = BEG_BYTE;
7311 ZV = Z;
7312 ZV_BYTE = Z_BYTE;
7313 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7315 /* Insert the string--maybe converting multibyte to single byte
7316 or vice versa, so that all the text fits the buffer. */
7317 if (multibyte
7318 && NILP (current_buffer->enable_multibyte_characters))
7320 int i, c, char_bytes;
7321 unsigned char work[1];
7323 /* Convert a multibyte string to single-byte
7324 for the *Message* buffer. */
7325 for (i = 0; i < nbytes; i += char_bytes)
7327 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7328 work[0] = (SINGLE_BYTE_CHAR_P (c)
7330 : multibyte_char_to_unibyte (c, Qnil));
7331 insert_1_both (work, 1, 1, 1, 0, 0);
7334 else if (! multibyte
7335 && ! NILP (current_buffer->enable_multibyte_characters))
7337 int i, c, char_bytes;
7338 unsigned char *msg = (unsigned char *) m;
7339 unsigned char str[MAX_MULTIBYTE_LENGTH];
7340 /* Convert a single-byte string to multibyte
7341 for the *Message* buffer. */
7342 for (i = 0; i < nbytes; i++)
7344 c = unibyte_char_to_multibyte (msg[i]);
7345 char_bytes = CHAR_STRING (c, str);
7346 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7349 else if (nbytes)
7350 insert_1 (m, nbytes, 1, 0, 0);
7352 if (nlflag)
7354 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7355 insert_1 ("\n", 1, 1, 0, 0);
7357 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7358 this_bol = PT;
7359 this_bol_byte = PT_BYTE;
7361 /* See if this line duplicates the previous one.
7362 If so, combine duplicates. */
7363 if (this_bol > BEG)
7365 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7366 prev_bol = PT;
7367 prev_bol_byte = PT_BYTE;
7369 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7370 this_bol, this_bol_byte);
7371 if (dup)
7373 del_range_both (prev_bol, prev_bol_byte,
7374 this_bol, this_bol_byte, 0);
7375 if (dup > 1)
7377 char dupstr[40];
7378 int duplen;
7380 /* If you change this format, don't forget to also
7381 change message_log_check_duplicate. */
7382 sprintf (dupstr, " [%d times]", dup);
7383 duplen = strlen (dupstr);
7384 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7385 insert_1 (dupstr, duplen, 1, 0, 1);
7390 /* If we have more than the desired maximum number of lines
7391 in the *Messages* buffer now, delete the oldest ones.
7392 This is safe because we don't have undo in this buffer. */
7394 if (NATNUMP (Vmessage_log_max))
7396 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7397 -XFASTINT (Vmessage_log_max) - 1, 0);
7398 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7401 BEGV = XMARKER (oldbegv)->charpos;
7402 BEGV_BYTE = marker_byte_position (oldbegv);
7404 if (zv_at_end)
7406 ZV = Z;
7407 ZV_BYTE = Z_BYTE;
7409 else
7411 ZV = XMARKER (oldzv)->charpos;
7412 ZV_BYTE = marker_byte_position (oldzv);
7415 if (point_at_end)
7416 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7417 else
7418 /* We can't do Fgoto_char (oldpoint) because it will run some
7419 Lisp code. */
7420 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7421 XMARKER (oldpoint)->bytepos);
7423 UNGCPRO;
7424 unchain_marker (XMARKER (oldpoint));
7425 unchain_marker (XMARKER (oldbegv));
7426 unchain_marker (XMARKER (oldzv));
7428 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7429 set_buffer_internal (oldbuf);
7430 if (NILP (tem))
7431 windows_or_buffers_changed = old_windows_or_buffers_changed;
7432 message_log_need_newline = !nlflag;
7433 Vdeactivate_mark = old_deactivate_mark;
7438 /* We are at the end of the buffer after just having inserted a newline.
7439 (Note: We depend on the fact we won't be crossing the gap.)
7440 Check to see if the most recent message looks a lot like the previous one.
7441 Return 0 if different, 1 if the new one should just replace it, or a
7442 value N > 1 if we should also append " [N times]". */
7444 static int
7445 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7446 int prev_bol, this_bol;
7447 int prev_bol_byte, this_bol_byte;
7449 int i;
7450 int len = Z_BYTE - 1 - this_bol_byte;
7451 int seen_dots = 0;
7452 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7453 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7455 for (i = 0; i < len; i++)
7457 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7458 seen_dots = 1;
7459 if (p1[i] != p2[i])
7460 return seen_dots;
7462 p1 += len;
7463 if (*p1 == '\n')
7464 return 2;
7465 if (*p1++ == ' ' && *p1++ == '[')
7467 int n = 0;
7468 while (*p1 >= '0' && *p1 <= '9')
7469 n = n * 10 + *p1++ - '0';
7470 if (strncmp (p1, " times]\n", 8) == 0)
7471 return n+1;
7473 return 0;
7477 /* Display an echo area message M with a specified length of NBYTES
7478 bytes. The string may include null characters. If M is 0, clear
7479 out any existing message, and let the mini-buffer text show
7480 through.
7482 This may GC, so the buffer M must NOT point to a Lisp string. */
7484 void
7485 message2 (m, nbytes, multibyte)
7486 const char *m;
7487 int nbytes;
7488 int multibyte;
7490 /* First flush out any partial line written with print. */
7491 message_log_maybe_newline ();
7492 if (m)
7493 message_dolog (m, nbytes, 1, multibyte);
7494 message2_nolog (m, nbytes, multibyte);
7498 /* The non-logging counterpart of message2. */
7500 void
7501 message2_nolog (m, nbytes, multibyte)
7502 const char *m;
7503 int nbytes, multibyte;
7505 struct frame *sf = SELECTED_FRAME ();
7506 message_enable_multibyte = multibyte;
7508 if (noninteractive)
7510 if (noninteractive_need_newline)
7511 putc ('\n', stderr);
7512 noninteractive_need_newline = 0;
7513 if (m)
7514 fwrite (m, nbytes, 1, stderr);
7515 if (cursor_in_echo_area == 0)
7516 fprintf (stderr, "\n");
7517 fflush (stderr);
7519 /* A null message buffer means that the frame hasn't really been
7520 initialized yet. Error messages get reported properly by
7521 cmd_error, so this must be just an informative message; toss it. */
7522 else if (INTERACTIVE
7523 && sf->glyphs_initialized_p
7524 && FRAME_MESSAGE_BUF (sf))
7526 Lisp_Object mini_window;
7527 struct frame *f;
7529 /* Get the frame containing the mini-buffer
7530 that the selected frame is using. */
7531 mini_window = FRAME_MINIBUF_WINDOW (sf);
7532 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7534 FRAME_SAMPLE_VISIBILITY (f);
7535 if (FRAME_VISIBLE_P (sf)
7536 && ! FRAME_VISIBLE_P (f))
7537 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7539 if (m)
7541 set_message (m, Qnil, nbytes, multibyte);
7542 if (minibuffer_auto_raise)
7543 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7545 else
7546 clear_message (1, 1);
7548 do_pending_window_change (0);
7549 echo_area_display (1);
7550 do_pending_window_change (0);
7551 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7552 (*frame_up_to_date_hook) (f);
7557 /* Display an echo area message M with a specified length of NBYTES
7558 bytes. The string may include null characters. If M is not a
7559 string, clear out any existing message, and let the mini-buffer
7560 text show through.
7562 This function cancels echoing. */
7564 void
7565 message3 (m, nbytes, multibyte)
7566 Lisp_Object m;
7567 int nbytes;
7568 int multibyte;
7570 struct gcpro gcpro1;
7572 GCPRO1 (m);
7573 clear_message (1,1);
7574 cancel_echoing ();
7576 /* First flush out any partial line written with print. */
7577 message_log_maybe_newline ();
7578 if (STRINGP (m))
7580 char *buffer;
7581 USE_SAFE_ALLOCA;
7583 SAFE_ALLOCA (buffer, char *, nbytes);
7584 bcopy (SDATA (m), buffer, nbytes);
7585 message_dolog (buffer, nbytes, 1, multibyte);
7586 SAFE_FREE ();
7588 message3_nolog (m, nbytes, multibyte);
7590 UNGCPRO;
7594 /* The non-logging version of message3.
7595 This does not cancel echoing, because it is used for echoing.
7596 Perhaps we need to make a separate function for echoing
7597 and make this cancel echoing. */
7599 void
7600 message3_nolog (m, nbytes, multibyte)
7601 Lisp_Object m;
7602 int nbytes, multibyte;
7604 struct frame *sf = SELECTED_FRAME ();
7605 message_enable_multibyte = multibyte;
7607 if (noninteractive)
7609 if (noninteractive_need_newline)
7610 putc ('\n', stderr);
7611 noninteractive_need_newline = 0;
7612 if (STRINGP (m))
7613 fwrite (SDATA (m), nbytes, 1, stderr);
7614 if (cursor_in_echo_area == 0)
7615 fprintf (stderr, "\n");
7616 fflush (stderr);
7618 /* A null message buffer means that the frame hasn't really been
7619 initialized yet. Error messages get reported properly by
7620 cmd_error, so this must be just an informative message; toss it. */
7621 else if (INTERACTIVE
7622 && sf->glyphs_initialized_p
7623 && FRAME_MESSAGE_BUF (sf))
7625 Lisp_Object mini_window;
7626 Lisp_Object frame;
7627 struct frame *f;
7629 /* Get the frame containing the mini-buffer
7630 that the selected frame is using. */
7631 mini_window = FRAME_MINIBUF_WINDOW (sf);
7632 frame = XWINDOW (mini_window)->frame;
7633 f = XFRAME (frame);
7635 FRAME_SAMPLE_VISIBILITY (f);
7636 if (FRAME_VISIBLE_P (sf)
7637 && !FRAME_VISIBLE_P (f))
7638 Fmake_frame_visible (frame);
7640 if (STRINGP (m) && SCHARS (m) > 0)
7642 set_message (NULL, m, nbytes, multibyte);
7643 if (minibuffer_auto_raise)
7644 Fraise_frame (frame);
7645 /* Assume we are not echoing.
7646 (If we are, echo_now will override this.) */
7647 echo_message_buffer = Qnil;
7649 else
7650 clear_message (1, 1);
7652 do_pending_window_change (0);
7653 echo_area_display (1);
7654 do_pending_window_change (0);
7655 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7656 (*frame_up_to_date_hook) (f);
7661 /* Display a null-terminated echo area message M. If M is 0, clear
7662 out any existing message, and let the mini-buffer text show through.
7664 The buffer M must continue to exist until after the echo area gets
7665 cleared or some other message gets displayed there. Do not pass
7666 text that is stored in a Lisp string. Do not pass text in a buffer
7667 that was alloca'd. */
7669 void
7670 message1 (m)
7671 char *m;
7673 message2 (m, (m ? strlen (m) : 0), 0);
7677 /* The non-logging counterpart of message1. */
7679 void
7680 message1_nolog (m)
7681 char *m;
7683 message2_nolog (m, (m ? strlen (m) : 0), 0);
7686 /* Display a message M which contains a single %s
7687 which gets replaced with STRING. */
7689 void
7690 message_with_string (m, string, log)
7691 char *m;
7692 Lisp_Object string;
7693 int log;
7695 CHECK_STRING (string);
7697 if (noninteractive)
7699 if (m)
7701 if (noninteractive_need_newline)
7702 putc ('\n', stderr);
7703 noninteractive_need_newline = 0;
7704 fprintf (stderr, m, SDATA (string));
7705 if (cursor_in_echo_area == 0)
7706 fprintf (stderr, "\n");
7707 fflush (stderr);
7710 else if (INTERACTIVE)
7712 /* The frame whose minibuffer we're going to display the message on.
7713 It may be larger than the selected frame, so we need
7714 to use its buffer, not the selected frame's buffer. */
7715 Lisp_Object mini_window;
7716 struct frame *f, *sf = SELECTED_FRAME ();
7718 /* Get the frame containing the minibuffer
7719 that the selected frame is using. */
7720 mini_window = FRAME_MINIBUF_WINDOW (sf);
7721 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7723 /* A null message buffer means that the frame hasn't really been
7724 initialized yet. Error messages get reported properly by
7725 cmd_error, so this must be just an informative message; toss it. */
7726 if (FRAME_MESSAGE_BUF (f))
7728 Lisp_Object args[2], message;
7729 struct gcpro gcpro1, gcpro2;
7731 args[0] = build_string (m);
7732 args[1] = message = string;
7733 GCPRO2 (args[0], message);
7734 gcpro1.nvars = 2;
7736 message = Fformat (2, args);
7738 if (log)
7739 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7740 else
7741 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7743 UNGCPRO;
7745 /* Print should start at the beginning of the message
7746 buffer next time. */
7747 message_buf_print = 0;
7753 /* Dump an informative message to the minibuf. If M is 0, clear out
7754 any existing message, and let the mini-buffer text show through. */
7756 /* VARARGS 1 */
7757 void
7758 message (m, a1, a2, a3)
7759 char *m;
7760 EMACS_INT a1, a2, a3;
7762 if (noninteractive)
7764 if (m)
7766 if (noninteractive_need_newline)
7767 putc ('\n', stderr);
7768 noninteractive_need_newline = 0;
7769 fprintf (stderr, m, a1, a2, a3);
7770 if (cursor_in_echo_area == 0)
7771 fprintf (stderr, "\n");
7772 fflush (stderr);
7775 else if (INTERACTIVE)
7777 /* The frame whose mini-buffer we're going to display the message
7778 on. It may be larger than the selected frame, so we need to
7779 use its buffer, not the selected frame's buffer. */
7780 Lisp_Object mini_window;
7781 struct frame *f, *sf = SELECTED_FRAME ();
7783 /* Get the frame containing the mini-buffer
7784 that the selected frame is using. */
7785 mini_window = FRAME_MINIBUF_WINDOW (sf);
7786 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7788 /* A null message buffer means that the frame hasn't really been
7789 initialized yet. Error messages get reported properly by
7790 cmd_error, so this must be just an informative message; toss
7791 it. */
7792 if (FRAME_MESSAGE_BUF (f))
7794 if (m)
7796 int len;
7797 #ifdef NO_ARG_ARRAY
7798 char *a[3];
7799 a[0] = (char *) a1;
7800 a[1] = (char *) a2;
7801 a[2] = (char *) a3;
7803 len = doprnt (FRAME_MESSAGE_BUF (f),
7804 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7805 #else
7806 len = doprnt (FRAME_MESSAGE_BUF (f),
7807 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7808 (char **) &a1);
7809 #endif /* NO_ARG_ARRAY */
7811 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7813 else
7814 message1 (0);
7816 /* Print should start at the beginning of the message
7817 buffer next time. */
7818 message_buf_print = 0;
7824 /* The non-logging version of message. */
7826 void
7827 message_nolog (m, a1, a2, a3)
7828 char *m;
7829 EMACS_INT a1, a2, a3;
7831 Lisp_Object old_log_max;
7832 old_log_max = Vmessage_log_max;
7833 Vmessage_log_max = Qnil;
7834 message (m, a1, a2, a3);
7835 Vmessage_log_max = old_log_max;
7839 /* Display the current message in the current mini-buffer. This is
7840 only called from error handlers in process.c, and is not time
7841 critical. */
7843 void
7844 update_echo_area ()
7846 if (!NILP (echo_area_buffer[0]))
7848 Lisp_Object string;
7849 string = Fcurrent_message ();
7850 message3 (string, SBYTES (string),
7851 !NILP (current_buffer->enable_multibyte_characters));
7856 /* Make sure echo area buffers in `echo_buffers' are live.
7857 If they aren't, make new ones. */
7859 static void
7860 ensure_echo_area_buffers ()
7862 int i;
7864 for (i = 0; i < 2; ++i)
7865 if (!BUFFERP (echo_buffer[i])
7866 || NILP (XBUFFER (echo_buffer[i])->name))
7868 char name[30];
7869 Lisp_Object old_buffer;
7870 int j;
7872 old_buffer = echo_buffer[i];
7873 sprintf (name, " *Echo Area %d*", i);
7874 echo_buffer[i] = Fget_buffer_create (build_string (name));
7875 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7877 for (j = 0; j < 2; ++j)
7878 if (EQ (old_buffer, echo_area_buffer[j]))
7879 echo_area_buffer[j] = echo_buffer[i];
7884 /* Call FN with args A1..A4 with either the current or last displayed
7885 echo_area_buffer as current buffer.
7887 WHICH zero means use the current message buffer
7888 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7889 from echo_buffer[] and clear it.
7891 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7892 suitable buffer from echo_buffer[] and clear it.
7894 Value is what FN returns. */
7896 static int
7897 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7898 struct window *w;
7899 int which;
7900 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7901 EMACS_INT a1;
7902 Lisp_Object a2;
7903 EMACS_INT a3, a4;
7905 Lisp_Object buffer;
7906 int this_one, the_other, clear_buffer_p, rc;
7907 int count = SPECPDL_INDEX ();
7909 /* If buffers aren't live, make new ones. */
7910 ensure_echo_area_buffers ();
7912 clear_buffer_p = 0;
7914 if (which == 0)
7915 this_one = 0, the_other = 1;
7916 else if (which > 0)
7917 this_one = 1, the_other = 0;
7919 /* Choose a suitable buffer from echo_buffer[] is we don't
7920 have one. */
7921 if (NILP (echo_area_buffer[this_one]))
7923 echo_area_buffer[this_one]
7924 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7925 ? echo_buffer[the_other]
7926 : echo_buffer[this_one]);
7927 clear_buffer_p = 1;
7930 buffer = echo_area_buffer[this_one];
7932 /* Don't get confused by reusing the buffer used for echoing
7933 for a different purpose. */
7934 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7935 cancel_echoing ();
7937 record_unwind_protect (unwind_with_echo_area_buffer,
7938 with_echo_area_buffer_unwind_data (w));
7940 /* Make the echo area buffer current. Note that for display
7941 purposes, it is not necessary that the displayed window's buffer
7942 == current_buffer, except for text property lookup. So, let's
7943 only set that buffer temporarily here without doing a full
7944 Fset_window_buffer. We must also change w->pointm, though,
7945 because otherwise an assertions in unshow_buffer fails, and Emacs
7946 aborts. */
7947 set_buffer_internal_1 (XBUFFER (buffer));
7948 if (w)
7950 w->buffer = buffer;
7951 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7954 current_buffer->undo_list = Qt;
7955 current_buffer->read_only = Qnil;
7956 specbind (Qinhibit_read_only, Qt);
7957 specbind (Qinhibit_modification_hooks, Qt);
7959 if (clear_buffer_p && Z > BEG)
7960 del_range (BEG, Z);
7962 xassert (BEGV >= BEG);
7963 xassert (ZV <= Z && ZV >= BEGV);
7965 rc = fn (a1, a2, a3, a4);
7967 xassert (BEGV >= BEG);
7968 xassert (ZV <= Z && ZV >= BEGV);
7970 unbind_to (count, Qnil);
7971 return rc;
7975 /* Save state that should be preserved around the call to the function
7976 FN called in with_echo_area_buffer. */
7978 static Lisp_Object
7979 with_echo_area_buffer_unwind_data (w)
7980 struct window *w;
7982 int i = 0;
7983 Lisp_Object vector;
7985 /* Reduce consing by keeping one vector in
7986 Vwith_echo_area_save_vector. */
7987 vector = Vwith_echo_area_save_vector;
7988 Vwith_echo_area_save_vector = Qnil;
7990 if (NILP (vector))
7991 vector = Fmake_vector (make_number (7), Qnil);
7993 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7994 AREF (vector, i) = Vdeactivate_mark, ++i;
7995 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7997 if (w)
7999 XSETWINDOW (AREF (vector, i), w); ++i;
8000 AREF (vector, i) = w->buffer; ++i;
8001 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8002 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8004 else
8006 int end = i + 4;
8007 for (; i < end; ++i)
8008 AREF (vector, i) = Qnil;
8011 xassert (i == ASIZE (vector));
8012 return vector;
8016 /* Restore global state from VECTOR which was created by
8017 with_echo_area_buffer_unwind_data. */
8019 static Lisp_Object
8020 unwind_with_echo_area_buffer (vector)
8021 Lisp_Object vector;
8023 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8024 Vdeactivate_mark = AREF (vector, 1);
8025 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8027 if (WINDOWP (AREF (vector, 3)))
8029 struct window *w;
8030 Lisp_Object buffer, charpos, bytepos;
8032 w = XWINDOW (AREF (vector, 3));
8033 buffer = AREF (vector, 4);
8034 charpos = AREF (vector, 5);
8035 bytepos = AREF (vector, 6);
8037 w->buffer = buffer;
8038 set_marker_both (w->pointm, buffer,
8039 XFASTINT (charpos), XFASTINT (bytepos));
8042 Vwith_echo_area_save_vector = vector;
8043 return Qnil;
8047 /* Set up the echo area for use by print functions. MULTIBYTE_P
8048 non-zero means we will print multibyte. */
8050 void
8051 setup_echo_area_for_printing (multibyte_p)
8052 int multibyte_p;
8054 /* If we can't find an echo area any more, exit. */
8055 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8056 Fkill_emacs (Qnil);
8058 ensure_echo_area_buffers ();
8060 if (!message_buf_print)
8062 /* A message has been output since the last time we printed.
8063 Choose a fresh echo area buffer. */
8064 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8065 echo_area_buffer[0] = echo_buffer[1];
8066 else
8067 echo_area_buffer[0] = echo_buffer[0];
8069 /* Switch to that buffer and clear it. */
8070 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8071 current_buffer->truncate_lines = Qnil;
8073 if (Z > BEG)
8075 int count = SPECPDL_INDEX ();
8076 specbind (Qinhibit_read_only, Qt);
8077 /* Note that undo recording is always disabled. */
8078 del_range (BEG, Z);
8079 unbind_to (count, Qnil);
8081 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8083 /* Set up the buffer for the multibyteness we need. */
8084 if (multibyte_p
8085 != !NILP (current_buffer->enable_multibyte_characters))
8086 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8088 /* Raise the frame containing the echo area. */
8089 if (minibuffer_auto_raise)
8091 struct frame *sf = SELECTED_FRAME ();
8092 Lisp_Object mini_window;
8093 mini_window = FRAME_MINIBUF_WINDOW (sf);
8094 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8097 message_log_maybe_newline ();
8098 message_buf_print = 1;
8100 else
8102 if (NILP (echo_area_buffer[0]))
8104 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8105 echo_area_buffer[0] = echo_buffer[1];
8106 else
8107 echo_area_buffer[0] = echo_buffer[0];
8110 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8112 /* Someone switched buffers between print requests. */
8113 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8114 current_buffer->truncate_lines = Qnil;
8120 /* Display an echo area message in window W. Value is non-zero if W's
8121 height is changed. If display_last_displayed_message_p is
8122 non-zero, display the message that was last displayed, otherwise
8123 display the current message. */
8125 static int
8126 display_echo_area (w)
8127 struct window *w;
8129 int i, no_message_p, window_height_changed_p, count;
8131 /* Temporarily disable garbage collections while displaying the echo
8132 area. This is done because a GC can print a message itself.
8133 That message would modify the echo area buffer's contents while a
8134 redisplay of the buffer is going on, and seriously confuse
8135 redisplay. */
8136 count = inhibit_garbage_collection ();
8138 /* If there is no message, we must call display_echo_area_1
8139 nevertheless because it resizes the window. But we will have to
8140 reset the echo_area_buffer in question to nil at the end because
8141 with_echo_area_buffer will sets it to an empty buffer. */
8142 i = display_last_displayed_message_p ? 1 : 0;
8143 no_message_p = NILP (echo_area_buffer[i]);
8145 window_height_changed_p
8146 = with_echo_area_buffer (w, display_last_displayed_message_p,
8147 display_echo_area_1,
8148 (EMACS_INT) w, Qnil, 0, 0);
8150 if (no_message_p)
8151 echo_area_buffer[i] = Qnil;
8153 unbind_to (count, Qnil);
8154 return window_height_changed_p;
8158 /* Helper for display_echo_area. Display the current buffer which
8159 contains the current echo area message in window W, a mini-window,
8160 a pointer to which is passed in A1. A2..A4 are currently not used.
8161 Change the height of W so that all of the message is displayed.
8162 Value is non-zero if height of W was changed. */
8164 static int
8165 display_echo_area_1 (a1, a2, a3, a4)
8166 EMACS_INT a1;
8167 Lisp_Object a2;
8168 EMACS_INT a3, a4;
8170 struct window *w = (struct window *) a1;
8171 Lisp_Object window;
8172 struct text_pos start;
8173 int window_height_changed_p = 0;
8175 /* Do this before displaying, so that we have a large enough glyph
8176 matrix for the display. If we can't get enough space for the
8177 whole text, display the last N lines. That works by setting w->start. */
8178 window_height_changed_p = resize_mini_window (w, 0);
8180 /* Use the starting position chosen by resize_mini_window. */
8181 SET_TEXT_POS_FROM_MARKER (start, w->start);
8183 /* Display. */
8184 clear_glyph_matrix (w->desired_matrix);
8185 XSETWINDOW (window, w);
8186 try_window (window, start, 0);
8188 return window_height_changed_p;
8192 /* Resize the echo area window to exactly the size needed for the
8193 currently displayed message, if there is one. If a mini-buffer
8194 is active, don't shrink it. */
8196 void
8197 resize_echo_area_exactly ()
8199 if (BUFFERP (echo_area_buffer[0])
8200 && WINDOWP (echo_area_window))
8202 struct window *w = XWINDOW (echo_area_window);
8203 int resized_p;
8204 Lisp_Object resize_exactly;
8206 if (minibuf_level == 0)
8207 resize_exactly = Qt;
8208 else
8209 resize_exactly = Qnil;
8211 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8212 (EMACS_INT) w, resize_exactly, 0, 0);
8213 if (resized_p)
8215 ++windows_or_buffers_changed;
8216 ++update_mode_lines;
8217 redisplay_internal (0);
8223 /* Callback function for with_echo_area_buffer, when used from
8224 resize_echo_area_exactly. A1 contains a pointer to the window to
8225 resize, EXACTLY non-nil means resize the mini-window exactly to the
8226 size of the text displayed. A3 and A4 are not used. Value is what
8227 resize_mini_window returns. */
8229 static int
8230 resize_mini_window_1 (a1, exactly, a3, a4)
8231 EMACS_INT a1;
8232 Lisp_Object exactly;
8233 EMACS_INT a3, a4;
8235 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8239 /* Resize mini-window W to fit the size of its contents. EXACT:P
8240 means size the window exactly to the size needed. Otherwise, it's
8241 only enlarged until W's buffer is empty.
8243 Set W->start to the right place to begin display. If the whole
8244 contents fit, start at the beginning. Otherwise, start so as
8245 to make the end of the contents appear. This is particularly
8246 important for y-or-n-p, but seems desirable generally.
8248 Value is non-zero if the window height has been changed. */
8251 resize_mini_window (w, exact_p)
8252 struct window *w;
8253 int exact_p;
8255 struct frame *f = XFRAME (w->frame);
8256 int window_height_changed_p = 0;
8258 xassert (MINI_WINDOW_P (w));
8260 /* By default, start display at the beginning. */
8261 set_marker_both (w->start, w->buffer,
8262 BUF_BEGV (XBUFFER (w->buffer)),
8263 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8265 /* Don't resize windows while redisplaying a window; it would
8266 confuse redisplay functions when the size of the window they are
8267 displaying changes from under them. Such a resizing can happen,
8268 for instance, when which-func prints a long message while
8269 we are running fontification-functions. We're running these
8270 functions with safe_call which binds inhibit-redisplay to t. */
8271 if (!NILP (Vinhibit_redisplay))
8272 return 0;
8274 /* Nil means don't try to resize. */
8275 if (NILP (Vresize_mini_windows)
8276 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8277 return 0;
8279 if (!FRAME_MINIBUF_ONLY_P (f))
8281 struct it it;
8282 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8283 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8284 int height, max_height;
8285 int unit = FRAME_LINE_HEIGHT (f);
8286 struct text_pos start;
8287 struct buffer *old_current_buffer = NULL;
8289 if (current_buffer != XBUFFER (w->buffer))
8291 old_current_buffer = current_buffer;
8292 set_buffer_internal (XBUFFER (w->buffer));
8295 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8297 /* Compute the max. number of lines specified by the user. */
8298 if (FLOATP (Vmax_mini_window_height))
8299 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8300 else if (INTEGERP (Vmax_mini_window_height))
8301 max_height = XINT (Vmax_mini_window_height);
8302 else
8303 max_height = total_height / 4;
8305 /* Correct that max. height if it's bogus. */
8306 max_height = max (1, max_height);
8307 max_height = min (total_height, max_height);
8309 /* Find out the height of the text in the window. */
8310 if (it.truncate_lines_p)
8311 height = 1;
8312 else
8314 last_height = 0;
8315 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8316 if (it.max_ascent == 0 && it.max_descent == 0)
8317 height = it.current_y + last_height;
8318 else
8319 height = it.current_y + it.max_ascent + it.max_descent;
8320 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8321 height = (height + unit - 1) / unit;
8324 /* Compute a suitable window start. */
8325 if (height > max_height)
8327 height = max_height;
8328 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8329 move_it_vertically_backward (&it, (height - 1) * unit);
8330 start = it.current.pos;
8332 else
8333 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8334 SET_MARKER_FROM_TEXT_POS (w->start, start);
8336 if (EQ (Vresize_mini_windows, Qgrow_only))
8338 /* Let it grow only, until we display an empty message, in which
8339 case the window shrinks again. */
8340 if (height > WINDOW_TOTAL_LINES (w))
8342 int old_height = WINDOW_TOTAL_LINES (w);
8343 freeze_window_starts (f, 1);
8344 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8345 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8347 else if (height < WINDOW_TOTAL_LINES (w)
8348 && (exact_p || BEGV == ZV))
8350 int old_height = WINDOW_TOTAL_LINES (w);
8351 freeze_window_starts (f, 0);
8352 shrink_mini_window (w);
8353 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8356 else
8358 /* Always resize to exact size needed. */
8359 if (height > WINDOW_TOTAL_LINES (w))
8361 int old_height = WINDOW_TOTAL_LINES (w);
8362 freeze_window_starts (f, 1);
8363 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8364 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8366 else if (height < WINDOW_TOTAL_LINES (w))
8368 int old_height = WINDOW_TOTAL_LINES (w);
8369 freeze_window_starts (f, 0);
8370 shrink_mini_window (w);
8372 if (height)
8374 freeze_window_starts (f, 1);
8375 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8378 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8382 if (old_current_buffer)
8383 set_buffer_internal (old_current_buffer);
8386 return window_height_changed_p;
8390 /* Value is the current message, a string, or nil if there is no
8391 current message. */
8393 Lisp_Object
8394 current_message ()
8396 Lisp_Object msg;
8398 if (NILP (echo_area_buffer[0]))
8399 msg = Qnil;
8400 else
8402 with_echo_area_buffer (0, 0, current_message_1,
8403 (EMACS_INT) &msg, Qnil, 0, 0);
8404 if (NILP (msg))
8405 echo_area_buffer[0] = Qnil;
8408 return msg;
8412 static int
8413 current_message_1 (a1, a2, a3, a4)
8414 EMACS_INT a1;
8415 Lisp_Object a2;
8416 EMACS_INT a3, a4;
8418 Lisp_Object *msg = (Lisp_Object *) a1;
8420 if (Z > BEG)
8421 *msg = make_buffer_string (BEG, Z, 1);
8422 else
8423 *msg = Qnil;
8424 return 0;
8428 /* Push the current message on Vmessage_stack for later restauration
8429 by restore_message. Value is non-zero if the current message isn't
8430 empty. This is a relatively infrequent operation, so it's not
8431 worth optimizing. */
8434 push_message ()
8436 Lisp_Object msg;
8437 msg = current_message ();
8438 Vmessage_stack = Fcons (msg, Vmessage_stack);
8439 return STRINGP (msg);
8443 /* Restore message display from the top of Vmessage_stack. */
8445 void
8446 restore_message ()
8448 Lisp_Object msg;
8450 xassert (CONSP (Vmessage_stack));
8451 msg = XCAR (Vmessage_stack);
8452 if (STRINGP (msg))
8453 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8454 else
8455 message3_nolog (msg, 0, 0);
8459 /* Handler for record_unwind_protect calling pop_message. */
8461 Lisp_Object
8462 pop_message_unwind (dummy)
8463 Lisp_Object dummy;
8465 pop_message ();
8466 return Qnil;
8469 /* Pop the top-most entry off Vmessage_stack. */
8471 void
8472 pop_message ()
8474 xassert (CONSP (Vmessage_stack));
8475 Vmessage_stack = XCDR (Vmessage_stack);
8479 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8480 exits. If the stack is not empty, we have a missing pop_message
8481 somewhere. */
8483 void
8484 check_message_stack ()
8486 if (!NILP (Vmessage_stack))
8487 abort ();
8491 /* Truncate to NCHARS what will be displayed in the echo area the next
8492 time we display it---but don't redisplay it now. */
8494 void
8495 truncate_echo_area (nchars)
8496 int nchars;
8498 if (nchars == 0)
8499 echo_area_buffer[0] = Qnil;
8500 /* A null message buffer means that the frame hasn't really been
8501 initialized yet. Error messages get reported properly by
8502 cmd_error, so this must be just an informative message; toss it. */
8503 else if (!noninteractive
8504 && INTERACTIVE
8505 && !NILP (echo_area_buffer[0]))
8507 struct frame *sf = SELECTED_FRAME ();
8508 if (FRAME_MESSAGE_BUF (sf))
8509 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8514 /* Helper function for truncate_echo_area. Truncate the current
8515 message to at most NCHARS characters. */
8517 static int
8518 truncate_message_1 (nchars, a2, a3, a4)
8519 EMACS_INT nchars;
8520 Lisp_Object a2;
8521 EMACS_INT a3, a4;
8523 if (BEG + nchars < Z)
8524 del_range (BEG + nchars, Z);
8525 if (Z == BEG)
8526 echo_area_buffer[0] = Qnil;
8527 return 0;
8531 /* Set the current message to a substring of S or STRING.
8533 If STRING is a Lisp string, set the message to the first NBYTES
8534 bytes from STRING. NBYTES zero means use the whole string. If
8535 STRING is multibyte, the message will be displayed multibyte.
8537 If S is not null, set the message to the first LEN bytes of S. LEN
8538 zero means use the whole string. MULTIBYTE_P non-zero means S is
8539 multibyte. Display the message multibyte in that case.
8541 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8542 to t before calling set_message_1 (which calls insert).
8545 void
8546 set_message (s, string, nbytes, multibyte_p)
8547 const char *s;
8548 Lisp_Object string;
8549 int nbytes, multibyte_p;
8551 message_enable_multibyte
8552 = ((s && multibyte_p)
8553 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8555 with_echo_area_buffer (0, 0, set_message_1,
8556 (EMACS_INT) s, string, nbytes, multibyte_p);
8557 message_buf_print = 0;
8558 help_echo_showing_p = 0;
8562 /* Helper function for set_message. Arguments have the same meaning
8563 as there, with A1 corresponding to S and A2 corresponding to STRING
8564 This function is called with the echo area buffer being
8565 current. */
8567 static int
8568 set_message_1 (a1, a2, nbytes, multibyte_p)
8569 EMACS_INT a1;
8570 Lisp_Object a2;
8571 EMACS_INT nbytes, multibyte_p;
8573 const char *s = (const char *) a1;
8574 Lisp_Object string = a2;
8576 /* Change multibyteness of the echo buffer appropriately. */
8577 if (message_enable_multibyte
8578 != !NILP (current_buffer->enable_multibyte_characters))
8579 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8581 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8583 /* Insert new message at BEG. */
8584 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8585 Ferase_buffer ();
8587 if (STRINGP (string))
8589 int nchars;
8591 if (nbytes == 0)
8592 nbytes = SBYTES (string);
8593 nchars = string_byte_to_char (string, nbytes);
8595 /* This function takes care of single/multibyte conversion. We
8596 just have to ensure that the echo area buffer has the right
8597 setting of enable_multibyte_characters. */
8598 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8600 else if (s)
8602 if (nbytes == 0)
8603 nbytes = strlen (s);
8605 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8607 /* Convert from multi-byte to single-byte. */
8608 int i, c, n;
8609 unsigned char work[1];
8611 /* Convert a multibyte string to single-byte. */
8612 for (i = 0; i < nbytes; i += n)
8614 c = string_char_and_length (s + i, nbytes - i, &n);
8615 work[0] = (SINGLE_BYTE_CHAR_P (c)
8617 : multibyte_char_to_unibyte (c, Qnil));
8618 insert_1_both (work, 1, 1, 1, 0, 0);
8621 else if (!multibyte_p
8622 && !NILP (current_buffer->enable_multibyte_characters))
8624 /* Convert from single-byte to multi-byte. */
8625 int i, c, n;
8626 const unsigned char *msg = (const unsigned char *) s;
8627 unsigned char str[MAX_MULTIBYTE_LENGTH];
8629 /* Convert a single-byte string to multibyte. */
8630 for (i = 0; i < nbytes; i++)
8632 c = unibyte_char_to_multibyte (msg[i]);
8633 n = CHAR_STRING (c, str);
8634 insert_1_both (str, 1, n, 1, 0, 0);
8637 else
8638 insert_1 (s, nbytes, 1, 0, 0);
8641 return 0;
8645 /* Clear messages. CURRENT_P non-zero means clear the current
8646 message. LAST_DISPLAYED_P non-zero means clear the message
8647 last displayed. */
8649 void
8650 clear_message (current_p, last_displayed_p)
8651 int current_p, last_displayed_p;
8653 if (current_p)
8655 echo_area_buffer[0] = Qnil;
8656 message_cleared_p = 1;
8659 if (last_displayed_p)
8660 echo_area_buffer[1] = Qnil;
8662 message_buf_print = 0;
8665 /* Clear garbaged frames.
8667 This function is used where the old redisplay called
8668 redraw_garbaged_frames which in turn called redraw_frame which in
8669 turn called clear_frame. The call to clear_frame was a source of
8670 flickering. I believe a clear_frame is not necessary. It should
8671 suffice in the new redisplay to invalidate all current matrices,
8672 and ensure a complete redisplay of all windows. */
8674 static void
8675 clear_garbaged_frames ()
8677 if (frame_garbaged)
8679 Lisp_Object tail, frame;
8680 int changed_count = 0;
8682 FOR_EACH_FRAME (tail, frame)
8684 struct frame *f = XFRAME (frame);
8686 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8688 if (f->resized_p)
8690 Fredraw_frame (frame);
8691 f->force_flush_display_p = 1;
8693 clear_current_matrices (f);
8694 changed_count++;
8695 f->garbaged = 0;
8696 f->resized_p = 0;
8700 frame_garbaged = 0;
8701 if (changed_count)
8702 ++windows_or_buffers_changed;
8707 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8708 is non-zero update selected_frame. Value is non-zero if the
8709 mini-windows height has been changed. */
8711 static int
8712 echo_area_display (update_frame_p)
8713 int update_frame_p;
8715 Lisp_Object mini_window;
8716 struct window *w;
8717 struct frame *f;
8718 int window_height_changed_p = 0;
8719 struct frame *sf = SELECTED_FRAME ();
8721 mini_window = FRAME_MINIBUF_WINDOW (sf);
8722 w = XWINDOW (mini_window);
8723 f = XFRAME (WINDOW_FRAME (w));
8725 /* Don't display if frame is invisible or not yet initialized. */
8726 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8727 return 0;
8729 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8730 #ifndef MAC_OS8
8731 #ifdef HAVE_WINDOW_SYSTEM
8732 /* When Emacs starts, selected_frame may be a visible terminal
8733 frame, even if we run under a window system. If we let this
8734 through, a message would be displayed on the terminal. */
8735 if (EQ (selected_frame, Vterminal_frame)
8736 && !NILP (Vwindow_system))
8737 return 0;
8738 #endif /* HAVE_WINDOW_SYSTEM */
8739 #endif
8741 /* Redraw garbaged frames. */
8742 if (frame_garbaged)
8743 clear_garbaged_frames ();
8745 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8747 echo_area_window = mini_window;
8748 window_height_changed_p = display_echo_area (w);
8749 w->must_be_updated_p = 1;
8751 /* Update the display, unless called from redisplay_internal.
8752 Also don't update the screen during redisplay itself. The
8753 update will happen at the end of redisplay, and an update
8754 here could cause confusion. */
8755 if (update_frame_p && !redisplaying_p)
8757 int n = 0;
8759 /* If the display update has been interrupted by pending
8760 input, update mode lines in the frame. Due to the
8761 pending input, it might have been that redisplay hasn't
8762 been called, so that mode lines above the echo area are
8763 garbaged. This looks odd, so we prevent it here. */
8764 if (!display_completed)
8765 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8767 if (window_height_changed_p
8768 /* Don't do this if Emacs is shutting down. Redisplay
8769 needs to run hooks. */
8770 && !NILP (Vrun_hooks))
8772 /* Must update other windows. Likewise as in other
8773 cases, don't let this update be interrupted by
8774 pending input. */
8775 int count = SPECPDL_INDEX ();
8776 specbind (Qredisplay_dont_pause, Qt);
8777 windows_or_buffers_changed = 1;
8778 redisplay_internal (0);
8779 unbind_to (count, Qnil);
8781 else if (FRAME_WINDOW_P (f) && n == 0)
8783 /* Window configuration is the same as before.
8784 Can do with a display update of the echo area,
8785 unless we displayed some mode lines. */
8786 update_single_window (w, 1);
8787 rif->flush_display (f);
8789 else
8790 update_frame (f, 1, 1);
8792 /* If cursor is in the echo area, make sure that the next
8793 redisplay displays the minibuffer, so that the cursor will
8794 be replaced with what the minibuffer wants. */
8795 if (cursor_in_echo_area)
8796 ++windows_or_buffers_changed;
8799 else if (!EQ (mini_window, selected_window))
8800 windows_or_buffers_changed++;
8802 /* The current message is now also the last one displayed. */
8803 echo_area_buffer[1] = echo_area_buffer[0];
8805 /* Prevent redisplay optimization in redisplay_internal by resetting
8806 this_line_start_pos. This is done because the mini-buffer now
8807 displays the message instead of its buffer text. */
8808 if (EQ (mini_window, selected_window))
8809 CHARPOS (this_line_start_pos) = 0;
8811 return window_height_changed_p;
8816 /***********************************************************************
8817 Mode Lines and Frame Titles
8818 ***********************************************************************/
8820 /* A buffer for constructing non-propertized mode-line strings and
8821 frame titles in it; allocated from the heap in init_xdisp and
8822 resized as needed in store_mode_line_noprop_char. */
8824 static char *mode_line_noprop_buf;
8826 /* The buffer's end, and a current output position in it. */
8828 static char *mode_line_noprop_buf_end;
8829 static char *mode_line_noprop_ptr;
8831 #define MODE_LINE_NOPROP_LEN(start) \
8832 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8834 static enum {
8835 MODE_LINE_DISPLAY = 0,
8836 MODE_LINE_TITLE,
8837 MODE_LINE_NOPROP,
8838 MODE_LINE_STRING
8839 } mode_line_target;
8841 /* Alist that caches the results of :propertize.
8842 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8843 static Lisp_Object mode_line_proptrans_alist;
8845 /* List of strings making up the mode-line. */
8846 static Lisp_Object mode_line_string_list;
8848 /* Base face property when building propertized mode line string. */
8849 static Lisp_Object mode_line_string_face;
8850 static Lisp_Object mode_line_string_face_prop;
8853 /* Unwind data for mode line strings */
8855 static Lisp_Object Vmode_line_unwind_vector;
8857 static Lisp_Object
8858 format_mode_line_unwind_data (obuf, save_proptrans)
8859 struct buffer *obuf;
8861 Lisp_Object vector;
8863 /* Reduce consing by keeping one vector in
8864 Vwith_echo_area_save_vector. */
8865 vector = Vmode_line_unwind_vector;
8866 Vmode_line_unwind_vector = Qnil;
8868 if (NILP (vector))
8869 vector = Fmake_vector (make_number (7), Qnil);
8871 AREF (vector, 0) = make_number (mode_line_target);
8872 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8873 AREF (vector, 2) = mode_line_string_list;
8874 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8875 AREF (vector, 4) = mode_line_string_face;
8876 AREF (vector, 5) = mode_line_string_face_prop;
8878 if (obuf)
8879 XSETBUFFER (AREF (vector, 6), obuf);
8880 else
8881 AREF (vector, 6) = Qnil;
8883 return vector;
8886 static Lisp_Object
8887 unwind_format_mode_line (vector)
8888 Lisp_Object vector;
8890 mode_line_target = XINT (AREF (vector, 0));
8891 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8892 mode_line_string_list = AREF (vector, 2);
8893 if (! EQ (AREF (vector, 3), Qt))
8894 mode_line_proptrans_alist = AREF (vector, 3);
8895 mode_line_string_face = AREF (vector, 4);
8896 mode_line_string_face_prop = AREF (vector, 5);
8898 if (!NILP (AREF (vector, 6)))
8900 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8901 AREF (vector, 6) = Qnil;
8904 Vmode_line_unwind_vector = vector;
8905 return Qnil;
8909 /* Store a single character C for the frame title in mode_line_noprop_buf.
8910 Re-allocate mode_line_noprop_buf if necessary. */
8912 static void
8913 #ifdef PROTOTYPES
8914 store_mode_line_noprop_char (char c)
8915 #else
8916 store_mode_line_noprop_char (c)
8917 char c;
8918 #endif
8920 /* If output position has reached the end of the allocated buffer,
8921 double the buffer's size. */
8922 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8924 int len = MODE_LINE_NOPROP_LEN (0);
8925 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8926 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8927 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8928 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8931 *mode_line_noprop_ptr++ = c;
8935 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8936 mode_line_noprop_ptr. STR is the string to store. Do not copy
8937 characters that yield more columns than PRECISION; PRECISION <= 0
8938 means copy the whole string. Pad with spaces until FIELD_WIDTH
8939 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8940 pad. Called from display_mode_element when it is used to build a
8941 frame title. */
8943 static int
8944 store_mode_line_noprop (str, field_width, precision)
8945 const unsigned char *str;
8946 int field_width, precision;
8948 int n = 0;
8949 int dummy, nbytes;
8951 /* Copy at most PRECISION chars from STR. */
8952 nbytes = strlen (str);
8953 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8954 while (nbytes--)
8955 store_mode_line_noprop_char (*str++);
8957 /* Fill up with spaces until FIELD_WIDTH reached. */
8958 while (field_width > 0
8959 && n < field_width)
8961 store_mode_line_noprop_char (' ');
8962 ++n;
8965 return n;
8968 /***********************************************************************
8969 Frame Titles
8970 ***********************************************************************/
8972 #ifdef HAVE_WINDOW_SYSTEM
8974 /* Set the title of FRAME, if it has changed. The title format is
8975 Vicon_title_format if FRAME is iconified, otherwise it is
8976 frame_title_format. */
8978 static void
8979 x_consider_frame_title (frame)
8980 Lisp_Object frame;
8982 struct frame *f = XFRAME (frame);
8984 if (FRAME_WINDOW_P (f)
8985 || FRAME_MINIBUF_ONLY_P (f)
8986 || f->explicit_name)
8988 /* Do we have more than one visible frame on this X display? */
8989 Lisp_Object tail;
8990 Lisp_Object fmt;
8991 int title_start;
8992 char *title;
8993 int len;
8994 struct it it;
8995 int count = SPECPDL_INDEX ();
8997 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8999 Lisp_Object other_frame = XCAR (tail);
9000 struct frame *tf = XFRAME (other_frame);
9002 if (tf != f
9003 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9004 && !FRAME_MINIBUF_ONLY_P (tf)
9005 && !EQ (other_frame, tip_frame)
9006 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9007 break;
9010 /* Set global variable indicating that multiple frames exist. */
9011 multiple_frames = CONSP (tail);
9013 /* Switch to the buffer of selected window of the frame. Set up
9014 mode_line_target so that display_mode_element will output into
9015 mode_line_noprop_buf; then display the title. */
9016 record_unwind_protect (unwind_format_mode_line,
9017 format_mode_line_unwind_data (current_buffer, 0));
9019 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9020 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9022 mode_line_target = MODE_LINE_TITLE;
9023 title_start = MODE_LINE_NOPROP_LEN (0);
9024 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9025 NULL, DEFAULT_FACE_ID);
9026 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9027 len = MODE_LINE_NOPROP_LEN (title_start);
9028 title = mode_line_noprop_buf + title_start;
9029 unbind_to (count, Qnil);
9031 /* Set the title only if it's changed. This avoids consing in
9032 the common case where it hasn't. (If it turns out that we've
9033 already wasted too much time by walking through the list with
9034 display_mode_element, then we might need to optimize at a
9035 higher level than this.) */
9036 if (! STRINGP (f->name)
9037 || SBYTES (f->name) != len
9038 || bcmp (title, SDATA (f->name), len) != 0)
9039 x_implicitly_set_name (f, make_string (title, len), Qnil);
9043 #endif /* not HAVE_WINDOW_SYSTEM */
9048 /***********************************************************************
9049 Menu Bars
9050 ***********************************************************************/
9053 /* Prepare for redisplay by updating menu-bar item lists when
9054 appropriate. This can call eval. */
9056 void
9057 prepare_menu_bars ()
9059 int all_windows;
9060 struct gcpro gcpro1, gcpro2;
9061 struct frame *f;
9062 Lisp_Object tooltip_frame;
9064 #ifdef HAVE_WINDOW_SYSTEM
9065 tooltip_frame = tip_frame;
9066 #else
9067 tooltip_frame = Qnil;
9068 #endif
9070 /* Update all frame titles based on their buffer names, etc. We do
9071 this before the menu bars so that the buffer-menu will show the
9072 up-to-date frame titles. */
9073 #ifdef HAVE_WINDOW_SYSTEM
9074 if (windows_or_buffers_changed || update_mode_lines)
9076 Lisp_Object tail, frame;
9078 FOR_EACH_FRAME (tail, frame)
9080 f = XFRAME (frame);
9081 if (!EQ (frame, tooltip_frame)
9082 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9083 x_consider_frame_title (frame);
9086 #endif /* HAVE_WINDOW_SYSTEM */
9088 /* Update the menu bar item lists, if appropriate. This has to be
9089 done before any actual redisplay or generation of display lines. */
9090 all_windows = (update_mode_lines
9091 || buffer_shared > 1
9092 || windows_or_buffers_changed);
9093 if (all_windows)
9095 Lisp_Object tail, frame;
9096 int count = SPECPDL_INDEX ();
9097 /* 1 means that update_menu_bar has run its hooks
9098 so any further calls to update_menu_bar shouldn't do so again. */
9099 int menu_bar_hooks_run = 0;
9101 record_unwind_save_match_data ();
9103 FOR_EACH_FRAME (tail, frame)
9105 f = XFRAME (frame);
9107 /* Ignore tooltip frame. */
9108 if (EQ (frame, tooltip_frame))
9109 continue;
9111 /* If a window on this frame changed size, report that to
9112 the user and clear the size-change flag. */
9113 if (FRAME_WINDOW_SIZES_CHANGED (f))
9115 Lisp_Object functions;
9117 /* Clear flag first in case we get an error below. */
9118 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9119 functions = Vwindow_size_change_functions;
9120 GCPRO2 (tail, functions);
9122 while (CONSP (functions))
9124 call1 (XCAR (functions), frame);
9125 functions = XCDR (functions);
9127 UNGCPRO;
9130 GCPRO1 (tail);
9131 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9132 #ifdef HAVE_WINDOW_SYSTEM
9133 update_tool_bar (f, 0);
9134 #ifdef MAC_OS
9135 mac_update_title_bar (f, 0);
9136 #endif
9137 #endif
9138 UNGCPRO;
9141 unbind_to (count, Qnil);
9143 else
9145 struct frame *sf = SELECTED_FRAME ();
9146 update_menu_bar (sf, 1, 0);
9147 #ifdef HAVE_WINDOW_SYSTEM
9148 update_tool_bar (sf, 1);
9149 #ifdef MAC_OS
9150 mac_update_title_bar (sf, 1);
9151 #endif
9152 #endif
9155 /* Motif needs this. See comment in xmenu.c. Turn it off when
9156 pending_menu_activation is not defined. */
9157 #ifdef USE_X_TOOLKIT
9158 pending_menu_activation = 0;
9159 #endif
9163 /* Update the menu bar item list for frame F. This has to be done
9164 before we start to fill in any display lines, because it can call
9165 eval.
9167 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9169 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9170 already ran the menu bar hooks for this redisplay, so there
9171 is no need to run them again. The return value is the
9172 updated value of this flag, to pass to the next call. */
9174 static int
9175 update_menu_bar (f, save_match_data, hooks_run)
9176 struct frame *f;
9177 int save_match_data;
9178 int hooks_run;
9180 Lisp_Object window;
9181 register struct window *w;
9183 /* If called recursively during a menu update, do nothing. This can
9184 happen when, for instance, an activate-menubar-hook causes a
9185 redisplay. */
9186 if (inhibit_menubar_update)
9187 return hooks_run;
9189 window = FRAME_SELECTED_WINDOW (f);
9190 w = XWINDOW (window);
9192 #if 0 /* The if statement below this if statement used to include the
9193 condition !NILP (w->update_mode_line), rather than using
9194 update_mode_lines directly, and this if statement may have
9195 been added to make that condition work. Now the if
9196 statement below matches its comment, this isn't needed. */
9197 if (update_mode_lines)
9198 w->update_mode_line = Qt;
9199 #endif
9201 if (FRAME_WINDOW_P (f)
9203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9204 || defined (USE_GTK)
9205 FRAME_EXTERNAL_MENU_BAR (f)
9206 #else
9207 FRAME_MENU_BAR_LINES (f) > 0
9208 #endif
9209 : FRAME_MENU_BAR_LINES (f) > 0)
9211 /* If the user has switched buffers or windows, we need to
9212 recompute to reflect the new bindings. But we'll
9213 recompute when update_mode_lines is set too; that means
9214 that people can use force-mode-line-update to request
9215 that the menu bar be recomputed. The adverse effect on
9216 the rest of the redisplay algorithm is about the same as
9217 windows_or_buffers_changed anyway. */
9218 if (windows_or_buffers_changed
9219 /* This used to test w->update_mode_line, but we believe
9220 there is no need to recompute the menu in that case. */
9221 || update_mode_lines
9222 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9223 < BUF_MODIFF (XBUFFER (w->buffer)))
9224 != !NILP (w->last_had_star))
9225 || ((!NILP (Vtransient_mark_mode)
9226 && !NILP (XBUFFER (w->buffer)->mark_active))
9227 != !NILP (w->region_showing)))
9229 struct buffer *prev = current_buffer;
9230 int count = SPECPDL_INDEX ();
9232 specbind (Qinhibit_menubar_update, Qt);
9234 set_buffer_internal_1 (XBUFFER (w->buffer));
9235 if (save_match_data)
9236 record_unwind_save_match_data ();
9237 if (NILP (Voverriding_local_map_menu_flag))
9239 specbind (Qoverriding_terminal_local_map, Qnil);
9240 specbind (Qoverriding_local_map, Qnil);
9243 if (!hooks_run)
9245 /* Run the Lucid hook. */
9246 safe_run_hooks (Qactivate_menubar_hook);
9248 /* If it has changed current-menubar from previous value,
9249 really recompute the menu-bar from the value. */
9250 if (! NILP (Vlucid_menu_bar_dirty_flag))
9251 call0 (Qrecompute_lucid_menubar);
9253 safe_run_hooks (Qmenu_bar_update_hook);
9255 hooks_run = 1;
9258 XSETFRAME (Vmenu_updating_frame, f);
9259 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9261 /* Redisplay the menu bar in case we changed it. */
9262 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9263 || defined (USE_GTK)
9264 if (FRAME_WINDOW_P (f))
9266 #ifdef MAC_OS
9267 /* All frames on Mac OS share the same menubar. So only
9268 the selected frame should be allowed to set it. */
9269 if (f == SELECTED_FRAME ())
9270 #endif
9271 set_frame_menubar (f, 0, 0);
9273 else
9274 /* On a terminal screen, the menu bar is an ordinary screen
9275 line, and this makes it get updated. */
9276 w->update_mode_line = Qt;
9277 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9278 /* In the non-toolkit version, the menu bar is an ordinary screen
9279 line, and this makes it get updated. */
9280 w->update_mode_line = Qt;
9281 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9283 unbind_to (count, Qnil);
9284 set_buffer_internal_1 (prev);
9288 return hooks_run;
9293 /***********************************************************************
9294 Output Cursor
9295 ***********************************************************************/
9297 #ifdef HAVE_WINDOW_SYSTEM
9299 /* EXPORT:
9300 Nominal cursor position -- where to draw output.
9301 HPOS and VPOS are window relative glyph matrix coordinates.
9302 X and Y are window relative pixel coordinates. */
9304 struct cursor_pos output_cursor;
9307 /* EXPORT:
9308 Set the global variable output_cursor to CURSOR. All cursor
9309 positions are relative to updated_window. */
9311 void
9312 set_output_cursor (cursor)
9313 struct cursor_pos *cursor;
9315 output_cursor.hpos = cursor->hpos;
9316 output_cursor.vpos = cursor->vpos;
9317 output_cursor.x = cursor->x;
9318 output_cursor.y = cursor->y;
9322 /* EXPORT for RIF:
9323 Set a nominal cursor position.
9325 HPOS and VPOS are column/row positions in a window glyph matrix. X
9326 and Y are window text area relative pixel positions.
9328 If this is done during an update, updated_window will contain the
9329 window that is being updated and the position is the future output
9330 cursor position for that window. If updated_window is null, use
9331 selected_window and display the cursor at the given position. */
9333 void
9334 x_cursor_to (vpos, hpos, y, x)
9335 int vpos, hpos, y, x;
9337 struct window *w;
9339 /* If updated_window is not set, work on selected_window. */
9340 if (updated_window)
9341 w = updated_window;
9342 else
9343 w = XWINDOW (selected_window);
9345 /* Set the output cursor. */
9346 output_cursor.hpos = hpos;
9347 output_cursor.vpos = vpos;
9348 output_cursor.x = x;
9349 output_cursor.y = y;
9351 /* If not called as part of an update, really display the cursor.
9352 This will also set the cursor position of W. */
9353 if (updated_window == NULL)
9355 BLOCK_INPUT;
9356 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9357 if (rif->flush_display_optional)
9358 rif->flush_display_optional (SELECTED_FRAME ());
9359 UNBLOCK_INPUT;
9363 #endif /* HAVE_WINDOW_SYSTEM */
9366 /***********************************************************************
9367 Tool-bars
9368 ***********************************************************************/
9370 #ifdef HAVE_WINDOW_SYSTEM
9372 /* Where the mouse was last time we reported a mouse event. */
9374 FRAME_PTR last_mouse_frame;
9376 /* Tool-bar item index of the item on which a mouse button was pressed
9377 or -1. */
9379 int last_tool_bar_item;
9382 /* Update the tool-bar item list for frame F. This has to be done
9383 before we start to fill in any display lines. Called from
9384 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9385 and restore it here. */
9387 static void
9388 update_tool_bar (f, save_match_data)
9389 struct frame *f;
9390 int save_match_data;
9392 #ifdef USE_GTK
9393 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9394 #else
9395 int do_update = WINDOWP (f->tool_bar_window)
9396 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9397 #endif
9399 if (do_update)
9401 Lisp_Object window;
9402 struct window *w;
9404 window = FRAME_SELECTED_WINDOW (f);
9405 w = XWINDOW (window);
9407 /* If the user has switched buffers or windows, we need to
9408 recompute to reflect the new bindings. But we'll
9409 recompute when update_mode_lines is set too; that means
9410 that people can use force-mode-line-update to request
9411 that the menu bar be recomputed. The adverse effect on
9412 the rest of the redisplay algorithm is about the same as
9413 windows_or_buffers_changed anyway. */
9414 if (windows_or_buffers_changed
9415 || !NILP (w->update_mode_line)
9416 || update_mode_lines
9417 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9418 < BUF_MODIFF (XBUFFER (w->buffer)))
9419 != !NILP (w->last_had_star))
9420 || ((!NILP (Vtransient_mark_mode)
9421 && !NILP (XBUFFER (w->buffer)->mark_active))
9422 != !NILP (w->region_showing)))
9424 struct buffer *prev = current_buffer;
9425 int count = SPECPDL_INDEX ();
9426 Lisp_Object new_tool_bar;
9427 int new_n_tool_bar;
9428 struct gcpro gcpro1;
9430 /* Set current_buffer to the buffer of the selected
9431 window of the frame, so that we get the right local
9432 keymaps. */
9433 set_buffer_internal_1 (XBUFFER (w->buffer));
9435 /* Save match data, if we must. */
9436 if (save_match_data)
9437 record_unwind_save_match_data ();
9439 /* Make sure that we don't accidentally use bogus keymaps. */
9440 if (NILP (Voverriding_local_map_menu_flag))
9442 specbind (Qoverriding_terminal_local_map, Qnil);
9443 specbind (Qoverriding_local_map, Qnil);
9446 GCPRO1 (new_tool_bar);
9448 /* Build desired tool-bar items from keymaps. */
9449 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9450 &new_n_tool_bar);
9452 /* Redisplay the tool-bar if we changed it. */
9453 if (new_n_tool_bar != f->n_tool_bar_items
9454 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9456 /* Redisplay that happens asynchronously due to an expose event
9457 may access f->tool_bar_items. Make sure we update both
9458 variables within BLOCK_INPUT so no such event interrupts. */
9459 BLOCK_INPUT;
9460 f->tool_bar_items = new_tool_bar;
9461 f->n_tool_bar_items = new_n_tool_bar;
9462 w->update_mode_line = Qt;
9463 UNBLOCK_INPUT;
9466 UNGCPRO;
9468 unbind_to (count, Qnil);
9469 set_buffer_internal_1 (prev);
9475 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9476 F's desired tool-bar contents. F->tool_bar_items must have
9477 been set up previously by calling prepare_menu_bars. */
9479 static void
9480 build_desired_tool_bar_string (f)
9481 struct frame *f;
9483 int i, size, size_needed;
9484 struct gcpro gcpro1, gcpro2, gcpro3;
9485 Lisp_Object image, plist, props;
9487 image = plist = props = Qnil;
9488 GCPRO3 (image, plist, props);
9490 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9491 Otherwise, make a new string. */
9493 /* The size of the string we might be able to reuse. */
9494 size = (STRINGP (f->desired_tool_bar_string)
9495 ? SCHARS (f->desired_tool_bar_string)
9496 : 0);
9498 /* We need one space in the string for each image. */
9499 size_needed = f->n_tool_bar_items;
9501 /* Reuse f->desired_tool_bar_string, if possible. */
9502 if (size < size_needed || NILP (f->desired_tool_bar_string))
9503 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9504 make_number (' '));
9505 else
9507 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9508 Fremove_text_properties (make_number (0), make_number (size),
9509 props, f->desired_tool_bar_string);
9512 /* Put a `display' property on the string for the images to display,
9513 put a `menu_item' property on tool-bar items with a value that
9514 is the index of the item in F's tool-bar item vector. */
9515 for (i = 0; i < f->n_tool_bar_items; ++i)
9517 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9519 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9520 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9521 int hmargin, vmargin, relief, idx, end;
9522 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9524 /* If image is a vector, choose the image according to the
9525 button state. */
9526 image = PROP (TOOL_BAR_ITEM_IMAGES);
9527 if (VECTORP (image))
9529 if (enabled_p)
9530 idx = (selected_p
9531 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9532 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9533 else
9534 idx = (selected_p
9535 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9536 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9538 xassert (ASIZE (image) >= idx);
9539 image = AREF (image, idx);
9541 else
9542 idx = -1;
9544 /* Ignore invalid image specifications. */
9545 if (!valid_image_p (image))
9546 continue;
9548 /* Display the tool-bar button pressed, or depressed. */
9549 plist = Fcopy_sequence (XCDR (image));
9551 /* Compute margin and relief to draw. */
9552 relief = (tool_bar_button_relief >= 0
9553 ? tool_bar_button_relief
9554 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9555 hmargin = vmargin = relief;
9557 if (INTEGERP (Vtool_bar_button_margin)
9558 && XINT (Vtool_bar_button_margin) > 0)
9560 hmargin += XFASTINT (Vtool_bar_button_margin);
9561 vmargin += XFASTINT (Vtool_bar_button_margin);
9563 else if (CONSP (Vtool_bar_button_margin))
9565 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9566 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9567 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9569 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9570 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9571 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9574 if (auto_raise_tool_bar_buttons_p)
9576 /* Add a `:relief' property to the image spec if the item is
9577 selected. */
9578 if (selected_p)
9580 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9581 hmargin -= relief;
9582 vmargin -= relief;
9585 else
9587 /* If image is selected, display it pressed, i.e. with a
9588 negative relief. If it's not selected, display it with a
9589 raised relief. */
9590 plist = Fplist_put (plist, QCrelief,
9591 (selected_p
9592 ? make_number (-relief)
9593 : make_number (relief)));
9594 hmargin -= relief;
9595 vmargin -= relief;
9598 /* Put a margin around the image. */
9599 if (hmargin || vmargin)
9601 if (hmargin == vmargin)
9602 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9603 else
9604 plist = Fplist_put (plist, QCmargin,
9605 Fcons (make_number (hmargin),
9606 make_number (vmargin)));
9609 /* If button is not enabled, and we don't have special images
9610 for the disabled state, make the image appear disabled by
9611 applying an appropriate algorithm to it. */
9612 if (!enabled_p && idx < 0)
9613 plist = Fplist_put (plist, QCconversion, Qdisabled);
9615 /* Put a `display' text property on the string for the image to
9616 display. Put a `menu-item' property on the string that gives
9617 the start of this item's properties in the tool-bar items
9618 vector. */
9619 image = Fcons (Qimage, plist);
9620 props = list4 (Qdisplay, image,
9621 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9623 /* Let the last image hide all remaining spaces in the tool bar
9624 string. The string can be longer than needed when we reuse a
9625 previous string. */
9626 if (i + 1 == f->n_tool_bar_items)
9627 end = SCHARS (f->desired_tool_bar_string);
9628 else
9629 end = i + 1;
9630 Fadd_text_properties (make_number (i), make_number (end),
9631 props, f->desired_tool_bar_string);
9632 #undef PROP
9635 UNGCPRO;
9639 /* Display one line of the tool-bar of frame IT->f.
9641 HEIGHT specifies the desired height of the tool-bar line.
9642 If the actual height of the glyph row is less than HEIGHT, the
9643 row's height is increased to HEIGHT, and the icons are centered
9644 vertically in the new height.
9646 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9647 count a final empty row in case the tool-bar width exactly matches
9648 the window width.
9651 static void
9652 display_tool_bar_line (it, height)
9653 struct it *it;
9654 int height;
9656 struct glyph_row *row = it->glyph_row;
9657 int max_x = it->last_visible_x;
9658 struct glyph *last;
9660 prepare_desired_row (row);
9661 row->y = it->current_y;
9663 /* Note that this isn't made use of if the face hasn't a box,
9664 so there's no need to check the face here. */
9665 it->start_of_box_run_p = 1;
9667 while (it->current_x < max_x)
9669 int x, n_glyphs_before, i, nglyphs;
9670 struct it it_before;
9672 /* Get the next display element. */
9673 if (!get_next_display_element (it))
9675 /* Don't count empty row if we are counting needed tool-bar lines. */
9676 if (height < 0 && !it->hpos)
9677 return;
9678 break;
9681 /* Produce glyphs. */
9682 n_glyphs_before = row->used[TEXT_AREA];
9683 it_before = *it;
9685 PRODUCE_GLYPHS (it);
9687 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9688 i = 0;
9689 x = it_before.current_x;
9690 while (i < nglyphs)
9692 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9694 if (x + glyph->pixel_width > max_x)
9696 /* Glyph doesn't fit on line. Backtrack. */
9697 row->used[TEXT_AREA] = n_glyphs_before;
9698 *it = it_before;
9699 /* If this is the only glyph on this line, it will never fit on the
9700 toolbar, so skip it. But ensure there is at least one glyph,
9701 so we don't accidentally disable the tool-bar. */
9702 if (n_glyphs_before == 0
9703 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9704 break;
9705 goto out;
9708 ++it->hpos;
9709 x += glyph->pixel_width;
9710 ++i;
9713 /* Stop at line ends. */
9714 if (ITERATOR_AT_END_OF_LINE_P (it))
9715 break;
9717 set_iterator_to_next (it, 1);
9720 out:;
9722 row->displays_text_p = row->used[TEXT_AREA] != 0;
9724 /* Use default face for the border below the tool bar.
9726 FIXME: When auto-resize-tool-bars is grow-only, there is
9727 no additional border below the possibly empty tool-bar lines.
9728 So to make the extra empty lines look "normal", we have to
9729 use the tool-bar face for the border too. */
9730 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9731 it->face_id = DEFAULT_FACE_ID;
9733 extend_face_to_end_of_line (it);
9734 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9735 last->right_box_line_p = 1;
9736 if (last == row->glyphs[TEXT_AREA])
9737 last->left_box_line_p = 1;
9739 /* Make line the desired height and center it vertically. */
9740 if ((height -= it->max_ascent + it->max_descent) > 0)
9742 /* Don't add more than one line height. */
9743 height %= FRAME_LINE_HEIGHT (it->f);
9744 it->max_ascent += height / 2;
9745 it->max_descent += (height + 1) / 2;
9748 compute_line_metrics (it);
9750 /* If line is empty, make it occupy the rest of the tool-bar. */
9751 if (!row->displays_text_p)
9753 row->height = row->phys_height = it->last_visible_y - row->y;
9754 row->visible_height = row->height;
9755 row->ascent = row->phys_ascent = 0;
9756 row->extra_line_spacing = 0;
9759 row->full_width_p = 1;
9760 row->continued_p = 0;
9761 row->truncated_on_left_p = 0;
9762 row->truncated_on_right_p = 0;
9764 it->current_x = it->hpos = 0;
9765 it->current_y += row->height;
9766 ++it->vpos;
9767 ++it->glyph_row;
9771 /* Max tool-bar height. */
9773 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9774 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9776 /* Value is the number of screen lines needed to make all tool-bar
9777 items of frame F visible. The number of actual rows needed is
9778 returned in *N_ROWS if non-NULL. */
9780 static int
9781 tool_bar_lines_needed (f, n_rows)
9782 struct frame *f;
9783 int *n_rows;
9785 struct window *w = XWINDOW (f->tool_bar_window);
9786 struct it it;
9787 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9788 the desired matrix, so use (unused) mode-line row as temporary row to
9789 avoid destroying the first tool-bar row. */
9790 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9792 /* Initialize an iterator for iteration over
9793 F->desired_tool_bar_string in the tool-bar window of frame F. */
9794 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9795 it.first_visible_x = 0;
9796 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9797 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9799 while (!ITERATOR_AT_END_P (&it))
9801 clear_glyph_row (temp_row);
9802 it.glyph_row = temp_row;
9803 display_tool_bar_line (&it, -1);
9805 clear_glyph_row (temp_row);
9807 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9808 if (n_rows)
9809 *n_rows = it.vpos > 0 ? it.vpos : -1;
9811 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9815 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9816 0, 1, 0,
9817 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9818 (frame)
9819 Lisp_Object frame;
9821 struct frame *f;
9822 struct window *w;
9823 int nlines = 0;
9825 if (NILP (frame))
9826 frame = selected_frame;
9827 else
9828 CHECK_FRAME (frame);
9829 f = XFRAME (frame);
9831 if (WINDOWP (f->tool_bar_window)
9832 || (w = XWINDOW (f->tool_bar_window),
9833 WINDOW_TOTAL_LINES (w) > 0))
9835 update_tool_bar (f, 1);
9836 if (f->n_tool_bar_items)
9838 build_desired_tool_bar_string (f);
9839 nlines = tool_bar_lines_needed (f, NULL);
9843 return make_number (nlines);
9847 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9848 height should be changed. */
9850 static int
9851 redisplay_tool_bar (f)
9852 struct frame *f;
9854 struct window *w;
9855 struct it it;
9856 struct glyph_row *row;
9858 #ifdef USE_GTK
9859 if (FRAME_EXTERNAL_TOOL_BAR (f))
9860 update_frame_tool_bar (f);
9861 return 0;
9862 #endif
9864 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9865 do anything. This means you must start with tool-bar-lines
9866 non-zero to get the auto-sizing effect. Or in other words, you
9867 can turn off tool-bars by specifying tool-bar-lines zero. */
9868 if (!WINDOWP (f->tool_bar_window)
9869 || (w = XWINDOW (f->tool_bar_window),
9870 WINDOW_TOTAL_LINES (w) == 0))
9871 return 0;
9873 /* Set up an iterator for the tool-bar window. */
9874 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9875 it.first_visible_x = 0;
9876 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9877 row = it.glyph_row;
9879 /* Build a string that represents the contents of the tool-bar. */
9880 build_desired_tool_bar_string (f);
9881 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9883 if (f->n_tool_bar_rows == 0)
9885 int nlines;
9887 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9888 nlines != WINDOW_TOTAL_LINES (w)))
9890 extern Lisp_Object Qtool_bar_lines;
9891 Lisp_Object frame;
9892 int old_height = WINDOW_TOTAL_LINES (w);
9894 XSETFRAME (frame, f);
9895 Fmodify_frame_parameters (frame,
9896 Fcons (Fcons (Qtool_bar_lines,
9897 make_number (nlines)),
9898 Qnil));
9899 if (WINDOW_TOTAL_LINES (w) != old_height)
9901 clear_glyph_matrix (w->desired_matrix);
9902 fonts_changed_p = 1;
9903 return 1;
9908 /* Display as many lines as needed to display all tool-bar items. */
9910 if (f->n_tool_bar_rows > 0)
9912 int border, rows, height, extra;
9914 if (INTEGERP (Vtool_bar_border))
9915 border = XINT (Vtool_bar_border);
9916 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9917 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9918 else if (EQ (Vtool_bar_border, Qborder_width))
9919 border = f->border_width;
9920 else
9921 border = 0;
9922 if (border < 0)
9923 border = 0;
9925 rows = f->n_tool_bar_rows;
9926 height = max (1, (it.last_visible_y - border) / rows);
9927 extra = it.last_visible_y - border - height * rows;
9929 while (it.current_y < it.last_visible_y)
9931 int h = 0;
9932 if (extra > 0 && rows-- > 0)
9934 h = (extra + rows - 1) / rows;
9935 extra -= h;
9937 display_tool_bar_line (&it, height + h);
9940 else
9942 while (it.current_y < it.last_visible_y)
9943 display_tool_bar_line (&it, 0);
9946 /* It doesn't make much sense to try scrolling in the tool-bar
9947 window, so don't do it. */
9948 w->desired_matrix->no_scrolling_p = 1;
9949 w->must_be_updated_p = 1;
9951 if (!NILP (Vauto_resize_tool_bars))
9953 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9954 int change_height_p = 0;
9956 /* If we couldn't display everything, change the tool-bar's
9957 height if there is room for more. */
9958 if (IT_STRING_CHARPOS (it) < it.end_charpos
9959 && it.current_y < max_tool_bar_height)
9960 change_height_p = 1;
9962 row = it.glyph_row - 1;
9964 /* If there are blank lines at the end, except for a partially
9965 visible blank line at the end that is smaller than
9966 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9967 if (!row->displays_text_p
9968 && row->height >= FRAME_LINE_HEIGHT (f))
9969 change_height_p = 1;
9971 /* If row displays tool-bar items, but is partially visible,
9972 change the tool-bar's height. */
9973 if (row->displays_text_p
9974 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9975 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9976 change_height_p = 1;
9978 /* Resize windows as needed by changing the `tool-bar-lines'
9979 frame parameter. */
9980 if (change_height_p)
9982 extern Lisp_Object Qtool_bar_lines;
9983 Lisp_Object frame;
9984 int old_height = WINDOW_TOTAL_LINES (w);
9985 int nrows;
9986 int nlines = tool_bar_lines_needed (f, &nrows);
9988 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
9989 && !f->minimize_tool_bar_window_p)
9990 ? (nlines > old_height)
9991 : (nlines != old_height));
9992 f->minimize_tool_bar_window_p = 0;
9994 if (change_height_p)
9996 XSETFRAME (frame, f);
9997 Fmodify_frame_parameters (frame,
9998 Fcons (Fcons (Qtool_bar_lines,
9999 make_number (nlines)),
10000 Qnil));
10001 if (WINDOW_TOTAL_LINES (w) != old_height)
10003 clear_glyph_matrix (w->desired_matrix);
10004 f->n_tool_bar_rows = nrows;
10005 fonts_changed_p = 1;
10006 return 1;
10012 f->minimize_tool_bar_window_p = 0;
10013 return 0;
10017 /* Get information about the tool-bar item which is displayed in GLYPH
10018 on frame F. Return in *PROP_IDX the index where tool-bar item
10019 properties start in F->tool_bar_items. Value is zero if
10020 GLYPH doesn't display a tool-bar item. */
10022 static int
10023 tool_bar_item_info (f, glyph, prop_idx)
10024 struct frame *f;
10025 struct glyph *glyph;
10026 int *prop_idx;
10028 Lisp_Object prop;
10029 int success_p;
10030 int charpos;
10032 /* This function can be called asynchronously, which means we must
10033 exclude any possibility that Fget_text_property signals an
10034 error. */
10035 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10036 charpos = max (0, charpos);
10038 /* Get the text property `menu-item' at pos. The value of that
10039 property is the start index of this item's properties in
10040 F->tool_bar_items. */
10041 prop = Fget_text_property (make_number (charpos),
10042 Qmenu_item, f->current_tool_bar_string);
10043 if (INTEGERP (prop))
10045 *prop_idx = XINT (prop);
10046 success_p = 1;
10048 else
10049 success_p = 0;
10051 return success_p;
10055 /* Get information about the tool-bar item at position X/Y on frame F.
10056 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10057 the current matrix of the tool-bar window of F, or NULL if not
10058 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10059 item in F->tool_bar_items. Value is
10061 -1 if X/Y is not on a tool-bar item
10062 0 if X/Y is on the same item that was highlighted before.
10063 1 otherwise. */
10065 static int
10066 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10067 struct frame *f;
10068 int x, y;
10069 struct glyph **glyph;
10070 int *hpos, *vpos, *prop_idx;
10072 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10073 struct window *w = XWINDOW (f->tool_bar_window);
10074 int area;
10076 /* Find the glyph under X/Y. */
10077 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10078 if (*glyph == NULL)
10079 return -1;
10081 /* Get the start of this tool-bar item's properties in
10082 f->tool_bar_items. */
10083 if (!tool_bar_item_info (f, *glyph, prop_idx))
10084 return -1;
10086 /* Is mouse on the highlighted item? */
10087 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10088 && *vpos >= dpyinfo->mouse_face_beg_row
10089 && *vpos <= dpyinfo->mouse_face_end_row
10090 && (*vpos > dpyinfo->mouse_face_beg_row
10091 || *hpos >= dpyinfo->mouse_face_beg_col)
10092 && (*vpos < dpyinfo->mouse_face_end_row
10093 || *hpos < dpyinfo->mouse_face_end_col
10094 || dpyinfo->mouse_face_past_end))
10095 return 0;
10097 return 1;
10101 /* EXPORT:
10102 Handle mouse button event on the tool-bar of frame F, at
10103 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10104 0 for button release. MODIFIERS is event modifiers for button
10105 release. */
10107 void
10108 handle_tool_bar_click (f, x, y, down_p, modifiers)
10109 struct frame *f;
10110 int x, y, down_p;
10111 unsigned int modifiers;
10113 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10114 struct window *w = XWINDOW (f->tool_bar_window);
10115 int hpos, vpos, prop_idx;
10116 struct glyph *glyph;
10117 Lisp_Object enabled_p;
10119 /* If not on the highlighted tool-bar item, return. */
10120 frame_to_window_pixel_xy (w, &x, &y);
10121 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10122 return;
10124 /* If item is disabled, do nothing. */
10125 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10126 if (NILP (enabled_p))
10127 return;
10129 if (down_p)
10131 /* Show item in pressed state. */
10132 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10133 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10134 last_tool_bar_item = prop_idx;
10136 else
10138 Lisp_Object key, frame;
10139 struct input_event event;
10140 EVENT_INIT (event);
10142 /* Show item in released state. */
10143 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10144 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10146 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10148 XSETFRAME (frame, f);
10149 event.kind = TOOL_BAR_EVENT;
10150 event.frame_or_window = frame;
10151 event.arg = frame;
10152 kbd_buffer_store_event (&event);
10154 event.kind = TOOL_BAR_EVENT;
10155 event.frame_or_window = frame;
10156 event.arg = key;
10157 event.modifiers = modifiers;
10158 kbd_buffer_store_event (&event);
10159 last_tool_bar_item = -1;
10164 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10165 tool-bar window-relative coordinates X/Y. Called from
10166 note_mouse_highlight. */
10168 static void
10169 note_tool_bar_highlight (f, x, y)
10170 struct frame *f;
10171 int x, y;
10173 Lisp_Object window = f->tool_bar_window;
10174 struct window *w = XWINDOW (window);
10175 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10176 int hpos, vpos;
10177 struct glyph *glyph;
10178 struct glyph_row *row;
10179 int i;
10180 Lisp_Object enabled_p;
10181 int prop_idx;
10182 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10183 int mouse_down_p, rc;
10185 /* Function note_mouse_highlight is called with negative x(y
10186 values when mouse moves outside of the frame. */
10187 if (x <= 0 || y <= 0)
10189 clear_mouse_face (dpyinfo);
10190 return;
10193 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10194 if (rc < 0)
10196 /* Not on tool-bar item. */
10197 clear_mouse_face (dpyinfo);
10198 return;
10200 else if (rc == 0)
10201 /* On same tool-bar item as before. */
10202 goto set_help_echo;
10204 clear_mouse_face (dpyinfo);
10206 /* Mouse is down, but on different tool-bar item? */
10207 mouse_down_p = (dpyinfo->grabbed
10208 && f == last_mouse_frame
10209 && FRAME_LIVE_P (f));
10210 if (mouse_down_p
10211 && last_tool_bar_item != prop_idx)
10212 return;
10214 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10215 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10217 /* If tool-bar item is not enabled, don't highlight it. */
10218 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10219 if (!NILP (enabled_p))
10221 /* Compute the x-position of the glyph. In front and past the
10222 image is a space. We include this in the highlighted area. */
10223 row = MATRIX_ROW (w->current_matrix, vpos);
10224 for (i = x = 0; i < hpos; ++i)
10225 x += row->glyphs[TEXT_AREA][i].pixel_width;
10227 /* Record this as the current active region. */
10228 dpyinfo->mouse_face_beg_col = hpos;
10229 dpyinfo->mouse_face_beg_row = vpos;
10230 dpyinfo->mouse_face_beg_x = x;
10231 dpyinfo->mouse_face_beg_y = row->y;
10232 dpyinfo->mouse_face_past_end = 0;
10234 dpyinfo->mouse_face_end_col = hpos + 1;
10235 dpyinfo->mouse_face_end_row = vpos;
10236 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10237 dpyinfo->mouse_face_end_y = row->y;
10238 dpyinfo->mouse_face_window = window;
10239 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10241 /* Display it as active. */
10242 show_mouse_face (dpyinfo, draw);
10243 dpyinfo->mouse_face_image_state = draw;
10246 set_help_echo:
10248 /* Set help_echo_string to a help string to display for this tool-bar item.
10249 XTread_socket does the rest. */
10250 help_echo_object = help_echo_window = Qnil;
10251 help_echo_pos = -1;
10252 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10253 if (NILP (help_echo_string))
10254 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10257 #endif /* HAVE_WINDOW_SYSTEM */
10261 /************************************************************************
10262 Horizontal scrolling
10263 ************************************************************************/
10265 static int hscroll_window_tree P_ ((Lisp_Object));
10266 static int hscroll_windows P_ ((Lisp_Object));
10268 /* For all leaf windows in the window tree rooted at WINDOW, set their
10269 hscroll value so that PT is (i) visible in the window, and (ii) so
10270 that it is not within a certain margin at the window's left and
10271 right border. Value is non-zero if any window's hscroll has been
10272 changed. */
10274 static int
10275 hscroll_window_tree (window)
10276 Lisp_Object window;
10278 int hscrolled_p = 0;
10279 int hscroll_relative_p = FLOATP (Vhscroll_step);
10280 int hscroll_step_abs = 0;
10281 double hscroll_step_rel = 0;
10283 if (hscroll_relative_p)
10285 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10286 if (hscroll_step_rel < 0)
10288 hscroll_relative_p = 0;
10289 hscroll_step_abs = 0;
10292 else if (INTEGERP (Vhscroll_step))
10294 hscroll_step_abs = XINT (Vhscroll_step);
10295 if (hscroll_step_abs < 0)
10296 hscroll_step_abs = 0;
10298 else
10299 hscroll_step_abs = 0;
10301 while (WINDOWP (window))
10303 struct window *w = XWINDOW (window);
10305 if (WINDOWP (w->hchild))
10306 hscrolled_p |= hscroll_window_tree (w->hchild);
10307 else if (WINDOWP (w->vchild))
10308 hscrolled_p |= hscroll_window_tree (w->vchild);
10309 else if (w->cursor.vpos >= 0)
10311 int h_margin;
10312 int text_area_width;
10313 struct glyph_row *current_cursor_row
10314 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10315 struct glyph_row *desired_cursor_row
10316 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10317 struct glyph_row *cursor_row
10318 = (desired_cursor_row->enabled_p
10319 ? desired_cursor_row
10320 : current_cursor_row);
10322 text_area_width = window_box_width (w, TEXT_AREA);
10324 /* Scroll when cursor is inside this scroll margin. */
10325 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10327 if ((XFASTINT (w->hscroll)
10328 && w->cursor.x <= h_margin)
10329 || (cursor_row->enabled_p
10330 && cursor_row->truncated_on_right_p
10331 && (w->cursor.x >= text_area_width - h_margin)))
10333 struct it it;
10334 int hscroll;
10335 struct buffer *saved_current_buffer;
10336 int pt;
10337 int wanted_x;
10339 /* Find point in a display of infinite width. */
10340 saved_current_buffer = current_buffer;
10341 current_buffer = XBUFFER (w->buffer);
10343 if (w == XWINDOW (selected_window))
10344 pt = BUF_PT (current_buffer);
10345 else
10347 pt = marker_position (w->pointm);
10348 pt = max (BEGV, pt);
10349 pt = min (ZV, pt);
10352 /* Move iterator to pt starting at cursor_row->start in
10353 a line with infinite width. */
10354 init_to_row_start (&it, w, cursor_row);
10355 it.last_visible_x = INFINITY;
10356 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10357 current_buffer = saved_current_buffer;
10359 /* Position cursor in window. */
10360 if (!hscroll_relative_p && hscroll_step_abs == 0)
10361 hscroll = max (0, (it.current_x
10362 - (ITERATOR_AT_END_OF_LINE_P (&it)
10363 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10364 : (text_area_width / 2))))
10365 / FRAME_COLUMN_WIDTH (it.f);
10366 else if (w->cursor.x >= text_area_width - h_margin)
10368 if (hscroll_relative_p)
10369 wanted_x = text_area_width * (1 - hscroll_step_rel)
10370 - h_margin;
10371 else
10372 wanted_x = text_area_width
10373 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10374 - h_margin;
10375 hscroll
10376 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10378 else
10380 if (hscroll_relative_p)
10381 wanted_x = text_area_width * hscroll_step_rel
10382 + h_margin;
10383 else
10384 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10385 + h_margin;
10386 hscroll
10387 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10389 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10391 /* Don't call Fset_window_hscroll if value hasn't
10392 changed because it will prevent redisplay
10393 optimizations. */
10394 if (XFASTINT (w->hscroll) != hscroll)
10396 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10397 w->hscroll = make_number (hscroll);
10398 hscrolled_p = 1;
10403 window = w->next;
10406 /* Value is non-zero if hscroll of any leaf window has been changed. */
10407 return hscrolled_p;
10411 /* Set hscroll so that cursor is visible and not inside horizontal
10412 scroll margins for all windows in the tree rooted at WINDOW. See
10413 also hscroll_window_tree above. Value is non-zero if any window's
10414 hscroll has been changed. If it has, desired matrices on the frame
10415 of WINDOW are cleared. */
10417 static int
10418 hscroll_windows (window)
10419 Lisp_Object window;
10421 int hscrolled_p;
10423 if (automatic_hscrolling_p)
10425 hscrolled_p = hscroll_window_tree (window);
10426 if (hscrolled_p)
10427 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10429 else
10430 hscrolled_p = 0;
10431 return hscrolled_p;
10436 /************************************************************************
10437 Redisplay
10438 ************************************************************************/
10440 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10441 to a non-zero value. This is sometimes handy to have in a debugger
10442 session. */
10444 #if GLYPH_DEBUG
10446 /* First and last unchanged row for try_window_id. */
10448 int debug_first_unchanged_at_end_vpos;
10449 int debug_last_unchanged_at_beg_vpos;
10451 /* Delta vpos and y. */
10453 int debug_dvpos, debug_dy;
10455 /* Delta in characters and bytes for try_window_id. */
10457 int debug_delta, debug_delta_bytes;
10459 /* Values of window_end_pos and window_end_vpos at the end of
10460 try_window_id. */
10462 EMACS_INT debug_end_pos, debug_end_vpos;
10464 /* Append a string to W->desired_matrix->method. FMT is a printf
10465 format string. A1...A9 are a supplement for a variable-length
10466 argument list. If trace_redisplay_p is non-zero also printf the
10467 resulting string to stderr. */
10469 static void
10470 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10471 struct window *w;
10472 char *fmt;
10473 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10475 char buffer[512];
10476 char *method = w->desired_matrix->method;
10477 int len = strlen (method);
10478 int size = sizeof w->desired_matrix->method;
10479 int remaining = size - len - 1;
10481 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10482 if (len && remaining)
10484 method[len] = '|';
10485 --remaining, ++len;
10488 strncpy (method + len, buffer, remaining);
10490 if (trace_redisplay_p)
10491 fprintf (stderr, "%p (%s): %s\n",
10493 ((BUFFERP (w->buffer)
10494 && STRINGP (XBUFFER (w->buffer)->name))
10495 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10496 : "no buffer"),
10497 buffer);
10500 #endif /* GLYPH_DEBUG */
10503 /* Value is non-zero if all changes in window W, which displays
10504 current_buffer, are in the text between START and END. START is a
10505 buffer position, END is given as a distance from Z. Used in
10506 redisplay_internal for display optimization. */
10508 static INLINE int
10509 text_outside_line_unchanged_p (w, start, end)
10510 struct window *w;
10511 int start, end;
10513 int unchanged_p = 1;
10515 /* If text or overlays have changed, see where. */
10516 if (XFASTINT (w->last_modified) < MODIFF
10517 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10519 /* Gap in the line? */
10520 if (GPT < start || Z - GPT < end)
10521 unchanged_p = 0;
10523 /* Changes start in front of the line, or end after it? */
10524 if (unchanged_p
10525 && (BEG_UNCHANGED < start - 1
10526 || END_UNCHANGED < end))
10527 unchanged_p = 0;
10529 /* If selective display, can't optimize if changes start at the
10530 beginning of the line. */
10531 if (unchanged_p
10532 && INTEGERP (current_buffer->selective_display)
10533 && XINT (current_buffer->selective_display) > 0
10534 && (BEG_UNCHANGED < start || GPT <= start))
10535 unchanged_p = 0;
10537 /* If there are overlays at the start or end of the line, these
10538 may have overlay strings with newlines in them. A change at
10539 START, for instance, may actually concern the display of such
10540 overlay strings as well, and they are displayed on different
10541 lines. So, quickly rule out this case. (For the future, it
10542 might be desirable to implement something more telling than
10543 just BEG/END_UNCHANGED.) */
10544 if (unchanged_p)
10546 if (BEG + BEG_UNCHANGED == start
10547 && overlay_touches_p (start))
10548 unchanged_p = 0;
10549 if (END_UNCHANGED == end
10550 && overlay_touches_p (Z - end))
10551 unchanged_p = 0;
10555 return unchanged_p;
10559 /* Do a frame update, taking possible shortcuts into account. This is
10560 the main external entry point for redisplay.
10562 If the last redisplay displayed an echo area message and that message
10563 is no longer requested, we clear the echo area or bring back the
10564 mini-buffer if that is in use. */
10566 void
10567 redisplay ()
10569 redisplay_internal (0);
10573 static Lisp_Object
10574 overlay_arrow_string_or_property (var)
10575 Lisp_Object var;
10577 Lisp_Object val;
10579 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10580 return val;
10582 return Voverlay_arrow_string;
10585 /* Return 1 if there are any overlay-arrows in current_buffer. */
10586 static int
10587 overlay_arrow_in_current_buffer_p ()
10589 Lisp_Object vlist;
10591 for (vlist = Voverlay_arrow_variable_list;
10592 CONSP (vlist);
10593 vlist = XCDR (vlist))
10595 Lisp_Object var = XCAR (vlist);
10596 Lisp_Object val;
10598 if (!SYMBOLP (var))
10599 continue;
10600 val = find_symbol_value (var);
10601 if (MARKERP (val)
10602 && current_buffer == XMARKER (val)->buffer)
10603 return 1;
10605 return 0;
10609 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10610 has changed. */
10612 static int
10613 overlay_arrows_changed_p ()
10615 Lisp_Object vlist;
10617 for (vlist = Voverlay_arrow_variable_list;
10618 CONSP (vlist);
10619 vlist = XCDR (vlist))
10621 Lisp_Object var = XCAR (vlist);
10622 Lisp_Object val, pstr;
10624 if (!SYMBOLP (var))
10625 continue;
10626 val = find_symbol_value (var);
10627 if (!MARKERP (val))
10628 continue;
10629 if (! EQ (COERCE_MARKER (val),
10630 Fget (var, Qlast_arrow_position))
10631 || ! (pstr = overlay_arrow_string_or_property (var),
10632 EQ (pstr, Fget (var, Qlast_arrow_string))))
10633 return 1;
10635 return 0;
10638 /* Mark overlay arrows to be updated on next redisplay. */
10640 static void
10641 update_overlay_arrows (up_to_date)
10642 int up_to_date;
10644 Lisp_Object vlist;
10646 for (vlist = Voverlay_arrow_variable_list;
10647 CONSP (vlist);
10648 vlist = XCDR (vlist))
10650 Lisp_Object var = XCAR (vlist);
10652 if (!SYMBOLP (var))
10653 continue;
10655 if (up_to_date > 0)
10657 Lisp_Object val = find_symbol_value (var);
10658 Fput (var, Qlast_arrow_position,
10659 COERCE_MARKER (val));
10660 Fput (var, Qlast_arrow_string,
10661 overlay_arrow_string_or_property (var));
10663 else if (up_to_date < 0
10664 || !NILP (Fget (var, Qlast_arrow_position)))
10666 Fput (var, Qlast_arrow_position, Qt);
10667 Fput (var, Qlast_arrow_string, Qt);
10673 /* Return overlay arrow string to display at row.
10674 Return integer (bitmap number) for arrow bitmap in left fringe.
10675 Return nil if no overlay arrow. */
10677 static Lisp_Object
10678 overlay_arrow_at_row (it, row)
10679 struct it *it;
10680 struct glyph_row *row;
10682 Lisp_Object vlist;
10684 for (vlist = Voverlay_arrow_variable_list;
10685 CONSP (vlist);
10686 vlist = XCDR (vlist))
10688 Lisp_Object var = XCAR (vlist);
10689 Lisp_Object val;
10691 if (!SYMBOLP (var))
10692 continue;
10694 val = find_symbol_value (var);
10696 if (MARKERP (val)
10697 && current_buffer == XMARKER (val)->buffer
10698 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10700 if (FRAME_WINDOW_P (it->f)
10701 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10703 #ifdef HAVE_WINDOW_SYSTEM
10704 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10706 int fringe_bitmap;
10707 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10708 return make_number (fringe_bitmap);
10710 #endif
10711 return make_number (-1); /* Use default arrow bitmap */
10713 return overlay_arrow_string_or_property (var);
10717 return Qnil;
10720 /* Return 1 if point moved out of or into a composition. Otherwise
10721 return 0. PREV_BUF and PREV_PT are the last point buffer and
10722 position. BUF and PT are the current point buffer and position. */
10725 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10726 struct buffer *prev_buf, *buf;
10727 int prev_pt, pt;
10729 int start, end;
10730 Lisp_Object prop;
10731 Lisp_Object buffer;
10733 XSETBUFFER (buffer, buf);
10734 /* Check a composition at the last point if point moved within the
10735 same buffer. */
10736 if (prev_buf == buf)
10738 if (prev_pt == pt)
10739 /* Point didn't move. */
10740 return 0;
10742 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10743 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10744 && COMPOSITION_VALID_P (start, end, prop)
10745 && start < prev_pt && end > prev_pt)
10746 /* The last point was within the composition. Return 1 iff
10747 point moved out of the composition. */
10748 return (pt <= start || pt >= end);
10751 /* Check a composition at the current point. */
10752 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10753 && find_composition (pt, -1, &start, &end, &prop, buffer)
10754 && COMPOSITION_VALID_P (start, end, prop)
10755 && start < pt && end > pt);
10759 /* Reconsider the setting of B->clip_changed which is displayed
10760 in window W. */
10762 static INLINE void
10763 reconsider_clip_changes (w, b)
10764 struct window *w;
10765 struct buffer *b;
10767 if (b->clip_changed
10768 && !NILP (w->window_end_valid)
10769 && w->current_matrix->buffer == b
10770 && w->current_matrix->zv == BUF_ZV (b)
10771 && w->current_matrix->begv == BUF_BEGV (b))
10772 b->clip_changed = 0;
10774 /* If display wasn't paused, and W is not a tool bar window, see if
10775 point has been moved into or out of a composition. In that case,
10776 we set b->clip_changed to 1 to force updating the screen. If
10777 b->clip_changed has already been set to 1, we can skip this
10778 check. */
10779 if (!b->clip_changed
10780 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10782 int pt;
10784 if (w == XWINDOW (selected_window))
10785 pt = BUF_PT (current_buffer);
10786 else
10787 pt = marker_position (w->pointm);
10789 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10790 || pt != XINT (w->last_point))
10791 && check_point_in_composition (w->current_matrix->buffer,
10792 XINT (w->last_point),
10793 XBUFFER (w->buffer), pt))
10794 b->clip_changed = 1;
10799 /* Select FRAME to forward the values of frame-local variables into C
10800 variables so that the redisplay routines can access those values
10801 directly. */
10803 static void
10804 select_frame_for_redisplay (frame)
10805 Lisp_Object frame;
10807 Lisp_Object tail, sym, val;
10808 Lisp_Object old = selected_frame;
10810 selected_frame = frame;
10812 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10813 if (CONSP (XCAR (tail))
10814 && (sym = XCAR (XCAR (tail)),
10815 SYMBOLP (sym))
10816 && (sym = indirect_variable (sym),
10817 val = SYMBOL_VALUE (sym),
10818 (BUFFER_LOCAL_VALUEP (val)
10819 || SOME_BUFFER_LOCAL_VALUEP (val)))
10820 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10821 /* Use find_symbol_value rather than Fsymbol_value
10822 to avoid an error if it is void. */
10823 find_symbol_value (sym);
10825 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10826 if (CONSP (XCAR (tail))
10827 && (sym = XCAR (XCAR (tail)),
10828 SYMBOLP (sym))
10829 && (sym = indirect_variable (sym),
10830 val = SYMBOL_VALUE (sym),
10831 (BUFFER_LOCAL_VALUEP (val)
10832 || SOME_BUFFER_LOCAL_VALUEP (val)))
10833 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10834 find_symbol_value (sym);
10838 #define STOP_POLLING \
10839 do { if (! polling_stopped_here) stop_polling (); \
10840 polling_stopped_here = 1; } while (0)
10842 #define RESUME_POLLING \
10843 do { if (polling_stopped_here) start_polling (); \
10844 polling_stopped_here = 0; } while (0)
10847 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10848 response to any user action; therefore, we should preserve the echo
10849 area. (Actually, our caller does that job.) Perhaps in the future
10850 avoid recentering windows if it is not necessary; currently that
10851 causes some problems. */
10853 static void
10854 redisplay_internal (preserve_echo_area)
10855 int preserve_echo_area;
10857 struct window *w = XWINDOW (selected_window);
10858 struct frame *f;
10859 int pause;
10860 int must_finish = 0;
10861 struct text_pos tlbufpos, tlendpos;
10862 int number_of_visible_frames;
10863 int count, count1;
10864 struct frame *sf;
10865 int polling_stopped_here = 0;
10867 /* Non-zero means redisplay has to consider all windows on all
10868 frames. Zero means, only selected_window is considered. */
10869 int consider_all_windows_p;
10871 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10873 /* No redisplay if running in batch mode or frame is not yet fully
10874 initialized, or redisplay is explicitly turned off by setting
10875 Vinhibit_redisplay. */
10876 if (noninteractive
10877 || !NILP (Vinhibit_redisplay))
10878 return;
10880 /* Don't examine these until after testing Vinhibit_redisplay.
10881 When Emacs is shutting down, perhaps because its connection to
10882 X has dropped, we should not look at them at all. */
10883 f = XFRAME (w->frame);
10884 sf = SELECTED_FRAME ();
10886 if (!f->glyphs_initialized_p)
10887 return;
10889 /* The flag redisplay_performed_directly_p is set by
10890 direct_output_for_insert when it already did the whole screen
10891 update necessary. */
10892 if (redisplay_performed_directly_p)
10894 redisplay_performed_directly_p = 0;
10895 if (!hscroll_windows (selected_window))
10896 return;
10899 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10900 if (popup_activated ())
10901 return;
10902 #endif
10904 /* I don't think this happens but let's be paranoid. */
10905 if (redisplaying_p)
10906 return;
10908 /* Record a function that resets redisplaying_p to its old value
10909 when we leave this function. */
10910 count = SPECPDL_INDEX ();
10911 record_unwind_protect (unwind_redisplay,
10912 Fcons (make_number (redisplaying_p), selected_frame));
10913 ++redisplaying_p;
10914 specbind (Qinhibit_free_realized_faces, Qnil);
10917 Lisp_Object tail, frame;
10919 FOR_EACH_FRAME (tail, frame)
10921 struct frame *f = XFRAME (frame);
10922 f->already_hscrolled_p = 0;
10926 retry:
10927 pause = 0;
10928 reconsider_clip_changes (w, current_buffer);
10929 last_escape_glyph_frame = NULL;
10930 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10932 /* If new fonts have been loaded that make a glyph matrix adjustment
10933 necessary, do it. */
10934 if (fonts_changed_p)
10936 adjust_glyphs (NULL);
10937 ++windows_or_buffers_changed;
10938 fonts_changed_p = 0;
10941 /* If face_change_count is non-zero, init_iterator will free all
10942 realized faces, which includes the faces referenced from current
10943 matrices. So, we can't reuse current matrices in this case. */
10944 if (face_change_count)
10945 ++windows_or_buffers_changed;
10947 if (! FRAME_WINDOW_P (sf)
10948 && previous_terminal_frame != sf)
10950 /* Since frames on an ASCII terminal share the same display
10951 area, displaying a different frame means redisplay the whole
10952 thing. */
10953 windows_or_buffers_changed++;
10954 SET_FRAME_GARBAGED (sf);
10955 XSETFRAME (Vterminal_frame, sf);
10957 previous_terminal_frame = sf;
10959 /* Set the visible flags for all frames. Do this before checking
10960 for resized or garbaged frames; they want to know if their frames
10961 are visible. See the comment in frame.h for
10962 FRAME_SAMPLE_VISIBILITY. */
10964 Lisp_Object tail, frame;
10966 number_of_visible_frames = 0;
10968 FOR_EACH_FRAME (tail, frame)
10970 struct frame *f = XFRAME (frame);
10972 FRAME_SAMPLE_VISIBILITY (f);
10973 if (FRAME_VISIBLE_P (f))
10974 ++number_of_visible_frames;
10975 clear_desired_matrices (f);
10979 /* Notice any pending interrupt request to change frame size. */
10980 do_pending_window_change (1);
10982 /* Clear frames marked as garbaged. */
10983 if (frame_garbaged)
10984 clear_garbaged_frames ();
10986 /* Build menubar and tool-bar items. */
10987 if (NILP (Vmemory_full))
10988 prepare_menu_bars ();
10990 if (windows_or_buffers_changed)
10991 update_mode_lines++;
10993 /* Detect case that we need to write or remove a star in the mode line. */
10994 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10996 w->update_mode_line = Qt;
10997 if (buffer_shared > 1)
10998 update_mode_lines++;
11001 /* Avoid invocation of point motion hooks by `current_column' below. */
11002 count1 = SPECPDL_INDEX ();
11003 specbind (Qinhibit_point_motion_hooks, Qt);
11005 /* If %c is in the mode line, update it if needed. */
11006 if (!NILP (w->column_number_displayed)
11007 /* This alternative quickly identifies a common case
11008 where no change is needed. */
11009 && !(PT == XFASTINT (w->last_point)
11010 && XFASTINT (w->last_modified) >= MODIFF
11011 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11012 && (XFASTINT (w->column_number_displayed)
11013 != (int) current_column ())) /* iftc */
11014 w->update_mode_line = Qt;
11016 unbind_to (count1, Qnil);
11018 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11020 /* The variable buffer_shared is set in redisplay_window and
11021 indicates that we redisplay a buffer in different windows. See
11022 there. */
11023 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11024 || cursor_type_changed);
11026 /* If specs for an arrow have changed, do thorough redisplay
11027 to ensure we remove any arrow that should no longer exist. */
11028 if (overlay_arrows_changed_p ())
11029 consider_all_windows_p = windows_or_buffers_changed = 1;
11031 /* Normally the message* functions will have already displayed and
11032 updated the echo area, but the frame may have been trashed, or
11033 the update may have been preempted, so display the echo area
11034 again here. Checking message_cleared_p captures the case that
11035 the echo area should be cleared. */
11036 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11037 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11038 || (message_cleared_p
11039 && minibuf_level == 0
11040 /* If the mini-window is currently selected, this means the
11041 echo-area doesn't show through. */
11042 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11044 int window_height_changed_p = echo_area_display (0);
11045 must_finish = 1;
11047 /* If we don't display the current message, don't clear the
11048 message_cleared_p flag, because, if we did, we wouldn't clear
11049 the echo area in the next redisplay which doesn't preserve
11050 the echo area. */
11051 if (!display_last_displayed_message_p)
11052 message_cleared_p = 0;
11054 if (fonts_changed_p)
11055 goto retry;
11056 else if (window_height_changed_p)
11058 consider_all_windows_p = 1;
11059 ++update_mode_lines;
11060 ++windows_or_buffers_changed;
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 ();
11069 else if (EQ (selected_window, minibuf_window)
11070 && (current_buffer->clip_changed
11071 || XFASTINT (w->last_modified) < MODIFF
11072 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11073 && resize_mini_window (w, 0))
11075 /* Resized active mini-window to fit the size of what it is
11076 showing if its contents might have changed. */
11077 must_finish = 1;
11078 consider_all_windows_p = 1;
11079 ++windows_or_buffers_changed;
11080 ++update_mode_lines;
11082 /* If window configuration was changed, frames may have been
11083 marked garbaged. Clear them or we will experience
11084 surprises wrt scrolling. */
11085 if (frame_garbaged)
11086 clear_garbaged_frames ();
11090 /* If showing the region, and mark has changed, we must redisplay
11091 the whole window. The assignment to this_line_start_pos prevents
11092 the optimization directly below this if-statement. */
11093 if (((!NILP (Vtransient_mark_mode)
11094 && !NILP (XBUFFER (w->buffer)->mark_active))
11095 != !NILP (w->region_showing))
11096 || (!NILP (w->region_showing)
11097 && !EQ (w->region_showing,
11098 Fmarker_position (XBUFFER (w->buffer)->mark))))
11099 CHARPOS (this_line_start_pos) = 0;
11101 /* Optimize the case that only the line containing the cursor in the
11102 selected window has changed. Variables starting with this_ are
11103 set in display_line and record information about the line
11104 containing the cursor. */
11105 tlbufpos = this_line_start_pos;
11106 tlendpos = this_line_end_pos;
11107 if (!consider_all_windows_p
11108 && CHARPOS (tlbufpos) > 0
11109 && NILP (w->update_mode_line)
11110 && !current_buffer->clip_changed
11111 && !current_buffer->prevent_redisplay_optimizations_p
11112 && FRAME_VISIBLE_P (XFRAME (w->frame))
11113 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11114 /* Make sure recorded data applies to current buffer, etc. */
11115 && this_line_buffer == current_buffer
11116 && current_buffer == XBUFFER (w->buffer)
11117 && NILP (w->force_start)
11118 && NILP (w->optional_new_start)
11119 /* Point must be on the line that we have info recorded about. */
11120 && PT >= CHARPOS (tlbufpos)
11121 && PT <= Z - CHARPOS (tlendpos)
11122 /* All text outside that line, including its final newline,
11123 must be unchanged */
11124 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11125 CHARPOS (tlendpos)))
11127 if (CHARPOS (tlbufpos) > BEGV
11128 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11129 && (CHARPOS (tlbufpos) == ZV
11130 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11131 /* Former continuation line has disappeared by becoming empty */
11132 goto cancel;
11133 else if (XFASTINT (w->last_modified) < MODIFF
11134 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11135 || MINI_WINDOW_P (w))
11137 /* We have to handle the case of continuation around a
11138 wide-column character (See the comment in indent.c around
11139 line 885).
11141 For instance, in the following case:
11143 -------- Insert --------
11144 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11145 J_I_ ==> J_I_ `^^' are cursors.
11146 ^^ ^^
11147 -------- --------
11149 As we have to redraw the line above, we should goto cancel. */
11151 struct it it;
11152 int line_height_before = this_line_pixel_height;
11154 /* Note that start_display will handle the case that the
11155 line starting at tlbufpos is a continuation lines. */
11156 start_display (&it, w, tlbufpos);
11158 /* Implementation note: It this still necessary? */
11159 if (it.current_x != this_line_start_x)
11160 goto cancel;
11162 TRACE ((stderr, "trying display optimization 1\n"));
11163 w->cursor.vpos = -1;
11164 overlay_arrow_seen = 0;
11165 it.vpos = this_line_vpos;
11166 it.current_y = this_line_y;
11167 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11168 display_line (&it);
11170 /* If line contains point, is not continued,
11171 and ends at same distance from eob as before, we win */
11172 if (w->cursor.vpos >= 0
11173 /* Line is not continued, otherwise this_line_start_pos
11174 would have been set to 0 in display_line. */
11175 && CHARPOS (this_line_start_pos)
11176 /* Line ends as before. */
11177 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11178 /* Line has same height as before. Otherwise other lines
11179 would have to be shifted up or down. */
11180 && this_line_pixel_height == line_height_before)
11182 /* If this is not the window's last line, we must adjust
11183 the charstarts of the lines below. */
11184 if (it.current_y < it.last_visible_y)
11186 struct glyph_row *row
11187 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11188 int delta, delta_bytes;
11190 if (Z - CHARPOS (tlendpos) == ZV)
11192 /* This line ends at end of (accessible part of)
11193 buffer. There is no newline to count. */
11194 delta = (Z
11195 - CHARPOS (tlendpos)
11196 - MATRIX_ROW_START_CHARPOS (row));
11197 delta_bytes = (Z_BYTE
11198 - BYTEPOS (tlendpos)
11199 - MATRIX_ROW_START_BYTEPOS (row));
11201 else
11203 /* This line ends in a newline. Must take
11204 account of the newline and the rest of the
11205 text that follows. */
11206 delta = (Z
11207 - CHARPOS (tlendpos)
11208 - MATRIX_ROW_START_CHARPOS (row));
11209 delta_bytes = (Z_BYTE
11210 - BYTEPOS (tlendpos)
11211 - MATRIX_ROW_START_BYTEPOS (row));
11214 increment_matrix_positions (w->current_matrix,
11215 this_line_vpos + 1,
11216 w->current_matrix->nrows,
11217 delta, delta_bytes);
11220 /* If this row displays text now but previously didn't,
11221 or vice versa, w->window_end_vpos may have to be
11222 adjusted. */
11223 if ((it.glyph_row - 1)->displays_text_p)
11225 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11226 XSETINT (w->window_end_vpos, this_line_vpos);
11228 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11229 && this_line_vpos > 0)
11230 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11231 w->window_end_valid = Qnil;
11233 /* Update hint: No need to try to scroll in update_window. */
11234 w->desired_matrix->no_scrolling_p = 1;
11236 #if GLYPH_DEBUG
11237 *w->desired_matrix->method = 0;
11238 debug_method_add (w, "optimization 1");
11239 #endif
11240 #ifdef HAVE_WINDOW_SYSTEM
11241 update_window_fringes (w, 0);
11242 #endif
11243 goto update;
11245 else
11246 goto cancel;
11248 else if (/* Cursor position hasn't changed. */
11249 PT == XFASTINT (w->last_point)
11250 /* Make sure the cursor was last displayed
11251 in this window. Otherwise we have to reposition it. */
11252 && 0 <= w->cursor.vpos
11253 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11255 if (!must_finish)
11257 do_pending_window_change (1);
11259 /* We used to always goto end_of_redisplay here, but this
11260 isn't enough if we have a blinking cursor. */
11261 if (w->cursor_off_p == w->last_cursor_off_p)
11262 goto end_of_redisplay;
11264 goto update;
11266 /* If highlighting the region, or if the cursor is in the echo area,
11267 then we can't just move the cursor. */
11268 else if (! (!NILP (Vtransient_mark_mode)
11269 && !NILP (current_buffer->mark_active))
11270 && (EQ (selected_window, current_buffer->last_selected_window)
11271 || highlight_nonselected_windows)
11272 && NILP (w->region_showing)
11273 && NILP (Vshow_trailing_whitespace)
11274 && !cursor_in_echo_area)
11276 struct it it;
11277 struct glyph_row *row;
11279 /* Skip from tlbufpos to PT and see where it is. Note that
11280 PT may be in invisible text. If so, we will end at the
11281 next visible position. */
11282 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11283 NULL, DEFAULT_FACE_ID);
11284 it.current_x = this_line_start_x;
11285 it.current_y = this_line_y;
11286 it.vpos = this_line_vpos;
11288 /* The call to move_it_to stops in front of PT, but
11289 moves over before-strings. */
11290 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11292 if (it.vpos == this_line_vpos
11293 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11294 row->enabled_p))
11296 xassert (this_line_vpos == it.vpos);
11297 xassert (this_line_y == it.current_y);
11298 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11299 #if GLYPH_DEBUG
11300 *w->desired_matrix->method = 0;
11301 debug_method_add (w, "optimization 3");
11302 #endif
11303 goto update;
11305 else
11306 goto cancel;
11309 cancel:
11310 /* Text changed drastically or point moved off of line. */
11311 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11314 CHARPOS (this_line_start_pos) = 0;
11315 consider_all_windows_p |= buffer_shared > 1;
11316 ++clear_face_cache_count;
11317 #ifdef HAVE_WINDOW_SYSTEM
11318 ++clear_image_cache_count;
11319 #endif
11321 /* Build desired matrices, and update the display. If
11322 consider_all_windows_p is non-zero, do it for all windows on all
11323 frames. Otherwise do it for selected_window, only. */
11325 if (consider_all_windows_p)
11327 Lisp_Object tail, frame;
11329 FOR_EACH_FRAME (tail, frame)
11330 XFRAME (frame)->updated_p = 0;
11332 /* Recompute # windows showing selected buffer. This will be
11333 incremented each time such a window is displayed. */
11334 buffer_shared = 0;
11336 FOR_EACH_FRAME (tail, frame)
11338 struct frame *f = XFRAME (frame);
11340 if (FRAME_WINDOW_P (f) || f == sf)
11342 if (! EQ (frame, selected_frame))
11343 /* Select the frame, for the sake of frame-local
11344 variables. */
11345 select_frame_for_redisplay (frame);
11347 /* Mark all the scroll bars to be removed; we'll redeem
11348 the ones we want when we redisplay their windows. */
11349 if (condemn_scroll_bars_hook)
11350 condemn_scroll_bars_hook (f);
11352 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11353 redisplay_windows (FRAME_ROOT_WINDOW (f));
11355 /* Any scroll bars which redisplay_windows should have
11356 nuked should now go away. */
11357 if (judge_scroll_bars_hook)
11358 judge_scroll_bars_hook (f);
11360 /* If fonts changed, display again. */
11361 /* ??? rms: I suspect it is a mistake to jump all the way
11362 back to retry here. It should just retry this frame. */
11363 if (fonts_changed_p)
11364 goto retry;
11366 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11368 /* See if we have to hscroll. */
11369 if (!f->already_hscrolled_p)
11371 f->already_hscrolled_p = 1;
11372 if (hscroll_windows (f->root_window))
11373 goto retry;
11376 /* Prevent various kinds of signals during display
11377 update. stdio is not robust about handling
11378 signals, which can cause an apparent I/O
11379 error. */
11380 if (interrupt_input)
11381 unrequest_sigio ();
11382 STOP_POLLING;
11384 /* Update the display. */
11385 set_window_update_flags (XWINDOW (f->root_window), 1);
11386 pause |= update_frame (f, 0, 0);
11387 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11388 if (pause)
11389 break;
11390 #endif
11392 f->updated_p = 1;
11397 if (!pause)
11399 /* Do the mark_window_display_accurate after all windows have
11400 been redisplayed because this call resets flags in buffers
11401 which are needed for proper redisplay. */
11402 FOR_EACH_FRAME (tail, frame)
11404 struct frame *f = XFRAME (frame);
11405 if (f->updated_p)
11407 mark_window_display_accurate (f->root_window, 1);
11408 if (frame_up_to_date_hook)
11409 frame_up_to_date_hook (f);
11414 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11416 Lisp_Object mini_window;
11417 struct frame *mini_frame;
11419 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11420 /* Use list_of_error, not Qerror, so that
11421 we catch only errors and don't run the debugger. */
11422 internal_condition_case_1 (redisplay_window_1, selected_window,
11423 list_of_error,
11424 redisplay_window_error);
11426 /* Compare desired and current matrices, perform output. */
11428 update:
11429 /* If fonts changed, display again. */
11430 if (fonts_changed_p)
11431 goto retry;
11433 /* Prevent various kinds of signals during display update.
11434 stdio is not robust about handling signals,
11435 which can cause an apparent I/O error. */
11436 if (interrupt_input)
11437 unrequest_sigio ();
11438 STOP_POLLING;
11440 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11442 if (hscroll_windows (selected_window))
11443 goto retry;
11445 XWINDOW (selected_window)->must_be_updated_p = 1;
11446 pause = update_frame (sf, 0, 0);
11449 /* We may have called echo_area_display at the top of this
11450 function. If the echo area is on another frame, that may
11451 have put text on a frame other than the selected one, so the
11452 above call to update_frame would not have caught it. Catch
11453 it here. */
11454 mini_window = FRAME_MINIBUF_WINDOW (sf);
11455 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11457 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11459 XWINDOW (mini_window)->must_be_updated_p = 1;
11460 pause |= update_frame (mini_frame, 0, 0);
11461 if (!pause && hscroll_windows (mini_window))
11462 goto retry;
11466 /* If display was paused because of pending input, make sure we do a
11467 thorough update the next time. */
11468 if (pause)
11470 /* Prevent the optimization at the beginning of
11471 redisplay_internal that tries a single-line update of the
11472 line containing the cursor in the selected window. */
11473 CHARPOS (this_line_start_pos) = 0;
11475 /* Let the overlay arrow be updated the next time. */
11476 update_overlay_arrows (0);
11478 /* If we pause after scrolling, some rows in the current
11479 matrices of some windows are not valid. */
11480 if (!WINDOW_FULL_WIDTH_P (w)
11481 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11482 update_mode_lines = 1;
11484 else
11486 if (!consider_all_windows_p)
11488 /* This has already been done above if
11489 consider_all_windows_p is set. */
11490 mark_window_display_accurate_1 (w, 1);
11492 /* Say overlay arrows are up to date. */
11493 update_overlay_arrows (1);
11495 if (frame_up_to_date_hook != 0)
11496 frame_up_to_date_hook (sf);
11499 update_mode_lines = 0;
11500 windows_or_buffers_changed = 0;
11501 cursor_type_changed = 0;
11504 /* Start SIGIO interrupts coming again. Having them off during the
11505 code above makes it less likely one will discard output, but not
11506 impossible, since there might be stuff in the system buffer here.
11507 But it is much hairier to try to do anything about that. */
11508 if (interrupt_input)
11509 request_sigio ();
11510 RESUME_POLLING;
11512 /* If a frame has become visible which was not before, redisplay
11513 again, so that we display it. Expose events for such a frame
11514 (which it gets when becoming visible) don't call the parts of
11515 redisplay constructing glyphs, so simply exposing a frame won't
11516 display anything in this case. So, we have to display these
11517 frames here explicitly. */
11518 if (!pause)
11520 Lisp_Object tail, frame;
11521 int new_count = 0;
11523 FOR_EACH_FRAME (tail, frame)
11525 int this_is_visible = 0;
11527 if (XFRAME (frame)->visible)
11528 this_is_visible = 1;
11529 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11530 if (XFRAME (frame)->visible)
11531 this_is_visible = 1;
11533 if (this_is_visible)
11534 new_count++;
11537 if (new_count != number_of_visible_frames)
11538 windows_or_buffers_changed++;
11541 /* Change frame size now if a change is pending. */
11542 do_pending_window_change (1);
11544 /* If we just did a pending size change, or have additional
11545 visible frames, redisplay again. */
11546 if (windows_or_buffers_changed && !pause)
11547 goto retry;
11549 /* Clear the face cache eventually. */
11550 if (consider_all_windows_p)
11552 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11554 clear_face_cache (0);
11555 clear_face_cache_count = 0;
11557 #ifdef HAVE_WINDOW_SYSTEM
11558 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11560 Lisp_Object tail, frame;
11561 FOR_EACH_FRAME (tail, frame)
11563 struct frame *f = XFRAME (frame);
11564 if (FRAME_WINDOW_P (f))
11565 clear_image_cache (f, 0);
11567 clear_image_cache_count = 0;
11569 #endif /* HAVE_WINDOW_SYSTEM */
11572 end_of_redisplay:
11573 unbind_to (count, Qnil);
11574 RESUME_POLLING;
11578 /* Redisplay, but leave alone any recent echo area message unless
11579 another message has been requested in its place.
11581 This is useful in situations where you need to redisplay but no
11582 user action has occurred, making it inappropriate for the message
11583 area to be cleared. See tracking_off and
11584 wait_reading_process_output for examples of these situations.
11586 FROM_WHERE is an integer saying from where this function was
11587 called. This is useful for debugging. */
11589 void
11590 redisplay_preserve_echo_area (from_where)
11591 int from_where;
11593 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11595 if (!NILP (echo_area_buffer[1]))
11597 /* We have a previously displayed message, but no current
11598 message. Redisplay the previous message. */
11599 display_last_displayed_message_p = 1;
11600 redisplay_internal (1);
11601 display_last_displayed_message_p = 0;
11603 else
11604 redisplay_internal (1);
11606 if (rif != NULL && rif->flush_display_optional)
11607 rif->flush_display_optional (NULL);
11611 /* Function registered with record_unwind_protect in
11612 redisplay_internal. Reset redisplaying_p to the value it had
11613 before redisplay_internal was called, and clear
11614 prevent_freeing_realized_faces_p. It also selects the previously
11615 selected frame. */
11617 static Lisp_Object
11618 unwind_redisplay (val)
11619 Lisp_Object val;
11621 Lisp_Object old_redisplaying_p, old_frame;
11623 old_redisplaying_p = XCAR (val);
11624 redisplaying_p = XFASTINT (old_redisplaying_p);
11625 old_frame = XCDR (val);
11626 if (! EQ (old_frame, selected_frame))
11627 select_frame_for_redisplay (old_frame);
11628 return Qnil;
11632 /* Mark the display of window W as accurate or inaccurate. If
11633 ACCURATE_P is non-zero mark display of W as accurate. If
11634 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11635 redisplay_internal is called. */
11637 static void
11638 mark_window_display_accurate_1 (w, accurate_p)
11639 struct window *w;
11640 int accurate_p;
11642 if (BUFFERP (w->buffer))
11644 struct buffer *b = XBUFFER (w->buffer);
11646 w->last_modified
11647 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11648 w->last_overlay_modified
11649 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11650 w->last_had_star
11651 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11653 if (accurate_p)
11655 b->clip_changed = 0;
11656 b->prevent_redisplay_optimizations_p = 0;
11658 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11659 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11660 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11661 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11663 w->current_matrix->buffer = b;
11664 w->current_matrix->begv = BUF_BEGV (b);
11665 w->current_matrix->zv = BUF_ZV (b);
11667 w->last_cursor = w->cursor;
11668 w->last_cursor_off_p = w->cursor_off_p;
11670 if (w == XWINDOW (selected_window))
11671 w->last_point = make_number (BUF_PT (b));
11672 else
11673 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11677 if (accurate_p)
11679 w->window_end_valid = w->buffer;
11680 #if 0 /* This is incorrect with variable-height lines. */
11681 xassert (XINT (w->window_end_vpos)
11682 < (WINDOW_TOTAL_LINES (w)
11683 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11684 #endif
11685 w->update_mode_line = Qnil;
11690 /* Mark the display of windows in the window tree rooted at WINDOW as
11691 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11692 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11693 be redisplayed the next time redisplay_internal is called. */
11695 void
11696 mark_window_display_accurate (window, accurate_p)
11697 Lisp_Object window;
11698 int accurate_p;
11700 struct window *w;
11702 for (; !NILP (window); window = w->next)
11704 w = XWINDOW (window);
11705 mark_window_display_accurate_1 (w, accurate_p);
11707 if (!NILP (w->vchild))
11708 mark_window_display_accurate (w->vchild, accurate_p);
11709 if (!NILP (w->hchild))
11710 mark_window_display_accurate (w->hchild, accurate_p);
11713 if (accurate_p)
11715 update_overlay_arrows (1);
11717 else
11719 /* Force a thorough redisplay the next time by setting
11720 last_arrow_position and last_arrow_string to t, which is
11721 unequal to any useful value of Voverlay_arrow_... */
11722 update_overlay_arrows (-1);
11727 /* Return value in display table DP (Lisp_Char_Table *) for character
11728 C. Since a display table doesn't have any parent, we don't have to
11729 follow parent. Do not call this function directly but use the
11730 macro DISP_CHAR_VECTOR. */
11732 Lisp_Object
11733 disp_char_vector (dp, c)
11734 struct Lisp_Char_Table *dp;
11735 int c;
11737 int code[4], i;
11738 Lisp_Object val;
11740 if (SINGLE_BYTE_CHAR_P (c))
11741 return (dp->contents[c]);
11743 SPLIT_CHAR (c, code[0], code[1], code[2]);
11744 if (code[1] < 32)
11745 code[1] = -1;
11746 else if (code[2] < 32)
11747 code[2] = -1;
11749 /* Here, the possible range of code[0] (== charset ID) is
11750 128..max_charset. Since the top level char table contains data
11751 for multibyte characters after 256th element, we must increment
11752 code[0] by 128 to get a correct index. */
11753 code[0] += 128;
11754 code[3] = -1; /* anchor */
11756 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11758 val = dp->contents[code[i]];
11759 if (!SUB_CHAR_TABLE_P (val))
11760 return (NILP (val) ? dp->defalt : val);
11763 /* Here, val is a sub char table. We return the default value of
11764 it. */
11765 return (dp->defalt);
11770 /***********************************************************************
11771 Window Redisplay
11772 ***********************************************************************/
11774 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11776 static void
11777 redisplay_windows (window)
11778 Lisp_Object window;
11780 while (!NILP (window))
11782 struct window *w = XWINDOW (window);
11784 if (!NILP (w->hchild))
11785 redisplay_windows (w->hchild);
11786 else if (!NILP (w->vchild))
11787 redisplay_windows (w->vchild);
11788 else
11790 displayed_buffer = XBUFFER (w->buffer);
11791 /* Use list_of_error, not Qerror, so that
11792 we catch only errors and don't run the debugger. */
11793 internal_condition_case_1 (redisplay_window_0, window,
11794 list_of_error,
11795 redisplay_window_error);
11798 window = w->next;
11802 static Lisp_Object
11803 redisplay_window_error ()
11805 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11806 return Qnil;
11809 static Lisp_Object
11810 redisplay_window_0 (window)
11811 Lisp_Object window;
11813 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11814 redisplay_window (window, 0);
11815 return Qnil;
11818 static Lisp_Object
11819 redisplay_window_1 (window)
11820 Lisp_Object window;
11822 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11823 redisplay_window (window, 1);
11824 return Qnil;
11828 /* Increment GLYPH until it reaches END or CONDITION fails while
11829 adding (GLYPH)->pixel_width to X. */
11831 #define SKIP_GLYPHS(glyph, end, x, condition) \
11832 do \
11834 (x) += (glyph)->pixel_width; \
11835 ++(glyph); \
11837 while ((glyph) < (end) && (condition))
11840 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11841 DELTA is the number of bytes by which positions recorded in ROW
11842 differ from current buffer positions.
11844 Return 0 if cursor is not on this row. 1 otherwise. */
11847 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11848 struct window *w;
11849 struct glyph_row *row;
11850 struct glyph_matrix *matrix;
11851 int delta, delta_bytes, dy, dvpos;
11853 struct glyph *glyph = row->glyphs[TEXT_AREA];
11854 struct glyph *end = glyph + row->used[TEXT_AREA];
11855 struct glyph *cursor = NULL;
11856 /* The first glyph that starts a sequence of glyphs from string. */
11857 struct glyph *string_start;
11858 /* The X coordinate of string_start. */
11859 int string_start_x;
11860 /* The last known character position. */
11861 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11862 /* The last known character position before string_start. */
11863 int string_before_pos;
11864 int x = row->x;
11865 int cursor_x = x;
11866 int cursor_from_overlay_pos = 0;
11867 int pt_old = PT - delta;
11869 /* Skip over glyphs not having an object at the start of the row.
11870 These are special glyphs like truncation marks on terminal
11871 frames. */
11872 if (row->displays_text_p)
11873 while (glyph < end
11874 && INTEGERP (glyph->object)
11875 && glyph->charpos < 0)
11877 x += glyph->pixel_width;
11878 ++glyph;
11881 string_start = NULL;
11882 while (glyph < end
11883 && !INTEGERP (glyph->object)
11884 && (!BUFFERP (glyph->object)
11885 || (last_pos = glyph->charpos) < pt_old))
11887 if (! STRINGP (glyph->object))
11889 string_start = NULL;
11890 x += glyph->pixel_width;
11891 ++glyph;
11892 if (cursor_from_overlay_pos
11893 && last_pos >= cursor_from_overlay_pos)
11895 cursor_from_overlay_pos = 0;
11896 cursor = 0;
11899 else
11901 if (string_start == NULL)
11903 string_before_pos = last_pos;
11904 string_start = glyph;
11905 string_start_x = x;
11907 /* Skip all glyphs from string. */
11910 Lisp_Object cprop;
11911 int pos;
11912 if ((cursor == NULL || glyph > cursor)
11913 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11914 Qcursor, (glyph)->object),
11915 !NILP (cprop))
11916 && (pos = string_buffer_position (w, glyph->object,
11917 string_before_pos),
11918 (pos == 0 /* From overlay */
11919 || pos == pt_old)))
11921 /* Estimate overlay buffer position from the buffer
11922 positions of the glyphs before and after the overlay.
11923 Add 1 to last_pos so that if point corresponds to the
11924 glyph right after the overlay, we still use a 'cursor'
11925 property found in that overlay. */
11926 cursor_from_overlay_pos = (pos ? 0 : last_pos
11927 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11928 cursor = glyph;
11929 cursor_x = x;
11931 x += glyph->pixel_width;
11932 ++glyph;
11934 while (glyph < end && EQ (glyph->object, string_start->object));
11938 if (cursor != NULL)
11940 glyph = cursor;
11941 x = cursor_x;
11943 else if (row->ends_in_ellipsis_p && glyph == end)
11945 /* Scan back over the ellipsis glyphs, decrementing positions. */
11946 while (glyph > row->glyphs[TEXT_AREA]
11947 && (glyph - 1)->charpos == last_pos)
11948 glyph--, x -= glyph->pixel_width;
11949 /* That loop always goes one position too far,
11950 including the glyph before the ellipsis.
11951 So scan forward over that one. */
11952 x += glyph->pixel_width;
11953 glyph++;
11955 else if (string_start
11956 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11958 /* We may have skipped over point because the previous glyphs
11959 are from string. As there's no easy way to know the
11960 character position of the current glyph, find the correct
11961 glyph on point by scanning from string_start again. */
11962 Lisp_Object limit;
11963 Lisp_Object string;
11964 struct glyph *stop = glyph;
11965 int pos;
11967 limit = make_number (pt_old + 1);
11968 glyph = string_start;
11969 x = string_start_x;
11970 string = glyph->object;
11971 pos = string_buffer_position (w, string, string_before_pos);
11972 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11973 because we always put cursor after overlay strings. */
11974 while (pos == 0 && glyph < stop)
11976 string = glyph->object;
11977 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11978 if (glyph < stop)
11979 pos = string_buffer_position (w, glyph->object, string_before_pos);
11982 while (glyph < stop)
11984 pos = XINT (Fnext_single_char_property_change
11985 (make_number (pos), Qdisplay, Qnil, limit));
11986 if (pos > pt_old)
11987 break;
11988 /* Skip glyphs from the same string. */
11989 string = glyph->object;
11990 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11991 /* Skip glyphs from an overlay. */
11992 while (glyph < stop
11993 && ! string_buffer_position (w, glyph->object, pos))
11995 string = glyph->object;
11996 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12000 /* If we reached the end of the line, and end was from a string,
12001 cursor is not on this line. */
12002 if (glyph == end && row->continued_p)
12003 return 0;
12006 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12007 w->cursor.x = x;
12008 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12009 w->cursor.y = row->y + dy;
12011 if (w == XWINDOW (selected_window))
12013 if (!row->continued_p
12014 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12015 && row->x == 0)
12017 this_line_buffer = XBUFFER (w->buffer);
12019 CHARPOS (this_line_start_pos)
12020 = MATRIX_ROW_START_CHARPOS (row) + delta;
12021 BYTEPOS (this_line_start_pos)
12022 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12024 CHARPOS (this_line_end_pos)
12025 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12026 BYTEPOS (this_line_end_pos)
12027 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12029 this_line_y = w->cursor.y;
12030 this_line_pixel_height = row->height;
12031 this_line_vpos = w->cursor.vpos;
12032 this_line_start_x = row->x;
12034 else
12035 CHARPOS (this_line_start_pos) = 0;
12038 return 1;
12042 /* Run window scroll functions, if any, for WINDOW with new window
12043 start STARTP. Sets the window start of WINDOW to that position.
12045 We assume that the window's buffer is really current. */
12047 static INLINE struct text_pos
12048 run_window_scroll_functions (window, startp)
12049 Lisp_Object window;
12050 struct text_pos startp;
12052 struct window *w = XWINDOW (window);
12053 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12055 if (current_buffer != XBUFFER (w->buffer))
12056 abort ();
12058 if (!NILP (Vwindow_scroll_functions))
12060 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12061 make_number (CHARPOS (startp)));
12062 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12063 /* In case the hook functions switch buffers. */
12064 if (current_buffer != XBUFFER (w->buffer))
12065 set_buffer_internal_1 (XBUFFER (w->buffer));
12068 return startp;
12072 /* Make sure the line containing the cursor is fully visible.
12073 A value of 1 means there is nothing to be done.
12074 (Either the line is fully visible, or it cannot be made so,
12075 or we cannot tell.)
12077 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12078 is higher than window.
12080 A value of 0 means the caller should do scrolling
12081 as if point had gone off the screen. */
12083 static int
12084 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12085 struct window *w;
12086 int force_p;
12087 int current_matrix_p;
12089 struct glyph_matrix *matrix;
12090 struct glyph_row *row;
12091 int window_height;
12093 if (!make_cursor_line_fully_visible_p)
12094 return 1;
12096 /* It's not always possible to find the cursor, e.g, when a window
12097 is full of overlay strings. Don't do anything in that case. */
12098 if (w->cursor.vpos < 0)
12099 return 1;
12101 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12102 row = MATRIX_ROW (matrix, w->cursor.vpos);
12104 /* If the cursor row is not partially visible, there's nothing to do. */
12105 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12106 return 1;
12108 /* If the row the cursor is in is taller than the window's height,
12109 it's not clear what to do, so do nothing. */
12110 window_height = window_box_height (w);
12111 if (row->height >= window_height)
12113 if (!force_p || MINI_WINDOW_P (w)
12114 || w->vscroll || w->cursor.vpos == 0)
12115 return 1;
12117 return 0;
12119 #if 0
12120 /* This code used to try to scroll the window just enough to make
12121 the line visible. It returned 0 to say that the caller should
12122 allocate larger glyph matrices. */
12124 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12126 int dy = row->height - row->visible_height;
12127 w->vscroll = 0;
12128 w->cursor.y += dy;
12129 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12131 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12133 int dy = - (row->height - row->visible_height);
12134 w->vscroll = dy;
12135 w->cursor.y += dy;
12136 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12139 /* When we change the cursor y-position of the selected window,
12140 change this_line_y as well so that the display optimization for
12141 the cursor line of the selected window in redisplay_internal uses
12142 the correct y-position. */
12143 if (w == XWINDOW (selected_window))
12144 this_line_y = w->cursor.y;
12146 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12147 redisplay with larger matrices. */
12148 if (matrix->nrows < required_matrix_height (w))
12150 fonts_changed_p = 1;
12151 return 0;
12154 return 1;
12155 #endif /* 0 */
12159 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12160 non-zero means only WINDOW is redisplayed in redisplay_internal.
12161 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12162 in redisplay_window to bring a partially visible line into view in
12163 the case that only the cursor has moved.
12165 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12166 last screen line's vertical height extends past the end of the screen.
12168 Value is
12170 1 if scrolling succeeded
12172 0 if scrolling didn't find point.
12174 -1 if new fonts have been loaded so that we must interrupt
12175 redisplay, adjust glyph matrices, and try again. */
12177 enum
12179 SCROLLING_SUCCESS,
12180 SCROLLING_FAILED,
12181 SCROLLING_NEED_LARGER_MATRICES
12184 static int
12185 try_scrolling (window, just_this_one_p, scroll_conservatively,
12186 scroll_step, temp_scroll_step, last_line_misfit)
12187 Lisp_Object window;
12188 int just_this_one_p;
12189 EMACS_INT scroll_conservatively, scroll_step;
12190 int temp_scroll_step;
12191 int last_line_misfit;
12193 struct window *w = XWINDOW (window);
12194 struct frame *f = XFRAME (w->frame);
12195 struct text_pos scroll_margin_pos;
12196 struct text_pos pos;
12197 struct text_pos startp;
12198 struct it it;
12199 Lisp_Object window_end;
12200 int this_scroll_margin;
12201 int dy = 0;
12202 int scroll_max;
12203 int rc;
12204 int amount_to_scroll = 0;
12205 Lisp_Object aggressive;
12206 int height;
12207 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12209 #if GLYPH_DEBUG
12210 debug_method_add (w, "try_scrolling");
12211 #endif
12213 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12215 /* Compute scroll margin height in pixels. We scroll when point is
12216 within this distance from the top or bottom of the window. */
12217 if (scroll_margin > 0)
12219 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12220 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12222 else
12223 this_scroll_margin = 0;
12225 /* Force scroll_conservatively to have a reasonable value so it doesn't
12226 cause an overflow while computing how much to scroll. */
12227 if (scroll_conservatively)
12228 scroll_conservatively = min (scroll_conservatively,
12229 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12231 /* Compute how much we should try to scroll maximally to bring point
12232 into view. */
12233 if (scroll_step || scroll_conservatively || temp_scroll_step)
12234 scroll_max = max (scroll_step,
12235 max (scroll_conservatively, temp_scroll_step));
12236 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12237 || NUMBERP (current_buffer->scroll_up_aggressively))
12238 /* We're trying to scroll because of aggressive scrolling
12239 but no scroll_step is set. Choose an arbitrary one. Maybe
12240 there should be a variable for this. */
12241 scroll_max = 10;
12242 else
12243 scroll_max = 0;
12244 scroll_max *= FRAME_LINE_HEIGHT (f);
12246 /* Decide whether we have to scroll down. Start at the window end
12247 and move this_scroll_margin up to find the position of the scroll
12248 margin. */
12249 window_end = Fwindow_end (window, Qt);
12251 too_near_end:
12253 CHARPOS (scroll_margin_pos) = XINT (window_end);
12254 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12256 if (this_scroll_margin || extra_scroll_margin_lines)
12258 start_display (&it, w, scroll_margin_pos);
12259 if (this_scroll_margin)
12260 move_it_vertically_backward (&it, this_scroll_margin);
12261 if (extra_scroll_margin_lines)
12262 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12263 scroll_margin_pos = it.current.pos;
12266 if (PT >= CHARPOS (scroll_margin_pos))
12268 int y0;
12270 /* Point is in the scroll margin at the bottom of the window, or
12271 below. Compute a new window start that makes point visible. */
12273 /* Compute the distance from the scroll margin to PT.
12274 Give up if the distance is greater than scroll_max. */
12275 start_display (&it, w, scroll_margin_pos);
12276 y0 = it.current_y;
12277 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12278 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12280 /* To make point visible, we have to move the window start
12281 down so that the line the cursor is in is visible, which
12282 means we have to add in the height of the cursor line. */
12283 dy = line_bottom_y (&it) - y0;
12285 if (dy > scroll_max)
12286 return SCROLLING_FAILED;
12288 /* Move the window start down. If scrolling conservatively,
12289 move it just enough down to make point visible. If
12290 scroll_step is set, move it down by scroll_step. */
12291 start_display (&it, w, startp);
12293 if (scroll_conservatively)
12294 /* Set AMOUNT_TO_SCROLL to at least one line,
12295 and at most scroll_conservatively lines. */
12296 amount_to_scroll
12297 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12298 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12299 else if (scroll_step || temp_scroll_step)
12300 amount_to_scroll = scroll_max;
12301 else
12303 aggressive = current_buffer->scroll_up_aggressively;
12304 height = WINDOW_BOX_TEXT_HEIGHT (w);
12305 if (NUMBERP (aggressive))
12307 double float_amount = XFLOATINT (aggressive) * height;
12308 amount_to_scroll = float_amount;
12309 if (amount_to_scroll == 0 && float_amount > 0)
12310 amount_to_scroll = 1;
12314 if (amount_to_scroll <= 0)
12315 return SCROLLING_FAILED;
12317 /* If moving by amount_to_scroll leaves STARTP unchanged,
12318 move it down one screen line. */
12320 move_it_vertically (&it, amount_to_scroll);
12321 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12322 move_it_by_lines (&it, 1, 1);
12323 startp = it.current.pos;
12325 else
12327 /* See if point is inside the scroll margin at the top of the
12328 window. */
12329 scroll_margin_pos = startp;
12330 if (this_scroll_margin)
12332 start_display (&it, w, startp);
12333 move_it_vertically (&it, this_scroll_margin);
12334 scroll_margin_pos = it.current.pos;
12337 if (PT < CHARPOS (scroll_margin_pos))
12339 /* Point is in the scroll margin at the top of the window or
12340 above what is displayed in the window. */
12341 int y0;
12343 /* Compute the vertical distance from PT to the scroll
12344 margin position. Give up if distance is greater than
12345 scroll_max. */
12346 SET_TEXT_POS (pos, PT, PT_BYTE);
12347 start_display (&it, w, pos);
12348 y0 = it.current_y;
12349 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12350 it.last_visible_y, -1,
12351 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12352 dy = it.current_y - y0;
12353 if (dy > scroll_max)
12354 return SCROLLING_FAILED;
12356 /* Compute new window start. */
12357 start_display (&it, w, startp);
12359 if (scroll_conservatively)
12360 amount_to_scroll
12361 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12362 else if (scroll_step || temp_scroll_step)
12363 amount_to_scroll = scroll_max;
12364 else
12366 aggressive = current_buffer->scroll_down_aggressively;
12367 height = WINDOW_BOX_TEXT_HEIGHT (w);
12368 if (NUMBERP (aggressive))
12370 double float_amount = XFLOATINT (aggressive) * height;
12371 amount_to_scroll = float_amount;
12372 if (amount_to_scroll == 0 && float_amount > 0)
12373 amount_to_scroll = 1;
12377 if (amount_to_scroll <= 0)
12378 return SCROLLING_FAILED;
12380 move_it_vertically_backward (&it, amount_to_scroll);
12381 startp = it.current.pos;
12385 /* Run window scroll functions. */
12386 startp = run_window_scroll_functions (window, startp);
12388 /* Display the window. Give up if new fonts are loaded, or if point
12389 doesn't appear. */
12390 if (!try_window (window, startp, 0))
12391 rc = SCROLLING_NEED_LARGER_MATRICES;
12392 else if (w->cursor.vpos < 0)
12394 clear_glyph_matrix (w->desired_matrix);
12395 rc = SCROLLING_FAILED;
12397 else
12399 /* Maybe forget recorded base line for line number display. */
12400 if (!just_this_one_p
12401 || current_buffer->clip_changed
12402 || BEG_UNCHANGED < CHARPOS (startp))
12403 w->base_line_number = Qnil;
12405 /* If cursor ends up on a partially visible line,
12406 treat that as being off the bottom of the screen. */
12407 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12409 clear_glyph_matrix (w->desired_matrix);
12410 ++extra_scroll_margin_lines;
12411 goto too_near_end;
12413 rc = SCROLLING_SUCCESS;
12416 return rc;
12420 /* Compute a suitable window start for window W if display of W starts
12421 on a continuation line. Value is non-zero if a new window start
12422 was computed.
12424 The new window start will be computed, based on W's width, starting
12425 from the start of the continued line. It is the start of the
12426 screen line with the minimum distance from the old start W->start. */
12428 static int
12429 compute_window_start_on_continuation_line (w)
12430 struct window *w;
12432 struct text_pos pos, start_pos;
12433 int window_start_changed_p = 0;
12435 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12437 /* If window start is on a continuation line... Window start may be
12438 < BEGV in case there's invisible text at the start of the
12439 buffer (M-x rmail, for example). */
12440 if (CHARPOS (start_pos) > BEGV
12441 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12443 struct it it;
12444 struct glyph_row *row;
12446 /* Handle the case that the window start is out of range. */
12447 if (CHARPOS (start_pos) < BEGV)
12448 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12449 else if (CHARPOS (start_pos) > ZV)
12450 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12452 /* Find the start of the continued line. This should be fast
12453 because scan_buffer is fast (newline cache). */
12454 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12455 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12456 row, DEFAULT_FACE_ID);
12457 reseat_at_previous_visible_line_start (&it);
12459 /* If the line start is "too far" away from the window start,
12460 say it takes too much time to compute a new window start. */
12461 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12462 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12464 int min_distance, distance;
12466 /* Move forward by display lines to find the new window
12467 start. If window width was enlarged, the new start can
12468 be expected to be > the old start. If window width was
12469 decreased, the new window start will be < the old start.
12470 So, we're looking for the display line start with the
12471 minimum distance from the old window start. */
12472 pos = it.current.pos;
12473 min_distance = INFINITY;
12474 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12475 distance < min_distance)
12477 min_distance = distance;
12478 pos = it.current.pos;
12479 move_it_by_lines (&it, 1, 0);
12482 /* Set the window start there. */
12483 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12484 window_start_changed_p = 1;
12488 return window_start_changed_p;
12492 /* Try cursor movement in case text has not changed in window WINDOW,
12493 with window start STARTP. Value is
12495 CURSOR_MOVEMENT_SUCCESS if successful
12497 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12499 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12500 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12501 we want to scroll as if scroll-step were set to 1. See the code.
12503 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12504 which case we have to abort this redisplay, and adjust matrices
12505 first. */
12507 enum
12509 CURSOR_MOVEMENT_SUCCESS,
12510 CURSOR_MOVEMENT_CANNOT_BE_USED,
12511 CURSOR_MOVEMENT_MUST_SCROLL,
12512 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12515 static int
12516 try_cursor_movement (window, startp, scroll_step)
12517 Lisp_Object window;
12518 struct text_pos startp;
12519 int *scroll_step;
12521 struct window *w = XWINDOW (window);
12522 struct frame *f = XFRAME (w->frame);
12523 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12525 #if GLYPH_DEBUG
12526 if (inhibit_try_cursor_movement)
12527 return rc;
12528 #endif
12530 /* Handle case where text has not changed, only point, and it has
12531 not moved off the frame. */
12532 if (/* Point may be in this window. */
12533 PT >= CHARPOS (startp)
12534 /* Selective display hasn't changed. */
12535 && !current_buffer->clip_changed
12536 /* Function force-mode-line-update is used to force a thorough
12537 redisplay. It sets either windows_or_buffers_changed or
12538 update_mode_lines. So don't take a shortcut here for these
12539 cases. */
12540 && !update_mode_lines
12541 && !windows_or_buffers_changed
12542 && !cursor_type_changed
12543 /* Can't use this case if highlighting a region. When a
12544 region exists, cursor movement has to do more than just
12545 set the cursor. */
12546 && !(!NILP (Vtransient_mark_mode)
12547 && !NILP (current_buffer->mark_active))
12548 && NILP (w->region_showing)
12549 && NILP (Vshow_trailing_whitespace)
12550 /* Right after splitting windows, last_point may be nil. */
12551 && INTEGERP (w->last_point)
12552 /* This code is not used for mini-buffer for the sake of the case
12553 of redisplaying to replace an echo area message; since in
12554 that case the mini-buffer contents per se are usually
12555 unchanged. This code is of no real use in the mini-buffer
12556 since the handling of this_line_start_pos, etc., in redisplay
12557 handles the same cases. */
12558 && !EQ (window, minibuf_window)
12559 /* When splitting windows or for new windows, it happens that
12560 redisplay is called with a nil window_end_vpos or one being
12561 larger than the window. This should really be fixed in
12562 window.c. I don't have this on my list, now, so we do
12563 approximately the same as the old redisplay code. --gerd. */
12564 && INTEGERP (w->window_end_vpos)
12565 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12566 && (FRAME_WINDOW_P (f)
12567 || !overlay_arrow_in_current_buffer_p ()))
12569 int this_scroll_margin, top_scroll_margin;
12570 struct glyph_row *row = NULL;
12572 #if GLYPH_DEBUG
12573 debug_method_add (w, "cursor movement");
12574 #endif
12576 /* Scroll if point within this distance from the top or bottom
12577 of the window. This is a pixel value. */
12578 this_scroll_margin = max (0, scroll_margin);
12579 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12580 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12582 top_scroll_margin = this_scroll_margin;
12583 if (WINDOW_WANTS_HEADER_LINE_P (w))
12584 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12586 /* Start with the row the cursor was displayed during the last
12587 not paused redisplay. Give up if that row is not valid. */
12588 if (w->last_cursor.vpos < 0
12589 || w->last_cursor.vpos >= w->current_matrix->nrows)
12590 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12591 else
12593 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12594 if (row->mode_line_p)
12595 ++row;
12596 if (!row->enabled_p)
12597 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12600 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12602 int scroll_p = 0;
12603 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12605 if (PT > XFASTINT (w->last_point))
12607 /* Point has moved forward. */
12608 while (MATRIX_ROW_END_CHARPOS (row) < PT
12609 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12611 xassert (row->enabled_p);
12612 ++row;
12615 /* The end position of a row equals the start position
12616 of the next row. If PT is there, we would rather
12617 display it in the next line. */
12618 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12619 && MATRIX_ROW_END_CHARPOS (row) == PT
12620 && !cursor_row_p (w, row))
12621 ++row;
12623 /* If within the scroll margin, scroll. Note that
12624 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12625 the next line would be drawn, and that
12626 this_scroll_margin can be zero. */
12627 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12628 || PT > MATRIX_ROW_END_CHARPOS (row)
12629 /* Line is completely visible last line in window
12630 and PT is to be set in the next line. */
12631 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12632 && PT == MATRIX_ROW_END_CHARPOS (row)
12633 && !row->ends_at_zv_p
12634 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12635 scroll_p = 1;
12637 else if (PT < XFASTINT (w->last_point))
12639 /* Cursor has to be moved backward. Note that PT >=
12640 CHARPOS (startp) because of the outer if-statement. */
12641 while (!row->mode_line_p
12642 && (MATRIX_ROW_START_CHARPOS (row) > PT
12643 || (MATRIX_ROW_START_CHARPOS (row) == PT
12644 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12645 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12646 row > w->current_matrix->rows
12647 && (row-1)->ends_in_newline_from_string_p))))
12648 && (row->y > top_scroll_margin
12649 || CHARPOS (startp) == BEGV))
12651 xassert (row->enabled_p);
12652 --row;
12655 /* Consider the following case: Window starts at BEGV,
12656 there is invisible, intangible text at BEGV, so that
12657 display starts at some point START > BEGV. It can
12658 happen that we are called with PT somewhere between
12659 BEGV and START. Try to handle that case. */
12660 if (row < w->current_matrix->rows
12661 || row->mode_line_p)
12663 row = w->current_matrix->rows;
12664 if (row->mode_line_p)
12665 ++row;
12668 /* Due to newlines in overlay strings, we may have to
12669 skip forward over overlay strings. */
12670 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12671 && MATRIX_ROW_END_CHARPOS (row) == PT
12672 && !cursor_row_p (w, row))
12673 ++row;
12675 /* If within the scroll margin, scroll. */
12676 if (row->y < top_scroll_margin
12677 && CHARPOS (startp) != BEGV)
12678 scroll_p = 1;
12680 else
12682 /* Cursor did not move. So don't scroll even if cursor line
12683 is partially visible, as it was so before. */
12684 rc = CURSOR_MOVEMENT_SUCCESS;
12687 if (PT < MATRIX_ROW_START_CHARPOS (row)
12688 || PT > MATRIX_ROW_END_CHARPOS (row))
12690 /* if PT is not in the glyph row, give up. */
12691 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12693 else if (rc != CURSOR_MOVEMENT_SUCCESS
12694 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12695 && make_cursor_line_fully_visible_p)
12697 if (PT == MATRIX_ROW_END_CHARPOS (row)
12698 && !row->ends_at_zv_p
12699 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12700 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12701 else if (row->height > window_box_height (w))
12703 /* If we end up in a partially visible line, let's
12704 make it fully visible, except when it's taller
12705 than the window, in which case we can't do much
12706 about it. */
12707 *scroll_step = 1;
12708 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12710 else
12712 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12713 if (!cursor_row_fully_visible_p (w, 0, 1))
12714 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12715 else
12716 rc = CURSOR_MOVEMENT_SUCCESS;
12719 else if (scroll_p)
12720 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12721 else
12725 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12727 rc = CURSOR_MOVEMENT_SUCCESS;
12728 break;
12730 ++row;
12732 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12733 && MATRIX_ROW_START_CHARPOS (row) == PT
12734 && cursor_row_p (w, row));
12739 return rc;
12742 void
12743 set_vertical_scroll_bar (w)
12744 struct window *w;
12746 int start, end, whole;
12748 /* Calculate the start and end positions for the current window.
12749 At some point, it would be nice to choose between scrollbars
12750 which reflect the whole buffer size, with special markers
12751 indicating narrowing, and scrollbars which reflect only the
12752 visible region.
12754 Note that mini-buffers sometimes aren't displaying any text. */
12755 if (!MINI_WINDOW_P (w)
12756 || (w == XWINDOW (minibuf_window)
12757 && NILP (echo_area_buffer[0])))
12759 struct buffer *buf = XBUFFER (w->buffer);
12760 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12761 start = marker_position (w->start) - BUF_BEGV (buf);
12762 /* I don't think this is guaranteed to be right. For the
12763 moment, we'll pretend it is. */
12764 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12766 if (end < start)
12767 end = start;
12768 if (whole < (end - start))
12769 whole = end - start;
12771 else
12772 start = end = whole = 0;
12774 /* Indicate what this scroll bar ought to be displaying now. */
12775 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12779 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12780 selected_window is redisplayed.
12782 We can return without actually redisplaying the window if
12783 fonts_changed_p is nonzero. In that case, redisplay_internal will
12784 retry. */
12786 static void
12787 redisplay_window (window, just_this_one_p)
12788 Lisp_Object window;
12789 int just_this_one_p;
12791 struct window *w = XWINDOW (window);
12792 struct frame *f = XFRAME (w->frame);
12793 struct buffer *buffer = XBUFFER (w->buffer);
12794 struct buffer *old = current_buffer;
12795 struct text_pos lpoint, opoint, startp;
12796 int update_mode_line;
12797 int tem;
12798 struct it it;
12799 /* Record it now because it's overwritten. */
12800 int current_matrix_up_to_date_p = 0;
12801 int used_current_matrix_p = 0;
12802 /* This is less strict than current_matrix_up_to_date_p.
12803 It indictes that the buffer contents and narrowing are unchanged. */
12804 int buffer_unchanged_p = 0;
12805 int temp_scroll_step = 0;
12806 int count = SPECPDL_INDEX ();
12807 int rc;
12808 int centering_position = -1;
12809 int last_line_misfit = 0;
12810 int save_beg_unchanged, save_end_unchanged;
12812 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12813 opoint = lpoint;
12815 /* W must be a leaf window here. */
12816 xassert (!NILP (w->buffer));
12817 #if GLYPH_DEBUG
12818 *w->desired_matrix->method = 0;
12819 #endif
12821 specbind (Qinhibit_point_motion_hooks, Qt);
12823 reconsider_clip_changes (w, buffer);
12825 /* Has the mode line to be updated? */
12826 update_mode_line = (!NILP (w->update_mode_line)
12827 || update_mode_lines
12828 || buffer->clip_changed
12829 || buffer->prevent_redisplay_optimizations_p);
12831 if (MINI_WINDOW_P (w))
12833 if (w == XWINDOW (echo_area_window)
12834 && !NILP (echo_area_buffer[0]))
12836 if (update_mode_line)
12837 /* We may have to update a tty frame's menu bar or a
12838 tool-bar. Example `M-x C-h C-h C-g'. */
12839 goto finish_menu_bars;
12840 else
12841 /* We've already displayed the echo area glyphs in this window. */
12842 goto finish_scroll_bars;
12844 else if ((w != XWINDOW (minibuf_window)
12845 || minibuf_level == 0)
12846 /* When buffer is nonempty, redisplay window normally. */
12847 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12848 /* Quail displays non-mini buffers in minibuffer window.
12849 In that case, redisplay the window normally. */
12850 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12852 /* W is a mini-buffer window, but it's not active, so clear
12853 it. */
12854 int yb = window_text_bottom_y (w);
12855 struct glyph_row *row;
12856 int y;
12858 for (y = 0, row = w->desired_matrix->rows;
12859 y < yb;
12860 y += row->height, ++row)
12861 blank_row (w, row, y);
12862 goto finish_scroll_bars;
12865 clear_glyph_matrix (w->desired_matrix);
12868 /* Otherwise set up data on this window; select its buffer and point
12869 value. */
12870 /* Really select the buffer, for the sake of buffer-local
12871 variables. */
12872 set_buffer_internal_1 (XBUFFER (w->buffer));
12873 SET_TEXT_POS (opoint, PT, PT_BYTE);
12875 save_beg_unchanged = BEG_UNCHANGED;
12876 save_end_unchanged = END_UNCHANGED;
12878 current_matrix_up_to_date_p
12879 = (!NILP (w->window_end_valid)
12880 && !current_buffer->clip_changed
12881 && !current_buffer->prevent_redisplay_optimizations_p
12882 && XFASTINT (w->last_modified) >= MODIFF
12883 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12885 buffer_unchanged_p
12886 = (!NILP (w->window_end_valid)
12887 && !current_buffer->clip_changed
12888 && XFASTINT (w->last_modified) >= MODIFF
12889 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12891 /* When windows_or_buffers_changed is non-zero, we can't rely on
12892 the window end being valid, so set it to nil there. */
12893 if (windows_or_buffers_changed)
12895 /* If window starts on a continuation line, maybe adjust the
12896 window start in case the window's width changed. */
12897 if (XMARKER (w->start)->buffer == current_buffer)
12898 compute_window_start_on_continuation_line (w);
12900 w->window_end_valid = Qnil;
12903 /* Some sanity checks. */
12904 CHECK_WINDOW_END (w);
12905 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12906 abort ();
12907 if (BYTEPOS (opoint) < CHARPOS (opoint))
12908 abort ();
12910 /* If %c is in mode line, update it if needed. */
12911 if (!NILP (w->column_number_displayed)
12912 /* This alternative quickly identifies a common case
12913 where no change is needed. */
12914 && !(PT == XFASTINT (w->last_point)
12915 && XFASTINT (w->last_modified) >= MODIFF
12916 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12917 && (XFASTINT (w->column_number_displayed)
12918 != (int) current_column ())) /* iftc */
12919 update_mode_line = 1;
12921 /* Count number of windows showing the selected buffer. An indirect
12922 buffer counts as its base buffer. */
12923 if (!just_this_one_p)
12925 struct buffer *current_base, *window_base;
12926 current_base = current_buffer;
12927 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12928 if (current_base->base_buffer)
12929 current_base = current_base->base_buffer;
12930 if (window_base->base_buffer)
12931 window_base = window_base->base_buffer;
12932 if (current_base == window_base)
12933 buffer_shared++;
12936 /* Point refers normally to the selected window. For any other
12937 window, set up appropriate value. */
12938 if (!EQ (window, selected_window))
12940 int new_pt = XMARKER (w->pointm)->charpos;
12941 int new_pt_byte = marker_byte_position (w->pointm);
12942 if (new_pt < BEGV)
12944 new_pt = BEGV;
12945 new_pt_byte = BEGV_BYTE;
12946 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12948 else if (new_pt > (ZV - 1))
12950 new_pt = ZV;
12951 new_pt_byte = ZV_BYTE;
12952 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12955 /* We don't use SET_PT so that the point-motion hooks don't run. */
12956 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12959 /* If any of the character widths specified in the display table
12960 have changed, invalidate the width run cache. It's true that
12961 this may be a bit late to catch such changes, but the rest of
12962 redisplay goes (non-fatally) haywire when the display table is
12963 changed, so why should we worry about doing any better? */
12964 if (current_buffer->width_run_cache)
12966 struct Lisp_Char_Table *disptab = buffer_display_table ();
12968 if (! disptab_matches_widthtab (disptab,
12969 XVECTOR (current_buffer->width_table)))
12971 invalidate_region_cache (current_buffer,
12972 current_buffer->width_run_cache,
12973 BEG, Z);
12974 recompute_width_table (current_buffer, disptab);
12978 /* If window-start is screwed up, choose a new one. */
12979 if (XMARKER (w->start)->buffer != current_buffer)
12980 goto recenter;
12982 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12984 /* If someone specified a new starting point but did not insist,
12985 check whether it can be used. */
12986 if (!NILP (w->optional_new_start)
12987 && CHARPOS (startp) >= BEGV
12988 && CHARPOS (startp) <= ZV)
12990 w->optional_new_start = Qnil;
12991 start_display (&it, w, startp);
12992 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12993 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12994 if (IT_CHARPOS (it) == PT)
12995 w->force_start = Qt;
12996 /* IT may overshoot PT if text at PT is invisible. */
12997 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12998 w->force_start = Qt;
13001 force_start:
13003 /* Handle case where place to start displaying has been specified,
13004 unless the specified location is outside the accessible range. */
13005 if (!NILP (w->force_start)
13006 || w->frozen_window_start_p)
13008 /* We set this later on if we have to adjust point. */
13009 int new_vpos = -1;
13010 int val;
13012 w->force_start = Qnil;
13013 w->vscroll = 0;
13014 w->window_end_valid = Qnil;
13016 /* Forget any recorded base line for line number display. */
13017 if (!buffer_unchanged_p)
13018 w->base_line_number = Qnil;
13020 /* Redisplay the mode line. Select the buffer properly for that.
13021 Also, run the hook window-scroll-functions
13022 because we have scrolled. */
13023 /* Note, we do this after clearing force_start because
13024 if there's an error, it is better to forget about force_start
13025 than to get into an infinite loop calling the hook functions
13026 and having them get more errors. */
13027 if (!update_mode_line
13028 || ! NILP (Vwindow_scroll_functions))
13030 update_mode_line = 1;
13031 w->update_mode_line = Qt;
13032 startp = run_window_scroll_functions (window, startp);
13035 w->last_modified = make_number (0);
13036 w->last_overlay_modified = make_number (0);
13037 if (CHARPOS (startp) < BEGV)
13038 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13039 else if (CHARPOS (startp) > ZV)
13040 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13042 /* Redisplay, then check if cursor has been set during the
13043 redisplay. Give up if new fonts were loaded. */
13044 val = try_window (window, startp, 1);
13045 if (!val)
13047 w->force_start = Qt;
13048 clear_glyph_matrix (w->desired_matrix);
13049 goto need_larger_matrices;
13051 /* Point was outside the scroll margins. */
13052 if (val < 0)
13053 new_vpos = window_box_height (w) / 2;
13055 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13057 /* If point does not appear, try to move point so it does
13058 appear. The desired matrix has been built above, so we
13059 can use it here. */
13060 new_vpos = window_box_height (w) / 2;
13063 if (!cursor_row_fully_visible_p (w, 0, 0))
13065 /* Point does appear, but on a line partly visible at end of window.
13066 Move it back to a fully-visible line. */
13067 new_vpos = window_box_height (w);
13070 /* If we need to move point for either of the above reasons,
13071 now actually do it. */
13072 if (new_vpos >= 0)
13074 struct glyph_row *row;
13076 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13077 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13078 ++row;
13080 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13081 MATRIX_ROW_START_BYTEPOS (row));
13083 if (w != XWINDOW (selected_window))
13084 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13085 else if (current_buffer == old)
13086 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13088 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13090 /* If we are highlighting the region, then we just changed
13091 the region, so redisplay to show it. */
13092 if (!NILP (Vtransient_mark_mode)
13093 && !NILP (current_buffer->mark_active))
13095 clear_glyph_matrix (w->desired_matrix);
13096 if (!try_window (window, startp, 0))
13097 goto need_larger_matrices;
13101 #if GLYPH_DEBUG
13102 debug_method_add (w, "forced window start");
13103 #endif
13104 goto done;
13107 /* Handle case where text has not changed, only point, and it has
13108 not moved off the frame, and we are not retrying after hscroll.
13109 (current_matrix_up_to_date_p is nonzero when retrying.) */
13110 if (current_matrix_up_to_date_p
13111 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13112 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13114 switch (rc)
13116 case CURSOR_MOVEMENT_SUCCESS:
13117 used_current_matrix_p = 1;
13118 goto done;
13120 #if 0 /* try_cursor_movement never returns this value. */
13121 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13122 goto need_larger_matrices;
13123 #endif
13125 case CURSOR_MOVEMENT_MUST_SCROLL:
13126 goto try_to_scroll;
13128 default:
13129 abort ();
13132 /* If current starting point was originally the beginning of a line
13133 but no longer is, find a new starting point. */
13134 else if (!NILP (w->start_at_line_beg)
13135 && !(CHARPOS (startp) <= BEGV
13136 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13138 #if GLYPH_DEBUG
13139 debug_method_add (w, "recenter 1");
13140 #endif
13141 goto recenter;
13144 /* Try scrolling with try_window_id. Value is > 0 if update has
13145 been done, it is -1 if we know that the same window start will
13146 not work. It is 0 if unsuccessful for some other reason. */
13147 else if ((tem = try_window_id (w)) != 0)
13149 #if GLYPH_DEBUG
13150 debug_method_add (w, "try_window_id %d", tem);
13151 #endif
13153 if (fonts_changed_p)
13154 goto need_larger_matrices;
13155 if (tem > 0)
13156 goto done;
13158 /* Otherwise try_window_id has returned -1 which means that we
13159 don't want the alternative below this comment to execute. */
13161 else if (CHARPOS (startp) >= BEGV
13162 && CHARPOS (startp) <= ZV
13163 && PT >= CHARPOS (startp)
13164 && (CHARPOS (startp) < ZV
13165 /* Avoid starting at end of buffer. */
13166 || CHARPOS (startp) == BEGV
13167 || (XFASTINT (w->last_modified) >= MODIFF
13168 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13171 /* If first window line is a continuation line, and window start
13172 is inside the modified region, but the first change is before
13173 current window start, we must select a new window start.
13175 However, if this is the result of a down-mouse event (e.g. by
13176 extending the mouse-drag-overlay), we don't want to select a
13177 new window start, since that would change the position under
13178 the mouse, resulting in an unwanted mouse-movement rather
13179 than a simple mouse-click. */
13180 if (NILP (w->start_at_line_beg)
13181 && NILP (do_mouse_tracking)
13182 && CHARPOS (startp) > BEGV
13183 && CHARPOS (startp) > BEG + save_beg_unchanged
13184 && CHARPOS (startp) <= Z - save_end_unchanged)
13186 w->force_start = Qt;
13187 if (XMARKER (w->start)->buffer == current_buffer)
13188 compute_window_start_on_continuation_line (w);
13189 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13190 goto force_start;
13193 #if GLYPH_DEBUG
13194 debug_method_add (w, "same window start");
13195 #endif
13197 /* Try to redisplay starting at same place as before.
13198 If point has not moved off frame, accept the results. */
13199 if (!current_matrix_up_to_date_p
13200 /* Don't use try_window_reusing_current_matrix in this case
13201 because a window scroll function can have changed the
13202 buffer. */
13203 || !NILP (Vwindow_scroll_functions)
13204 || MINI_WINDOW_P (w)
13205 || !(used_current_matrix_p
13206 = try_window_reusing_current_matrix (w)))
13208 IF_DEBUG (debug_method_add (w, "1"));
13209 if (try_window (window, startp, 1) < 0)
13210 /* -1 means we need to scroll.
13211 0 means we need new matrices, but fonts_changed_p
13212 is set in that case, so we will detect it below. */
13213 goto try_to_scroll;
13216 if (fonts_changed_p)
13217 goto need_larger_matrices;
13219 if (w->cursor.vpos >= 0)
13221 if (!just_this_one_p
13222 || current_buffer->clip_changed
13223 || BEG_UNCHANGED < CHARPOS (startp))
13224 /* Forget any recorded base line for line number display. */
13225 w->base_line_number = Qnil;
13227 if (!cursor_row_fully_visible_p (w, 1, 0))
13229 clear_glyph_matrix (w->desired_matrix);
13230 last_line_misfit = 1;
13232 /* Drop through and scroll. */
13233 else
13234 goto done;
13236 else
13237 clear_glyph_matrix (w->desired_matrix);
13240 try_to_scroll:
13242 w->last_modified = make_number (0);
13243 w->last_overlay_modified = make_number (0);
13245 /* Redisplay the mode line. Select the buffer properly for that. */
13246 if (!update_mode_line)
13248 update_mode_line = 1;
13249 w->update_mode_line = Qt;
13252 /* Try to scroll by specified few lines. */
13253 if ((scroll_conservatively
13254 || scroll_step
13255 || temp_scroll_step
13256 || NUMBERP (current_buffer->scroll_up_aggressively)
13257 || NUMBERP (current_buffer->scroll_down_aggressively))
13258 && !current_buffer->clip_changed
13259 && CHARPOS (startp) >= BEGV
13260 && CHARPOS (startp) <= ZV)
13262 /* The function returns -1 if new fonts were loaded, 1 if
13263 successful, 0 if not successful. */
13264 int rc = try_scrolling (window, just_this_one_p,
13265 scroll_conservatively,
13266 scroll_step,
13267 temp_scroll_step, last_line_misfit);
13268 switch (rc)
13270 case SCROLLING_SUCCESS:
13271 goto done;
13273 case SCROLLING_NEED_LARGER_MATRICES:
13274 goto need_larger_matrices;
13276 case SCROLLING_FAILED:
13277 break;
13279 default:
13280 abort ();
13284 /* Finally, just choose place to start which centers point */
13286 recenter:
13287 if (centering_position < 0)
13288 centering_position = window_box_height (w) / 2;
13290 #if GLYPH_DEBUG
13291 debug_method_add (w, "recenter");
13292 #endif
13294 /* w->vscroll = 0; */
13296 /* Forget any previously recorded base line for line number display. */
13297 if (!buffer_unchanged_p)
13298 w->base_line_number = Qnil;
13300 /* Move backward half the height of the window. */
13301 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13302 it.current_y = it.last_visible_y;
13303 move_it_vertically_backward (&it, centering_position);
13304 xassert (IT_CHARPOS (it) >= BEGV);
13306 /* The function move_it_vertically_backward may move over more
13307 than the specified y-distance. If it->w is small, e.g. a
13308 mini-buffer window, we may end up in front of the window's
13309 display area. Start displaying at the start of the line
13310 containing PT in this case. */
13311 if (it.current_y <= 0)
13313 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13314 move_it_vertically_backward (&it, 0);
13315 #if 0
13316 /* I think this assert is bogus if buffer contains
13317 invisible text or images. KFS. */
13318 xassert (IT_CHARPOS (it) <= PT);
13319 #endif
13320 it.current_y = 0;
13323 it.current_x = it.hpos = 0;
13325 /* Set startp here explicitly in case that helps avoid an infinite loop
13326 in case the window-scroll-functions functions get errors. */
13327 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13329 /* Run scroll hooks. */
13330 startp = run_window_scroll_functions (window, it.current.pos);
13332 /* Redisplay the window. */
13333 if (!current_matrix_up_to_date_p
13334 || windows_or_buffers_changed
13335 || cursor_type_changed
13336 /* Don't use try_window_reusing_current_matrix in this case
13337 because it can have changed the buffer. */
13338 || !NILP (Vwindow_scroll_functions)
13339 || !just_this_one_p
13340 || MINI_WINDOW_P (w)
13341 || !(used_current_matrix_p
13342 = try_window_reusing_current_matrix (w)))
13343 try_window (window, startp, 0);
13345 /* If new fonts have been loaded (due to fontsets), give up. We
13346 have to start a new redisplay since we need to re-adjust glyph
13347 matrices. */
13348 if (fonts_changed_p)
13349 goto need_larger_matrices;
13351 /* If cursor did not appear assume that the middle of the window is
13352 in the first line of the window. Do it again with the next line.
13353 (Imagine a window of height 100, displaying two lines of height
13354 60. Moving back 50 from it->last_visible_y will end in the first
13355 line.) */
13356 if (w->cursor.vpos < 0)
13358 if (!NILP (w->window_end_valid)
13359 && PT >= Z - XFASTINT (w->window_end_pos))
13361 clear_glyph_matrix (w->desired_matrix);
13362 move_it_by_lines (&it, 1, 0);
13363 try_window (window, it.current.pos, 0);
13365 else if (PT < IT_CHARPOS (it))
13367 clear_glyph_matrix (w->desired_matrix);
13368 move_it_by_lines (&it, -1, 0);
13369 try_window (window, it.current.pos, 0);
13371 else
13373 /* Not much we can do about it. */
13377 /* Consider the following case: Window starts at BEGV, there is
13378 invisible, intangible text at BEGV, so that display starts at
13379 some point START > BEGV. It can happen that we are called with
13380 PT somewhere between BEGV and START. Try to handle that case. */
13381 if (w->cursor.vpos < 0)
13383 struct glyph_row *row = w->current_matrix->rows;
13384 if (row->mode_line_p)
13385 ++row;
13386 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13389 if (!cursor_row_fully_visible_p (w, 0, 0))
13391 /* If vscroll is enabled, disable it and try again. */
13392 if (w->vscroll)
13394 w->vscroll = 0;
13395 clear_glyph_matrix (w->desired_matrix);
13396 goto recenter;
13399 /* If centering point failed to make the whole line visible,
13400 put point at the top instead. That has to make the whole line
13401 visible, if it can be done. */
13402 if (centering_position == 0)
13403 goto done;
13405 clear_glyph_matrix (w->desired_matrix);
13406 centering_position = 0;
13407 goto recenter;
13410 done:
13412 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13413 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13414 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13415 ? Qt : Qnil);
13417 /* Display the mode line, if we must. */
13418 if ((update_mode_line
13419 /* If window not full width, must redo its mode line
13420 if (a) the window to its side is being redone and
13421 (b) we do a frame-based redisplay. This is a consequence
13422 of how inverted lines are drawn in frame-based redisplay. */
13423 || (!just_this_one_p
13424 && !FRAME_WINDOW_P (f)
13425 && !WINDOW_FULL_WIDTH_P (w))
13426 /* Line number to display. */
13427 || INTEGERP (w->base_line_pos)
13428 /* Column number is displayed and different from the one displayed. */
13429 || (!NILP (w->column_number_displayed)
13430 && (XFASTINT (w->column_number_displayed)
13431 != (int) current_column ()))) /* iftc */
13432 /* This means that the window has a mode line. */
13433 && (WINDOW_WANTS_MODELINE_P (w)
13434 || WINDOW_WANTS_HEADER_LINE_P (w)))
13436 display_mode_lines (w);
13438 /* If mode line height has changed, arrange for a thorough
13439 immediate redisplay using the correct mode line height. */
13440 if (WINDOW_WANTS_MODELINE_P (w)
13441 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13443 fonts_changed_p = 1;
13444 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13445 = DESIRED_MODE_LINE_HEIGHT (w);
13448 /* If top line height has changed, arrange for a thorough
13449 immediate redisplay using the correct mode line height. */
13450 if (WINDOW_WANTS_HEADER_LINE_P (w)
13451 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13453 fonts_changed_p = 1;
13454 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13455 = DESIRED_HEADER_LINE_HEIGHT (w);
13458 if (fonts_changed_p)
13459 goto need_larger_matrices;
13462 if (!line_number_displayed
13463 && !BUFFERP (w->base_line_pos))
13465 w->base_line_pos = Qnil;
13466 w->base_line_number = Qnil;
13469 finish_menu_bars:
13471 /* When we reach a frame's selected window, redo the frame's menu bar. */
13472 if (update_mode_line
13473 && EQ (FRAME_SELECTED_WINDOW (f), window))
13475 int redisplay_menu_p = 0;
13476 int redisplay_tool_bar_p = 0;
13478 if (FRAME_WINDOW_P (f))
13480 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13481 || defined (USE_GTK)
13482 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13483 #else
13484 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13485 #endif
13487 else
13488 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13490 if (redisplay_menu_p)
13491 display_menu_bar (w);
13493 #ifdef HAVE_WINDOW_SYSTEM
13494 #ifdef USE_GTK
13495 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13496 #else
13497 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13498 && (FRAME_TOOL_BAR_LINES (f) > 0
13499 || !NILP (Vauto_resize_tool_bars));
13501 #endif
13503 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13505 extern int ignore_mouse_drag_p;
13506 ignore_mouse_drag_p = 1;
13508 #endif
13511 #ifdef HAVE_WINDOW_SYSTEM
13512 if (FRAME_WINDOW_P (f)
13513 && update_window_fringes (w, (just_this_one_p
13514 || (!used_current_matrix_p && !overlay_arrow_seen)
13515 || w->pseudo_window_p)))
13517 update_begin (f);
13518 BLOCK_INPUT;
13519 if (draw_window_fringes (w, 1))
13520 x_draw_vertical_border (w);
13521 UNBLOCK_INPUT;
13522 update_end (f);
13524 #endif /* HAVE_WINDOW_SYSTEM */
13526 /* We go to this label, with fonts_changed_p nonzero,
13527 if it is necessary to try again using larger glyph matrices.
13528 We have to redeem the scroll bar even in this case,
13529 because the loop in redisplay_internal expects that. */
13530 need_larger_matrices:
13532 finish_scroll_bars:
13534 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13536 /* Set the thumb's position and size. */
13537 set_vertical_scroll_bar (w);
13539 /* Note that we actually used the scroll bar attached to this
13540 window, so it shouldn't be deleted at the end of redisplay. */
13541 redeem_scroll_bar_hook (w);
13544 /* Restore current_buffer and value of point in it. */
13545 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13546 set_buffer_internal_1 (old);
13547 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13548 shorter. This can be caused by log truncation in *Messages*. */
13549 if (CHARPOS (lpoint) <= ZV)
13550 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13552 unbind_to (count, Qnil);
13556 /* Build the complete desired matrix of WINDOW with a window start
13557 buffer position POS.
13559 Value is 1 if successful. It is zero if fonts were loaded during
13560 redisplay which makes re-adjusting glyph matrices necessary, and -1
13561 if point would appear in the scroll margins.
13562 (We check that only if CHECK_MARGINS is nonzero. */
13565 try_window (window, pos, check_margins)
13566 Lisp_Object window;
13567 struct text_pos pos;
13568 int check_margins;
13570 struct window *w = XWINDOW (window);
13571 struct it it;
13572 struct glyph_row *last_text_row = NULL;
13573 struct frame *f = XFRAME (w->frame);
13575 /* Make POS the new window start. */
13576 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13578 /* Mark cursor position as unknown. No overlay arrow seen. */
13579 w->cursor.vpos = -1;
13580 overlay_arrow_seen = 0;
13582 /* Initialize iterator and info to start at POS. */
13583 start_display (&it, w, pos);
13585 /* Display all lines of W. */
13586 while (it.current_y < it.last_visible_y)
13588 if (display_line (&it))
13589 last_text_row = it.glyph_row - 1;
13590 if (fonts_changed_p)
13591 return 0;
13594 /* Don't let the cursor end in the scroll margins. */
13595 if (check_margins
13596 && !MINI_WINDOW_P (w))
13598 int this_scroll_margin;
13600 this_scroll_margin = max (0, scroll_margin);
13601 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13602 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13604 if ((w->cursor.y >= 0 /* not vscrolled */
13605 && w->cursor.y < this_scroll_margin
13606 && CHARPOS (pos) > BEGV
13607 && IT_CHARPOS (it) < ZV)
13608 /* rms: considering make_cursor_line_fully_visible_p here
13609 seems to give wrong results. We don't want to recenter
13610 when the last line is partly visible, we want to allow
13611 that case to be handled in the usual way. */
13612 || (w->cursor.y + 1) > it.last_visible_y)
13614 w->cursor.vpos = -1;
13615 clear_glyph_matrix (w->desired_matrix);
13616 return -1;
13620 /* If bottom moved off end of frame, change mode line percentage. */
13621 if (XFASTINT (w->window_end_pos) <= 0
13622 && Z != IT_CHARPOS (it))
13623 w->update_mode_line = Qt;
13625 /* Set window_end_pos to the offset of the last character displayed
13626 on the window from the end of current_buffer. Set
13627 window_end_vpos to its row number. */
13628 if (last_text_row)
13630 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13631 w->window_end_bytepos
13632 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13633 w->window_end_pos
13634 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13635 w->window_end_vpos
13636 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13637 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13638 ->displays_text_p);
13640 else
13642 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13643 w->window_end_pos = make_number (Z - ZV);
13644 w->window_end_vpos = make_number (0);
13647 /* But that is not valid info until redisplay finishes. */
13648 w->window_end_valid = Qnil;
13649 return 1;
13654 /************************************************************************
13655 Window redisplay reusing current matrix when buffer has not changed
13656 ************************************************************************/
13658 /* Try redisplay of window W showing an unchanged buffer with a
13659 different window start than the last time it was displayed by
13660 reusing its current matrix. Value is non-zero if successful.
13661 W->start is the new window start. */
13663 static int
13664 try_window_reusing_current_matrix (w)
13665 struct window *w;
13667 struct frame *f = XFRAME (w->frame);
13668 struct glyph_row *row, *bottom_row;
13669 struct it it;
13670 struct run run;
13671 struct text_pos start, new_start;
13672 int nrows_scrolled, i;
13673 struct glyph_row *last_text_row;
13674 struct glyph_row *last_reused_text_row;
13675 struct glyph_row *start_row;
13676 int start_vpos, min_y, max_y;
13678 #if GLYPH_DEBUG
13679 if (inhibit_try_window_reusing)
13680 return 0;
13681 #endif
13683 if (/* This function doesn't handle terminal frames. */
13684 !FRAME_WINDOW_P (f)
13685 /* Don't try to reuse the display if windows have been split
13686 or such. */
13687 || windows_or_buffers_changed
13688 || cursor_type_changed)
13689 return 0;
13691 /* Can't do this if region may have changed. */
13692 if ((!NILP (Vtransient_mark_mode)
13693 && !NILP (current_buffer->mark_active))
13694 || !NILP (w->region_showing)
13695 || !NILP (Vshow_trailing_whitespace))
13696 return 0;
13698 /* If top-line visibility has changed, give up. */
13699 if (WINDOW_WANTS_HEADER_LINE_P (w)
13700 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13701 return 0;
13703 /* Give up if old or new display is scrolled vertically. We could
13704 make this function handle this, but right now it doesn't. */
13705 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13706 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13707 return 0;
13709 /* The variable new_start now holds the new window start. The old
13710 start `start' can be determined from the current matrix. */
13711 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13712 start = start_row->start.pos;
13713 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13715 /* Clear the desired matrix for the display below. */
13716 clear_glyph_matrix (w->desired_matrix);
13718 if (CHARPOS (new_start) <= CHARPOS (start))
13720 int first_row_y;
13722 /* Don't use this method if the display starts with an ellipsis
13723 displayed for invisible text. It's not easy to handle that case
13724 below, and it's certainly not worth the effort since this is
13725 not a frequent case. */
13726 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13727 return 0;
13729 IF_DEBUG (debug_method_add (w, "twu1"));
13731 /* Display up to a row that can be reused. The variable
13732 last_text_row is set to the last row displayed that displays
13733 text. Note that it.vpos == 0 if or if not there is a
13734 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13735 start_display (&it, w, new_start);
13736 first_row_y = it.current_y;
13737 w->cursor.vpos = -1;
13738 last_text_row = last_reused_text_row = NULL;
13740 while (it.current_y < it.last_visible_y
13741 && !fonts_changed_p)
13743 /* If we have reached into the characters in the START row,
13744 that means the line boundaries have changed. So we
13745 can't start copying with the row START. Maybe it will
13746 work to start copying with the following row. */
13747 while (IT_CHARPOS (it) > CHARPOS (start))
13749 /* Advance to the next row as the "start". */
13750 start_row++;
13751 start = start_row->start.pos;
13752 /* If there are no more rows to try, or just one, give up. */
13753 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13754 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13755 || CHARPOS (start) == ZV)
13757 clear_glyph_matrix (w->desired_matrix);
13758 return 0;
13761 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13763 /* If we have reached alignment,
13764 we can copy the rest of the rows. */
13765 if (IT_CHARPOS (it) == CHARPOS (start))
13766 break;
13768 if (display_line (&it))
13769 last_text_row = it.glyph_row - 1;
13772 /* A value of current_y < last_visible_y means that we stopped
13773 at the previous window start, which in turn means that we
13774 have at least one reusable row. */
13775 if (it.current_y < it.last_visible_y)
13777 /* IT.vpos always starts from 0; it counts text lines. */
13778 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13780 /* Find PT if not already found in the lines displayed. */
13781 if (w->cursor.vpos < 0)
13783 int dy = it.current_y - start_row->y;
13785 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13786 row = row_containing_pos (w, PT, row, NULL, dy);
13787 if (row)
13788 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13789 dy, nrows_scrolled);
13790 else
13792 clear_glyph_matrix (w->desired_matrix);
13793 return 0;
13797 /* Scroll the display. Do it before the current matrix is
13798 changed. The problem here is that update has not yet
13799 run, i.e. part of the current matrix is not up to date.
13800 scroll_run_hook will clear the cursor, and use the
13801 current matrix to get the height of the row the cursor is
13802 in. */
13803 run.current_y = start_row->y;
13804 run.desired_y = it.current_y;
13805 run.height = it.last_visible_y - it.current_y;
13807 if (run.height > 0 && run.current_y != run.desired_y)
13809 update_begin (f);
13810 rif->update_window_begin_hook (w);
13811 rif->clear_window_mouse_face (w);
13812 rif->scroll_run_hook (w, &run);
13813 rif->update_window_end_hook (w, 0, 0);
13814 update_end (f);
13817 /* Shift current matrix down by nrows_scrolled lines. */
13818 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13819 rotate_matrix (w->current_matrix,
13820 start_vpos,
13821 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13822 nrows_scrolled);
13824 /* Disable lines that must be updated. */
13825 for (i = 0; i < nrows_scrolled; ++i)
13826 (start_row + i)->enabled_p = 0;
13828 /* Re-compute Y positions. */
13829 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13830 max_y = it.last_visible_y;
13831 for (row = start_row + nrows_scrolled;
13832 row < bottom_row;
13833 ++row)
13835 row->y = it.current_y;
13836 row->visible_height = row->height;
13838 if (row->y < min_y)
13839 row->visible_height -= min_y - row->y;
13840 if (row->y + row->height > max_y)
13841 row->visible_height -= row->y + row->height - max_y;
13842 row->redraw_fringe_bitmaps_p = 1;
13844 it.current_y += row->height;
13846 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13847 last_reused_text_row = row;
13848 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13849 break;
13852 /* Disable lines in the current matrix which are now
13853 below the window. */
13854 for (++row; row < bottom_row; ++row)
13855 row->enabled_p = row->mode_line_p = 0;
13858 /* Update window_end_pos etc.; last_reused_text_row is the last
13859 reused row from the current matrix containing text, if any.
13860 The value of last_text_row is the last displayed line
13861 containing text. */
13862 if (last_reused_text_row)
13864 w->window_end_bytepos
13865 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13866 w->window_end_pos
13867 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13868 w->window_end_vpos
13869 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13870 w->current_matrix));
13872 else if (last_text_row)
13874 w->window_end_bytepos
13875 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13876 w->window_end_pos
13877 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13878 w->window_end_vpos
13879 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13881 else
13883 /* This window must be completely empty. */
13884 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13885 w->window_end_pos = make_number (Z - ZV);
13886 w->window_end_vpos = make_number (0);
13888 w->window_end_valid = Qnil;
13890 /* Update hint: don't try scrolling again in update_window. */
13891 w->desired_matrix->no_scrolling_p = 1;
13893 #if GLYPH_DEBUG
13894 debug_method_add (w, "try_window_reusing_current_matrix 1");
13895 #endif
13896 return 1;
13898 else if (CHARPOS (new_start) > CHARPOS (start))
13900 struct glyph_row *pt_row, *row;
13901 struct glyph_row *first_reusable_row;
13902 struct glyph_row *first_row_to_display;
13903 int dy;
13904 int yb = window_text_bottom_y (w);
13906 /* Find the row starting at new_start, if there is one. Don't
13907 reuse a partially visible line at the end. */
13908 first_reusable_row = start_row;
13909 while (first_reusable_row->enabled_p
13910 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13911 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13912 < CHARPOS (new_start)))
13913 ++first_reusable_row;
13915 /* Give up if there is no row to reuse. */
13916 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13917 || !first_reusable_row->enabled_p
13918 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13919 != CHARPOS (new_start)))
13920 return 0;
13922 /* We can reuse fully visible rows beginning with
13923 first_reusable_row to the end of the window. Set
13924 first_row_to_display to the first row that cannot be reused.
13925 Set pt_row to the row containing point, if there is any. */
13926 pt_row = NULL;
13927 for (first_row_to_display = first_reusable_row;
13928 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13929 ++first_row_to_display)
13931 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13932 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13933 pt_row = first_row_to_display;
13936 /* Start displaying at the start of first_row_to_display. */
13937 xassert (first_row_to_display->y < yb);
13938 init_to_row_start (&it, w, first_row_to_display);
13940 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13941 - start_vpos);
13942 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13943 - nrows_scrolled);
13944 it.current_y = (first_row_to_display->y - first_reusable_row->y
13945 + WINDOW_HEADER_LINE_HEIGHT (w));
13947 /* Display lines beginning with first_row_to_display in the
13948 desired matrix. Set last_text_row to the last row displayed
13949 that displays text. */
13950 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13951 if (pt_row == NULL)
13952 w->cursor.vpos = -1;
13953 last_text_row = NULL;
13954 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13955 if (display_line (&it))
13956 last_text_row = it.glyph_row - 1;
13958 /* Give up If point isn't in a row displayed or reused. */
13959 if (w->cursor.vpos < 0)
13961 clear_glyph_matrix (w->desired_matrix);
13962 return 0;
13965 /* If point is in a reused row, adjust y and vpos of the cursor
13966 position. */
13967 if (pt_row)
13969 w->cursor.vpos -= nrows_scrolled;
13970 w->cursor.y -= first_reusable_row->y - start_row->y;
13973 /* Scroll the display. */
13974 run.current_y = first_reusable_row->y;
13975 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13976 run.height = it.last_visible_y - run.current_y;
13977 dy = run.current_y - run.desired_y;
13979 if (run.height)
13981 update_begin (f);
13982 rif->update_window_begin_hook (w);
13983 rif->clear_window_mouse_face (w);
13984 rif->scroll_run_hook (w, &run);
13985 rif->update_window_end_hook (w, 0, 0);
13986 update_end (f);
13989 /* Adjust Y positions of reused rows. */
13990 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13991 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13992 max_y = it.last_visible_y;
13993 for (row = first_reusable_row; row < first_row_to_display; ++row)
13995 row->y -= dy;
13996 row->visible_height = row->height;
13997 if (row->y < min_y)
13998 row->visible_height -= min_y - row->y;
13999 if (row->y + row->height > max_y)
14000 row->visible_height -= row->y + row->height - max_y;
14001 row->redraw_fringe_bitmaps_p = 1;
14004 /* Scroll the current matrix. */
14005 xassert (nrows_scrolled > 0);
14006 rotate_matrix (w->current_matrix,
14007 start_vpos,
14008 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14009 -nrows_scrolled);
14011 /* Disable rows not reused. */
14012 for (row -= nrows_scrolled; row < bottom_row; ++row)
14013 row->enabled_p = 0;
14015 /* Point may have moved to a different line, so we cannot assume that
14016 the previous cursor position is valid; locate the correct row. */
14017 if (pt_row)
14019 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14020 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14021 row++)
14023 w->cursor.vpos++;
14024 w->cursor.y = row->y;
14026 if (row < bottom_row)
14028 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14029 while (glyph->charpos < PT)
14031 w->cursor.hpos++;
14032 w->cursor.x += glyph->pixel_width;
14033 glyph++;
14038 /* Adjust window end. A null value of last_text_row means that
14039 the window end is in reused rows which in turn means that
14040 only its vpos can have changed. */
14041 if (last_text_row)
14043 w->window_end_bytepos
14044 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14045 w->window_end_pos
14046 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14047 w->window_end_vpos
14048 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14050 else
14052 w->window_end_vpos
14053 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14056 w->window_end_valid = Qnil;
14057 w->desired_matrix->no_scrolling_p = 1;
14059 #if GLYPH_DEBUG
14060 debug_method_add (w, "try_window_reusing_current_matrix 2");
14061 #endif
14062 return 1;
14065 return 0;
14070 /************************************************************************
14071 Window redisplay reusing current matrix when buffer has changed
14072 ************************************************************************/
14074 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14075 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14076 int *, int *));
14077 static struct glyph_row *
14078 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14079 struct glyph_row *));
14082 /* Return the last row in MATRIX displaying text. If row START is
14083 non-null, start searching with that row. IT gives the dimensions
14084 of the display. Value is null if matrix is empty; otherwise it is
14085 a pointer to the row found. */
14087 static struct glyph_row *
14088 find_last_row_displaying_text (matrix, it, start)
14089 struct glyph_matrix *matrix;
14090 struct it *it;
14091 struct glyph_row *start;
14093 struct glyph_row *row, *row_found;
14095 /* Set row_found to the last row in IT->w's current matrix
14096 displaying text. The loop looks funny but think of partially
14097 visible lines. */
14098 row_found = NULL;
14099 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14100 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14102 xassert (row->enabled_p);
14103 row_found = row;
14104 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14105 break;
14106 ++row;
14109 return row_found;
14113 /* Return the last row in the current matrix of W that is not affected
14114 by changes at the start of current_buffer that occurred since W's
14115 current matrix was built. Value is null if no such row exists.
14117 BEG_UNCHANGED us the number of characters unchanged at the start of
14118 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14119 first changed character in current_buffer. Characters at positions <
14120 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14121 when the current matrix was built. */
14123 static struct glyph_row *
14124 find_last_unchanged_at_beg_row (w)
14125 struct window *w;
14127 int first_changed_pos = BEG + BEG_UNCHANGED;
14128 struct glyph_row *row;
14129 struct glyph_row *row_found = NULL;
14130 int yb = window_text_bottom_y (w);
14132 /* Find the last row displaying unchanged text. */
14133 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14134 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14135 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14137 if (/* If row ends before first_changed_pos, it is unchanged,
14138 except in some case. */
14139 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14140 /* When row ends in ZV and we write at ZV it is not
14141 unchanged. */
14142 && !row->ends_at_zv_p
14143 /* When first_changed_pos is the end of a continued line,
14144 row is not unchanged because it may be no longer
14145 continued. */
14146 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14147 && (row->continued_p
14148 || row->exact_window_width_line_p)))
14149 row_found = row;
14151 /* Stop if last visible row. */
14152 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14153 break;
14155 ++row;
14158 return row_found;
14162 /* Find the first glyph row in the current matrix of W that is not
14163 affected by changes at the end of current_buffer since the
14164 time W's current matrix was built.
14166 Return in *DELTA the number of chars by which buffer positions in
14167 unchanged text at the end of current_buffer must be adjusted.
14169 Return in *DELTA_BYTES the corresponding number of bytes.
14171 Value is null if no such row exists, i.e. all rows are affected by
14172 changes. */
14174 static struct glyph_row *
14175 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14176 struct window *w;
14177 int *delta, *delta_bytes;
14179 struct glyph_row *row;
14180 struct glyph_row *row_found = NULL;
14182 *delta = *delta_bytes = 0;
14184 /* Display must not have been paused, otherwise the current matrix
14185 is not up to date. */
14186 if (NILP (w->window_end_valid))
14187 abort ();
14189 /* A value of window_end_pos >= END_UNCHANGED means that the window
14190 end is in the range of changed text. If so, there is no
14191 unchanged row at the end of W's current matrix. */
14192 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14193 return NULL;
14195 /* Set row to the last row in W's current matrix displaying text. */
14196 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14198 /* If matrix is entirely empty, no unchanged row exists. */
14199 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14201 /* The value of row is the last glyph row in the matrix having a
14202 meaningful buffer position in it. The end position of row
14203 corresponds to window_end_pos. This allows us to translate
14204 buffer positions in the current matrix to current buffer
14205 positions for characters not in changed text. */
14206 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14207 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14208 int last_unchanged_pos, last_unchanged_pos_old;
14209 struct glyph_row *first_text_row
14210 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14212 *delta = Z - Z_old;
14213 *delta_bytes = Z_BYTE - Z_BYTE_old;
14215 /* Set last_unchanged_pos to the buffer position of the last
14216 character in the buffer that has not been changed. Z is the
14217 index + 1 of the last character in current_buffer, i.e. by
14218 subtracting END_UNCHANGED we get the index of the last
14219 unchanged character, and we have to add BEG to get its buffer
14220 position. */
14221 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14222 last_unchanged_pos_old = last_unchanged_pos - *delta;
14224 /* Search backward from ROW for a row displaying a line that
14225 starts at a minimum position >= last_unchanged_pos_old. */
14226 for (; row > first_text_row; --row)
14228 /* This used to abort, but it can happen.
14229 It is ok to just stop the search instead here. KFS. */
14230 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14231 break;
14233 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14234 row_found = row;
14238 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14239 abort ();
14241 return row_found;
14245 /* Make sure that glyph rows in the current matrix of window W
14246 reference the same glyph memory as corresponding rows in the
14247 frame's frame matrix. This function is called after scrolling W's
14248 current matrix on a terminal frame in try_window_id and
14249 try_window_reusing_current_matrix. */
14251 static void
14252 sync_frame_with_window_matrix_rows (w)
14253 struct window *w;
14255 struct frame *f = XFRAME (w->frame);
14256 struct glyph_row *window_row, *window_row_end, *frame_row;
14258 /* Preconditions: W must be a leaf window and full-width. Its frame
14259 must have a frame matrix. */
14260 xassert (NILP (w->hchild) && NILP (w->vchild));
14261 xassert (WINDOW_FULL_WIDTH_P (w));
14262 xassert (!FRAME_WINDOW_P (f));
14264 /* If W is a full-width window, glyph pointers in W's current matrix
14265 have, by definition, to be the same as glyph pointers in the
14266 corresponding frame matrix. Note that frame matrices have no
14267 marginal areas (see build_frame_matrix). */
14268 window_row = w->current_matrix->rows;
14269 window_row_end = window_row + w->current_matrix->nrows;
14270 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14271 while (window_row < window_row_end)
14273 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14274 struct glyph *end = window_row->glyphs[LAST_AREA];
14276 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14277 frame_row->glyphs[TEXT_AREA] = start;
14278 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14279 frame_row->glyphs[LAST_AREA] = end;
14281 /* Disable frame rows whose corresponding window rows have
14282 been disabled in try_window_id. */
14283 if (!window_row->enabled_p)
14284 frame_row->enabled_p = 0;
14286 ++window_row, ++frame_row;
14291 /* Find the glyph row in window W containing CHARPOS. Consider all
14292 rows between START and END (not inclusive). END null means search
14293 all rows to the end of the display area of W. Value is the row
14294 containing CHARPOS or null. */
14296 struct glyph_row *
14297 row_containing_pos (w, charpos, start, end, dy)
14298 struct window *w;
14299 int charpos;
14300 struct glyph_row *start, *end;
14301 int dy;
14303 struct glyph_row *row = start;
14304 int last_y;
14306 /* If we happen to start on a header-line, skip that. */
14307 if (row->mode_line_p)
14308 ++row;
14310 if ((end && row >= end) || !row->enabled_p)
14311 return NULL;
14313 last_y = window_text_bottom_y (w) - dy;
14315 while (1)
14317 /* Give up if we have gone too far. */
14318 if (end && row >= end)
14319 return NULL;
14320 /* This formerly returned if they were equal.
14321 I think that both quantities are of a "last plus one" type;
14322 if so, when they are equal, the row is within the screen. -- rms. */
14323 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14324 return NULL;
14326 /* If it is in this row, return this row. */
14327 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14328 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14329 /* The end position of a row equals the start
14330 position of the next row. If CHARPOS is there, we
14331 would rather display it in the next line, except
14332 when this line ends in ZV. */
14333 && !row->ends_at_zv_p
14334 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14335 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14336 return row;
14337 ++row;
14342 /* Try to redisplay window W by reusing its existing display. W's
14343 current matrix must be up to date when this function is called,
14344 i.e. window_end_valid must not be nil.
14346 Value is
14348 1 if display has been updated
14349 0 if otherwise unsuccessful
14350 -1 if redisplay with same window start is known not to succeed
14352 The following steps are performed:
14354 1. Find the last row in the current matrix of W that is not
14355 affected by changes at the start of current_buffer. If no such row
14356 is found, give up.
14358 2. Find the first row in W's current matrix that is not affected by
14359 changes at the end of current_buffer. Maybe there is no such row.
14361 3. Display lines beginning with the row + 1 found in step 1 to the
14362 row found in step 2 or, if step 2 didn't find a row, to the end of
14363 the window.
14365 4. If cursor is not known to appear on the window, give up.
14367 5. If display stopped at the row found in step 2, scroll the
14368 display and current matrix as needed.
14370 6. Maybe display some lines at the end of W, if we must. This can
14371 happen under various circumstances, like a partially visible line
14372 becoming fully visible, or because newly displayed lines are displayed
14373 in smaller font sizes.
14375 7. Update W's window end information. */
14377 static int
14378 try_window_id (w)
14379 struct window *w;
14381 struct frame *f = XFRAME (w->frame);
14382 struct glyph_matrix *current_matrix = w->current_matrix;
14383 struct glyph_matrix *desired_matrix = w->desired_matrix;
14384 struct glyph_row *last_unchanged_at_beg_row;
14385 struct glyph_row *first_unchanged_at_end_row;
14386 struct glyph_row *row;
14387 struct glyph_row *bottom_row;
14388 int bottom_vpos;
14389 struct it it;
14390 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14391 struct text_pos start_pos;
14392 struct run run;
14393 int first_unchanged_at_end_vpos = 0;
14394 struct glyph_row *last_text_row, *last_text_row_at_end;
14395 struct text_pos start;
14396 int first_changed_charpos, last_changed_charpos;
14398 #if GLYPH_DEBUG
14399 if (inhibit_try_window_id)
14400 return 0;
14401 #endif
14403 /* This is handy for debugging. */
14404 #if 0
14405 #define GIVE_UP(X) \
14406 do { \
14407 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14408 return 0; \
14409 } while (0)
14410 #else
14411 #define GIVE_UP(X) return 0
14412 #endif
14414 SET_TEXT_POS_FROM_MARKER (start, w->start);
14416 /* Don't use this for mini-windows because these can show
14417 messages and mini-buffers, and we don't handle that here. */
14418 if (MINI_WINDOW_P (w))
14419 GIVE_UP (1);
14421 /* This flag is used to prevent redisplay optimizations. */
14422 if (windows_or_buffers_changed || cursor_type_changed)
14423 GIVE_UP (2);
14425 /* Verify that narrowing has not changed.
14426 Also verify that we were not told to prevent redisplay optimizations.
14427 It would be nice to further
14428 reduce the number of cases where this prevents try_window_id. */
14429 if (current_buffer->clip_changed
14430 || current_buffer->prevent_redisplay_optimizations_p)
14431 GIVE_UP (3);
14433 /* Window must either use window-based redisplay or be full width. */
14434 if (!FRAME_WINDOW_P (f)
14435 && (!line_ins_del_ok
14436 || !WINDOW_FULL_WIDTH_P (w)))
14437 GIVE_UP (4);
14439 /* Give up if point is not known NOT to appear in W. */
14440 if (PT < CHARPOS (start))
14441 GIVE_UP (5);
14443 /* Another way to prevent redisplay optimizations. */
14444 if (XFASTINT (w->last_modified) == 0)
14445 GIVE_UP (6);
14447 /* Verify that window is not hscrolled. */
14448 if (XFASTINT (w->hscroll) != 0)
14449 GIVE_UP (7);
14451 /* Verify that display wasn't paused. */
14452 if (NILP (w->window_end_valid))
14453 GIVE_UP (8);
14455 /* Can't use this if highlighting a region because a cursor movement
14456 will do more than just set the cursor. */
14457 if (!NILP (Vtransient_mark_mode)
14458 && !NILP (current_buffer->mark_active))
14459 GIVE_UP (9);
14461 /* Likewise if highlighting trailing whitespace. */
14462 if (!NILP (Vshow_trailing_whitespace))
14463 GIVE_UP (11);
14465 /* Likewise if showing a region. */
14466 if (!NILP (w->region_showing))
14467 GIVE_UP (10);
14469 /* Can use this if overlay arrow position and or string have changed. */
14470 if (overlay_arrows_changed_p ())
14471 GIVE_UP (12);
14474 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14475 only if buffer has really changed. The reason is that the gap is
14476 initially at Z for freshly visited files. The code below would
14477 set end_unchanged to 0 in that case. */
14478 if (MODIFF > SAVE_MODIFF
14479 /* This seems to happen sometimes after saving a buffer. */
14480 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14482 if (GPT - BEG < BEG_UNCHANGED)
14483 BEG_UNCHANGED = GPT - BEG;
14484 if (Z - GPT < END_UNCHANGED)
14485 END_UNCHANGED = Z - GPT;
14488 /* The position of the first and last character that has been changed. */
14489 first_changed_charpos = BEG + BEG_UNCHANGED;
14490 last_changed_charpos = Z - END_UNCHANGED;
14492 /* If window starts after a line end, and the last change is in
14493 front of that newline, then changes don't affect the display.
14494 This case happens with stealth-fontification. Note that although
14495 the display is unchanged, glyph positions in the matrix have to
14496 be adjusted, of course. */
14497 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14498 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14499 && ((last_changed_charpos < CHARPOS (start)
14500 && CHARPOS (start) == BEGV)
14501 || (last_changed_charpos < CHARPOS (start) - 1
14502 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14504 int Z_old, delta, Z_BYTE_old, delta_bytes;
14505 struct glyph_row *r0;
14507 /* Compute how many chars/bytes have been added to or removed
14508 from the buffer. */
14509 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14510 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14511 delta = Z - Z_old;
14512 delta_bytes = Z_BYTE - Z_BYTE_old;
14514 /* Give up if PT is not in the window. Note that it already has
14515 been checked at the start of try_window_id that PT is not in
14516 front of the window start. */
14517 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14518 GIVE_UP (13);
14520 /* If window start is unchanged, we can reuse the whole matrix
14521 as is, after adjusting glyph positions. No need to compute
14522 the window end again, since its offset from Z hasn't changed. */
14523 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14524 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14525 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14526 /* PT must not be in a partially visible line. */
14527 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14528 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14530 /* Adjust positions in the glyph matrix. */
14531 if (delta || delta_bytes)
14533 struct glyph_row *r1
14534 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14535 increment_matrix_positions (w->current_matrix,
14536 MATRIX_ROW_VPOS (r0, current_matrix),
14537 MATRIX_ROW_VPOS (r1, current_matrix),
14538 delta, delta_bytes);
14541 /* Set the cursor. */
14542 row = row_containing_pos (w, PT, r0, NULL, 0);
14543 if (row)
14544 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14545 else
14546 abort ();
14547 return 1;
14551 /* Handle the case that changes are all below what is displayed in
14552 the window, and that PT is in the window. This shortcut cannot
14553 be taken if ZV is visible in the window, and text has been added
14554 there that is visible in the window. */
14555 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14556 /* ZV is not visible in the window, or there are no
14557 changes at ZV, actually. */
14558 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14559 || first_changed_charpos == last_changed_charpos))
14561 struct glyph_row *r0;
14563 /* Give up if PT is not in the window. Note that it already has
14564 been checked at the start of try_window_id that PT is not in
14565 front of the window start. */
14566 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14567 GIVE_UP (14);
14569 /* If window start is unchanged, we can reuse the whole matrix
14570 as is, without changing glyph positions since no text has
14571 been added/removed in front of the window end. */
14572 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14573 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14574 /* PT must not be in a partially visible line. */
14575 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14576 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14578 /* We have to compute the window end anew since text
14579 can have been added/removed after it. */
14580 w->window_end_pos
14581 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14582 w->window_end_bytepos
14583 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14585 /* Set the cursor. */
14586 row = row_containing_pos (w, PT, r0, NULL, 0);
14587 if (row)
14588 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14589 else
14590 abort ();
14591 return 2;
14595 /* Give up if window start is in the changed area.
14597 The condition used to read
14599 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14601 but why that was tested escapes me at the moment. */
14602 if (CHARPOS (start) >= first_changed_charpos
14603 && CHARPOS (start) <= last_changed_charpos)
14604 GIVE_UP (15);
14606 /* Check that window start agrees with the start of the first glyph
14607 row in its current matrix. Check this after we know the window
14608 start is not in changed text, otherwise positions would not be
14609 comparable. */
14610 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14611 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14612 GIVE_UP (16);
14614 /* Give up if the window ends in strings. Overlay strings
14615 at the end are difficult to handle, so don't try. */
14616 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14617 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14618 GIVE_UP (20);
14620 /* Compute the position at which we have to start displaying new
14621 lines. Some of the lines at the top of the window might be
14622 reusable because they are not displaying changed text. Find the
14623 last row in W's current matrix not affected by changes at the
14624 start of current_buffer. Value is null if changes start in the
14625 first line of window. */
14626 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14627 if (last_unchanged_at_beg_row)
14629 /* Avoid starting to display in the moddle of a character, a TAB
14630 for instance. This is easier than to set up the iterator
14631 exactly, and it's not a frequent case, so the additional
14632 effort wouldn't really pay off. */
14633 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14634 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14635 && last_unchanged_at_beg_row > w->current_matrix->rows)
14636 --last_unchanged_at_beg_row;
14638 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14639 GIVE_UP (17);
14641 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14642 GIVE_UP (18);
14643 start_pos = it.current.pos;
14645 /* Start displaying new lines in the desired matrix at the same
14646 vpos we would use in the current matrix, i.e. below
14647 last_unchanged_at_beg_row. */
14648 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14649 current_matrix);
14650 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14651 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14653 xassert (it.hpos == 0 && it.current_x == 0);
14655 else
14657 /* There are no reusable lines at the start of the window.
14658 Start displaying in the first text line. */
14659 start_display (&it, w, start);
14660 it.vpos = it.first_vpos;
14661 start_pos = it.current.pos;
14664 /* Find the first row that is not affected by changes at the end of
14665 the buffer. Value will be null if there is no unchanged row, in
14666 which case we must redisplay to the end of the window. delta
14667 will be set to the value by which buffer positions beginning with
14668 first_unchanged_at_end_row have to be adjusted due to text
14669 changes. */
14670 first_unchanged_at_end_row
14671 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14672 IF_DEBUG (debug_delta = delta);
14673 IF_DEBUG (debug_delta_bytes = delta_bytes);
14675 /* Set stop_pos to the buffer position up to which we will have to
14676 display new lines. If first_unchanged_at_end_row != NULL, this
14677 is the buffer position of the start of the line displayed in that
14678 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14679 that we don't stop at a buffer position. */
14680 stop_pos = 0;
14681 if (first_unchanged_at_end_row)
14683 xassert (last_unchanged_at_beg_row == NULL
14684 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14686 /* If this is a continuation line, move forward to the next one
14687 that isn't. Changes in lines above affect this line.
14688 Caution: this may move first_unchanged_at_end_row to a row
14689 not displaying text. */
14690 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14691 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14692 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14693 < it.last_visible_y))
14694 ++first_unchanged_at_end_row;
14696 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14697 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14698 >= it.last_visible_y))
14699 first_unchanged_at_end_row = NULL;
14700 else
14702 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14703 + delta);
14704 first_unchanged_at_end_vpos
14705 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14706 xassert (stop_pos >= Z - END_UNCHANGED);
14709 else if (last_unchanged_at_beg_row == NULL)
14710 GIVE_UP (19);
14713 #if GLYPH_DEBUG
14715 /* Either there is no unchanged row at the end, or the one we have
14716 now displays text. This is a necessary condition for the window
14717 end pos calculation at the end of this function. */
14718 xassert (first_unchanged_at_end_row == NULL
14719 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14721 debug_last_unchanged_at_beg_vpos
14722 = (last_unchanged_at_beg_row
14723 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14724 : -1);
14725 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14727 #endif /* GLYPH_DEBUG != 0 */
14730 /* Display new lines. Set last_text_row to the last new line
14731 displayed which has text on it, i.e. might end up as being the
14732 line where the window_end_vpos is. */
14733 w->cursor.vpos = -1;
14734 last_text_row = NULL;
14735 overlay_arrow_seen = 0;
14736 while (it.current_y < it.last_visible_y
14737 && !fonts_changed_p
14738 && (first_unchanged_at_end_row == NULL
14739 || IT_CHARPOS (it) < stop_pos))
14741 if (display_line (&it))
14742 last_text_row = it.glyph_row - 1;
14745 if (fonts_changed_p)
14746 return -1;
14749 /* Compute differences in buffer positions, y-positions etc. for
14750 lines reused at the bottom of the window. Compute what we can
14751 scroll. */
14752 if (first_unchanged_at_end_row
14753 /* No lines reused because we displayed everything up to the
14754 bottom of the window. */
14755 && it.current_y < it.last_visible_y)
14757 dvpos = (it.vpos
14758 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14759 current_matrix));
14760 dy = it.current_y - first_unchanged_at_end_row->y;
14761 run.current_y = first_unchanged_at_end_row->y;
14762 run.desired_y = run.current_y + dy;
14763 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14765 else
14767 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14768 first_unchanged_at_end_row = NULL;
14770 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14773 /* Find the cursor if not already found. We have to decide whether
14774 PT will appear on this window (it sometimes doesn't, but this is
14775 not a very frequent case.) This decision has to be made before
14776 the current matrix is altered. A value of cursor.vpos < 0 means
14777 that PT is either in one of the lines beginning at
14778 first_unchanged_at_end_row or below the window. Don't care for
14779 lines that might be displayed later at the window end; as
14780 mentioned, this is not a frequent case. */
14781 if (w->cursor.vpos < 0)
14783 /* Cursor in unchanged rows at the top? */
14784 if (PT < CHARPOS (start_pos)
14785 && last_unchanged_at_beg_row)
14787 row = row_containing_pos (w, PT,
14788 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14789 last_unchanged_at_beg_row + 1, 0);
14790 if (row)
14791 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14794 /* Start from first_unchanged_at_end_row looking for PT. */
14795 else if (first_unchanged_at_end_row)
14797 row = row_containing_pos (w, PT - delta,
14798 first_unchanged_at_end_row, NULL, 0);
14799 if (row)
14800 set_cursor_from_row (w, row, w->current_matrix, delta,
14801 delta_bytes, dy, dvpos);
14804 /* Give up if cursor was not found. */
14805 if (w->cursor.vpos < 0)
14807 clear_glyph_matrix (w->desired_matrix);
14808 return -1;
14812 /* Don't let the cursor end in the scroll margins. */
14814 int this_scroll_margin, cursor_height;
14816 this_scroll_margin = max (0, scroll_margin);
14817 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14818 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14819 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14821 if ((w->cursor.y < this_scroll_margin
14822 && CHARPOS (start) > BEGV)
14823 /* Old redisplay didn't take scroll margin into account at the bottom,
14824 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14825 || (w->cursor.y + (make_cursor_line_fully_visible_p
14826 ? cursor_height + this_scroll_margin
14827 : 1)) > it.last_visible_y)
14829 w->cursor.vpos = -1;
14830 clear_glyph_matrix (w->desired_matrix);
14831 return -1;
14835 /* Scroll the display. Do it before changing the current matrix so
14836 that xterm.c doesn't get confused about where the cursor glyph is
14837 found. */
14838 if (dy && run.height)
14840 update_begin (f);
14842 if (FRAME_WINDOW_P (f))
14844 rif->update_window_begin_hook (w);
14845 rif->clear_window_mouse_face (w);
14846 rif->scroll_run_hook (w, &run);
14847 rif->update_window_end_hook (w, 0, 0);
14849 else
14851 /* Terminal frame. In this case, dvpos gives the number of
14852 lines to scroll by; dvpos < 0 means scroll up. */
14853 int first_unchanged_at_end_vpos
14854 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14855 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14856 int end = (WINDOW_TOP_EDGE_LINE (w)
14857 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14858 + window_internal_height (w));
14860 /* Perform the operation on the screen. */
14861 if (dvpos > 0)
14863 /* Scroll last_unchanged_at_beg_row to the end of the
14864 window down dvpos lines. */
14865 set_terminal_window (end);
14867 /* On dumb terminals delete dvpos lines at the end
14868 before inserting dvpos empty lines. */
14869 if (!scroll_region_ok)
14870 ins_del_lines (end - dvpos, -dvpos);
14872 /* Insert dvpos empty lines in front of
14873 last_unchanged_at_beg_row. */
14874 ins_del_lines (from, dvpos);
14876 else if (dvpos < 0)
14878 /* Scroll up last_unchanged_at_beg_vpos to the end of
14879 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14880 set_terminal_window (end);
14882 /* Delete dvpos lines in front of
14883 last_unchanged_at_beg_vpos. ins_del_lines will set
14884 the cursor to the given vpos and emit |dvpos| delete
14885 line sequences. */
14886 ins_del_lines (from + dvpos, dvpos);
14888 /* On a dumb terminal insert dvpos empty lines at the
14889 end. */
14890 if (!scroll_region_ok)
14891 ins_del_lines (end + dvpos, -dvpos);
14894 set_terminal_window (0);
14897 update_end (f);
14900 /* Shift reused rows of the current matrix to the right position.
14901 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14902 text. */
14903 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14904 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14905 if (dvpos < 0)
14907 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14908 bottom_vpos, dvpos);
14909 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14910 bottom_vpos, 0);
14912 else if (dvpos > 0)
14914 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14915 bottom_vpos, dvpos);
14916 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14917 first_unchanged_at_end_vpos + dvpos, 0);
14920 /* For frame-based redisplay, make sure that current frame and window
14921 matrix are in sync with respect to glyph memory. */
14922 if (!FRAME_WINDOW_P (f))
14923 sync_frame_with_window_matrix_rows (w);
14925 /* Adjust buffer positions in reused rows. */
14926 if (delta || delta_bytes)
14927 increment_matrix_positions (current_matrix,
14928 first_unchanged_at_end_vpos + dvpos,
14929 bottom_vpos, delta, delta_bytes);
14931 /* Adjust Y positions. */
14932 if (dy)
14933 shift_glyph_matrix (w, current_matrix,
14934 first_unchanged_at_end_vpos + dvpos,
14935 bottom_vpos, dy);
14937 if (first_unchanged_at_end_row)
14939 first_unchanged_at_end_row += dvpos;
14940 if (first_unchanged_at_end_row->y >= it.last_visible_y
14941 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14942 first_unchanged_at_end_row = NULL;
14945 /* If scrolling up, there may be some lines to display at the end of
14946 the window. */
14947 last_text_row_at_end = NULL;
14948 if (dy < 0)
14950 /* Scrolling up can leave for example a partially visible line
14951 at the end of the window to be redisplayed. */
14952 /* Set last_row to the glyph row in the current matrix where the
14953 window end line is found. It has been moved up or down in
14954 the matrix by dvpos. */
14955 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14956 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14958 /* If last_row is the window end line, it should display text. */
14959 xassert (last_row->displays_text_p);
14961 /* If window end line was partially visible before, begin
14962 displaying at that line. Otherwise begin displaying with the
14963 line following it. */
14964 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14966 init_to_row_start (&it, w, last_row);
14967 it.vpos = last_vpos;
14968 it.current_y = last_row->y;
14970 else
14972 init_to_row_end (&it, w, last_row);
14973 it.vpos = 1 + last_vpos;
14974 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14975 ++last_row;
14978 /* We may start in a continuation line. If so, we have to
14979 get the right continuation_lines_width and current_x. */
14980 it.continuation_lines_width = last_row->continuation_lines_width;
14981 it.hpos = it.current_x = 0;
14983 /* Display the rest of the lines at the window end. */
14984 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14985 while (it.current_y < it.last_visible_y
14986 && !fonts_changed_p)
14988 /* Is it always sure that the display agrees with lines in
14989 the current matrix? I don't think so, so we mark rows
14990 displayed invalid in the current matrix by setting their
14991 enabled_p flag to zero. */
14992 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14993 if (display_line (&it))
14994 last_text_row_at_end = it.glyph_row - 1;
14998 /* Update window_end_pos and window_end_vpos. */
14999 if (first_unchanged_at_end_row
15000 && !last_text_row_at_end)
15002 /* Window end line if one of the preserved rows from the current
15003 matrix. Set row to the last row displaying text in current
15004 matrix starting at first_unchanged_at_end_row, after
15005 scrolling. */
15006 xassert (first_unchanged_at_end_row->displays_text_p);
15007 row = find_last_row_displaying_text (w->current_matrix, &it,
15008 first_unchanged_at_end_row);
15009 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15011 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15012 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15013 w->window_end_vpos
15014 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15015 xassert (w->window_end_bytepos >= 0);
15016 IF_DEBUG (debug_method_add (w, "A"));
15018 else if (last_text_row_at_end)
15020 w->window_end_pos
15021 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15022 w->window_end_bytepos
15023 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15024 w->window_end_vpos
15025 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15026 xassert (w->window_end_bytepos >= 0);
15027 IF_DEBUG (debug_method_add (w, "B"));
15029 else if (last_text_row)
15031 /* We have displayed either to the end of the window or at the
15032 end of the window, i.e. the last row with text is to be found
15033 in the desired matrix. */
15034 w->window_end_pos
15035 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15036 w->window_end_bytepos
15037 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15038 w->window_end_vpos
15039 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15040 xassert (w->window_end_bytepos >= 0);
15042 else if (first_unchanged_at_end_row == NULL
15043 && last_text_row == NULL
15044 && last_text_row_at_end == NULL)
15046 /* Displayed to end of window, but no line containing text was
15047 displayed. Lines were deleted at the end of the window. */
15048 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15049 int vpos = XFASTINT (w->window_end_vpos);
15050 struct glyph_row *current_row = current_matrix->rows + vpos;
15051 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15053 for (row = NULL;
15054 row == NULL && vpos >= first_vpos;
15055 --vpos, --current_row, --desired_row)
15057 if (desired_row->enabled_p)
15059 if (desired_row->displays_text_p)
15060 row = desired_row;
15062 else if (current_row->displays_text_p)
15063 row = current_row;
15066 xassert (row != NULL);
15067 w->window_end_vpos = make_number (vpos + 1);
15068 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15069 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15070 xassert (w->window_end_bytepos >= 0);
15071 IF_DEBUG (debug_method_add (w, "C"));
15073 else
15074 abort ();
15076 #if 0 /* This leads to problems, for instance when the cursor is
15077 at ZV, and the cursor line displays no text. */
15078 /* Disable rows below what's displayed in the window. This makes
15079 debugging easier. */
15080 enable_glyph_matrix_rows (current_matrix,
15081 XFASTINT (w->window_end_vpos) + 1,
15082 bottom_vpos, 0);
15083 #endif
15085 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15086 debug_end_vpos = XFASTINT (w->window_end_vpos));
15088 /* Record that display has not been completed. */
15089 w->window_end_valid = Qnil;
15090 w->desired_matrix->no_scrolling_p = 1;
15091 return 3;
15093 #undef GIVE_UP
15098 /***********************************************************************
15099 More debugging support
15100 ***********************************************************************/
15102 #if GLYPH_DEBUG
15104 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15105 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15106 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15109 /* Dump the contents of glyph matrix MATRIX on stderr.
15111 GLYPHS 0 means don't show glyph contents.
15112 GLYPHS 1 means show glyphs in short form
15113 GLYPHS > 1 means show glyphs in long form. */
15115 void
15116 dump_glyph_matrix (matrix, glyphs)
15117 struct glyph_matrix *matrix;
15118 int glyphs;
15120 int i;
15121 for (i = 0; i < matrix->nrows; ++i)
15122 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15126 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15127 the glyph row and area where the glyph comes from. */
15129 void
15130 dump_glyph (row, glyph, area)
15131 struct glyph_row *row;
15132 struct glyph *glyph;
15133 int area;
15135 if (glyph->type == CHAR_GLYPH)
15137 fprintf (stderr,
15138 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15139 glyph - row->glyphs[TEXT_AREA],
15140 'C',
15141 glyph->charpos,
15142 (BUFFERP (glyph->object)
15143 ? 'B'
15144 : (STRINGP (glyph->object)
15145 ? 'S'
15146 : '-')),
15147 glyph->pixel_width,
15148 glyph->u.ch,
15149 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15150 ? glyph->u.ch
15151 : '.'),
15152 glyph->face_id,
15153 glyph->left_box_line_p,
15154 glyph->right_box_line_p);
15156 else if (glyph->type == STRETCH_GLYPH)
15158 fprintf (stderr,
15159 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15160 glyph - row->glyphs[TEXT_AREA],
15161 'S',
15162 glyph->charpos,
15163 (BUFFERP (glyph->object)
15164 ? 'B'
15165 : (STRINGP (glyph->object)
15166 ? 'S'
15167 : '-')),
15168 glyph->pixel_width,
15170 '.',
15171 glyph->face_id,
15172 glyph->left_box_line_p,
15173 glyph->right_box_line_p);
15175 else if (glyph->type == IMAGE_GLYPH)
15177 fprintf (stderr,
15178 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15179 glyph - row->glyphs[TEXT_AREA],
15180 'I',
15181 glyph->charpos,
15182 (BUFFERP (glyph->object)
15183 ? 'B'
15184 : (STRINGP (glyph->object)
15185 ? 'S'
15186 : '-')),
15187 glyph->pixel_width,
15188 glyph->u.img_id,
15189 '.',
15190 glyph->face_id,
15191 glyph->left_box_line_p,
15192 glyph->right_box_line_p);
15194 else if (glyph->type == COMPOSITE_GLYPH)
15196 fprintf (stderr,
15197 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15198 glyph - row->glyphs[TEXT_AREA],
15199 '+',
15200 glyph->charpos,
15201 (BUFFERP (glyph->object)
15202 ? 'B'
15203 : (STRINGP (glyph->object)
15204 ? 'S'
15205 : '-')),
15206 glyph->pixel_width,
15207 glyph->u.cmp_id,
15208 '.',
15209 glyph->face_id,
15210 glyph->left_box_line_p,
15211 glyph->right_box_line_p);
15216 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15217 GLYPHS 0 means don't show glyph contents.
15218 GLYPHS 1 means show glyphs in short form
15219 GLYPHS > 1 means show glyphs in long form. */
15221 void
15222 dump_glyph_row (row, vpos, glyphs)
15223 struct glyph_row *row;
15224 int vpos, glyphs;
15226 if (glyphs != 1)
15228 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15229 fprintf (stderr, "======================================================================\n");
15231 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15232 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15233 vpos,
15234 MATRIX_ROW_START_CHARPOS (row),
15235 MATRIX_ROW_END_CHARPOS (row),
15236 row->used[TEXT_AREA],
15237 row->contains_overlapping_glyphs_p,
15238 row->enabled_p,
15239 row->truncated_on_left_p,
15240 row->truncated_on_right_p,
15241 row->continued_p,
15242 MATRIX_ROW_CONTINUATION_LINE_P (row),
15243 row->displays_text_p,
15244 row->ends_at_zv_p,
15245 row->fill_line_p,
15246 row->ends_in_middle_of_char_p,
15247 row->starts_in_middle_of_char_p,
15248 row->mouse_face_p,
15249 row->x,
15250 row->y,
15251 row->pixel_width,
15252 row->height,
15253 row->visible_height,
15254 row->ascent,
15255 row->phys_ascent);
15256 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15257 row->end.overlay_string_index,
15258 row->continuation_lines_width);
15259 fprintf (stderr, "%9d %5d\n",
15260 CHARPOS (row->start.string_pos),
15261 CHARPOS (row->end.string_pos));
15262 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15263 row->end.dpvec_index);
15266 if (glyphs > 1)
15268 int area;
15270 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15272 struct glyph *glyph = row->glyphs[area];
15273 struct glyph *glyph_end = glyph + row->used[area];
15275 /* Glyph for a line end in text. */
15276 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15277 ++glyph_end;
15279 if (glyph < glyph_end)
15280 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15282 for (; glyph < glyph_end; ++glyph)
15283 dump_glyph (row, glyph, area);
15286 else if (glyphs == 1)
15288 int area;
15290 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15292 char *s = (char *) alloca (row->used[area] + 1);
15293 int i;
15295 for (i = 0; i < row->used[area]; ++i)
15297 struct glyph *glyph = row->glyphs[area] + i;
15298 if (glyph->type == CHAR_GLYPH
15299 && glyph->u.ch < 0x80
15300 && glyph->u.ch >= ' ')
15301 s[i] = glyph->u.ch;
15302 else
15303 s[i] = '.';
15306 s[i] = '\0';
15307 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15313 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15314 Sdump_glyph_matrix, 0, 1, "p",
15315 doc: /* Dump the current matrix of the selected window to stderr.
15316 Shows contents of glyph row structures. With non-nil
15317 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15318 glyphs in short form, otherwise show glyphs in long form. */)
15319 (glyphs)
15320 Lisp_Object glyphs;
15322 struct window *w = XWINDOW (selected_window);
15323 struct buffer *buffer = XBUFFER (w->buffer);
15325 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15326 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15327 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15328 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15329 fprintf (stderr, "=============================================\n");
15330 dump_glyph_matrix (w->current_matrix,
15331 NILP (glyphs) ? 0 : XINT (glyphs));
15332 return Qnil;
15336 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15337 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15340 struct frame *f = XFRAME (selected_frame);
15341 dump_glyph_matrix (f->current_matrix, 1);
15342 return Qnil;
15346 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15347 doc: /* Dump glyph row ROW to stderr.
15348 GLYPH 0 means don't dump glyphs.
15349 GLYPH 1 means dump glyphs in short form.
15350 GLYPH > 1 or omitted means dump glyphs in long form. */)
15351 (row, glyphs)
15352 Lisp_Object row, glyphs;
15354 struct glyph_matrix *matrix;
15355 int vpos;
15357 CHECK_NUMBER (row);
15358 matrix = XWINDOW (selected_window)->current_matrix;
15359 vpos = XINT (row);
15360 if (vpos >= 0 && vpos < matrix->nrows)
15361 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15362 vpos,
15363 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15364 return Qnil;
15368 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15369 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15370 GLYPH 0 means don't dump glyphs.
15371 GLYPH 1 means dump glyphs in short form.
15372 GLYPH > 1 or omitted means dump glyphs in long form. */)
15373 (row, glyphs)
15374 Lisp_Object row, glyphs;
15376 struct frame *sf = SELECTED_FRAME ();
15377 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15378 int vpos;
15380 CHECK_NUMBER (row);
15381 vpos = XINT (row);
15382 if (vpos >= 0 && vpos < m->nrows)
15383 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15384 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15385 return Qnil;
15389 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15390 doc: /* Toggle tracing of redisplay.
15391 With ARG, turn tracing on if and only if ARG is positive. */)
15392 (arg)
15393 Lisp_Object arg;
15395 if (NILP (arg))
15396 trace_redisplay_p = !trace_redisplay_p;
15397 else
15399 arg = Fprefix_numeric_value (arg);
15400 trace_redisplay_p = XINT (arg) > 0;
15403 return Qnil;
15407 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15408 doc: /* Like `format', but print result to stderr.
15409 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15410 (nargs, args)
15411 int nargs;
15412 Lisp_Object *args;
15414 Lisp_Object s = Fformat (nargs, args);
15415 fprintf (stderr, "%s", SDATA (s));
15416 return Qnil;
15419 #endif /* GLYPH_DEBUG */
15423 /***********************************************************************
15424 Building Desired Matrix Rows
15425 ***********************************************************************/
15427 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15428 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15430 static struct glyph_row *
15431 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15432 struct window *w;
15433 Lisp_Object overlay_arrow_string;
15435 struct frame *f = XFRAME (WINDOW_FRAME (w));
15436 struct buffer *buffer = XBUFFER (w->buffer);
15437 struct buffer *old = current_buffer;
15438 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15439 int arrow_len = SCHARS (overlay_arrow_string);
15440 const unsigned char *arrow_end = arrow_string + arrow_len;
15441 const unsigned char *p;
15442 struct it it;
15443 int multibyte_p;
15444 int n_glyphs_before;
15446 set_buffer_temp (buffer);
15447 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15448 it.glyph_row->used[TEXT_AREA] = 0;
15449 SET_TEXT_POS (it.position, 0, 0);
15451 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15452 p = arrow_string;
15453 while (p < arrow_end)
15455 Lisp_Object face, ilisp;
15457 /* Get the next character. */
15458 if (multibyte_p)
15459 it.c = string_char_and_length (p, arrow_len, &it.len);
15460 else
15461 it.c = *p, it.len = 1;
15462 p += it.len;
15464 /* Get its face. */
15465 ilisp = make_number (p - arrow_string);
15466 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15467 it.face_id = compute_char_face (f, it.c, face);
15469 /* Compute its width, get its glyphs. */
15470 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15471 SET_TEXT_POS (it.position, -1, -1);
15472 PRODUCE_GLYPHS (&it);
15474 /* If this character doesn't fit any more in the line, we have
15475 to remove some glyphs. */
15476 if (it.current_x > it.last_visible_x)
15478 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15479 break;
15483 set_buffer_temp (old);
15484 return it.glyph_row;
15488 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15489 glyphs are only inserted for terminal frames since we can't really
15490 win with truncation glyphs when partially visible glyphs are
15491 involved. Which glyphs to insert is determined by
15492 produce_special_glyphs. */
15494 static void
15495 insert_left_trunc_glyphs (it)
15496 struct it *it;
15498 struct it truncate_it;
15499 struct glyph *from, *end, *to, *toend;
15501 xassert (!FRAME_WINDOW_P (it->f));
15503 /* Get the truncation glyphs. */
15504 truncate_it = *it;
15505 truncate_it.current_x = 0;
15506 truncate_it.face_id = DEFAULT_FACE_ID;
15507 truncate_it.glyph_row = &scratch_glyph_row;
15508 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15509 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15510 truncate_it.object = make_number (0);
15511 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15513 /* Overwrite glyphs from IT with truncation glyphs. */
15514 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15515 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15516 to = it->glyph_row->glyphs[TEXT_AREA];
15517 toend = to + it->glyph_row->used[TEXT_AREA];
15519 while (from < end)
15520 *to++ = *from++;
15522 /* There may be padding glyphs left over. Overwrite them too. */
15523 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15525 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15526 while (from < end)
15527 *to++ = *from++;
15530 if (to > toend)
15531 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15535 /* Compute the pixel height and width of IT->glyph_row.
15537 Most of the time, ascent and height of a display line will be equal
15538 to the max_ascent and max_height values of the display iterator
15539 structure. This is not the case if
15541 1. We hit ZV without displaying anything. In this case, max_ascent
15542 and max_height will be zero.
15544 2. We have some glyphs that don't contribute to the line height.
15545 (The glyph row flag contributes_to_line_height_p is for future
15546 pixmap extensions).
15548 The first case is easily covered by using default values because in
15549 these cases, the line height does not really matter, except that it
15550 must not be zero. */
15552 static void
15553 compute_line_metrics (it)
15554 struct it *it;
15556 struct glyph_row *row = it->glyph_row;
15557 int area, i;
15559 if (FRAME_WINDOW_P (it->f))
15561 int i, min_y, max_y;
15563 /* The line may consist of one space only, that was added to
15564 place the cursor on it. If so, the row's height hasn't been
15565 computed yet. */
15566 if (row->height == 0)
15568 if (it->max_ascent + it->max_descent == 0)
15569 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15570 row->ascent = it->max_ascent;
15571 row->height = it->max_ascent + it->max_descent;
15572 row->phys_ascent = it->max_phys_ascent;
15573 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15574 row->extra_line_spacing = it->max_extra_line_spacing;
15577 /* Compute the width of this line. */
15578 row->pixel_width = row->x;
15579 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15580 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15582 xassert (row->pixel_width >= 0);
15583 xassert (row->ascent >= 0 && row->height > 0);
15585 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15586 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15588 /* If first line's physical ascent is larger than its logical
15589 ascent, use the physical ascent, and make the row taller.
15590 This makes accented characters fully visible. */
15591 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15592 && row->phys_ascent > row->ascent)
15594 row->height += row->phys_ascent - row->ascent;
15595 row->ascent = row->phys_ascent;
15598 /* Compute how much of the line is visible. */
15599 row->visible_height = row->height;
15601 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15602 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15604 if (row->y < min_y)
15605 row->visible_height -= min_y - row->y;
15606 if (row->y + row->height > max_y)
15607 row->visible_height -= row->y + row->height - max_y;
15609 else
15611 row->pixel_width = row->used[TEXT_AREA];
15612 if (row->continued_p)
15613 row->pixel_width -= it->continuation_pixel_width;
15614 else if (row->truncated_on_right_p)
15615 row->pixel_width -= it->truncation_pixel_width;
15616 row->ascent = row->phys_ascent = 0;
15617 row->height = row->phys_height = row->visible_height = 1;
15618 row->extra_line_spacing = 0;
15621 /* Compute a hash code for this row. */
15622 row->hash = 0;
15623 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15624 for (i = 0; i < row->used[area]; ++i)
15625 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15626 + row->glyphs[area][i].u.val
15627 + row->glyphs[area][i].face_id
15628 + row->glyphs[area][i].padding_p
15629 + (row->glyphs[area][i].type << 2));
15631 it->max_ascent = it->max_descent = 0;
15632 it->max_phys_ascent = it->max_phys_descent = 0;
15636 /* Append one space to the glyph row of iterator IT if doing a
15637 window-based redisplay. The space has the same face as
15638 IT->face_id. Value is non-zero if a space was added.
15640 This function is called to make sure that there is always one glyph
15641 at the end of a glyph row that the cursor can be set on under
15642 window-systems. (If there weren't such a glyph we would not know
15643 how wide and tall a box cursor should be displayed).
15645 At the same time this space let's a nicely handle clearing to the
15646 end of the line if the row ends in italic text. */
15648 static int
15649 append_space_for_newline (it, default_face_p)
15650 struct it *it;
15651 int default_face_p;
15653 if (FRAME_WINDOW_P (it->f))
15655 int n = it->glyph_row->used[TEXT_AREA];
15657 if (it->glyph_row->glyphs[TEXT_AREA] + n
15658 < it->glyph_row->glyphs[1 + TEXT_AREA])
15660 /* Save some values that must not be changed.
15661 Must save IT->c and IT->len because otherwise
15662 ITERATOR_AT_END_P wouldn't work anymore after
15663 append_space_for_newline has been called. */
15664 enum display_element_type saved_what = it->what;
15665 int saved_c = it->c, saved_len = it->len;
15666 int saved_x = it->current_x;
15667 int saved_face_id = it->face_id;
15668 struct text_pos saved_pos;
15669 Lisp_Object saved_object;
15670 struct face *face;
15672 saved_object = it->object;
15673 saved_pos = it->position;
15675 it->what = IT_CHARACTER;
15676 bzero (&it->position, sizeof it->position);
15677 it->object = make_number (0);
15678 it->c = ' ';
15679 it->len = 1;
15681 if (default_face_p)
15682 it->face_id = DEFAULT_FACE_ID;
15683 else if (it->face_before_selective_p)
15684 it->face_id = it->saved_face_id;
15685 face = FACE_FROM_ID (it->f, it->face_id);
15686 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15688 PRODUCE_GLYPHS (it);
15690 it->override_ascent = -1;
15691 it->constrain_row_ascent_descent_p = 0;
15692 it->current_x = saved_x;
15693 it->object = saved_object;
15694 it->position = saved_pos;
15695 it->what = saved_what;
15696 it->face_id = saved_face_id;
15697 it->len = saved_len;
15698 it->c = saved_c;
15699 return 1;
15703 return 0;
15707 /* Extend the face of the last glyph in the text area of IT->glyph_row
15708 to the end of the display line. Called from display_line.
15709 If the glyph row is empty, add a space glyph to it so that we
15710 know the face to draw. Set the glyph row flag fill_line_p. */
15712 static void
15713 extend_face_to_end_of_line (it)
15714 struct it *it;
15716 struct face *face;
15717 struct frame *f = it->f;
15719 /* If line is already filled, do nothing. */
15720 if (it->current_x >= it->last_visible_x)
15721 return;
15723 /* Face extension extends the background and box of IT->face_id
15724 to the end of the line. If the background equals the background
15725 of the frame, we don't have to do anything. */
15726 if (it->face_before_selective_p)
15727 face = FACE_FROM_ID (it->f, it->saved_face_id);
15728 else
15729 face = FACE_FROM_ID (f, it->face_id);
15731 if (FRAME_WINDOW_P (f)
15732 && it->glyph_row->displays_text_p
15733 && face->box == FACE_NO_BOX
15734 && face->background == FRAME_BACKGROUND_PIXEL (f)
15735 && !face->stipple)
15736 return;
15738 /* Set the glyph row flag indicating that the face of the last glyph
15739 in the text area has to be drawn to the end of the text area. */
15740 it->glyph_row->fill_line_p = 1;
15742 /* If current character of IT is not ASCII, make sure we have the
15743 ASCII face. This will be automatically undone the next time
15744 get_next_display_element returns a multibyte character. Note
15745 that the character will always be single byte in unibyte text. */
15746 if (!SINGLE_BYTE_CHAR_P (it->c))
15748 it->face_id = FACE_FOR_CHAR (f, face, 0);
15751 if (FRAME_WINDOW_P (f))
15753 /* If the row is empty, add a space with the current face of IT,
15754 so that we know which face to draw. */
15755 if (it->glyph_row->used[TEXT_AREA] == 0)
15757 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15758 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15759 it->glyph_row->used[TEXT_AREA] = 1;
15762 else
15764 /* Save some values that must not be changed. */
15765 int saved_x = it->current_x;
15766 struct text_pos saved_pos;
15767 Lisp_Object saved_object;
15768 enum display_element_type saved_what = it->what;
15769 int saved_face_id = it->face_id;
15771 saved_object = it->object;
15772 saved_pos = it->position;
15774 it->what = IT_CHARACTER;
15775 bzero (&it->position, sizeof it->position);
15776 it->object = make_number (0);
15777 it->c = ' ';
15778 it->len = 1;
15779 it->face_id = face->id;
15781 PRODUCE_GLYPHS (it);
15783 while (it->current_x <= it->last_visible_x)
15784 PRODUCE_GLYPHS (it);
15786 /* Don't count these blanks really. It would let us insert a left
15787 truncation glyph below and make us set the cursor on them, maybe. */
15788 it->current_x = saved_x;
15789 it->object = saved_object;
15790 it->position = saved_pos;
15791 it->what = saved_what;
15792 it->face_id = saved_face_id;
15797 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15798 trailing whitespace. */
15800 static int
15801 trailing_whitespace_p (charpos)
15802 int charpos;
15804 int bytepos = CHAR_TO_BYTE (charpos);
15805 int c = 0;
15807 while (bytepos < ZV_BYTE
15808 && (c = FETCH_CHAR (bytepos),
15809 c == ' ' || c == '\t'))
15810 ++bytepos;
15812 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15814 if (bytepos != PT_BYTE)
15815 return 1;
15817 return 0;
15821 /* Highlight trailing whitespace, if any, in ROW. */
15823 void
15824 highlight_trailing_whitespace (f, row)
15825 struct frame *f;
15826 struct glyph_row *row;
15828 int used = row->used[TEXT_AREA];
15830 if (used)
15832 struct glyph *start = row->glyphs[TEXT_AREA];
15833 struct glyph *glyph = start + used - 1;
15835 /* Skip over glyphs inserted to display the cursor at the
15836 end of a line, for extending the face of the last glyph
15837 to the end of the line on terminals, and for truncation
15838 and continuation glyphs. */
15839 while (glyph >= start
15840 && glyph->type == CHAR_GLYPH
15841 && INTEGERP (glyph->object))
15842 --glyph;
15844 /* If last glyph is a space or stretch, and it's trailing
15845 whitespace, set the face of all trailing whitespace glyphs in
15846 IT->glyph_row to `trailing-whitespace'. */
15847 if (glyph >= start
15848 && BUFFERP (glyph->object)
15849 && (glyph->type == STRETCH_GLYPH
15850 || (glyph->type == CHAR_GLYPH
15851 && glyph->u.ch == ' '))
15852 && trailing_whitespace_p (glyph->charpos))
15854 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15855 if (face_id < 0)
15856 return;
15858 while (glyph >= start
15859 && BUFFERP (glyph->object)
15860 && (glyph->type == STRETCH_GLYPH
15861 || (glyph->type == CHAR_GLYPH
15862 && glyph->u.ch == ' ')))
15863 (glyph--)->face_id = face_id;
15869 /* Value is non-zero if glyph row ROW in window W should be
15870 used to hold the cursor. */
15872 static int
15873 cursor_row_p (w, row)
15874 struct window *w;
15875 struct glyph_row *row;
15877 int cursor_row_p = 1;
15879 if (PT == MATRIX_ROW_END_CHARPOS (row))
15881 /* Suppose the row ends on a string.
15882 Unless the row is continued, that means it ends on a newline
15883 in the string. If it's anything other than a display string
15884 (e.g. a before-string from an overlay), we don't want the
15885 cursor there. (This heuristic seems to give the optimal
15886 behavior for the various types of multi-line strings.) */
15887 if (CHARPOS (row->end.string_pos) >= 0)
15889 if (row->continued_p)
15890 cursor_row_p = 1;
15891 else
15893 /* Check for `display' property. */
15894 struct glyph *beg = row->glyphs[TEXT_AREA];
15895 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15896 struct glyph *glyph;
15898 cursor_row_p = 0;
15899 for (glyph = end; glyph >= beg; --glyph)
15900 if (STRINGP (glyph->object))
15902 Lisp_Object prop
15903 = Fget_char_property (make_number (PT),
15904 Qdisplay, Qnil);
15905 cursor_row_p =
15906 (!NILP (prop)
15907 && display_prop_string_p (prop, glyph->object));
15908 break;
15912 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15914 /* If the row ends in middle of a real character,
15915 and the line is continued, we want the cursor here.
15916 That's because MATRIX_ROW_END_CHARPOS would equal
15917 PT if PT is before the character. */
15918 if (!row->ends_in_ellipsis_p)
15919 cursor_row_p = row->continued_p;
15920 else
15921 /* If the row ends in an ellipsis, then
15922 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15923 We want that position to be displayed after the ellipsis. */
15924 cursor_row_p = 0;
15926 /* If the row ends at ZV, display the cursor at the end of that
15927 row instead of at the start of the row below. */
15928 else if (row->ends_at_zv_p)
15929 cursor_row_p = 1;
15930 else
15931 cursor_row_p = 0;
15934 return cursor_row_p;
15938 /* Construct the glyph row IT->glyph_row in the desired matrix of
15939 IT->w from text at the current position of IT. See dispextern.h
15940 for an overview of struct it. Value is non-zero if
15941 IT->glyph_row displays text, as opposed to a line displaying ZV
15942 only. */
15944 static int
15945 display_line (it)
15946 struct it *it;
15948 struct glyph_row *row = it->glyph_row;
15949 Lisp_Object overlay_arrow_string;
15951 /* We always start displaying at hpos zero even if hscrolled. */
15952 xassert (it->hpos == 0 && it->current_x == 0);
15954 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15955 >= it->w->desired_matrix->nrows)
15957 it->w->nrows_scale_factor++;
15958 fonts_changed_p = 1;
15959 return 0;
15962 /* Is IT->w showing the region? */
15963 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15965 /* Clear the result glyph row and enable it. */
15966 prepare_desired_row (row);
15968 row->y = it->current_y;
15969 row->start = it->start;
15970 row->continuation_lines_width = it->continuation_lines_width;
15971 row->displays_text_p = 1;
15972 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15973 it->starts_in_middle_of_char_p = 0;
15975 /* Arrange the overlays nicely for our purposes. Usually, we call
15976 display_line on only one line at a time, in which case this
15977 can't really hurt too much, or we call it on lines which appear
15978 one after another in the buffer, in which case all calls to
15979 recenter_overlay_lists but the first will be pretty cheap. */
15980 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15982 /* Move over display elements that are not visible because we are
15983 hscrolled. This may stop at an x-position < IT->first_visible_x
15984 if the first glyph is partially visible or if we hit a line end. */
15985 if (it->current_x < it->first_visible_x)
15987 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15988 MOVE_TO_POS | MOVE_TO_X);
15991 /* Get the initial row height. This is either the height of the
15992 text hscrolled, if there is any, or zero. */
15993 row->ascent = it->max_ascent;
15994 row->height = it->max_ascent + it->max_descent;
15995 row->phys_ascent = it->max_phys_ascent;
15996 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15997 row->extra_line_spacing = it->max_extra_line_spacing;
15999 /* Loop generating characters. The loop is left with IT on the next
16000 character to display. */
16001 while (1)
16003 int n_glyphs_before, hpos_before, x_before;
16004 int x, i, nglyphs;
16005 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16007 /* Retrieve the next thing to display. Value is zero if end of
16008 buffer reached. */
16009 if (!get_next_display_element (it))
16011 /* Maybe add a space at the end of this line that is used to
16012 display the cursor there under X. Set the charpos of the
16013 first glyph of blank lines not corresponding to any text
16014 to -1. */
16015 #ifdef HAVE_WINDOW_SYSTEM
16016 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16017 row->exact_window_width_line_p = 1;
16018 else
16019 #endif /* HAVE_WINDOW_SYSTEM */
16020 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16021 || row->used[TEXT_AREA] == 0)
16023 row->glyphs[TEXT_AREA]->charpos = -1;
16024 row->displays_text_p = 0;
16026 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16027 && (!MINI_WINDOW_P (it->w)
16028 || (minibuf_level && EQ (it->window, minibuf_window))))
16029 row->indicate_empty_line_p = 1;
16032 it->continuation_lines_width = 0;
16033 row->ends_at_zv_p = 1;
16034 break;
16037 /* Now, get the metrics of what we want to display. This also
16038 generates glyphs in `row' (which is IT->glyph_row). */
16039 n_glyphs_before = row->used[TEXT_AREA];
16040 x = it->current_x;
16042 /* Remember the line height so far in case the next element doesn't
16043 fit on the line. */
16044 if (!it->truncate_lines_p)
16046 ascent = it->max_ascent;
16047 descent = it->max_descent;
16048 phys_ascent = it->max_phys_ascent;
16049 phys_descent = it->max_phys_descent;
16052 PRODUCE_GLYPHS (it);
16054 /* If this display element was in marginal areas, continue with
16055 the next one. */
16056 if (it->area != TEXT_AREA)
16058 row->ascent = max (row->ascent, it->max_ascent);
16059 row->height = max (row->height, it->max_ascent + it->max_descent);
16060 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16061 row->phys_height = max (row->phys_height,
16062 it->max_phys_ascent + it->max_phys_descent);
16063 row->extra_line_spacing = max (row->extra_line_spacing,
16064 it->max_extra_line_spacing);
16065 set_iterator_to_next (it, 1);
16066 continue;
16069 /* Does the display element fit on the line? If we truncate
16070 lines, we should draw past the right edge of the window. If
16071 we don't truncate, we want to stop so that we can display the
16072 continuation glyph before the right margin. If lines are
16073 continued, there are two possible strategies for characters
16074 resulting in more than 1 glyph (e.g. tabs): Display as many
16075 glyphs as possible in this line and leave the rest for the
16076 continuation line, or display the whole element in the next
16077 line. Original redisplay did the former, so we do it also. */
16078 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16079 hpos_before = it->hpos;
16080 x_before = x;
16082 if (/* Not a newline. */
16083 nglyphs > 0
16084 /* Glyphs produced fit entirely in the line. */
16085 && it->current_x < it->last_visible_x)
16087 it->hpos += nglyphs;
16088 row->ascent = max (row->ascent, it->max_ascent);
16089 row->height = max (row->height, it->max_ascent + it->max_descent);
16090 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16091 row->phys_height = max (row->phys_height,
16092 it->max_phys_ascent + it->max_phys_descent);
16093 row->extra_line_spacing = max (row->extra_line_spacing,
16094 it->max_extra_line_spacing);
16095 if (it->current_x - it->pixel_width < it->first_visible_x)
16096 row->x = x - it->first_visible_x;
16098 else
16100 int new_x;
16101 struct glyph *glyph;
16103 for (i = 0; i < nglyphs; ++i, x = new_x)
16105 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16106 new_x = x + glyph->pixel_width;
16108 if (/* Lines are continued. */
16109 !it->truncate_lines_p
16110 && (/* Glyph doesn't fit on the line. */
16111 new_x > it->last_visible_x
16112 /* Or it fits exactly on a window system frame. */
16113 || (new_x == it->last_visible_x
16114 && FRAME_WINDOW_P (it->f))))
16116 /* End of a continued line. */
16118 if (it->hpos == 0
16119 || (new_x == it->last_visible_x
16120 && FRAME_WINDOW_P (it->f)))
16122 /* Current glyph is the only one on the line or
16123 fits exactly on the line. We must continue
16124 the line because we can't draw the cursor
16125 after the glyph. */
16126 row->continued_p = 1;
16127 it->current_x = new_x;
16128 it->continuation_lines_width += new_x;
16129 ++it->hpos;
16130 if (i == nglyphs - 1)
16132 set_iterator_to_next (it, 1);
16133 #ifdef HAVE_WINDOW_SYSTEM
16134 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16136 if (!get_next_display_element (it))
16138 row->exact_window_width_line_p = 1;
16139 it->continuation_lines_width = 0;
16140 row->continued_p = 0;
16141 row->ends_at_zv_p = 1;
16143 else if (ITERATOR_AT_END_OF_LINE_P (it))
16145 row->continued_p = 0;
16146 row->exact_window_width_line_p = 1;
16149 #endif /* HAVE_WINDOW_SYSTEM */
16152 else if (CHAR_GLYPH_PADDING_P (*glyph)
16153 && !FRAME_WINDOW_P (it->f))
16155 /* A padding glyph that doesn't fit on this line.
16156 This means the whole character doesn't fit
16157 on the line. */
16158 row->used[TEXT_AREA] = n_glyphs_before;
16160 /* Fill the rest of the row with continuation
16161 glyphs like in 20.x. */
16162 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16163 < row->glyphs[1 + TEXT_AREA])
16164 produce_special_glyphs (it, IT_CONTINUATION);
16166 row->continued_p = 1;
16167 it->current_x = x_before;
16168 it->continuation_lines_width += x_before;
16170 /* Restore the height to what it was before the
16171 element not fitting on the line. */
16172 it->max_ascent = ascent;
16173 it->max_descent = descent;
16174 it->max_phys_ascent = phys_ascent;
16175 it->max_phys_descent = phys_descent;
16177 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16179 /* A TAB that extends past the right edge of the
16180 window. This produces a single glyph on
16181 window system frames. We leave the glyph in
16182 this row and let it fill the row, but don't
16183 consume the TAB. */
16184 it->continuation_lines_width += it->last_visible_x;
16185 row->ends_in_middle_of_char_p = 1;
16186 row->continued_p = 1;
16187 glyph->pixel_width = it->last_visible_x - x;
16188 it->starts_in_middle_of_char_p = 1;
16190 else
16192 /* Something other than a TAB that draws past
16193 the right edge of the window. Restore
16194 positions to values before the element. */
16195 row->used[TEXT_AREA] = n_glyphs_before + i;
16197 /* Display continuation glyphs. */
16198 if (!FRAME_WINDOW_P (it->f))
16199 produce_special_glyphs (it, IT_CONTINUATION);
16200 row->continued_p = 1;
16202 it->current_x = x_before;
16203 it->continuation_lines_width += x;
16204 extend_face_to_end_of_line (it);
16206 if (nglyphs > 1 && i > 0)
16208 row->ends_in_middle_of_char_p = 1;
16209 it->starts_in_middle_of_char_p = 1;
16212 /* Restore the height to what it was before the
16213 element not fitting on the line. */
16214 it->max_ascent = ascent;
16215 it->max_descent = descent;
16216 it->max_phys_ascent = phys_ascent;
16217 it->max_phys_descent = phys_descent;
16220 break;
16222 else if (new_x > it->first_visible_x)
16224 /* Increment number of glyphs actually displayed. */
16225 ++it->hpos;
16227 if (x < it->first_visible_x)
16228 /* Glyph is partially visible, i.e. row starts at
16229 negative X position. */
16230 row->x = x - it->first_visible_x;
16232 else
16234 /* Glyph is completely off the left margin of the
16235 window. This should not happen because of the
16236 move_it_in_display_line at the start of this
16237 function, unless the text display area of the
16238 window is empty. */
16239 xassert (it->first_visible_x <= it->last_visible_x);
16243 row->ascent = max (row->ascent, it->max_ascent);
16244 row->height = max (row->height, it->max_ascent + it->max_descent);
16245 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16246 row->phys_height = max (row->phys_height,
16247 it->max_phys_ascent + it->max_phys_descent);
16248 row->extra_line_spacing = max (row->extra_line_spacing,
16249 it->max_extra_line_spacing);
16251 /* End of this display line if row is continued. */
16252 if (row->continued_p || row->ends_at_zv_p)
16253 break;
16256 at_end_of_line:
16257 /* Is this a line end? If yes, we're also done, after making
16258 sure that a non-default face is extended up to the right
16259 margin of the window. */
16260 if (ITERATOR_AT_END_OF_LINE_P (it))
16262 int used_before = row->used[TEXT_AREA];
16264 row->ends_in_newline_from_string_p = STRINGP (it->object);
16266 #ifdef HAVE_WINDOW_SYSTEM
16267 /* Add a space at the end of the line that is used to
16268 display the cursor there. */
16269 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16270 append_space_for_newline (it, 0);
16271 #endif /* HAVE_WINDOW_SYSTEM */
16273 /* Extend the face to the end of the line. */
16274 extend_face_to_end_of_line (it);
16276 /* Make sure we have the position. */
16277 if (used_before == 0)
16278 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16280 /* Consume the line end. This skips over invisible lines. */
16281 set_iterator_to_next (it, 1);
16282 it->continuation_lines_width = 0;
16283 break;
16286 /* Proceed with next display element. Note that this skips
16287 over lines invisible because of selective display. */
16288 set_iterator_to_next (it, 1);
16290 /* If we truncate lines, we are done when the last displayed
16291 glyphs reach past the right margin of the window. */
16292 if (it->truncate_lines_p
16293 && (FRAME_WINDOW_P (it->f)
16294 ? (it->current_x >= it->last_visible_x)
16295 : (it->current_x > it->last_visible_x)))
16297 /* Maybe add truncation glyphs. */
16298 if (!FRAME_WINDOW_P (it->f))
16300 int i, n;
16302 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16303 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16304 break;
16306 for (n = row->used[TEXT_AREA]; i < n; ++i)
16308 row->used[TEXT_AREA] = i;
16309 produce_special_glyphs (it, IT_TRUNCATION);
16312 #ifdef HAVE_WINDOW_SYSTEM
16313 else
16315 /* Don't truncate if we can overflow newline into fringe. */
16316 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16318 if (!get_next_display_element (it))
16320 it->continuation_lines_width = 0;
16321 row->ends_at_zv_p = 1;
16322 row->exact_window_width_line_p = 1;
16323 break;
16325 if (ITERATOR_AT_END_OF_LINE_P (it))
16327 row->exact_window_width_line_p = 1;
16328 goto at_end_of_line;
16332 #endif /* HAVE_WINDOW_SYSTEM */
16334 row->truncated_on_right_p = 1;
16335 it->continuation_lines_width = 0;
16336 reseat_at_next_visible_line_start (it, 0);
16337 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16338 it->hpos = hpos_before;
16339 it->current_x = x_before;
16340 break;
16344 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16345 at the left window margin. */
16346 if (it->first_visible_x
16347 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16349 if (!FRAME_WINDOW_P (it->f))
16350 insert_left_trunc_glyphs (it);
16351 row->truncated_on_left_p = 1;
16354 /* If the start of this line is the overlay arrow-position, then
16355 mark this glyph row as the one containing the overlay arrow.
16356 This is clearly a mess with variable size fonts. It would be
16357 better to let it be displayed like cursors under X. */
16358 if ((row->displays_text_p || !overlay_arrow_seen)
16359 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16360 !NILP (overlay_arrow_string)))
16362 /* Overlay arrow in window redisplay is a fringe bitmap. */
16363 if (STRINGP (overlay_arrow_string))
16365 struct glyph_row *arrow_row
16366 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16367 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16368 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16369 struct glyph *p = row->glyphs[TEXT_AREA];
16370 struct glyph *p2, *end;
16372 /* Copy the arrow glyphs. */
16373 while (glyph < arrow_end)
16374 *p++ = *glyph++;
16376 /* Throw away padding glyphs. */
16377 p2 = p;
16378 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16379 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16380 ++p2;
16381 if (p2 > p)
16383 while (p2 < end)
16384 *p++ = *p2++;
16385 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16388 else
16390 xassert (INTEGERP (overlay_arrow_string));
16391 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16393 overlay_arrow_seen = 1;
16396 /* Compute pixel dimensions of this line. */
16397 compute_line_metrics (it);
16399 /* Remember the position at which this line ends. */
16400 row->end = it->current;
16402 /* Record whether this row ends inside an ellipsis. */
16403 row->ends_in_ellipsis_p
16404 = (it->method == GET_FROM_DISPLAY_VECTOR
16405 && it->ellipsis_p);
16407 /* Save fringe bitmaps in this row. */
16408 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16409 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16410 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16411 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16413 it->left_user_fringe_bitmap = 0;
16414 it->left_user_fringe_face_id = 0;
16415 it->right_user_fringe_bitmap = 0;
16416 it->right_user_fringe_face_id = 0;
16418 /* Maybe set the cursor. */
16419 if (it->w->cursor.vpos < 0
16420 && PT >= MATRIX_ROW_START_CHARPOS (row)
16421 && PT <= MATRIX_ROW_END_CHARPOS (row)
16422 && cursor_row_p (it->w, row))
16423 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16425 /* Highlight trailing whitespace. */
16426 if (!NILP (Vshow_trailing_whitespace))
16427 highlight_trailing_whitespace (it->f, it->glyph_row);
16429 /* Prepare for the next line. This line starts horizontally at (X
16430 HPOS) = (0 0). Vertical positions are incremented. As a
16431 convenience for the caller, IT->glyph_row is set to the next
16432 row to be used. */
16433 it->current_x = it->hpos = 0;
16434 it->current_y += row->height;
16435 ++it->vpos;
16436 ++it->glyph_row;
16437 it->start = it->current;
16438 return row->displays_text_p;
16443 /***********************************************************************
16444 Menu Bar
16445 ***********************************************************************/
16447 /* Redisplay the menu bar in the frame for window W.
16449 The menu bar of X frames that don't have X toolkit support is
16450 displayed in a special window W->frame->menu_bar_window.
16452 The menu bar of terminal frames is treated specially as far as
16453 glyph matrices are concerned. Menu bar lines are not part of
16454 windows, so the update is done directly on the frame matrix rows
16455 for the menu bar. */
16457 static void
16458 display_menu_bar (w)
16459 struct window *w;
16461 struct frame *f = XFRAME (WINDOW_FRAME (w));
16462 struct it it;
16463 Lisp_Object items;
16464 int i;
16466 /* Don't do all this for graphical frames. */
16467 #ifdef HAVE_NTGUI
16468 if (!NILP (Vwindow_system))
16469 return;
16470 #endif
16471 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16472 if (FRAME_X_P (f))
16473 return;
16474 #endif
16475 #ifdef MAC_OS
16476 if (FRAME_MAC_P (f))
16477 return;
16478 #endif
16480 #ifdef USE_X_TOOLKIT
16481 xassert (!FRAME_WINDOW_P (f));
16482 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16483 it.first_visible_x = 0;
16484 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16485 #else /* not USE_X_TOOLKIT */
16486 if (FRAME_WINDOW_P (f))
16488 /* Menu bar lines are displayed in the desired matrix of the
16489 dummy window menu_bar_window. */
16490 struct window *menu_w;
16491 xassert (WINDOWP (f->menu_bar_window));
16492 menu_w = XWINDOW (f->menu_bar_window);
16493 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16494 MENU_FACE_ID);
16495 it.first_visible_x = 0;
16496 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16498 else
16500 /* This is a TTY frame, i.e. character hpos/vpos are used as
16501 pixel x/y. */
16502 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16503 MENU_FACE_ID);
16504 it.first_visible_x = 0;
16505 it.last_visible_x = FRAME_COLS (f);
16507 #endif /* not USE_X_TOOLKIT */
16509 if (! mode_line_inverse_video)
16510 /* Force the menu-bar to be displayed in the default face. */
16511 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16513 /* Clear all rows of the menu bar. */
16514 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16516 struct glyph_row *row = it.glyph_row + i;
16517 clear_glyph_row (row);
16518 row->enabled_p = 1;
16519 row->full_width_p = 1;
16522 /* Display all items of the menu bar. */
16523 items = FRAME_MENU_BAR_ITEMS (it.f);
16524 for (i = 0; i < XVECTOR (items)->size; i += 4)
16526 Lisp_Object string;
16528 /* Stop at nil string. */
16529 string = AREF (items, i + 1);
16530 if (NILP (string))
16531 break;
16533 /* Remember where item was displayed. */
16534 AREF (items, i + 3) = make_number (it.hpos);
16536 /* Display the item, pad with one space. */
16537 if (it.current_x < it.last_visible_x)
16538 display_string (NULL, string, Qnil, 0, 0, &it,
16539 SCHARS (string) + 1, 0, 0, -1);
16542 /* Fill out the line with spaces. */
16543 if (it.current_x < it.last_visible_x)
16544 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16546 /* Compute the total height of the lines. */
16547 compute_line_metrics (&it);
16552 /***********************************************************************
16553 Mode Line
16554 ***********************************************************************/
16556 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16557 FORCE is non-zero, redisplay mode lines unconditionally.
16558 Otherwise, redisplay only mode lines that are garbaged. Value is
16559 the number of windows whose mode lines were redisplayed. */
16561 static int
16562 redisplay_mode_lines (window, force)
16563 Lisp_Object window;
16564 int force;
16566 int nwindows = 0;
16568 while (!NILP (window))
16570 struct window *w = XWINDOW (window);
16572 if (WINDOWP (w->hchild))
16573 nwindows += redisplay_mode_lines (w->hchild, force);
16574 else if (WINDOWP (w->vchild))
16575 nwindows += redisplay_mode_lines (w->vchild, force);
16576 else if (force
16577 || FRAME_GARBAGED_P (XFRAME (w->frame))
16578 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16580 struct text_pos lpoint;
16581 struct buffer *old = current_buffer;
16583 /* Set the window's buffer for the mode line display. */
16584 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16585 set_buffer_internal_1 (XBUFFER (w->buffer));
16587 /* Point refers normally to the selected window. For any
16588 other window, set up appropriate value. */
16589 if (!EQ (window, selected_window))
16591 struct text_pos pt;
16593 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16594 if (CHARPOS (pt) < BEGV)
16595 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16596 else if (CHARPOS (pt) > (ZV - 1))
16597 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16598 else
16599 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16602 /* Display mode lines. */
16603 clear_glyph_matrix (w->desired_matrix);
16604 if (display_mode_lines (w))
16606 ++nwindows;
16607 w->must_be_updated_p = 1;
16610 /* Restore old settings. */
16611 set_buffer_internal_1 (old);
16612 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16615 window = w->next;
16618 return nwindows;
16622 /* Display the mode and/or top line of window W. Value is the number
16623 of mode lines displayed. */
16625 static int
16626 display_mode_lines (w)
16627 struct window *w;
16629 Lisp_Object old_selected_window, old_selected_frame;
16630 int n = 0;
16632 old_selected_frame = selected_frame;
16633 selected_frame = w->frame;
16634 old_selected_window = selected_window;
16635 XSETWINDOW (selected_window, w);
16637 /* These will be set while the mode line specs are processed. */
16638 line_number_displayed = 0;
16639 w->column_number_displayed = Qnil;
16641 if (WINDOW_WANTS_MODELINE_P (w))
16643 struct window *sel_w = XWINDOW (old_selected_window);
16645 /* Select mode line face based on the real selected window. */
16646 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16647 current_buffer->mode_line_format);
16648 ++n;
16651 if (WINDOW_WANTS_HEADER_LINE_P (w))
16653 display_mode_line (w, HEADER_LINE_FACE_ID,
16654 current_buffer->header_line_format);
16655 ++n;
16658 selected_frame = old_selected_frame;
16659 selected_window = old_selected_window;
16660 return n;
16664 /* Display mode or top line of window W. FACE_ID specifies which line
16665 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16666 FORMAT is the mode line format to display. Value is the pixel
16667 height of the mode line displayed. */
16669 static int
16670 display_mode_line (w, face_id, format)
16671 struct window *w;
16672 enum face_id face_id;
16673 Lisp_Object format;
16675 struct it it;
16676 struct face *face;
16677 int count = SPECPDL_INDEX ();
16679 init_iterator (&it, w, -1, -1, NULL, face_id);
16680 /* Don't extend on a previously drawn mode-line.
16681 This may happen if called from pos_visible_p. */
16682 it.glyph_row->enabled_p = 0;
16683 prepare_desired_row (it.glyph_row);
16685 it.glyph_row->mode_line_p = 1;
16687 if (! mode_line_inverse_video)
16688 /* Force the mode-line to be displayed in the default face. */
16689 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16691 record_unwind_protect (unwind_format_mode_line,
16692 format_mode_line_unwind_data (NULL, 0));
16694 mode_line_target = MODE_LINE_DISPLAY;
16696 /* Temporarily make frame's keyboard the current kboard so that
16697 kboard-local variables in the mode_line_format will get the right
16698 values. */
16699 push_frame_kboard (it.f);
16700 record_unwind_save_match_data ();
16701 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16702 pop_frame_kboard ();
16704 unbind_to (count, Qnil);
16706 /* Fill up with spaces. */
16707 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16709 compute_line_metrics (&it);
16710 it.glyph_row->full_width_p = 1;
16711 it.glyph_row->continued_p = 0;
16712 it.glyph_row->truncated_on_left_p = 0;
16713 it.glyph_row->truncated_on_right_p = 0;
16715 /* Make a 3D mode-line have a shadow at its right end. */
16716 face = FACE_FROM_ID (it.f, face_id);
16717 extend_face_to_end_of_line (&it);
16718 if (face->box != FACE_NO_BOX)
16720 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16721 + it.glyph_row->used[TEXT_AREA] - 1);
16722 last->right_box_line_p = 1;
16725 return it.glyph_row->height;
16728 /* Move element ELT in LIST to the front of LIST.
16729 Return the updated list. */
16731 static Lisp_Object
16732 move_elt_to_front (elt, list)
16733 Lisp_Object elt, list;
16735 register Lisp_Object tail, prev;
16736 register Lisp_Object tem;
16738 tail = list;
16739 prev = Qnil;
16740 while (CONSP (tail))
16742 tem = XCAR (tail);
16744 if (EQ (elt, tem))
16746 /* Splice out the link TAIL. */
16747 if (NILP (prev))
16748 list = XCDR (tail);
16749 else
16750 Fsetcdr (prev, XCDR (tail));
16752 /* Now make it the first. */
16753 Fsetcdr (tail, list);
16754 return tail;
16756 else
16757 prev = tail;
16758 tail = XCDR (tail);
16759 QUIT;
16762 /* Not found--return unchanged LIST. */
16763 return list;
16766 /* Contribute ELT to the mode line for window IT->w. How it
16767 translates into text depends on its data type.
16769 IT describes the display environment in which we display, as usual.
16771 DEPTH is the depth in recursion. It is used to prevent
16772 infinite recursion here.
16774 FIELD_WIDTH is the number of characters the display of ELT should
16775 occupy in the mode line, and PRECISION is the maximum number of
16776 characters to display from ELT's representation. See
16777 display_string for details.
16779 Returns the hpos of the end of the text generated by ELT.
16781 PROPS is a property list to add to any string we encounter.
16783 If RISKY is nonzero, remove (disregard) any properties in any string
16784 we encounter, and ignore :eval and :propertize.
16786 The global variable `mode_line_target' determines whether the
16787 output is passed to `store_mode_line_noprop',
16788 `store_mode_line_string', or `display_string'. */
16790 static int
16791 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16792 struct it *it;
16793 int depth;
16794 int field_width, precision;
16795 Lisp_Object elt, props;
16796 int risky;
16798 int n = 0, field, prec;
16799 int literal = 0;
16801 tail_recurse:
16802 if (depth > 100)
16803 elt = build_string ("*too-deep*");
16805 depth++;
16807 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16809 case Lisp_String:
16811 /* A string: output it and check for %-constructs within it. */
16812 unsigned char c;
16813 int offset = 0;
16815 if (SCHARS (elt) > 0
16816 && (!NILP (props) || risky))
16818 Lisp_Object oprops, aelt;
16819 oprops = Ftext_properties_at (make_number (0), elt);
16821 /* If the starting string's properties are not what
16822 we want, translate the string. Also, if the string
16823 is risky, do that anyway. */
16825 if (NILP (Fequal (props, oprops)) || risky)
16827 /* If the starting string has properties,
16828 merge the specified ones onto the existing ones. */
16829 if (! NILP (oprops) && !risky)
16831 Lisp_Object tem;
16833 oprops = Fcopy_sequence (oprops);
16834 tem = props;
16835 while (CONSP (tem))
16837 oprops = Fplist_put (oprops, XCAR (tem),
16838 XCAR (XCDR (tem)));
16839 tem = XCDR (XCDR (tem));
16841 props = oprops;
16844 aelt = Fassoc (elt, mode_line_proptrans_alist);
16845 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16847 /* AELT is what we want. Move it to the front
16848 without consing. */
16849 elt = XCAR (aelt);
16850 mode_line_proptrans_alist
16851 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16853 else
16855 Lisp_Object tem;
16857 /* If AELT has the wrong props, it is useless.
16858 so get rid of it. */
16859 if (! NILP (aelt))
16860 mode_line_proptrans_alist
16861 = Fdelq (aelt, mode_line_proptrans_alist);
16863 elt = Fcopy_sequence (elt);
16864 Fset_text_properties (make_number (0), Flength (elt),
16865 props, elt);
16866 /* Add this item to mode_line_proptrans_alist. */
16867 mode_line_proptrans_alist
16868 = Fcons (Fcons (elt, props),
16869 mode_line_proptrans_alist);
16870 /* Truncate mode_line_proptrans_alist
16871 to at most 50 elements. */
16872 tem = Fnthcdr (make_number (50),
16873 mode_line_proptrans_alist);
16874 if (! NILP (tem))
16875 XSETCDR (tem, Qnil);
16880 offset = 0;
16882 if (literal)
16884 prec = precision - n;
16885 switch (mode_line_target)
16887 case MODE_LINE_NOPROP:
16888 case MODE_LINE_TITLE:
16889 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16890 break;
16891 case MODE_LINE_STRING:
16892 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16893 break;
16894 case MODE_LINE_DISPLAY:
16895 n += display_string (NULL, elt, Qnil, 0, 0, it,
16896 0, prec, 0, STRING_MULTIBYTE (elt));
16897 break;
16900 break;
16903 /* Handle the non-literal case. */
16905 while ((precision <= 0 || n < precision)
16906 && SREF (elt, offset) != 0
16907 && (mode_line_target != MODE_LINE_DISPLAY
16908 || it->current_x < it->last_visible_x))
16910 int last_offset = offset;
16912 /* Advance to end of string or next format specifier. */
16913 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16916 if (offset - 1 != last_offset)
16918 int nchars, nbytes;
16920 /* Output to end of string or up to '%'. Field width
16921 is length of string. Don't output more than
16922 PRECISION allows us. */
16923 offset--;
16925 prec = c_string_width (SDATA (elt) + last_offset,
16926 offset - last_offset, precision - n,
16927 &nchars, &nbytes);
16929 switch (mode_line_target)
16931 case MODE_LINE_NOPROP:
16932 case MODE_LINE_TITLE:
16933 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16934 break;
16935 case MODE_LINE_STRING:
16937 int bytepos = last_offset;
16938 int charpos = string_byte_to_char (elt, bytepos);
16939 int endpos = (precision <= 0
16940 ? string_byte_to_char (elt, offset)
16941 : charpos + nchars);
16943 n += store_mode_line_string (NULL,
16944 Fsubstring (elt, make_number (charpos),
16945 make_number (endpos)),
16946 0, 0, 0, Qnil);
16948 break;
16949 case MODE_LINE_DISPLAY:
16951 int bytepos = last_offset;
16952 int charpos = string_byte_to_char (elt, bytepos);
16954 if (precision <= 0)
16955 nchars = string_byte_to_char (elt, offset) - charpos;
16956 n += display_string (NULL, elt, Qnil, 0, charpos,
16957 it, 0, nchars, 0,
16958 STRING_MULTIBYTE (elt));
16960 break;
16963 else /* c == '%' */
16965 int percent_position = offset;
16967 /* Get the specified minimum width. Zero means
16968 don't pad. */
16969 field = 0;
16970 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16971 field = field * 10 + c - '0';
16973 /* Don't pad beyond the total padding allowed. */
16974 if (field_width - n > 0 && field > field_width - n)
16975 field = field_width - n;
16977 /* Note that either PRECISION <= 0 or N < PRECISION. */
16978 prec = precision - n;
16980 if (c == 'M')
16981 n += display_mode_element (it, depth, field, prec,
16982 Vglobal_mode_string, props,
16983 risky);
16984 else if (c != 0)
16986 int multibyte;
16987 int bytepos, charpos;
16988 unsigned char *spec;
16990 bytepos = percent_position;
16991 charpos = (STRING_MULTIBYTE (elt)
16992 ? string_byte_to_char (elt, bytepos)
16993 : bytepos);
16995 spec
16996 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16998 switch (mode_line_target)
17000 case MODE_LINE_NOPROP:
17001 case MODE_LINE_TITLE:
17002 n += store_mode_line_noprop (spec, field, prec);
17003 break;
17004 case MODE_LINE_STRING:
17006 int len = strlen (spec);
17007 Lisp_Object tem = make_string (spec, len);
17008 props = Ftext_properties_at (make_number (charpos), elt);
17009 /* Should only keep face property in props */
17010 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17012 break;
17013 case MODE_LINE_DISPLAY:
17015 int nglyphs_before, nwritten;
17017 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17018 nwritten = display_string (spec, Qnil, elt,
17019 charpos, 0, it,
17020 field, prec, 0,
17021 multibyte);
17023 /* Assign to the glyphs written above the
17024 string where the `%x' came from, position
17025 of the `%'. */
17026 if (nwritten > 0)
17028 struct glyph *glyph
17029 = (it->glyph_row->glyphs[TEXT_AREA]
17030 + nglyphs_before);
17031 int i;
17033 for (i = 0; i < nwritten; ++i)
17035 glyph[i].object = elt;
17036 glyph[i].charpos = charpos;
17039 n += nwritten;
17042 break;
17045 else /* c == 0 */
17046 break;
17050 break;
17052 case Lisp_Symbol:
17053 /* A symbol: process the value of the symbol recursively
17054 as if it appeared here directly. Avoid error if symbol void.
17055 Special case: if value of symbol is a string, output the string
17056 literally. */
17058 register Lisp_Object tem;
17060 /* If the variable is not marked as risky to set
17061 then its contents are risky to use. */
17062 if (NILP (Fget (elt, Qrisky_local_variable)))
17063 risky = 1;
17065 tem = Fboundp (elt);
17066 if (!NILP (tem))
17068 tem = Fsymbol_value (elt);
17069 /* If value is a string, output that string literally:
17070 don't check for % within it. */
17071 if (STRINGP (tem))
17072 literal = 1;
17074 if (!EQ (tem, elt))
17076 /* Give up right away for nil or t. */
17077 elt = tem;
17078 goto tail_recurse;
17082 break;
17084 case Lisp_Cons:
17086 register Lisp_Object car, tem;
17088 /* A cons cell: five distinct cases.
17089 If first element is :eval or :propertize, do something special.
17090 If first element is a string or a cons, process all the elements
17091 and effectively concatenate them.
17092 If first element is a negative number, truncate displaying cdr to
17093 at most that many characters. If positive, pad (with spaces)
17094 to at least that many characters.
17095 If first element is a symbol, process the cadr or caddr recursively
17096 according to whether the symbol's value is non-nil or nil. */
17097 car = XCAR (elt);
17098 if (EQ (car, QCeval))
17100 /* An element of the form (:eval FORM) means evaluate FORM
17101 and use the result as mode line elements. */
17103 if (risky)
17104 break;
17106 if (CONSP (XCDR (elt)))
17108 Lisp_Object spec;
17109 spec = safe_eval (XCAR (XCDR (elt)));
17110 n += display_mode_element (it, depth, field_width - n,
17111 precision - n, spec, props,
17112 risky);
17115 else if (EQ (car, QCpropertize))
17117 /* An element of the form (:propertize ELT PROPS...)
17118 means display ELT but applying properties PROPS. */
17120 if (risky)
17121 break;
17123 if (CONSP (XCDR (elt)))
17124 n += display_mode_element (it, depth, field_width - n,
17125 precision - n, XCAR (XCDR (elt)),
17126 XCDR (XCDR (elt)), risky);
17128 else if (SYMBOLP (car))
17130 tem = Fboundp (car);
17131 elt = XCDR (elt);
17132 if (!CONSP (elt))
17133 goto invalid;
17134 /* elt is now the cdr, and we know it is a cons cell.
17135 Use its car if CAR has a non-nil value. */
17136 if (!NILP (tem))
17138 tem = Fsymbol_value (car);
17139 if (!NILP (tem))
17141 elt = XCAR (elt);
17142 goto tail_recurse;
17145 /* Symbol's value is nil (or symbol is unbound)
17146 Get the cddr of the original list
17147 and if possible find the caddr and use that. */
17148 elt = XCDR (elt);
17149 if (NILP (elt))
17150 break;
17151 else if (!CONSP (elt))
17152 goto invalid;
17153 elt = XCAR (elt);
17154 goto tail_recurse;
17156 else if (INTEGERP (car))
17158 register int lim = XINT (car);
17159 elt = XCDR (elt);
17160 if (lim < 0)
17162 /* Negative int means reduce maximum width. */
17163 if (precision <= 0)
17164 precision = -lim;
17165 else
17166 precision = min (precision, -lim);
17168 else if (lim > 0)
17170 /* Padding specified. Don't let it be more than
17171 current maximum. */
17172 if (precision > 0)
17173 lim = min (precision, lim);
17175 /* If that's more padding than already wanted, queue it.
17176 But don't reduce padding already specified even if
17177 that is beyond the current truncation point. */
17178 field_width = max (lim, field_width);
17180 goto tail_recurse;
17182 else if (STRINGP (car) || CONSP (car))
17184 register int limit = 50;
17185 /* Limit is to protect against circular lists. */
17186 while (CONSP (elt)
17187 && --limit > 0
17188 && (precision <= 0 || n < precision))
17190 n += display_mode_element (it, depth,
17191 /* Do padding only after the last
17192 element in the list. */
17193 (! CONSP (XCDR (elt))
17194 ? field_width - n
17195 : 0),
17196 precision - n, XCAR (elt),
17197 props, risky);
17198 elt = XCDR (elt);
17202 break;
17204 default:
17205 invalid:
17206 elt = build_string ("*invalid*");
17207 goto tail_recurse;
17210 /* Pad to FIELD_WIDTH. */
17211 if (field_width > 0 && n < field_width)
17213 switch (mode_line_target)
17215 case MODE_LINE_NOPROP:
17216 case MODE_LINE_TITLE:
17217 n += store_mode_line_noprop ("", field_width - n, 0);
17218 break;
17219 case MODE_LINE_STRING:
17220 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17221 break;
17222 case MODE_LINE_DISPLAY:
17223 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17224 0, 0, 0);
17225 break;
17229 return n;
17232 /* Store a mode-line string element in mode_line_string_list.
17234 If STRING is non-null, display that C string. Otherwise, the Lisp
17235 string LISP_STRING is displayed.
17237 FIELD_WIDTH is the minimum number of output glyphs to produce.
17238 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17239 with spaces. FIELD_WIDTH <= 0 means don't pad.
17241 PRECISION is the maximum number of characters to output from
17242 STRING. PRECISION <= 0 means don't truncate the string.
17244 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17245 properties to the string.
17247 PROPS are the properties to add to the string.
17248 The mode_line_string_face face property is always added to the string.
17251 static int
17252 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17253 char *string;
17254 Lisp_Object lisp_string;
17255 int copy_string;
17256 int field_width;
17257 int precision;
17258 Lisp_Object props;
17260 int len;
17261 int n = 0;
17263 if (string != NULL)
17265 len = strlen (string);
17266 if (precision > 0 && len > precision)
17267 len = precision;
17268 lisp_string = make_string (string, len);
17269 if (NILP (props))
17270 props = mode_line_string_face_prop;
17271 else if (!NILP (mode_line_string_face))
17273 Lisp_Object face = Fplist_get (props, Qface);
17274 props = Fcopy_sequence (props);
17275 if (NILP (face))
17276 face = mode_line_string_face;
17277 else
17278 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17279 props = Fplist_put (props, Qface, face);
17281 Fadd_text_properties (make_number (0), make_number (len),
17282 props, lisp_string);
17284 else
17286 len = XFASTINT (Flength (lisp_string));
17287 if (precision > 0 && len > precision)
17289 len = precision;
17290 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17291 precision = -1;
17293 if (!NILP (mode_line_string_face))
17295 Lisp_Object face;
17296 if (NILP (props))
17297 props = Ftext_properties_at (make_number (0), lisp_string);
17298 face = Fplist_get (props, Qface);
17299 if (NILP (face))
17300 face = mode_line_string_face;
17301 else
17302 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17303 props = Fcons (Qface, Fcons (face, Qnil));
17304 if (copy_string)
17305 lisp_string = Fcopy_sequence (lisp_string);
17307 if (!NILP (props))
17308 Fadd_text_properties (make_number (0), make_number (len),
17309 props, lisp_string);
17312 if (len > 0)
17314 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17315 n += len;
17318 if (field_width > len)
17320 field_width -= len;
17321 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17322 if (!NILP (props))
17323 Fadd_text_properties (make_number (0), make_number (field_width),
17324 props, lisp_string);
17325 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17326 n += field_width;
17329 return n;
17333 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17334 1, 4, 0,
17335 doc: /* Format a string out of a mode line format specification.
17336 First arg FORMAT specifies the mode line format (see `mode-line-format'
17337 for details) to use.
17339 Optional second arg FACE specifies the face property to put
17340 on all characters for which no face is specified.
17341 The value t means whatever face the window's mode line currently uses
17342 \(either `mode-line' or `mode-line-inactive', depending).
17343 A value of nil means the default is no face property.
17344 If FACE is an integer, the value string has no text properties.
17346 Optional third and fourth args WINDOW and BUFFER specify the window
17347 and buffer to use as the context for the formatting (defaults
17348 are the selected window and the window's buffer). */)
17349 (format, face, window, buffer)
17350 Lisp_Object format, face, window, buffer;
17352 struct it it;
17353 int len;
17354 struct window *w;
17355 struct buffer *old_buffer = NULL;
17356 int face_id = -1;
17357 int no_props = INTEGERP (face);
17358 int count = SPECPDL_INDEX ();
17359 Lisp_Object str;
17360 int string_start = 0;
17362 if (NILP (window))
17363 window = selected_window;
17364 CHECK_WINDOW (window);
17365 w = XWINDOW (window);
17367 if (NILP (buffer))
17368 buffer = w->buffer;
17369 CHECK_BUFFER (buffer);
17371 if (NILP (format))
17372 return build_string ("");
17374 if (no_props)
17375 face = Qnil;
17377 if (!NILP (face))
17379 if (EQ (face, Qt))
17380 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17381 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17384 if (face_id < 0)
17385 face_id = DEFAULT_FACE_ID;
17387 if (XBUFFER (buffer) != current_buffer)
17388 old_buffer = current_buffer;
17390 /* Save things including mode_line_proptrans_alist,
17391 and set that to nil so that we don't alter the outer value. */
17392 record_unwind_protect (unwind_format_mode_line,
17393 format_mode_line_unwind_data (old_buffer, 1));
17394 mode_line_proptrans_alist = Qnil;
17396 if (old_buffer)
17397 set_buffer_internal_1 (XBUFFER (buffer));
17399 init_iterator (&it, w, -1, -1, NULL, face_id);
17401 if (no_props)
17403 mode_line_target = MODE_LINE_NOPROP;
17404 mode_line_string_face_prop = Qnil;
17405 mode_line_string_list = Qnil;
17406 string_start = MODE_LINE_NOPROP_LEN (0);
17408 else
17410 mode_line_target = MODE_LINE_STRING;
17411 mode_line_string_list = Qnil;
17412 mode_line_string_face = face;
17413 mode_line_string_face_prop
17414 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17417 push_frame_kboard (it.f);
17418 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17419 pop_frame_kboard ();
17421 if (no_props)
17423 len = MODE_LINE_NOPROP_LEN (string_start);
17424 str = make_string (mode_line_noprop_buf + string_start, len);
17426 else
17428 mode_line_string_list = Fnreverse (mode_line_string_list);
17429 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17430 make_string ("", 0));
17433 unbind_to (count, Qnil);
17434 return str;
17437 /* Write a null-terminated, right justified decimal representation of
17438 the positive integer D to BUF using a minimal field width WIDTH. */
17440 static void
17441 pint2str (buf, width, d)
17442 register char *buf;
17443 register int width;
17444 register int d;
17446 register char *p = buf;
17448 if (d <= 0)
17449 *p++ = '0';
17450 else
17452 while (d > 0)
17454 *p++ = d % 10 + '0';
17455 d /= 10;
17459 for (width -= (int) (p - buf); width > 0; --width)
17460 *p++ = ' ';
17461 *p-- = '\0';
17462 while (p > buf)
17464 d = *buf;
17465 *buf++ = *p;
17466 *p-- = d;
17470 /* Write a null-terminated, right justified decimal and "human
17471 readable" representation of the nonnegative integer D to BUF using
17472 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17474 static const char power_letter[] =
17476 0, /* not used */
17477 'k', /* kilo */
17478 'M', /* mega */
17479 'G', /* giga */
17480 'T', /* tera */
17481 'P', /* peta */
17482 'E', /* exa */
17483 'Z', /* zetta */
17484 'Y' /* yotta */
17487 static void
17488 pint2hrstr (buf, width, d)
17489 char *buf;
17490 int width;
17491 int d;
17493 /* We aim to represent the nonnegative integer D as
17494 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17495 int quotient = d;
17496 int remainder = 0;
17497 /* -1 means: do not use TENTHS. */
17498 int tenths = -1;
17499 int exponent = 0;
17501 /* Length of QUOTIENT.TENTHS as a string. */
17502 int length;
17504 char * psuffix;
17505 char * p;
17507 if (1000 <= quotient)
17509 /* Scale to the appropriate EXPONENT. */
17512 remainder = quotient % 1000;
17513 quotient /= 1000;
17514 exponent++;
17516 while (1000 <= quotient);
17518 /* Round to nearest and decide whether to use TENTHS or not. */
17519 if (quotient <= 9)
17521 tenths = remainder / 100;
17522 if (50 <= remainder % 100)
17524 if (tenths < 9)
17525 tenths++;
17526 else
17528 quotient++;
17529 if (quotient == 10)
17530 tenths = -1;
17531 else
17532 tenths = 0;
17536 else
17537 if (500 <= remainder)
17539 if (quotient < 999)
17540 quotient++;
17541 else
17543 quotient = 1;
17544 exponent++;
17545 tenths = 0;
17550 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17551 if (tenths == -1 && quotient <= 99)
17552 if (quotient <= 9)
17553 length = 1;
17554 else
17555 length = 2;
17556 else
17557 length = 3;
17558 p = psuffix = buf + max (width, length);
17560 /* Print EXPONENT. */
17561 if (exponent)
17562 *psuffix++ = power_letter[exponent];
17563 *psuffix = '\0';
17565 /* Print TENTHS. */
17566 if (tenths >= 0)
17568 *--p = '0' + tenths;
17569 *--p = '.';
17572 /* Print QUOTIENT. */
17575 int digit = quotient % 10;
17576 *--p = '0' + digit;
17578 while ((quotient /= 10) != 0);
17580 /* Print leading spaces. */
17581 while (buf < p)
17582 *--p = ' ';
17585 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17586 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17587 type of CODING_SYSTEM. Return updated pointer into BUF. */
17589 static unsigned char invalid_eol_type[] = "(*invalid*)";
17591 static char *
17592 decode_mode_spec_coding (coding_system, buf, eol_flag)
17593 Lisp_Object coding_system;
17594 register char *buf;
17595 int eol_flag;
17597 Lisp_Object val;
17598 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17599 const unsigned char *eol_str;
17600 int eol_str_len;
17601 /* The EOL conversion we are using. */
17602 Lisp_Object eoltype;
17604 val = Fget (coding_system, Qcoding_system);
17605 eoltype = Qnil;
17607 if (!VECTORP (val)) /* Not yet decided. */
17609 if (multibyte)
17610 *buf++ = '-';
17611 if (eol_flag)
17612 eoltype = eol_mnemonic_undecided;
17613 /* Don't mention EOL conversion if it isn't decided. */
17615 else
17617 Lisp_Object eolvalue;
17619 eolvalue = Fget (coding_system, Qeol_type);
17621 if (multibyte)
17622 *buf++ = XFASTINT (AREF (val, 1));
17624 if (eol_flag)
17626 /* The EOL conversion that is normal on this system. */
17628 if (NILP (eolvalue)) /* Not yet decided. */
17629 eoltype = eol_mnemonic_undecided;
17630 else if (VECTORP (eolvalue)) /* Not yet decided. */
17631 eoltype = eol_mnemonic_undecided;
17632 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17633 eoltype = (XFASTINT (eolvalue) == 0
17634 ? eol_mnemonic_unix
17635 : (XFASTINT (eolvalue) == 1
17636 ? eol_mnemonic_dos : eol_mnemonic_mac));
17640 if (eol_flag)
17642 /* Mention the EOL conversion if it is not the usual one. */
17643 if (STRINGP (eoltype))
17645 eol_str = SDATA (eoltype);
17646 eol_str_len = SBYTES (eoltype);
17648 else if (INTEGERP (eoltype)
17649 && CHAR_VALID_P (XINT (eoltype), 0))
17651 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17652 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17653 eol_str = tmp;
17655 else
17657 eol_str = invalid_eol_type;
17658 eol_str_len = sizeof (invalid_eol_type) - 1;
17660 bcopy (eol_str, buf, eol_str_len);
17661 buf += eol_str_len;
17664 return buf;
17667 /* Return a string for the output of a mode line %-spec for window W,
17668 generated by character C. PRECISION >= 0 means don't return a
17669 string longer than that value. FIELD_WIDTH > 0 means pad the
17670 string returned with spaces to that value. Return 1 in *MULTIBYTE
17671 if the result is multibyte text.
17673 Note we operate on the current buffer for most purposes,
17674 the exception being w->base_line_pos. */
17676 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17678 static char *
17679 decode_mode_spec (w, c, field_width, precision, multibyte)
17680 struct window *w;
17681 register int c;
17682 int field_width, precision;
17683 int *multibyte;
17685 Lisp_Object obj;
17686 struct frame *f = XFRAME (WINDOW_FRAME (w));
17687 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17688 struct buffer *b = current_buffer;
17690 obj = Qnil;
17691 *multibyte = 0;
17693 switch (c)
17695 case '*':
17696 if (!NILP (b->read_only))
17697 return "%";
17698 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17699 return "*";
17700 return "-";
17702 case '+':
17703 /* This differs from %* only for a modified read-only buffer. */
17704 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17705 return "*";
17706 if (!NILP (b->read_only))
17707 return "%";
17708 return "-";
17710 case '&':
17711 /* This differs from %* in ignoring read-only-ness. */
17712 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17713 return "*";
17714 return "-";
17716 case '%':
17717 return "%";
17719 case '[':
17721 int i;
17722 char *p;
17724 if (command_loop_level > 5)
17725 return "[[[... ";
17726 p = decode_mode_spec_buf;
17727 for (i = 0; i < command_loop_level; i++)
17728 *p++ = '[';
17729 *p = 0;
17730 return decode_mode_spec_buf;
17733 case ']':
17735 int i;
17736 char *p;
17738 if (command_loop_level > 5)
17739 return " ...]]]";
17740 p = decode_mode_spec_buf;
17741 for (i = 0; i < command_loop_level; i++)
17742 *p++ = ']';
17743 *p = 0;
17744 return decode_mode_spec_buf;
17747 case '-':
17749 register int i;
17751 /* Let lots_of_dashes be a string of infinite length. */
17752 if (mode_line_target == MODE_LINE_NOPROP ||
17753 mode_line_target == MODE_LINE_STRING)
17754 return "--";
17755 if (field_width <= 0
17756 || field_width > sizeof (lots_of_dashes))
17758 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17759 decode_mode_spec_buf[i] = '-';
17760 decode_mode_spec_buf[i] = '\0';
17761 return decode_mode_spec_buf;
17763 else
17764 return lots_of_dashes;
17767 case 'b':
17768 obj = b->name;
17769 break;
17771 case 'c':
17772 /* %c and %l are ignored in `frame-title-format'.
17773 (In redisplay_internal, the frame title is drawn _before_ the
17774 windows are updated, so the stuff which depends on actual
17775 window contents (such as %l) may fail to render properly, or
17776 even crash emacs.) */
17777 if (mode_line_target == MODE_LINE_TITLE)
17778 return "";
17779 else
17781 int col = (int) current_column (); /* iftc */
17782 w->column_number_displayed = make_number (col);
17783 pint2str (decode_mode_spec_buf, field_width, col);
17784 return decode_mode_spec_buf;
17787 case 'e':
17788 #ifndef SYSTEM_MALLOC
17790 if (NILP (Vmemory_full))
17791 return "";
17792 else
17793 return "!MEM FULL! ";
17795 #else
17796 return "";
17797 #endif
17799 case 'F':
17800 /* %F displays the frame name. */
17801 if (!NILP (f->title))
17802 return (char *) SDATA (f->title);
17803 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17804 return (char *) SDATA (f->name);
17805 return "Emacs";
17807 case 'f':
17808 obj = b->filename;
17809 break;
17811 case 'i':
17813 int size = ZV - BEGV;
17814 pint2str (decode_mode_spec_buf, field_width, size);
17815 return decode_mode_spec_buf;
17818 case 'I':
17820 int size = ZV - BEGV;
17821 pint2hrstr (decode_mode_spec_buf, field_width, size);
17822 return decode_mode_spec_buf;
17825 case 'l':
17827 int startpos, startpos_byte, line, linepos, linepos_byte;
17828 int topline, nlines, junk, height;
17830 /* %c and %l are ignored in `frame-title-format'. */
17831 if (mode_line_target == MODE_LINE_TITLE)
17832 return "";
17834 startpos = XMARKER (w->start)->charpos;
17835 startpos_byte = marker_byte_position (w->start);
17836 height = WINDOW_TOTAL_LINES (w);
17838 /* If we decided that this buffer isn't suitable for line numbers,
17839 don't forget that too fast. */
17840 if (EQ (w->base_line_pos, w->buffer))
17841 goto no_value;
17842 /* But do forget it, if the window shows a different buffer now. */
17843 else if (BUFFERP (w->base_line_pos))
17844 w->base_line_pos = Qnil;
17846 /* If the buffer is very big, don't waste time. */
17847 if (INTEGERP (Vline_number_display_limit)
17848 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17850 w->base_line_pos = Qnil;
17851 w->base_line_number = Qnil;
17852 goto no_value;
17855 if (!NILP (w->base_line_number)
17856 && !NILP (w->base_line_pos)
17857 && XFASTINT (w->base_line_pos) <= startpos)
17859 line = XFASTINT (w->base_line_number);
17860 linepos = XFASTINT (w->base_line_pos);
17861 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17863 else
17865 line = 1;
17866 linepos = BUF_BEGV (b);
17867 linepos_byte = BUF_BEGV_BYTE (b);
17870 /* Count lines from base line to window start position. */
17871 nlines = display_count_lines (linepos, linepos_byte,
17872 startpos_byte,
17873 startpos, &junk);
17875 topline = nlines + line;
17877 /* Determine a new base line, if the old one is too close
17878 or too far away, or if we did not have one.
17879 "Too close" means it's plausible a scroll-down would
17880 go back past it. */
17881 if (startpos == BUF_BEGV (b))
17883 w->base_line_number = make_number (topline);
17884 w->base_line_pos = make_number (BUF_BEGV (b));
17886 else if (nlines < height + 25 || nlines > height * 3 + 50
17887 || linepos == BUF_BEGV (b))
17889 int limit = BUF_BEGV (b);
17890 int limit_byte = BUF_BEGV_BYTE (b);
17891 int position;
17892 int distance = (height * 2 + 30) * line_number_display_limit_width;
17894 if (startpos - distance > limit)
17896 limit = startpos - distance;
17897 limit_byte = CHAR_TO_BYTE (limit);
17900 nlines = display_count_lines (startpos, startpos_byte,
17901 limit_byte,
17902 - (height * 2 + 30),
17903 &position);
17904 /* If we couldn't find the lines we wanted within
17905 line_number_display_limit_width chars per line,
17906 give up on line numbers for this window. */
17907 if (position == limit_byte && limit == startpos - distance)
17909 w->base_line_pos = w->buffer;
17910 w->base_line_number = Qnil;
17911 goto no_value;
17914 w->base_line_number = make_number (topline - nlines);
17915 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17918 /* Now count lines from the start pos to point. */
17919 nlines = display_count_lines (startpos, startpos_byte,
17920 PT_BYTE, PT, &junk);
17922 /* Record that we did display the line number. */
17923 line_number_displayed = 1;
17925 /* Make the string to show. */
17926 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17927 return decode_mode_spec_buf;
17928 no_value:
17930 char* p = decode_mode_spec_buf;
17931 int pad = field_width - 2;
17932 while (pad-- > 0)
17933 *p++ = ' ';
17934 *p++ = '?';
17935 *p++ = '?';
17936 *p = '\0';
17937 return decode_mode_spec_buf;
17940 break;
17942 case 'm':
17943 obj = b->mode_name;
17944 break;
17946 case 'n':
17947 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17948 return " Narrow";
17949 break;
17951 case 'p':
17953 int pos = marker_position (w->start);
17954 int total = BUF_ZV (b) - BUF_BEGV (b);
17956 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17958 if (pos <= BUF_BEGV (b))
17959 return "All";
17960 else
17961 return "Bottom";
17963 else if (pos <= BUF_BEGV (b))
17964 return "Top";
17965 else
17967 if (total > 1000000)
17968 /* Do it differently for a large value, to avoid overflow. */
17969 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17970 else
17971 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17972 /* We can't normally display a 3-digit number,
17973 so get us a 2-digit number that is close. */
17974 if (total == 100)
17975 total = 99;
17976 sprintf (decode_mode_spec_buf, "%2d%%", total);
17977 return decode_mode_spec_buf;
17981 /* Display percentage of size above the bottom of the screen. */
17982 case 'P':
17984 int toppos = marker_position (w->start);
17985 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17986 int total = BUF_ZV (b) - BUF_BEGV (b);
17988 if (botpos >= BUF_ZV (b))
17990 if (toppos <= BUF_BEGV (b))
17991 return "All";
17992 else
17993 return "Bottom";
17995 else
17997 if (total > 1000000)
17998 /* Do it differently for a large value, to avoid overflow. */
17999 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18000 else
18001 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18002 /* We can't normally display a 3-digit number,
18003 so get us a 2-digit number that is close. */
18004 if (total == 100)
18005 total = 99;
18006 if (toppos <= BUF_BEGV (b))
18007 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18008 else
18009 sprintf (decode_mode_spec_buf, "%2d%%", total);
18010 return decode_mode_spec_buf;
18014 case 's':
18015 /* status of process */
18016 obj = Fget_buffer_process (Fcurrent_buffer ());
18017 if (NILP (obj))
18018 return "no process";
18019 #ifdef subprocesses
18020 obj = Fsymbol_name (Fprocess_status (obj));
18021 #endif
18022 break;
18024 case 't': /* indicate TEXT or BINARY */
18025 #ifdef MODE_LINE_BINARY_TEXT
18026 return MODE_LINE_BINARY_TEXT (b);
18027 #else
18028 return "T";
18029 #endif
18031 case 'z':
18032 /* coding-system (not including end-of-line format) */
18033 case 'Z':
18034 /* coding-system (including end-of-line type) */
18036 int eol_flag = (c == 'Z');
18037 char *p = decode_mode_spec_buf;
18039 if (! FRAME_WINDOW_P (f))
18041 /* No need to mention EOL here--the terminal never needs
18042 to do EOL conversion. */
18043 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
18044 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
18046 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18047 p, eol_flag);
18049 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18050 #ifdef subprocesses
18051 obj = Fget_buffer_process (Fcurrent_buffer ());
18052 if (PROCESSP (obj))
18054 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18055 p, eol_flag);
18056 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18057 p, eol_flag);
18059 #endif /* subprocesses */
18060 #endif /* 0 */
18061 *p = 0;
18062 return decode_mode_spec_buf;
18066 if (STRINGP (obj))
18068 *multibyte = STRING_MULTIBYTE (obj);
18069 return (char *) SDATA (obj);
18071 else
18072 return "";
18076 /* Count up to COUNT lines starting from START / START_BYTE.
18077 But don't go beyond LIMIT_BYTE.
18078 Return the number of lines thus found (always nonnegative).
18080 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18082 static int
18083 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18084 int start, start_byte, limit_byte, count;
18085 int *byte_pos_ptr;
18087 register unsigned char *cursor;
18088 unsigned char *base;
18090 register int ceiling;
18091 register unsigned char *ceiling_addr;
18092 int orig_count = count;
18094 /* If we are not in selective display mode,
18095 check only for newlines. */
18096 int selective_display = (!NILP (current_buffer->selective_display)
18097 && !INTEGERP (current_buffer->selective_display));
18099 if (count > 0)
18101 while (start_byte < limit_byte)
18103 ceiling = BUFFER_CEILING_OF (start_byte);
18104 ceiling = min (limit_byte - 1, ceiling);
18105 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18106 base = (cursor = BYTE_POS_ADDR (start_byte));
18107 while (1)
18109 if (selective_display)
18110 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18112 else
18113 while (*cursor != '\n' && ++cursor != ceiling_addr)
18116 if (cursor != ceiling_addr)
18118 if (--count == 0)
18120 start_byte += cursor - base + 1;
18121 *byte_pos_ptr = start_byte;
18122 return orig_count;
18124 else
18125 if (++cursor == ceiling_addr)
18126 break;
18128 else
18129 break;
18131 start_byte += cursor - base;
18134 else
18136 while (start_byte > limit_byte)
18138 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18139 ceiling = max (limit_byte, ceiling);
18140 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18141 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18142 while (1)
18144 if (selective_display)
18145 while (--cursor != ceiling_addr
18146 && *cursor != '\n' && *cursor != 015)
18148 else
18149 while (--cursor != ceiling_addr && *cursor != '\n')
18152 if (cursor != ceiling_addr)
18154 if (++count == 0)
18156 start_byte += cursor - base + 1;
18157 *byte_pos_ptr = start_byte;
18158 /* When scanning backwards, we should
18159 not count the newline posterior to which we stop. */
18160 return - orig_count - 1;
18163 else
18164 break;
18166 /* Here we add 1 to compensate for the last decrement
18167 of CURSOR, which took it past the valid range. */
18168 start_byte += cursor - base + 1;
18172 *byte_pos_ptr = limit_byte;
18174 if (count < 0)
18175 return - orig_count + count;
18176 return orig_count - count;
18182 /***********************************************************************
18183 Displaying strings
18184 ***********************************************************************/
18186 /* Display a NUL-terminated string, starting with index START.
18188 If STRING is non-null, display that C string. Otherwise, the Lisp
18189 string LISP_STRING is displayed.
18191 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18192 FACE_STRING. Display STRING or LISP_STRING with the face at
18193 FACE_STRING_POS in FACE_STRING:
18195 Display the string in the environment given by IT, but use the
18196 standard display table, temporarily.
18198 FIELD_WIDTH is the minimum number of output glyphs to produce.
18199 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18200 with spaces. If STRING has more characters, more than FIELD_WIDTH
18201 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18203 PRECISION is the maximum number of characters to output from
18204 STRING. PRECISION < 0 means don't truncate the string.
18206 This is roughly equivalent to printf format specifiers:
18208 FIELD_WIDTH PRECISION PRINTF
18209 ----------------------------------------
18210 -1 -1 %s
18211 -1 10 %.10s
18212 10 -1 %10s
18213 20 10 %20.10s
18215 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18216 display them, and < 0 means obey the current buffer's value of
18217 enable_multibyte_characters.
18219 Value is the number of columns displayed. */
18221 static int
18222 display_string (string, lisp_string, face_string, face_string_pos,
18223 start, it, field_width, precision, max_x, multibyte)
18224 unsigned char *string;
18225 Lisp_Object lisp_string;
18226 Lisp_Object face_string;
18227 int face_string_pos;
18228 int start;
18229 struct it *it;
18230 int field_width, precision, max_x;
18231 int multibyte;
18233 int hpos_at_start = it->hpos;
18234 int saved_face_id = it->face_id;
18235 struct glyph_row *row = it->glyph_row;
18237 /* Initialize the iterator IT for iteration over STRING beginning
18238 with index START. */
18239 reseat_to_string (it, string, lisp_string, start,
18240 precision, field_width, multibyte);
18242 /* If displaying STRING, set up the face of the iterator
18243 from LISP_STRING, if that's given. */
18244 if (STRINGP (face_string))
18246 int endptr;
18247 struct face *face;
18249 it->face_id
18250 = face_at_string_position (it->w, face_string, face_string_pos,
18251 0, it->region_beg_charpos,
18252 it->region_end_charpos,
18253 &endptr, it->base_face_id, 0);
18254 face = FACE_FROM_ID (it->f, it->face_id);
18255 it->face_box_p = face->box != FACE_NO_BOX;
18258 /* Set max_x to the maximum allowed X position. Don't let it go
18259 beyond the right edge of the window. */
18260 if (max_x <= 0)
18261 max_x = it->last_visible_x;
18262 else
18263 max_x = min (max_x, it->last_visible_x);
18265 /* Skip over display elements that are not visible. because IT->w is
18266 hscrolled. */
18267 if (it->current_x < it->first_visible_x)
18268 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18269 MOVE_TO_POS | MOVE_TO_X);
18271 row->ascent = it->max_ascent;
18272 row->height = it->max_ascent + it->max_descent;
18273 row->phys_ascent = it->max_phys_ascent;
18274 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18275 row->extra_line_spacing = it->max_extra_line_spacing;
18277 /* This condition is for the case that we are called with current_x
18278 past last_visible_x. */
18279 while (it->current_x < max_x)
18281 int x_before, x, n_glyphs_before, i, nglyphs;
18283 /* Get the next display element. */
18284 if (!get_next_display_element (it))
18285 break;
18287 /* Produce glyphs. */
18288 x_before = it->current_x;
18289 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18290 PRODUCE_GLYPHS (it);
18292 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18293 i = 0;
18294 x = x_before;
18295 while (i < nglyphs)
18297 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18299 if (!it->truncate_lines_p
18300 && x + glyph->pixel_width > max_x)
18302 /* End of continued line or max_x reached. */
18303 if (CHAR_GLYPH_PADDING_P (*glyph))
18305 /* A wide character is unbreakable. */
18306 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18307 it->current_x = x_before;
18309 else
18311 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18312 it->current_x = x;
18314 break;
18316 else if (x + glyph->pixel_width > it->first_visible_x)
18318 /* Glyph is at least partially visible. */
18319 ++it->hpos;
18320 if (x < it->first_visible_x)
18321 it->glyph_row->x = x - it->first_visible_x;
18323 else
18325 /* Glyph is off the left margin of the display area.
18326 Should not happen. */
18327 abort ();
18330 row->ascent = max (row->ascent, it->max_ascent);
18331 row->height = max (row->height, it->max_ascent + it->max_descent);
18332 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18333 row->phys_height = max (row->phys_height,
18334 it->max_phys_ascent + it->max_phys_descent);
18335 row->extra_line_spacing = max (row->extra_line_spacing,
18336 it->max_extra_line_spacing);
18337 x += glyph->pixel_width;
18338 ++i;
18341 /* Stop if max_x reached. */
18342 if (i < nglyphs)
18343 break;
18345 /* Stop at line ends. */
18346 if (ITERATOR_AT_END_OF_LINE_P (it))
18348 it->continuation_lines_width = 0;
18349 break;
18352 set_iterator_to_next (it, 1);
18354 /* Stop if truncating at the right edge. */
18355 if (it->truncate_lines_p
18356 && it->current_x >= it->last_visible_x)
18358 /* Add truncation mark, but don't do it if the line is
18359 truncated at a padding space. */
18360 if (IT_CHARPOS (*it) < it->string_nchars)
18362 if (!FRAME_WINDOW_P (it->f))
18364 int i, n;
18366 if (it->current_x > it->last_visible_x)
18368 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18369 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18370 break;
18371 for (n = row->used[TEXT_AREA]; i < n; ++i)
18373 row->used[TEXT_AREA] = i;
18374 produce_special_glyphs (it, IT_TRUNCATION);
18377 produce_special_glyphs (it, IT_TRUNCATION);
18379 it->glyph_row->truncated_on_right_p = 1;
18381 break;
18385 /* Maybe insert a truncation at the left. */
18386 if (it->first_visible_x
18387 && IT_CHARPOS (*it) > 0)
18389 if (!FRAME_WINDOW_P (it->f))
18390 insert_left_trunc_glyphs (it);
18391 it->glyph_row->truncated_on_left_p = 1;
18394 it->face_id = saved_face_id;
18396 /* Value is number of columns displayed. */
18397 return it->hpos - hpos_at_start;
18402 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18403 appears as an element of LIST or as the car of an element of LIST.
18404 If PROPVAL is a list, compare each element against LIST in that
18405 way, and return 1/2 if any element of PROPVAL is found in LIST.
18406 Otherwise return 0. This function cannot quit.
18407 The return value is 2 if the text is invisible but with an ellipsis
18408 and 1 if it's invisible and without an ellipsis. */
18411 invisible_p (propval, list)
18412 register Lisp_Object propval;
18413 Lisp_Object list;
18415 register Lisp_Object tail, proptail;
18417 for (tail = list; CONSP (tail); tail = XCDR (tail))
18419 register Lisp_Object tem;
18420 tem = XCAR (tail);
18421 if (EQ (propval, tem))
18422 return 1;
18423 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18424 return NILP (XCDR (tem)) ? 1 : 2;
18427 if (CONSP (propval))
18429 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18431 Lisp_Object propelt;
18432 propelt = XCAR (proptail);
18433 for (tail = list; CONSP (tail); tail = XCDR (tail))
18435 register Lisp_Object tem;
18436 tem = XCAR (tail);
18437 if (EQ (propelt, tem))
18438 return 1;
18439 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18440 return NILP (XCDR (tem)) ? 1 : 2;
18445 return 0;
18448 /* Calculate a width or height in pixels from a specification using
18449 the following elements:
18451 SPEC ::=
18452 NUM - a (fractional) multiple of the default font width/height
18453 (NUM) - specifies exactly NUM pixels
18454 UNIT - a fixed number of pixels, see below.
18455 ELEMENT - size of a display element in pixels, see below.
18456 (NUM . SPEC) - equals NUM * SPEC
18457 (+ SPEC SPEC ...) - add pixel values
18458 (- SPEC SPEC ...) - subtract pixel values
18459 (- SPEC) - negate pixel value
18461 NUM ::=
18462 INT or FLOAT - a number constant
18463 SYMBOL - use symbol's (buffer local) variable binding.
18465 UNIT ::=
18466 in - pixels per inch *)
18467 mm - pixels per 1/1000 meter *)
18468 cm - pixels per 1/100 meter *)
18469 width - width of current font in pixels.
18470 height - height of current font in pixels.
18472 *) using the ratio(s) defined in display-pixels-per-inch.
18474 ELEMENT ::=
18476 left-fringe - left fringe width in pixels
18477 right-fringe - right fringe width in pixels
18479 left-margin - left margin width in pixels
18480 right-margin - right margin width in pixels
18482 scroll-bar - scroll-bar area width in pixels
18484 Examples:
18486 Pixels corresponding to 5 inches:
18487 (5 . in)
18489 Total width of non-text areas on left side of window (if scroll-bar is on left):
18490 '(space :width (+ left-fringe left-margin scroll-bar))
18492 Align to first text column (in header line):
18493 '(space :align-to 0)
18495 Align to middle of text area minus half the width of variable `my-image'
18496 containing a loaded image:
18497 '(space :align-to (0.5 . (- text my-image)))
18499 Width of left margin minus width of 1 character in the default font:
18500 '(space :width (- left-margin 1))
18502 Width of left margin minus width of 2 characters in the current font:
18503 '(space :width (- left-margin (2 . width)))
18505 Center 1 character over left-margin (in header line):
18506 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18508 Different ways to express width of left fringe plus left margin minus one pixel:
18509 '(space :width (- (+ left-fringe left-margin) (1)))
18510 '(space :width (+ left-fringe left-margin (- (1))))
18511 '(space :width (+ left-fringe left-margin (-1)))
18515 #define NUMVAL(X) \
18516 ((INTEGERP (X) || FLOATP (X)) \
18517 ? XFLOATINT (X) \
18518 : - 1)
18521 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18522 double *res;
18523 struct it *it;
18524 Lisp_Object prop;
18525 void *font;
18526 int width_p, *align_to;
18528 double pixels;
18530 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18531 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18533 if (NILP (prop))
18534 return OK_PIXELS (0);
18536 if (SYMBOLP (prop))
18538 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18540 char *unit = SDATA (SYMBOL_NAME (prop));
18542 if (unit[0] == 'i' && unit[1] == 'n')
18543 pixels = 1.0;
18544 else if (unit[0] == 'm' && unit[1] == 'm')
18545 pixels = 25.4;
18546 else if (unit[0] == 'c' && unit[1] == 'm')
18547 pixels = 2.54;
18548 else
18549 pixels = 0;
18550 if (pixels > 0)
18552 double ppi;
18553 #ifdef HAVE_WINDOW_SYSTEM
18554 if (FRAME_WINDOW_P (it->f)
18555 && (ppi = (width_p
18556 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18557 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18558 ppi > 0))
18559 return OK_PIXELS (ppi / pixels);
18560 #endif
18562 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18563 || (CONSP (Vdisplay_pixels_per_inch)
18564 && (ppi = (width_p
18565 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18566 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18567 ppi > 0)))
18568 return OK_PIXELS (ppi / pixels);
18570 return 0;
18574 #ifdef HAVE_WINDOW_SYSTEM
18575 if (EQ (prop, Qheight))
18576 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18577 if (EQ (prop, Qwidth))
18578 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18579 #else
18580 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18581 return OK_PIXELS (1);
18582 #endif
18584 if (EQ (prop, Qtext))
18585 return OK_PIXELS (width_p
18586 ? window_box_width (it->w, TEXT_AREA)
18587 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18589 if (align_to && *align_to < 0)
18591 *res = 0;
18592 if (EQ (prop, Qleft))
18593 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18594 if (EQ (prop, Qright))
18595 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18596 if (EQ (prop, Qcenter))
18597 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18598 + window_box_width (it->w, TEXT_AREA) / 2);
18599 if (EQ (prop, Qleft_fringe))
18600 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18601 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18602 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18603 if (EQ (prop, Qright_fringe))
18604 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18605 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18606 : window_box_right_offset (it->w, TEXT_AREA));
18607 if (EQ (prop, Qleft_margin))
18608 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18609 if (EQ (prop, Qright_margin))
18610 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18611 if (EQ (prop, Qscroll_bar))
18612 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18614 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18615 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18616 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18617 : 0)));
18619 else
18621 if (EQ (prop, Qleft_fringe))
18622 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18623 if (EQ (prop, Qright_fringe))
18624 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18625 if (EQ (prop, Qleft_margin))
18626 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18627 if (EQ (prop, Qright_margin))
18628 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18629 if (EQ (prop, Qscroll_bar))
18630 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18633 prop = Fbuffer_local_value (prop, it->w->buffer);
18636 if (INTEGERP (prop) || FLOATP (prop))
18638 int base_unit = (width_p
18639 ? FRAME_COLUMN_WIDTH (it->f)
18640 : FRAME_LINE_HEIGHT (it->f));
18641 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18644 if (CONSP (prop))
18646 Lisp_Object car = XCAR (prop);
18647 Lisp_Object cdr = XCDR (prop);
18649 if (SYMBOLP (car))
18651 #ifdef HAVE_WINDOW_SYSTEM
18652 if (valid_image_p (prop))
18654 int id = lookup_image (it->f, prop);
18655 struct image *img = IMAGE_FROM_ID (it->f, id);
18657 return OK_PIXELS (width_p ? img->width : img->height);
18659 #endif
18660 if (EQ (car, Qplus) || EQ (car, Qminus))
18662 int first = 1;
18663 double px;
18665 pixels = 0;
18666 while (CONSP (cdr))
18668 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18669 font, width_p, align_to))
18670 return 0;
18671 if (first)
18672 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18673 else
18674 pixels += px;
18675 cdr = XCDR (cdr);
18677 if (EQ (car, Qminus))
18678 pixels = -pixels;
18679 return OK_PIXELS (pixels);
18682 car = Fbuffer_local_value (car, it->w->buffer);
18685 if (INTEGERP (car) || FLOATP (car))
18687 double fact;
18688 pixels = XFLOATINT (car);
18689 if (NILP (cdr))
18690 return OK_PIXELS (pixels);
18691 if (calc_pixel_width_or_height (&fact, it, cdr,
18692 font, width_p, align_to))
18693 return OK_PIXELS (pixels * fact);
18694 return 0;
18697 return 0;
18700 return 0;
18704 /***********************************************************************
18705 Glyph Display
18706 ***********************************************************************/
18708 #ifdef HAVE_WINDOW_SYSTEM
18710 #if GLYPH_DEBUG
18712 void
18713 dump_glyph_string (s)
18714 struct glyph_string *s;
18716 fprintf (stderr, "glyph string\n");
18717 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18718 s->x, s->y, s->width, s->height);
18719 fprintf (stderr, " ybase = %d\n", s->ybase);
18720 fprintf (stderr, " hl = %d\n", s->hl);
18721 fprintf (stderr, " left overhang = %d, right = %d\n",
18722 s->left_overhang, s->right_overhang);
18723 fprintf (stderr, " nchars = %d\n", s->nchars);
18724 fprintf (stderr, " extends to end of line = %d\n",
18725 s->extends_to_end_of_line_p);
18726 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18727 fprintf (stderr, " bg width = %d\n", s->background_width);
18730 #endif /* GLYPH_DEBUG */
18732 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18733 of XChar2b structures for S; it can't be allocated in
18734 init_glyph_string because it must be allocated via `alloca'. W
18735 is the window on which S is drawn. ROW and AREA are the glyph row
18736 and area within the row from which S is constructed. START is the
18737 index of the first glyph structure covered by S. HL is a
18738 face-override for drawing S. */
18740 #ifdef HAVE_NTGUI
18741 #define OPTIONAL_HDC(hdc) hdc,
18742 #define DECLARE_HDC(hdc) HDC hdc;
18743 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18744 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18745 #endif
18747 #ifndef OPTIONAL_HDC
18748 #define OPTIONAL_HDC(hdc)
18749 #define DECLARE_HDC(hdc)
18750 #define ALLOCATE_HDC(hdc, f)
18751 #define RELEASE_HDC(hdc, f)
18752 #endif
18754 static void
18755 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18756 struct glyph_string *s;
18757 DECLARE_HDC (hdc)
18758 XChar2b *char2b;
18759 struct window *w;
18760 struct glyph_row *row;
18761 enum glyph_row_area area;
18762 int start;
18763 enum draw_glyphs_face hl;
18765 bzero (s, sizeof *s);
18766 s->w = w;
18767 s->f = XFRAME (w->frame);
18768 #ifdef HAVE_NTGUI
18769 s->hdc = hdc;
18770 #endif
18771 s->display = FRAME_X_DISPLAY (s->f);
18772 s->window = FRAME_X_WINDOW (s->f);
18773 s->char2b = char2b;
18774 s->hl = hl;
18775 s->row = row;
18776 s->area = area;
18777 s->first_glyph = row->glyphs[area] + start;
18778 s->height = row->height;
18779 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18781 /* Display the internal border below the tool-bar window. */
18782 if (WINDOWP (s->f->tool_bar_window)
18783 && s->w == XWINDOW (s->f->tool_bar_window))
18784 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18786 s->ybase = s->y + row->ascent;
18790 /* Append the list of glyph strings with head H and tail T to the list
18791 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18793 static INLINE void
18794 append_glyph_string_lists (head, tail, h, t)
18795 struct glyph_string **head, **tail;
18796 struct glyph_string *h, *t;
18798 if (h)
18800 if (*head)
18801 (*tail)->next = h;
18802 else
18803 *head = h;
18804 h->prev = *tail;
18805 *tail = t;
18810 /* Prepend the list of glyph strings with head H and tail T to the
18811 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18812 result. */
18814 static INLINE void
18815 prepend_glyph_string_lists (head, tail, h, t)
18816 struct glyph_string **head, **tail;
18817 struct glyph_string *h, *t;
18819 if (h)
18821 if (*head)
18822 (*head)->prev = t;
18823 else
18824 *tail = t;
18825 t->next = *head;
18826 *head = h;
18831 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18832 Set *HEAD and *TAIL to the resulting list. */
18834 static INLINE void
18835 append_glyph_string (head, tail, s)
18836 struct glyph_string **head, **tail;
18837 struct glyph_string *s;
18839 s->next = s->prev = NULL;
18840 append_glyph_string_lists (head, tail, s, s);
18844 /* Get face and two-byte form of character glyph GLYPH on frame F.
18845 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18846 a pointer to a realized face that is ready for display. */
18848 static INLINE struct face *
18849 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18850 struct frame *f;
18851 struct glyph *glyph;
18852 XChar2b *char2b;
18853 int *two_byte_p;
18855 struct face *face;
18857 xassert (glyph->type == CHAR_GLYPH);
18858 face = FACE_FROM_ID (f, glyph->face_id);
18860 if (two_byte_p)
18861 *two_byte_p = 0;
18863 if (!glyph->multibyte_p)
18865 /* Unibyte case. We don't have to encode, but we have to make
18866 sure to use a face suitable for unibyte. */
18867 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18869 else if (glyph->u.ch < 128)
18871 /* Case of ASCII in a face known to fit ASCII. */
18872 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18874 else
18876 int c1, c2, charset;
18878 /* Split characters into bytes. If c2 is -1 afterwards, C is
18879 really a one-byte character so that byte1 is zero. */
18880 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18881 if (c2 > 0)
18882 STORE_XCHAR2B (char2b, c1, c2);
18883 else
18884 STORE_XCHAR2B (char2b, 0, c1);
18886 /* Maybe encode the character in *CHAR2B. */
18887 if (charset != CHARSET_ASCII)
18889 struct font_info *font_info
18890 = FONT_INFO_FROM_ID (f, face->font_info_id);
18891 if (font_info)
18892 glyph->font_type
18893 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18897 /* Make sure X resources of the face are allocated. */
18898 xassert (face != NULL);
18899 PREPARE_FACE_FOR_DISPLAY (f, face);
18900 return face;
18904 /* Fill glyph string S with composition components specified by S->cmp.
18906 FACES is an array of faces for all components of this composition.
18907 S->gidx is the index of the first component for S.
18909 OVERLAPS non-zero means S should draw the foreground only, and use
18910 its physical height for clipping. See also draw_glyphs.
18912 Value is the index of a component not in S. */
18914 static int
18915 fill_composite_glyph_string (s, faces, overlaps)
18916 struct glyph_string *s;
18917 struct face **faces;
18918 int overlaps;
18920 int i;
18922 xassert (s);
18924 s->for_overlaps = overlaps;
18926 s->face = faces[s->gidx];
18927 s->font = s->face->font;
18928 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18930 /* For all glyphs of this composition, starting at the offset
18931 S->gidx, until we reach the end of the definition or encounter a
18932 glyph that requires the different face, add it to S. */
18933 ++s->nchars;
18934 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18935 ++s->nchars;
18937 /* All glyph strings for the same composition has the same width,
18938 i.e. the width set for the first component of the composition. */
18940 s->width = s->first_glyph->pixel_width;
18942 /* If the specified font could not be loaded, use the frame's
18943 default font, but record the fact that we couldn't load it in
18944 the glyph string so that we can draw rectangles for the
18945 characters of the glyph string. */
18946 if (s->font == NULL)
18948 s->font_not_found_p = 1;
18949 s->font = FRAME_FONT (s->f);
18952 /* Adjust base line for subscript/superscript text. */
18953 s->ybase += s->first_glyph->voffset;
18955 xassert (s->face && s->face->gc);
18957 /* This glyph string must always be drawn with 16-bit functions. */
18958 s->two_byte_p = 1;
18960 return s->gidx + s->nchars;
18964 /* Fill glyph string S from a sequence of character glyphs.
18966 FACE_ID is the face id of the string. START is the index of the
18967 first glyph to consider, END is the index of the last + 1.
18968 OVERLAPS non-zero means S should draw the foreground only, and use
18969 its physical height for clipping. See also draw_glyphs.
18971 Value is the index of the first glyph not in S. */
18973 static int
18974 fill_glyph_string (s, face_id, start, end, overlaps)
18975 struct glyph_string *s;
18976 int face_id;
18977 int start, end, overlaps;
18979 struct glyph *glyph, *last;
18980 int voffset;
18981 int glyph_not_available_p;
18983 xassert (s->f == XFRAME (s->w->frame));
18984 xassert (s->nchars == 0);
18985 xassert (start >= 0 && end > start);
18987 s->for_overlaps = overlaps,
18988 glyph = s->row->glyphs[s->area] + start;
18989 last = s->row->glyphs[s->area] + end;
18990 voffset = glyph->voffset;
18992 glyph_not_available_p = glyph->glyph_not_available_p;
18994 while (glyph < last
18995 && glyph->type == CHAR_GLYPH
18996 && glyph->voffset == voffset
18997 /* Same face id implies same font, nowadays. */
18998 && glyph->face_id == face_id
18999 && glyph->glyph_not_available_p == glyph_not_available_p)
19001 int two_byte_p;
19003 s->face = get_glyph_face_and_encoding (s->f, glyph,
19004 s->char2b + s->nchars,
19005 &two_byte_p);
19006 s->two_byte_p = two_byte_p;
19007 ++s->nchars;
19008 xassert (s->nchars <= end - start);
19009 s->width += glyph->pixel_width;
19010 ++glyph;
19013 s->font = s->face->font;
19014 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19016 /* If the specified font could not be loaded, use the frame's font,
19017 but record the fact that we couldn't load it in
19018 S->font_not_found_p so that we can draw rectangles for the
19019 characters of the glyph string. */
19020 if (s->font == NULL || glyph_not_available_p)
19022 s->font_not_found_p = 1;
19023 s->font = FRAME_FONT (s->f);
19026 /* Adjust base line for subscript/superscript text. */
19027 s->ybase += voffset;
19029 xassert (s->face && s->face->gc);
19030 return glyph - s->row->glyphs[s->area];
19034 /* Fill glyph string S from image glyph S->first_glyph. */
19036 static void
19037 fill_image_glyph_string (s)
19038 struct glyph_string *s;
19040 xassert (s->first_glyph->type == IMAGE_GLYPH);
19041 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19042 xassert (s->img);
19043 s->slice = s->first_glyph->slice;
19044 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19045 s->font = s->face->font;
19046 s->width = s->first_glyph->pixel_width;
19048 /* Adjust base line for subscript/superscript text. */
19049 s->ybase += s->first_glyph->voffset;
19053 /* Fill glyph string S from a sequence of stretch glyphs.
19055 ROW is the glyph row in which the glyphs are found, AREA is the
19056 area within the row. START is the index of the first glyph to
19057 consider, END is the index of the last + 1.
19059 Value is the index of the first glyph not in S. */
19061 static int
19062 fill_stretch_glyph_string (s, row, area, start, end)
19063 struct glyph_string *s;
19064 struct glyph_row *row;
19065 enum glyph_row_area area;
19066 int start, end;
19068 struct glyph *glyph, *last;
19069 int voffset, face_id;
19071 xassert (s->first_glyph->type == STRETCH_GLYPH);
19073 glyph = s->row->glyphs[s->area] + start;
19074 last = s->row->glyphs[s->area] + end;
19075 face_id = glyph->face_id;
19076 s->face = FACE_FROM_ID (s->f, face_id);
19077 s->font = s->face->font;
19078 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19079 s->width = glyph->pixel_width;
19080 s->nchars = 1;
19081 voffset = glyph->voffset;
19083 for (++glyph;
19084 (glyph < last
19085 && glyph->type == STRETCH_GLYPH
19086 && glyph->voffset == voffset
19087 && glyph->face_id == face_id);
19088 ++glyph)
19089 s->width += glyph->pixel_width;
19091 /* Adjust base line for subscript/superscript text. */
19092 s->ybase += voffset;
19094 /* The case that face->gc == 0 is handled when drawing the glyph
19095 string by calling PREPARE_FACE_FOR_DISPLAY. */
19096 xassert (s->face);
19097 return glyph - s->row->glyphs[s->area];
19101 /* EXPORT for RIF:
19102 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19103 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19104 assumed to be zero. */
19106 void
19107 x_get_glyph_overhangs (glyph, f, left, right)
19108 struct glyph *glyph;
19109 struct frame *f;
19110 int *left, *right;
19112 *left = *right = 0;
19114 if (glyph->type == CHAR_GLYPH)
19116 XFontStruct *font;
19117 struct face *face;
19118 struct font_info *font_info;
19119 XChar2b char2b;
19120 XCharStruct *pcm;
19122 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19123 font = face->font;
19124 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19125 if (font /* ++KFS: Should this be font_info ? */
19126 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19128 if (pcm->rbearing > pcm->width)
19129 *right = pcm->rbearing - pcm->width;
19130 if (pcm->lbearing < 0)
19131 *left = -pcm->lbearing;
19137 /* Return the index of the first glyph preceding glyph string S that
19138 is overwritten by S because of S's left overhang. Value is -1
19139 if no glyphs are overwritten. */
19141 static int
19142 left_overwritten (s)
19143 struct glyph_string *s;
19145 int k;
19147 if (s->left_overhang)
19149 int x = 0, i;
19150 struct glyph *glyphs = s->row->glyphs[s->area];
19151 int first = s->first_glyph - glyphs;
19153 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19154 x -= glyphs[i].pixel_width;
19156 k = i + 1;
19158 else
19159 k = -1;
19161 return k;
19165 /* Return the index of the first glyph preceding glyph string S that
19166 is overwriting S because of its right overhang. Value is -1 if no
19167 glyph in front of S overwrites S. */
19169 static int
19170 left_overwriting (s)
19171 struct glyph_string *s;
19173 int i, k, x;
19174 struct glyph *glyphs = s->row->glyphs[s->area];
19175 int first = s->first_glyph - glyphs;
19177 k = -1;
19178 x = 0;
19179 for (i = first - 1; i >= 0; --i)
19181 int left, right;
19182 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19183 if (x + right > 0)
19184 k = i;
19185 x -= glyphs[i].pixel_width;
19188 return k;
19192 /* Return the index of the last glyph following glyph string S that is
19193 not overwritten by S because of S's right overhang. Value is -1 if
19194 no such glyph is found. */
19196 static int
19197 right_overwritten (s)
19198 struct glyph_string *s;
19200 int k = -1;
19202 if (s->right_overhang)
19204 int x = 0, i;
19205 struct glyph *glyphs = s->row->glyphs[s->area];
19206 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19207 int end = s->row->used[s->area];
19209 for (i = first; i < end && s->right_overhang > x; ++i)
19210 x += glyphs[i].pixel_width;
19212 k = i;
19215 return k;
19219 /* Return the index of the last glyph following glyph string S that
19220 overwrites S because of its left overhang. Value is negative
19221 if no such glyph is found. */
19223 static int
19224 right_overwriting (s)
19225 struct glyph_string *s;
19227 int i, k, x;
19228 int end = s->row->used[s->area];
19229 struct glyph *glyphs = s->row->glyphs[s->area];
19230 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19232 k = -1;
19233 x = 0;
19234 for (i = first; i < end; ++i)
19236 int left, right;
19237 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19238 if (x - left < 0)
19239 k = i;
19240 x += glyphs[i].pixel_width;
19243 return k;
19247 /* Get face and two-byte form of character C in face FACE_ID on frame
19248 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19249 means we want to display multibyte text. DISPLAY_P non-zero means
19250 make sure that X resources for the face returned are allocated.
19251 Value is a pointer to a realized face that is ready for display if
19252 DISPLAY_P is non-zero. */
19254 static INLINE struct face *
19255 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19256 struct frame *f;
19257 int c, face_id;
19258 XChar2b *char2b;
19259 int multibyte_p, display_p;
19261 struct face *face = FACE_FROM_ID (f, face_id);
19263 if (!multibyte_p)
19265 /* Unibyte case. We don't have to encode, but we have to make
19266 sure to use a face suitable for unibyte. */
19267 STORE_XCHAR2B (char2b, 0, c);
19268 face_id = FACE_FOR_CHAR (f, face, c);
19269 face = FACE_FROM_ID (f, face_id);
19271 else if (c < 128)
19273 /* Case of ASCII in a face known to fit ASCII. */
19274 STORE_XCHAR2B (char2b, 0, c);
19276 else
19278 int c1, c2, charset;
19280 /* Split characters into bytes. If c2 is -1 afterwards, C is
19281 really a one-byte character so that byte1 is zero. */
19282 SPLIT_CHAR (c, charset, c1, c2);
19283 if (c2 > 0)
19284 STORE_XCHAR2B (char2b, c1, c2);
19285 else
19286 STORE_XCHAR2B (char2b, 0, c1);
19288 /* Maybe encode the character in *CHAR2B. */
19289 if (face->font != NULL)
19291 struct font_info *font_info
19292 = FONT_INFO_FROM_ID (f, face->font_info_id);
19293 if (font_info)
19294 rif->encode_char (c, char2b, font_info, 0);
19298 /* Make sure X resources of the face are allocated. */
19299 #ifdef HAVE_X_WINDOWS
19300 if (display_p)
19301 #endif
19303 xassert (face != NULL);
19304 PREPARE_FACE_FOR_DISPLAY (f, face);
19307 return face;
19311 /* Set background width of glyph string S. START is the index of the
19312 first glyph following S. LAST_X is the right-most x-position + 1
19313 in the drawing area. */
19315 static INLINE void
19316 set_glyph_string_background_width (s, start, last_x)
19317 struct glyph_string *s;
19318 int start;
19319 int last_x;
19321 /* If the face of this glyph string has to be drawn to the end of
19322 the drawing area, set S->extends_to_end_of_line_p. */
19324 if (start == s->row->used[s->area]
19325 && s->area == TEXT_AREA
19326 && ((s->row->fill_line_p
19327 && (s->hl == DRAW_NORMAL_TEXT
19328 || s->hl == DRAW_IMAGE_RAISED
19329 || s->hl == DRAW_IMAGE_SUNKEN))
19330 || s->hl == DRAW_MOUSE_FACE))
19331 s->extends_to_end_of_line_p = 1;
19333 /* If S extends its face to the end of the line, set its
19334 background_width to the distance to the right edge of the drawing
19335 area. */
19336 if (s->extends_to_end_of_line_p)
19337 s->background_width = last_x - s->x + 1;
19338 else
19339 s->background_width = s->width;
19343 /* Compute overhangs and x-positions for glyph string S and its
19344 predecessors, or successors. X is the starting x-position for S.
19345 BACKWARD_P non-zero means process predecessors. */
19347 static void
19348 compute_overhangs_and_x (s, x, backward_p)
19349 struct glyph_string *s;
19350 int x;
19351 int backward_p;
19353 if (backward_p)
19355 while (s)
19357 if (rif->compute_glyph_string_overhangs)
19358 rif->compute_glyph_string_overhangs (s);
19359 x -= s->width;
19360 s->x = x;
19361 s = s->prev;
19364 else
19366 while (s)
19368 if (rif->compute_glyph_string_overhangs)
19369 rif->compute_glyph_string_overhangs (s);
19370 s->x = x;
19371 x += s->width;
19372 s = s->next;
19379 /* The following macros are only called from draw_glyphs below.
19380 They reference the following parameters of that function directly:
19381 `w', `row', `area', and `overlap_p'
19382 as well as the following local variables:
19383 `s', `f', and `hdc' (in W32) */
19385 #ifdef HAVE_NTGUI
19386 /* On W32, silently add local `hdc' variable to argument list of
19387 init_glyph_string. */
19388 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19389 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19390 #else
19391 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19392 init_glyph_string (s, char2b, w, row, area, start, hl)
19393 #endif
19395 /* Add a glyph string for a stretch glyph to the list of strings
19396 between HEAD and TAIL. START is the index of the stretch glyph in
19397 row area AREA of glyph row ROW. END is the index of the last glyph
19398 in that glyph row area. X is the current output position assigned
19399 to the new glyph string constructed. HL overrides that face of the
19400 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19401 is the right-most x-position of the drawing area. */
19403 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19404 and below -- keep them on one line. */
19405 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19406 do \
19408 s = (struct glyph_string *) alloca (sizeof *s); \
19409 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19410 START = fill_stretch_glyph_string (s, row, area, START, END); \
19411 append_glyph_string (&HEAD, &TAIL, s); \
19412 s->x = (X); \
19414 while (0)
19417 /* Add a glyph string for an image glyph to the list of strings
19418 between HEAD and TAIL. START is the index of the image glyph in
19419 row area AREA of glyph row ROW. END is the index of the last glyph
19420 in that glyph row area. X is the current output position assigned
19421 to the new glyph string constructed. HL overrides that face of the
19422 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19423 is the right-most x-position of the drawing area. */
19425 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19426 do \
19428 s = (struct glyph_string *) alloca (sizeof *s); \
19429 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19430 fill_image_glyph_string (s); \
19431 append_glyph_string (&HEAD, &TAIL, s); \
19432 ++START; \
19433 s->x = (X); \
19435 while (0)
19438 /* Add a glyph string for a sequence of character glyphs to the list
19439 of strings between HEAD and TAIL. START is the index of the first
19440 glyph in row area AREA of glyph row ROW that is part of the new
19441 glyph string. END is the index of the last glyph in that glyph row
19442 area. X is the current output position assigned to the new glyph
19443 string constructed. HL overrides that face of the glyph; e.g. it
19444 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19445 right-most x-position of the drawing area. */
19447 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19448 do \
19450 int c, face_id; \
19451 XChar2b *char2b; \
19453 c = (row)->glyphs[area][START].u.ch; \
19454 face_id = (row)->glyphs[area][START].face_id; \
19456 s = (struct glyph_string *) alloca (sizeof *s); \
19457 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19458 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19459 append_glyph_string (&HEAD, &TAIL, s); \
19460 s->x = (X); \
19461 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19463 while (0)
19466 /* Add a glyph string for a composite sequence to the list of strings
19467 between HEAD and TAIL. START is the index of the first glyph in
19468 row area AREA of glyph row ROW that is part of the new glyph
19469 string. END is the index of the last glyph in that glyph row area.
19470 X is the current output position assigned to the new glyph string
19471 constructed. HL overrides that face of the glyph; e.g. it is
19472 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19473 x-position of the drawing area. */
19475 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19476 do { \
19477 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19478 int face_id = (row)->glyphs[area][START].face_id; \
19479 struct face *base_face = FACE_FROM_ID (f, face_id); \
19480 struct composition *cmp = composition_table[cmp_id]; \
19481 int glyph_len = cmp->glyph_len; \
19482 XChar2b *char2b; \
19483 struct face **faces; \
19484 struct glyph_string *first_s = NULL; \
19485 int n; \
19487 base_face = base_face->ascii_face; \
19488 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19489 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19490 /* At first, fill in `char2b' and `faces'. */ \
19491 for (n = 0; n < glyph_len; n++) \
19493 int c = COMPOSITION_GLYPH (cmp, n); \
19494 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19495 faces[n] = FACE_FROM_ID (f, this_face_id); \
19496 get_char_face_and_encoding (f, c, this_face_id, \
19497 char2b + n, 1, 1); \
19500 /* Make glyph_strings for each glyph sequence that is drawable by \
19501 the same face, and append them to HEAD/TAIL. */ \
19502 for (n = 0; n < cmp->glyph_len;) \
19504 s = (struct glyph_string *) alloca (sizeof *s); \
19505 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19506 append_glyph_string (&(HEAD), &(TAIL), s); \
19507 s->cmp = cmp; \
19508 s->gidx = n; \
19509 s->x = (X); \
19511 if (n == 0) \
19512 first_s = s; \
19514 n = fill_composite_glyph_string (s, faces, overlaps); \
19517 ++START; \
19518 s = first_s; \
19519 } while (0)
19522 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19523 of AREA of glyph row ROW on window W between indices START and END.
19524 HL overrides the face for drawing glyph strings, e.g. it is
19525 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19526 x-positions of the drawing area.
19528 This is an ugly monster macro construct because we must use alloca
19529 to allocate glyph strings (because draw_glyphs can be called
19530 asynchronously). */
19532 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19533 do \
19535 HEAD = TAIL = NULL; \
19536 while (START < END) \
19538 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19539 switch (first_glyph->type) \
19541 case CHAR_GLYPH: \
19542 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19543 HL, X, LAST_X); \
19544 break; \
19546 case COMPOSITE_GLYPH: \
19547 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19548 HL, X, LAST_X); \
19549 break; \
19551 case STRETCH_GLYPH: \
19552 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19553 HL, X, LAST_X); \
19554 break; \
19556 case IMAGE_GLYPH: \
19557 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19558 HL, X, LAST_X); \
19559 break; \
19561 default: \
19562 abort (); \
19565 set_glyph_string_background_width (s, START, LAST_X); \
19566 (X) += s->width; \
19569 while (0)
19572 /* Draw glyphs between START and END in AREA of ROW on window W,
19573 starting at x-position X. X is relative to AREA in W. HL is a
19574 face-override with the following meaning:
19576 DRAW_NORMAL_TEXT draw normally
19577 DRAW_CURSOR draw in cursor face
19578 DRAW_MOUSE_FACE draw in mouse face.
19579 DRAW_INVERSE_VIDEO draw in mode line face
19580 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19581 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19583 If OVERLAPS is non-zero, draw only the foreground of characters and
19584 clip to the physical height of ROW. Non-zero value also defines
19585 the overlapping part to be drawn:
19587 OVERLAPS_PRED overlap with preceding rows
19588 OVERLAPS_SUCC overlap with succeeding rows
19589 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19590 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19592 Value is the x-position reached, relative to AREA of W. */
19594 static int
19595 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19596 struct window *w;
19597 int x;
19598 struct glyph_row *row;
19599 enum glyph_row_area area;
19600 int start, end;
19601 enum draw_glyphs_face hl;
19602 int overlaps;
19604 struct glyph_string *head, *tail;
19605 struct glyph_string *s;
19606 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19607 int last_x, area_width;
19608 int x_reached;
19609 int i, j;
19610 struct frame *f = XFRAME (WINDOW_FRAME (w));
19611 DECLARE_HDC (hdc);
19613 ALLOCATE_HDC (hdc, f);
19615 /* Let's rather be paranoid than getting a SEGV. */
19616 end = min (end, row->used[area]);
19617 start = max (0, start);
19618 start = min (end, start);
19620 /* Translate X to frame coordinates. Set last_x to the right
19621 end of the drawing area. */
19622 if (row->full_width_p)
19624 /* X is relative to the left edge of W, without scroll bars
19625 or fringes. */
19626 x += WINDOW_LEFT_EDGE_X (w);
19627 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19629 else
19631 int area_left = window_box_left (w, area);
19632 x += area_left;
19633 area_width = window_box_width (w, area);
19634 last_x = area_left + area_width;
19637 /* Build a doubly-linked list of glyph_string structures between
19638 head and tail from what we have to draw. Note that the macro
19639 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19640 the reason we use a separate variable `i'. */
19641 i = start;
19642 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19643 if (tail)
19644 x_reached = tail->x + tail->background_width;
19645 else
19646 x_reached = x;
19648 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19649 the row, redraw some glyphs in front or following the glyph
19650 strings built above. */
19651 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19653 int dummy_x = 0;
19654 struct glyph_string *h, *t;
19656 /* Compute overhangs for all glyph strings. */
19657 if (rif->compute_glyph_string_overhangs)
19658 for (s = head; s; s = s->next)
19659 rif->compute_glyph_string_overhangs (s);
19661 /* Prepend glyph strings for glyphs in front of the first glyph
19662 string that are overwritten because of the first glyph
19663 string's left overhang. The background of all strings
19664 prepended must be drawn because the first glyph string
19665 draws over it. */
19666 i = left_overwritten (head);
19667 if (i >= 0)
19669 j = i;
19670 BUILD_GLYPH_STRINGS (j, start, h, t,
19671 DRAW_NORMAL_TEXT, dummy_x, last_x);
19672 start = i;
19673 compute_overhangs_and_x (t, head->x, 1);
19674 prepend_glyph_string_lists (&head, &tail, h, t);
19675 clip_head = head;
19678 /* Prepend glyph strings for glyphs in front of the first glyph
19679 string that overwrite that glyph string because of their
19680 right overhang. For these strings, only the foreground must
19681 be drawn, because it draws over the glyph string at `head'.
19682 The background must not be drawn because this would overwrite
19683 right overhangs of preceding glyphs for which no glyph
19684 strings exist. */
19685 i = left_overwriting (head);
19686 if (i >= 0)
19688 clip_head = head;
19689 BUILD_GLYPH_STRINGS (i, start, h, t,
19690 DRAW_NORMAL_TEXT, dummy_x, last_x);
19691 for (s = h; s; s = s->next)
19692 s->background_filled_p = 1;
19693 compute_overhangs_and_x (t, head->x, 1);
19694 prepend_glyph_string_lists (&head, &tail, h, t);
19697 /* Append glyphs strings for glyphs following the last glyph
19698 string tail that are overwritten by tail. The background of
19699 these strings has to be drawn because tail's foreground draws
19700 over it. */
19701 i = right_overwritten (tail);
19702 if (i >= 0)
19704 BUILD_GLYPH_STRINGS (end, i, h, t,
19705 DRAW_NORMAL_TEXT, x, last_x);
19706 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19707 append_glyph_string_lists (&head, &tail, h, t);
19708 clip_tail = tail;
19711 /* Append glyph strings for glyphs following the last glyph
19712 string tail that overwrite tail. The foreground of such
19713 glyphs has to be drawn because it writes into the background
19714 of tail. The background must not be drawn because it could
19715 paint over the foreground of following glyphs. */
19716 i = right_overwriting (tail);
19717 if (i >= 0)
19719 clip_tail = tail;
19720 BUILD_GLYPH_STRINGS (end, i, h, t,
19721 DRAW_NORMAL_TEXT, x, last_x);
19722 for (s = h; s; s = s->next)
19723 s->background_filled_p = 1;
19724 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19725 append_glyph_string_lists (&head, &tail, h, t);
19727 if (clip_head || clip_tail)
19728 for (s = head; s; s = s->next)
19730 s->clip_head = clip_head;
19731 s->clip_tail = clip_tail;
19735 /* Draw all strings. */
19736 for (s = head; s; s = s->next)
19737 rif->draw_glyph_string (s);
19739 if (area == TEXT_AREA
19740 && !row->full_width_p
19741 /* When drawing overlapping rows, only the glyph strings'
19742 foreground is drawn, which doesn't erase a cursor
19743 completely. */
19744 && !overlaps)
19746 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19747 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19748 : (tail ? tail->x + tail->background_width : x));
19750 int text_left = window_box_left (w, TEXT_AREA);
19751 x0 -= text_left;
19752 x1 -= text_left;
19754 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19755 row->y, MATRIX_ROW_BOTTOM_Y (row));
19758 /* Value is the x-position up to which drawn, relative to AREA of W.
19759 This doesn't include parts drawn because of overhangs. */
19760 if (row->full_width_p)
19761 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19762 else
19763 x_reached -= window_box_left (w, area);
19765 RELEASE_HDC (hdc, f);
19767 return x_reached;
19770 /* Expand row matrix if too narrow. Don't expand if area
19771 is not present. */
19773 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19775 if (!fonts_changed_p \
19776 && (it->glyph_row->glyphs[area] \
19777 < it->glyph_row->glyphs[area + 1])) \
19779 it->w->ncols_scale_factor++; \
19780 fonts_changed_p = 1; \
19784 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19785 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19787 static INLINE void
19788 append_glyph (it)
19789 struct it *it;
19791 struct glyph *glyph;
19792 enum glyph_row_area area = it->area;
19794 xassert (it->glyph_row);
19795 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19797 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19798 if (glyph < it->glyph_row->glyphs[area + 1])
19800 glyph->charpos = CHARPOS (it->position);
19801 glyph->object = it->object;
19802 glyph->pixel_width = it->pixel_width;
19803 glyph->ascent = it->ascent;
19804 glyph->descent = it->descent;
19805 glyph->voffset = it->voffset;
19806 glyph->type = CHAR_GLYPH;
19807 glyph->multibyte_p = it->multibyte_p;
19808 glyph->left_box_line_p = it->start_of_box_run_p;
19809 glyph->right_box_line_p = it->end_of_box_run_p;
19810 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19811 || it->phys_descent > it->descent);
19812 glyph->padding_p = 0;
19813 glyph->glyph_not_available_p = it->glyph_not_available_p;
19814 glyph->face_id = it->face_id;
19815 glyph->u.ch = it->char_to_display;
19816 glyph->slice = null_glyph_slice;
19817 glyph->font_type = FONT_TYPE_UNKNOWN;
19818 ++it->glyph_row->used[area];
19820 else
19821 IT_EXPAND_MATRIX_WIDTH (it, area);
19824 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19825 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19827 static INLINE void
19828 append_composite_glyph (it)
19829 struct it *it;
19831 struct glyph *glyph;
19832 enum glyph_row_area area = it->area;
19834 xassert (it->glyph_row);
19836 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19837 if (glyph < it->glyph_row->glyphs[area + 1])
19839 glyph->charpos = CHARPOS (it->position);
19840 glyph->object = it->object;
19841 glyph->pixel_width = it->pixel_width;
19842 glyph->ascent = it->ascent;
19843 glyph->descent = it->descent;
19844 glyph->voffset = it->voffset;
19845 glyph->type = COMPOSITE_GLYPH;
19846 glyph->multibyte_p = it->multibyte_p;
19847 glyph->left_box_line_p = it->start_of_box_run_p;
19848 glyph->right_box_line_p = it->end_of_box_run_p;
19849 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19850 || it->phys_descent > it->descent);
19851 glyph->padding_p = 0;
19852 glyph->glyph_not_available_p = 0;
19853 glyph->face_id = it->face_id;
19854 glyph->u.cmp_id = it->cmp_id;
19855 glyph->slice = null_glyph_slice;
19856 glyph->font_type = FONT_TYPE_UNKNOWN;
19857 ++it->glyph_row->used[area];
19859 else
19860 IT_EXPAND_MATRIX_WIDTH (it, area);
19864 /* Change IT->ascent and IT->height according to the setting of
19865 IT->voffset. */
19867 static INLINE void
19868 take_vertical_position_into_account (it)
19869 struct it *it;
19871 if (it->voffset)
19873 if (it->voffset < 0)
19874 /* Increase the ascent so that we can display the text higher
19875 in the line. */
19876 it->ascent -= it->voffset;
19877 else
19878 /* Increase the descent so that we can display the text lower
19879 in the line. */
19880 it->descent += it->voffset;
19885 /* Produce glyphs/get display metrics for the image IT is loaded with.
19886 See the description of struct display_iterator in dispextern.h for
19887 an overview of struct display_iterator. */
19889 static void
19890 produce_image_glyph (it)
19891 struct it *it;
19893 struct image *img;
19894 struct face *face;
19895 int glyph_ascent, crop;
19896 struct glyph_slice slice;
19898 xassert (it->what == IT_IMAGE);
19900 face = FACE_FROM_ID (it->f, it->face_id);
19901 xassert (face);
19902 /* Make sure X resources of the face is loaded. */
19903 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19905 if (it->image_id < 0)
19907 /* Fringe bitmap. */
19908 it->ascent = it->phys_ascent = 0;
19909 it->descent = it->phys_descent = 0;
19910 it->pixel_width = 0;
19911 it->nglyphs = 0;
19912 return;
19915 img = IMAGE_FROM_ID (it->f, it->image_id);
19916 xassert (img);
19917 /* Make sure X resources of the image is loaded. */
19918 prepare_image_for_display (it->f, img);
19920 slice.x = slice.y = 0;
19921 slice.width = img->width;
19922 slice.height = img->height;
19924 if (INTEGERP (it->slice.x))
19925 slice.x = XINT (it->slice.x);
19926 else if (FLOATP (it->slice.x))
19927 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19929 if (INTEGERP (it->slice.y))
19930 slice.y = XINT (it->slice.y);
19931 else if (FLOATP (it->slice.y))
19932 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19934 if (INTEGERP (it->slice.width))
19935 slice.width = XINT (it->slice.width);
19936 else if (FLOATP (it->slice.width))
19937 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19939 if (INTEGERP (it->slice.height))
19940 slice.height = XINT (it->slice.height);
19941 else if (FLOATP (it->slice.height))
19942 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19944 if (slice.x >= img->width)
19945 slice.x = img->width;
19946 if (slice.y >= img->height)
19947 slice.y = img->height;
19948 if (slice.x + slice.width >= img->width)
19949 slice.width = img->width - slice.x;
19950 if (slice.y + slice.height > img->height)
19951 slice.height = img->height - slice.y;
19953 if (slice.width == 0 || slice.height == 0)
19954 return;
19956 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19958 it->descent = slice.height - glyph_ascent;
19959 if (slice.y == 0)
19960 it->descent += img->vmargin;
19961 if (slice.y + slice.height == img->height)
19962 it->descent += img->vmargin;
19963 it->phys_descent = it->descent;
19965 it->pixel_width = slice.width;
19966 if (slice.x == 0)
19967 it->pixel_width += img->hmargin;
19968 if (slice.x + slice.width == img->width)
19969 it->pixel_width += img->hmargin;
19971 /* It's quite possible for images to have an ascent greater than
19972 their height, so don't get confused in that case. */
19973 if (it->descent < 0)
19974 it->descent = 0;
19976 #if 0 /* this breaks image tiling */
19977 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19978 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19979 if (face_ascent > it->ascent)
19980 it->ascent = it->phys_ascent = face_ascent;
19981 #endif
19983 it->nglyphs = 1;
19985 if (face->box != FACE_NO_BOX)
19987 if (face->box_line_width > 0)
19989 if (slice.y == 0)
19990 it->ascent += face->box_line_width;
19991 if (slice.y + slice.height == img->height)
19992 it->descent += face->box_line_width;
19995 if (it->start_of_box_run_p && slice.x == 0)
19996 it->pixel_width += abs (face->box_line_width);
19997 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19998 it->pixel_width += abs (face->box_line_width);
20001 take_vertical_position_into_account (it);
20003 /* Automatically crop wide image glyphs at right edge so we can
20004 draw the cursor on same display row. */
20005 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20006 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20008 it->pixel_width -= crop;
20009 slice.width -= crop;
20012 if (it->glyph_row)
20014 struct glyph *glyph;
20015 enum glyph_row_area area = it->area;
20017 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20018 if (glyph < it->glyph_row->glyphs[area + 1])
20020 glyph->charpos = CHARPOS (it->position);
20021 glyph->object = it->object;
20022 glyph->pixel_width = it->pixel_width;
20023 glyph->ascent = glyph_ascent;
20024 glyph->descent = it->descent;
20025 glyph->voffset = it->voffset;
20026 glyph->type = IMAGE_GLYPH;
20027 glyph->multibyte_p = it->multibyte_p;
20028 glyph->left_box_line_p = it->start_of_box_run_p;
20029 glyph->right_box_line_p = it->end_of_box_run_p;
20030 glyph->overlaps_vertically_p = 0;
20031 glyph->padding_p = 0;
20032 glyph->glyph_not_available_p = 0;
20033 glyph->face_id = it->face_id;
20034 glyph->u.img_id = img->id;
20035 glyph->slice = slice;
20036 glyph->font_type = FONT_TYPE_UNKNOWN;
20037 ++it->glyph_row->used[area];
20039 else
20040 IT_EXPAND_MATRIX_WIDTH (it, area);
20045 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20046 of the glyph, WIDTH and HEIGHT are the width and height of the
20047 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20049 static void
20050 append_stretch_glyph (it, object, width, height, ascent)
20051 struct it *it;
20052 Lisp_Object object;
20053 int width, height;
20054 int ascent;
20056 struct glyph *glyph;
20057 enum glyph_row_area area = it->area;
20059 xassert (ascent >= 0 && ascent <= height);
20061 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20062 if (glyph < it->glyph_row->glyphs[area + 1])
20064 glyph->charpos = CHARPOS (it->position);
20065 glyph->object = object;
20066 glyph->pixel_width = width;
20067 glyph->ascent = ascent;
20068 glyph->descent = height - ascent;
20069 glyph->voffset = it->voffset;
20070 glyph->type = STRETCH_GLYPH;
20071 glyph->multibyte_p = it->multibyte_p;
20072 glyph->left_box_line_p = it->start_of_box_run_p;
20073 glyph->right_box_line_p = it->end_of_box_run_p;
20074 glyph->overlaps_vertically_p = 0;
20075 glyph->padding_p = 0;
20076 glyph->glyph_not_available_p = 0;
20077 glyph->face_id = it->face_id;
20078 glyph->u.stretch.ascent = ascent;
20079 glyph->u.stretch.height = height;
20080 glyph->slice = null_glyph_slice;
20081 glyph->font_type = FONT_TYPE_UNKNOWN;
20082 ++it->glyph_row->used[area];
20084 else
20085 IT_EXPAND_MATRIX_WIDTH (it, area);
20089 /* Produce a stretch glyph for iterator IT. IT->object is the value
20090 of the glyph property displayed. The value must be a list
20091 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20092 being recognized:
20094 1. `:width WIDTH' specifies that the space should be WIDTH *
20095 canonical char width wide. WIDTH may be an integer or floating
20096 point number.
20098 2. `:relative-width FACTOR' specifies that the width of the stretch
20099 should be computed from the width of the first character having the
20100 `glyph' property, and should be FACTOR times that width.
20102 3. `:align-to HPOS' specifies that the space should be wide enough
20103 to reach HPOS, a value in canonical character units.
20105 Exactly one of the above pairs must be present.
20107 4. `:height HEIGHT' specifies that the height of the stretch produced
20108 should be HEIGHT, measured in canonical character units.
20110 5. `:relative-height FACTOR' specifies that the height of the
20111 stretch should be FACTOR times the height of the characters having
20112 the glyph property.
20114 Either none or exactly one of 4 or 5 must be present.
20116 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20117 of the stretch should be used for the ascent of the stretch.
20118 ASCENT must be in the range 0 <= ASCENT <= 100. */
20120 static void
20121 produce_stretch_glyph (it)
20122 struct it *it;
20124 /* (space :width WIDTH :height HEIGHT ...) */
20125 Lisp_Object prop, plist;
20126 int width = 0, height = 0, align_to = -1;
20127 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20128 int ascent = 0;
20129 double tem;
20130 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20131 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20133 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20135 /* List should start with `space'. */
20136 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20137 plist = XCDR (it->object);
20139 /* Compute the width of the stretch. */
20140 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20141 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20143 /* Absolute width `:width WIDTH' specified and valid. */
20144 zero_width_ok_p = 1;
20145 width = (int)tem;
20147 else if (prop = Fplist_get (plist, QCrelative_width),
20148 NUMVAL (prop) > 0)
20150 /* Relative width `:relative-width FACTOR' specified and valid.
20151 Compute the width of the characters having the `glyph'
20152 property. */
20153 struct it it2;
20154 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20156 it2 = *it;
20157 if (it->multibyte_p)
20159 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20160 - IT_BYTEPOS (*it));
20161 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20163 else
20164 it2.c = *p, it2.len = 1;
20166 it2.glyph_row = NULL;
20167 it2.what = IT_CHARACTER;
20168 x_produce_glyphs (&it2);
20169 width = NUMVAL (prop) * it2.pixel_width;
20171 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20172 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20174 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20175 align_to = (align_to < 0
20177 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20178 else if (align_to < 0)
20179 align_to = window_box_left_offset (it->w, TEXT_AREA);
20180 width = max (0, (int)tem + align_to - it->current_x);
20181 zero_width_ok_p = 1;
20183 else
20184 /* Nothing specified -> width defaults to canonical char width. */
20185 width = FRAME_COLUMN_WIDTH (it->f);
20187 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20188 width = 1;
20190 /* Compute height. */
20191 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20192 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20194 height = (int)tem;
20195 zero_height_ok_p = 1;
20197 else if (prop = Fplist_get (plist, QCrelative_height),
20198 NUMVAL (prop) > 0)
20199 height = FONT_HEIGHT (font) * NUMVAL (prop);
20200 else
20201 height = FONT_HEIGHT (font);
20203 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20204 height = 1;
20206 /* Compute percentage of height used for ascent. If
20207 `:ascent ASCENT' is present and valid, use that. Otherwise,
20208 derive the ascent from the font in use. */
20209 if (prop = Fplist_get (plist, QCascent),
20210 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20211 ascent = height * NUMVAL (prop) / 100.0;
20212 else if (!NILP (prop)
20213 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20214 ascent = min (max (0, (int)tem), height);
20215 else
20216 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20218 if (width > 0 && !it->truncate_lines_p
20219 && it->current_x + width > it->last_visible_x)
20220 width = it->last_visible_x - it->current_x - 1;
20222 if (width > 0 && height > 0 && it->glyph_row)
20224 Lisp_Object object = it->stack[it->sp - 1].string;
20225 if (!STRINGP (object))
20226 object = it->w->buffer;
20227 append_stretch_glyph (it, object, width, height, ascent);
20230 it->pixel_width = width;
20231 it->ascent = it->phys_ascent = ascent;
20232 it->descent = it->phys_descent = height - it->ascent;
20233 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20235 take_vertical_position_into_account (it);
20238 /* Get line-height and line-spacing property at point.
20239 If line-height has format (HEIGHT TOTAL), return TOTAL
20240 in TOTAL_HEIGHT. */
20242 static Lisp_Object
20243 get_line_height_property (it, prop)
20244 struct it *it;
20245 Lisp_Object prop;
20247 Lisp_Object position;
20249 if (STRINGP (it->object))
20250 position = make_number (IT_STRING_CHARPOS (*it));
20251 else if (BUFFERP (it->object))
20252 position = make_number (IT_CHARPOS (*it));
20253 else
20254 return Qnil;
20256 return Fget_char_property (position, prop, it->object);
20259 /* Calculate line-height and line-spacing properties.
20260 An integer value specifies explicit pixel value.
20261 A float value specifies relative value to current face height.
20262 A cons (float . face-name) specifies relative value to
20263 height of specified face font.
20265 Returns height in pixels, or nil. */
20268 static Lisp_Object
20269 calc_line_height_property (it, val, font, boff, override)
20270 struct it *it;
20271 Lisp_Object val;
20272 XFontStruct *font;
20273 int boff, override;
20275 Lisp_Object face_name = Qnil;
20276 int ascent, descent, height;
20278 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20279 return val;
20281 if (CONSP (val))
20283 face_name = XCAR (val);
20284 val = XCDR (val);
20285 if (!NUMBERP (val))
20286 val = make_number (1);
20287 if (NILP (face_name))
20289 height = it->ascent + it->descent;
20290 goto scale;
20294 if (NILP (face_name))
20296 font = FRAME_FONT (it->f);
20297 boff = FRAME_BASELINE_OFFSET (it->f);
20299 else if (EQ (face_name, Qt))
20301 override = 0;
20303 else
20305 int face_id;
20306 struct face *face;
20307 struct font_info *font_info;
20309 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20310 if (face_id < 0)
20311 return make_number (-1);
20313 face = FACE_FROM_ID (it->f, face_id);
20314 font = face->font;
20315 if (font == NULL)
20316 return make_number (-1);
20318 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20319 boff = font_info->baseline_offset;
20320 if (font_info->vertical_centering)
20321 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20324 ascent = FONT_BASE (font) + boff;
20325 descent = FONT_DESCENT (font) - boff;
20327 if (override)
20329 it->override_ascent = ascent;
20330 it->override_descent = descent;
20331 it->override_boff = boff;
20334 height = ascent + descent;
20336 scale:
20337 if (FLOATP (val))
20338 height = (int)(XFLOAT_DATA (val) * height);
20339 else if (INTEGERP (val))
20340 height *= XINT (val);
20342 return make_number (height);
20346 /* RIF:
20347 Produce glyphs/get display metrics for the display element IT is
20348 loaded with. See the description of struct it in dispextern.h
20349 for an overview of struct it. */
20351 void
20352 x_produce_glyphs (it)
20353 struct it *it;
20355 int extra_line_spacing = it->extra_line_spacing;
20357 it->glyph_not_available_p = 0;
20359 if (it->what == IT_CHARACTER)
20361 XChar2b char2b;
20362 XFontStruct *font;
20363 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20364 XCharStruct *pcm;
20365 int font_not_found_p;
20366 struct font_info *font_info;
20367 int boff; /* baseline offset */
20368 /* We may change it->multibyte_p upon unibyte<->multibyte
20369 conversion. So, save the current value now and restore it
20370 later.
20372 Note: It seems that we don't have to record multibyte_p in
20373 struct glyph because the character code itself tells if or
20374 not the character is multibyte. Thus, in the future, we must
20375 consider eliminating the field `multibyte_p' in the struct
20376 glyph. */
20377 int saved_multibyte_p = it->multibyte_p;
20379 /* Maybe translate single-byte characters to multibyte, or the
20380 other way. */
20381 it->char_to_display = it->c;
20382 if (!ASCII_BYTE_P (it->c))
20384 if (unibyte_display_via_language_environment
20385 && SINGLE_BYTE_CHAR_P (it->c)
20386 && (it->c >= 0240
20387 || !NILP (Vnonascii_translation_table)))
20389 it->char_to_display = unibyte_char_to_multibyte (it->c);
20390 it->multibyte_p = 1;
20391 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20392 face = FACE_FROM_ID (it->f, it->face_id);
20394 else if (!SINGLE_BYTE_CHAR_P (it->c)
20395 && !it->multibyte_p)
20397 it->multibyte_p = 1;
20398 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20399 face = FACE_FROM_ID (it->f, it->face_id);
20403 /* Get font to use. Encode IT->char_to_display. */
20404 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20405 &char2b, it->multibyte_p, 0);
20406 font = face->font;
20408 /* When no suitable font found, use the default font. */
20409 font_not_found_p = font == NULL;
20410 if (font_not_found_p)
20412 font = FRAME_FONT (it->f);
20413 boff = FRAME_BASELINE_OFFSET (it->f);
20414 font_info = NULL;
20416 else
20418 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20419 boff = font_info->baseline_offset;
20420 if (font_info->vertical_centering)
20421 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20424 if (it->char_to_display >= ' '
20425 && (!it->multibyte_p || it->char_to_display < 128))
20427 /* Either unibyte or ASCII. */
20428 int stretched_p;
20430 it->nglyphs = 1;
20432 pcm = rif->per_char_metric (font, &char2b,
20433 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20435 if (it->override_ascent >= 0)
20437 it->ascent = it->override_ascent;
20438 it->descent = it->override_descent;
20439 boff = it->override_boff;
20441 else
20443 it->ascent = FONT_BASE (font) + boff;
20444 it->descent = FONT_DESCENT (font) - boff;
20447 if (pcm)
20449 it->phys_ascent = pcm->ascent + boff;
20450 it->phys_descent = pcm->descent - boff;
20451 it->pixel_width = pcm->width;
20453 else
20455 it->glyph_not_available_p = 1;
20456 it->phys_ascent = it->ascent;
20457 it->phys_descent = it->descent;
20458 it->pixel_width = FONT_WIDTH (font);
20461 if (it->constrain_row_ascent_descent_p)
20463 if (it->descent > it->max_descent)
20465 it->ascent += it->descent - it->max_descent;
20466 it->descent = it->max_descent;
20468 if (it->ascent > it->max_ascent)
20470 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20471 it->ascent = it->max_ascent;
20473 it->phys_ascent = min (it->phys_ascent, it->ascent);
20474 it->phys_descent = min (it->phys_descent, it->descent);
20475 extra_line_spacing = 0;
20478 /* If this is a space inside a region of text with
20479 `space-width' property, change its width. */
20480 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20481 if (stretched_p)
20482 it->pixel_width *= XFLOATINT (it->space_width);
20484 /* If face has a box, add the box thickness to the character
20485 height. If character has a box line to the left and/or
20486 right, add the box line width to the character's width. */
20487 if (face->box != FACE_NO_BOX)
20489 int thick = face->box_line_width;
20491 if (thick > 0)
20493 it->ascent += thick;
20494 it->descent += thick;
20496 else
20497 thick = -thick;
20499 if (it->start_of_box_run_p)
20500 it->pixel_width += thick;
20501 if (it->end_of_box_run_p)
20502 it->pixel_width += thick;
20505 /* If face has an overline, add the height of the overline
20506 (1 pixel) and a 1 pixel margin to the character height. */
20507 if (face->overline_p)
20508 it->ascent += overline_margin;
20510 if (it->constrain_row_ascent_descent_p)
20512 if (it->ascent > it->max_ascent)
20513 it->ascent = it->max_ascent;
20514 if (it->descent > it->max_descent)
20515 it->descent = it->max_descent;
20518 take_vertical_position_into_account (it);
20520 /* If we have to actually produce glyphs, do it. */
20521 if (it->glyph_row)
20523 if (stretched_p)
20525 /* Translate a space with a `space-width' property
20526 into a stretch glyph. */
20527 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20528 / FONT_HEIGHT (font));
20529 append_stretch_glyph (it, it->object, it->pixel_width,
20530 it->ascent + it->descent, ascent);
20532 else
20533 append_glyph (it);
20535 /* If characters with lbearing or rbearing are displayed
20536 in this line, record that fact in a flag of the
20537 glyph row. This is used to optimize X output code. */
20538 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20539 it->glyph_row->contains_overlapping_glyphs_p = 1;
20542 else if (it->char_to_display == '\n')
20544 /* A newline has no width but we need the height of the line.
20545 But if previous part of the line set a height, don't
20546 increase that height */
20548 Lisp_Object height;
20549 Lisp_Object total_height = Qnil;
20551 it->override_ascent = -1;
20552 it->pixel_width = 0;
20553 it->nglyphs = 0;
20555 height = get_line_height_property(it, Qline_height);
20556 /* Split (line-height total-height) list */
20557 if (CONSP (height)
20558 && CONSP (XCDR (height))
20559 && NILP (XCDR (XCDR (height))))
20561 total_height = XCAR (XCDR (height));
20562 height = XCAR (height);
20564 height = calc_line_height_property(it, height, font, boff, 1);
20566 if (it->override_ascent >= 0)
20568 it->ascent = it->override_ascent;
20569 it->descent = it->override_descent;
20570 boff = it->override_boff;
20572 else
20574 it->ascent = FONT_BASE (font) + boff;
20575 it->descent = FONT_DESCENT (font) - boff;
20578 if (EQ (height, Qt))
20580 if (it->descent > it->max_descent)
20582 it->ascent += it->descent - it->max_descent;
20583 it->descent = it->max_descent;
20585 if (it->ascent > it->max_ascent)
20587 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20588 it->ascent = it->max_ascent;
20590 it->phys_ascent = min (it->phys_ascent, it->ascent);
20591 it->phys_descent = min (it->phys_descent, it->descent);
20592 it->constrain_row_ascent_descent_p = 1;
20593 extra_line_spacing = 0;
20595 else
20597 Lisp_Object spacing;
20599 it->phys_ascent = it->ascent;
20600 it->phys_descent = it->descent;
20602 if ((it->max_ascent > 0 || it->max_descent > 0)
20603 && face->box != FACE_NO_BOX
20604 && face->box_line_width > 0)
20606 it->ascent += face->box_line_width;
20607 it->descent += face->box_line_width;
20609 if (!NILP (height)
20610 && XINT (height) > it->ascent + it->descent)
20611 it->ascent = XINT (height) - it->descent;
20613 if (!NILP (total_height))
20614 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20615 else
20617 spacing = get_line_height_property(it, Qline_spacing);
20618 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20620 if (INTEGERP (spacing))
20622 extra_line_spacing = XINT (spacing);
20623 if (!NILP (total_height))
20624 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20628 else if (it->char_to_display == '\t')
20630 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20631 int x = it->current_x + it->continuation_lines_width;
20632 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20634 /* If the distance from the current position to the next tab
20635 stop is less than a space character width, use the
20636 tab stop after that. */
20637 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20638 next_tab_x += tab_width;
20640 it->pixel_width = next_tab_x - x;
20641 it->nglyphs = 1;
20642 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20643 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20645 if (it->glyph_row)
20647 append_stretch_glyph (it, it->object, it->pixel_width,
20648 it->ascent + it->descent, it->ascent);
20651 else
20653 /* A multi-byte character. Assume that the display width of the
20654 character is the width of the character multiplied by the
20655 width of the font. */
20657 /* If we found a font, this font should give us the right
20658 metrics. If we didn't find a font, use the frame's
20659 default font and calculate the width of the character
20660 from the charset width; this is what old redisplay code
20661 did. */
20663 pcm = rif->per_char_metric (font, &char2b,
20664 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20666 if (font_not_found_p || !pcm)
20668 int charset = CHAR_CHARSET (it->char_to_display);
20670 it->glyph_not_available_p = 1;
20671 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20672 * CHARSET_WIDTH (charset));
20673 it->phys_ascent = FONT_BASE (font) + boff;
20674 it->phys_descent = FONT_DESCENT (font) - boff;
20676 else
20678 it->pixel_width = pcm->width;
20679 it->phys_ascent = pcm->ascent + boff;
20680 it->phys_descent = pcm->descent - boff;
20681 if (it->glyph_row
20682 && (pcm->lbearing < 0
20683 || pcm->rbearing > pcm->width))
20684 it->glyph_row->contains_overlapping_glyphs_p = 1;
20686 it->nglyphs = 1;
20687 it->ascent = FONT_BASE (font) + boff;
20688 it->descent = FONT_DESCENT (font) - boff;
20689 if (face->box != FACE_NO_BOX)
20691 int thick = face->box_line_width;
20693 if (thick > 0)
20695 it->ascent += thick;
20696 it->descent += thick;
20698 else
20699 thick = - thick;
20701 if (it->start_of_box_run_p)
20702 it->pixel_width += thick;
20703 if (it->end_of_box_run_p)
20704 it->pixel_width += thick;
20707 /* If face has an overline, add the height of the overline
20708 (1 pixel) and a 1 pixel margin to the character height. */
20709 if (face->overline_p)
20710 it->ascent += overline_margin;
20712 take_vertical_position_into_account (it);
20714 if (it->glyph_row)
20715 append_glyph (it);
20717 it->multibyte_p = saved_multibyte_p;
20719 else if (it->what == IT_COMPOSITION)
20721 /* Note: A composition is represented as one glyph in the
20722 glyph matrix. There are no padding glyphs. */
20723 XChar2b char2b;
20724 XFontStruct *font;
20725 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20726 XCharStruct *pcm;
20727 int font_not_found_p;
20728 struct font_info *font_info;
20729 int boff; /* baseline offset */
20730 struct composition *cmp = composition_table[it->cmp_id];
20732 /* Maybe translate single-byte characters to multibyte. */
20733 it->char_to_display = it->c;
20734 if (unibyte_display_via_language_environment
20735 && SINGLE_BYTE_CHAR_P (it->c)
20736 && (it->c >= 0240
20737 || (it->c >= 0200
20738 && !NILP (Vnonascii_translation_table))))
20740 it->char_to_display = unibyte_char_to_multibyte (it->c);
20743 /* Get face and font to use. Encode IT->char_to_display. */
20744 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20745 face = FACE_FROM_ID (it->f, it->face_id);
20746 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20747 &char2b, it->multibyte_p, 0);
20748 font = face->font;
20750 /* When no suitable font found, use the default font. */
20751 font_not_found_p = font == NULL;
20752 if (font_not_found_p)
20754 font = FRAME_FONT (it->f);
20755 boff = FRAME_BASELINE_OFFSET (it->f);
20756 font_info = NULL;
20758 else
20760 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20761 boff = font_info->baseline_offset;
20762 if (font_info->vertical_centering)
20763 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20766 /* There are no padding glyphs, so there is only one glyph to
20767 produce for the composition. Important is that pixel_width,
20768 ascent and descent are the values of what is drawn by
20769 draw_glyphs (i.e. the values of the overall glyphs composed). */
20770 it->nglyphs = 1;
20772 /* If we have not yet calculated pixel size data of glyphs of
20773 the composition for the current face font, calculate them
20774 now. Theoretically, we have to check all fonts for the
20775 glyphs, but that requires much time and memory space. So,
20776 here we check only the font of the first glyph. This leads
20777 to incorrect display very rarely, and C-l (recenter) can
20778 correct the display anyway. */
20779 if (cmp->font != (void *) font)
20781 /* Ascent and descent of the font of the first character of
20782 this composition (adjusted by baseline offset). Ascent
20783 and descent of overall glyphs should not be less than
20784 them respectively. */
20785 int font_ascent = FONT_BASE (font) + boff;
20786 int font_descent = FONT_DESCENT (font) - boff;
20787 /* Bounding box of the overall glyphs. */
20788 int leftmost, rightmost, lowest, highest;
20789 int i, width, ascent, descent;
20791 cmp->font = (void *) font;
20793 /* Initialize the bounding box. */
20794 if (font_info
20795 && (pcm = rif->per_char_metric (font, &char2b,
20796 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20798 width = pcm->width;
20799 ascent = pcm->ascent;
20800 descent = pcm->descent;
20802 else
20804 width = FONT_WIDTH (font);
20805 ascent = FONT_BASE (font);
20806 descent = FONT_DESCENT (font);
20809 rightmost = width;
20810 lowest = - descent + boff;
20811 highest = ascent + boff;
20812 leftmost = 0;
20814 if (font_info
20815 && font_info->default_ascent
20816 && CHAR_TABLE_P (Vuse_default_ascent)
20817 && !NILP (Faref (Vuse_default_ascent,
20818 make_number (it->char_to_display))))
20819 highest = font_info->default_ascent + boff;
20821 /* Draw the first glyph at the normal position. It may be
20822 shifted to right later if some other glyphs are drawn at
20823 the left. */
20824 cmp->offsets[0] = 0;
20825 cmp->offsets[1] = boff;
20827 /* Set cmp->offsets for the remaining glyphs. */
20828 for (i = 1; i < cmp->glyph_len; i++)
20830 int left, right, btm, top;
20831 int ch = COMPOSITION_GLYPH (cmp, i);
20832 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20834 face = FACE_FROM_ID (it->f, face_id);
20835 get_char_face_and_encoding (it->f, ch, face->id,
20836 &char2b, it->multibyte_p, 0);
20837 font = face->font;
20838 if (font == NULL)
20840 font = FRAME_FONT (it->f);
20841 boff = FRAME_BASELINE_OFFSET (it->f);
20842 font_info = NULL;
20844 else
20846 font_info
20847 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20848 boff = font_info->baseline_offset;
20849 if (font_info->vertical_centering)
20850 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20853 if (font_info
20854 && (pcm = rif->per_char_metric (font, &char2b,
20855 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20857 width = pcm->width;
20858 ascent = pcm->ascent;
20859 descent = pcm->descent;
20861 else
20863 width = FONT_WIDTH (font);
20864 ascent = 1;
20865 descent = 0;
20868 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20870 /* Relative composition with or without
20871 alternate chars. */
20872 left = (leftmost + rightmost - width) / 2;
20873 btm = - descent + boff;
20874 if (font_info && font_info->relative_compose
20875 && (! CHAR_TABLE_P (Vignore_relative_composition)
20876 || NILP (Faref (Vignore_relative_composition,
20877 make_number (ch)))))
20880 if (- descent >= font_info->relative_compose)
20881 /* One extra pixel between two glyphs. */
20882 btm = highest + 1;
20883 else if (ascent <= 0)
20884 /* One extra pixel between two glyphs. */
20885 btm = lowest - 1 - ascent - descent;
20888 else
20890 /* A composition rule is specified by an integer
20891 value that encodes global and new reference
20892 points (GREF and NREF). GREF and NREF are
20893 specified by numbers as below:
20895 0---1---2 -- ascent
20899 9--10--11 -- center
20901 ---3---4---5--- baseline
20903 6---7---8 -- descent
20905 int rule = COMPOSITION_RULE (cmp, i);
20906 int gref, nref, grefx, grefy, nrefx, nrefy;
20908 COMPOSITION_DECODE_RULE (rule, gref, nref);
20909 grefx = gref % 3, nrefx = nref % 3;
20910 grefy = gref / 3, nrefy = nref / 3;
20912 left = (leftmost
20913 + grefx * (rightmost - leftmost) / 2
20914 - nrefx * width / 2);
20915 btm = ((grefy == 0 ? highest
20916 : grefy == 1 ? 0
20917 : grefy == 2 ? lowest
20918 : (highest + lowest) / 2)
20919 - (nrefy == 0 ? ascent + descent
20920 : nrefy == 1 ? descent - boff
20921 : nrefy == 2 ? 0
20922 : (ascent + descent) / 2));
20925 cmp->offsets[i * 2] = left;
20926 cmp->offsets[i * 2 + 1] = btm + descent;
20928 /* Update the bounding box of the overall glyphs. */
20929 right = left + width;
20930 top = btm + descent + ascent;
20931 if (left < leftmost)
20932 leftmost = left;
20933 if (right > rightmost)
20934 rightmost = right;
20935 if (top > highest)
20936 highest = top;
20937 if (btm < lowest)
20938 lowest = btm;
20941 /* If there are glyphs whose x-offsets are negative,
20942 shift all glyphs to the right and make all x-offsets
20943 non-negative. */
20944 if (leftmost < 0)
20946 for (i = 0; i < cmp->glyph_len; i++)
20947 cmp->offsets[i * 2] -= leftmost;
20948 rightmost -= leftmost;
20951 cmp->pixel_width = rightmost;
20952 cmp->ascent = highest;
20953 cmp->descent = - lowest;
20954 if (cmp->ascent < font_ascent)
20955 cmp->ascent = font_ascent;
20956 if (cmp->descent < font_descent)
20957 cmp->descent = font_descent;
20960 it->pixel_width = cmp->pixel_width;
20961 it->ascent = it->phys_ascent = cmp->ascent;
20962 it->descent = it->phys_descent = cmp->descent;
20964 if (face->box != FACE_NO_BOX)
20966 int thick = face->box_line_width;
20968 if (thick > 0)
20970 it->ascent += thick;
20971 it->descent += thick;
20973 else
20974 thick = - thick;
20976 if (it->start_of_box_run_p)
20977 it->pixel_width += thick;
20978 if (it->end_of_box_run_p)
20979 it->pixel_width += thick;
20982 /* If face has an overline, add the height of the overline
20983 (1 pixel) and a 1 pixel margin to the character height. */
20984 if (face->overline_p)
20985 it->ascent += overline_margin;
20987 take_vertical_position_into_account (it);
20989 if (it->glyph_row)
20990 append_composite_glyph (it);
20992 else if (it->what == IT_IMAGE)
20993 produce_image_glyph (it);
20994 else if (it->what == IT_STRETCH)
20995 produce_stretch_glyph (it);
20997 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20998 because this isn't true for images with `:ascent 100'. */
20999 xassert (it->ascent >= 0 && it->descent >= 0);
21000 if (it->area == TEXT_AREA)
21001 it->current_x += it->pixel_width;
21003 if (extra_line_spacing > 0)
21005 it->descent += extra_line_spacing;
21006 if (extra_line_spacing > it->max_extra_line_spacing)
21007 it->max_extra_line_spacing = extra_line_spacing;
21010 it->max_ascent = max (it->max_ascent, it->ascent);
21011 it->max_descent = max (it->max_descent, it->descent);
21012 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21013 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21016 /* EXPORT for RIF:
21017 Output LEN glyphs starting at START at the nominal cursor position.
21018 Advance the nominal cursor over the text. The global variable
21019 updated_window contains the window being updated, updated_row is
21020 the glyph row being updated, and updated_area is the area of that
21021 row being updated. */
21023 void
21024 x_write_glyphs (start, len)
21025 struct glyph *start;
21026 int len;
21028 int x, hpos;
21030 xassert (updated_window && updated_row);
21031 BLOCK_INPUT;
21033 /* Write glyphs. */
21035 hpos = start - updated_row->glyphs[updated_area];
21036 x = draw_glyphs (updated_window, output_cursor.x,
21037 updated_row, updated_area,
21038 hpos, hpos + len,
21039 DRAW_NORMAL_TEXT, 0);
21041 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21042 if (updated_area == TEXT_AREA
21043 && updated_window->phys_cursor_on_p
21044 && updated_window->phys_cursor.vpos == output_cursor.vpos
21045 && updated_window->phys_cursor.hpos >= hpos
21046 && updated_window->phys_cursor.hpos < hpos + len)
21047 updated_window->phys_cursor_on_p = 0;
21049 UNBLOCK_INPUT;
21051 /* Advance the output cursor. */
21052 output_cursor.hpos += len;
21053 output_cursor.x = x;
21057 /* EXPORT for RIF:
21058 Insert LEN glyphs from START at the nominal cursor position. */
21060 void
21061 x_insert_glyphs (start, len)
21062 struct glyph *start;
21063 int len;
21065 struct frame *f;
21066 struct window *w;
21067 int line_height, shift_by_width, shifted_region_width;
21068 struct glyph_row *row;
21069 struct glyph *glyph;
21070 int frame_x, frame_y, hpos;
21072 xassert (updated_window && updated_row);
21073 BLOCK_INPUT;
21074 w = updated_window;
21075 f = XFRAME (WINDOW_FRAME (w));
21077 /* Get the height of the line we are in. */
21078 row = updated_row;
21079 line_height = row->height;
21081 /* Get the width of the glyphs to insert. */
21082 shift_by_width = 0;
21083 for (glyph = start; glyph < start + len; ++glyph)
21084 shift_by_width += glyph->pixel_width;
21086 /* Get the width of the region to shift right. */
21087 shifted_region_width = (window_box_width (w, updated_area)
21088 - output_cursor.x
21089 - shift_by_width);
21091 /* Shift right. */
21092 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21093 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21095 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21096 line_height, shift_by_width);
21098 /* Write the glyphs. */
21099 hpos = start - row->glyphs[updated_area];
21100 draw_glyphs (w, output_cursor.x, row, updated_area,
21101 hpos, hpos + len,
21102 DRAW_NORMAL_TEXT, 0);
21104 /* Advance the output cursor. */
21105 output_cursor.hpos += len;
21106 output_cursor.x += shift_by_width;
21107 UNBLOCK_INPUT;
21111 /* EXPORT for RIF:
21112 Erase the current text line from the nominal cursor position
21113 (inclusive) to pixel column TO_X (exclusive). The idea is that
21114 everything from TO_X onward is already erased.
21116 TO_X is a pixel position relative to updated_area of
21117 updated_window. TO_X == -1 means clear to the end of this area. */
21119 void
21120 x_clear_end_of_line (to_x)
21121 int to_x;
21123 struct frame *f;
21124 struct window *w = updated_window;
21125 int max_x, min_y, max_y;
21126 int from_x, from_y, to_y;
21128 xassert (updated_window && updated_row);
21129 f = XFRAME (w->frame);
21131 if (updated_row->full_width_p)
21132 max_x = WINDOW_TOTAL_WIDTH (w);
21133 else
21134 max_x = window_box_width (w, updated_area);
21135 max_y = window_text_bottom_y (w);
21137 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21138 of window. For TO_X > 0, truncate to end of drawing area. */
21139 if (to_x == 0)
21140 return;
21141 else if (to_x < 0)
21142 to_x = max_x;
21143 else
21144 to_x = min (to_x, max_x);
21146 to_y = min (max_y, output_cursor.y + updated_row->height);
21148 /* Notice if the cursor will be cleared by this operation. */
21149 if (!updated_row->full_width_p)
21150 notice_overwritten_cursor (w, updated_area,
21151 output_cursor.x, -1,
21152 updated_row->y,
21153 MATRIX_ROW_BOTTOM_Y (updated_row));
21155 from_x = output_cursor.x;
21157 /* Translate to frame coordinates. */
21158 if (updated_row->full_width_p)
21160 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21161 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21163 else
21165 int area_left = window_box_left (w, updated_area);
21166 from_x += area_left;
21167 to_x += area_left;
21170 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21171 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21172 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21174 /* Prevent inadvertently clearing to end of the X window. */
21175 if (to_x > from_x && to_y > from_y)
21177 BLOCK_INPUT;
21178 rif->clear_frame_area (f, from_x, from_y,
21179 to_x - from_x, to_y - from_y);
21180 UNBLOCK_INPUT;
21184 #endif /* HAVE_WINDOW_SYSTEM */
21188 /***********************************************************************
21189 Cursor types
21190 ***********************************************************************/
21192 /* Value is the internal representation of the specified cursor type
21193 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21194 of the bar cursor. */
21196 static enum text_cursor_kinds
21197 get_specified_cursor_type (arg, width)
21198 Lisp_Object arg;
21199 int *width;
21201 enum text_cursor_kinds type;
21203 if (NILP (arg))
21204 return NO_CURSOR;
21206 if (EQ (arg, Qbox))
21207 return FILLED_BOX_CURSOR;
21209 if (EQ (arg, Qhollow))
21210 return HOLLOW_BOX_CURSOR;
21212 if (EQ (arg, Qbar))
21214 *width = 2;
21215 return BAR_CURSOR;
21218 if (CONSP (arg)
21219 && EQ (XCAR (arg), Qbar)
21220 && INTEGERP (XCDR (arg))
21221 && XINT (XCDR (arg)) >= 0)
21223 *width = XINT (XCDR (arg));
21224 return BAR_CURSOR;
21227 if (EQ (arg, Qhbar))
21229 *width = 2;
21230 return HBAR_CURSOR;
21233 if (CONSP (arg)
21234 && EQ (XCAR (arg), Qhbar)
21235 && INTEGERP (XCDR (arg))
21236 && XINT (XCDR (arg)) >= 0)
21238 *width = XINT (XCDR (arg));
21239 return HBAR_CURSOR;
21242 /* Treat anything unknown as "hollow box cursor".
21243 It was bad to signal an error; people have trouble fixing
21244 .Xdefaults with Emacs, when it has something bad in it. */
21245 type = HOLLOW_BOX_CURSOR;
21247 return type;
21250 /* Set the default cursor types for specified frame. */
21251 void
21252 set_frame_cursor_types (f, arg)
21253 struct frame *f;
21254 Lisp_Object arg;
21256 int width;
21257 Lisp_Object tem;
21259 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21260 FRAME_CURSOR_WIDTH (f) = width;
21262 /* By default, set up the blink-off state depending on the on-state. */
21264 tem = Fassoc (arg, Vblink_cursor_alist);
21265 if (!NILP (tem))
21267 FRAME_BLINK_OFF_CURSOR (f)
21268 = get_specified_cursor_type (XCDR (tem), &width);
21269 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21271 else
21272 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21276 /* Return the cursor we want to be displayed in window W. Return
21277 width of bar/hbar cursor through WIDTH arg. Return with
21278 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21279 (i.e. if the `system caret' should track this cursor).
21281 In a mini-buffer window, we want the cursor only to appear if we
21282 are reading input from this window. For the selected window, we
21283 want the cursor type given by the frame parameter or buffer local
21284 setting of cursor-type. If explicitly marked off, draw no cursor.
21285 In all other cases, we want a hollow box cursor. */
21287 static enum text_cursor_kinds
21288 get_window_cursor_type (w, glyph, width, active_cursor)
21289 struct window *w;
21290 struct glyph *glyph;
21291 int *width;
21292 int *active_cursor;
21294 struct frame *f = XFRAME (w->frame);
21295 struct buffer *b = XBUFFER (w->buffer);
21296 int cursor_type = DEFAULT_CURSOR;
21297 Lisp_Object alt_cursor;
21298 int non_selected = 0;
21300 *active_cursor = 1;
21302 /* Echo area */
21303 if (cursor_in_echo_area
21304 && FRAME_HAS_MINIBUF_P (f)
21305 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21307 if (w == XWINDOW (echo_area_window))
21309 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21311 *width = FRAME_CURSOR_WIDTH (f);
21312 return FRAME_DESIRED_CURSOR (f);
21314 else
21315 return get_specified_cursor_type (b->cursor_type, width);
21318 *active_cursor = 0;
21319 non_selected = 1;
21322 /* Detect a nonselected window or nonselected frame. */
21323 else if (w != XWINDOW (f->selected_window)
21324 #ifdef HAVE_WINDOW_SYSTEM
21325 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21326 #endif
21329 *active_cursor = 0;
21331 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21332 return NO_CURSOR;
21334 non_selected = 1;
21337 /* Never display a cursor in a window in which cursor-type is nil. */
21338 if (NILP (b->cursor_type))
21339 return NO_CURSOR;
21341 /* Get the normal cursor type for this window. */
21342 if (EQ (b->cursor_type, Qt))
21344 cursor_type = FRAME_DESIRED_CURSOR (f);
21345 *width = FRAME_CURSOR_WIDTH (f);
21347 else
21348 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21350 /* Use cursor-in-non-selected-windows instead
21351 for non-selected window or frame. */
21352 if (non_selected)
21354 alt_cursor = b->cursor_in_non_selected_windows;
21355 if (!EQ (Qt, alt_cursor))
21356 return get_specified_cursor_type (alt_cursor, width);
21357 /* t means modify the normal cursor type. */
21358 if (cursor_type == FILLED_BOX_CURSOR)
21359 cursor_type = HOLLOW_BOX_CURSOR;
21360 else if (cursor_type == BAR_CURSOR && *width > 1)
21361 --*width;
21362 return cursor_type;
21365 /* Use normal cursor if not blinked off. */
21366 if (!w->cursor_off_p)
21368 #ifdef HAVE_WINDOW_SYSTEM
21369 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21371 if (cursor_type == FILLED_BOX_CURSOR)
21373 /* Using a block cursor on large images can be very annoying.
21374 So use a hollow cursor for "large" images.
21375 If image is not transparent (no mask), also use hollow cursor. */
21376 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21377 if (img != NULL && IMAGEP (img->spec))
21379 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21380 where N = size of default frame font size.
21381 This should cover most of the "tiny" icons people may use. */
21382 if (!img->mask
21383 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21384 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21385 cursor_type = HOLLOW_BOX_CURSOR;
21388 else if (cursor_type != NO_CURSOR)
21390 /* Display current only supports BOX and HOLLOW cursors for images.
21391 So for now, unconditionally use a HOLLOW cursor when cursor is
21392 not a solid box cursor. */
21393 cursor_type = HOLLOW_BOX_CURSOR;
21396 #endif
21397 return cursor_type;
21400 /* Cursor is blinked off, so determine how to "toggle" it. */
21402 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21403 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21404 return get_specified_cursor_type (XCDR (alt_cursor), width);
21406 /* Then see if frame has specified a specific blink off cursor type. */
21407 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21409 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21410 return FRAME_BLINK_OFF_CURSOR (f);
21413 #if 0
21414 /* Some people liked having a permanently visible blinking cursor,
21415 while others had very strong opinions against it. So it was
21416 decided to remove it. KFS 2003-09-03 */
21418 /* Finally perform built-in cursor blinking:
21419 filled box <-> hollow box
21420 wide [h]bar <-> narrow [h]bar
21421 narrow [h]bar <-> no cursor
21422 other type <-> no cursor */
21424 if (cursor_type == FILLED_BOX_CURSOR)
21425 return HOLLOW_BOX_CURSOR;
21427 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21429 *width = 1;
21430 return cursor_type;
21432 #endif
21434 return NO_CURSOR;
21438 #ifdef HAVE_WINDOW_SYSTEM
21440 /* Notice when the text cursor of window W has been completely
21441 overwritten by a drawing operation that outputs glyphs in AREA
21442 starting at X0 and ending at X1 in the line starting at Y0 and
21443 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21444 the rest of the line after X0 has been written. Y coordinates
21445 are window-relative. */
21447 static void
21448 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21449 struct window *w;
21450 enum glyph_row_area area;
21451 int x0, y0, x1, y1;
21453 int cx0, cx1, cy0, cy1;
21454 struct glyph_row *row;
21456 if (!w->phys_cursor_on_p)
21457 return;
21458 if (area != TEXT_AREA)
21459 return;
21461 if (w->phys_cursor.vpos < 0
21462 || w->phys_cursor.vpos >= w->current_matrix->nrows
21463 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21464 !(row->enabled_p && row->displays_text_p)))
21465 return;
21467 if (row->cursor_in_fringe_p)
21469 row->cursor_in_fringe_p = 0;
21470 draw_fringe_bitmap (w, row, 0);
21471 w->phys_cursor_on_p = 0;
21472 return;
21475 cx0 = w->phys_cursor.x;
21476 cx1 = cx0 + w->phys_cursor_width;
21477 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21478 return;
21480 /* The cursor image will be completely removed from the
21481 screen if the output area intersects the cursor area in
21482 y-direction. When we draw in [y0 y1[, and some part of
21483 the cursor is at y < y0, that part must have been drawn
21484 before. When scrolling, the cursor is erased before
21485 actually scrolling, so we don't come here. When not
21486 scrolling, the rows above the old cursor row must have
21487 changed, and in this case these rows must have written
21488 over the cursor image.
21490 Likewise if part of the cursor is below y1, with the
21491 exception of the cursor being in the first blank row at
21492 the buffer and window end because update_text_area
21493 doesn't draw that row. (Except when it does, but
21494 that's handled in update_text_area.) */
21496 cy0 = w->phys_cursor.y;
21497 cy1 = cy0 + w->phys_cursor_height;
21498 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21499 return;
21501 w->phys_cursor_on_p = 0;
21504 #endif /* HAVE_WINDOW_SYSTEM */
21507 /************************************************************************
21508 Mouse Face
21509 ************************************************************************/
21511 #ifdef HAVE_WINDOW_SYSTEM
21513 /* EXPORT for RIF:
21514 Fix the display of area AREA of overlapping row ROW in window W
21515 with respect to the overlapping part OVERLAPS. */
21517 void
21518 x_fix_overlapping_area (w, row, area, overlaps)
21519 struct window *w;
21520 struct glyph_row *row;
21521 enum glyph_row_area area;
21522 int overlaps;
21524 int i, x;
21526 BLOCK_INPUT;
21528 x = 0;
21529 for (i = 0; i < row->used[area];)
21531 if (row->glyphs[area][i].overlaps_vertically_p)
21533 int start = i, start_x = x;
21537 x += row->glyphs[area][i].pixel_width;
21538 ++i;
21540 while (i < row->used[area]
21541 && row->glyphs[area][i].overlaps_vertically_p);
21543 draw_glyphs (w, start_x, row, area,
21544 start, i,
21545 DRAW_NORMAL_TEXT, overlaps);
21547 else
21549 x += row->glyphs[area][i].pixel_width;
21550 ++i;
21554 UNBLOCK_INPUT;
21558 /* EXPORT:
21559 Draw the cursor glyph of window W in glyph row ROW. See the
21560 comment of draw_glyphs for the meaning of HL. */
21562 void
21563 draw_phys_cursor_glyph (w, row, hl)
21564 struct window *w;
21565 struct glyph_row *row;
21566 enum draw_glyphs_face hl;
21568 /* If cursor hpos is out of bounds, don't draw garbage. This can
21569 happen in mini-buffer windows when switching between echo area
21570 glyphs and mini-buffer. */
21571 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21573 int on_p = w->phys_cursor_on_p;
21574 int x1;
21575 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21576 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21577 hl, 0);
21578 w->phys_cursor_on_p = on_p;
21580 if (hl == DRAW_CURSOR)
21581 w->phys_cursor_width = x1 - w->phys_cursor.x;
21582 /* When we erase the cursor, and ROW is overlapped by other
21583 rows, make sure that these overlapping parts of other rows
21584 are redrawn. */
21585 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21587 w->phys_cursor_width = x1 - w->phys_cursor.x;
21589 if (row > w->current_matrix->rows
21590 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21591 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21592 OVERLAPS_ERASED_CURSOR);
21594 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21595 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21596 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21597 OVERLAPS_ERASED_CURSOR);
21603 /* EXPORT:
21604 Erase the image of a cursor of window W from the screen. */
21606 void
21607 erase_phys_cursor (w)
21608 struct window *w;
21610 struct frame *f = XFRAME (w->frame);
21611 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21612 int hpos = w->phys_cursor.hpos;
21613 int vpos = w->phys_cursor.vpos;
21614 int mouse_face_here_p = 0;
21615 struct glyph_matrix *active_glyphs = w->current_matrix;
21616 struct glyph_row *cursor_row;
21617 struct glyph *cursor_glyph;
21618 enum draw_glyphs_face hl;
21620 /* No cursor displayed or row invalidated => nothing to do on the
21621 screen. */
21622 if (w->phys_cursor_type == NO_CURSOR)
21623 goto mark_cursor_off;
21625 /* VPOS >= active_glyphs->nrows means that window has been resized.
21626 Don't bother to erase the cursor. */
21627 if (vpos >= active_glyphs->nrows)
21628 goto mark_cursor_off;
21630 /* If row containing cursor is marked invalid, there is nothing we
21631 can do. */
21632 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21633 if (!cursor_row->enabled_p)
21634 goto mark_cursor_off;
21636 /* If line spacing is > 0, old cursor may only be partially visible in
21637 window after split-window. So adjust visible height. */
21638 cursor_row->visible_height = min (cursor_row->visible_height,
21639 window_text_bottom_y (w) - cursor_row->y);
21641 /* If row is completely invisible, don't attempt to delete a cursor which
21642 isn't there. This can happen if cursor is at top of a window, and
21643 we switch to a buffer with a header line in that window. */
21644 if (cursor_row->visible_height <= 0)
21645 goto mark_cursor_off;
21647 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21648 if (cursor_row->cursor_in_fringe_p)
21650 cursor_row->cursor_in_fringe_p = 0;
21651 draw_fringe_bitmap (w, cursor_row, 0);
21652 goto mark_cursor_off;
21655 /* This can happen when the new row is shorter than the old one.
21656 In this case, either draw_glyphs or clear_end_of_line
21657 should have cleared the cursor. Note that we wouldn't be
21658 able to erase the cursor in this case because we don't have a
21659 cursor glyph at hand. */
21660 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21661 goto mark_cursor_off;
21663 /* If the cursor is in the mouse face area, redisplay that when
21664 we clear the cursor. */
21665 if (! NILP (dpyinfo->mouse_face_window)
21666 && w == XWINDOW (dpyinfo->mouse_face_window)
21667 && (vpos > dpyinfo->mouse_face_beg_row
21668 || (vpos == dpyinfo->mouse_face_beg_row
21669 && hpos >= dpyinfo->mouse_face_beg_col))
21670 && (vpos < dpyinfo->mouse_face_end_row
21671 || (vpos == dpyinfo->mouse_face_end_row
21672 && hpos < dpyinfo->mouse_face_end_col))
21673 /* Don't redraw the cursor's spot in mouse face if it is at the
21674 end of a line (on a newline). The cursor appears there, but
21675 mouse highlighting does not. */
21676 && cursor_row->used[TEXT_AREA] > hpos)
21677 mouse_face_here_p = 1;
21679 /* Maybe clear the display under the cursor. */
21680 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21682 int x, y, left_x;
21683 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21684 int width;
21686 cursor_glyph = get_phys_cursor_glyph (w);
21687 if (cursor_glyph == NULL)
21688 goto mark_cursor_off;
21690 width = cursor_glyph->pixel_width;
21691 left_x = window_box_left_offset (w, TEXT_AREA);
21692 x = w->phys_cursor.x;
21693 if (x < left_x)
21694 width -= left_x - x;
21695 width = min (width, window_box_width (w, TEXT_AREA) - x);
21696 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21697 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21699 if (width > 0)
21700 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21703 /* Erase the cursor by redrawing the character underneath it. */
21704 if (mouse_face_here_p)
21705 hl = DRAW_MOUSE_FACE;
21706 else
21707 hl = DRAW_NORMAL_TEXT;
21708 draw_phys_cursor_glyph (w, cursor_row, hl);
21710 mark_cursor_off:
21711 w->phys_cursor_on_p = 0;
21712 w->phys_cursor_type = NO_CURSOR;
21716 /* EXPORT:
21717 Display or clear cursor of window W. If ON is zero, clear the
21718 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21719 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21721 void
21722 display_and_set_cursor (w, on, hpos, vpos, x, y)
21723 struct window *w;
21724 int on, hpos, vpos, x, y;
21726 struct frame *f = XFRAME (w->frame);
21727 int new_cursor_type;
21728 int new_cursor_width;
21729 int active_cursor;
21730 struct glyph_row *glyph_row;
21731 struct glyph *glyph;
21733 /* This is pointless on invisible frames, and dangerous on garbaged
21734 windows and frames; in the latter case, the frame or window may
21735 be in the midst of changing its size, and x and y may be off the
21736 window. */
21737 if (! FRAME_VISIBLE_P (f)
21738 || FRAME_GARBAGED_P (f)
21739 || vpos >= w->current_matrix->nrows
21740 || hpos >= w->current_matrix->matrix_w)
21741 return;
21743 /* If cursor is off and we want it off, return quickly. */
21744 if (!on && !w->phys_cursor_on_p)
21745 return;
21747 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21748 /* If cursor row is not enabled, we don't really know where to
21749 display the cursor. */
21750 if (!glyph_row->enabled_p)
21752 w->phys_cursor_on_p = 0;
21753 return;
21756 glyph = NULL;
21757 if (!glyph_row->exact_window_width_line_p
21758 || hpos < glyph_row->used[TEXT_AREA])
21759 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21761 xassert (interrupt_input_blocked);
21763 /* Set new_cursor_type to the cursor we want to be displayed. */
21764 new_cursor_type = get_window_cursor_type (w, glyph,
21765 &new_cursor_width, &active_cursor);
21767 /* If cursor is currently being shown and we don't want it to be or
21768 it is in the wrong place, or the cursor type is not what we want,
21769 erase it. */
21770 if (w->phys_cursor_on_p
21771 && (!on
21772 || w->phys_cursor.x != x
21773 || w->phys_cursor.y != y
21774 || new_cursor_type != w->phys_cursor_type
21775 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21776 && new_cursor_width != w->phys_cursor_width)))
21777 erase_phys_cursor (w);
21779 /* Don't check phys_cursor_on_p here because that flag is only set
21780 to zero in some cases where we know that the cursor has been
21781 completely erased, to avoid the extra work of erasing the cursor
21782 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21783 still not be visible, or it has only been partly erased. */
21784 if (on)
21786 w->phys_cursor_ascent = glyph_row->ascent;
21787 w->phys_cursor_height = glyph_row->height;
21789 /* Set phys_cursor_.* before x_draw_.* is called because some
21790 of them may need the information. */
21791 w->phys_cursor.x = x;
21792 w->phys_cursor.y = glyph_row->y;
21793 w->phys_cursor.hpos = hpos;
21794 w->phys_cursor.vpos = vpos;
21797 rif->draw_window_cursor (w, glyph_row, x, y,
21798 new_cursor_type, new_cursor_width,
21799 on, active_cursor);
21803 /* Switch the display of W's cursor on or off, according to the value
21804 of ON. */
21806 static void
21807 update_window_cursor (w, on)
21808 struct window *w;
21809 int on;
21811 /* Don't update cursor in windows whose frame is in the process
21812 of being deleted. */
21813 if (w->current_matrix)
21815 BLOCK_INPUT;
21816 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21817 w->phys_cursor.x, w->phys_cursor.y);
21818 UNBLOCK_INPUT;
21823 /* Call update_window_cursor with parameter ON_P on all leaf windows
21824 in the window tree rooted at W. */
21826 static void
21827 update_cursor_in_window_tree (w, on_p)
21828 struct window *w;
21829 int on_p;
21831 while (w)
21833 if (!NILP (w->hchild))
21834 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21835 else if (!NILP (w->vchild))
21836 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21837 else
21838 update_window_cursor (w, on_p);
21840 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21845 /* EXPORT:
21846 Display the cursor on window W, or clear it, according to ON_P.
21847 Don't change the cursor's position. */
21849 void
21850 x_update_cursor (f, on_p)
21851 struct frame *f;
21852 int on_p;
21854 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21858 /* EXPORT:
21859 Clear the cursor of window W to background color, and mark the
21860 cursor as not shown. This is used when the text where the cursor
21861 is is about to be rewritten. */
21863 void
21864 x_clear_cursor (w)
21865 struct window *w;
21867 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21868 update_window_cursor (w, 0);
21872 /* EXPORT:
21873 Display the active region described by mouse_face_* according to DRAW. */
21875 void
21876 show_mouse_face (dpyinfo, draw)
21877 Display_Info *dpyinfo;
21878 enum draw_glyphs_face draw;
21880 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21881 struct frame *f = XFRAME (WINDOW_FRAME (w));
21883 if (/* If window is in the process of being destroyed, don't bother
21884 to do anything. */
21885 w->current_matrix != NULL
21886 /* Don't update mouse highlight if hidden */
21887 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21888 /* Recognize when we are called to operate on rows that don't exist
21889 anymore. This can happen when a window is split. */
21890 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21892 int phys_cursor_on_p = w->phys_cursor_on_p;
21893 struct glyph_row *row, *first, *last;
21895 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21896 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21898 for (row = first; row <= last && row->enabled_p; ++row)
21900 int start_hpos, end_hpos, start_x;
21902 /* For all but the first row, the highlight starts at column 0. */
21903 if (row == first)
21905 start_hpos = dpyinfo->mouse_face_beg_col;
21906 start_x = dpyinfo->mouse_face_beg_x;
21908 else
21910 start_hpos = 0;
21911 start_x = 0;
21914 if (row == last)
21915 end_hpos = dpyinfo->mouse_face_end_col;
21916 else
21918 end_hpos = row->used[TEXT_AREA];
21919 if (draw == DRAW_NORMAL_TEXT)
21920 row->fill_line_p = 1; /* Clear to end of line */
21923 if (end_hpos > start_hpos)
21925 draw_glyphs (w, start_x, row, TEXT_AREA,
21926 start_hpos, end_hpos,
21927 draw, 0);
21929 row->mouse_face_p
21930 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21934 /* When we've written over the cursor, arrange for it to
21935 be displayed again. */
21936 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21938 BLOCK_INPUT;
21939 display_and_set_cursor (w, 1,
21940 w->phys_cursor.hpos, w->phys_cursor.vpos,
21941 w->phys_cursor.x, w->phys_cursor.y);
21942 UNBLOCK_INPUT;
21946 /* Change the mouse cursor. */
21947 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
21948 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21949 else if (draw == DRAW_MOUSE_FACE)
21950 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21951 else
21952 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21955 /* EXPORT:
21956 Clear out the mouse-highlighted active region.
21957 Redraw it un-highlighted first. Value is non-zero if mouse
21958 face was actually drawn unhighlighted. */
21961 clear_mouse_face (dpyinfo)
21962 Display_Info *dpyinfo;
21964 int cleared = 0;
21966 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21968 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21969 cleared = 1;
21972 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21973 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21974 dpyinfo->mouse_face_window = Qnil;
21975 dpyinfo->mouse_face_overlay = Qnil;
21976 return cleared;
21980 /* EXPORT:
21981 Non-zero if physical cursor of window W is within mouse face. */
21984 cursor_in_mouse_face_p (w)
21985 struct window *w;
21987 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21988 int in_mouse_face = 0;
21990 if (WINDOWP (dpyinfo->mouse_face_window)
21991 && XWINDOW (dpyinfo->mouse_face_window) == w)
21993 int hpos = w->phys_cursor.hpos;
21994 int vpos = w->phys_cursor.vpos;
21996 if (vpos >= dpyinfo->mouse_face_beg_row
21997 && vpos <= dpyinfo->mouse_face_end_row
21998 && (vpos > dpyinfo->mouse_face_beg_row
21999 || hpos >= dpyinfo->mouse_face_beg_col)
22000 && (vpos < dpyinfo->mouse_face_end_row
22001 || hpos < dpyinfo->mouse_face_end_col
22002 || dpyinfo->mouse_face_past_end))
22003 in_mouse_face = 1;
22006 return in_mouse_face;
22012 /* Find the glyph matrix position of buffer position CHARPOS in window
22013 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22014 current glyphs must be up to date. If CHARPOS is above window
22015 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22016 of last line in W. In the row containing CHARPOS, stop before glyphs
22017 having STOP as object. */
22019 #if 1 /* This is a version of fast_find_position that's more correct
22020 in the presence of hscrolling, for example. I didn't install
22021 it right away because the problem fixed is minor, it failed
22022 in 20.x as well, and I think it's too risky to install
22023 so near the release of 21.1. 2001-09-25 gerd. */
22025 static int
22026 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22027 struct window *w;
22028 int charpos;
22029 int *hpos, *vpos, *x, *y;
22030 Lisp_Object stop;
22032 struct glyph_row *row, *first;
22033 struct glyph *glyph, *end;
22034 int past_end = 0;
22036 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22037 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22039 *x = first->x;
22040 *y = first->y;
22041 *hpos = 0;
22042 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22043 return 1;
22046 row = row_containing_pos (w, charpos, first, NULL, 0);
22047 if (row == NULL)
22049 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22050 past_end = 1;
22053 /* If whole rows or last part of a row came from a display overlay,
22054 row_containing_pos will skip over such rows because their end pos
22055 equals the start pos of the overlay or interval.
22057 Move back if we have a STOP object and previous row's
22058 end glyph came from STOP. */
22059 if (!NILP (stop))
22061 struct glyph_row *prev;
22062 while ((prev = row - 1, prev >= first)
22063 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22064 && prev->used[TEXT_AREA] > 0)
22066 struct glyph *beg = prev->glyphs[TEXT_AREA];
22067 glyph = beg + prev->used[TEXT_AREA];
22068 while (--glyph >= beg
22069 && INTEGERP (glyph->object));
22070 if (glyph < beg
22071 || !EQ (stop, glyph->object))
22072 break;
22073 row = prev;
22077 *x = row->x;
22078 *y = row->y;
22079 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22081 glyph = row->glyphs[TEXT_AREA];
22082 end = glyph + row->used[TEXT_AREA];
22084 /* Skip over glyphs not having an object at the start of the row.
22085 These are special glyphs like truncation marks on terminal
22086 frames. */
22087 if (row->displays_text_p)
22088 while (glyph < end
22089 && INTEGERP (glyph->object)
22090 && !EQ (stop, glyph->object)
22091 && glyph->charpos < 0)
22093 *x += glyph->pixel_width;
22094 ++glyph;
22097 while (glyph < end
22098 && !INTEGERP (glyph->object)
22099 && !EQ (stop, glyph->object)
22100 && (!BUFFERP (glyph->object)
22101 || glyph->charpos < charpos))
22103 *x += glyph->pixel_width;
22104 ++glyph;
22107 *hpos = glyph - row->glyphs[TEXT_AREA];
22108 return !past_end;
22111 #else /* not 1 */
22113 static int
22114 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22115 struct window *w;
22116 int pos;
22117 int *hpos, *vpos, *x, *y;
22118 Lisp_Object stop;
22120 int i;
22121 int lastcol;
22122 int maybe_next_line_p = 0;
22123 int line_start_position;
22124 int yb = window_text_bottom_y (w);
22125 struct glyph_row *row, *best_row;
22126 int row_vpos, best_row_vpos;
22127 int current_x;
22129 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22130 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22132 while (row->y < yb)
22134 if (row->used[TEXT_AREA])
22135 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22136 else
22137 line_start_position = 0;
22139 if (line_start_position > pos)
22140 break;
22141 /* If the position sought is the end of the buffer,
22142 don't include the blank lines at the bottom of the window. */
22143 else if (line_start_position == pos
22144 && pos == BUF_ZV (XBUFFER (w->buffer)))
22146 maybe_next_line_p = 1;
22147 break;
22149 else if (line_start_position > 0)
22151 best_row = row;
22152 best_row_vpos = row_vpos;
22155 if (row->y + row->height >= yb)
22156 break;
22158 ++row;
22159 ++row_vpos;
22162 /* Find the right column within BEST_ROW. */
22163 lastcol = 0;
22164 current_x = best_row->x;
22165 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22167 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22168 int charpos = glyph->charpos;
22170 if (BUFFERP (glyph->object))
22172 if (charpos == pos)
22174 *hpos = i;
22175 *vpos = best_row_vpos;
22176 *x = current_x;
22177 *y = best_row->y;
22178 return 1;
22180 else if (charpos > pos)
22181 break;
22183 else if (EQ (glyph->object, stop))
22184 break;
22186 if (charpos > 0)
22187 lastcol = i;
22188 current_x += glyph->pixel_width;
22191 /* If we're looking for the end of the buffer,
22192 and we didn't find it in the line we scanned,
22193 use the start of the following line. */
22194 if (maybe_next_line_p)
22196 ++best_row;
22197 ++best_row_vpos;
22198 lastcol = 0;
22199 current_x = best_row->x;
22202 *vpos = best_row_vpos;
22203 *hpos = lastcol + 1;
22204 *x = current_x;
22205 *y = best_row->y;
22206 return 0;
22209 #endif /* not 1 */
22212 /* Find the position of the glyph for position POS in OBJECT in
22213 window W's current matrix, and return in *X, *Y the pixel
22214 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22216 RIGHT_P non-zero means return the position of the right edge of the
22217 glyph, RIGHT_P zero means return the left edge position.
22219 If no glyph for POS exists in the matrix, return the position of
22220 the glyph with the next smaller position that is in the matrix, if
22221 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22222 exists in the matrix, return the position of the glyph with the
22223 next larger position in OBJECT.
22225 Value is non-zero if a glyph was found. */
22227 static int
22228 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22229 struct window *w;
22230 int pos;
22231 Lisp_Object object;
22232 int *hpos, *vpos, *x, *y;
22233 int right_p;
22235 int yb = window_text_bottom_y (w);
22236 struct glyph_row *r;
22237 struct glyph *best_glyph = NULL;
22238 struct glyph_row *best_row = NULL;
22239 int best_x = 0;
22241 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22242 r->enabled_p && r->y < yb;
22243 ++r)
22245 struct glyph *g = r->glyphs[TEXT_AREA];
22246 struct glyph *e = g + r->used[TEXT_AREA];
22247 int gx;
22249 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22250 if (EQ (g->object, object))
22252 if (g->charpos == pos)
22254 best_glyph = g;
22255 best_x = gx;
22256 best_row = r;
22257 goto found;
22259 else if (best_glyph == NULL
22260 || ((abs (g->charpos - pos)
22261 < abs (best_glyph->charpos - pos))
22262 && (right_p
22263 ? g->charpos < pos
22264 : g->charpos > pos)))
22266 best_glyph = g;
22267 best_x = gx;
22268 best_row = r;
22273 found:
22275 if (best_glyph)
22277 *x = best_x;
22278 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22280 if (right_p)
22282 *x += best_glyph->pixel_width;
22283 ++*hpos;
22286 *y = best_row->y;
22287 *vpos = best_row - w->current_matrix->rows;
22290 return best_glyph != NULL;
22294 /* See if position X, Y is within a hot-spot of an image. */
22296 static int
22297 on_hot_spot_p (hot_spot, x, y)
22298 Lisp_Object hot_spot;
22299 int x, y;
22301 if (!CONSP (hot_spot))
22302 return 0;
22304 if (EQ (XCAR (hot_spot), Qrect))
22306 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22307 Lisp_Object rect = XCDR (hot_spot);
22308 Lisp_Object tem;
22309 if (!CONSP (rect))
22310 return 0;
22311 if (!CONSP (XCAR (rect)))
22312 return 0;
22313 if (!CONSP (XCDR (rect)))
22314 return 0;
22315 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22316 return 0;
22317 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22318 return 0;
22319 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22320 return 0;
22321 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22322 return 0;
22323 return 1;
22325 else if (EQ (XCAR (hot_spot), Qcircle))
22327 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22328 Lisp_Object circ = XCDR (hot_spot);
22329 Lisp_Object lr, lx0, ly0;
22330 if (CONSP (circ)
22331 && CONSP (XCAR (circ))
22332 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22333 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22334 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22336 double r = XFLOATINT (lr);
22337 double dx = XINT (lx0) - x;
22338 double dy = XINT (ly0) - y;
22339 return (dx * dx + dy * dy <= r * r);
22342 else if (EQ (XCAR (hot_spot), Qpoly))
22344 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22345 if (VECTORP (XCDR (hot_spot)))
22347 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22348 Lisp_Object *poly = v->contents;
22349 int n = v->size;
22350 int i;
22351 int inside = 0;
22352 Lisp_Object lx, ly;
22353 int x0, y0;
22355 /* Need an even number of coordinates, and at least 3 edges. */
22356 if (n < 6 || n & 1)
22357 return 0;
22359 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22360 If count is odd, we are inside polygon. Pixels on edges
22361 may or may not be included depending on actual geometry of the
22362 polygon. */
22363 if ((lx = poly[n-2], !INTEGERP (lx))
22364 || (ly = poly[n-1], !INTEGERP (lx)))
22365 return 0;
22366 x0 = XINT (lx), y0 = XINT (ly);
22367 for (i = 0; i < n; i += 2)
22369 int x1 = x0, y1 = y0;
22370 if ((lx = poly[i], !INTEGERP (lx))
22371 || (ly = poly[i+1], !INTEGERP (ly)))
22372 return 0;
22373 x0 = XINT (lx), y0 = XINT (ly);
22375 /* Does this segment cross the X line? */
22376 if (x0 >= x)
22378 if (x1 >= x)
22379 continue;
22381 else if (x1 < x)
22382 continue;
22383 if (y > y0 && y > y1)
22384 continue;
22385 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22386 inside = !inside;
22388 return inside;
22391 /* If we don't understand the format, pretend we're not in the hot-spot. */
22392 return 0;
22395 Lisp_Object
22396 find_hot_spot (map, x, y)
22397 Lisp_Object map;
22398 int x, y;
22400 while (CONSP (map))
22402 if (CONSP (XCAR (map))
22403 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22404 return XCAR (map);
22405 map = XCDR (map);
22408 return Qnil;
22411 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22412 3, 3, 0,
22413 doc: /* Lookup in image map MAP coordinates X and Y.
22414 An image map is an alist where each element has the format (AREA ID PLIST).
22415 An AREA is specified as either a rectangle, a circle, or a polygon:
22416 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22417 pixel coordinates of the upper left and bottom right corners.
22418 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22419 and the radius of the circle; r may be a float or integer.
22420 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22421 vector describes one corner in the polygon.
22422 Returns the alist element for the first matching AREA in MAP. */)
22423 (map, x, y)
22424 Lisp_Object map;
22425 Lisp_Object x, y;
22427 if (NILP (map))
22428 return Qnil;
22430 CHECK_NUMBER (x);
22431 CHECK_NUMBER (y);
22433 return find_hot_spot (map, XINT (x), XINT (y));
22437 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22438 static void
22439 define_frame_cursor1 (f, cursor, pointer)
22440 struct frame *f;
22441 Cursor cursor;
22442 Lisp_Object pointer;
22444 /* Do not change cursor shape while dragging mouse. */
22445 if (!NILP (do_mouse_tracking))
22446 return;
22448 if (!NILP (pointer))
22450 if (EQ (pointer, Qarrow))
22451 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22452 else if (EQ (pointer, Qhand))
22453 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22454 else if (EQ (pointer, Qtext))
22455 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22456 else if (EQ (pointer, intern ("hdrag")))
22457 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22458 #ifdef HAVE_X_WINDOWS
22459 else if (EQ (pointer, intern ("vdrag")))
22460 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22461 #endif
22462 else if (EQ (pointer, intern ("hourglass")))
22463 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22464 else if (EQ (pointer, Qmodeline))
22465 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22466 else
22467 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22470 if (cursor != No_Cursor)
22471 rif->define_frame_cursor (f, cursor);
22474 /* Take proper action when mouse has moved to the mode or header line
22475 or marginal area AREA of window W, x-position X and y-position Y.
22476 X is relative to the start of the text display area of W, so the
22477 width of bitmap areas and scroll bars must be subtracted to get a
22478 position relative to the start of the mode line. */
22480 static void
22481 note_mode_line_or_margin_highlight (window, x, y, area)
22482 Lisp_Object window;
22483 int x, y;
22484 enum window_part area;
22486 struct window *w = XWINDOW (window);
22487 struct frame *f = XFRAME (w->frame);
22488 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22489 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22490 Lisp_Object pointer = Qnil;
22491 int charpos, dx, dy, width, height;
22492 Lisp_Object string, object = Qnil;
22493 Lisp_Object pos, help;
22495 Lisp_Object mouse_face;
22496 int original_x_pixel = x;
22497 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22498 struct glyph_row *row;
22500 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22502 int x0;
22503 struct glyph *end;
22505 string = mode_line_string (w, area, &x, &y, &charpos,
22506 &object, &dx, &dy, &width, &height);
22508 row = (area == ON_MODE_LINE
22509 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22510 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22512 /* Find glyph */
22513 if (row->mode_line_p && row->enabled_p)
22515 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22516 end = glyph + row->used[TEXT_AREA];
22518 for (x0 = original_x_pixel;
22519 glyph < end && x0 >= glyph->pixel_width;
22520 ++glyph)
22521 x0 -= glyph->pixel_width;
22523 if (glyph >= end)
22524 glyph = NULL;
22527 else
22529 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22530 string = marginal_area_string (w, area, &x, &y, &charpos,
22531 &object, &dx, &dy, &width, &height);
22534 help = Qnil;
22536 if (IMAGEP (object))
22538 Lisp_Object image_map, hotspot;
22539 if ((image_map = Fplist_get (XCDR (object), QCmap),
22540 !NILP (image_map))
22541 && (hotspot = find_hot_spot (image_map, dx, dy),
22542 CONSP (hotspot))
22543 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22545 Lisp_Object area_id, plist;
22547 area_id = XCAR (hotspot);
22548 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22549 If so, we could look for mouse-enter, mouse-leave
22550 properties in PLIST (and do something...). */
22551 hotspot = XCDR (hotspot);
22552 if (CONSP (hotspot)
22553 && (plist = XCAR (hotspot), CONSP (plist)))
22555 pointer = Fplist_get (plist, Qpointer);
22556 if (NILP (pointer))
22557 pointer = Qhand;
22558 help = Fplist_get (plist, Qhelp_echo);
22559 if (!NILP (help))
22561 help_echo_string = help;
22562 /* Is this correct? ++kfs */
22563 XSETWINDOW (help_echo_window, w);
22564 help_echo_object = w->buffer;
22565 help_echo_pos = charpos;
22569 if (NILP (pointer))
22570 pointer = Fplist_get (XCDR (object), QCpointer);
22573 if (STRINGP (string))
22575 pos = make_number (charpos);
22576 /* If we're on a string with `help-echo' text property, arrange
22577 for the help to be displayed. This is done by setting the
22578 global variable help_echo_string to the help string. */
22579 if (NILP (help))
22581 help = Fget_text_property (pos, Qhelp_echo, string);
22582 if (!NILP (help))
22584 help_echo_string = help;
22585 XSETWINDOW (help_echo_window, w);
22586 help_echo_object = string;
22587 help_echo_pos = charpos;
22591 if (NILP (pointer))
22592 pointer = Fget_text_property (pos, Qpointer, string);
22594 /* Change the mouse pointer according to what is under X/Y. */
22595 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22597 Lisp_Object map;
22598 map = Fget_text_property (pos, Qlocal_map, string);
22599 if (!KEYMAPP (map))
22600 map = Fget_text_property (pos, Qkeymap, string);
22601 if (!KEYMAPP (map))
22602 cursor = dpyinfo->vertical_scroll_bar_cursor;
22605 /* Change the mouse face according to what is under X/Y. */
22606 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22607 if (!NILP (mouse_face)
22608 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22609 && glyph)
22611 Lisp_Object b, e;
22613 struct glyph * tmp_glyph;
22615 int gpos;
22616 int gseq_length;
22617 int total_pixel_width;
22618 int ignore;
22620 int vpos, hpos;
22622 b = Fprevious_single_property_change (make_number (charpos + 1),
22623 Qmouse_face, string, Qnil);
22624 if (NILP (b))
22625 b = make_number (0);
22627 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22628 if (NILP (e))
22629 e = make_number (SCHARS (string));
22631 /* Calculate the position(glyph position: GPOS) of GLYPH in
22632 displayed string. GPOS is different from CHARPOS.
22634 CHARPOS is the position of glyph in internal string
22635 object. A mode line string format has structures which
22636 is converted to a flatten by emacs lisp interpreter.
22637 The internal string is an element of the structures.
22638 The displayed string is the flatten string. */
22639 gpos = 0;
22640 if (glyph > row_start_glyph)
22642 tmp_glyph = glyph - 1;
22643 while (tmp_glyph >= row_start_glyph
22644 && tmp_glyph->charpos >= XINT (b)
22645 && EQ (tmp_glyph->object, glyph->object))
22647 tmp_glyph--;
22648 gpos++;
22652 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22653 displayed string holding GLYPH.
22655 GSEQ_LENGTH is different from SCHARS (STRING).
22656 SCHARS (STRING) returns the length of the internal string. */
22657 for (tmp_glyph = glyph, gseq_length = gpos;
22658 tmp_glyph->charpos < XINT (e);
22659 tmp_glyph++, gseq_length++)
22661 if (!EQ (tmp_glyph->object, glyph->object))
22662 break;
22665 total_pixel_width = 0;
22666 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22667 total_pixel_width += tmp_glyph->pixel_width;
22669 /* Pre calculation of re-rendering position */
22670 vpos = (x - gpos);
22671 hpos = (area == ON_MODE_LINE
22672 ? (w->current_matrix)->nrows - 1
22673 : 0);
22675 /* If the re-rendering position is included in the last
22676 re-rendering area, we should do nothing. */
22677 if ( EQ (window, dpyinfo->mouse_face_window)
22678 && dpyinfo->mouse_face_beg_col <= vpos
22679 && vpos < dpyinfo->mouse_face_end_col
22680 && dpyinfo->mouse_face_beg_row == hpos )
22681 return;
22683 if (clear_mouse_face (dpyinfo))
22684 cursor = No_Cursor;
22686 dpyinfo->mouse_face_beg_col = vpos;
22687 dpyinfo->mouse_face_beg_row = hpos;
22689 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22690 dpyinfo->mouse_face_beg_y = 0;
22692 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22693 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22695 dpyinfo->mouse_face_end_x = 0;
22696 dpyinfo->mouse_face_end_y = 0;
22698 dpyinfo->mouse_face_past_end = 0;
22699 dpyinfo->mouse_face_window = window;
22701 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22702 charpos,
22703 0, 0, 0, &ignore,
22704 glyph->face_id, 1);
22705 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22707 if (NILP (pointer))
22708 pointer = Qhand;
22710 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22711 clear_mouse_face (dpyinfo);
22713 define_frame_cursor1 (f, cursor, pointer);
22717 /* EXPORT:
22718 Take proper action when the mouse has moved to position X, Y on
22719 frame F as regards highlighting characters that have mouse-face
22720 properties. Also de-highlighting chars where the mouse was before.
22721 X and Y can be negative or out of range. */
22723 void
22724 note_mouse_highlight (f, x, y)
22725 struct frame *f;
22726 int x, y;
22728 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22729 enum window_part part;
22730 Lisp_Object window;
22731 struct window *w;
22732 Cursor cursor = No_Cursor;
22733 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22734 struct buffer *b;
22736 /* When a menu is active, don't highlight because this looks odd. */
22737 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22738 if (popup_activated ())
22739 return;
22740 #endif
22742 if (NILP (Vmouse_highlight)
22743 || !f->glyphs_initialized_p)
22744 return;
22746 dpyinfo->mouse_face_mouse_x = x;
22747 dpyinfo->mouse_face_mouse_y = y;
22748 dpyinfo->mouse_face_mouse_frame = f;
22750 if (dpyinfo->mouse_face_defer)
22751 return;
22753 if (gc_in_progress)
22755 dpyinfo->mouse_face_deferred_gc = 1;
22756 return;
22759 /* Which window is that in? */
22760 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22762 /* If we were displaying active text in another window, clear that.
22763 Also clear if we move out of text area in same window. */
22764 if (! EQ (window, dpyinfo->mouse_face_window)
22765 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22766 && !NILP (dpyinfo->mouse_face_window)))
22767 clear_mouse_face (dpyinfo);
22769 /* Not on a window -> return. */
22770 if (!WINDOWP (window))
22771 return;
22773 /* Reset help_echo_string. It will get recomputed below. */
22774 help_echo_string = Qnil;
22776 /* Convert to window-relative pixel coordinates. */
22777 w = XWINDOW (window);
22778 frame_to_window_pixel_xy (w, &x, &y);
22780 /* Handle tool-bar window differently since it doesn't display a
22781 buffer. */
22782 if (EQ (window, f->tool_bar_window))
22784 note_tool_bar_highlight (f, x, y);
22785 return;
22788 /* Mouse is on the mode, header line or margin? */
22789 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22790 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22792 note_mode_line_or_margin_highlight (window, x, y, part);
22793 return;
22796 if (part == ON_VERTICAL_BORDER)
22798 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22799 help_echo_string = build_string ("drag-mouse-1: resize");
22801 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22802 || part == ON_SCROLL_BAR)
22803 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22804 else
22805 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22807 /* Are we in a window whose display is up to date?
22808 And verify the buffer's text has not changed. */
22809 b = XBUFFER (w->buffer);
22810 if (part == ON_TEXT
22811 && EQ (w->window_end_valid, w->buffer)
22812 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22813 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22815 int hpos, vpos, pos, i, dx, dy, area;
22816 struct glyph *glyph;
22817 Lisp_Object object;
22818 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22819 Lisp_Object *overlay_vec = NULL;
22820 int noverlays;
22821 struct buffer *obuf;
22822 int obegv, ozv, same_region;
22824 /* Find the glyph under X/Y. */
22825 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22827 /* Look for :pointer property on image. */
22828 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22830 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22831 if (img != NULL && IMAGEP (img->spec))
22833 Lisp_Object image_map, hotspot;
22834 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22835 !NILP (image_map))
22836 && (hotspot = find_hot_spot (image_map,
22837 glyph->slice.x + dx,
22838 glyph->slice.y + dy),
22839 CONSP (hotspot))
22840 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22842 Lisp_Object area_id, plist;
22844 area_id = XCAR (hotspot);
22845 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22846 If so, we could look for mouse-enter, mouse-leave
22847 properties in PLIST (and do something...). */
22848 hotspot = XCDR (hotspot);
22849 if (CONSP (hotspot)
22850 && (plist = XCAR (hotspot), CONSP (plist)))
22852 pointer = Fplist_get (plist, Qpointer);
22853 if (NILP (pointer))
22854 pointer = Qhand;
22855 help_echo_string = Fplist_get (plist, Qhelp_echo);
22856 if (!NILP (help_echo_string))
22858 help_echo_window = window;
22859 help_echo_object = glyph->object;
22860 help_echo_pos = glyph->charpos;
22864 if (NILP (pointer))
22865 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22869 /* Clear mouse face if X/Y not over text. */
22870 if (glyph == NULL
22871 || area != TEXT_AREA
22872 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22874 if (clear_mouse_face (dpyinfo))
22875 cursor = No_Cursor;
22876 if (NILP (pointer))
22878 if (area != TEXT_AREA)
22879 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22880 else
22881 pointer = Vvoid_text_area_pointer;
22883 goto set_cursor;
22886 pos = glyph->charpos;
22887 object = glyph->object;
22888 if (!STRINGP (object) && !BUFFERP (object))
22889 goto set_cursor;
22891 /* If we get an out-of-range value, return now; avoid an error. */
22892 if (BUFFERP (object) && pos > BUF_Z (b))
22893 goto set_cursor;
22895 /* Make the window's buffer temporarily current for
22896 overlays_at and compute_char_face. */
22897 obuf = current_buffer;
22898 current_buffer = b;
22899 obegv = BEGV;
22900 ozv = ZV;
22901 BEGV = BEG;
22902 ZV = Z;
22904 /* Is this char mouse-active or does it have help-echo? */
22905 position = make_number (pos);
22907 if (BUFFERP (object))
22909 /* Put all the overlays we want in a vector in overlay_vec. */
22910 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22911 /* Sort overlays into increasing priority order. */
22912 noverlays = sort_overlays (overlay_vec, noverlays, w);
22914 else
22915 noverlays = 0;
22917 same_region = (EQ (window, dpyinfo->mouse_face_window)
22918 && vpos >= dpyinfo->mouse_face_beg_row
22919 && vpos <= dpyinfo->mouse_face_end_row
22920 && (vpos > dpyinfo->mouse_face_beg_row
22921 || hpos >= dpyinfo->mouse_face_beg_col)
22922 && (vpos < dpyinfo->mouse_face_end_row
22923 || hpos < dpyinfo->mouse_face_end_col
22924 || dpyinfo->mouse_face_past_end));
22926 if (same_region)
22927 cursor = No_Cursor;
22929 /* Check mouse-face highlighting. */
22930 if (! same_region
22931 /* If there exists an overlay with mouse-face overlapping
22932 the one we are currently highlighting, we have to
22933 check if we enter the overlapping overlay, and then
22934 highlight only that. */
22935 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22936 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22938 /* Find the highest priority overlay that has a mouse-face
22939 property. */
22940 overlay = Qnil;
22941 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22943 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22944 if (!NILP (mouse_face))
22945 overlay = overlay_vec[i];
22948 /* If we're actually highlighting the same overlay as
22949 before, there's no need to do that again. */
22950 if (!NILP (overlay)
22951 && EQ (overlay, dpyinfo->mouse_face_overlay))
22952 goto check_help_echo;
22954 dpyinfo->mouse_face_overlay = overlay;
22956 /* Clear the display of the old active region, if any. */
22957 if (clear_mouse_face (dpyinfo))
22958 cursor = No_Cursor;
22960 /* If no overlay applies, get a text property. */
22961 if (NILP (overlay))
22962 mouse_face = Fget_text_property (position, Qmouse_face, object);
22964 /* Handle the overlay case. */
22965 if (!NILP (overlay))
22967 /* Find the range of text around this char that
22968 should be active. */
22969 Lisp_Object before, after;
22970 int ignore;
22972 before = Foverlay_start (overlay);
22973 after = Foverlay_end (overlay);
22974 /* Record this as the current active region. */
22975 fast_find_position (w, XFASTINT (before),
22976 &dpyinfo->mouse_face_beg_col,
22977 &dpyinfo->mouse_face_beg_row,
22978 &dpyinfo->mouse_face_beg_x,
22979 &dpyinfo->mouse_face_beg_y, Qnil);
22981 dpyinfo->mouse_face_past_end
22982 = !fast_find_position (w, XFASTINT (after),
22983 &dpyinfo->mouse_face_end_col,
22984 &dpyinfo->mouse_face_end_row,
22985 &dpyinfo->mouse_face_end_x,
22986 &dpyinfo->mouse_face_end_y, Qnil);
22987 dpyinfo->mouse_face_window = window;
22989 dpyinfo->mouse_face_face_id
22990 = face_at_buffer_position (w, pos, 0, 0,
22991 &ignore, pos + 1,
22992 !dpyinfo->mouse_face_hidden);
22994 /* Display it as active. */
22995 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22996 cursor = No_Cursor;
22998 /* Handle the text property case. */
22999 else if (!NILP (mouse_face) && BUFFERP (object))
23001 /* Find the range of text around this char that
23002 should be active. */
23003 Lisp_Object before, after, beginning, end;
23004 int ignore;
23006 beginning = Fmarker_position (w->start);
23007 end = make_number (BUF_Z (XBUFFER (object))
23008 - XFASTINT (w->window_end_pos));
23009 before
23010 = Fprevious_single_property_change (make_number (pos + 1),
23011 Qmouse_face,
23012 object, beginning);
23013 after
23014 = Fnext_single_property_change (position, Qmouse_face,
23015 object, end);
23017 /* Record this as the current active region. */
23018 fast_find_position (w, XFASTINT (before),
23019 &dpyinfo->mouse_face_beg_col,
23020 &dpyinfo->mouse_face_beg_row,
23021 &dpyinfo->mouse_face_beg_x,
23022 &dpyinfo->mouse_face_beg_y, Qnil);
23023 dpyinfo->mouse_face_past_end
23024 = !fast_find_position (w, XFASTINT (after),
23025 &dpyinfo->mouse_face_end_col,
23026 &dpyinfo->mouse_face_end_row,
23027 &dpyinfo->mouse_face_end_x,
23028 &dpyinfo->mouse_face_end_y, Qnil);
23029 dpyinfo->mouse_face_window = window;
23031 if (BUFFERP (object))
23032 dpyinfo->mouse_face_face_id
23033 = face_at_buffer_position (w, pos, 0, 0,
23034 &ignore, pos + 1,
23035 !dpyinfo->mouse_face_hidden);
23037 /* Display it as active. */
23038 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23039 cursor = No_Cursor;
23041 else if (!NILP (mouse_face) && STRINGP (object))
23043 Lisp_Object b, e;
23044 int ignore;
23046 b = Fprevious_single_property_change (make_number (pos + 1),
23047 Qmouse_face,
23048 object, Qnil);
23049 e = Fnext_single_property_change (position, Qmouse_face,
23050 object, Qnil);
23051 if (NILP (b))
23052 b = make_number (0);
23053 if (NILP (e))
23054 e = make_number (SCHARS (object) - 1);
23056 fast_find_string_pos (w, XINT (b), object,
23057 &dpyinfo->mouse_face_beg_col,
23058 &dpyinfo->mouse_face_beg_row,
23059 &dpyinfo->mouse_face_beg_x,
23060 &dpyinfo->mouse_face_beg_y, 0);
23061 fast_find_string_pos (w, XINT (e), object,
23062 &dpyinfo->mouse_face_end_col,
23063 &dpyinfo->mouse_face_end_row,
23064 &dpyinfo->mouse_face_end_x,
23065 &dpyinfo->mouse_face_end_y, 1);
23066 dpyinfo->mouse_face_past_end = 0;
23067 dpyinfo->mouse_face_window = window;
23068 dpyinfo->mouse_face_face_id
23069 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23070 glyph->face_id, 1);
23071 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23072 cursor = No_Cursor;
23074 else if (STRINGP (object) && NILP (mouse_face))
23076 /* A string which doesn't have mouse-face, but
23077 the text ``under'' it might have. */
23078 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23079 int start = MATRIX_ROW_START_CHARPOS (r);
23081 pos = string_buffer_position (w, object, start);
23082 if (pos > 0)
23083 mouse_face = get_char_property_and_overlay (make_number (pos),
23084 Qmouse_face,
23085 w->buffer,
23086 &overlay);
23087 if (!NILP (mouse_face) && !NILP (overlay))
23089 Lisp_Object before = Foverlay_start (overlay);
23090 Lisp_Object after = Foverlay_end (overlay);
23091 int ignore;
23093 /* Note that we might not be able to find position
23094 BEFORE in the glyph matrix if the overlay is
23095 entirely covered by a `display' property. In
23096 this case, we overshoot. So let's stop in
23097 the glyph matrix before glyphs for OBJECT. */
23098 fast_find_position (w, XFASTINT (before),
23099 &dpyinfo->mouse_face_beg_col,
23100 &dpyinfo->mouse_face_beg_row,
23101 &dpyinfo->mouse_face_beg_x,
23102 &dpyinfo->mouse_face_beg_y,
23103 object);
23105 dpyinfo->mouse_face_past_end
23106 = !fast_find_position (w, XFASTINT (after),
23107 &dpyinfo->mouse_face_end_col,
23108 &dpyinfo->mouse_face_end_row,
23109 &dpyinfo->mouse_face_end_x,
23110 &dpyinfo->mouse_face_end_y,
23111 Qnil);
23112 dpyinfo->mouse_face_window = window;
23113 dpyinfo->mouse_face_face_id
23114 = face_at_buffer_position (w, pos, 0, 0,
23115 &ignore, pos + 1,
23116 !dpyinfo->mouse_face_hidden);
23118 /* Display it as active. */
23119 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23120 cursor = No_Cursor;
23125 check_help_echo:
23127 /* Look for a `help-echo' property. */
23128 if (NILP (help_echo_string)) {
23129 Lisp_Object help, overlay;
23131 /* Check overlays first. */
23132 help = overlay = Qnil;
23133 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23135 overlay = overlay_vec[i];
23136 help = Foverlay_get (overlay, Qhelp_echo);
23139 if (!NILP (help))
23141 help_echo_string = help;
23142 help_echo_window = window;
23143 help_echo_object = overlay;
23144 help_echo_pos = pos;
23146 else
23148 Lisp_Object object = glyph->object;
23149 int charpos = glyph->charpos;
23151 /* Try text properties. */
23152 if (STRINGP (object)
23153 && charpos >= 0
23154 && charpos < SCHARS (object))
23156 help = Fget_text_property (make_number (charpos),
23157 Qhelp_echo, object);
23158 if (NILP (help))
23160 /* If the string itself doesn't specify a help-echo,
23161 see if the buffer text ``under'' it does. */
23162 struct glyph_row *r
23163 = MATRIX_ROW (w->current_matrix, vpos);
23164 int start = MATRIX_ROW_START_CHARPOS (r);
23165 int pos = string_buffer_position (w, object, start);
23166 if (pos > 0)
23168 help = Fget_char_property (make_number (pos),
23169 Qhelp_echo, w->buffer);
23170 if (!NILP (help))
23172 charpos = pos;
23173 object = w->buffer;
23178 else if (BUFFERP (object)
23179 && charpos >= BEGV
23180 && charpos < ZV)
23181 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23182 object);
23184 if (!NILP (help))
23186 help_echo_string = help;
23187 help_echo_window = window;
23188 help_echo_object = object;
23189 help_echo_pos = charpos;
23194 /* Look for a `pointer' property. */
23195 if (NILP (pointer))
23197 /* Check overlays first. */
23198 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23199 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23201 if (NILP (pointer))
23203 Lisp_Object object = glyph->object;
23204 int charpos = glyph->charpos;
23206 /* Try text properties. */
23207 if (STRINGP (object)
23208 && charpos >= 0
23209 && charpos < SCHARS (object))
23211 pointer = Fget_text_property (make_number (charpos),
23212 Qpointer, object);
23213 if (NILP (pointer))
23215 /* If the string itself doesn't specify a pointer,
23216 see if the buffer text ``under'' it does. */
23217 struct glyph_row *r
23218 = MATRIX_ROW (w->current_matrix, vpos);
23219 int start = MATRIX_ROW_START_CHARPOS (r);
23220 int pos = string_buffer_position (w, object, start);
23221 if (pos > 0)
23222 pointer = Fget_char_property (make_number (pos),
23223 Qpointer, w->buffer);
23226 else if (BUFFERP (object)
23227 && charpos >= BEGV
23228 && charpos < ZV)
23229 pointer = Fget_text_property (make_number (charpos),
23230 Qpointer, object);
23234 BEGV = obegv;
23235 ZV = ozv;
23236 current_buffer = obuf;
23239 set_cursor:
23241 define_frame_cursor1 (f, cursor, pointer);
23245 /* EXPORT for RIF:
23246 Clear any mouse-face on window W. This function is part of the
23247 redisplay interface, and is called from try_window_id and similar
23248 functions to ensure the mouse-highlight is off. */
23250 void
23251 x_clear_window_mouse_face (w)
23252 struct window *w;
23254 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23255 Lisp_Object window;
23257 BLOCK_INPUT;
23258 XSETWINDOW (window, w);
23259 if (EQ (window, dpyinfo->mouse_face_window))
23260 clear_mouse_face (dpyinfo);
23261 UNBLOCK_INPUT;
23265 /* EXPORT:
23266 Just discard the mouse face information for frame F, if any.
23267 This is used when the size of F is changed. */
23269 void
23270 cancel_mouse_face (f)
23271 struct frame *f;
23273 Lisp_Object window;
23274 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23276 window = dpyinfo->mouse_face_window;
23277 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23279 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23280 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23281 dpyinfo->mouse_face_window = Qnil;
23286 #endif /* HAVE_WINDOW_SYSTEM */
23289 /***********************************************************************
23290 Exposure Events
23291 ***********************************************************************/
23293 #ifdef HAVE_WINDOW_SYSTEM
23295 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23296 which intersects rectangle R. R is in window-relative coordinates. */
23298 static void
23299 expose_area (w, row, r, area)
23300 struct window *w;
23301 struct glyph_row *row;
23302 XRectangle *r;
23303 enum glyph_row_area area;
23305 struct glyph *first = row->glyphs[area];
23306 struct glyph *end = row->glyphs[area] + row->used[area];
23307 struct glyph *last;
23308 int first_x, start_x, x;
23310 if (area == TEXT_AREA && row->fill_line_p)
23311 /* If row extends face to end of line write the whole line. */
23312 draw_glyphs (w, 0, row, area,
23313 0, row->used[area],
23314 DRAW_NORMAL_TEXT, 0);
23315 else
23317 /* Set START_X to the window-relative start position for drawing glyphs of
23318 AREA. The first glyph of the text area can be partially visible.
23319 The first glyphs of other areas cannot. */
23320 start_x = window_box_left_offset (w, area);
23321 x = start_x;
23322 if (area == TEXT_AREA)
23323 x += row->x;
23325 /* Find the first glyph that must be redrawn. */
23326 while (first < end
23327 && x + first->pixel_width < r->x)
23329 x += first->pixel_width;
23330 ++first;
23333 /* Find the last one. */
23334 last = first;
23335 first_x = x;
23336 while (last < end
23337 && x < r->x + r->width)
23339 x += last->pixel_width;
23340 ++last;
23343 /* Repaint. */
23344 if (last > first)
23345 draw_glyphs (w, first_x - start_x, row, area,
23346 first - row->glyphs[area], last - row->glyphs[area],
23347 DRAW_NORMAL_TEXT, 0);
23352 /* Redraw the parts of the glyph row ROW on window W intersecting
23353 rectangle R. R is in window-relative coordinates. Value is
23354 non-zero if mouse-face was overwritten. */
23356 static int
23357 expose_line (w, row, r)
23358 struct window *w;
23359 struct glyph_row *row;
23360 XRectangle *r;
23362 xassert (row->enabled_p);
23364 if (row->mode_line_p || w->pseudo_window_p)
23365 draw_glyphs (w, 0, row, TEXT_AREA,
23366 0, row->used[TEXT_AREA],
23367 DRAW_NORMAL_TEXT, 0);
23368 else
23370 if (row->used[LEFT_MARGIN_AREA])
23371 expose_area (w, row, r, LEFT_MARGIN_AREA);
23372 if (row->used[TEXT_AREA])
23373 expose_area (w, row, r, TEXT_AREA);
23374 if (row->used[RIGHT_MARGIN_AREA])
23375 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23376 draw_row_fringe_bitmaps (w, row);
23379 return row->mouse_face_p;
23383 /* Redraw those parts of glyphs rows during expose event handling that
23384 overlap other rows. Redrawing of an exposed line writes over parts
23385 of lines overlapping that exposed line; this function fixes that.
23387 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23388 row in W's current matrix that is exposed and overlaps other rows.
23389 LAST_OVERLAPPING_ROW is the last such row. */
23391 static void
23392 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23393 struct window *w;
23394 struct glyph_row *first_overlapping_row;
23395 struct glyph_row *last_overlapping_row;
23397 struct glyph_row *row;
23399 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23400 if (row->overlapping_p)
23402 xassert (row->enabled_p && !row->mode_line_p);
23404 if (row->used[LEFT_MARGIN_AREA])
23405 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23407 if (row->used[TEXT_AREA])
23408 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23410 if (row->used[RIGHT_MARGIN_AREA])
23411 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23416 /* Return non-zero if W's cursor intersects rectangle R. */
23418 static int
23419 phys_cursor_in_rect_p (w, r)
23420 struct window *w;
23421 XRectangle *r;
23423 XRectangle cr, result;
23424 struct glyph *cursor_glyph;
23426 cursor_glyph = get_phys_cursor_glyph (w);
23427 if (cursor_glyph)
23429 /* r is relative to W's box, but w->phys_cursor.x is relative
23430 to left edge of W's TEXT area. Adjust it. */
23431 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23432 cr.y = w->phys_cursor.y;
23433 cr.width = cursor_glyph->pixel_width;
23434 cr.height = w->phys_cursor_height;
23435 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23436 I assume the effect is the same -- and this is portable. */
23437 return x_intersect_rectangles (&cr, r, &result);
23439 else
23440 return 0;
23444 /* EXPORT:
23445 Draw a vertical window border to the right of window W if W doesn't
23446 have vertical scroll bars. */
23448 void
23449 x_draw_vertical_border (w)
23450 struct window *w;
23452 /* We could do better, if we knew what type of scroll-bar the adjacent
23453 windows (on either side) have... But we don't :-(
23454 However, I think this works ok. ++KFS 2003-04-25 */
23456 /* Redraw borders between horizontally adjacent windows. Don't
23457 do it for frames with vertical scroll bars because either the
23458 right scroll bar of a window, or the left scroll bar of its
23459 neighbor will suffice as a border. */
23460 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23461 return;
23463 if (!WINDOW_RIGHTMOST_P (w)
23464 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23466 int x0, x1, y0, y1;
23468 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23469 y1 -= 1;
23471 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23472 x1 -= 1;
23474 rif->draw_vertical_window_border (w, x1, y0, y1);
23476 else if (!WINDOW_LEFTMOST_P (w)
23477 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23479 int x0, x1, y0, y1;
23481 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23482 y1 -= 1;
23484 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23485 x0 -= 1;
23487 rif->draw_vertical_window_border (w, x0, y0, y1);
23492 /* Redraw the part of window W intersection rectangle FR. Pixel
23493 coordinates in FR are frame-relative. Call this function with
23494 input blocked. Value is non-zero if the exposure overwrites
23495 mouse-face. */
23497 static int
23498 expose_window (w, fr)
23499 struct window *w;
23500 XRectangle *fr;
23502 struct frame *f = XFRAME (w->frame);
23503 XRectangle wr, r;
23504 int mouse_face_overwritten_p = 0;
23506 /* If window is not yet fully initialized, do nothing. This can
23507 happen when toolkit scroll bars are used and a window is split.
23508 Reconfiguring the scroll bar will generate an expose for a newly
23509 created window. */
23510 if (w->current_matrix == NULL)
23511 return 0;
23513 /* When we're currently updating the window, display and current
23514 matrix usually don't agree. Arrange for a thorough display
23515 later. */
23516 if (w == updated_window)
23518 SET_FRAME_GARBAGED (f);
23519 return 0;
23522 /* Frame-relative pixel rectangle of W. */
23523 wr.x = WINDOW_LEFT_EDGE_X (w);
23524 wr.y = WINDOW_TOP_EDGE_Y (w);
23525 wr.width = WINDOW_TOTAL_WIDTH (w);
23526 wr.height = WINDOW_TOTAL_HEIGHT (w);
23528 if (x_intersect_rectangles (fr, &wr, &r))
23530 int yb = window_text_bottom_y (w);
23531 struct glyph_row *row;
23532 int cursor_cleared_p;
23533 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23535 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23536 r.x, r.y, r.width, r.height));
23538 /* Convert to window coordinates. */
23539 r.x -= WINDOW_LEFT_EDGE_X (w);
23540 r.y -= WINDOW_TOP_EDGE_Y (w);
23542 /* Turn off the cursor. */
23543 if (!w->pseudo_window_p
23544 && phys_cursor_in_rect_p (w, &r))
23546 x_clear_cursor (w);
23547 cursor_cleared_p = 1;
23549 else
23550 cursor_cleared_p = 0;
23552 /* Update lines intersecting rectangle R. */
23553 first_overlapping_row = last_overlapping_row = NULL;
23554 for (row = w->current_matrix->rows;
23555 row->enabled_p;
23556 ++row)
23558 int y0 = row->y;
23559 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23561 if ((y0 >= r.y && y0 < r.y + r.height)
23562 || (y1 > r.y && y1 < r.y + r.height)
23563 || (r.y >= y0 && r.y < y1)
23564 || (r.y + r.height > y0 && r.y + r.height < y1))
23566 /* A header line may be overlapping, but there is no need
23567 to fix overlapping areas for them. KFS 2005-02-12 */
23568 if (row->overlapping_p && !row->mode_line_p)
23570 if (first_overlapping_row == NULL)
23571 first_overlapping_row = row;
23572 last_overlapping_row = row;
23575 if (expose_line (w, row, &r))
23576 mouse_face_overwritten_p = 1;
23579 if (y1 >= yb)
23580 break;
23583 /* Display the mode line if there is one. */
23584 if (WINDOW_WANTS_MODELINE_P (w)
23585 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23586 row->enabled_p)
23587 && row->y < r.y + r.height)
23589 if (expose_line (w, row, &r))
23590 mouse_face_overwritten_p = 1;
23593 if (!w->pseudo_window_p)
23595 /* Fix the display of overlapping rows. */
23596 if (first_overlapping_row)
23597 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23599 /* Draw border between windows. */
23600 x_draw_vertical_border (w);
23602 /* Turn the cursor on again. */
23603 if (cursor_cleared_p)
23604 update_window_cursor (w, 1);
23608 return mouse_face_overwritten_p;
23613 /* Redraw (parts) of all windows in the window tree rooted at W that
23614 intersect R. R contains frame pixel coordinates. Value is
23615 non-zero if the exposure overwrites mouse-face. */
23617 static int
23618 expose_window_tree (w, r)
23619 struct window *w;
23620 XRectangle *r;
23622 struct frame *f = XFRAME (w->frame);
23623 int mouse_face_overwritten_p = 0;
23625 while (w && !FRAME_GARBAGED_P (f))
23627 if (!NILP (w->hchild))
23628 mouse_face_overwritten_p
23629 |= expose_window_tree (XWINDOW (w->hchild), r);
23630 else if (!NILP (w->vchild))
23631 mouse_face_overwritten_p
23632 |= expose_window_tree (XWINDOW (w->vchild), r);
23633 else
23634 mouse_face_overwritten_p |= expose_window (w, r);
23636 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23639 return mouse_face_overwritten_p;
23643 /* EXPORT:
23644 Redisplay an exposed area of frame F. X and Y are the upper-left
23645 corner of the exposed rectangle. W and H are width and height of
23646 the exposed area. All are pixel values. W or H zero means redraw
23647 the entire frame. */
23649 void
23650 expose_frame (f, x, y, w, h)
23651 struct frame *f;
23652 int x, y, w, h;
23654 XRectangle r;
23655 int mouse_face_overwritten_p = 0;
23657 TRACE ((stderr, "expose_frame "));
23659 /* No need to redraw if frame will be redrawn soon. */
23660 if (FRAME_GARBAGED_P (f))
23662 TRACE ((stderr, " garbaged\n"));
23663 return;
23666 /* If basic faces haven't been realized yet, there is no point in
23667 trying to redraw anything. This can happen when we get an expose
23668 event while Emacs is starting, e.g. by moving another window. */
23669 if (FRAME_FACE_CACHE (f) == NULL
23670 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23672 TRACE ((stderr, " no faces\n"));
23673 return;
23676 if (w == 0 || h == 0)
23678 r.x = r.y = 0;
23679 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23680 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23682 else
23684 r.x = x;
23685 r.y = y;
23686 r.width = w;
23687 r.height = h;
23690 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23691 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23693 if (WINDOWP (f->tool_bar_window))
23694 mouse_face_overwritten_p
23695 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23697 #ifdef HAVE_X_WINDOWS
23698 #ifndef MSDOS
23699 #ifndef USE_X_TOOLKIT
23700 if (WINDOWP (f->menu_bar_window))
23701 mouse_face_overwritten_p
23702 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23703 #endif /* not USE_X_TOOLKIT */
23704 #endif
23705 #endif
23707 /* Some window managers support a focus-follows-mouse style with
23708 delayed raising of frames. Imagine a partially obscured frame,
23709 and moving the mouse into partially obscured mouse-face on that
23710 frame. The visible part of the mouse-face will be highlighted,
23711 then the WM raises the obscured frame. With at least one WM, KDE
23712 2.1, Emacs is not getting any event for the raising of the frame
23713 (even tried with SubstructureRedirectMask), only Expose events.
23714 These expose events will draw text normally, i.e. not
23715 highlighted. Which means we must redo the highlight here.
23716 Subsume it under ``we love X''. --gerd 2001-08-15 */
23717 /* Included in Windows version because Windows most likely does not
23718 do the right thing if any third party tool offers
23719 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23720 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23722 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23723 if (f == dpyinfo->mouse_face_mouse_frame)
23725 int x = dpyinfo->mouse_face_mouse_x;
23726 int y = dpyinfo->mouse_face_mouse_y;
23727 clear_mouse_face (dpyinfo);
23728 note_mouse_highlight (f, x, y);
23734 /* EXPORT:
23735 Determine the intersection of two rectangles R1 and R2. Return
23736 the intersection in *RESULT. Value is non-zero if RESULT is not
23737 empty. */
23740 x_intersect_rectangles (r1, r2, result)
23741 XRectangle *r1, *r2, *result;
23743 XRectangle *left, *right;
23744 XRectangle *upper, *lower;
23745 int intersection_p = 0;
23747 /* Rearrange so that R1 is the left-most rectangle. */
23748 if (r1->x < r2->x)
23749 left = r1, right = r2;
23750 else
23751 left = r2, right = r1;
23753 /* X0 of the intersection is right.x0, if this is inside R1,
23754 otherwise there is no intersection. */
23755 if (right->x <= left->x + left->width)
23757 result->x = right->x;
23759 /* The right end of the intersection is the minimum of the
23760 the right ends of left and right. */
23761 result->width = (min (left->x + left->width, right->x + right->width)
23762 - result->x);
23764 /* Same game for Y. */
23765 if (r1->y < r2->y)
23766 upper = r1, lower = r2;
23767 else
23768 upper = r2, lower = r1;
23770 /* The upper end of the intersection is lower.y0, if this is inside
23771 of upper. Otherwise, there is no intersection. */
23772 if (lower->y <= upper->y + upper->height)
23774 result->y = lower->y;
23776 /* The lower end of the intersection is the minimum of the lower
23777 ends of upper and lower. */
23778 result->height = (min (lower->y + lower->height,
23779 upper->y + upper->height)
23780 - result->y);
23781 intersection_p = 1;
23785 return intersection_p;
23788 #endif /* HAVE_WINDOW_SYSTEM */
23791 /***********************************************************************
23792 Initialization
23793 ***********************************************************************/
23795 void
23796 syms_of_xdisp ()
23798 Vwith_echo_area_save_vector = Qnil;
23799 staticpro (&Vwith_echo_area_save_vector);
23801 Vmessage_stack = Qnil;
23802 staticpro (&Vmessage_stack);
23804 Qinhibit_redisplay = intern ("inhibit-redisplay");
23805 staticpro (&Qinhibit_redisplay);
23807 message_dolog_marker1 = Fmake_marker ();
23808 staticpro (&message_dolog_marker1);
23809 message_dolog_marker2 = Fmake_marker ();
23810 staticpro (&message_dolog_marker2);
23811 message_dolog_marker3 = Fmake_marker ();
23812 staticpro (&message_dolog_marker3);
23814 #if GLYPH_DEBUG
23815 defsubr (&Sdump_frame_glyph_matrix);
23816 defsubr (&Sdump_glyph_matrix);
23817 defsubr (&Sdump_glyph_row);
23818 defsubr (&Sdump_tool_bar_row);
23819 defsubr (&Strace_redisplay);
23820 defsubr (&Strace_to_stderr);
23821 #endif
23822 #ifdef HAVE_WINDOW_SYSTEM
23823 defsubr (&Stool_bar_lines_needed);
23824 defsubr (&Slookup_image_map);
23825 #endif
23826 defsubr (&Sformat_mode_line);
23828 staticpro (&Qmenu_bar_update_hook);
23829 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23831 staticpro (&Qoverriding_terminal_local_map);
23832 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23834 staticpro (&Qoverriding_local_map);
23835 Qoverriding_local_map = intern ("overriding-local-map");
23837 staticpro (&Qwindow_scroll_functions);
23838 Qwindow_scroll_functions = intern ("window-scroll-functions");
23840 staticpro (&Qredisplay_end_trigger_functions);
23841 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23843 staticpro (&Qinhibit_point_motion_hooks);
23844 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23846 QCdata = intern (":data");
23847 staticpro (&QCdata);
23848 Qdisplay = intern ("display");
23849 staticpro (&Qdisplay);
23850 Qspace_width = intern ("space-width");
23851 staticpro (&Qspace_width);
23852 Qraise = intern ("raise");
23853 staticpro (&Qraise);
23854 Qslice = intern ("slice");
23855 staticpro (&Qslice);
23856 Qspace = intern ("space");
23857 staticpro (&Qspace);
23858 Qmargin = intern ("margin");
23859 staticpro (&Qmargin);
23860 Qpointer = intern ("pointer");
23861 staticpro (&Qpointer);
23862 Qleft_margin = intern ("left-margin");
23863 staticpro (&Qleft_margin);
23864 Qright_margin = intern ("right-margin");
23865 staticpro (&Qright_margin);
23866 Qcenter = intern ("center");
23867 staticpro (&Qcenter);
23868 Qline_height = intern ("line-height");
23869 staticpro (&Qline_height);
23870 QCalign_to = intern (":align-to");
23871 staticpro (&QCalign_to);
23872 QCrelative_width = intern (":relative-width");
23873 staticpro (&QCrelative_width);
23874 QCrelative_height = intern (":relative-height");
23875 staticpro (&QCrelative_height);
23876 QCeval = intern (":eval");
23877 staticpro (&QCeval);
23878 QCpropertize = intern (":propertize");
23879 staticpro (&QCpropertize);
23880 QCfile = intern (":file");
23881 staticpro (&QCfile);
23882 Qfontified = intern ("fontified");
23883 staticpro (&Qfontified);
23884 Qfontification_functions = intern ("fontification-functions");
23885 staticpro (&Qfontification_functions);
23886 Qtrailing_whitespace = intern ("trailing-whitespace");
23887 staticpro (&Qtrailing_whitespace);
23888 Qescape_glyph = intern ("escape-glyph");
23889 staticpro (&Qescape_glyph);
23890 Qnobreak_space = intern ("nobreak-space");
23891 staticpro (&Qnobreak_space);
23892 Qimage = intern ("image");
23893 staticpro (&Qimage);
23894 QCmap = intern (":map");
23895 staticpro (&QCmap);
23896 QCpointer = intern (":pointer");
23897 staticpro (&QCpointer);
23898 Qrect = intern ("rect");
23899 staticpro (&Qrect);
23900 Qcircle = intern ("circle");
23901 staticpro (&Qcircle);
23902 Qpoly = intern ("poly");
23903 staticpro (&Qpoly);
23904 Qmessage_truncate_lines = intern ("message-truncate-lines");
23905 staticpro (&Qmessage_truncate_lines);
23906 Qgrow_only = intern ("grow-only");
23907 staticpro (&Qgrow_only);
23908 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23909 staticpro (&Qinhibit_menubar_update);
23910 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23911 staticpro (&Qinhibit_eval_during_redisplay);
23912 Qposition = intern ("position");
23913 staticpro (&Qposition);
23914 Qbuffer_position = intern ("buffer-position");
23915 staticpro (&Qbuffer_position);
23916 Qobject = intern ("object");
23917 staticpro (&Qobject);
23918 Qbar = intern ("bar");
23919 staticpro (&Qbar);
23920 Qhbar = intern ("hbar");
23921 staticpro (&Qhbar);
23922 Qbox = intern ("box");
23923 staticpro (&Qbox);
23924 Qhollow = intern ("hollow");
23925 staticpro (&Qhollow);
23926 Qhand = intern ("hand");
23927 staticpro (&Qhand);
23928 Qarrow = intern ("arrow");
23929 staticpro (&Qarrow);
23930 Qtext = intern ("text");
23931 staticpro (&Qtext);
23932 Qrisky_local_variable = intern ("risky-local-variable");
23933 staticpro (&Qrisky_local_variable);
23934 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23935 staticpro (&Qinhibit_free_realized_faces);
23937 list_of_error = Fcons (Fcons (intern ("error"),
23938 Fcons (intern ("void-variable"), Qnil)),
23939 Qnil);
23940 staticpro (&list_of_error);
23942 Qlast_arrow_position = intern ("last-arrow-position");
23943 staticpro (&Qlast_arrow_position);
23944 Qlast_arrow_string = intern ("last-arrow-string");
23945 staticpro (&Qlast_arrow_string);
23947 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23948 staticpro (&Qoverlay_arrow_string);
23949 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23950 staticpro (&Qoverlay_arrow_bitmap);
23952 echo_buffer[0] = echo_buffer[1] = Qnil;
23953 staticpro (&echo_buffer[0]);
23954 staticpro (&echo_buffer[1]);
23956 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23957 staticpro (&echo_area_buffer[0]);
23958 staticpro (&echo_area_buffer[1]);
23960 Vmessages_buffer_name = build_string ("*Messages*");
23961 staticpro (&Vmessages_buffer_name);
23963 mode_line_proptrans_alist = Qnil;
23964 staticpro (&mode_line_proptrans_alist);
23965 mode_line_string_list = Qnil;
23966 staticpro (&mode_line_string_list);
23967 mode_line_string_face = Qnil;
23968 staticpro (&mode_line_string_face);
23969 mode_line_string_face_prop = Qnil;
23970 staticpro (&mode_line_string_face_prop);
23971 Vmode_line_unwind_vector = Qnil;
23972 staticpro (&Vmode_line_unwind_vector);
23974 help_echo_string = Qnil;
23975 staticpro (&help_echo_string);
23976 help_echo_object = Qnil;
23977 staticpro (&help_echo_object);
23978 help_echo_window = Qnil;
23979 staticpro (&help_echo_window);
23980 previous_help_echo_string = Qnil;
23981 staticpro (&previous_help_echo_string);
23982 help_echo_pos = -1;
23984 #ifdef HAVE_WINDOW_SYSTEM
23985 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23986 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23987 For example, if a block cursor is over a tab, it will be drawn as
23988 wide as that tab on the display. */);
23989 x_stretch_cursor_p = 0;
23990 #endif
23992 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23993 doc: /* *Non-nil means highlight trailing whitespace.
23994 The face used for trailing whitespace is `trailing-whitespace'. */);
23995 Vshow_trailing_whitespace = Qnil;
23997 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23998 doc: /* *Control highlighting of nobreak space and soft hyphen.
23999 A value of t means highlight the character itself (for nobreak space,
24000 use face `nobreak-space').
24001 A value of nil means no highlighting.
24002 Other values mean display the escape glyph followed by an ordinary
24003 space or ordinary hyphen. */);
24004 Vnobreak_char_display = Qt;
24006 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24007 doc: /* *The pointer shape to show in void text areas.
24008 A value of nil means to show the text pointer. Other options are `arrow',
24009 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24010 Vvoid_text_area_pointer = Qarrow;
24012 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24013 doc: /* Non-nil means don't actually do any redisplay.
24014 This is used for internal purposes. */);
24015 Vinhibit_redisplay = Qnil;
24017 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24018 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24019 Vglobal_mode_string = Qnil;
24021 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24022 doc: /* Marker for where to display an arrow on top of the buffer text.
24023 This must be the beginning of a line in order to work.
24024 See also `overlay-arrow-string'. */);
24025 Voverlay_arrow_position = Qnil;
24027 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24028 doc: /* String to display as an arrow in non-window frames.
24029 See also `overlay-arrow-position'. */);
24030 Voverlay_arrow_string = build_string ("=>");
24032 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24033 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24034 The symbols on this list are examined during redisplay to determine
24035 where to display overlay arrows. */);
24036 Voverlay_arrow_variable_list
24037 = Fcons (intern ("overlay-arrow-position"), Qnil);
24039 DEFVAR_INT ("scroll-step", &scroll_step,
24040 doc: /* *The number of lines to try scrolling a window by when point moves out.
24041 If that fails to bring point back on frame, point is centered instead.
24042 If this is zero, point is always centered after it moves off frame.
24043 If you want scrolling to always be a line at a time, you should set
24044 `scroll-conservatively' to a large value rather than set this to 1. */);
24046 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24047 doc: /* *Scroll up to this many lines, to bring point back on screen.
24048 A value of zero means to scroll the text to center point vertically
24049 in the window. */);
24050 scroll_conservatively = 0;
24052 DEFVAR_INT ("scroll-margin", &scroll_margin,
24053 doc: /* *Number of lines of margin at the top and bottom of a window.
24054 Recenter the window whenever point gets within this many lines
24055 of the top or bottom of the window. */);
24056 scroll_margin = 0;
24058 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24059 doc: /* Pixels per inch value for non-window system displays.
24060 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24061 Vdisplay_pixels_per_inch = make_float (72.0);
24063 #if GLYPH_DEBUG
24064 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24065 #endif
24067 DEFVAR_BOOL ("truncate-partial-width-windows",
24068 &truncate_partial_width_windows,
24069 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24070 truncate_partial_width_windows = 1;
24072 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24073 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24074 Any other value means to use the appropriate face, `mode-line',
24075 `header-line', or `menu' respectively. */);
24076 mode_line_inverse_video = 1;
24078 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24079 doc: /* *Maximum buffer size for which line number should be displayed.
24080 If the buffer is bigger than this, the line number does not appear
24081 in the mode line. A value of nil means no limit. */);
24082 Vline_number_display_limit = Qnil;
24084 DEFVAR_INT ("line-number-display-limit-width",
24085 &line_number_display_limit_width,
24086 doc: /* *Maximum line width (in characters) for line number display.
24087 If the average length of the lines near point is bigger than this, then the
24088 line number may be omitted from the mode line. */);
24089 line_number_display_limit_width = 200;
24091 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24092 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24093 highlight_nonselected_windows = 0;
24095 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24096 doc: /* Non-nil if more than one frame is visible on this display.
24097 Minibuffer-only frames don't count, but iconified frames do.
24098 This variable is not guaranteed to be accurate except while processing
24099 `frame-title-format' and `icon-title-format'. */);
24101 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24102 doc: /* Template for displaying the title bar of visible frames.
24103 \(Assuming the window manager supports this feature.)
24105 This variable has the same structure as `mode-line-format', except that
24106 the %c and %l constructs are ignored. It is used only on frames for
24107 which no explicit name has been set \(see `modify-frame-parameters'). */);
24109 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24110 doc: /* Template for displaying the title bar of an iconified frame.
24111 \(Assuming the window manager supports this feature.)
24112 This variable has the same structure as `mode-line-format' (which see),
24113 and is used only on frames for which no explicit name has been set
24114 \(see `modify-frame-parameters'). */);
24115 Vicon_title_format
24116 = Vframe_title_format
24117 = Fcons (intern ("multiple-frames"),
24118 Fcons (build_string ("%b"),
24119 Fcons (Fcons (empty_string,
24120 Fcons (intern ("invocation-name"),
24121 Fcons (build_string ("@"),
24122 Fcons (intern ("system-name"),
24123 Qnil)))),
24124 Qnil)));
24126 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24127 doc: /* Maximum number of lines to keep in the message log buffer.
24128 If nil, disable message logging. If t, log messages but don't truncate
24129 the buffer when it becomes large. */);
24130 Vmessage_log_max = make_number (100);
24132 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24133 doc: /* Functions called before redisplay, if window sizes have changed.
24134 The value should be a list of functions that take one argument.
24135 Just before redisplay, for each frame, if any of its windows have changed
24136 size since the last redisplay, or have been split or deleted,
24137 all the functions in the list are called, with the frame as argument. */);
24138 Vwindow_size_change_functions = Qnil;
24140 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24141 doc: /* List of functions to call before redisplaying a window with scrolling.
24142 Each function is called with two arguments, the window
24143 and its new display-start position. Note that the value of `window-end'
24144 is not valid when these functions are called. */);
24145 Vwindow_scroll_functions = Qnil;
24147 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24148 doc: /* Functions called when redisplay of a window reaches the end trigger.
24149 Each function is called with two arguments, the window and the end trigger value.
24150 See `set-window-redisplay-end-trigger'. */);
24151 Vredisplay_end_trigger_functions = Qnil;
24153 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24154 doc: /* *Non-nil means autoselect window with mouse pointer.
24155 If nil, do not autoselect windows.
24156 A positive number means delay autoselection by that many seconds: a
24157 window is autoselected only after the mouse has remained in that
24158 window for the duration of the delay.
24159 A negative number has a similar effect, but causes windows to be
24160 autoselected only after the mouse has stopped moving. \(Because of
24161 the way Emacs compares mouse events, you will occasionally wait twice
24162 that time before the window gets selected.\)
24163 Any other value means to autoselect window instantaneously when the
24164 mouse pointer enters it.
24166 Autoselection selects the minibuffer only if it is active, and never
24167 unselects the minibuffer if it is active.
24169 When customizing this variable make sure that the actual value of
24170 `focus-follows-mouse' matches the behavior of your window manager. */);
24171 Vmouse_autoselect_window = Qnil;
24173 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24174 doc: /* *Non-nil means automatically resize tool-bars.
24175 This dynamically changes the tool-bar's height to the minimum height
24176 that is needed to make all tool-bar items visible.
24177 If value is `grow-only', the tool-bar's height is only increased
24178 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24179 Vauto_resize_tool_bars = Qt;
24181 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24182 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24183 auto_raise_tool_bar_buttons_p = 1;
24185 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24186 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24187 make_cursor_line_fully_visible_p = 1;
24189 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24190 doc: /* *Border below tool-bar in pixels.
24191 If an integer, use it as the height of the border.
24192 If it is one of `internal-border-width' or `border-width', use the
24193 value of the corresponding frame parameter.
24194 Otherwise, no border is added below the tool-bar. */);
24195 Vtool_bar_border = Qinternal_border_width;
24197 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24198 doc: /* *Margin around tool-bar buttons in pixels.
24199 If an integer, use that for both horizontal and vertical margins.
24200 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24201 HORZ specifying the horizontal margin, and VERT specifying the
24202 vertical margin. */);
24203 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24205 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24206 doc: /* *Relief thickness of tool-bar buttons. */);
24207 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24209 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24210 doc: /* List of functions to call to fontify regions of text.
24211 Each function is called with one argument POS. Functions must
24212 fontify a region starting at POS in the current buffer, and give
24213 fontified regions the property `fontified'. */);
24214 Vfontification_functions = Qnil;
24215 Fmake_variable_buffer_local (Qfontification_functions);
24217 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24218 &unibyte_display_via_language_environment,
24219 doc: /* *Non-nil means display unibyte text according to language environment.
24220 Specifically this means that unibyte non-ASCII characters
24221 are displayed by converting them to the equivalent multibyte characters
24222 according to the current language environment. As a result, they are
24223 displayed according to the current fontset. */);
24224 unibyte_display_via_language_environment = 0;
24226 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24227 doc: /* *Maximum height for resizing mini-windows.
24228 If a float, it specifies a fraction of the mini-window frame's height.
24229 If an integer, it specifies a number of lines. */);
24230 Vmax_mini_window_height = make_float (0.25);
24232 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24233 doc: /* *How to resize mini-windows.
24234 A value of nil means don't automatically resize mini-windows.
24235 A value of t means resize them to fit the text displayed in them.
24236 A value of `grow-only', the default, means let mini-windows grow
24237 only, until their display becomes empty, at which point the windows
24238 go back to their normal size. */);
24239 Vresize_mini_windows = Qgrow_only;
24241 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24242 doc: /* Alist specifying how to blink the cursor off.
24243 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24244 `cursor-type' frame-parameter or variable equals ON-STATE,
24245 comparing using `equal', Emacs uses OFF-STATE to specify
24246 how to blink it off. ON-STATE and OFF-STATE are values for
24247 the `cursor-type' frame parameter.
24249 If a frame's ON-STATE has no entry in this list,
24250 the frame's other specifications determine how to blink the cursor off. */);
24251 Vblink_cursor_alist = Qnil;
24253 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24254 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24255 automatic_hscrolling_p = 1;
24257 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24258 doc: /* *How many columns away from the window edge point is allowed to get
24259 before automatic hscrolling will horizontally scroll the window. */);
24260 hscroll_margin = 5;
24262 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24263 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24264 When point is less than `hscroll-margin' columns from the window
24265 edge, automatic hscrolling will scroll the window by the amount of columns
24266 determined by this variable. If its value is a positive integer, scroll that
24267 many columns. If it's a positive floating-point number, it specifies the
24268 fraction of the window's width to scroll. If it's nil or zero, point will be
24269 centered horizontally after the scroll. Any other value, including negative
24270 numbers, are treated as if the value were zero.
24272 Automatic hscrolling always moves point outside the scroll margin, so if
24273 point was more than scroll step columns inside the margin, the window will
24274 scroll more than the value given by the scroll step.
24276 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24277 and `scroll-right' overrides this variable's effect. */);
24278 Vhscroll_step = make_number (0);
24280 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24281 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24282 Bind this around calls to `message' to let it take effect. */);
24283 message_truncate_lines = 0;
24285 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24286 doc: /* Normal hook run to update the menu bar definitions.
24287 Redisplay runs this hook before it redisplays the menu bar.
24288 This is used to update submenus such as Buffers,
24289 whose contents depend on various data. */);
24290 Vmenu_bar_update_hook = Qnil;
24292 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24293 doc: /* Frame for which we are updating a menu.
24294 The enable predicate for a menu binding should check this variable. */);
24295 Vmenu_updating_frame = Qnil;
24297 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24298 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24299 inhibit_menubar_update = 0;
24301 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24302 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24303 inhibit_eval_during_redisplay = 0;
24305 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24306 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24307 inhibit_free_realized_faces = 0;
24309 #if GLYPH_DEBUG
24310 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24311 doc: /* Inhibit try_window_id display optimization. */);
24312 inhibit_try_window_id = 0;
24314 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24315 doc: /* Inhibit try_window_reusing display optimization. */);
24316 inhibit_try_window_reusing = 0;
24318 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24319 doc: /* Inhibit try_cursor_movement display optimization. */);
24320 inhibit_try_cursor_movement = 0;
24321 #endif /* GLYPH_DEBUG */
24323 DEFVAR_INT ("overline-margin", &overline_margin,
24324 doc: /* *Space between overline and text, in pixels.
24325 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24326 margin to the caracter height. */);
24327 overline_margin = 2;
24331 /* Initialize this module when Emacs starts. */
24333 void
24334 init_xdisp ()
24336 Lisp_Object root_window;
24337 struct window *mini_w;
24339 current_header_line_height = current_mode_line_height = -1;
24341 CHARPOS (this_line_start_pos) = 0;
24343 mini_w = XWINDOW (minibuf_window);
24344 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24346 if (!noninteractive)
24348 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24349 int i;
24351 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24352 set_window_height (root_window,
24353 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24355 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24356 set_window_height (minibuf_window, 1, 0);
24358 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24359 mini_w->total_cols = make_number (FRAME_COLS (f));
24361 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24362 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24363 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24365 /* The default ellipsis glyphs `...'. */
24366 for (i = 0; i < 3; ++i)
24367 default_invis_vector[i] = make_number ('.');
24371 /* Allocate the buffer for frame titles.
24372 Also used for `format-mode-line'. */
24373 int size = 100;
24374 mode_line_noprop_buf = (char *) xmalloc (size);
24375 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24376 mode_line_noprop_ptr = mode_line_noprop_buf;
24377 mode_line_target = MODE_LINE_DISPLAY;
24380 help_echo_showing_p = 0;
24384 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24385 (do not change this comment) */