[USE_ATSUI] (mac_draw_image_string_atsui) [MAC_OS_X]: Fix coordinate flipping.
[emacs.git] / src / xdisp.c
blob20d30f3217ae8238d28e74f75410a46fe4d5c0c4
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
25 Redisplay.
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
64 expose_window (asynchronous) |
66 X expose events -----+
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
90 Direct operations.
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
111 Desired matrices.
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
208 #define INFINITY 10000000
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
216 extern int interrupt_input;
217 extern int command_loop_level;
219 extern Lisp_Object do_mouse_tracking;
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
249 Lisp_Object Qrisky_local_variable;
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
254 /* Functions called to fontify regions of text. */
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
259 /* Non-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_autoselect_window;
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
266 int auto_raise_tool_bar_buttons_p;
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
270 int make_cursor_line_fully_visible_p;
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
276 Lisp_Object Vtool_bar_border;
278 /* Margin around tool bar buttons in pixels. */
280 Lisp_Object Vtool_bar_button_margin;
282 /* Thickness of shadow to draw around tool bar buttons. */
284 EMACS_INT tool_bar_button_relief;
286 /* Non-nil means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain.
289 If value is `grow-only', only make tool-bar bigger. */
291 Lisp_Object Vauto_resize_tool_bars;
293 /* Non-zero means draw block and hollow cursor as wide as the glyph
294 under it. For example, if a block cursor is over a tab, it will be
295 drawn as wide as that tab on the display. */
297 int x_stretch_cursor_p;
299 /* Non-nil means don't actually do any redisplay. */
301 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
303 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
305 int inhibit_eval_during_redisplay;
307 /* Names of text properties relevant for redisplay. */
309 Lisp_Object Qdisplay;
310 extern Lisp_Object Qface, Qinvisible, Qwidth;
312 /* Symbols used in text property values. */
314 Lisp_Object Vdisplay_pixels_per_inch;
315 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
316 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
317 Lisp_Object Qslice;
318 Lisp_Object Qcenter;
319 Lisp_Object Qmargin, Qpointer;
320 Lisp_Object Qline_height;
321 extern Lisp_Object Qheight;
322 extern Lisp_Object QCwidth, QCheight, QCascent;
323 extern Lisp_Object Qscroll_bar;
324 extern Lisp_Object Qcursor;
326 /* Non-nil means highlight trailing whitespace. */
328 Lisp_Object Vshow_trailing_whitespace;
330 /* Non-nil means escape non-break space and hyphens. */
332 Lisp_Object Vnobreak_char_display;
334 #ifdef HAVE_WINDOW_SYSTEM
335 extern Lisp_Object Voverflow_newline_into_fringe;
337 /* Test if overflow newline into fringe. Called with iterator IT
338 at or past right window margin, and with IT->current_x set. */
340 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
341 (!NILP (Voverflow_newline_into_fringe) \
342 && FRAME_WINDOW_P (it->f) \
343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
344 && it->current_x == it->last_visible_x)
346 #endif /* HAVE_WINDOW_SYSTEM */
348 /* Non-nil means show the text cursor in void text areas
349 i.e. in blank areas after eol and eob. This used to be
350 the default in 21.3. */
352 Lisp_Object Vvoid_text_area_pointer;
354 /* Name of the face used to highlight trailing whitespace. */
356 Lisp_Object Qtrailing_whitespace;
358 /* Name and number of the face used to highlight escape glyphs. */
360 Lisp_Object Qescape_glyph;
362 /* Name and number of the face used to highlight non-breaking spaces. */
364 Lisp_Object Qnobreak_space;
366 /* The symbol `image' which is the car of the lists used to represent
367 images in Lisp. */
369 Lisp_Object Qimage;
371 /* The image map types. */
372 Lisp_Object QCmap, QCpointer;
373 Lisp_Object Qrect, Qcircle, Qpoly;
375 /* Non-zero means print newline to stdout before next mini-buffer
376 message. */
378 int noninteractive_need_newline;
380 /* Non-zero means print newline to message log before next message. */
382 static int message_log_need_newline;
384 /* Three markers that message_dolog uses.
385 It could allocate them itself, but that causes trouble
386 in handling memory-full errors. */
387 static Lisp_Object message_dolog_marker1;
388 static Lisp_Object message_dolog_marker2;
389 static Lisp_Object message_dolog_marker3;
391 /* The buffer position of the first character appearing entirely or
392 partially on the line of the selected window which contains the
393 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
394 redisplay optimization in redisplay_internal. */
396 static struct text_pos this_line_start_pos;
398 /* Number of characters past the end of the line above, including the
399 terminating newline. */
401 static struct text_pos this_line_end_pos;
403 /* The vertical positions and the height of this line. */
405 static int this_line_vpos;
406 static int this_line_y;
407 static int this_line_pixel_height;
409 /* X position at which this display line starts. Usually zero;
410 negative if first character is partially visible. */
412 static int this_line_start_x;
414 /* Buffer that this_line_.* variables are referring to. */
416 static struct buffer *this_line_buffer;
418 /* Nonzero means truncate lines in all windows less wide than the
419 frame. */
421 int truncate_partial_width_windows;
423 /* A flag to control how to display unibyte 8-bit character. */
425 int unibyte_display_via_language_environment;
427 /* Nonzero means we have more than one non-mini-buffer-only frame.
428 Not guaranteed to be accurate except while parsing
429 frame-title-format. */
431 int multiple_frames;
433 Lisp_Object Vglobal_mode_string;
436 /* List of variables (symbols) which hold markers for overlay arrows.
437 The symbols on this list are examined during redisplay to determine
438 where to display overlay arrows. */
440 Lisp_Object Voverlay_arrow_variable_list;
442 /* Marker for where to display an arrow on top of the buffer text. */
444 Lisp_Object Voverlay_arrow_position;
446 /* String to display for the arrow. Only used on terminal frames. */
448 Lisp_Object Voverlay_arrow_string;
450 /* Values of those variables at last redisplay are stored as
451 properties on `overlay-arrow-position' symbol. However, if
452 Voverlay_arrow_position is a marker, last-arrow-position is its
453 numerical position. */
455 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
457 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
458 properties on a symbol in overlay-arrow-variable-list. */
460 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
462 /* Like mode-line-format, but for the title bar on a visible frame. */
464 Lisp_Object Vframe_title_format;
466 /* Like mode-line-format, but for the title bar on an iconified frame. */
468 Lisp_Object Vicon_title_format;
470 /* List of functions to call when a window's size changes. These
471 functions get one arg, a frame on which one or more windows' sizes
472 have changed. */
474 static Lisp_Object Vwindow_size_change_functions;
476 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
478 /* Nonzero if an overlay arrow has been displayed in this window. */
480 static int overlay_arrow_seen;
482 /* Nonzero means highlight the region even in nonselected windows. */
484 int highlight_nonselected_windows;
486 /* If cursor motion alone moves point off frame, try scrolling this
487 many lines up or down if that will bring it back. */
489 static EMACS_INT scroll_step;
491 /* Nonzero means scroll just far enough to bring point back on the
492 screen, when appropriate. */
494 static EMACS_INT scroll_conservatively;
496 /* Recenter the window whenever point gets within this many lines of
497 the top or bottom of the window. This value is translated into a
498 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
499 that there is really a fixed pixel height scroll margin. */
501 EMACS_INT scroll_margin;
503 /* Number of windows showing the buffer of the selected window (or
504 another buffer with the same base buffer). keyboard.c refers to
505 this. */
507 int buffer_shared;
509 /* Vector containing glyphs for an ellipsis `...'. */
511 static Lisp_Object default_invis_vector[3];
513 /* Zero means display the mode-line/header-line/menu-bar in the default face
514 (this slightly odd definition is for compatibility with previous versions
515 of emacs), non-zero means display them using their respective faces.
517 This variable is deprecated. */
519 int mode_line_inverse_video;
521 /* Prompt to display in front of the mini-buffer contents. */
523 Lisp_Object minibuf_prompt;
525 /* Width of current mini-buffer prompt. Only set after display_line
526 of the line that contains the prompt. */
528 int minibuf_prompt_width;
530 /* This is the window where the echo area message was displayed. It
531 is always a mini-buffer window, but it may not be the same window
532 currently active as a mini-buffer. */
534 Lisp_Object echo_area_window;
536 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
537 pushes the current message and the value of
538 message_enable_multibyte on the stack, the function restore_message
539 pops the stack and displays MESSAGE again. */
541 Lisp_Object Vmessage_stack;
543 /* Nonzero means multibyte characters were enabled when the echo area
544 message was specified. */
546 int message_enable_multibyte;
548 /* Nonzero if we should redraw the mode lines on the next redisplay. */
550 int update_mode_lines;
552 /* Nonzero if window sizes or contents have changed since last
553 redisplay that finished. */
555 int windows_or_buffers_changed;
557 /* Nonzero means a frame's cursor type has been changed. */
559 int cursor_type_changed;
561 /* Nonzero after display_mode_line if %l was used and it displayed a
562 line number. */
564 int line_number_displayed;
566 /* Maximum buffer size for which to display line numbers. */
568 Lisp_Object Vline_number_display_limit;
570 /* Line width to consider when repositioning for line number display. */
572 static EMACS_INT line_number_display_limit_width;
574 /* Number of lines to keep in the message log buffer. t means
575 infinite. nil means don't log at all. */
577 Lisp_Object Vmessage_log_max;
579 /* The name of the *Messages* buffer, a string. */
581 static Lisp_Object Vmessages_buffer_name;
583 /* Index 0 is the buffer that holds the current (desired) echo area message,
584 or nil if none is desired right now.
586 Index 1 is the buffer that holds the previously displayed echo area message,
587 or nil to indicate no message. This is normally what's on the screen now.
589 These two can point to the same buffer. That happens when the last
590 message output by the user (or made by echoing) has been displayed. */
592 Lisp_Object echo_area_buffer[2];
594 /* Permanent pointers to the two buffers that are used for echo area
595 purposes. Once the two buffers are made, and their pointers are
596 placed here, these two slots remain unchanged unless those buffers
597 need to be created afresh. */
599 static Lisp_Object echo_buffer[2];
601 /* A vector saved used in with_area_buffer to reduce consing. */
603 static Lisp_Object Vwith_echo_area_save_vector;
605 /* Non-zero means display_echo_area should display the last echo area
606 message again. Set by redisplay_preserve_echo_area. */
608 static int display_last_displayed_message_p;
610 /* Nonzero if echo area is being used by print; zero if being used by
611 message. */
613 int message_buf_print;
615 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
617 Lisp_Object Qinhibit_menubar_update;
618 int inhibit_menubar_update;
620 /* When evaluating expressions from menu bar items (enable conditions,
621 for instance), this is the frame they are being processed for. */
623 Lisp_Object Vmenu_updating_frame;
625 /* Maximum height for resizing mini-windows. Either a float
626 specifying a fraction of the available height, or an integer
627 specifying a number of lines. */
629 Lisp_Object Vmax_mini_window_height;
631 /* Non-zero means messages should be displayed with truncated
632 lines instead of being continued. */
634 int message_truncate_lines;
635 Lisp_Object Qmessage_truncate_lines;
637 /* Set to 1 in clear_message to make redisplay_internal aware
638 of an emptied echo area. */
640 static int message_cleared_p;
642 /* How to blink the default frame cursor off. */
643 Lisp_Object Vblink_cursor_alist;
645 /* A scratch glyph row with contents used for generating truncation
646 glyphs. Also used in direct_output_for_insert. */
648 #define MAX_SCRATCH_GLYPHS 100
649 struct glyph_row scratch_glyph_row;
650 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
652 /* Ascent and height of the last line processed by move_it_to. */
654 static int last_max_ascent, last_height;
656 /* Non-zero if there's a help-echo in the echo area. */
658 int help_echo_showing_p;
660 /* If >= 0, computed, exact values of mode-line and header-line height
661 to use in the macros CURRENT_MODE_LINE_HEIGHT and
662 CURRENT_HEADER_LINE_HEIGHT. */
664 int current_mode_line_height, current_header_line_height;
666 /* The maximum distance to look ahead for text properties. Values
667 that are too small let us call compute_char_face and similar
668 functions too often which is expensive. Values that are too large
669 let us call compute_char_face and alike too often because we
670 might not be interested in text properties that far away. */
672 #define TEXT_PROP_DISTANCE_LIMIT 100
674 #if GLYPH_DEBUG
676 /* Variables to turn off display optimizations from Lisp. */
678 int inhibit_try_window_id, inhibit_try_window_reusing;
679 int inhibit_try_cursor_movement;
681 /* Non-zero means print traces of redisplay if compiled with
682 GLYPH_DEBUG != 0. */
684 int trace_redisplay_p;
686 #endif /* GLYPH_DEBUG */
688 #ifdef DEBUG_TRACE_MOVE
689 /* Non-zero means trace with TRACE_MOVE to stderr. */
690 int trace_move;
692 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
693 #else
694 #define TRACE_MOVE(x) (void) 0
695 #endif
697 /* Non-zero means automatically scroll windows horizontally to make
698 point visible. */
700 int automatic_hscrolling_p;
702 /* How close to the margin can point get before the window is scrolled
703 horizontally. */
704 EMACS_INT hscroll_margin;
706 /* How much to scroll horizontally when point is inside the above margin. */
707 Lisp_Object Vhscroll_step;
709 /* The variable `resize-mini-windows'. If nil, don't resize
710 mini-windows. If t, always resize them to fit the text they
711 display. If `grow-only', let mini-windows grow only until they
712 become empty. */
714 Lisp_Object Vresize_mini_windows;
716 /* Buffer being redisplayed -- for redisplay_window_error. */
718 struct buffer *displayed_buffer;
720 /* Space between overline and text. */
722 EMACS_INT overline_margin;
724 /* Value returned from text property handlers (see below). */
726 enum prop_handled
728 HANDLED_NORMALLY,
729 HANDLED_RECOMPUTE_PROPS,
730 HANDLED_OVERLAY_STRING_CONSUMED,
731 HANDLED_RETURN
734 /* A description of text properties that redisplay is interested
735 in. */
737 struct props
739 /* The name of the property. */
740 Lisp_Object *name;
742 /* A unique index for the property. */
743 enum prop_idx idx;
745 /* A handler function called to set up iterator IT from the property
746 at IT's current position. Value is used to steer handle_stop. */
747 enum prop_handled (*handler) P_ ((struct it *it));
750 static enum prop_handled handle_face_prop P_ ((struct it *));
751 static enum prop_handled handle_invisible_prop P_ ((struct it *));
752 static enum prop_handled handle_display_prop P_ ((struct it *));
753 static enum prop_handled handle_composition_prop P_ ((struct it *));
754 static enum prop_handled handle_overlay_change P_ ((struct it *));
755 static enum prop_handled handle_fontified_prop P_ ((struct it *));
757 /* Properties handled by iterators. */
759 static struct props it_props[] =
761 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
762 /* Handle `face' before `display' because some sub-properties of
763 `display' need to know the face. */
764 {&Qface, FACE_PROP_IDX, handle_face_prop},
765 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
766 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
767 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
768 {NULL, 0, NULL}
771 /* Value is the position described by X. If X is a marker, value is
772 the marker_position of X. Otherwise, value is X. */
774 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
776 /* Enumeration returned by some move_it_.* functions internally. */
778 enum move_it_result
780 /* Not used. Undefined value. */
781 MOVE_UNDEFINED,
783 /* Move ended at the requested buffer position or ZV. */
784 MOVE_POS_MATCH_OR_ZV,
786 /* Move ended at the requested X pixel position. */
787 MOVE_X_REACHED,
789 /* Move within a line ended at the end of a line that must be
790 continued. */
791 MOVE_LINE_CONTINUED,
793 /* Move within a line ended at the end of a line that would
794 be displayed truncated. */
795 MOVE_LINE_TRUNCATED,
797 /* Move within a line ended at a line end. */
798 MOVE_NEWLINE_OR_CR
801 /* This counter is used to clear the face cache every once in a while
802 in redisplay_internal. It is incremented for each redisplay.
803 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
804 cleared. */
806 #define CLEAR_FACE_CACHE_COUNT 500
807 static int clear_face_cache_count;
809 /* Similarly for the image cache. */
811 #ifdef HAVE_WINDOW_SYSTEM
812 #define CLEAR_IMAGE_CACHE_COUNT 101
813 static int clear_image_cache_count;
814 #endif
816 /* Record the previous terminal frame we displayed. */
818 static struct frame *previous_terminal_frame;
820 /* Non-zero while redisplay_internal is in progress. */
822 int redisplaying_p;
824 /* Non-zero means don't free realized faces. Bound while freeing
825 realized faces is dangerous because glyph matrices might still
826 reference them. */
828 int inhibit_free_realized_faces;
829 Lisp_Object Qinhibit_free_realized_faces;
831 /* If a string, XTread_socket generates an event to display that string.
832 (The display is done in read_char.) */
834 Lisp_Object help_echo_string;
835 Lisp_Object help_echo_window;
836 Lisp_Object help_echo_object;
837 int help_echo_pos;
839 /* Temporary variable for XTread_socket. */
841 Lisp_Object previous_help_echo_string;
843 /* Null glyph slice */
845 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
848 /* Function prototypes. */
850 static void setup_for_ellipsis P_ ((struct it *, int));
851 static void mark_window_display_accurate_1 P_ ((struct window *, int));
852 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
853 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
854 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
855 static int redisplay_mode_lines P_ ((Lisp_Object, int));
856 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
858 #if 0
859 static int invisible_text_between_p P_ ((struct it *, int, int));
860 #endif
862 static void pint2str P_ ((char *, int, int));
863 static void pint2hrstr P_ ((char *, int, int));
864 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
865 struct text_pos));
866 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
867 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
868 static void store_mode_line_noprop_char P_ ((char));
869 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
870 static void x_consider_frame_title P_ ((Lisp_Object));
871 static void handle_stop P_ ((struct it *));
872 static int tool_bar_lines_needed P_ ((struct frame *, int *));
873 static int single_display_spec_intangible_p P_ ((Lisp_Object));
874 static void ensure_echo_area_buffers P_ ((void));
875 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
876 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
877 static int with_echo_area_buffer P_ ((struct window *, int,
878 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
879 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static void clear_garbaged_frames P_ ((void));
881 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
883 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
884 static int display_echo_area P_ ((struct window *));
885 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
886 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
887 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
888 static int string_char_and_length P_ ((const unsigned char *, int, int *));
889 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
890 struct text_pos));
891 static int compute_window_start_on_continuation_line P_ ((struct window *));
892 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
893 static void insert_left_trunc_glyphs P_ ((struct it *));
894 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
895 Lisp_Object));
896 static void extend_face_to_end_of_line P_ ((struct it *));
897 static int append_space_for_newline P_ ((struct it *, int));
898 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
899 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
900 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
901 static int trailing_whitespace_p P_ ((int));
902 static int message_log_check_duplicate P_ ((int, int, int, int));
903 static void push_it P_ ((struct it *));
904 static void pop_it P_ ((struct it *));
905 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
906 static void select_frame_for_redisplay P_ ((Lisp_Object));
907 static void redisplay_internal P_ ((int));
908 static int echo_area_display P_ ((int));
909 static void redisplay_windows P_ ((Lisp_Object));
910 static void redisplay_window P_ ((Lisp_Object, int));
911 static Lisp_Object redisplay_window_error ();
912 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
913 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
914 static int update_menu_bar P_ ((struct frame *, int, int));
915 static int try_window_reusing_current_matrix P_ ((struct window *));
916 static int try_window_id P_ ((struct window *));
917 static int display_line P_ ((struct it *));
918 static int display_mode_lines P_ ((struct window *));
919 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
920 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
921 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
922 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
923 static void display_menu_bar P_ ((struct window *));
924 static int display_count_lines P_ ((int, int, int, int, int *));
925 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
926 int, int, struct it *, int, int, int, int));
927 static void compute_line_metrics P_ ((struct it *));
928 static void run_redisplay_end_trigger_hook P_ ((struct it *));
929 static int get_overlay_strings P_ ((struct it *, int));
930 static int get_overlay_strings_1 P_ ((struct it *, int, int));
931 static void next_overlay_string P_ ((struct it *));
932 static void reseat P_ ((struct it *, struct text_pos, int));
933 static void reseat_1 P_ ((struct it *, struct text_pos, int));
934 static void back_to_previous_visible_line_start P_ ((struct it *));
935 void reseat_at_previous_visible_line_start P_ ((struct it *));
936 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
937 static int next_element_from_ellipsis P_ ((struct it *));
938 static int next_element_from_display_vector P_ ((struct it *));
939 static int next_element_from_string P_ ((struct it *));
940 static int next_element_from_c_string P_ ((struct it *));
941 static int next_element_from_buffer P_ ((struct it *));
942 static int next_element_from_composition P_ ((struct it *));
943 static int next_element_from_image P_ ((struct it *));
944 static int next_element_from_stretch P_ ((struct it *));
945 static void load_overlay_strings P_ ((struct it *, int));
946 static int init_from_display_pos P_ ((struct it *, struct window *,
947 struct display_pos *));
948 static void reseat_to_string P_ ((struct it *, unsigned char *,
949 Lisp_Object, int, int, int, int));
950 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
951 int, int, int));
952 void move_it_vertically_backward P_ ((struct it *, int));
953 static void init_to_row_start P_ ((struct it *, struct window *,
954 struct glyph_row *));
955 static int init_to_row_end P_ ((struct it *, struct window *,
956 struct glyph_row *));
957 static void back_to_previous_line_start P_ ((struct it *));
958 static int forward_to_next_line_start P_ ((struct it *, int *));
959 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
960 Lisp_Object, int));
961 static struct text_pos string_pos P_ ((int, Lisp_Object));
962 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
963 static int number_of_chars P_ ((unsigned char *, int));
964 static void compute_stop_pos P_ ((struct it *));
965 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
966 Lisp_Object));
967 static int face_before_or_after_it_pos P_ ((struct it *, int));
968 static int next_overlay_change P_ ((int));
969 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
970 Lisp_Object, Lisp_Object,
971 struct text_pos *, int));
972 static int underlying_face_id P_ ((struct it *));
973 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
974 struct window *));
976 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
977 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
979 #ifdef HAVE_WINDOW_SYSTEM
981 static void update_tool_bar P_ ((struct frame *, int));
982 static void build_desired_tool_bar_string P_ ((struct frame *f));
983 static int redisplay_tool_bar P_ ((struct frame *));
984 static void display_tool_bar_line P_ ((struct it *, int));
985 static void notice_overwritten_cursor P_ ((struct window *,
986 enum glyph_row_area,
987 int, int, int, int));
991 #endif /* HAVE_WINDOW_SYSTEM */
994 /***********************************************************************
995 Window display dimensions
996 ***********************************************************************/
998 /* Return the bottom boundary y-position for text lines in window W.
999 This is the first y position at which a line cannot start.
1000 It is relative to the top of the window.
1002 This is the height of W minus the height of a mode line, if any. */
1004 INLINE int
1005 window_text_bottom_y (w)
1006 struct window *w;
1008 int height = WINDOW_TOTAL_HEIGHT (w);
1010 if (WINDOW_WANTS_MODELINE_P (w))
1011 height -= CURRENT_MODE_LINE_HEIGHT (w);
1012 return height;
1015 /* Return the pixel width of display area AREA of window W. AREA < 0
1016 means return the total width of W, not including fringes to
1017 the left and right of the window. */
1019 INLINE int
1020 window_box_width (w, area)
1021 struct window *w;
1022 int area;
1024 int cols = XFASTINT (w->total_cols);
1025 int pixels = 0;
1027 if (!w->pseudo_window_p)
1029 cols -= WINDOW_SCROLL_BAR_COLS (w);
1031 if (area == TEXT_AREA)
1033 if (INTEGERP (w->left_margin_cols))
1034 cols -= XFASTINT (w->left_margin_cols);
1035 if (INTEGERP (w->right_margin_cols))
1036 cols -= XFASTINT (w->right_margin_cols);
1037 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1039 else if (area == LEFT_MARGIN_AREA)
1041 cols = (INTEGERP (w->left_margin_cols)
1042 ? XFASTINT (w->left_margin_cols) : 0);
1043 pixels = 0;
1045 else if (area == RIGHT_MARGIN_AREA)
1047 cols = (INTEGERP (w->right_margin_cols)
1048 ? XFASTINT (w->right_margin_cols) : 0);
1049 pixels = 0;
1053 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1057 /* Return the pixel height of the display area of window W, not
1058 including mode lines of W, if any. */
1060 INLINE int
1061 window_box_height (w)
1062 struct window *w;
1064 struct frame *f = XFRAME (w->frame);
1065 int height = WINDOW_TOTAL_HEIGHT (w);
1067 xassert (height >= 0);
1069 /* Note: the code below that determines the mode-line/header-line
1070 height is essentially the same as that contained in the macro
1071 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1072 the appropriate glyph row has its `mode_line_p' flag set,
1073 and if it doesn't, uses estimate_mode_line_height instead. */
1075 if (WINDOW_WANTS_MODELINE_P (w))
1077 struct glyph_row *ml_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (ml_row && ml_row->mode_line_p)
1082 height -= ml_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1087 if (WINDOW_WANTS_HEADER_LINE_P (w))
1089 struct glyph_row *hl_row
1090 = (w->current_matrix && w->current_matrix->rows
1091 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1092 : 0);
1093 if (hl_row && hl_row->mode_line_p)
1094 height -= hl_row->height;
1095 else
1096 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1099 /* With a very small font and a mode-line that's taller than
1100 default, we might end up with a negative height. */
1101 return max (0, height);
1104 /* Return the window-relative coordinate of the left edge of display
1105 area AREA of window W. AREA < 0 means return the left edge of the
1106 whole window, to the right of the left fringe of W. */
1108 INLINE int
1109 window_box_left_offset (w, area)
1110 struct window *w;
1111 int area;
1113 int x;
1115 if (w->pseudo_window_p)
1116 return 0;
1118 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1120 if (area == TEXT_AREA)
1121 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1122 + window_box_width (w, LEFT_MARGIN_AREA));
1123 else if (area == RIGHT_MARGIN_AREA)
1124 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1125 + window_box_width (w, LEFT_MARGIN_AREA)
1126 + window_box_width (w, TEXT_AREA)
1127 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1129 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1130 else if (area == LEFT_MARGIN_AREA
1131 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1132 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1134 return x;
1138 /* Return the window-relative coordinate of the right edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the left of the right fringe of W. */
1142 INLINE int
1143 window_box_right_offset (w, area)
1144 struct window *w;
1145 int area;
1147 return window_box_left_offset (w, area) + window_box_width (w, area);
1150 /* Return the frame-relative coordinate of the left edge of display
1151 area AREA of window W. AREA < 0 means return the left edge of the
1152 whole window, to the right of the left fringe of W. */
1154 INLINE int
1155 window_box_left (w, area)
1156 struct window *w;
1157 int area;
1159 struct frame *f = XFRAME (w->frame);
1160 int x;
1162 if (w->pseudo_window_p)
1163 return FRAME_INTERNAL_BORDER_WIDTH (f);
1165 x = (WINDOW_LEFT_EDGE_X (w)
1166 + window_box_left_offset (w, area));
1168 return x;
1172 /* Return the frame-relative coordinate of the right edge of display
1173 area AREA of window W. AREA < 0 means return the left edge of the
1174 whole window, to the left of the right fringe of W. */
1176 INLINE int
1177 window_box_right (w, area)
1178 struct window *w;
1179 int area;
1181 return window_box_left (w, area) + window_box_width (w, area);
1184 /* Get the bounding box of the display area AREA of window W, without
1185 mode lines, in frame-relative coordinates. AREA < 0 means the
1186 whole window, not including the left and right fringes of
1187 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1188 coordinates of the upper-left corner of the box. Return in
1189 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1191 INLINE void
1192 window_box (w, area, box_x, box_y, box_width, box_height)
1193 struct window *w;
1194 int area;
1195 int *box_x, *box_y, *box_width, *box_height;
1197 if (box_width)
1198 *box_width = window_box_width (w, area);
1199 if (box_height)
1200 *box_height = window_box_height (w);
1201 if (box_x)
1202 *box_x = window_box_left (w, area);
1203 if (box_y)
1205 *box_y = WINDOW_TOP_EDGE_Y (w);
1206 if (WINDOW_WANTS_HEADER_LINE_P (w))
1207 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1212 /* Get the bounding box of the display area AREA of window W, without
1213 mode lines. AREA < 0 means the whole window, not including the
1214 left and right fringe of the window. Return in *TOP_LEFT_X
1215 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1216 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1217 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1218 box. */
1220 INLINE void
1221 window_box_edges (w, area, top_left_x, top_left_y,
1222 bottom_right_x, bottom_right_y)
1223 struct window *w;
1224 int area;
1225 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1227 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1228 bottom_right_y);
1229 *bottom_right_x += *top_left_x;
1230 *bottom_right_y += *top_left_y;
1235 /***********************************************************************
1236 Utilities
1237 ***********************************************************************/
1239 /* Return the bottom y-position of the line the iterator IT is in.
1240 This can modify IT's settings. */
1243 line_bottom_y (it)
1244 struct it *it;
1246 int line_height = it->max_ascent + it->max_descent;
1247 int line_top_y = it->current_y;
1249 if (line_height == 0)
1251 if (last_height)
1252 line_height = last_height;
1253 else if (IT_CHARPOS (*it) < ZV)
1255 move_it_by_lines (it, 1, 1);
1256 line_height = (it->max_ascent || it->max_descent
1257 ? it->max_ascent + it->max_descent
1258 : last_height);
1260 else
1262 struct glyph_row *row = it->glyph_row;
1264 /* Use the default character height. */
1265 it->glyph_row = NULL;
1266 it->what = IT_CHARACTER;
1267 it->c = ' ';
1268 it->len = 1;
1269 PRODUCE_GLYPHS (it);
1270 line_height = it->ascent + it->descent;
1271 it->glyph_row = row;
1275 return line_top_y + line_height;
1279 /* Return 1 if position CHARPOS is visible in window W.
1280 CHARPOS < 0 means return info about WINDOW_END position.
1281 If visible, set *X and *Y to pixel coordinates of top left corner.
1282 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1283 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1286 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1287 struct window *w;
1288 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1290 struct it it;
1291 struct text_pos top;
1292 int visible_p = 0;
1293 struct buffer *old_buffer = NULL;
1295 if (noninteractive)
1296 return visible_p;
1298 if (XBUFFER (w->buffer) != current_buffer)
1300 old_buffer = current_buffer;
1301 set_buffer_internal_1 (XBUFFER (w->buffer));
1304 SET_TEXT_POS_FROM_MARKER (top, w->start);
1306 /* Compute exact mode line heights. */
1307 if (WINDOW_WANTS_MODELINE_P (w))
1308 current_mode_line_height
1309 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1310 current_buffer->mode_line_format);
1312 if (WINDOW_WANTS_HEADER_LINE_P (w))
1313 current_header_line_height
1314 = display_mode_line (w, HEADER_LINE_FACE_ID,
1315 current_buffer->header_line_format);
1317 start_display (&it, w, top);
1318 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1319 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1321 /* Note that we may overshoot because of invisible text. */
1322 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1324 int top_x = it.current_x;
1325 int top_y = it.current_y;
1326 int bottom_y = (last_height = 0, line_bottom_y (&it));
1327 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1329 if (top_y < window_top_y)
1330 visible_p = bottom_y > window_top_y;
1331 else if (top_y < it.last_visible_y)
1332 visible_p = 1;
1333 if (visible_p)
1335 *x = top_x;
1336 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1337 *rtop = max (0, window_top_y - top_y);
1338 *rbot = max (0, bottom_y - it.last_visible_y);
1339 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1340 - max (top_y, window_top_y)));
1341 *vpos = it.vpos;
1344 else
1346 struct it it2;
1348 it2 = it;
1349 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1350 move_it_by_lines (&it, 1, 0);
1351 if (charpos < IT_CHARPOS (it)
1352 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1354 visible_p = 1;
1355 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1356 *x = it2.current_x;
1357 *y = it2.current_y + it2.max_ascent - it2.ascent;
1358 *rtop = max (0, -it2.current_y);
1359 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1360 - it.last_visible_y));
1361 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1362 it.last_visible_y)
1363 - max (it2.current_y,
1364 WINDOW_HEADER_LINE_HEIGHT (w))));
1365 *vpos = it2.vpos;
1369 if (old_buffer)
1370 set_buffer_internal_1 (old_buffer);
1372 current_header_line_height = current_mode_line_height = -1;
1374 if (visible_p && XFASTINT (w->hscroll) > 0)
1375 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1377 #if 0
1378 /* Debugging code. */
1379 if (visible_p)
1380 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1381 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1382 else
1383 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1384 #endif
1386 return visible_p;
1390 /* Return the next character from STR which is MAXLEN bytes long.
1391 Return in *LEN the length of the character. This is like
1392 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1393 we find one, we return a `?', but with the length of the invalid
1394 character. */
1396 static INLINE int
1397 string_char_and_length (str, maxlen, len)
1398 const unsigned char *str;
1399 int maxlen, *len;
1401 int c;
1403 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1404 if (!CHAR_VALID_P (c, 1))
1405 /* We may not change the length here because other places in Emacs
1406 don't use this function, i.e. they silently accept invalid
1407 characters. */
1408 c = '?';
1410 return c;
1415 /* Given a position POS containing a valid character and byte position
1416 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1418 static struct text_pos
1419 string_pos_nchars_ahead (pos, string, nchars)
1420 struct text_pos pos;
1421 Lisp_Object string;
1422 int nchars;
1424 xassert (STRINGP (string) && nchars >= 0);
1426 if (STRING_MULTIBYTE (string))
1428 int rest = SBYTES (string) - BYTEPOS (pos);
1429 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1430 int len;
1432 while (nchars--)
1434 string_char_and_length (p, rest, &len);
1435 p += len, rest -= len;
1436 xassert (rest >= 0);
1437 CHARPOS (pos) += 1;
1438 BYTEPOS (pos) += len;
1441 else
1442 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1444 return pos;
1448 /* Value is the text position, i.e. character and byte position,
1449 for character position CHARPOS in STRING. */
1451 static INLINE struct text_pos
1452 string_pos (charpos, string)
1453 int charpos;
1454 Lisp_Object string;
1456 struct text_pos pos;
1457 xassert (STRINGP (string));
1458 xassert (charpos >= 0);
1459 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1460 return pos;
1464 /* Value is a text position, i.e. character and byte position, for
1465 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1466 means recognize multibyte characters. */
1468 static struct text_pos
1469 c_string_pos (charpos, s, multibyte_p)
1470 int charpos;
1471 unsigned char *s;
1472 int multibyte_p;
1474 struct text_pos pos;
1476 xassert (s != NULL);
1477 xassert (charpos >= 0);
1479 if (multibyte_p)
1481 int rest = strlen (s), len;
1483 SET_TEXT_POS (pos, 0, 0);
1484 while (charpos--)
1486 string_char_and_length (s, rest, &len);
1487 s += len, rest -= len;
1488 xassert (rest >= 0);
1489 CHARPOS (pos) += 1;
1490 BYTEPOS (pos) += len;
1493 else
1494 SET_TEXT_POS (pos, charpos, charpos);
1496 return pos;
1500 /* Value is the number of characters in C string S. MULTIBYTE_P
1501 non-zero means recognize multibyte characters. */
1503 static int
1504 number_of_chars (s, multibyte_p)
1505 unsigned char *s;
1506 int multibyte_p;
1508 int nchars;
1510 if (multibyte_p)
1512 int rest = strlen (s), len;
1513 unsigned char *p = (unsigned char *) s;
1515 for (nchars = 0; rest > 0; ++nchars)
1517 string_char_and_length (p, rest, &len);
1518 rest -= len, p += len;
1521 else
1522 nchars = strlen (s);
1524 return nchars;
1528 /* Compute byte position NEWPOS->bytepos corresponding to
1529 NEWPOS->charpos. POS is a known position in string STRING.
1530 NEWPOS->charpos must be >= POS.charpos. */
1532 static void
1533 compute_string_pos (newpos, pos, string)
1534 struct text_pos *newpos, pos;
1535 Lisp_Object string;
1537 xassert (STRINGP (string));
1538 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1540 if (STRING_MULTIBYTE (string))
1541 *newpos = string_pos_nchars_ahead (pos, string,
1542 CHARPOS (*newpos) - CHARPOS (pos));
1543 else
1544 BYTEPOS (*newpos) = CHARPOS (*newpos);
1547 /* EXPORT:
1548 Return an estimation of the pixel height of mode or top lines on
1549 frame F. FACE_ID specifies what line's height to estimate. */
1552 estimate_mode_line_height (f, face_id)
1553 struct frame *f;
1554 enum face_id face_id;
1556 #ifdef HAVE_WINDOW_SYSTEM
1557 if (FRAME_WINDOW_P (f))
1559 int height = FONT_HEIGHT (FRAME_FONT (f));
1561 /* This function is called so early when Emacs starts that the face
1562 cache and mode line face are not yet initialized. */
1563 if (FRAME_FACE_CACHE (f))
1565 struct face *face = FACE_FROM_ID (f, face_id);
1566 if (face)
1568 if (face->font)
1569 height = FONT_HEIGHT (face->font);
1570 if (face->box_line_width > 0)
1571 height += 2 * face->box_line_width;
1575 return height;
1577 #endif
1579 return 1;
1582 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1583 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1584 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1585 not force the value into range. */
1587 void
1588 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1589 FRAME_PTR f;
1590 register int pix_x, pix_y;
1591 int *x, *y;
1592 NativeRectangle *bounds;
1593 int noclip;
1596 #ifdef HAVE_WINDOW_SYSTEM
1597 if (FRAME_WINDOW_P (f))
1599 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1600 even for negative values. */
1601 if (pix_x < 0)
1602 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1603 if (pix_y < 0)
1604 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1606 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1607 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1609 if (bounds)
1610 STORE_NATIVE_RECT (*bounds,
1611 FRAME_COL_TO_PIXEL_X (f, pix_x),
1612 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1613 FRAME_COLUMN_WIDTH (f) - 1,
1614 FRAME_LINE_HEIGHT (f) - 1);
1616 if (!noclip)
1618 if (pix_x < 0)
1619 pix_x = 0;
1620 else if (pix_x > FRAME_TOTAL_COLS (f))
1621 pix_x = FRAME_TOTAL_COLS (f);
1623 if (pix_y < 0)
1624 pix_y = 0;
1625 else if (pix_y > FRAME_LINES (f))
1626 pix_y = FRAME_LINES (f);
1629 #endif
1631 *x = pix_x;
1632 *y = pix_y;
1636 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1637 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1638 can't tell the positions because W's display is not up to date,
1639 return 0. */
1642 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1643 struct window *w;
1644 int hpos, vpos;
1645 int *frame_x, *frame_y;
1647 #ifdef HAVE_WINDOW_SYSTEM
1648 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1650 int success_p;
1652 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1653 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1655 if (display_completed)
1657 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1658 struct glyph *glyph = row->glyphs[TEXT_AREA];
1659 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1661 hpos = row->x;
1662 vpos = row->y;
1663 while (glyph < end)
1665 hpos += glyph->pixel_width;
1666 ++glyph;
1669 /* If first glyph is partially visible, its first visible position is still 0. */
1670 if (hpos < 0)
1671 hpos = 0;
1673 success_p = 1;
1675 else
1677 hpos = vpos = 0;
1678 success_p = 0;
1681 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1682 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1683 return success_p;
1685 #endif
1687 *frame_x = hpos;
1688 *frame_y = vpos;
1689 return 1;
1693 #ifdef HAVE_WINDOW_SYSTEM
1695 /* Find the glyph under window-relative coordinates X/Y in window W.
1696 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1697 strings. Return in *HPOS and *VPOS the row and column number of
1698 the glyph found. Return in *AREA the glyph area containing X.
1699 Value is a pointer to the glyph found or null if X/Y is not on
1700 text, or we can't tell because W's current matrix is not up to
1701 date. */
1703 #ifndef HAVE_CARBON
1704 static
1705 #endif
1706 struct glyph *
1707 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1708 struct window *w;
1709 int x, y;
1710 int *hpos, *vpos, *dx, *dy, *area;
1712 struct glyph *glyph, *end;
1713 struct glyph_row *row = NULL;
1714 int x0, i;
1716 /* Find row containing Y. Give up if some row is not enabled. */
1717 for (i = 0; i < w->current_matrix->nrows; ++i)
1719 row = MATRIX_ROW (w->current_matrix, i);
1720 if (!row->enabled_p)
1721 return NULL;
1722 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1723 break;
1726 *vpos = i;
1727 *hpos = 0;
1729 /* Give up if Y is not in the window. */
1730 if (i == w->current_matrix->nrows)
1731 return NULL;
1733 /* Get the glyph area containing X. */
1734 if (w->pseudo_window_p)
1736 *area = TEXT_AREA;
1737 x0 = 0;
1739 else
1741 if (x < window_box_left_offset (w, TEXT_AREA))
1743 *area = LEFT_MARGIN_AREA;
1744 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1746 else if (x < window_box_right_offset (w, TEXT_AREA))
1748 *area = TEXT_AREA;
1749 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1751 else
1753 *area = RIGHT_MARGIN_AREA;
1754 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1758 /* Find glyph containing X. */
1759 glyph = row->glyphs[*area];
1760 end = glyph + row->used[*area];
1761 x -= x0;
1762 while (glyph < end && x >= glyph->pixel_width)
1764 x -= glyph->pixel_width;
1765 ++glyph;
1768 if (glyph == end)
1769 return NULL;
1771 if (dx)
1773 *dx = x;
1774 *dy = y - (row->y + row->ascent - glyph->ascent);
1777 *hpos = glyph - row->glyphs[*area];
1778 return glyph;
1782 /* EXPORT:
1783 Convert frame-relative x/y to coordinates relative to window W.
1784 Takes pseudo-windows into account. */
1786 void
1787 frame_to_window_pixel_xy (w, x, y)
1788 struct window *w;
1789 int *x, *y;
1791 if (w->pseudo_window_p)
1793 /* A pseudo-window is always full-width, and starts at the
1794 left edge of the frame, plus a frame border. */
1795 struct frame *f = XFRAME (w->frame);
1796 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1797 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1799 else
1801 *x -= WINDOW_LEFT_EDGE_X (w);
1802 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1806 /* EXPORT:
1807 Return in RECTS[] at most N clipping rectangles for glyph string S.
1808 Return the number of stored rectangles. */
1811 get_glyph_string_clip_rects (s, rects, n)
1812 struct glyph_string *s;
1813 NativeRectangle *rects;
1814 int n;
1816 XRectangle r;
1818 if (n <= 0)
1819 return 0;
1821 if (s->row->full_width_p)
1823 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1824 r.x = WINDOW_LEFT_EDGE_X (s->w);
1825 r.width = WINDOW_TOTAL_WIDTH (s->w);
1827 /* Unless displaying a mode or menu bar line, which are always
1828 fully visible, clip to the visible part of the row. */
1829 if (s->w->pseudo_window_p)
1830 r.height = s->row->visible_height;
1831 else
1832 r.height = s->height;
1834 else
1836 /* This is a text line that may be partially visible. */
1837 r.x = window_box_left (s->w, s->area);
1838 r.width = window_box_width (s->w, s->area);
1839 r.height = s->row->visible_height;
1842 if (s->clip_head)
1843 if (r.x < s->clip_head->x)
1845 if (r.width >= s->clip_head->x - r.x)
1846 r.width -= s->clip_head->x - r.x;
1847 else
1848 r.width = 0;
1849 r.x = s->clip_head->x;
1851 if (s->clip_tail)
1852 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1854 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1855 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1856 else
1857 r.width = 0;
1860 /* If S draws overlapping rows, it's sufficient to use the top and
1861 bottom of the window for clipping because this glyph string
1862 intentionally draws over other lines. */
1863 if (s->for_overlaps)
1865 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1866 r.height = window_text_bottom_y (s->w) - r.y;
1868 /* Alas, the above simple strategy does not work for the
1869 environments with anti-aliased text: if the same text is
1870 drawn onto the same place multiple times, it gets thicker.
1871 If the overlap we are processing is for the erased cursor, we
1872 take the intersection with the rectagle of the cursor. */
1873 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1875 XRectangle rc, r_save = r;
1877 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1878 rc.y = s->w->phys_cursor.y;
1879 rc.width = s->w->phys_cursor_width;
1880 rc.height = s->w->phys_cursor_height;
1882 x_intersect_rectangles (&r_save, &rc, &r);
1885 else
1887 /* Don't use S->y for clipping because it doesn't take partially
1888 visible lines into account. For example, it can be negative for
1889 partially visible lines at the top of a window. */
1890 if (!s->row->full_width_p
1891 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1892 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1893 else
1894 r.y = max (0, s->row->y);
1896 /* If drawing a tool-bar window, draw it over the internal border
1897 at the top of the window. */
1898 if (WINDOWP (s->f->tool_bar_window)
1899 && s->w == XWINDOW (s->f->tool_bar_window))
1900 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1903 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1905 /* If drawing the cursor, don't let glyph draw outside its
1906 advertised boundaries. Cleartype does this under some circumstances. */
1907 if (s->hl == DRAW_CURSOR)
1909 struct glyph *glyph = s->first_glyph;
1910 int height, max_y;
1912 if (s->x > r.x)
1914 r.width -= s->x - r.x;
1915 r.x = s->x;
1917 r.width = min (r.width, glyph->pixel_width);
1919 /* If r.y is below window bottom, ensure that we still see a cursor. */
1920 height = min (glyph->ascent + glyph->descent,
1921 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1922 max_y = window_text_bottom_y (s->w) - height;
1923 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1924 if (s->ybase - glyph->ascent > max_y)
1926 r.y = max_y;
1927 r.height = height;
1929 else
1931 /* Don't draw cursor glyph taller than our actual glyph. */
1932 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1933 if (height < r.height)
1935 max_y = r.y + r.height;
1936 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1937 r.height = min (max_y - r.y, height);
1942 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1943 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1945 #ifdef CONVERT_FROM_XRECT
1946 CONVERT_FROM_XRECT (r, *rects);
1947 #else
1948 *rects = r;
1949 #endif
1950 return 1;
1952 else
1954 /* If we are processing overlapping and allowed to return
1955 multiple clipping rectangles, we exclude the row of the glyph
1956 string from the clipping rectangle. This is to avoid drawing
1957 the same text on the environment with anti-aliasing. */
1958 #ifdef CONVERT_FROM_XRECT
1959 XRectangle rs[2];
1960 #else
1961 XRectangle *rs = rects;
1962 #endif
1963 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1965 if (s->for_overlaps & OVERLAPS_PRED)
1967 rs[i] = r;
1968 if (r.y + r.height > row_y)
1970 if (r.y < row_y)
1971 rs[i].height = row_y - r.y;
1972 else
1973 rs[i].height = 0;
1975 i++;
1977 if (s->for_overlaps & OVERLAPS_SUCC)
1979 rs[i] = r;
1980 if (r.y < row_y + s->row->visible_height)
1982 if (r.y + r.height > row_y + s->row->visible_height)
1984 rs[i].y = row_y + s->row->visible_height;
1985 rs[i].height = r.y + r.height - rs[i].y;
1987 else
1988 rs[i].height = 0;
1990 i++;
1993 n = i;
1994 #ifdef CONVERT_FROM_XRECT
1995 for (i = 0; i < n; i++)
1996 CONVERT_FROM_XRECT (rs[i], rects[i]);
1997 #endif
1998 return n;
2002 /* EXPORT:
2003 Return in *NR the clipping rectangle for glyph string S. */
2005 void
2006 get_glyph_string_clip_rect (s, nr)
2007 struct glyph_string *s;
2008 NativeRectangle *nr;
2010 get_glyph_string_clip_rects (s, nr, 1);
2014 /* EXPORT:
2015 Return the position and height of the phys cursor in window W.
2016 Set w->phys_cursor_width to width of phys cursor.
2019 void
2020 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2021 struct window *w;
2022 struct glyph_row *row;
2023 struct glyph *glyph;
2024 int *xp, *yp, *heightp;
2026 struct frame *f = XFRAME (WINDOW_FRAME (w));
2027 int x, y, wd, h, h0, y0;
2029 /* Compute the width of the rectangle to draw. If on a stretch
2030 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2031 rectangle as wide as the glyph, but use a canonical character
2032 width instead. */
2033 wd = glyph->pixel_width - 1;
2034 #ifdef HAVE_NTGUI
2035 wd++; /* Why? */
2036 #endif
2038 x = w->phys_cursor.x;
2039 if (x < 0)
2041 wd += x;
2042 x = 0;
2045 if (glyph->type == STRETCH_GLYPH
2046 && !x_stretch_cursor_p)
2047 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2048 w->phys_cursor_width = wd;
2050 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2052 /* If y is below window bottom, ensure that we still see a cursor. */
2053 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2055 h = max (h0, glyph->ascent + glyph->descent);
2056 h0 = min (h0, glyph->ascent + glyph->descent);
2058 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2059 if (y < y0)
2061 h = max (h - (y0 - y) + 1, h0);
2062 y = y0 - 1;
2064 else
2066 y0 = window_text_bottom_y (w) - h0;
2067 if (y > y0)
2069 h += y - y0;
2070 y = y0;
2074 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2075 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2076 *heightp = h;
2080 * Remember which glyph the mouse is over.
2083 void
2084 remember_mouse_glyph (f, gx, gy, rect)
2085 struct frame *f;
2086 int gx, gy;
2087 NativeRectangle *rect;
2089 Lisp_Object window;
2090 struct window *w;
2091 struct glyph_row *r, *gr, *end_row;
2092 enum window_part part;
2093 enum glyph_row_area area;
2094 int x, y, width, height;
2096 /* Try to determine frame pixel position and size of the glyph under
2097 frame pixel coordinates X/Y on frame F. */
2099 if (!f->glyphs_initialized_p
2100 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2101 NILP (window)))
2103 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2104 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2105 goto virtual_glyph;
2108 w = XWINDOW (window);
2109 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2110 height = WINDOW_FRAME_LINE_HEIGHT (w);
2112 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2113 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2115 if (w->pseudo_window_p)
2117 area = TEXT_AREA;
2118 part = ON_MODE_LINE; /* Don't adjust margin. */
2119 goto text_glyph;
2122 switch (part)
2124 case ON_LEFT_MARGIN:
2125 area = LEFT_MARGIN_AREA;
2126 goto text_glyph;
2128 case ON_RIGHT_MARGIN:
2129 area = RIGHT_MARGIN_AREA;
2130 goto text_glyph;
2132 case ON_HEADER_LINE:
2133 case ON_MODE_LINE:
2134 gr = (part == ON_HEADER_LINE
2135 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2136 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2137 gy = gr->y;
2138 area = TEXT_AREA;
2139 goto text_glyph_row_found;
2141 case ON_TEXT:
2142 area = TEXT_AREA;
2144 text_glyph:
2145 gr = 0; gy = 0;
2146 for (; r <= end_row && r->enabled_p; ++r)
2147 if (r->y + r->height > y)
2149 gr = r; gy = r->y;
2150 break;
2153 text_glyph_row_found:
2154 if (gr && gy <= y)
2156 struct glyph *g = gr->glyphs[area];
2157 struct glyph *end = g + gr->used[area];
2159 height = gr->height;
2160 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2161 if (gx + g->pixel_width > x)
2162 break;
2164 if (g < end)
2166 if (g->type == IMAGE_GLYPH)
2168 /* Don't remember when mouse is over image, as
2169 image may have hot-spots. */
2170 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2171 return;
2173 width = g->pixel_width;
2175 else
2177 /* Use nominal char spacing at end of line. */
2178 x -= gx;
2179 gx += (x / width) * width;
2182 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2183 gx += window_box_left_offset (w, area);
2185 else
2187 /* Use nominal line height at end of window. */
2188 gx = (x / width) * width;
2189 y -= gy;
2190 gy += (y / height) * height;
2192 break;
2194 case ON_LEFT_FRINGE:
2195 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2196 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2197 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2198 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2199 goto row_glyph;
2201 case ON_RIGHT_FRINGE:
2202 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2203 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2204 : window_box_right_offset (w, TEXT_AREA));
2205 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2206 goto row_glyph;
2208 case ON_SCROLL_BAR:
2209 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2211 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2212 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2213 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2214 : 0)));
2215 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2217 row_glyph:
2218 gr = 0, gy = 0;
2219 for (; r <= end_row && r->enabled_p; ++r)
2220 if (r->y + r->height > y)
2222 gr = r; gy = r->y;
2223 break;
2226 if (gr && gy <= y)
2227 height = gr->height;
2228 else
2230 /* Use nominal line height at end of window. */
2231 y -= gy;
2232 gy += (y / height) * height;
2234 break;
2236 default:
2238 virtual_glyph:
2239 /* If there is no glyph under the mouse, then we divide the screen
2240 into a grid of the smallest glyph in the frame, and use that
2241 as our "glyph". */
2243 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2244 round down even for negative values. */
2245 if (gx < 0)
2246 gx -= width - 1;
2247 if (gy < 0)
2248 gy -= height - 1;
2250 gx = (gx / width) * width;
2251 gy = (gy / height) * height;
2253 goto store_rect;
2256 gx += WINDOW_LEFT_EDGE_X (w);
2257 gy += WINDOW_TOP_EDGE_Y (w);
2259 store_rect:
2260 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2262 /* Visible feedback for debugging. */
2263 #if 0
2264 #if HAVE_X_WINDOWS
2265 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2266 f->output_data.x->normal_gc,
2267 gx, gy, width, height);
2268 #endif
2269 #endif
2273 #endif /* HAVE_WINDOW_SYSTEM */
2276 /***********************************************************************
2277 Lisp form evaluation
2278 ***********************************************************************/
2280 /* Error handler for safe_eval and safe_call. */
2282 static Lisp_Object
2283 safe_eval_handler (arg)
2284 Lisp_Object arg;
2286 add_to_log ("Error during redisplay: %s", arg, Qnil);
2287 return Qnil;
2291 /* Evaluate SEXPR and return the result, or nil if something went
2292 wrong. Prevent redisplay during the evaluation. */
2294 Lisp_Object
2295 safe_eval (sexpr)
2296 Lisp_Object sexpr;
2298 Lisp_Object val;
2300 if (inhibit_eval_during_redisplay)
2301 val = Qnil;
2302 else
2304 int count = SPECPDL_INDEX ();
2305 struct gcpro gcpro1;
2307 GCPRO1 (sexpr);
2308 specbind (Qinhibit_redisplay, Qt);
2309 /* Use Qt to ensure debugger does not run,
2310 so there is no possibility of wanting to redisplay. */
2311 val = internal_condition_case_1 (Feval, sexpr, Qt,
2312 safe_eval_handler);
2313 UNGCPRO;
2314 val = unbind_to (count, val);
2317 return val;
2321 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2322 Return the result, or nil if something went wrong. Prevent
2323 redisplay during the evaluation. */
2325 Lisp_Object
2326 safe_call (nargs, args)
2327 int nargs;
2328 Lisp_Object *args;
2330 Lisp_Object val;
2332 if (inhibit_eval_during_redisplay)
2333 val = Qnil;
2334 else
2336 int count = SPECPDL_INDEX ();
2337 struct gcpro gcpro1;
2339 GCPRO1 (args[0]);
2340 gcpro1.nvars = nargs;
2341 specbind (Qinhibit_redisplay, Qt);
2342 /* Use Qt to ensure debugger does not run,
2343 so there is no possibility of wanting to redisplay. */
2344 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2345 safe_eval_handler);
2346 UNGCPRO;
2347 val = unbind_to (count, val);
2350 return val;
2354 /* Call function FN with one argument ARG.
2355 Return the result, or nil if something went wrong. */
2357 Lisp_Object
2358 safe_call1 (fn, arg)
2359 Lisp_Object fn, arg;
2361 Lisp_Object args[2];
2362 args[0] = fn;
2363 args[1] = arg;
2364 return safe_call (2, args);
2369 /***********************************************************************
2370 Debugging
2371 ***********************************************************************/
2373 #if 0
2375 /* Define CHECK_IT to perform sanity checks on iterators.
2376 This is for debugging. It is too slow to do unconditionally. */
2378 static void
2379 check_it (it)
2380 struct it *it;
2382 if (it->method == GET_FROM_STRING)
2384 xassert (STRINGP (it->string));
2385 xassert (IT_STRING_CHARPOS (*it) >= 0);
2387 else
2389 xassert (IT_STRING_CHARPOS (*it) < 0);
2390 if (it->method == GET_FROM_BUFFER)
2392 /* Check that character and byte positions agree. */
2393 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2397 if (it->dpvec)
2398 xassert (it->current.dpvec_index >= 0);
2399 else
2400 xassert (it->current.dpvec_index < 0);
2403 #define CHECK_IT(IT) check_it ((IT))
2405 #else /* not 0 */
2407 #define CHECK_IT(IT) (void) 0
2409 #endif /* not 0 */
2412 #if GLYPH_DEBUG
2414 /* Check that the window end of window W is what we expect it
2415 to be---the last row in the current matrix displaying text. */
2417 static void
2418 check_window_end (w)
2419 struct window *w;
2421 if (!MINI_WINDOW_P (w)
2422 && !NILP (w->window_end_valid))
2424 struct glyph_row *row;
2425 xassert ((row = MATRIX_ROW (w->current_matrix,
2426 XFASTINT (w->window_end_vpos)),
2427 !row->enabled_p
2428 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2429 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2433 #define CHECK_WINDOW_END(W) check_window_end ((W))
2435 #else /* not GLYPH_DEBUG */
2437 #define CHECK_WINDOW_END(W) (void) 0
2439 #endif /* not GLYPH_DEBUG */
2443 /***********************************************************************
2444 Iterator initialization
2445 ***********************************************************************/
2447 /* Initialize IT for displaying current_buffer in window W, starting
2448 at character position CHARPOS. CHARPOS < 0 means that no buffer
2449 position is specified which is useful when the iterator is assigned
2450 a position later. BYTEPOS is the byte position corresponding to
2451 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2453 If ROW is not null, calls to produce_glyphs with IT as parameter
2454 will produce glyphs in that row.
2456 BASE_FACE_ID is the id of a base face to use. It must be one of
2457 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2458 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2459 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2461 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2462 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2463 will be initialized to use the corresponding mode line glyph row of
2464 the desired matrix of W. */
2466 void
2467 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2468 struct it *it;
2469 struct window *w;
2470 int charpos, bytepos;
2471 struct glyph_row *row;
2472 enum face_id base_face_id;
2474 int highlight_region_p;
2476 /* Some precondition checks. */
2477 xassert (w != NULL && it != NULL);
2478 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2479 && charpos <= ZV));
2481 /* If face attributes have been changed since the last redisplay,
2482 free realized faces now because they depend on face definitions
2483 that might have changed. Don't free faces while there might be
2484 desired matrices pending which reference these faces. */
2485 if (face_change_count && !inhibit_free_realized_faces)
2487 face_change_count = 0;
2488 free_all_realized_faces (Qnil);
2491 /* Use one of the mode line rows of W's desired matrix if
2492 appropriate. */
2493 if (row == NULL)
2495 if (base_face_id == MODE_LINE_FACE_ID
2496 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2497 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2498 else if (base_face_id == HEADER_LINE_FACE_ID)
2499 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2502 /* Clear IT. */
2503 bzero (it, sizeof *it);
2504 it->current.overlay_string_index = -1;
2505 it->current.dpvec_index = -1;
2506 it->base_face_id = base_face_id;
2507 it->string = Qnil;
2508 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2510 /* The window in which we iterate over current_buffer: */
2511 XSETWINDOW (it->window, w);
2512 it->w = w;
2513 it->f = XFRAME (w->frame);
2515 /* Extra space between lines (on window systems only). */
2516 if (base_face_id == DEFAULT_FACE_ID
2517 && FRAME_WINDOW_P (it->f))
2519 if (NATNUMP (current_buffer->extra_line_spacing))
2520 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2521 else if (FLOATP (current_buffer->extra_line_spacing))
2522 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2523 * FRAME_LINE_HEIGHT (it->f));
2524 else if (it->f->extra_line_spacing > 0)
2525 it->extra_line_spacing = it->f->extra_line_spacing;
2526 it->max_extra_line_spacing = 0;
2529 /* If realized faces have been removed, e.g. because of face
2530 attribute changes of named faces, recompute them. When running
2531 in batch mode, the face cache of Vterminal_frame is null. If
2532 we happen to get called, make a dummy face cache. */
2533 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2534 init_frame_faces (it->f);
2535 if (FRAME_FACE_CACHE (it->f)->used == 0)
2536 recompute_basic_faces (it->f);
2538 /* Current value of the `slice', `space-width', and 'height' properties. */
2539 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2540 it->space_width = Qnil;
2541 it->font_height = Qnil;
2542 it->override_ascent = -1;
2544 /* Are control characters displayed as `^C'? */
2545 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2547 /* -1 means everything between a CR and the following line end
2548 is invisible. >0 means lines indented more than this value are
2549 invisible. */
2550 it->selective = (INTEGERP (current_buffer->selective_display)
2551 ? XFASTINT (current_buffer->selective_display)
2552 : (!NILP (current_buffer->selective_display)
2553 ? -1 : 0));
2554 it->selective_display_ellipsis_p
2555 = !NILP (current_buffer->selective_display_ellipses);
2557 /* Display table to use. */
2558 it->dp = window_display_table (w);
2560 /* Are multibyte characters enabled in current_buffer? */
2561 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2563 /* Non-zero if we should highlight the region. */
2564 highlight_region_p
2565 = (!NILP (Vtransient_mark_mode)
2566 && !NILP (current_buffer->mark_active)
2567 && XMARKER (current_buffer->mark)->buffer != 0);
2569 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2570 start and end of a visible region in window IT->w. Set both to
2571 -1 to indicate no region. */
2572 if (highlight_region_p
2573 /* Maybe highlight only in selected window. */
2574 && (/* Either show region everywhere. */
2575 highlight_nonselected_windows
2576 /* Or show region in the selected window. */
2577 || w == XWINDOW (selected_window)
2578 /* Or show the region if we are in the mini-buffer and W is
2579 the window the mini-buffer refers to. */
2580 || (MINI_WINDOW_P (XWINDOW (selected_window))
2581 && WINDOWP (minibuf_selected_window)
2582 && w == XWINDOW (minibuf_selected_window))))
2584 int charpos = marker_position (current_buffer->mark);
2585 it->region_beg_charpos = min (PT, charpos);
2586 it->region_end_charpos = max (PT, charpos);
2588 else
2589 it->region_beg_charpos = it->region_end_charpos = -1;
2591 /* Get the position at which the redisplay_end_trigger hook should
2592 be run, if it is to be run at all. */
2593 if (MARKERP (w->redisplay_end_trigger)
2594 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2595 it->redisplay_end_trigger_charpos
2596 = marker_position (w->redisplay_end_trigger);
2597 else if (INTEGERP (w->redisplay_end_trigger))
2598 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2600 /* Correct bogus values of tab_width. */
2601 it->tab_width = XINT (current_buffer->tab_width);
2602 if (it->tab_width <= 0 || it->tab_width > 1000)
2603 it->tab_width = 8;
2605 /* Are lines in the display truncated? */
2606 it->truncate_lines_p
2607 = (base_face_id != DEFAULT_FACE_ID
2608 || XINT (it->w->hscroll)
2609 || (truncate_partial_width_windows
2610 && !WINDOW_FULL_WIDTH_P (it->w))
2611 || !NILP (current_buffer->truncate_lines));
2613 /* Get dimensions of truncation and continuation glyphs. These are
2614 displayed as fringe bitmaps under X, so we don't need them for such
2615 frames. */
2616 if (!FRAME_WINDOW_P (it->f))
2618 if (it->truncate_lines_p)
2620 /* We will need the truncation glyph. */
2621 xassert (it->glyph_row == NULL);
2622 produce_special_glyphs (it, IT_TRUNCATION);
2623 it->truncation_pixel_width = it->pixel_width;
2625 else
2627 /* We will need the continuation glyph. */
2628 xassert (it->glyph_row == NULL);
2629 produce_special_glyphs (it, IT_CONTINUATION);
2630 it->continuation_pixel_width = it->pixel_width;
2633 /* Reset these values to zero because the produce_special_glyphs
2634 above has changed them. */
2635 it->pixel_width = it->ascent = it->descent = 0;
2636 it->phys_ascent = it->phys_descent = 0;
2639 /* Set this after getting the dimensions of truncation and
2640 continuation glyphs, so that we don't produce glyphs when calling
2641 produce_special_glyphs, above. */
2642 it->glyph_row = row;
2643 it->area = TEXT_AREA;
2645 /* Get the dimensions of the display area. The display area
2646 consists of the visible window area plus a horizontally scrolled
2647 part to the left of the window. All x-values are relative to the
2648 start of this total display area. */
2649 if (base_face_id != DEFAULT_FACE_ID)
2651 /* Mode lines, menu bar in terminal frames. */
2652 it->first_visible_x = 0;
2653 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2655 else
2657 it->first_visible_x
2658 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2659 it->last_visible_x = (it->first_visible_x
2660 + window_box_width (w, TEXT_AREA));
2662 /* If we truncate lines, leave room for the truncator glyph(s) at
2663 the right margin. Otherwise, leave room for the continuation
2664 glyph(s). Truncation and continuation glyphs are not inserted
2665 for window-based redisplay. */
2666 if (!FRAME_WINDOW_P (it->f))
2668 if (it->truncate_lines_p)
2669 it->last_visible_x -= it->truncation_pixel_width;
2670 else
2671 it->last_visible_x -= it->continuation_pixel_width;
2674 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2675 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2678 /* Leave room for a border glyph. */
2679 if (!FRAME_WINDOW_P (it->f)
2680 && !WINDOW_RIGHTMOST_P (it->w))
2681 it->last_visible_x -= 1;
2683 it->last_visible_y = window_text_bottom_y (w);
2685 /* For mode lines and alike, arrange for the first glyph having a
2686 left box line if the face specifies a box. */
2687 if (base_face_id != DEFAULT_FACE_ID)
2689 struct face *face;
2691 it->face_id = base_face_id;
2693 /* If we have a boxed mode line, make the first character appear
2694 with a left box line. */
2695 face = FACE_FROM_ID (it->f, base_face_id);
2696 if (face->box != FACE_NO_BOX)
2697 it->start_of_box_run_p = 1;
2700 /* If a buffer position was specified, set the iterator there,
2701 getting overlays and face properties from that position. */
2702 if (charpos >= BUF_BEG (current_buffer))
2704 it->end_charpos = ZV;
2705 it->face_id = -1;
2706 IT_CHARPOS (*it) = charpos;
2708 /* Compute byte position if not specified. */
2709 if (bytepos < charpos)
2710 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2711 else
2712 IT_BYTEPOS (*it) = bytepos;
2714 it->start = it->current;
2716 /* Compute faces etc. */
2717 reseat (it, it->current.pos, 1);
2720 CHECK_IT (it);
2724 /* Initialize IT for the display of window W with window start POS. */
2726 void
2727 start_display (it, w, pos)
2728 struct it *it;
2729 struct window *w;
2730 struct text_pos pos;
2732 struct glyph_row *row;
2733 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2735 row = w->desired_matrix->rows + first_vpos;
2736 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2737 it->first_vpos = first_vpos;
2739 /* Don't reseat to previous visible line start if current start
2740 position is in a string or image. */
2741 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2743 int start_at_line_beg_p;
2744 int first_y = it->current_y;
2746 /* If window start is not at a line start, skip forward to POS to
2747 get the correct continuation lines width. */
2748 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2749 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2750 if (!start_at_line_beg_p)
2752 int new_x;
2754 reseat_at_previous_visible_line_start (it);
2755 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2757 new_x = it->current_x + it->pixel_width;
2759 /* If lines are continued, this line may end in the middle
2760 of a multi-glyph character (e.g. a control character
2761 displayed as \003, or in the middle of an overlay
2762 string). In this case move_it_to above will not have
2763 taken us to the start of the continuation line but to the
2764 end of the continued line. */
2765 if (it->current_x > 0
2766 && !it->truncate_lines_p /* Lines are continued. */
2767 && (/* And glyph doesn't fit on the line. */
2768 new_x > it->last_visible_x
2769 /* Or it fits exactly and we're on a window
2770 system frame. */
2771 || (new_x == it->last_visible_x
2772 && FRAME_WINDOW_P (it->f))))
2774 if (it->current.dpvec_index >= 0
2775 || it->current.overlay_string_index >= 0)
2777 set_iterator_to_next (it, 1);
2778 move_it_in_display_line_to (it, -1, -1, 0);
2781 it->continuation_lines_width += it->current_x;
2784 /* We're starting a new display line, not affected by the
2785 height of the continued line, so clear the appropriate
2786 fields in the iterator structure. */
2787 it->max_ascent = it->max_descent = 0;
2788 it->max_phys_ascent = it->max_phys_descent = 0;
2790 it->current_y = first_y;
2791 it->vpos = 0;
2792 it->current_x = it->hpos = 0;
2796 #if 0 /* Don't assert the following because start_display is sometimes
2797 called intentionally with a window start that is not at a
2798 line start. Please leave this code in as a comment. */
2800 /* Window start should be on a line start, now. */
2801 xassert (it->continuation_lines_width
2802 || IT_CHARPOS (it) == BEGV
2803 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2804 #endif /* 0 */
2808 /* Return 1 if POS is a position in ellipses displayed for invisible
2809 text. W is the window we display, for text property lookup. */
2811 static int
2812 in_ellipses_for_invisible_text_p (pos, w)
2813 struct display_pos *pos;
2814 struct window *w;
2816 Lisp_Object prop, window;
2817 int ellipses_p = 0;
2818 int charpos = CHARPOS (pos->pos);
2820 /* If POS specifies a position in a display vector, this might
2821 be for an ellipsis displayed for invisible text. We won't
2822 get the iterator set up for delivering that ellipsis unless
2823 we make sure that it gets aware of the invisible text. */
2824 if (pos->dpvec_index >= 0
2825 && pos->overlay_string_index < 0
2826 && CHARPOS (pos->string_pos) < 0
2827 && charpos > BEGV
2828 && (XSETWINDOW (window, w),
2829 prop = Fget_char_property (make_number (charpos),
2830 Qinvisible, window),
2831 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2833 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2834 window);
2835 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2838 return ellipses_p;
2842 /* Initialize IT for stepping through current_buffer in window W,
2843 starting at position POS that includes overlay string and display
2844 vector/ control character translation position information. Value
2845 is zero if there are overlay strings with newlines at POS. */
2847 static int
2848 init_from_display_pos (it, w, pos)
2849 struct it *it;
2850 struct window *w;
2851 struct display_pos *pos;
2853 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2854 int i, overlay_strings_with_newlines = 0;
2856 /* If POS specifies a position in a display vector, this might
2857 be for an ellipsis displayed for invisible text. We won't
2858 get the iterator set up for delivering that ellipsis unless
2859 we make sure that it gets aware of the invisible text. */
2860 if (in_ellipses_for_invisible_text_p (pos, w))
2862 --charpos;
2863 bytepos = 0;
2866 /* Keep in mind: the call to reseat in init_iterator skips invisible
2867 text, so we might end up at a position different from POS. This
2868 is only a problem when POS is a row start after a newline and an
2869 overlay starts there with an after-string, and the overlay has an
2870 invisible property. Since we don't skip invisible text in
2871 display_line and elsewhere immediately after consuming the
2872 newline before the row start, such a POS will not be in a string,
2873 but the call to init_iterator below will move us to the
2874 after-string. */
2875 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2877 /* This only scans the current chunk -- it should scan all chunks.
2878 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2879 to 16 in 22.1 to make this a lesser problem. */
2880 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2882 const char *s = SDATA (it->overlay_strings[i]);
2883 const char *e = s + SBYTES (it->overlay_strings[i]);
2885 while (s < e && *s != '\n')
2886 ++s;
2888 if (s < e)
2890 overlay_strings_with_newlines = 1;
2891 break;
2895 /* If position is within an overlay string, set up IT to the right
2896 overlay string. */
2897 if (pos->overlay_string_index >= 0)
2899 int relative_index;
2901 /* If the first overlay string happens to have a `display'
2902 property for an image, the iterator will be set up for that
2903 image, and we have to undo that setup first before we can
2904 correct the overlay string index. */
2905 if (it->method == GET_FROM_IMAGE)
2906 pop_it (it);
2908 /* We already have the first chunk of overlay strings in
2909 IT->overlay_strings. Load more until the one for
2910 pos->overlay_string_index is in IT->overlay_strings. */
2911 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2913 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2914 it->current.overlay_string_index = 0;
2915 while (n--)
2917 load_overlay_strings (it, 0);
2918 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2922 it->current.overlay_string_index = pos->overlay_string_index;
2923 relative_index = (it->current.overlay_string_index
2924 % OVERLAY_STRING_CHUNK_SIZE);
2925 it->string = it->overlay_strings[relative_index];
2926 xassert (STRINGP (it->string));
2927 it->current.string_pos = pos->string_pos;
2928 it->method = GET_FROM_STRING;
2931 #if 0 /* This is bogus because POS not having an overlay string
2932 position does not mean it's after the string. Example: A
2933 line starting with a before-string and initialization of IT
2934 to the previous row's end position. */
2935 else if (it->current.overlay_string_index >= 0)
2937 /* If POS says we're already after an overlay string ending at
2938 POS, make sure to pop the iterator because it will be in
2939 front of that overlay string. When POS is ZV, we've thereby
2940 also ``processed'' overlay strings at ZV. */
2941 while (it->sp)
2942 pop_it (it);
2943 xassert (it->current.overlay_string_index == -1);
2944 xassert (it->method == GET_FROM_BUFFER);
2945 if (CHARPOS (pos->pos) == ZV)
2946 it->overlay_strings_at_end_processed_p = 1;
2948 #endif /* 0 */
2950 if (CHARPOS (pos->string_pos) >= 0)
2952 /* Recorded position is not in an overlay string, but in another
2953 string. This can only be a string from a `display' property.
2954 IT should already be filled with that string. */
2955 it->current.string_pos = pos->string_pos;
2956 xassert (STRINGP (it->string));
2959 /* Restore position in display vector translations, control
2960 character translations or ellipses. */
2961 if (pos->dpvec_index >= 0)
2963 if (it->dpvec == NULL)
2964 get_next_display_element (it);
2965 xassert (it->dpvec && it->current.dpvec_index == 0);
2966 it->current.dpvec_index = pos->dpvec_index;
2969 CHECK_IT (it);
2970 return !overlay_strings_with_newlines;
2974 /* Initialize IT for stepping through current_buffer in window W
2975 starting at ROW->start. */
2977 static void
2978 init_to_row_start (it, w, row)
2979 struct it *it;
2980 struct window *w;
2981 struct glyph_row *row;
2983 init_from_display_pos (it, w, &row->start);
2984 it->start = row->start;
2985 it->continuation_lines_width = row->continuation_lines_width;
2986 CHECK_IT (it);
2990 /* Initialize IT for stepping through current_buffer in window W
2991 starting in the line following ROW, i.e. starting at ROW->end.
2992 Value is zero if there are overlay strings with newlines at ROW's
2993 end position. */
2995 static int
2996 init_to_row_end (it, w, row)
2997 struct it *it;
2998 struct window *w;
2999 struct glyph_row *row;
3001 int success = 0;
3003 if (init_from_display_pos (it, w, &row->end))
3005 if (row->continued_p)
3006 it->continuation_lines_width
3007 = row->continuation_lines_width + row->pixel_width;
3008 CHECK_IT (it);
3009 success = 1;
3012 return success;
3018 /***********************************************************************
3019 Text properties
3020 ***********************************************************************/
3022 /* Called when IT reaches IT->stop_charpos. Handle text property and
3023 overlay changes. Set IT->stop_charpos to the next position where
3024 to stop. */
3026 static void
3027 handle_stop (it)
3028 struct it *it;
3030 enum prop_handled handled;
3031 int handle_overlay_change_p;
3032 struct props *p;
3034 it->dpvec = NULL;
3035 it->current.dpvec_index = -1;
3036 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3037 it->ignore_overlay_strings_at_pos_p = 0;
3039 /* Use face of preceding text for ellipsis (if invisible) */
3040 if (it->selective_display_ellipsis_p)
3041 it->saved_face_id = it->face_id;
3045 handled = HANDLED_NORMALLY;
3047 /* Call text property handlers. */
3048 for (p = it_props; p->handler; ++p)
3050 handled = p->handler (it);
3052 if (handled == HANDLED_RECOMPUTE_PROPS)
3053 break;
3054 else if (handled == HANDLED_RETURN)
3056 /* We still want to show before and after strings from
3057 overlays even if the actual buffer text is replaced. */
3058 if (!handle_overlay_change_p || it->sp > 1)
3059 return;
3060 if (!get_overlay_strings_1 (it, 0, 0))
3061 return;
3062 it->ignore_overlay_strings_at_pos_p = 1;
3063 it->string_from_display_prop_p = 0;
3064 handle_overlay_change_p = 0;
3065 handled = HANDLED_RECOMPUTE_PROPS;
3066 break;
3068 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3069 handle_overlay_change_p = 0;
3072 if (handled != HANDLED_RECOMPUTE_PROPS)
3074 /* Don't check for overlay strings below when set to deliver
3075 characters from a display vector. */
3076 if (it->method == GET_FROM_DISPLAY_VECTOR)
3077 handle_overlay_change_p = 0;
3079 /* Handle overlay changes.
3080 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3081 if it finds overlays. */
3082 if (handle_overlay_change_p)
3083 handled = handle_overlay_change (it);
3086 while (handled == HANDLED_RECOMPUTE_PROPS);
3088 /* Determine where to stop next. */
3089 if (handled == HANDLED_NORMALLY)
3090 compute_stop_pos (it);
3094 /* Compute IT->stop_charpos from text property and overlay change
3095 information for IT's current position. */
3097 static void
3098 compute_stop_pos (it)
3099 struct it *it;
3101 register INTERVAL iv, next_iv;
3102 Lisp_Object object, limit, position;
3104 /* If nowhere else, stop at the end. */
3105 it->stop_charpos = it->end_charpos;
3107 if (STRINGP (it->string))
3109 /* Strings are usually short, so don't limit the search for
3110 properties. */
3111 object = it->string;
3112 limit = Qnil;
3113 position = make_number (IT_STRING_CHARPOS (*it));
3115 else
3117 int charpos;
3119 /* If next overlay change is in front of the current stop pos
3120 (which is IT->end_charpos), stop there. Note: value of
3121 next_overlay_change is point-max if no overlay change
3122 follows. */
3123 charpos = next_overlay_change (IT_CHARPOS (*it));
3124 if (charpos < it->stop_charpos)
3125 it->stop_charpos = charpos;
3127 /* If showing the region, we have to stop at the region
3128 start or end because the face might change there. */
3129 if (it->region_beg_charpos > 0)
3131 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3132 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3133 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3134 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3137 /* Set up variables for computing the stop position from text
3138 property changes. */
3139 XSETBUFFER (object, current_buffer);
3140 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3141 position = make_number (IT_CHARPOS (*it));
3145 /* Get the interval containing IT's position. Value is a null
3146 interval if there isn't such an interval. */
3147 iv = validate_interval_range (object, &position, &position, 0);
3148 if (!NULL_INTERVAL_P (iv))
3150 Lisp_Object values_here[LAST_PROP_IDX];
3151 struct props *p;
3153 /* Get properties here. */
3154 for (p = it_props; p->handler; ++p)
3155 values_here[p->idx] = textget (iv->plist, *p->name);
3157 /* Look for an interval following iv that has different
3158 properties. */
3159 for (next_iv = next_interval (iv);
3160 (!NULL_INTERVAL_P (next_iv)
3161 && (NILP (limit)
3162 || XFASTINT (limit) > next_iv->position));
3163 next_iv = next_interval (next_iv))
3165 for (p = it_props; p->handler; ++p)
3167 Lisp_Object new_value;
3169 new_value = textget (next_iv->plist, *p->name);
3170 if (!EQ (values_here[p->idx], new_value))
3171 break;
3174 if (p->handler)
3175 break;
3178 if (!NULL_INTERVAL_P (next_iv))
3180 if (INTEGERP (limit)
3181 && next_iv->position >= XFASTINT (limit))
3182 /* No text property change up to limit. */
3183 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3184 else
3185 /* Text properties change in next_iv. */
3186 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3190 xassert (STRINGP (it->string)
3191 || (it->stop_charpos >= BEGV
3192 && it->stop_charpos >= IT_CHARPOS (*it)));
3196 /* Return the position of the next overlay change after POS in
3197 current_buffer. Value is point-max if no overlay change
3198 follows. This is like `next-overlay-change' but doesn't use
3199 xmalloc. */
3201 static int
3202 next_overlay_change (pos)
3203 int pos;
3205 int noverlays;
3206 int endpos;
3207 Lisp_Object *overlays;
3208 int i;
3210 /* Get all overlays at the given position. */
3211 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3213 /* If any of these overlays ends before endpos,
3214 use its ending point instead. */
3215 for (i = 0; i < noverlays; ++i)
3217 Lisp_Object oend;
3218 int oendpos;
3220 oend = OVERLAY_END (overlays[i]);
3221 oendpos = OVERLAY_POSITION (oend);
3222 endpos = min (endpos, oendpos);
3225 return endpos;
3230 /***********************************************************************
3231 Fontification
3232 ***********************************************************************/
3234 /* Handle changes in the `fontified' property of the current buffer by
3235 calling hook functions from Qfontification_functions to fontify
3236 regions of text. */
3238 static enum prop_handled
3239 handle_fontified_prop (it)
3240 struct it *it;
3242 Lisp_Object prop, pos;
3243 enum prop_handled handled = HANDLED_NORMALLY;
3245 if (!NILP (Vmemory_full))
3246 return handled;
3248 /* Get the value of the `fontified' property at IT's current buffer
3249 position. (The `fontified' property doesn't have a special
3250 meaning in strings.) If the value is nil, call functions from
3251 Qfontification_functions. */
3252 if (!STRINGP (it->string)
3253 && it->s == NULL
3254 && !NILP (Vfontification_functions)
3255 && !NILP (Vrun_hooks)
3256 && (pos = make_number (IT_CHARPOS (*it)),
3257 prop = Fget_char_property (pos, Qfontified, Qnil),
3258 /* Ignore the special cased nil value always present at EOB since
3259 no amount of fontifying will be able to change it. */
3260 NILP (prop) && IT_CHARPOS (*it) < Z))
3262 int count = SPECPDL_INDEX ();
3263 Lisp_Object val;
3265 val = Vfontification_functions;
3266 specbind (Qfontification_functions, Qnil);
3268 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3269 safe_call1 (val, pos);
3270 else
3272 Lisp_Object globals, fn;
3273 struct gcpro gcpro1, gcpro2;
3275 globals = Qnil;
3276 GCPRO2 (val, globals);
3278 for (; CONSP (val); val = XCDR (val))
3280 fn = XCAR (val);
3282 if (EQ (fn, Qt))
3284 /* A value of t indicates this hook has a local
3285 binding; it means to run the global binding too.
3286 In a global value, t should not occur. If it
3287 does, we must ignore it to avoid an endless
3288 loop. */
3289 for (globals = Fdefault_value (Qfontification_functions);
3290 CONSP (globals);
3291 globals = XCDR (globals))
3293 fn = XCAR (globals);
3294 if (!EQ (fn, Qt))
3295 safe_call1 (fn, pos);
3298 else
3299 safe_call1 (fn, pos);
3302 UNGCPRO;
3305 unbind_to (count, Qnil);
3307 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3308 something. This avoids an endless loop if they failed to
3309 fontify the text for which reason ever. */
3310 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3311 handled = HANDLED_RECOMPUTE_PROPS;
3314 return handled;
3319 /***********************************************************************
3320 Faces
3321 ***********************************************************************/
3323 /* Set up iterator IT from face properties at its current position.
3324 Called from handle_stop. */
3326 static enum prop_handled
3327 handle_face_prop (it)
3328 struct it *it;
3330 int new_face_id, next_stop;
3332 if (!STRINGP (it->string))
3334 new_face_id
3335 = face_at_buffer_position (it->w,
3336 IT_CHARPOS (*it),
3337 it->region_beg_charpos,
3338 it->region_end_charpos,
3339 &next_stop,
3340 (IT_CHARPOS (*it)
3341 + TEXT_PROP_DISTANCE_LIMIT),
3344 /* Is this a start of a run of characters with box face?
3345 Caveat: this can be called for a freshly initialized
3346 iterator; face_id is -1 in this case. We know that the new
3347 face will not change until limit, i.e. if the new face has a
3348 box, all characters up to limit will have one. But, as
3349 usual, we don't know whether limit is really the end. */
3350 if (new_face_id != it->face_id)
3352 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3354 /* If new face has a box but old face has not, this is
3355 the start of a run of characters with box, i.e. it has
3356 a shadow on the left side. The value of face_id of the
3357 iterator will be -1 if this is the initial call that gets
3358 the face. In this case, we have to look in front of IT's
3359 position and see whether there is a face != new_face_id. */
3360 it->start_of_box_run_p
3361 = (new_face->box != FACE_NO_BOX
3362 && (it->face_id >= 0
3363 || IT_CHARPOS (*it) == BEG
3364 || new_face_id != face_before_it_pos (it)));
3365 it->face_box_p = new_face->box != FACE_NO_BOX;
3368 else
3370 int base_face_id, bufpos;
3371 int i;
3372 Lisp_Object from_overlay
3373 = (it->current.overlay_string_index >= 0
3374 ? it->string_overlays[it->current.overlay_string_index]
3375 : Qnil);
3377 /* See if we got to this string directly or indirectly from
3378 an overlay property. That includes the before-string or
3379 after-string of an overlay, strings in display properties
3380 provided by an overlay, their text properties, etc.
3382 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3383 if (! NILP (from_overlay))
3384 for (i = it->sp - 1; i >= 0; i--)
3386 if (it->stack[i].current.overlay_string_index >= 0)
3387 from_overlay
3388 = it->string_overlays[it->stack[i].current.overlay_string_index];
3389 else if (! NILP (it->stack[i].from_overlay))
3390 from_overlay = it->stack[i].from_overlay;
3392 if (!NILP (from_overlay))
3393 break;
3396 if (! NILP (from_overlay))
3398 bufpos = IT_CHARPOS (*it);
3399 /* For a string from an overlay, the base face depends
3400 only on text properties and ignores overlays. */
3401 base_face_id
3402 = face_for_overlay_string (it->w,
3403 IT_CHARPOS (*it),
3404 it->region_beg_charpos,
3405 it->region_end_charpos,
3406 &next_stop,
3407 (IT_CHARPOS (*it)
3408 + TEXT_PROP_DISTANCE_LIMIT),
3410 from_overlay);
3412 else
3414 bufpos = 0;
3416 /* For strings from a `display' property, use the face at
3417 IT's current buffer position as the base face to merge
3418 with, so that overlay strings appear in the same face as
3419 surrounding text, unless they specify their own
3420 faces. */
3421 base_face_id = underlying_face_id (it);
3424 new_face_id = face_at_string_position (it->w,
3425 it->string,
3426 IT_STRING_CHARPOS (*it),
3427 bufpos,
3428 it->region_beg_charpos,
3429 it->region_end_charpos,
3430 &next_stop,
3431 base_face_id, 0);
3433 #if 0 /* This shouldn't be neccessary. Let's check it. */
3434 /* If IT is used to display a mode line we would really like to
3435 use the mode line face instead of the frame's default face. */
3436 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3437 && new_face_id == DEFAULT_FACE_ID)
3438 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3439 #endif
3441 /* Is this a start of a run of characters with box? Caveat:
3442 this can be called for a freshly allocated iterator; face_id
3443 is -1 is this case. We know that the new face will not
3444 change until the next check pos, i.e. if the new face has a
3445 box, all characters up to that position will have a
3446 box. But, as usual, we don't know whether that position
3447 is really the end. */
3448 if (new_face_id != it->face_id)
3450 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3451 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3453 /* If new face has a box but old face hasn't, this is the
3454 start of a run of characters with box, i.e. it has a
3455 shadow on the left side. */
3456 it->start_of_box_run_p
3457 = new_face->box && (old_face == NULL || !old_face->box);
3458 it->face_box_p = new_face->box != FACE_NO_BOX;
3462 it->face_id = new_face_id;
3463 return HANDLED_NORMALLY;
3467 /* Return the ID of the face ``underlying'' IT's current position,
3468 which is in a string. If the iterator is associated with a
3469 buffer, return the face at IT's current buffer position.
3470 Otherwise, use the iterator's base_face_id. */
3472 static int
3473 underlying_face_id (it)
3474 struct it *it;
3476 int face_id = it->base_face_id, i;
3478 xassert (STRINGP (it->string));
3480 for (i = it->sp - 1; i >= 0; --i)
3481 if (NILP (it->stack[i].string))
3482 face_id = it->stack[i].face_id;
3484 return face_id;
3488 /* Compute the face one character before or after the current position
3489 of IT. BEFORE_P non-zero means get the face in front of IT's
3490 position. Value is the id of the face. */
3492 static int
3493 face_before_or_after_it_pos (it, before_p)
3494 struct it *it;
3495 int before_p;
3497 int face_id, limit;
3498 int next_check_charpos;
3499 struct text_pos pos;
3501 xassert (it->s == NULL);
3503 if (STRINGP (it->string))
3505 int bufpos, base_face_id;
3507 /* No face change past the end of the string (for the case
3508 we are padding with spaces). No face change before the
3509 string start. */
3510 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3511 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3512 return it->face_id;
3514 /* Set pos to the position before or after IT's current position. */
3515 if (before_p)
3516 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3517 else
3518 /* For composition, we must check the character after the
3519 composition. */
3520 pos = (it->what == IT_COMPOSITION
3521 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3522 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3524 if (it->current.overlay_string_index >= 0)
3525 bufpos = IT_CHARPOS (*it);
3526 else
3527 bufpos = 0;
3529 base_face_id = underlying_face_id (it);
3531 /* Get the face for ASCII, or unibyte. */
3532 face_id = face_at_string_position (it->w,
3533 it->string,
3534 CHARPOS (pos),
3535 bufpos,
3536 it->region_beg_charpos,
3537 it->region_end_charpos,
3538 &next_check_charpos,
3539 base_face_id, 0);
3541 /* Correct the face for charsets different from ASCII. Do it
3542 for the multibyte case only. The face returned above is
3543 suitable for unibyte text if IT->string is unibyte. */
3544 if (STRING_MULTIBYTE (it->string))
3546 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3547 int rest = SBYTES (it->string) - BYTEPOS (pos);
3548 int c, len;
3549 struct face *face = FACE_FROM_ID (it->f, face_id);
3551 c = string_char_and_length (p, rest, &len);
3552 face_id = FACE_FOR_CHAR (it->f, face, c);
3555 else
3557 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3558 || (IT_CHARPOS (*it) <= BEGV && before_p))
3559 return it->face_id;
3561 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3562 pos = it->current.pos;
3564 if (before_p)
3565 DEC_TEXT_POS (pos, it->multibyte_p);
3566 else
3568 if (it->what == IT_COMPOSITION)
3569 /* For composition, we must check the position after the
3570 composition. */
3571 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3572 else
3573 INC_TEXT_POS (pos, it->multibyte_p);
3576 /* Determine face for CHARSET_ASCII, or unibyte. */
3577 face_id = face_at_buffer_position (it->w,
3578 CHARPOS (pos),
3579 it->region_beg_charpos,
3580 it->region_end_charpos,
3581 &next_check_charpos,
3582 limit, 0);
3584 /* Correct the face for charsets different from ASCII. Do it
3585 for the multibyte case only. The face returned above is
3586 suitable for unibyte text if current_buffer is unibyte. */
3587 if (it->multibyte_p)
3589 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3590 struct face *face = FACE_FROM_ID (it->f, face_id);
3591 face_id = FACE_FOR_CHAR (it->f, face, c);
3595 return face_id;
3600 /***********************************************************************
3601 Invisible text
3602 ***********************************************************************/
3604 /* Set up iterator IT from invisible properties at its current
3605 position. Called from handle_stop. */
3607 static enum prop_handled
3608 handle_invisible_prop (it)
3609 struct it *it;
3611 enum prop_handled handled = HANDLED_NORMALLY;
3613 if (STRINGP (it->string))
3615 extern Lisp_Object Qinvisible;
3616 Lisp_Object prop, end_charpos, limit, charpos;
3618 /* Get the value of the invisible text property at the
3619 current position. Value will be nil if there is no such
3620 property. */
3621 charpos = make_number (IT_STRING_CHARPOS (*it));
3622 prop = Fget_text_property (charpos, Qinvisible, it->string);
3624 if (!NILP (prop)
3625 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3627 handled = HANDLED_RECOMPUTE_PROPS;
3629 /* Get the position at which the next change of the
3630 invisible text property can be found in IT->string.
3631 Value will be nil if the property value is the same for
3632 all the rest of IT->string. */
3633 XSETINT (limit, SCHARS (it->string));
3634 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3635 it->string, limit);
3637 /* Text at current position is invisible. The next
3638 change in the property is at position end_charpos.
3639 Move IT's current position to that position. */
3640 if (INTEGERP (end_charpos)
3641 && XFASTINT (end_charpos) < XFASTINT (limit))
3643 struct text_pos old;
3644 old = it->current.string_pos;
3645 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3646 compute_string_pos (&it->current.string_pos, old, it->string);
3648 else
3650 /* The rest of the string is invisible. If this is an
3651 overlay string, proceed with the next overlay string
3652 or whatever comes and return a character from there. */
3653 if (it->current.overlay_string_index >= 0)
3655 next_overlay_string (it);
3656 /* Don't check for overlay strings when we just
3657 finished processing them. */
3658 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3660 else
3662 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3663 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3668 else
3670 int invis_p, newpos, next_stop, start_charpos;
3671 Lisp_Object pos, prop, overlay;
3673 /* First of all, is there invisible text at this position? */
3674 start_charpos = IT_CHARPOS (*it);
3675 pos = make_number (IT_CHARPOS (*it));
3676 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3677 &overlay);
3678 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3680 /* If we are on invisible text, skip over it. */
3681 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3683 /* Record whether we have to display an ellipsis for the
3684 invisible text. */
3685 int display_ellipsis_p = invis_p == 2;
3687 handled = HANDLED_RECOMPUTE_PROPS;
3689 /* Loop skipping over invisible text. The loop is left at
3690 ZV or with IT on the first char being visible again. */
3693 /* Try to skip some invisible text. Return value is the
3694 position reached which can be equal to IT's position
3695 if there is nothing invisible here. This skips both
3696 over invisible text properties and overlays with
3697 invisible property. */
3698 newpos = skip_invisible (IT_CHARPOS (*it),
3699 &next_stop, ZV, it->window);
3701 /* If we skipped nothing at all we weren't at invisible
3702 text in the first place. If everything to the end of
3703 the buffer was skipped, end the loop. */
3704 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3705 invis_p = 0;
3706 else
3708 /* We skipped some characters but not necessarily
3709 all there are. Check if we ended up on visible
3710 text. Fget_char_property returns the property of
3711 the char before the given position, i.e. if we
3712 get invis_p = 0, this means that the char at
3713 newpos is visible. */
3714 pos = make_number (newpos);
3715 prop = Fget_char_property (pos, Qinvisible, it->window);
3716 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3719 /* If we ended up on invisible text, proceed to
3720 skip starting with next_stop. */
3721 if (invis_p)
3722 IT_CHARPOS (*it) = next_stop;
3724 /* If there are adjacent invisible texts, don't lose the
3725 second one's ellipsis. */
3726 if (invis_p == 2)
3727 display_ellipsis_p = 1;
3729 while (invis_p);
3731 /* The position newpos is now either ZV or on visible text. */
3732 IT_CHARPOS (*it) = newpos;
3733 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3735 /* If there are before-strings at the start of invisible
3736 text, and the text is invisible because of a text
3737 property, arrange to show before-strings because 20.x did
3738 it that way. (If the text is invisible because of an
3739 overlay property instead of a text property, this is
3740 already handled in the overlay code.) */
3741 if (NILP (overlay)
3742 && get_overlay_strings (it, start_charpos))
3744 handled = HANDLED_RECOMPUTE_PROPS;
3745 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3747 else if (display_ellipsis_p)
3749 /* Make sure that the glyphs of the ellipsis will get
3750 correct `charpos' values. If we would not update
3751 it->position here, the glyphs would belong to the
3752 last visible character _before_ the invisible
3753 text, which confuses `set_cursor_from_row'.
3755 We use the last invisible position instead of the
3756 first because this way the cursor is always drawn on
3757 the first "." of the ellipsis, whenever PT is inside
3758 the invisible text. Otherwise the cursor would be
3759 placed _after_ the ellipsis when the point is after the
3760 first invisible character. */
3761 if (!STRINGP (it->object))
3763 it->position.charpos = IT_CHARPOS (*it) - 1;
3764 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3766 setup_for_ellipsis (it, 0);
3767 /* Let the ellipsis display before
3768 considering any properties of the following char.
3769 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3770 handled = HANDLED_RETURN;
3775 return handled;
3779 /* Make iterator IT return `...' next.
3780 Replaces LEN characters from buffer. */
3782 static void
3783 setup_for_ellipsis (it, len)
3784 struct it *it;
3785 int len;
3787 /* Use the display table definition for `...'. Invalid glyphs
3788 will be handled by the method returning elements from dpvec. */
3789 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3791 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3792 it->dpvec = v->contents;
3793 it->dpend = v->contents + v->size;
3795 else
3797 /* Default `...'. */
3798 it->dpvec = default_invis_vector;
3799 it->dpend = default_invis_vector + 3;
3802 it->dpvec_char_len = len;
3803 it->current.dpvec_index = 0;
3804 it->dpvec_face_id = -1;
3806 /* Remember the current face id in case glyphs specify faces.
3807 IT's face is restored in set_iterator_to_next.
3808 saved_face_id was set to preceding char's face in handle_stop. */
3809 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3810 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3812 it->method = GET_FROM_DISPLAY_VECTOR;
3813 it->ellipsis_p = 1;
3818 /***********************************************************************
3819 'display' property
3820 ***********************************************************************/
3822 /* Set up iterator IT from `display' property at its current position.
3823 Called from handle_stop.
3824 We return HANDLED_RETURN if some part of the display property
3825 overrides the display of the buffer text itself.
3826 Otherwise we return HANDLED_NORMALLY. */
3828 static enum prop_handled
3829 handle_display_prop (it)
3830 struct it *it;
3832 Lisp_Object prop, object, overlay;
3833 struct text_pos *position;
3834 /* Nonzero if some property replaces the display of the text itself. */
3835 int display_replaced_p = 0;
3837 if (STRINGP (it->string))
3839 object = it->string;
3840 position = &it->current.string_pos;
3842 else
3844 XSETWINDOW (object, it->w);
3845 position = &it->current.pos;
3848 /* Reset those iterator values set from display property values. */
3849 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3850 it->space_width = Qnil;
3851 it->font_height = Qnil;
3852 it->voffset = 0;
3854 /* We don't support recursive `display' properties, i.e. string
3855 values that have a string `display' property, that have a string
3856 `display' property etc. */
3857 if (!it->string_from_display_prop_p)
3858 it->area = TEXT_AREA;
3860 prop = get_char_property_and_overlay (make_number (position->charpos),
3861 Qdisplay, object, &overlay);
3862 if (NILP (prop))
3863 return HANDLED_NORMALLY;
3864 /* Now OVERLAY is the overlay that gave us this property, or nil
3865 if it was a text property. */
3867 if (!STRINGP (it->string))
3868 object = it->w->buffer;
3870 if (CONSP (prop)
3871 /* Simple properties. */
3872 && !EQ (XCAR (prop), Qimage)
3873 && !EQ (XCAR (prop), Qspace)
3874 && !EQ (XCAR (prop), Qwhen)
3875 && !EQ (XCAR (prop), Qslice)
3876 && !EQ (XCAR (prop), Qspace_width)
3877 && !EQ (XCAR (prop), Qheight)
3878 && !EQ (XCAR (prop), Qraise)
3879 /* Marginal area specifications. */
3880 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3881 && !EQ (XCAR (prop), Qleft_fringe)
3882 && !EQ (XCAR (prop), Qright_fringe)
3883 && !NILP (XCAR (prop)))
3885 for (; CONSP (prop); prop = XCDR (prop))
3887 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3888 position, display_replaced_p))
3890 display_replaced_p = 1;
3891 /* If some text in a string is replaced, `position' no
3892 longer points to the position of `object'. */
3893 if (STRINGP (object))
3894 break;
3898 else if (VECTORP (prop))
3900 int i;
3901 for (i = 0; i < ASIZE (prop); ++i)
3902 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3903 position, display_replaced_p))
3905 display_replaced_p = 1;
3906 /* If some text in a string is replaced, `position' no
3907 longer points to the position of `object'. */
3908 if (STRINGP (object))
3909 break;
3912 else
3914 int ret = handle_single_display_spec (it, prop, object, overlay,
3915 position, 0);
3916 if (ret < 0) /* Replaced by "", i.e. nothing. */
3917 return HANDLED_RECOMPUTE_PROPS;
3918 if (ret)
3919 display_replaced_p = 1;
3922 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3926 /* Value is the position of the end of the `display' property starting
3927 at START_POS in OBJECT. */
3929 static struct text_pos
3930 display_prop_end (it, object, start_pos)
3931 struct it *it;
3932 Lisp_Object object;
3933 struct text_pos start_pos;
3935 Lisp_Object end;
3936 struct text_pos end_pos;
3938 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3939 Qdisplay, object, Qnil);
3940 CHARPOS (end_pos) = XFASTINT (end);
3941 if (STRINGP (object))
3942 compute_string_pos (&end_pos, start_pos, it->string);
3943 else
3944 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3946 return end_pos;
3950 /* Set up IT from a single `display' specification PROP. OBJECT
3951 is the object in which the `display' property was found. *POSITION
3952 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3953 means that we previously saw a display specification which already
3954 replaced text display with something else, for example an image;
3955 we ignore such properties after the first one has been processed.
3957 OVERLAY is the overlay this `display' property came from,
3958 or nil if it was a text property.
3960 If PROP is a `space' or `image' specification, and in some other
3961 cases too, set *POSITION to the position where the `display'
3962 property ends.
3964 Value is non-zero if something was found which replaces the display
3965 of buffer or string text. Specifically, the value is -1 if that
3966 "something" is "nothing". */
3968 static int
3969 handle_single_display_spec (it, spec, object, overlay, position,
3970 display_replaced_before_p)
3971 struct it *it;
3972 Lisp_Object spec;
3973 Lisp_Object object;
3974 Lisp_Object overlay;
3975 struct text_pos *position;
3976 int display_replaced_before_p;
3978 Lisp_Object form;
3979 Lisp_Object location, value;
3980 struct text_pos start_pos, save_pos;
3981 int valid_p;
3983 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3984 If the result is non-nil, use VALUE instead of SPEC. */
3985 form = Qt;
3986 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3988 spec = XCDR (spec);
3989 if (!CONSP (spec))
3990 return 0;
3991 form = XCAR (spec);
3992 spec = XCDR (spec);
3995 if (!NILP (form) && !EQ (form, Qt))
3997 int count = SPECPDL_INDEX ();
3998 struct gcpro gcpro1;
4000 /* Bind `object' to the object having the `display' property, a
4001 buffer or string. Bind `position' to the position in the
4002 object where the property was found, and `buffer-position'
4003 to the current position in the buffer. */
4004 specbind (Qobject, object);
4005 specbind (Qposition, make_number (CHARPOS (*position)));
4006 specbind (Qbuffer_position,
4007 make_number (STRINGP (object)
4008 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4009 GCPRO1 (form);
4010 form = safe_eval (form);
4011 UNGCPRO;
4012 unbind_to (count, Qnil);
4015 if (NILP (form))
4016 return 0;
4018 /* Handle `(height HEIGHT)' specifications. */
4019 if (CONSP (spec)
4020 && EQ (XCAR (spec), Qheight)
4021 && CONSP (XCDR (spec)))
4023 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4024 return 0;
4026 it->font_height = XCAR (XCDR (spec));
4027 if (!NILP (it->font_height))
4029 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4030 int new_height = -1;
4032 if (CONSP (it->font_height)
4033 && (EQ (XCAR (it->font_height), Qplus)
4034 || EQ (XCAR (it->font_height), Qminus))
4035 && CONSP (XCDR (it->font_height))
4036 && INTEGERP (XCAR (XCDR (it->font_height))))
4038 /* `(+ N)' or `(- N)' where N is an integer. */
4039 int steps = XINT (XCAR (XCDR (it->font_height)));
4040 if (EQ (XCAR (it->font_height), Qplus))
4041 steps = - steps;
4042 it->face_id = smaller_face (it->f, it->face_id, steps);
4044 else if (FUNCTIONP (it->font_height))
4046 /* Call function with current height as argument.
4047 Value is the new height. */
4048 Lisp_Object height;
4049 height = safe_call1 (it->font_height,
4050 face->lface[LFACE_HEIGHT_INDEX]);
4051 if (NUMBERP (height))
4052 new_height = XFLOATINT (height);
4054 else if (NUMBERP (it->font_height))
4056 /* Value is a multiple of the canonical char height. */
4057 struct face *face;
4059 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4060 new_height = (XFLOATINT (it->font_height)
4061 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4063 else
4065 /* Evaluate IT->font_height with `height' bound to the
4066 current specified height to get the new height. */
4067 int count = SPECPDL_INDEX ();
4069 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4070 value = safe_eval (it->font_height);
4071 unbind_to (count, Qnil);
4073 if (NUMBERP (value))
4074 new_height = XFLOATINT (value);
4077 if (new_height > 0)
4078 it->face_id = face_with_height (it->f, it->face_id, new_height);
4081 return 0;
4084 /* Handle `(space-width WIDTH)'. */
4085 if (CONSP (spec)
4086 && EQ (XCAR (spec), Qspace_width)
4087 && CONSP (XCDR (spec)))
4089 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4090 return 0;
4092 value = XCAR (XCDR (spec));
4093 if (NUMBERP (value) && XFLOATINT (value) > 0)
4094 it->space_width = value;
4096 return 0;
4099 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4100 if (CONSP (spec)
4101 && EQ (XCAR (spec), Qslice))
4103 Lisp_Object tem;
4105 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4106 return 0;
4108 if (tem = XCDR (spec), CONSP (tem))
4110 it->slice.x = XCAR (tem);
4111 if (tem = XCDR (tem), CONSP (tem))
4113 it->slice.y = XCAR (tem);
4114 if (tem = XCDR (tem), CONSP (tem))
4116 it->slice.width = XCAR (tem);
4117 if (tem = XCDR (tem), CONSP (tem))
4118 it->slice.height = XCAR (tem);
4123 return 0;
4126 /* Handle `(raise FACTOR)'. */
4127 if (CONSP (spec)
4128 && EQ (XCAR (spec), Qraise)
4129 && CONSP (XCDR (spec)))
4131 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4132 return 0;
4134 #ifdef HAVE_WINDOW_SYSTEM
4135 value = XCAR (XCDR (spec));
4136 if (NUMBERP (value))
4138 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4139 it->voffset = - (XFLOATINT (value)
4140 * (FONT_HEIGHT (face->font)));
4142 #endif /* HAVE_WINDOW_SYSTEM */
4144 return 0;
4147 /* Don't handle the other kinds of display specifications
4148 inside a string that we got from a `display' property. */
4149 if (it->string_from_display_prop_p)
4150 return 0;
4152 /* Characters having this form of property are not displayed, so
4153 we have to find the end of the property. */
4154 start_pos = *position;
4155 *position = display_prop_end (it, object, start_pos);
4156 value = Qnil;
4158 /* Stop the scan at that end position--we assume that all
4159 text properties change there. */
4160 it->stop_charpos = position->charpos;
4162 /* Handle `(left-fringe BITMAP [FACE])'
4163 and `(right-fringe BITMAP [FACE])'. */
4164 if (CONSP (spec)
4165 && (EQ (XCAR (spec), Qleft_fringe)
4166 || EQ (XCAR (spec), Qright_fringe))
4167 && CONSP (XCDR (spec)))
4169 int face_id = DEFAULT_FACE_ID;
4170 int fringe_bitmap;
4172 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4173 /* If we return here, POSITION has been advanced
4174 across the text with this property. */
4175 return 0;
4177 #ifdef HAVE_WINDOW_SYSTEM
4178 value = XCAR (XCDR (spec));
4179 if (!SYMBOLP (value)
4180 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4181 /* If we return here, POSITION has been advanced
4182 across the text with this property. */
4183 return 0;
4185 if (CONSP (XCDR (XCDR (spec))))
4187 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4188 int face_id2 = lookup_derived_face (it->f, face_name,
4189 'A', FRINGE_FACE_ID, 0);
4190 if (face_id2 >= 0)
4191 face_id = face_id2;
4194 /* Save current settings of IT so that we can restore them
4195 when we are finished with the glyph property value. */
4197 save_pos = it->position;
4198 it->position = *position;
4199 push_it (it);
4200 it->position = save_pos;
4202 it->area = TEXT_AREA;
4203 it->what = IT_IMAGE;
4204 it->image_id = -1; /* no image */
4205 it->position = start_pos;
4206 it->object = NILP (object) ? it->w->buffer : object;
4207 it->method = GET_FROM_IMAGE;
4208 it->from_overlay = Qnil;
4209 it->face_id = face_id;
4211 /* Say that we haven't consumed the characters with
4212 `display' property yet. The call to pop_it in
4213 set_iterator_to_next will clean this up. */
4214 *position = start_pos;
4216 if (EQ (XCAR (spec), Qleft_fringe))
4218 it->left_user_fringe_bitmap = fringe_bitmap;
4219 it->left_user_fringe_face_id = face_id;
4221 else
4223 it->right_user_fringe_bitmap = fringe_bitmap;
4224 it->right_user_fringe_face_id = face_id;
4226 #endif /* HAVE_WINDOW_SYSTEM */
4227 return 1;
4230 /* Prepare to handle `((margin left-margin) ...)',
4231 `((margin right-margin) ...)' and `((margin nil) ...)'
4232 prefixes for display specifications. */
4233 location = Qunbound;
4234 if (CONSP (spec) && CONSP (XCAR (spec)))
4236 Lisp_Object tem;
4238 value = XCDR (spec);
4239 if (CONSP (value))
4240 value = XCAR (value);
4242 tem = XCAR (spec);
4243 if (EQ (XCAR (tem), Qmargin)
4244 && (tem = XCDR (tem),
4245 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4246 (NILP (tem)
4247 || EQ (tem, Qleft_margin)
4248 || EQ (tem, Qright_margin))))
4249 location = tem;
4252 if (EQ (location, Qunbound))
4254 location = Qnil;
4255 value = spec;
4258 /* After this point, VALUE is the property after any
4259 margin prefix has been stripped. It must be a string,
4260 an image specification, or `(space ...)'.
4262 LOCATION specifies where to display: `left-margin',
4263 `right-margin' or nil. */
4265 valid_p = (STRINGP (value)
4266 #ifdef HAVE_WINDOW_SYSTEM
4267 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4268 #endif /* not HAVE_WINDOW_SYSTEM */
4269 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4271 if (valid_p && !display_replaced_before_p)
4273 /* Save current settings of IT so that we can restore them
4274 when we are finished with the glyph property value. */
4275 save_pos = it->position;
4276 it->position = *position;
4277 push_it (it);
4278 it->position = save_pos;
4279 it->from_overlay = overlay;
4281 if (NILP (location))
4282 it->area = TEXT_AREA;
4283 else if (EQ (location, Qleft_margin))
4284 it->area = LEFT_MARGIN_AREA;
4285 else
4286 it->area = RIGHT_MARGIN_AREA;
4288 if (STRINGP (value))
4290 if (SCHARS (value) == 0)
4292 pop_it (it);
4293 return -1; /* Replaced by "", i.e. nothing. */
4295 it->string = value;
4296 it->multibyte_p = STRING_MULTIBYTE (it->string);
4297 it->current.overlay_string_index = -1;
4298 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4299 it->end_charpos = it->string_nchars = SCHARS (it->string);
4300 it->method = GET_FROM_STRING;
4301 it->stop_charpos = 0;
4302 it->string_from_display_prop_p = 1;
4303 /* Say that we haven't consumed the characters with
4304 `display' property yet. The call to pop_it in
4305 set_iterator_to_next will clean this up. */
4306 if (BUFFERP (object))
4307 *position = start_pos;
4309 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4311 it->method = GET_FROM_STRETCH;
4312 it->object = value;
4313 *position = it->position = start_pos;
4315 #ifdef HAVE_WINDOW_SYSTEM
4316 else
4318 it->what = IT_IMAGE;
4319 it->image_id = lookup_image (it->f, value);
4320 it->position = start_pos;
4321 it->object = NILP (object) ? it->w->buffer : object;
4322 it->method = GET_FROM_IMAGE;
4324 /* Say that we haven't consumed the characters with
4325 `display' property yet. The call to pop_it in
4326 set_iterator_to_next will clean this up. */
4327 *position = start_pos;
4329 #endif /* HAVE_WINDOW_SYSTEM */
4331 return 1;
4334 /* Invalid property or property not supported. Restore
4335 POSITION to what it was before. */
4336 *position = start_pos;
4337 return 0;
4341 /* Check if SPEC is a display specification value whose text should be
4342 treated as intangible. */
4344 static int
4345 single_display_spec_intangible_p (prop)
4346 Lisp_Object prop;
4348 /* Skip over `when FORM'. */
4349 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4351 prop = XCDR (prop);
4352 if (!CONSP (prop))
4353 return 0;
4354 prop = XCDR (prop);
4357 if (STRINGP (prop))
4358 return 1;
4360 if (!CONSP (prop))
4361 return 0;
4363 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4364 we don't need to treat text as intangible. */
4365 if (EQ (XCAR (prop), Qmargin))
4367 prop = XCDR (prop);
4368 if (!CONSP (prop))
4369 return 0;
4371 prop = XCDR (prop);
4372 if (!CONSP (prop)
4373 || EQ (XCAR (prop), Qleft_margin)
4374 || EQ (XCAR (prop), Qright_margin))
4375 return 0;
4378 return (CONSP (prop)
4379 && (EQ (XCAR (prop), Qimage)
4380 || EQ (XCAR (prop), Qspace)));
4384 /* Check if PROP is a display property value whose text should be
4385 treated as intangible. */
4388 display_prop_intangible_p (prop)
4389 Lisp_Object prop;
4391 if (CONSP (prop)
4392 && CONSP (XCAR (prop))
4393 && !EQ (Qmargin, XCAR (XCAR (prop))))
4395 /* A list of sub-properties. */
4396 while (CONSP (prop))
4398 if (single_display_spec_intangible_p (XCAR (prop)))
4399 return 1;
4400 prop = XCDR (prop);
4403 else if (VECTORP (prop))
4405 /* A vector of sub-properties. */
4406 int i;
4407 for (i = 0; i < ASIZE (prop); ++i)
4408 if (single_display_spec_intangible_p (AREF (prop, i)))
4409 return 1;
4411 else
4412 return single_display_spec_intangible_p (prop);
4414 return 0;
4418 /* Return 1 if PROP is a display sub-property value containing STRING. */
4420 static int
4421 single_display_spec_string_p (prop, string)
4422 Lisp_Object prop, string;
4424 if (EQ (string, prop))
4425 return 1;
4427 /* Skip over `when FORM'. */
4428 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4430 prop = XCDR (prop);
4431 if (!CONSP (prop))
4432 return 0;
4433 prop = XCDR (prop);
4436 if (CONSP (prop))
4437 /* Skip over `margin LOCATION'. */
4438 if (EQ (XCAR (prop), Qmargin))
4440 prop = XCDR (prop);
4441 if (!CONSP (prop))
4442 return 0;
4444 prop = XCDR (prop);
4445 if (!CONSP (prop))
4446 return 0;
4449 return CONSP (prop) && EQ (XCAR (prop), string);
4453 /* Return 1 if STRING appears in the `display' property PROP. */
4455 static int
4456 display_prop_string_p (prop, string)
4457 Lisp_Object prop, string;
4459 if (CONSP (prop)
4460 && CONSP (XCAR (prop))
4461 && !EQ (Qmargin, XCAR (XCAR (prop))))
4463 /* A list of sub-properties. */
4464 while (CONSP (prop))
4466 if (single_display_spec_string_p (XCAR (prop), string))
4467 return 1;
4468 prop = XCDR (prop);
4471 else if (VECTORP (prop))
4473 /* A vector of sub-properties. */
4474 int i;
4475 for (i = 0; i < ASIZE (prop); ++i)
4476 if (single_display_spec_string_p (AREF (prop, i), string))
4477 return 1;
4479 else
4480 return single_display_spec_string_p (prop, string);
4482 return 0;
4486 /* Determine from which buffer position in W's buffer STRING comes
4487 from. AROUND_CHARPOS is an approximate position where it could
4488 be from. Value is the buffer position or 0 if it couldn't be
4489 determined.
4491 W's buffer must be current.
4493 This function is necessary because we don't record buffer positions
4494 in glyphs generated from strings (to keep struct glyph small).
4495 This function may only use code that doesn't eval because it is
4496 called asynchronously from note_mouse_highlight. */
4499 string_buffer_position (w, string, around_charpos)
4500 struct window *w;
4501 Lisp_Object string;
4502 int around_charpos;
4504 Lisp_Object limit, prop, pos;
4505 const int MAX_DISTANCE = 1000;
4506 int found = 0;
4508 pos = make_number (around_charpos);
4509 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4510 while (!found && !EQ (pos, limit))
4512 prop = Fget_char_property (pos, Qdisplay, Qnil);
4513 if (!NILP (prop) && display_prop_string_p (prop, string))
4514 found = 1;
4515 else
4516 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4519 if (!found)
4521 pos = make_number (around_charpos);
4522 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4523 while (!found && !EQ (pos, limit))
4525 prop = Fget_char_property (pos, Qdisplay, Qnil);
4526 if (!NILP (prop) && display_prop_string_p (prop, string))
4527 found = 1;
4528 else
4529 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4530 limit);
4534 return found ? XINT (pos) : 0;
4539 /***********************************************************************
4540 `composition' property
4541 ***********************************************************************/
4543 /* Set up iterator IT from `composition' property at its current
4544 position. Called from handle_stop. */
4546 static enum prop_handled
4547 handle_composition_prop (it)
4548 struct it *it;
4550 Lisp_Object prop, string;
4551 int pos, pos_byte, end;
4552 enum prop_handled handled = HANDLED_NORMALLY;
4554 if (STRINGP (it->string))
4556 pos = IT_STRING_CHARPOS (*it);
4557 pos_byte = IT_STRING_BYTEPOS (*it);
4558 string = it->string;
4560 else
4562 pos = IT_CHARPOS (*it);
4563 pos_byte = IT_BYTEPOS (*it);
4564 string = Qnil;
4567 /* If there's a valid composition and point is not inside of the
4568 composition (in the case that the composition is from the current
4569 buffer), draw a glyph composed from the composition components. */
4570 if (find_composition (pos, -1, &pos, &end, &prop, string)
4571 && COMPOSITION_VALID_P (pos, end, prop)
4572 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4574 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4576 if (id >= 0)
4578 struct composition *cmp = composition_table[id];
4580 if (cmp->glyph_len == 0)
4582 /* No glyph. */
4583 if (STRINGP (it->string))
4585 IT_STRING_CHARPOS (*it) = end;
4586 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4587 end);
4589 else
4591 IT_CHARPOS (*it) = end;
4592 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4594 return HANDLED_RECOMPUTE_PROPS;
4597 it->stop_charpos = end;
4598 push_it (it);
4600 it->method = GET_FROM_COMPOSITION;
4601 it->cmp_id = id;
4602 it->cmp_len = COMPOSITION_LENGTH (prop);
4603 /* For a terminal, draw only the first character of the
4604 components. */
4605 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4606 it->len = (STRINGP (it->string)
4607 ? string_char_to_byte (it->string, end)
4608 : CHAR_TO_BYTE (end)) - pos_byte;
4609 handled = HANDLED_RETURN;
4613 return handled;
4618 /***********************************************************************
4619 Overlay strings
4620 ***********************************************************************/
4622 /* The following structure is used to record overlay strings for
4623 later sorting in load_overlay_strings. */
4625 struct overlay_entry
4627 Lisp_Object overlay;
4628 Lisp_Object string;
4629 int priority;
4630 int after_string_p;
4634 /* Set up iterator IT from overlay strings at its current position.
4635 Called from handle_stop. */
4637 static enum prop_handled
4638 handle_overlay_change (it)
4639 struct it *it;
4641 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4642 return HANDLED_RECOMPUTE_PROPS;
4643 else
4644 return HANDLED_NORMALLY;
4648 /* Set up the next overlay string for delivery by IT, if there is an
4649 overlay string to deliver. Called by set_iterator_to_next when the
4650 end of the current overlay string is reached. If there are more
4651 overlay strings to display, IT->string and
4652 IT->current.overlay_string_index are set appropriately here.
4653 Otherwise IT->string is set to nil. */
4655 static void
4656 next_overlay_string (it)
4657 struct it *it;
4659 ++it->current.overlay_string_index;
4660 if (it->current.overlay_string_index == it->n_overlay_strings)
4662 /* No more overlay strings. Restore IT's settings to what
4663 they were before overlay strings were processed, and
4664 continue to deliver from current_buffer. */
4665 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4667 pop_it (it);
4668 xassert (it->sp > 0
4669 || it->method == GET_FROM_COMPOSITION
4670 || (NILP (it->string)
4671 && it->method == GET_FROM_BUFFER
4672 && it->stop_charpos >= BEGV
4673 && it->stop_charpos <= it->end_charpos));
4674 it->current.overlay_string_index = -1;
4675 it->n_overlay_strings = 0;
4677 /* If we're at the end of the buffer, record that we have
4678 processed the overlay strings there already, so that
4679 next_element_from_buffer doesn't try it again. */
4680 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4681 it->overlay_strings_at_end_processed_p = 1;
4683 /* If we have to display `...' for invisible text, set
4684 the iterator up for that. */
4685 if (display_ellipsis_p)
4686 setup_for_ellipsis (it, 0);
4688 else
4690 /* There are more overlay strings to process. If
4691 IT->current.overlay_string_index has advanced to a position
4692 where we must load IT->overlay_strings with more strings, do
4693 it. */
4694 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4696 if (it->current.overlay_string_index && i == 0)
4697 load_overlay_strings (it, 0);
4699 /* Initialize IT to deliver display elements from the overlay
4700 string. */
4701 it->string = it->overlay_strings[i];
4702 it->multibyte_p = STRING_MULTIBYTE (it->string);
4703 SET_TEXT_POS (it->current.string_pos, 0, 0);
4704 it->method = GET_FROM_STRING;
4705 it->stop_charpos = 0;
4708 CHECK_IT (it);
4712 /* Compare two overlay_entry structures E1 and E2. Used as a
4713 comparison function for qsort in load_overlay_strings. Overlay
4714 strings for the same position are sorted so that
4716 1. All after-strings come in front of before-strings, except
4717 when they come from the same overlay.
4719 2. Within after-strings, strings are sorted so that overlay strings
4720 from overlays with higher priorities come first.
4722 2. Within before-strings, strings are sorted so that overlay
4723 strings from overlays with higher priorities come last.
4725 Value is analogous to strcmp. */
4728 static int
4729 compare_overlay_entries (e1, e2)
4730 void *e1, *e2;
4732 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4733 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4734 int result;
4736 if (entry1->after_string_p != entry2->after_string_p)
4738 /* Let after-strings appear in front of before-strings if
4739 they come from different overlays. */
4740 if (EQ (entry1->overlay, entry2->overlay))
4741 result = entry1->after_string_p ? 1 : -1;
4742 else
4743 result = entry1->after_string_p ? -1 : 1;
4745 else if (entry1->after_string_p)
4746 /* After-strings sorted in order of decreasing priority. */
4747 result = entry2->priority - entry1->priority;
4748 else
4749 /* Before-strings sorted in order of increasing priority. */
4750 result = entry1->priority - entry2->priority;
4752 return result;
4756 /* Load the vector IT->overlay_strings with overlay strings from IT's
4757 current buffer position, or from CHARPOS if that is > 0. Set
4758 IT->n_overlays to the total number of overlay strings found.
4760 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4761 a time. On entry into load_overlay_strings,
4762 IT->current.overlay_string_index gives the number of overlay
4763 strings that have already been loaded by previous calls to this
4764 function.
4766 IT->add_overlay_start contains an additional overlay start
4767 position to consider for taking overlay strings from, if non-zero.
4768 This position comes into play when the overlay has an `invisible'
4769 property, and both before and after-strings. When we've skipped to
4770 the end of the overlay, because of its `invisible' property, we
4771 nevertheless want its before-string to appear.
4772 IT->add_overlay_start will contain the overlay start position
4773 in this case.
4775 Overlay strings are sorted so that after-string strings come in
4776 front of before-string strings. Within before and after-strings,
4777 strings are sorted by overlay priority. See also function
4778 compare_overlay_entries. */
4780 static void
4781 load_overlay_strings (it, charpos)
4782 struct it *it;
4783 int charpos;
4785 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4786 Lisp_Object overlay, window, str, invisible;
4787 struct Lisp_Overlay *ov;
4788 int start, end;
4789 int size = 20;
4790 int n = 0, i, j, invis_p;
4791 struct overlay_entry *entries
4792 = (struct overlay_entry *) alloca (size * sizeof *entries);
4794 if (charpos <= 0)
4795 charpos = IT_CHARPOS (*it);
4797 /* Append the overlay string STRING of overlay OVERLAY to vector
4798 `entries' which has size `size' and currently contains `n'
4799 elements. AFTER_P non-zero means STRING is an after-string of
4800 OVERLAY. */
4801 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4802 do \
4804 Lisp_Object priority; \
4806 if (n == size) \
4808 int new_size = 2 * size; \
4809 struct overlay_entry *old = entries; \
4810 entries = \
4811 (struct overlay_entry *) alloca (new_size \
4812 * sizeof *entries); \
4813 bcopy (old, entries, size * sizeof *entries); \
4814 size = new_size; \
4817 entries[n].string = (STRING); \
4818 entries[n].overlay = (OVERLAY); \
4819 priority = Foverlay_get ((OVERLAY), Qpriority); \
4820 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4821 entries[n].after_string_p = (AFTER_P); \
4822 ++n; \
4824 while (0)
4826 /* Process overlay before the overlay center. */
4827 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4829 XSETMISC (overlay, ov);
4830 xassert (OVERLAYP (overlay));
4831 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4832 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4834 if (end < charpos)
4835 break;
4837 /* Skip this overlay if it doesn't start or end at IT's current
4838 position. */
4839 if (end != charpos && start != charpos)
4840 continue;
4842 /* Skip this overlay if it doesn't apply to IT->w. */
4843 window = Foverlay_get (overlay, Qwindow);
4844 if (WINDOWP (window) && XWINDOW (window) != it->w)
4845 continue;
4847 /* If the text ``under'' the overlay is invisible, both before-
4848 and after-strings from this overlay are visible; start and
4849 end position are indistinguishable. */
4850 invisible = Foverlay_get (overlay, Qinvisible);
4851 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4853 /* If overlay has a non-empty before-string, record it. */
4854 if ((start == charpos || (end == charpos && invis_p))
4855 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4856 && SCHARS (str))
4857 RECORD_OVERLAY_STRING (overlay, str, 0);
4859 /* If overlay has a non-empty after-string, record it. */
4860 if ((end == charpos || (start == charpos && invis_p))
4861 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4862 && SCHARS (str))
4863 RECORD_OVERLAY_STRING (overlay, str, 1);
4866 /* Process overlays after the overlay center. */
4867 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4869 XSETMISC (overlay, ov);
4870 xassert (OVERLAYP (overlay));
4871 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4872 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4874 if (start > charpos)
4875 break;
4877 /* Skip this overlay if it doesn't start or end at IT's current
4878 position. */
4879 if (end != charpos && start != charpos)
4880 continue;
4882 /* Skip this overlay if it doesn't apply to IT->w. */
4883 window = Foverlay_get (overlay, Qwindow);
4884 if (WINDOWP (window) && XWINDOW (window) != it->w)
4885 continue;
4887 /* If the text ``under'' the overlay is invisible, it has a zero
4888 dimension, and both before- and after-strings apply. */
4889 invisible = Foverlay_get (overlay, Qinvisible);
4890 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4892 /* If overlay has a non-empty before-string, record it. */
4893 if ((start == charpos || (end == charpos && invis_p))
4894 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4895 && SCHARS (str))
4896 RECORD_OVERLAY_STRING (overlay, str, 0);
4898 /* If overlay has a non-empty after-string, record it. */
4899 if ((end == charpos || (start == charpos && invis_p))
4900 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4901 && SCHARS (str))
4902 RECORD_OVERLAY_STRING (overlay, str, 1);
4905 #undef RECORD_OVERLAY_STRING
4907 /* Sort entries. */
4908 if (n > 1)
4909 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4911 /* Record the total number of strings to process. */
4912 it->n_overlay_strings = n;
4914 /* IT->current.overlay_string_index is the number of overlay strings
4915 that have already been consumed by IT. Copy some of the
4916 remaining overlay strings to IT->overlay_strings. */
4917 i = 0;
4918 j = it->current.overlay_string_index;
4919 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4921 it->overlay_strings[i] = entries[j].string;
4922 it->string_overlays[i++] = entries[j++].overlay;
4925 CHECK_IT (it);
4929 /* Get the first chunk of overlay strings at IT's current buffer
4930 position, or at CHARPOS if that is > 0. Value is non-zero if at
4931 least one overlay string was found. */
4933 static int
4934 get_overlay_strings_1 (it, charpos, compute_stop_p)
4935 struct it *it;
4936 int charpos;
4937 int compute_stop_p;
4939 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4940 process. This fills IT->overlay_strings with strings, and sets
4941 IT->n_overlay_strings to the total number of strings to process.
4942 IT->pos.overlay_string_index has to be set temporarily to zero
4943 because load_overlay_strings needs this; it must be set to -1
4944 when no overlay strings are found because a zero value would
4945 indicate a position in the first overlay string. */
4946 it->current.overlay_string_index = 0;
4947 load_overlay_strings (it, charpos);
4949 /* If we found overlay strings, set up IT to deliver display
4950 elements from the first one. Otherwise set up IT to deliver
4951 from current_buffer. */
4952 if (it->n_overlay_strings)
4954 /* Make sure we know settings in current_buffer, so that we can
4955 restore meaningful values when we're done with the overlay
4956 strings. */
4957 if (compute_stop_p)
4958 compute_stop_pos (it);
4959 xassert (it->face_id >= 0);
4961 /* Save IT's settings. They are restored after all overlay
4962 strings have been processed. */
4963 xassert (!compute_stop_p || it->sp == 0);
4964 push_it (it);
4966 /* Set up IT to deliver display elements from the first overlay
4967 string. */
4968 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4969 it->string = it->overlay_strings[0];
4970 it->from_overlay = Qnil;
4971 it->stop_charpos = 0;
4972 xassert (STRINGP (it->string));
4973 it->end_charpos = SCHARS (it->string);
4974 it->multibyte_p = STRING_MULTIBYTE (it->string);
4975 it->method = GET_FROM_STRING;
4976 return 1;
4979 it->current.overlay_string_index = -1;
4980 return 0;
4983 static int
4984 get_overlay_strings (it, charpos)
4985 struct it *it;
4986 int charpos;
4988 it->string = Qnil;
4989 it->method = GET_FROM_BUFFER;
4991 (void) get_overlay_strings_1 (it, charpos, 1);
4993 CHECK_IT (it);
4995 /* Value is non-zero if we found at least one overlay string. */
4996 return STRINGP (it->string);
5001 /***********************************************************************
5002 Saving and restoring state
5003 ***********************************************************************/
5005 /* Save current settings of IT on IT->stack. Called, for example,
5006 before setting up IT for an overlay string, to be able to restore
5007 IT's settings to what they were after the overlay string has been
5008 processed. */
5010 static void
5011 push_it (it)
5012 struct it *it;
5014 struct iterator_stack_entry *p;
5016 xassert (it->sp < IT_STACK_SIZE);
5017 p = it->stack + it->sp;
5019 p->stop_charpos = it->stop_charpos;
5020 xassert (it->face_id >= 0);
5021 p->face_id = it->face_id;
5022 p->string = it->string;
5023 p->method = it->method;
5024 p->from_overlay = it->from_overlay;
5025 switch (p->method)
5027 case GET_FROM_IMAGE:
5028 p->u.image.object = it->object;
5029 p->u.image.image_id = it->image_id;
5030 p->u.image.slice = it->slice;
5031 break;
5032 case GET_FROM_COMPOSITION:
5033 p->u.comp.object = it->object;
5034 p->u.comp.c = it->c;
5035 p->u.comp.len = it->len;
5036 p->u.comp.cmp_id = it->cmp_id;
5037 p->u.comp.cmp_len = it->cmp_len;
5038 break;
5039 case GET_FROM_STRETCH:
5040 p->u.stretch.object = it->object;
5041 break;
5043 p->position = it->position;
5044 p->current = it->current;
5045 p->end_charpos = it->end_charpos;
5046 p->string_nchars = it->string_nchars;
5047 p->area = it->area;
5048 p->multibyte_p = it->multibyte_p;
5049 p->space_width = it->space_width;
5050 p->font_height = it->font_height;
5051 p->voffset = it->voffset;
5052 p->string_from_display_prop_p = it->string_from_display_prop_p;
5053 p->display_ellipsis_p = 0;
5054 ++it->sp;
5058 /* Restore IT's settings from IT->stack. Called, for example, when no
5059 more overlay strings must be processed, and we return to delivering
5060 display elements from a buffer, or when the end of a string from a
5061 `display' property is reached and we return to delivering display
5062 elements from an overlay string, or from a buffer. */
5064 static void
5065 pop_it (it)
5066 struct it *it;
5068 struct iterator_stack_entry *p;
5070 xassert (it->sp > 0);
5071 --it->sp;
5072 p = it->stack + it->sp;
5073 it->stop_charpos = p->stop_charpos;
5074 it->face_id = p->face_id;
5075 it->current = p->current;
5076 it->position = p->position;
5077 it->string = p->string;
5078 it->from_overlay = p->from_overlay;
5079 if (NILP (it->string))
5080 SET_TEXT_POS (it->current.string_pos, -1, -1);
5081 it->method = p->method;
5082 switch (it->method)
5084 case GET_FROM_IMAGE:
5085 it->image_id = p->u.image.image_id;
5086 it->object = p->u.image.object;
5087 it->slice = p->u.image.slice;
5088 break;
5089 case GET_FROM_COMPOSITION:
5090 it->object = p->u.comp.object;
5091 it->c = p->u.comp.c;
5092 it->len = p->u.comp.len;
5093 it->cmp_id = p->u.comp.cmp_id;
5094 it->cmp_len = p->u.comp.cmp_len;
5095 break;
5096 case GET_FROM_STRETCH:
5097 it->object = p->u.comp.object;
5098 break;
5099 case GET_FROM_BUFFER:
5100 it->object = it->w->buffer;
5101 break;
5102 case GET_FROM_STRING:
5103 it->object = it->string;
5104 break;
5106 it->end_charpos = p->end_charpos;
5107 it->string_nchars = p->string_nchars;
5108 it->area = p->area;
5109 it->multibyte_p = p->multibyte_p;
5110 it->space_width = p->space_width;
5111 it->font_height = p->font_height;
5112 it->voffset = p->voffset;
5113 it->string_from_display_prop_p = p->string_from_display_prop_p;
5118 /***********************************************************************
5119 Moving over lines
5120 ***********************************************************************/
5122 /* Set IT's current position to the previous line start. */
5124 static void
5125 back_to_previous_line_start (it)
5126 struct it *it;
5128 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5129 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5133 /* Move IT to the next line start.
5135 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5136 we skipped over part of the text (as opposed to moving the iterator
5137 continuously over the text). Otherwise, don't change the value
5138 of *SKIPPED_P.
5140 Newlines may come from buffer text, overlay strings, or strings
5141 displayed via the `display' property. That's the reason we can't
5142 simply use find_next_newline_no_quit.
5144 Note that this function may not skip over invisible text that is so
5145 because of text properties and immediately follows a newline. If
5146 it would, function reseat_at_next_visible_line_start, when called
5147 from set_iterator_to_next, would effectively make invisible
5148 characters following a newline part of the wrong glyph row, which
5149 leads to wrong cursor motion. */
5151 static int
5152 forward_to_next_line_start (it, skipped_p)
5153 struct it *it;
5154 int *skipped_p;
5156 int old_selective, newline_found_p, n;
5157 const int MAX_NEWLINE_DISTANCE = 500;
5159 /* If already on a newline, just consume it to avoid unintended
5160 skipping over invisible text below. */
5161 if (it->what == IT_CHARACTER
5162 && it->c == '\n'
5163 && CHARPOS (it->position) == IT_CHARPOS (*it))
5165 set_iterator_to_next (it, 0);
5166 it->c = 0;
5167 return 1;
5170 /* Don't handle selective display in the following. It's (a)
5171 unnecessary because it's done by the caller, and (b) leads to an
5172 infinite recursion because next_element_from_ellipsis indirectly
5173 calls this function. */
5174 old_selective = it->selective;
5175 it->selective = 0;
5177 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5178 from buffer text. */
5179 for (n = newline_found_p = 0;
5180 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5181 n += STRINGP (it->string) ? 0 : 1)
5183 if (!get_next_display_element (it))
5184 return 0;
5185 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5186 set_iterator_to_next (it, 0);
5189 /* If we didn't find a newline near enough, see if we can use a
5190 short-cut. */
5191 if (!newline_found_p)
5193 int start = IT_CHARPOS (*it);
5194 int limit = find_next_newline_no_quit (start, 1);
5195 Lisp_Object pos;
5197 xassert (!STRINGP (it->string));
5199 /* If there isn't any `display' property in sight, and no
5200 overlays, we can just use the position of the newline in
5201 buffer text. */
5202 if (it->stop_charpos >= limit
5203 || ((pos = Fnext_single_property_change (make_number (start),
5204 Qdisplay,
5205 Qnil, make_number (limit)),
5206 NILP (pos))
5207 && next_overlay_change (start) == ZV))
5209 IT_CHARPOS (*it) = limit;
5210 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5211 *skipped_p = newline_found_p = 1;
5213 else
5215 while (get_next_display_element (it)
5216 && !newline_found_p)
5218 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5219 set_iterator_to_next (it, 0);
5224 it->selective = old_selective;
5225 return newline_found_p;
5229 /* Set IT's current position to the previous visible line start. Skip
5230 invisible text that is so either due to text properties or due to
5231 selective display. Caution: this does not change IT->current_x and
5232 IT->hpos. */
5234 static void
5235 back_to_previous_visible_line_start (it)
5236 struct it *it;
5238 while (IT_CHARPOS (*it) > BEGV)
5240 back_to_previous_line_start (it);
5242 if (IT_CHARPOS (*it) <= BEGV)
5243 break;
5245 /* If selective > 0, then lines indented more than that values
5246 are invisible. */
5247 if (it->selective > 0
5248 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5249 (double) it->selective)) /* iftc */
5250 continue;
5252 /* Check the newline before point for invisibility. */
5254 Lisp_Object prop;
5255 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5256 Qinvisible, it->window);
5257 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5258 continue;
5261 if (IT_CHARPOS (*it) <= BEGV)
5262 break;
5265 struct it it2;
5266 int pos;
5267 int beg, end;
5268 Lisp_Object val, overlay;
5270 /* If newline is part of a composition, continue from start of composition */
5271 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5272 && beg < IT_CHARPOS (*it))
5273 goto replaced;
5275 /* If newline is replaced by a display property, find start of overlay
5276 or interval and continue search from that point. */
5277 it2 = *it;
5278 pos = --IT_CHARPOS (it2);
5279 --IT_BYTEPOS (it2);
5280 it2.sp = 0;
5281 if (handle_display_prop (&it2) == HANDLED_RETURN
5282 && !NILP (val = get_char_property_and_overlay
5283 (make_number (pos), Qdisplay, Qnil, &overlay))
5284 && (OVERLAYP (overlay)
5285 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5286 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5287 goto replaced;
5289 /* Newline is not replaced by anything -- so we are done. */
5290 break;
5292 replaced:
5293 if (beg < BEGV)
5294 beg = BEGV;
5295 IT_CHARPOS (*it) = beg;
5296 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5300 it->continuation_lines_width = 0;
5302 xassert (IT_CHARPOS (*it) >= BEGV);
5303 xassert (IT_CHARPOS (*it) == BEGV
5304 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5305 CHECK_IT (it);
5309 /* Reseat iterator IT at the previous visible line start. Skip
5310 invisible text that is so either due to text properties or due to
5311 selective display. At the end, update IT's overlay information,
5312 face information etc. */
5314 void
5315 reseat_at_previous_visible_line_start (it)
5316 struct it *it;
5318 back_to_previous_visible_line_start (it);
5319 reseat (it, it->current.pos, 1);
5320 CHECK_IT (it);
5324 /* Reseat iterator IT on the next visible line start in the current
5325 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5326 preceding the line start. Skip over invisible text that is so
5327 because of selective display. Compute faces, overlays etc at the
5328 new position. Note that this function does not skip over text that
5329 is invisible because of text properties. */
5331 static void
5332 reseat_at_next_visible_line_start (it, on_newline_p)
5333 struct it *it;
5334 int on_newline_p;
5336 int newline_found_p, skipped_p = 0;
5338 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5340 /* Skip over lines that are invisible because they are indented
5341 more than the value of IT->selective. */
5342 if (it->selective > 0)
5343 while (IT_CHARPOS (*it) < ZV
5344 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5345 (double) it->selective)) /* iftc */
5347 xassert (IT_BYTEPOS (*it) == BEGV
5348 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5349 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5352 /* Position on the newline if that's what's requested. */
5353 if (on_newline_p && newline_found_p)
5355 if (STRINGP (it->string))
5357 if (IT_STRING_CHARPOS (*it) > 0)
5359 --IT_STRING_CHARPOS (*it);
5360 --IT_STRING_BYTEPOS (*it);
5363 else if (IT_CHARPOS (*it) > BEGV)
5365 --IT_CHARPOS (*it);
5366 --IT_BYTEPOS (*it);
5367 reseat (it, it->current.pos, 0);
5370 else if (skipped_p)
5371 reseat (it, it->current.pos, 0);
5373 CHECK_IT (it);
5378 /***********************************************************************
5379 Changing an iterator's position
5380 ***********************************************************************/
5382 /* Change IT's current position to POS in current_buffer. If FORCE_P
5383 is non-zero, always check for text properties at the new position.
5384 Otherwise, text properties are only looked up if POS >=
5385 IT->check_charpos of a property. */
5387 static void
5388 reseat (it, pos, force_p)
5389 struct it *it;
5390 struct text_pos pos;
5391 int force_p;
5393 int original_pos = IT_CHARPOS (*it);
5395 reseat_1 (it, pos, 0);
5397 /* Determine where to check text properties. Avoid doing it
5398 where possible because text property lookup is very expensive. */
5399 if (force_p
5400 || CHARPOS (pos) > it->stop_charpos
5401 || CHARPOS (pos) < original_pos)
5402 handle_stop (it);
5404 CHECK_IT (it);
5408 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5409 IT->stop_pos to POS, also. */
5411 static void
5412 reseat_1 (it, pos, set_stop_p)
5413 struct it *it;
5414 struct text_pos pos;
5415 int set_stop_p;
5417 /* Don't call this function when scanning a C string. */
5418 xassert (it->s == NULL);
5420 /* POS must be a reasonable value. */
5421 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5423 it->current.pos = it->position = pos;
5424 it->end_charpos = ZV;
5425 it->dpvec = NULL;
5426 it->current.dpvec_index = -1;
5427 it->current.overlay_string_index = -1;
5428 IT_STRING_CHARPOS (*it) = -1;
5429 IT_STRING_BYTEPOS (*it) = -1;
5430 it->string = Qnil;
5431 it->method = GET_FROM_BUFFER;
5432 it->object = it->w->buffer;
5433 it->area = TEXT_AREA;
5434 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5435 it->sp = 0;
5436 it->string_from_display_prop_p = 0;
5437 it->face_before_selective_p = 0;
5439 if (set_stop_p)
5440 it->stop_charpos = CHARPOS (pos);
5444 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5445 If S is non-null, it is a C string to iterate over. Otherwise,
5446 STRING gives a Lisp string to iterate over.
5448 If PRECISION > 0, don't return more then PRECISION number of
5449 characters from the string.
5451 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5452 characters have been returned. FIELD_WIDTH < 0 means an infinite
5453 field width.
5455 MULTIBYTE = 0 means disable processing of multibyte characters,
5456 MULTIBYTE > 0 means enable it,
5457 MULTIBYTE < 0 means use IT->multibyte_p.
5459 IT must be initialized via a prior call to init_iterator before
5460 calling this function. */
5462 static void
5463 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5464 struct it *it;
5465 unsigned char *s;
5466 Lisp_Object string;
5467 int charpos;
5468 int precision, field_width, multibyte;
5470 /* No region in strings. */
5471 it->region_beg_charpos = it->region_end_charpos = -1;
5473 /* No text property checks performed by default, but see below. */
5474 it->stop_charpos = -1;
5476 /* Set iterator position and end position. */
5477 bzero (&it->current, sizeof it->current);
5478 it->current.overlay_string_index = -1;
5479 it->current.dpvec_index = -1;
5480 xassert (charpos >= 0);
5482 /* If STRING is specified, use its multibyteness, otherwise use the
5483 setting of MULTIBYTE, if specified. */
5484 if (multibyte >= 0)
5485 it->multibyte_p = multibyte > 0;
5487 if (s == NULL)
5489 xassert (STRINGP (string));
5490 it->string = string;
5491 it->s = NULL;
5492 it->end_charpos = it->string_nchars = SCHARS (string);
5493 it->method = GET_FROM_STRING;
5494 it->current.string_pos = string_pos (charpos, string);
5496 else
5498 it->s = s;
5499 it->string = Qnil;
5501 /* Note that we use IT->current.pos, not it->current.string_pos,
5502 for displaying C strings. */
5503 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5504 if (it->multibyte_p)
5506 it->current.pos = c_string_pos (charpos, s, 1);
5507 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5509 else
5511 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5512 it->end_charpos = it->string_nchars = strlen (s);
5515 it->method = GET_FROM_C_STRING;
5518 /* PRECISION > 0 means don't return more than PRECISION characters
5519 from the string. */
5520 if (precision > 0 && it->end_charpos - charpos > precision)
5521 it->end_charpos = it->string_nchars = charpos + precision;
5523 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5524 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5525 FIELD_WIDTH < 0 means infinite field width. This is useful for
5526 padding with `-' at the end of a mode line. */
5527 if (field_width < 0)
5528 field_width = INFINITY;
5529 if (field_width > it->end_charpos - charpos)
5530 it->end_charpos = charpos + field_width;
5532 /* Use the standard display table for displaying strings. */
5533 if (DISP_TABLE_P (Vstandard_display_table))
5534 it->dp = XCHAR_TABLE (Vstandard_display_table);
5536 it->stop_charpos = charpos;
5537 CHECK_IT (it);
5542 /***********************************************************************
5543 Iteration
5544 ***********************************************************************/
5546 /* Map enum it_method value to corresponding next_element_from_* function. */
5548 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5550 next_element_from_buffer,
5551 next_element_from_display_vector,
5552 next_element_from_composition,
5553 next_element_from_string,
5554 next_element_from_c_string,
5555 next_element_from_image,
5556 next_element_from_stretch
5560 /* Load IT's display element fields with information about the next
5561 display element from the current position of IT. Value is zero if
5562 end of buffer (or C string) is reached. */
5564 static struct frame *last_escape_glyph_frame = NULL;
5565 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5566 static int last_escape_glyph_merged_face_id = 0;
5569 get_next_display_element (it)
5570 struct it *it;
5572 /* Non-zero means that we found a display element. Zero means that
5573 we hit the end of what we iterate over. Performance note: the
5574 function pointer `method' used here turns out to be faster than
5575 using a sequence of if-statements. */
5576 int success_p;
5578 get_next:
5579 success_p = (*get_next_element[it->method]) (it);
5581 if (it->what == IT_CHARACTER)
5583 /* Map via display table or translate control characters.
5584 IT->c, IT->len etc. have been set to the next character by
5585 the function call above. If we have a display table, and it
5586 contains an entry for IT->c, translate it. Don't do this if
5587 IT->c itself comes from a display table, otherwise we could
5588 end up in an infinite recursion. (An alternative could be to
5589 count the recursion depth of this function and signal an
5590 error when a certain maximum depth is reached.) Is it worth
5591 it? */
5592 if (success_p && it->dpvec == NULL)
5594 Lisp_Object dv;
5596 if (it->dp
5597 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5598 VECTORP (dv)))
5600 struct Lisp_Vector *v = XVECTOR (dv);
5602 /* Return the first character from the display table
5603 entry, if not empty. If empty, don't display the
5604 current character. */
5605 if (v->size)
5607 it->dpvec_char_len = it->len;
5608 it->dpvec = v->contents;
5609 it->dpend = v->contents + v->size;
5610 it->current.dpvec_index = 0;
5611 it->dpvec_face_id = -1;
5612 it->saved_face_id = it->face_id;
5613 it->method = GET_FROM_DISPLAY_VECTOR;
5614 it->ellipsis_p = 0;
5616 else
5618 set_iterator_to_next (it, 0);
5620 goto get_next;
5623 /* Translate control characters into `\003' or `^C' form.
5624 Control characters coming from a display table entry are
5625 currently not translated because we use IT->dpvec to hold
5626 the translation. This could easily be changed but I
5627 don't believe that it is worth doing.
5629 If it->multibyte_p is nonzero, eight-bit characters and
5630 non-printable multibyte characters are also translated to
5631 octal form.
5633 If it->multibyte_p is zero, eight-bit characters that
5634 don't have corresponding multibyte char code are also
5635 translated to octal form. */
5636 else if ((it->c < ' '
5637 && (it->area != TEXT_AREA
5638 /* In mode line, treat \n like other crl chars. */
5639 || (it->c != '\t'
5640 && it->glyph_row && it->glyph_row->mode_line_p)
5641 || (it->c != '\n' && it->c != '\t')))
5642 || (it->multibyte_p
5643 ? ((it->c >= 127
5644 && it->len == 1)
5645 || !CHAR_PRINTABLE_P (it->c)
5646 || (!NILP (Vnobreak_char_display)
5647 && (it->c == 0x8a0 || it->c == 0x8ad
5648 || it->c == 0x920 || it->c == 0x92d
5649 || it->c == 0xe20 || it->c == 0xe2d
5650 || it->c == 0xf20 || it->c == 0xf2d)))
5651 : (it->c >= 127
5652 && (!unibyte_display_via_language_environment
5653 || it->c == unibyte_char_to_multibyte (it->c)))))
5655 /* IT->c is a control character which must be displayed
5656 either as '\003' or as `^C' where the '\\' and '^'
5657 can be defined in the display table. Fill
5658 IT->ctl_chars with glyphs for what we have to
5659 display. Then, set IT->dpvec to these glyphs. */
5660 GLYPH g;
5661 int ctl_len;
5662 int face_id, lface_id = 0 ;
5663 GLYPH escape_glyph;
5665 /* Handle control characters with ^. */
5667 if (it->c < 128 && it->ctl_arrow_p)
5669 g = '^'; /* default glyph for Control */
5670 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5671 if (it->dp
5672 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5673 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5675 g = XINT (DISP_CTRL_GLYPH (it->dp));
5676 lface_id = FAST_GLYPH_FACE (g);
5678 if (lface_id)
5680 g = FAST_GLYPH_CHAR (g);
5681 face_id = merge_faces (it->f, Qt, lface_id,
5682 it->face_id);
5684 else if (it->f == last_escape_glyph_frame
5685 && it->face_id == last_escape_glyph_face_id)
5687 face_id = last_escape_glyph_merged_face_id;
5689 else
5691 /* Merge the escape-glyph face into the current face. */
5692 face_id = merge_faces (it->f, Qescape_glyph, 0,
5693 it->face_id);
5694 last_escape_glyph_frame = it->f;
5695 last_escape_glyph_face_id = it->face_id;
5696 last_escape_glyph_merged_face_id = face_id;
5699 XSETINT (it->ctl_chars[0], g);
5700 g = it->c ^ 0100;
5701 XSETINT (it->ctl_chars[1], g);
5702 ctl_len = 2;
5703 goto display_control;
5706 /* Handle non-break space in the mode where it only gets
5707 highlighting. */
5709 if (EQ (Vnobreak_char_display, Qt)
5710 && (it->c == 0x8a0 || it->c == 0x920
5711 || it->c == 0xe20 || it->c == 0xf20))
5713 /* Merge the no-break-space face into the current face. */
5714 face_id = merge_faces (it->f, Qnobreak_space, 0,
5715 it->face_id);
5717 g = it->c = ' ';
5718 XSETINT (it->ctl_chars[0], g);
5719 ctl_len = 1;
5720 goto display_control;
5723 /* Handle sequences that start with the "escape glyph". */
5725 /* the default escape glyph is \. */
5726 escape_glyph = '\\';
5728 if (it->dp
5729 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5730 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5732 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5733 lface_id = FAST_GLYPH_FACE (escape_glyph);
5735 if (lface_id)
5737 /* The display table specified a face.
5738 Merge it into face_id and also into escape_glyph. */
5739 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5740 face_id = merge_faces (it->f, Qt, lface_id,
5741 it->face_id);
5743 else if (it->f == last_escape_glyph_frame
5744 && it->face_id == last_escape_glyph_face_id)
5746 face_id = last_escape_glyph_merged_face_id;
5748 else
5750 /* Merge the escape-glyph face into the current face. */
5751 face_id = merge_faces (it->f, Qescape_glyph, 0,
5752 it->face_id);
5753 last_escape_glyph_frame = it->f;
5754 last_escape_glyph_face_id = it->face_id;
5755 last_escape_glyph_merged_face_id = face_id;
5758 /* Handle soft hyphens in the mode where they only get
5759 highlighting. */
5761 if (EQ (Vnobreak_char_display, Qt)
5762 && (it->c == 0x8ad || it->c == 0x92d
5763 || it->c == 0xe2d || it->c == 0xf2d))
5765 g = it->c = '-';
5766 XSETINT (it->ctl_chars[0], g);
5767 ctl_len = 1;
5768 goto display_control;
5771 /* Handle non-break space and soft hyphen
5772 with the escape glyph. */
5774 if (it->c == 0x8a0 || it->c == 0x8ad
5775 || it->c == 0x920 || it->c == 0x92d
5776 || it->c == 0xe20 || it->c == 0xe2d
5777 || it->c == 0xf20 || it->c == 0xf2d)
5779 XSETINT (it->ctl_chars[0], escape_glyph);
5780 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5781 XSETINT (it->ctl_chars[1], g);
5782 ctl_len = 2;
5783 goto display_control;
5787 unsigned char str[MAX_MULTIBYTE_LENGTH];
5788 int len;
5789 int i;
5791 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5792 if (SINGLE_BYTE_CHAR_P (it->c))
5793 str[0] = it->c, len = 1;
5794 else
5796 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5797 if (len < 0)
5799 /* It's an invalid character, which shouldn't
5800 happen actually, but due to bugs it may
5801 happen. Let's print the char as is, there's
5802 not much meaningful we can do with it. */
5803 str[0] = it->c;
5804 str[1] = it->c >> 8;
5805 str[2] = it->c >> 16;
5806 str[3] = it->c >> 24;
5807 len = 4;
5811 for (i = 0; i < len; i++)
5813 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5814 /* Insert three more glyphs into IT->ctl_chars for
5815 the octal display of the character. */
5816 g = ((str[i] >> 6) & 7) + '0';
5817 XSETINT (it->ctl_chars[i * 4 + 1], g);
5818 g = ((str[i] >> 3) & 7) + '0';
5819 XSETINT (it->ctl_chars[i * 4 + 2], g);
5820 g = (str[i] & 7) + '0';
5821 XSETINT (it->ctl_chars[i * 4 + 3], g);
5823 ctl_len = len * 4;
5826 display_control:
5827 /* Set up IT->dpvec and return first character from it. */
5828 it->dpvec_char_len = it->len;
5829 it->dpvec = it->ctl_chars;
5830 it->dpend = it->dpvec + ctl_len;
5831 it->current.dpvec_index = 0;
5832 it->dpvec_face_id = face_id;
5833 it->saved_face_id = it->face_id;
5834 it->method = GET_FROM_DISPLAY_VECTOR;
5835 it->ellipsis_p = 0;
5836 goto get_next;
5840 /* Adjust face id for a multibyte character. There are no
5841 multibyte character in unibyte text. */
5842 if (it->multibyte_p
5843 && success_p
5844 && FRAME_WINDOW_P (it->f))
5846 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5847 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5851 /* Is this character the last one of a run of characters with
5852 box? If yes, set IT->end_of_box_run_p to 1. */
5853 if (it->face_box_p
5854 && it->s == NULL)
5856 int face_id;
5857 struct face *face;
5859 it->end_of_box_run_p
5860 = ((face_id = face_after_it_pos (it),
5861 face_id != it->face_id)
5862 && (face = FACE_FROM_ID (it->f, face_id),
5863 face->box == FACE_NO_BOX));
5866 /* Value is 0 if end of buffer or string reached. */
5867 return success_p;
5871 /* Move IT to the next display element.
5873 RESEAT_P non-zero means if called on a newline in buffer text,
5874 skip to the next visible line start.
5876 Functions get_next_display_element and set_iterator_to_next are
5877 separate because I find this arrangement easier to handle than a
5878 get_next_display_element function that also increments IT's
5879 position. The way it is we can first look at an iterator's current
5880 display element, decide whether it fits on a line, and if it does,
5881 increment the iterator position. The other way around we probably
5882 would either need a flag indicating whether the iterator has to be
5883 incremented the next time, or we would have to implement a
5884 decrement position function which would not be easy to write. */
5886 void
5887 set_iterator_to_next (it, reseat_p)
5888 struct it *it;
5889 int reseat_p;
5891 /* Reset flags indicating start and end of a sequence of characters
5892 with box. Reset them at the start of this function because
5893 moving the iterator to a new position might set them. */
5894 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5896 switch (it->method)
5898 case GET_FROM_BUFFER:
5899 /* The current display element of IT is a character from
5900 current_buffer. Advance in the buffer, and maybe skip over
5901 invisible lines that are so because of selective display. */
5902 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5903 reseat_at_next_visible_line_start (it, 0);
5904 else
5906 xassert (it->len != 0);
5907 IT_BYTEPOS (*it) += it->len;
5908 IT_CHARPOS (*it) += 1;
5909 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5911 break;
5913 case GET_FROM_COMPOSITION:
5914 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5915 xassert (it->sp > 0);
5916 pop_it (it);
5917 if (it->method == GET_FROM_STRING)
5919 IT_STRING_BYTEPOS (*it) += it->len;
5920 IT_STRING_CHARPOS (*it) += it->cmp_len;
5921 goto consider_string_end;
5923 else if (it->method == GET_FROM_BUFFER)
5925 IT_BYTEPOS (*it) += it->len;
5926 IT_CHARPOS (*it) += it->cmp_len;
5928 break;
5930 case GET_FROM_C_STRING:
5931 /* Current display element of IT is from a C string. */
5932 IT_BYTEPOS (*it) += it->len;
5933 IT_CHARPOS (*it) += 1;
5934 break;
5936 case GET_FROM_DISPLAY_VECTOR:
5937 /* Current display element of IT is from a display table entry.
5938 Advance in the display table definition. Reset it to null if
5939 end reached, and continue with characters from buffers/
5940 strings. */
5941 ++it->current.dpvec_index;
5943 /* Restore face of the iterator to what they were before the
5944 display vector entry (these entries may contain faces). */
5945 it->face_id = it->saved_face_id;
5947 if (it->dpvec + it->current.dpvec_index == it->dpend)
5949 int recheck_faces = it->ellipsis_p;
5951 if (it->s)
5952 it->method = GET_FROM_C_STRING;
5953 else if (STRINGP (it->string))
5954 it->method = GET_FROM_STRING;
5955 else
5957 it->method = GET_FROM_BUFFER;
5958 it->object = it->w->buffer;
5961 it->dpvec = NULL;
5962 it->current.dpvec_index = -1;
5964 /* Skip over characters which were displayed via IT->dpvec. */
5965 if (it->dpvec_char_len < 0)
5966 reseat_at_next_visible_line_start (it, 1);
5967 else if (it->dpvec_char_len > 0)
5969 if (it->method == GET_FROM_STRING
5970 && it->n_overlay_strings > 0)
5971 it->ignore_overlay_strings_at_pos_p = 1;
5972 it->len = it->dpvec_char_len;
5973 set_iterator_to_next (it, reseat_p);
5976 /* Maybe recheck faces after display vector */
5977 if (recheck_faces)
5978 it->stop_charpos = IT_CHARPOS (*it);
5980 break;
5982 case GET_FROM_STRING:
5983 /* Current display element is a character from a Lisp string. */
5984 xassert (it->s == NULL && STRINGP (it->string));
5985 IT_STRING_BYTEPOS (*it) += it->len;
5986 IT_STRING_CHARPOS (*it) += 1;
5988 consider_string_end:
5990 if (it->current.overlay_string_index >= 0)
5992 /* IT->string is an overlay string. Advance to the
5993 next, if there is one. */
5994 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5995 next_overlay_string (it);
5997 else
5999 /* IT->string is not an overlay string. If we reached
6000 its end, and there is something on IT->stack, proceed
6001 with what is on the stack. This can be either another
6002 string, this time an overlay string, or a buffer. */
6003 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6004 && it->sp > 0)
6006 pop_it (it);
6007 if (it->method == GET_FROM_STRING)
6008 goto consider_string_end;
6011 break;
6013 case GET_FROM_IMAGE:
6014 case GET_FROM_STRETCH:
6015 /* The position etc with which we have to proceed are on
6016 the stack. The position may be at the end of a string,
6017 if the `display' property takes up the whole string. */
6018 xassert (it->sp > 0);
6019 pop_it (it);
6020 if (it->method == GET_FROM_STRING)
6021 goto consider_string_end;
6022 break;
6024 default:
6025 /* There are no other methods defined, so this should be a bug. */
6026 abort ();
6029 xassert (it->method != GET_FROM_STRING
6030 || (STRINGP (it->string)
6031 && IT_STRING_CHARPOS (*it) >= 0));
6034 /* Load IT's display element fields with information about the next
6035 display element which comes from a display table entry or from the
6036 result of translating a control character to one of the forms `^C'
6037 or `\003'.
6039 IT->dpvec holds the glyphs to return as characters.
6040 IT->saved_face_id holds the face id before the display vector--
6041 it is restored into IT->face_idin set_iterator_to_next. */
6043 static int
6044 next_element_from_display_vector (it)
6045 struct it *it;
6047 /* Precondition. */
6048 xassert (it->dpvec && it->current.dpvec_index >= 0);
6050 it->face_id = it->saved_face_id;
6052 if (INTEGERP (*it->dpvec)
6053 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6055 GLYPH g;
6057 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6058 it->c = FAST_GLYPH_CHAR (g);
6059 it->len = CHAR_BYTES (it->c);
6061 /* The entry may contain a face id to use. Such a face id is
6062 the id of a Lisp face, not a realized face. A face id of
6063 zero means no face is specified. */
6064 if (it->dpvec_face_id >= 0)
6065 it->face_id = it->dpvec_face_id;
6066 else
6068 int lface_id = FAST_GLYPH_FACE (g);
6069 if (lface_id > 0)
6070 it->face_id = merge_faces (it->f, Qt, lface_id,
6071 it->saved_face_id);
6074 else
6075 /* Display table entry is invalid. Return a space. */
6076 it->c = ' ', it->len = 1;
6078 /* Don't change position and object of the iterator here. They are
6079 still the values of the character that had this display table
6080 entry or was translated, and that's what we want. */
6081 it->what = IT_CHARACTER;
6082 return 1;
6086 /* Load IT with the next display element from Lisp string IT->string.
6087 IT->current.string_pos is the current position within the string.
6088 If IT->current.overlay_string_index >= 0, the Lisp string is an
6089 overlay string. */
6091 static int
6092 next_element_from_string (it)
6093 struct it *it;
6095 struct text_pos position;
6097 xassert (STRINGP (it->string));
6098 xassert (IT_STRING_CHARPOS (*it) >= 0);
6099 position = it->current.string_pos;
6101 /* Time to check for invisible text? */
6102 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6103 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6105 handle_stop (it);
6107 /* Since a handler may have changed IT->method, we must
6108 recurse here. */
6109 return get_next_display_element (it);
6112 if (it->current.overlay_string_index >= 0)
6114 /* Get the next character from an overlay string. In overlay
6115 strings, There is no field width or padding with spaces to
6116 do. */
6117 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6119 it->what = IT_EOB;
6120 return 0;
6122 else if (STRING_MULTIBYTE (it->string))
6124 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6125 const unsigned char *s = (SDATA (it->string)
6126 + IT_STRING_BYTEPOS (*it));
6127 it->c = string_char_and_length (s, remaining, &it->len);
6129 else
6131 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6132 it->len = 1;
6135 else
6137 /* Get the next character from a Lisp string that is not an
6138 overlay string. Such strings come from the mode line, for
6139 example. We may have to pad with spaces, or truncate the
6140 string. See also next_element_from_c_string. */
6141 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6143 it->what = IT_EOB;
6144 return 0;
6146 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6148 /* Pad with spaces. */
6149 it->c = ' ', it->len = 1;
6150 CHARPOS (position) = BYTEPOS (position) = -1;
6152 else if (STRING_MULTIBYTE (it->string))
6154 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6155 const unsigned char *s = (SDATA (it->string)
6156 + IT_STRING_BYTEPOS (*it));
6157 it->c = string_char_and_length (s, maxlen, &it->len);
6159 else
6161 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6162 it->len = 1;
6166 /* Record what we have and where it came from. */
6167 it->what = IT_CHARACTER;
6168 it->object = it->string;
6169 it->position = position;
6170 return 1;
6174 /* Load IT with next display element from C string IT->s.
6175 IT->string_nchars is the maximum number of characters to return
6176 from the string. IT->end_charpos may be greater than
6177 IT->string_nchars when this function is called, in which case we
6178 may have to return padding spaces. Value is zero if end of string
6179 reached, including padding spaces. */
6181 static int
6182 next_element_from_c_string (it)
6183 struct it *it;
6185 int success_p = 1;
6187 xassert (it->s);
6188 it->what = IT_CHARACTER;
6189 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6190 it->object = Qnil;
6192 /* IT's position can be greater IT->string_nchars in case a field
6193 width or precision has been specified when the iterator was
6194 initialized. */
6195 if (IT_CHARPOS (*it) >= it->end_charpos)
6197 /* End of the game. */
6198 it->what = IT_EOB;
6199 success_p = 0;
6201 else if (IT_CHARPOS (*it) >= it->string_nchars)
6203 /* Pad with spaces. */
6204 it->c = ' ', it->len = 1;
6205 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6207 else if (it->multibyte_p)
6209 /* Implementation note: The calls to strlen apparently aren't a
6210 performance problem because there is no noticeable performance
6211 difference between Emacs running in unibyte or multibyte mode. */
6212 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6213 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6214 maxlen, &it->len);
6216 else
6217 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6219 return success_p;
6223 /* Set up IT to return characters from an ellipsis, if appropriate.
6224 The definition of the ellipsis glyphs may come from a display table
6225 entry. This function Fills IT with the first glyph from the
6226 ellipsis if an ellipsis is to be displayed. */
6228 static int
6229 next_element_from_ellipsis (it)
6230 struct it *it;
6232 if (it->selective_display_ellipsis_p)
6233 setup_for_ellipsis (it, it->len);
6234 else
6236 /* The face at the current position may be different from the
6237 face we find after the invisible text. Remember what it
6238 was in IT->saved_face_id, and signal that it's there by
6239 setting face_before_selective_p. */
6240 it->saved_face_id = it->face_id;
6241 it->method = GET_FROM_BUFFER;
6242 it->object = it->w->buffer;
6243 reseat_at_next_visible_line_start (it, 1);
6244 it->face_before_selective_p = 1;
6247 return get_next_display_element (it);
6251 /* Deliver an image display element. The iterator IT is already
6252 filled with image information (done in handle_display_prop). Value
6253 is always 1. */
6256 static int
6257 next_element_from_image (it)
6258 struct it *it;
6260 it->what = IT_IMAGE;
6261 return 1;
6265 /* Fill iterator IT with next display element from a stretch glyph
6266 property. IT->object is the value of the text property. Value is
6267 always 1. */
6269 static int
6270 next_element_from_stretch (it)
6271 struct it *it;
6273 it->what = IT_STRETCH;
6274 return 1;
6278 /* Load IT with the next display element from current_buffer. Value
6279 is zero if end of buffer reached. IT->stop_charpos is the next
6280 position at which to stop and check for text properties or buffer
6281 end. */
6283 static int
6284 next_element_from_buffer (it)
6285 struct it *it;
6287 int success_p = 1;
6289 /* Check this assumption, otherwise, we would never enter the
6290 if-statement, below. */
6291 xassert (IT_CHARPOS (*it) >= BEGV
6292 && IT_CHARPOS (*it) <= it->stop_charpos);
6294 if (IT_CHARPOS (*it) >= it->stop_charpos)
6296 if (IT_CHARPOS (*it) >= it->end_charpos)
6298 int overlay_strings_follow_p;
6300 /* End of the game, except when overlay strings follow that
6301 haven't been returned yet. */
6302 if (it->overlay_strings_at_end_processed_p)
6303 overlay_strings_follow_p = 0;
6304 else
6306 it->overlay_strings_at_end_processed_p = 1;
6307 overlay_strings_follow_p = get_overlay_strings (it, 0);
6310 if (overlay_strings_follow_p)
6311 success_p = get_next_display_element (it);
6312 else
6314 it->what = IT_EOB;
6315 it->position = it->current.pos;
6316 success_p = 0;
6319 else
6321 handle_stop (it);
6322 return get_next_display_element (it);
6325 else
6327 /* No face changes, overlays etc. in sight, so just return a
6328 character from current_buffer. */
6329 unsigned char *p;
6331 /* Maybe run the redisplay end trigger hook. Performance note:
6332 This doesn't seem to cost measurable time. */
6333 if (it->redisplay_end_trigger_charpos
6334 && it->glyph_row
6335 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6336 run_redisplay_end_trigger_hook (it);
6338 /* Get the next character, maybe multibyte. */
6339 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6340 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6342 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6343 - IT_BYTEPOS (*it));
6344 it->c = string_char_and_length (p, maxlen, &it->len);
6346 else
6347 it->c = *p, it->len = 1;
6349 /* Record what we have and where it came from. */
6350 it->what = IT_CHARACTER;
6351 it->object = it->w->buffer;
6352 it->position = it->current.pos;
6354 /* Normally we return the character found above, except when we
6355 really want to return an ellipsis for selective display. */
6356 if (it->selective)
6358 if (it->c == '\n')
6360 /* A value of selective > 0 means hide lines indented more
6361 than that number of columns. */
6362 if (it->selective > 0
6363 && IT_CHARPOS (*it) + 1 < ZV
6364 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6365 IT_BYTEPOS (*it) + 1,
6366 (double) it->selective)) /* iftc */
6368 success_p = next_element_from_ellipsis (it);
6369 it->dpvec_char_len = -1;
6372 else if (it->c == '\r' && it->selective == -1)
6374 /* A value of selective == -1 means that everything from the
6375 CR to the end of the line is invisible, with maybe an
6376 ellipsis displayed for it. */
6377 success_p = next_element_from_ellipsis (it);
6378 it->dpvec_char_len = -1;
6383 /* Value is zero if end of buffer reached. */
6384 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6385 return success_p;
6389 /* Run the redisplay end trigger hook for IT. */
6391 static void
6392 run_redisplay_end_trigger_hook (it)
6393 struct it *it;
6395 Lisp_Object args[3];
6397 /* IT->glyph_row should be non-null, i.e. we should be actually
6398 displaying something, or otherwise we should not run the hook. */
6399 xassert (it->glyph_row);
6401 /* Set up hook arguments. */
6402 args[0] = Qredisplay_end_trigger_functions;
6403 args[1] = it->window;
6404 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6405 it->redisplay_end_trigger_charpos = 0;
6407 /* Since we are *trying* to run these functions, don't try to run
6408 them again, even if they get an error. */
6409 it->w->redisplay_end_trigger = Qnil;
6410 Frun_hook_with_args (3, args);
6412 /* Notice if it changed the face of the character we are on. */
6413 handle_face_prop (it);
6417 /* Deliver a composition display element. The iterator IT is already
6418 filled with composition information (done in
6419 handle_composition_prop). Value is always 1. */
6421 static int
6422 next_element_from_composition (it)
6423 struct it *it;
6425 it->what = IT_COMPOSITION;
6426 it->position = (STRINGP (it->string)
6427 ? it->current.string_pos
6428 : it->current.pos);
6429 if (STRINGP (it->string))
6430 it->object = it->string;
6431 else
6432 it->object = it->w->buffer;
6433 return 1;
6438 /***********************************************************************
6439 Moving an iterator without producing glyphs
6440 ***********************************************************************/
6442 /* Check if iterator is at a position corresponding to a valid buffer
6443 position after some move_it_ call. */
6445 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6446 ((it)->method == GET_FROM_STRING \
6447 ? IT_STRING_CHARPOS (*it) == 0 \
6448 : 1)
6451 /* Move iterator IT to a specified buffer or X position within one
6452 line on the display without producing glyphs.
6454 OP should be a bit mask including some or all of these bits:
6455 MOVE_TO_X: Stop on reaching x-position TO_X.
6456 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6457 Regardless of OP's value, stop in reaching the end of the display line.
6459 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6460 This means, in particular, that TO_X includes window's horizontal
6461 scroll amount.
6463 The return value has several possible values that
6464 say what condition caused the scan to stop:
6466 MOVE_POS_MATCH_OR_ZV
6467 - when TO_POS or ZV was reached.
6469 MOVE_X_REACHED
6470 -when TO_X was reached before TO_POS or ZV were reached.
6472 MOVE_LINE_CONTINUED
6473 - when we reached the end of the display area and the line must
6474 be continued.
6476 MOVE_LINE_TRUNCATED
6477 - when we reached the end of the display area and the line is
6478 truncated.
6480 MOVE_NEWLINE_OR_CR
6481 - when we stopped at a line end, i.e. a newline or a CR and selective
6482 display is on. */
6484 static enum move_it_result
6485 move_it_in_display_line_to (it, to_charpos, to_x, op)
6486 struct it *it;
6487 int to_charpos, to_x, op;
6489 enum move_it_result result = MOVE_UNDEFINED;
6490 struct glyph_row *saved_glyph_row;
6492 /* Don't produce glyphs in produce_glyphs. */
6493 saved_glyph_row = it->glyph_row;
6494 it->glyph_row = NULL;
6496 #define BUFFER_POS_REACHED_P() \
6497 ((op & MOVE_TO_POS) != 0 \
6498 && BUFFERP (it->object) \
6499 && IT_CHARPOS (*it) >= to_charpos \
6500 && (it->method == GET_FROM_BUFFER \
6501 || (it->method == GET_FROM_DISPLAY_VECTOR \
6502 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6505 while (1)
6507 int x, i, ascent = 0, descent = 0;
6509 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6510 if ((op & MOVE_TO_POS) != 0
6511 && BUFFERP (it->object)
6512 && it->method == GET_FROM_BUFFER
6513 && IT_CHARPOS (*it) > to_charpos)
6515 result = MOVE_POS_MATCH_OR_ZV;
6516 break;
6519 /* Stop when ZV reached.
6520 We used to stop here when TO_CHARPOS reached as well, but that is
6521 too soon if this glyph does not fit on this line. So we handle it
6522 explicitly below. */
6523 if (!get_next_display_element (it)
6524 || (it->truncate_lines_p
6525 && BUFFER_POS_REACHED_P ()))
6527 result = MOVE_POS_MATCH_OR_ZV;
6528 break;
6531 /* The call to produce_glyphs will get the metrics of the
6532 display element IT is loaded with. We record in x the
6533 x-position before this display element in case it does not
6534 fit on the line. */
6535 x = it->current_x;
6537 /* Remember the line height so far in case the next element doesn't
6538 fit on the line. */
6539 if (!it->truncate_lines_p)
6541 ascent = it->max_ascent;
6542 descent = it->max_descent;
6545 PRODUCE_GLYPHS (it);
6547 if (it->area != TEXT_AREA)
6549 set_iterator_to_next (it, 1);
6550 continue;
6553 /* The number of glyphs we get back in IT->nglyphs will normally
6554 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6555 character on a terminal frame, or (iii) a line end. For the
6556 second case, IT->nglyphs - 1 padding glyphs will be present
6557 (on X frames, there is only one glyph produced for a
6558 composite character.
6560 The behavior implemented below means, for continuation lines,
6561 that as many spaces of a TAB as fit on the current line are
6562 displayed there. For terminal frames, as many glyphs of a
6563 multi-glyph character are displayed in the current line, too.
6564 This is what the old redisplay code did, and we keep it that
6565 way. Under X, the whole shape of a complex character must
6566 fit on the line or it will be completely displayed in the
6567 next line.
6569 Note that both for tabs and padding glyphs, all glyphs have
6570 the same width. */
6571 if (it->nglyphs)
6573 /* More than one glyph or glyph doesn't fit on line. All
6574 glyphs have the same width. */
6575 int single_glyph_width = it->pixel_width / it->nglyphs;
6576 int new_x;
6577 int x_before_this_char = x;
6578 int hpos_before_this_char = it->hpos;
6580 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6582 new_x = x + single_glyph_width;
6584 /* We want to leave anything reaching TO_X to the caller. */
6585 if ((op & MOVE_TO_X) && new_x > to_x)
6587 if (BUFFER_POS_REACHED_P ())
6588 goto buffer_pos_reached;
6589 it->current_x = x;
6590 result = MOVE_X_REACHED;
6591 break;
6593 else if (/* Lines are continued. */
6594 !it->truncate_lines_p
6595 && (/* And glyph doesn't fit on the line. */
6596 new_x > it->last_visible_x
6597 /* Or it fits exactly and we're on a window
6598 system frame. */
6599 || (new_x == it->last_visible_x
6600 && FRAME_WINDOW_P (it->f))))
6602 if (/* IT->hpos == 0 means the very first glyph
6603 doesn't fit on the line, e.g. a wide image. */
6604 it->hpos == 0
6605 || (new_x == it->last_visible_x
6606 && FRAME_WINDOW_P (it->f)))
6608 ++it->hpos;
6609 it->current_x = new_x;
6611 /* The character's last glyph just barely fits
6612 in this row. */
6613 if (i == it->nglyphs - 1)
6615 /* If this is the destination position,
6616 return a position *before* it in this row,
6617 now that we know it fits in this row. */
6618 if (BUFFER_POS_REACHED_P ())
6620 it->hpos = hpos_before_this_char;
6621 it->current_x = x_before_this_char;
6622 result = MOVE_POS_MATCH_OR_ZV;
6623 break;
6626 set_iterator_to_next (it, 1);
6627 #ifdef HAVE_WINDOW_SYSTEM
6628 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6630 if (!get_next_display_element (it))
6632 result = MOVE_POS_MATCH_OR_ZV;
6633 break;
6635 if (BUFFER_POS_REACHED_P ())
6637 if (ITERATOR_AT_END_OF_LINE_P (it))
6638 result = MOVE_POS_MATCH_OR_ZV;
6639 else
6640 result = MOVE_LINE_CONTINUED;
6641 break;
6643 if (ITERATOR_AT_END_OF_LINE_P (it))
6645 result = MOVE_NEWLINE_OR_CR;
6646 break;
6649 #endif /* HAVE_WINDOW_SYSTEM */
6652 else
6654 it->current_x = x;
6655 it->max_ascent = ascent;
6656 it->max_descent = descent;
6659 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6660 IT_CHARPOS (*it)));
6661 result = MOVE_LINE_CONTINUED;
6662 break;
6664 else if (BUFFER_POS_REACHED_P ())
6665 goto buffer_pos_reached;
6666 else if (new_x > it->first_visible_x)
6668 /* Glyph is visible. Increment number of glyphs that
6669 would be displayed. */
6670 ++it->hpos;
6672 else
6674 /* Glyph is completely off the left margin of the display
6675 area. Nothing to do. */
6679 if (result != MOVE_UNDEFINED)
6680 break;
6682 else if (BUFFER_POS_REACHED_P ())
6684 buffer_pos_reached:
6685 it->current_x = x;
6686 it->max_ascent = ascent;
6687 it->max_descent = descent;
6688 result = MOVE_POS_MATCH_OR_ZV;
6689 break;
6691 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6693 /* Stop when TO_X specified and reached. This check is
6694 necessary here because of lines consisting of a line end,
6695 only. The line end will not produce any glyphs and we
6696 would never get MOVE_X_REACHED. */
6697 xassert (it->nglyphs == 0);
6698 result = MOVE_X_REACHED;
6699 break;
6702 /* Is this a line end? If yes, we're done. */
6703 if (ITERATOR_AT_END_OF_LINE_P (it))
6705 result = MOVE_NEWLINE_OR_CR;
6706 break;
6709 /* The current display element has been consumed. Advance
6710 to the next. */
6711 set_iterator_to_next (it, 1);
6713 /* Stop if lines are truncated and IT's current x-position is
6714 past the right edge of the window now. */
6715 if (it->truncate_lines_p
6716 && it->current_x >= it->last_visible_x)
6718 #ifdef HAVE_WINDOW_SYSTEM
6719 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6721 if (!get_next_display_element (it)
6722 || BUFFER_POS_REACHED_P ())
6724 result = MOVE_POS_MATCH_OR_ZV;
6725 break;
6727 if (ITERATOR_AT_END_OF_LINE_P (it))
6729 result = MOVE_NEWLINE_OR_CR;
6730 break;
6733 #endif /* HAVE_WINDOW_SYSTEM */
6734 result = MOVE_LINE_TRUNCATED;
6735 break;
6739 #undef BUFFER_POS_REACHED_P
6741 /* Restore the iterator settings altered at the beginning of this
6742 function. */
6743 it->glyph_row = saved_glyph_row;
6744 return result;
6748 /* Move IT forward until it satisfies one or more of the criteria in
6749 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6751 OP is a bit-mask that specifies where to stop, and in particular,
6752 which of those four position arguments makes a difference. See the
6753 description of enum move_operation_enum.
6755 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6756 screen line, this function will set IT to the next position >
6757 TO_CHARPOS. */
6759 void
6760 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6761 struct it *it;
6762 int to_charpos, to_x, to_y, to_vpos;
6763 int op;
6765 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6766 int line_height;
6767 int reached = 0;
6769 for (;;)
6771 if (op & MOVE_TO_VPOS)
6773 /* If no TO_CHARPOS and no TO_X specified, stop at the
6774 start of the line TO_VPOS. */
6775 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6777 if (it->vpos == to_vpos)
6779 reached = 1;
6780 break;
6782 else
6783 skip = move_it_in_display_line_to (it, -1, -1, 0);
6785 else
6787 /* TO_VPOS >= 0 means stop at TO_X in the line at
6788 TO_VPOS, or at TO_POS, whichever comes first. */
6789 if (it->vpos == to_vpos)
6791 reached = 2;
6792 break;
6795 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6797 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6799 reached = 3;
6800 break;
6802 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6804 /* We have reached TO_X but not in the line we want. */
6805 skip = move_it_in_display_line_to (it, to_charpos,
6806 -1, MOVE_TO_POS);
6807 if (skip == MOVE_POS_MATCH_OR_ZV)
6809 reached = 4;
6810 break;
6815 else if (op & MOVE_TO_Y)
6817 struct it it_backup;
6819 /* TO_Y specified means stop at TO_X in the line containing
6820 TO_Y---or at TO_CHARPOS if this is reached first. The
6821 problem is that we can't really tell whether the line
6822 contains TO_Y before we have completely scanned it, and
6823 this may skip past TO_X. What we do is to first scan to
6824 TO_X.
6826 If TO_X is not specified, use a TO_X of zero. The reason
6827 is to make the outcome of this function more predictable.
6828 If we didn't use TO_X == 0, we would stop at the end of
6829 the line which is probably not what a caller would expect
6830 to happen. */
6831 skip = move_it_in_display_line_to (it, to_charpos,
6832 ((op & MOVE_TO_X)
6833 ? to_x : 0),
6834 (MOVE_TO_X
6835 | (op & MOVE_TO_POS)));
6837 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6838 if (skip == MOVE_POS_MATCH_OR_ZV)
6840 reached = 5;
6841 break;
6844 /* If TO_X was reached, we would like to know whether TO_Y
6845 is in the line. This can only be said if we know the
6846 total line height which requires us to scan the rest of
6847 the line. */
6848 if (skip == MOVE_X_REACHED)
6850 it_backup = *it;
6851 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6852 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6853 op & MOVE_TO_POS);
6854 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6857 /* Now, decide whether TO_Y is in this line. */
6858 line_height = it->max_ascent + it->max_descent;
6859 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6861 if (to_y >= it->current_y
6862 && to_y < it->current_y + line_height)
6864 if (skip == MOVE_X_REACHED)
6865 /* If TO_Y is in this line and TO_X was reached above,
6866 we scanned too far. We have to restore IT's settings
6867 to the ones before skipping. */
6868 *it = it_backup;
6869 reached = 6;
6871 else if (skip == MOVE_X_REACHED)
6873 skip = skip2;
6874 if (skip == MOVE_POS_MATCH_OR_ZV)
6875 reached = 7;
6878 if (reached)
6879 break;
6881 else if (BUFFERP (it->object)
6882 && it->method == GET_FROM_BUFFER
6883 && IT_CHARPOS (*it) >= to_charpos)
6884 skip = MOVE_POS_MATCH_OR_ZV;
6885 else
6886 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6888 switch (skip)
6890 case MOVE_POS_MATCH_OR_ZV:
6891 reached = 8;
6892 goto out;
6894 case MOVE_NEWLINE_OR_CR:
6895 set_iterator_to_next (it, 1);
6896 it->continuation_lines_width = 0;
6897 break;
6899 case MOVE_LINE_TRUNCATED:
6900 it->continuation_lines_width = 0;
6901 reseat_at_next_visible_line_start (it, 0);
6902 if ((op & MOVE_TO_POS) != 0
6903 && IT_CHARPOS (*it) > to_charpos)
6905 reached = 9;
6906 goto out;
6908 break;
6910 case MOVE_LINE_CONTINUED:
6911 /* For continued lines ending in a tab, some of the glyphs
6912 associated with the tab are displayed on the current
6913 line. Since it->current_x does not include these glyphs,
6914 we use it->last_visible_x instead. */
6915 it->continuation_lines_width +=
6916 (it->c == '\t') ? it->last_visible_x : it->current_x;
6917 break;
6919 default:
6920 abort ();
6923 /* Reset/increment for the next run. */
6924 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6925 it->current_x = it->hpos = 0;
6926 it->current_y += it->max_ascent + it->max_descent;
6927 ++it->vpos;
6928 last_height = it->max_ascent + it->max_descent;
6929 last_max_ascent = it->max_ascent;
6930 it->max_ascent = it->max_descent = 0;
6933 out:
6935 /* On text terminals, we may stop at the end of a line in the middle
6936 of a multi-character glyph. If the glyph itself is continued,
6937 i.e. it is actually displayed on the next line, don't treat this
6938 stopping point as valid; move to the next line instead (unless
6939 that brings us offscreen). */
6940 if (!FRAME_WINDOW_P (it->f)
6941 && op & MOVE_TO_POS
6942 && IT_CHARPOS (*it) == to_charpos
6943 && it->what == IT_CHARACTER
6944 && it->nglyphs > 1
6945 && !it->truncate_lines_p
6946 && it->current_x == it->last_visible_x - 1
6947 && it->c != '\n'
6948 && it->c != '\t'
6949 && it->vpos < XFASTINT (it->w->window_end_vpos))
6951 it->continuation_lines_width += it->current_x;
6952 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
6953 it->current_y += it->max_ascent + it->max_descent;
6954 ++it->vpos;
6955 last_height = it->max_ascent + it->max_descent;
6956 last_max_ascent = it->max_ascent;
6959 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6963 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6965 If DY > 0, move IT backward at least that many pixels. DY = 0
6966 means move IT backward to the preceding line start or BEGV. This
6967 function may move over more than DY pixels if IT->current_y - DY
6968 ends up in the middle of a line; in this case IT->current_y will be
6969 set to the top of the line moved to. */
6971 void
6972 move_it_vertically_backward (it, dy)
6973 struct it *it;
6974 int dy;
6976 int nlines, h;
6977 struct it it2, it3;
6978 int start_pos;
6980 move_further_back:
6981 xassert (dy >= 0);
6983 start_pos = IT_CHARPOS (*it);
6985 /* Estimate how many newlines we must move back. */
6986 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6988 /* Set the iterator's position that many lines back. */
6989 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6990 back_to_previous_visible_line_start (it);
6992 /* Reseat the iterator here. When moving backward, we don't want
6993 reseat to skip forward over invisible text, set up the iterator
6994 to deliver from overlay strings at the new position etc. So,
6995 use reseat_1 here. */
6996 reseat_1 (it, it->current.pos, 1);
6998 /* We are now surely at a line start. */
6999 it->current_x = it->hpos = 0;
7000 it->continuation_lines_width = 0;
7002 /* Move forward and see what y-distance we moved. First move to the
7003 start of the next line so that we get its height. We need this
7004 height to be able to tell whether we reached the specified
7005 y-distance. */
7006 it2 = *it;
7007 it2.max_ascent = it2.max_descent = 0;
7010 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7011 MOVE_TO_POS | MOVE_TO_VPOS);
7013 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7014 xassert (IT_CHARPOS (*it) >= BEGV);
7015 it3 = it2;
7017 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7018 xassert (IT_CHARPOS (*it) >= BEGV);
7019 /* H is the actual vertical distance from the position in *IT
7020 and the starting position. */
7021 h = it2.current_y - it->current_y;
7022 /* NLINES is the distance in number of lines. */
7023 nlines = it2.vpos - it->vpos;
7025 /* Correct IT's y and vpos position
7026 so that they are relative to the starting point. */
7027 it->vpos -= nlines;
7028 it->current_y -= h;
7030 if (dy == 0)
7032 /* DY == 0 means move to the start of the screen line. The
7033 value of nlines is > 0 if continuation lines were involved. */
7034 if (nlines > 0)
7035 move_it_by_lines (it, nlines, 1);
7036 #if 0
7037 /* I think this assert is bogus if buffer contains
7038 invisible text or images. KFS. */
7039 xassert (IT_CHARPOS (*it) <= start_pos);
7040 #endif
7042 else
7044 /* The y-position we try to reach, relative to *IT.
7045 Note that H has been subtracted in front of the if-statement. */
7046 int target_y = it->current_y + h - dy;
7047 int y0 = it3.current_y;
7048 int y1 = line_bottom_y (&it3);
7049 int line_height = y1 - y0;
7051 /* If we did not reach target_y, try to move further backward if
7052 we can. If we moved too far backward, try to move forward. */
7053 if (target_y < it->current_y
7054 /* This is heuristic. In a window that's 3 lines high, with
7055 a line height of 13 pixels each, recentering with point
7056 on the bottom line will try to move -39/2 = 19 pixels
7057 backward. Try to avoid moving into the first line. */
7058 && (it->current_y - target_y
7059 > min (window_box_height (it->w), line_height * 2 / 3))
7060 && IT_CHARPOS (*it) > BEGV)
7062 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7063 target_y - it->current_y));
7064 dy = it->current_y - target_y;
7065 goto move_further_back;
7067 else if (target_y >= it->current_y + line_height
7068 && IT_CHARPOS (*it) < ZV)
7070 /* Should move forward by at least one line, maybe more.
7072 Note: Calling move_it_by_lines can be expensive on
7073 terminal frames, where compute_motion is used (via
7074 vmotion) to do the job, when there are very long lines
7075 and truncate-lines is nil. That's the reason for
7076 treating terminal frames specially here. */
7078 if (!FRAME_WINDOW_P (it->f))
7079 move_it_vertically (it, target_y - (it->current_y + line_height));
7080 else
7084 move_it_by_lines (it, 1, 1);
7086 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7089 #if 0
7090 /* I think this assert is bogus if buffer contains
7091 invisible text or images. KFS. */
7092 xassert (IT_CHARPOS (*it) >= BEGV);
7093 #endif
7099 /* Move IT by a specified amount of pixel lines DY. DY negative means
7100 move backwards. DY = 0 means move to start of screen line. At the
7101 end, IT will be on the start of a screen line. */
7103 void
7104 move_it_vertically (it, dy)
7105 struct it *it;
7106 int dy;
7108 if (dy <= 0)
7109 move_it_vertically_backward (it, -dy);
7110 else
7112 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7113 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7114 MOVE_TO_POS | MOVE_TO_Y);
7115 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7117 /* If buffer ends in ZV without a newline, move to the start of
7118 the line to satisfy the post-condition. */
7119 if (IT_CHARPOS (*it) == ZV
7120 && ZV > BEGV
7121 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7122 move_it_by_lines (it, 0, 0);
7127 /* Move iterator IT past the end of the text line it is in. */
7129 void
7130 move_it_past_eol (it)
7131 struct it *it;
7133 enum move_it_result rc;
7135 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7136 if (rc == MOVE_NEWLINE_OR_CR)
7137 set_iterator_to_next (it, 0);
7141 #if 0 /* Currently not used. */
7143 /* Return non-zero if some text between buffer positions START_CHARPOS
7144 and END_CHARPOS is invisible. IT->window is the window for text
7145 property lookup. */
7147 static int
7148 invisible_text_between_p (it, start_charpos, end_charpos)
7149 struct it *it;
7150 int start_charpos, end_charpos;
7152 Lisp_Object prop, limit;
7153 int invisible_found_p;
7155 xassert (it != NULL && start_charpos <= end_charpos);
7157 /* Is text at START invisible? */
7158 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7159 it->window);
7160 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7161 invisible_found_p = 1;
7162 else
7164 limit = Fnext_single_char_property_change (make_number (start_charpos),
7165 Qinvisible, Qnil,
7166 make_number (end_charpos));
7167 invisible_found_p = XFASTINT (limit) < end_charpos;
7170 return invisible_found_p;
7173 #endif /* 0 */
7176 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7177 negative means move up. DVPOS == 0 means move to the start of the
7178 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7179 NEED_Y_P is zero, IT->current_y will be left unchanged.
7181 Further optimization ideas: If we would know that IT->f doesn't use
7182 a face with proportional font, we could be faster for
7183 truncate-lines nil. */
7185 void
7186 move_it_by_lines (it, dvpos, need_y_p)
7187 struct it *it;
7188 int dvpos, need_y_p;
7190 struct position pos;
7192 /* The commented-out optimization uses vmotion on terminals. This
7193 gives bad results, because elements like it->what, on which
7194 callers such as pos_visible_p rely, aren't updated. */
7195 /* if (!FRAME_WINDOW_P (it->f))
7197 struct text_pos textpos;
7199 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7200 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7201 reseat (it, textpos, 1);
7202 it->vpos += pos.vpos;
7203 it->current_y += pos.vpos;
7205 else */
7207 if (dvpos == 0)
7209 /* DVPOS == 0 means move to the start of the screen line. */
7210 move_it_vertically_backward (it, 0);
7211 xassert (it->current_x == 0 && it->hpos == 0);
7212 /* Let next call to line_bottom_y calculate real line height */
7213 last_height = 0;
7215 else if (dvpos > 0)
7217 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7218 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7219 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7221 else
7223 struct it it2;
7224 int start_charpos, i;
7226 /* Start at the beginning of the screen line containing IT's
7227 position. This may actually move vertically backwards,
7228 in case of overlays, so adjust dvpos accordingly. */
7229 dvpos += it->vpos;
7230 move_it_vertically_backward (it, 0);
7231 dvpos -= it->vpos;
7233 /* Go back -DVPOS visible lines and reseat the iterator there. */
7234 start_charpos = IT_CHARPOS (*it);
7235 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7236 back_to_previous_visible_line_start (it);
7237 reseat (it, it->current.pos, 1);
7239 /* Move further back if we end up in a string or an image. */
7240 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7242 /* First try to move to start of display line. */
7243 dvpos += it->vpos;
7244 move_it_vertically_backward (it, 0);
7245 dvpos -= it->vpos;
7246 if (IT_POS_VALID_AFTER_MOVE_P (it))
7247 break;
7248 /* If start of line is still in string or image,
7249 move further back. */
7250 back_to_previous_visible_line_start (it);
7251 reseat (it, it->current.pos, 1);
7252 dvpos--;
7255 it->current_x = it->hpos = 0;
7257 /* Above call may have moved too far if continuation lines
7258 are involved. Scan forward and see if it did. */
7259 it2 = *it;
7260 it2.vpos = it2.current_y = 0;
7261 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7262 it->vpos -= it2.vpos;
7263 it->current_y -= it2.current_y;
7264 it->current_x = it->hpos = 0;
7266 /* If we moved too far back, move IT some lines forward. */
7267 if (it2.vpos > -dvpos)
7269 int delta = it2.vpos + dvpos;
7270 it2 = *it;
7271 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7272 /* Move back again if we got too far ahead. */
7273 if (IT_CHARPOS (*it) >= start_charpos)
7274 *it = it2;
7279 /* Return 1 if IT points into the middle of a display vector. */
7282 in_display_vector_p (it)
7283 struct it *it;
7285 return (it->method == GET_FROM_DISPLAY_VECTOR
7286 && it->current.dpvec_index > 0
7287 && it->dpvec + it->current.dpvec_index != it->dpend);
7291 /***********************************************************************
7292 Messages
7293 ***********************************************************************/
7296 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7297 to *Messages*. */
7299 void
7300 add_to_log (format, arg1, arg2)
7301 char *format;
7302 Lisp_Object arg1, arg2;
7304 Lisp_Object args[3];
7305 Lisp_Object msg, fmt;
7306 char *buffer;
7307 int len;
7308 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7309 USE_SAFE_ALLOCA;
7311 /* Do nothing if called asynchronously. Inserting text into
7312 a buffer may call after-change-functions and alike and
7313 that would means running Lisp asynchronously. */
7314 if (handling_signal)
7315 return;
7317 fmt = msg = Qnil;
7318 GCPRO4 (fmt, msg, arg1, arg2);
7320 args[0] = fmt = build_string (format);
7321 args[1] = arg1;
7322 args[2] = arg2;
7323 msg = Fformat (3, args);
7325 len = SBYTES (msg) + 1;
7326 SAFE_ALLOCA (buffer, char *, len);
7327 bcopy (SDATA (msg), buffer, len);
7329 message_dolog (buffer, len - 1, 1, 0);
7330 SAFE_FREE ();
7332 UNGCPRO;
7336 /* Output a newline in the *Messages* buffer if "needs" one. */
7338 void
7339 message_log_maybe_newline ()
7341 if (message_log_need_newline)
7342 message_dolog ("", 0, 1, 0);
7346 /* Add a string M of length NBYTES to the message log, optionally
7347 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7348 nonzero, means interpret the contents of M as multibyte. This
7349 function calls low-level routines in order to bypass text property
7350 hooks, etc. which might not be safe to run.
7352 This may GC (insert may run before/after change hooks),
7353 so the buffer M must NOT point to a Lisp string. */
7355 void
7356 message_dolog (m, nbytes, nlflag, multibyte)
7357 const char *m;
7358 int nbytes, nlflag, multibyte;
7360 if (!NILP (Vmemory_full))
7361 return;
7363 if (!NILP (Vmessage_log_max))
7365 struct buffer *oldbuf;
7366 Lisp_Object oldpoint, oldbegv, oldzv;
7367 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7368 int point_at_end = 0;
7369 int zv_at_end = 0;
7370 Lisp_Object old_deactivate_mark, tem;
7371 struct gcpro gcpro1;
7373 old_deactivate_mark = Vdeactivate_mark;
7374 oldbuf = current_buffer;
7375 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7376 current_buffer->undo_list = Qt;
7378 oldpoint = message_dolog_marker1;
7379 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7380 oldbegv = message_dolog_marker2;
7381 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7382 oldzv = message_dolog_marker3;
7383 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7384 GCPRO1 (old_deactivate_mark);
7386 if (PT == Z)
7387 point_at_end = 1;
7388 if (ZV == Z)
7389 zv_at_end = 1;
7391 BEGV = BEG;
7392 BEGV_BYTE = BEG_BYTE;
7393 ZV = Z;
7394 ZV_BYTE = Z_BYTE;
7395 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7397 /* Insert the string--maybe converting multibyte to single byte
7398 or vice versa, so that all the text fits the buffer. */
7399 if (multibyte
7400 && NILP (current_buffer->enable_multibyte_characters))
7402 int i, c, char_bytes;
7403 unsigned char work[1];
7405 /* Convert a multibyte string to single-byte
7406 for the *Message* buffer. */
7407 for (i = 0; i < nbytes; i += char_bytes)
7409 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7410 work[0] = (SINGLE_BYTE_CHAR_P (c)
7412 : multibyte_char_to_unibyte (c, Qnil));
7413 insert_1_both (work, 1, 1, 1, 0, 0);
7416 else if (! multibyte
7417 && ! NILP (current_buffer->enable_multibyte_characters))
7419 int i, c, char_bytes;
7420 unsigned char *msg = (unsigned char *) m;
7421 unsigned char str[MAX_MULTIBYTE_LENGTH];
7422 /* Convert a single-byte string to multibyte
7423 for the *Message* buffer. */
7424 for (i = 0; i < nbytes; i++)
7426 c = unibyte_char_to_multibyte (msg[i]);
7427 char_bytes = CHAR_STRING (c, str);
7428 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7431 else if (nbytes)
7432 insert_1 (m, nbytes, 1, 0, 0);
7434 if (nlflag)
7436 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7437 insert_1 ("\n", 1, 1, 0, 0);
7439 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7440 this_bol = PT;
7441 this_bol_byte = PT_BYTE;
7443 /* See if this line duplicates the previous one.
7444 If so, combine duplicates. */
7445 if (this_bol > BEG)
7447 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7448 prev_bol = PT;
7449 prev_bol_byte = PT_BYTE;
7451 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7452 this_bol, this_bol_byte);
7453 if (dup)
7455 del_range_both (prev_bol, prev_bol_byte,
7456 this_bol, this_bol_byte, 0);
7457 if (dup > 1)
7459 char dupstr[40];
7460 int duplen;
7462 /* If you change this format, don't forget to also
7463 change message_log_check_duplicate. */
7464 sprintf (dupstr, " [%d times]", dup);
7465 duplen = strlen (dupstr);
7466 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7467 insert_1 (dupstr, duplen, 1, 0, 1);
7472 /* If we have more than the desired maximum number of lines
7473 in the *Messages* buffer now, delete the oldest ones.
7474 This is safe because we don't have undo in this buffer. */
7476 if (NATNUMP (Vmessage_log_max))
7478 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7479 -XFASTINT (Vmessage_log_max) - 1, 0);
7480 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7483 BEGV = XMARKER (oldbegv)->charpos;
7484 BEGV_BYTE = marker_byte_position (oldbegv);
7486 if (zv_at_end)
7488 ZV = Z;
7489 ZV_BYTE = Z_BYTE;
7491 else
7493 ZV = XMARKER (oldzv)->charpos;
7494 ZV_BYTE = marker_byte_position (oldzv);
7497 if (point_at_end)
7498 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7499 else
7500 /* We can't do Fgoto_char (oldpoint) because it will run some
7501 Lisp code. */
7502 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7503 XMARKER (oldpoint)->bytepos);
7505 UNGCPRO;
7506 unchain_marker (XMARKER (oldpoint));
7507 unchain_marker (XMARKER (oldbegv));
7508 unchain_marker (XMARKER (oldzv));
7510 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7511 set_buffer_internal (oldbuf);
7512 if (NILP (tem))
7513 windows_or_buffers_changed = old_windows_or_buffers_changed;
7514 message_log_need_newline = !nlflag;
7515 Vdeactivate_mark = old_deactivate_mark;
7520 /* We are at the end of the buffer after just having inserted a newline.
7521 (Note: We depend on the fact we won't be crossing the gap.)
7522 Check to see if the most recent message looks a lot like the previous one.
7523 Return 0 if different, 1 if the new one should just replace it, or a
7524 value N > 1 if we should also append " [N times]". */
7526 static int
7527 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7528 int prev_bol, this_bol;
7529 int prev_bol_byte, this_bol_byte;
7531 int i;
7532 int len = Z_BYTE - 1 - this_bol_byte;
7533 int seen_dots = 0;
7534 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7535 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7537 for (i = 0; i < len; i++)
7539 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7540 seen_dots = 1;
7541 if (p1[i] != p2[i])
7542 return seen_dots;
7544 p1 += len;
7545 if (*p1 == '\n')
7546 return 2;
7547 if (*p1++ == ' ' && *p1++ == '[')
7549 int n = 0;
7550 while (*p1 >= '0' && *p1 <= '9')
7551 n = n * 10 + *p1++ - '0';
7552 if (strncmp (p1, " times]\n", 8) == 0)
7553 return n+1;
7555 return 0;
7559 /* Display an echo area message M with a specified length of NBYTES
7560 bytes. The string may include null characters. If M is 0, clear
7561 out any existing message, and let the mini-buffer text show
7562 through.
7564 This may GC, so the buffer M must NOT point to a Lisp string. */
7566 void
7567 message2 (m, nbytes, multibyte)
7568 const char *m;
7569 int nbytes;
7570 int multibyte;
7572 /* First flush out any partial line written with print. */
7573 message_log_maybe_newline ();
7574 if (m)
7575 message_dolog (m, nbytes, 1, multibyte);
7576 message2_nolog (m, nbytes, multibyte);
7580 /* The non-logging counterpart of message2. */
7582 void
7583 message2_nolog (m, nbytes, multibyte)
7584 const char *m;
7585 int nbytes, multibyte;
7587 struct frame *sf = SELECTED_FRAME ();
7588 message_enable_multibyte = multibyte;
7590 if (noninteractive)
7592 if (noninteractive_need_newline)
7593 putc ('\n', stderr);
7594 noninteractive_need_newline = 0;
7595 if (m)
7596 fwrite (m, nbytes, 1, stderr);
7597 if (cursor_in_echo_area == 0)
7598 fprintf (stderr, "\n");
7599 fflush (stderr);
7601 /* A null message buffer means that the frame hasn't really been
7602 initialized yet. Error messages get reported properly by
7603 cmd_error, so this must be just an informative message; toss it. */
7604 else if (INTERACTIVE
7605 && sf->glyphs_initialized_p
7606 && FRAME_MESSAGE_BUF (sf))
7608 Lisp_Object mini_window;
7609 struct frame *f;
7611 /* Get the frame containing the mini-buffer
7612 that the selected frame is using. */
7613 mini_window = FRAME_MINIBUF_WINDOW (sf);
7614 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7616 FRAME_SAMPLE_VISIBILITY (f);
7617 if (FRAME_VISIBLE_P (sf)
7618 && ! FRAME_VISIBLE_P (f))
7619 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7621 if (m)
7623 set_message (m, Qnil, nbytes, multibyte);
7624 if (minibuffer_auto_raise)
7625 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7627 else
7628 clear_message (1, 1);
7630 do_pending_window_change (0);
7631 echo_area_display (1);
7632 do_pending_window_change (0);
7633 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7634 (*frame_up_to_date_hook) (f);
7639 /* Display an echo area message M with a specified length of NBYTES
7640 bytes. The string may include null characters. If M is not a
7641 string, clear out any existing message, and let the mini-buffer
7642 text show through.
7644 This function cancels echoing. */
7646 void
7647 message3 (m, nbytes, multibyte)
7648 Lisp_Object m;
7649 int nbytes;
7650 int multibyte;
7652 struct gcpro gcpro1;
7654 GCPRO1 (m);
7655 clear_message (1,1);
7656 cancel_echoing ();
7658 /* First flush out any partial line written with print. */
7659 message_log_maybe_newline ();
7660 if (STRINGP (m))
7662 char *buffer;
7663 USE_SAFE_ALLOCA;
7665 SAFE_ALLOCA (buffer, char *, nbytes);
7666 bcopy (SDATA (m), buffer, nbytes);
7667 message_dolog (buffer, nbytes, 1, multibyte);
7668 SAFE_FREE ();
7670 message3_nolog (m, nbytes, multibyte);
7672 UNGCPRO;
7676 /* The non-logging version of message3.
7677 This does not cancel echoing, because it is used for echoing.
7678 Perhaps we need to make a separate function for echoing
7679 and make this cancel echoing. */
7681 void
7682 message3_nolog (m, nbytes, multibyte)
7683 Lisp_Object m;
7684 int nbytes, multibyte;
7686 struct frame *sf = SELECTED_FRAME ();
7687 message_enable_multibyte = multibyte;
7689 if (noninteractive)
7691 if (noninteractive_need_newline)
7692 putc ('\n', stderr);
7693 noninteractive_need_newline = 0;
7694 if (STRINGP (m))
7695 fwrite (SDATA (m), nbytes, 1, stderr);
7696 if (cursor_in_echo_area == 0)
7697 fprintf (stderr, "\n");
7698 fflush (stderr);
7700 /* A null message buffer means that the frame hasn't really been
7701 initialized yet. Error messages get reported properly by
7702 cmd_error, so this must be just an informative message; toss it. */
7703 else if (INTERACTIVE
7704 && sf->glyphs_initialized_p
7705 && FRAME_MESSAGE_BUF (sf))
7707 Lisp_Object mini_window;
7708 Lisp_Object frame;
7709 struct frame *f;
7711 /* Get the frame containing the mini-buffer
7712 that the selected frame is using. */
7713 mini_window = FRAME_MINIBUF_WINDOW (sf);
7714 frame = XWINDOW (mini_window)->frame;
7715 f = XFRAME (frame);
7717 FRAME_SAMPLE_VISIBILITY (f);
7718 if (FRAME_VISIBLE_P (sf)
7719 && !FRAME_VISIBLE_P (f))
7720 Fmake_frame_visible (frame);
7722 if (STRINGP (m) && SCHARS (m) > 0)
7724 set_message (NULL, m, nbytes, multibyte);
7725 if (minibuffer_auto_raise)
7726 Fraise_frame (frame);
7727 /* Assume we are not echoing.
7728 (If we are, echo_now will override this.) */
7729 echo_message_buffer = Qnil;
7731 else
7732 clear_message (1, 1);
7734 do_pending_window_change (0);
7735 echo_area_display (1);
7736 do_pending_window_change (0);
7737 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7738 (*frame_up_to_date_hook) (f);
7743 /* Display a null-terminated echo area message M. If M is 0, clear
7744 out any existing message, and let the mini-buffer text show through.
7746 The buffer M must continue to exist until after the echo area gets
7747 cleared or some other message gets displayed there. Do not pass
7748 text that is stored in a Lisp string. Do not pass text in a buffer
7749 that was alloca'd. */
7751 void
7752 message1 (m)
7753 char *m;
7755 message2 (m, (m ? strlen (m) : 0), 0);
7759 /* The non-logging counterpart of message1. */
7761 void
7762 message1_nolog (m)
7763 char *m;
7765 message2_nolog (m, (m ? strlen (m) : 0), 0);
7768 /* Display a message M which contains a single %s
7769 which gets replaced with STRING. */
7771 void
7772 message_with_string (m, string, log)
7773 char *m;
7774 Lisp_Object string;
7775 int log;
7777 CHECK_STRING (string);
7779 if (noninteractive)
7781 if (m)
7783 if (noninteractive_need_newline)
7784 putc ('\n', stderr);
7785 noninteractive_need_newline = 0;
7786 fprintf (stderr, m, SDATA (string));
7787 if (cursor_in_echo_area == 0)
7788 fprintf (stderr, "\n");
7789 fflush (stderr);
7792 else if (INTERACTIVE)
7794 /* The frame whose minibuffer we're going to display the message on.
7795 It may be larger than the selected frame, so we need
7796 to use its buffer, not the selected frame's buffer. */
7797 Lisp_Object mini_window;
7798 struct frame *f, *sf = SELECTED_FRAME ();
7800 /* Get the frame containing the minibuffer
7801 that the selected frame is using. */
7802 mini_window = FRAME_MINIBUF_WINDOW (sf);
7803 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7805 /* A null message buffer means that the frame hasn't really been
7806 initialized yet. Error messages get reported properly by
7807 cmd_error, so this must be just an informative message; toss it. */
7808 if (FRAME_MESSAGE_BUF (f))
7810 Lisp_Object args[2], message;
7811 struct gcpro gcpro1, gcpro2;
7813 args[0] = build_string (m);
7814 args[1] = message = string;
7815 GCPRO2 (args[0], message);
7816 gcpro1.nvars = 2;
7818 message = Fformat (2, args);
7820 if (log)
7821 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7822 else
7823 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7825 UNGCPRO;
7827 /* Print should start at the beginning of the message
7828 buffer next time. */
7829 message_buf_print = 0;
7835 /* Dump an informative message to the minibuf. If M is 0, clear out
7836 any existing message, and let the mini-buffer text show through. */
7838 /* VARARGS 1 */
7839 void
7840 message (m, a1, a2, a3)
7841 char *m;
7842 EMACS_INT a1, a2, a3;
7844 if (noninteractive)
7846 if (m)
7848 if (noninteractive_need_newline)
7849 putc ('\n', stderr);
7850 noninteractive_need_newline = 0;
7851 fprintf (stderr, m, a1, a2, a3);
7852 if (cursor_in_echo_area == 0)
7853 fprintf (stderr, "\n");
7854 fflush (stderr);
7857 else if (INTERACTIVE)
7859 /* The frame whose mini-buffer we're going to display the message
7860 on. It may be larger than the selected frame, so we need to
7861 use its buffer, not the selected frame's buffer. */
7862 Lisp_Object mini_window;
7863 struct frame *f, *sf = SELECTED_FRAME ();
7865 /* Get the frame containing the mini-buffer
7866 that the selected frame is using. */
7867 mini_window = FRAME_MINIBUF_WINDOW (sf);
7868 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7870 /* A null message buffer means that the frame hasn't really been
7871 initialized yet. Error messages get reported properly by
7872 cmd_error, so this must be just an informative message; toss
7873 it. */
7874 if (FRAME_MESSAGE_BUF (f))
7876 if (m)
7878 int len;
7879 #ifdef NO_ARG_ARRAY
7880 char *a[3];
7881 a[0] = (char *) a1;
7882 a[1] = (char *) a2;
7883 a[2] = (char *) a3;
7885 len = doprnt (FRAME_MESSAGE_BUF (f),
7886 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7887 #else
7888 len = doprnt (FRAME_MESSAGE_BUF (f),
7889 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7890 (char **) &a1);
7891 #endif /* NO_ARG_ARRAY */
7893 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7895 else
7896 message1 (0);
7898 /* Print should start at the beginning of the message
7899 buffer next time. */
7900 message_buf_print = 0;
7906 /* The non-logging version of message. */
7908 void
7909 message_nolog (m, a1, a2, a3)
7910 char *m;
7911 EMACS_INT a1, a2, a3;
7913 Lisp_Object old_log_max;
7914 old_log_max = Vmessage_log_max;
7915 Vmessage_log_max = Qnil;
7916 message (m, a1, a2, a3);
7917 Vmessage_log_max = old_log_max;
7921 /* Display the current message in the current mini-buffer. This is
7922 only called from error handlers in process.c, and is not time
7923 critical. */
7925 void
7926 update_echo_area ()
7928 if (!NILP (echo_area_buffer[0]))
7930 Lisp_Object string;
7931 string = Fcurrent_message ();
7932 message3 (string, SBYTES (string),
7933 !NILP (current_buffer->enable_multibyte_characters));
7938 /* Make sure echo area buffers in `echo_buffers' are live.
7939 If they aren't, make new ones. */
7941 static void
7942 ensure_echo_area_buffers ()
7944 int i;
7946 for (i = 0; i < 2; ++i)
7947 if (!BUFFERP (echo_buffer[i])
7948 || NILP (XBUFFER (echo_buffer[i])->name))
7950 char name[30];
7951 Lisp_Object old_buffer;
7952 int j;
7954 old_buffer = echo_buffer[i];
7955 sprintf (name, " *Echo Area %d*", i);
7956 echo_buffer[i] = Fget_buffer_create (build_string (name));
7957 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7959 for (j = 0; j < 2; ++j)
7960 if (EQ (old_buffer, echo_area_buffer[j]))
7961 echo_area_buffer[j] = echo_buffer[i];
7966 /* Call FN with args A1..A4 with either the current or last displayed
7967 echo_area_buffer as current buffer.
7969 WHICH zero means use the current message buffer
7970 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7971 from echo_buffer[] and clear it.
7973 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7974 suitable buffer from echo_buffer[] and clear it.
7976 Value is what FN returns. */
7978 static int
7979 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7980 struct window *w;
7981 int which;
7982 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7983 EMACS_INT a1;
7984 Lisp_Object a2;
7985 EMACS_INT a3, a4;
7987 Lisp_Object buffer;
7988 int this_one, the_other, clear_buffer_p, rc;
7989 int count = SPECPDL_INDEX ();
7991 /* If buffers aren't live, make new ones. */
7992 ensure_echo_area_buffers ();
7994 clear_buffer_p = 0;
7996 if (which == 0)
7997 this_one = 0, the_other = 1;
7998 else if (which > 0)
7999 this_one = 1, the_other = 0;
8001 /* Choose a suitable buffer from echo_buffer[] is we don't
8002 have one. */
8003 if (NILP (echo_area_buffer[this_one]))
8005 echo_area_buffer[this_one]
8006 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8007 ? echo_buffer[the_other]
8008 : echo_buffer[this_one]);
8009 clear_buffer_p = 1;
8012 buffer = echo_area_buffer[this_one];
8014 /* Don't get confused by reusing the buffer used for echoing
8015 for a different purpose. */
8016 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8017 cancel_echoing ();
8019 record_unwind_protect (unwind_with_echo_area_buffer,
8020 with_echo_area_buffer_unwind_data (w));
8022 /* Make the echo area buffer current. Note that for display
8023 purposes, it is not necessary that the displayed window's buffer
8024 == current_buffer, except for text property lookup. So, let's
8025 only set that buffer temporarily here without doing a full
8026 Fset_window_buffer. We must also change w->pointm, though,
8027 because otherwise an assertions in unshow_buffer fails, and Emacs
8028 aborts. */
8029 set_buffer_internal_1 (XBUFFER (buffer));
8030 if (w)
8032 w->buffer = buffer;
8033 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8036 current_buffer->undo_list = Qt;
8037 current_buffer->read_only = Qnil;
8038 specbind (Qinhibit_read_only, Qt);
8039 specbind (Qinhibit_modification_hooks, Qt);
8041 if (clear_buffer_p && Z > BEG)
8042 del_range (BEG, Z);
8044 xassert (BEGV >= BEG);
8045 xassert (ZV <= Z && ZV >= BEGV);
8047 rc = fn (a1, a2, a3, a4);
8049 xassert (BEGV >= BEG);
8050 xassert (ZV <= Z && ZV >= BEGV);
8052 unbind_to (count, Qnil);
8053 return rc;
8057 /* Save state that should be preserved around the call to the function
8058 FN called in with_echo_area_buffer. */
8060 static Lisp_Object
8061 with_echo_area_buffer_unwind_data (w)
8062 struct window *w;
8064 int i = 0;
8065 Lisp_Object vector;
8067 /* Reduce consing by keeping one vector in
8068 Vwith_echo_area_save_vector. */
8069 vector = Vwith_echo_area_save_vector;
8070 Vwith_echo_area_save_vector = Qnil;
8072 if (NILP (vector))
8073 vector = Fmake_vector (make_number (7), Qnil);
8075 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8076 AREF (vector, i) = Vdeactivate_mark, ++i;
8077 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8079 if (w)
8081 XSETWINDOW (AREF (vector, i), w); ++i;
8082 AREF (vector, i) = w->buffer; ++i;
8083 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8084 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8086 else
8088 int end = i + 4;
8089 for (; i < end; ++i)
8090 AREF (vector, i) = Qnil;
8093 xassert (i == ASIZE (vector));
8094 return vector;
8098 /* Restore global state from VECTOR which was created by
8099 with_echo_area_buffer_unwind_data. */
8101 static Lisp_Object
8102 unwind_with_echo_area_buffer (vector)
8103 Lisp_Object vector;
8105 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8106 Vdeactivate_mark = AREF (vector, 1);
8107 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8109 if (WINDOWP (AREF (vector, 3)))
8111 struct window *w;
8112 Lisp_Object buffer, charpos, bytepos;
8114 w = XWINDOW (AREF (vector, 3));
8115 buffer = AREF (vector, 4);
8116 charpos = AREF (vector, 5);
8117 bytepos = AREF (vector, 6);
8119 w->buffer = buffer;
8120 set_marker_both (w->pointm, buffer,
8121 XFASTINT (charpos), XFASTINT (bytepos));
8124 Vwith_echo_area_save_vector = vector;
8125 return Qnil;
8129 /* Set up the echo area for use by print functions. MULTIBYTE_P
8130 non-zero means we will print multibyte. */
8132 void
8133 setup_echo_area_for_printing (multibyte_p)
8134 int multibyte_p;
8136 /* If we can't find an echo area any more, exit. */
8137 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8138 Fkill_emacs (Qnil);
8140 ensure_echo_area_buffers ();
8142 if (!message_buf_print)
8144 /* A message has been output since the last time we printed.
8145 Choose a fresh echo area buffer. */
8146 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8147 echo_area_buffer[0] = echo_buffer[1];
8148 else
8149 echo_area_buffer[0] = echo_buffer[0];
8151 /* Switch to that buffer and clear it. */
8152 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8153 current_buffer->truncate_lines = Qnil;
8155 if (Z > BEG)
8157 int count = SPECPDL_INDEX ();
8158 specbind (Qinhibit_read_only, Qt);
8159 /* Note that undo recording is always disabled. */
8160 del_range (BEG, Z);
8161 unbind_to (count, Qnil);
8163 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8165 /* Set up the buffer for the multibyteness we need. */
8166 if (multibyte_p
8167 != !NILP (current_buffer->enable_multibyte_characters))
8168 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8170 /* Raise the frame containing the echo area. */
8171 if (minibuffer_auto_raise)
8173 struct frame *sf = SELECTED_FRAME ();
8174 Lisp_Object mini_window;
8175 mini_window = FRAME_MINIBUF_WINDOW (sf);
8176 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8179 message_log_maybe_newline ();
8180 message_buf_print = 1;
8182 else
8184 if (NILP (echo_area_buffer[0]))
8186 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8187 echo_area_buffer[0] = echo_buffer[1];
8188 else
8189 echo_area_buffer[0] = echo_buffer[0];
8192 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8194 /* Someone switched buffers between print requests. */
8195 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8196 current_buffer->truncate_lines = Qnil;
8202 /* Display an echo area message in window W. Value is non-zero if W's
8203 height is changed. If display_last_displayed_message_p is
8204 non-zero, display the message that was last displayed, otherwise
8205 display the current message. */
8207 static int
8208 display_echo_area (w)
8209 struct window *w;
8211 int i, no_message_p, window_height_changed_p, count;
8213 /* Temporarily disable garbage collections while displaying the echo
8214 area. This is done because a GC can print a message itself.
8215 That message would modify the echo area buffer's contents while a
8216 redisplay of the buffer is going on, and seriously confuse
8217 redisplay. */
8218 count = inhibit_garbage_collection ();
8220 /* If there is no message, we must call display_echo_area_1
8221 nevertheless because it resizes the window. But we will have to
8222 reset the echo_area_buffer in question to nil at the end because
8223 with_echo_area_buffer will sets it to an empty buffer. */
8224 i = display_last_displayed_message_p ? 1 : 0;
8225 no_message_p = NILP (echo_area_buffer[i]);
8227 window_height_changed_p
8228 = with_echo_area_buffer (w, display_last_displayed_message_p,
8229 display_echo_area_1,
8230 (EMACS_INT) w, Qnil, 0, 0);
8232 if (no_message_p)
8233 echo_area_buffer[i] = Qnil;
8235 unbind_to (count, Qnil);
8236 return window_height_changed_p;
8240 /* Helper for display_echo_area. Display the current buffer which
8241 contains the current echo area message in window W, a mini-window,
8242 a pointer to which is passed in A1. A2..A4 are currently not used.
8243 Change the height of W so that all of the message is displayed.
8244 Value is non-zero if height of W was changed. */
8246 static int
8247 display_echo_area_1 (a1, a2, a3, a4)
8248 EMACS_INT a1;
8249 Lisp_Object a2;
8250 EMACS_INT a3, a4;
8252 struct window *w = (struct window *) a1;
8253 Lisp_Object window;
8254 struct text_pos start;
8255 int window_height_changed_p = 0;
8257 /* Do this before displaying, so that we have a large enough glyph
8258 matrix for the display. If we can't get enough space for the
8259 whole text, display the last N lines. That works by setting w->start. */
8260 window_height_changed_p = resize_mini_window (w, 0);
8262 /* Use the starting position chosen by resize_mini_window. */
8263 SET_TEXT_POS_FROM_MARKER (start, w->start);
8265 /* Display. */
8266 clear_glyph_matrix (w->desired_matrix);
8267 XSETWINDOW (window, w);
8268 try_window (window, start, 0);
8270 return window_height_changed_p;
8274 /* Resize the echo area window to exactly the size needed for the
8275 currently displayed message, if there is one. If a mini-buffer
8276 is active, don't shrink it. */
8278 void
8279 resize_echo_area_exactly ()
8281 if (BUFFERP (echo_area_buffer[0])
8282 && WINDOWP (echo_area_window))
8284 struct window *w = XWINDOW (echo_area_window);
8285 int resized_p;
8286 Lisp_Object resize_exactly;
8288 if (minibuf_level == 0)
8289 resize_exactly = Qt;
8290 else
8291 resize_exactly = Qnil;
8293 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8294 (EMACS_INT) w, resize_exactly, 0, 0);
8295 if (resized_p)
8297 ++windows_or_buffers_changed;
8298 ++update_mode_lines;
8299 redisplay_internal (0);
8305 /* Callback function for with_echo_area_buffer, when used from
8306 resize_echo_area_exactly. A1 contains a pointer to the window to
8307 resize, EXACTLY non-nil means resize the mini-window exactly to the
8308 size of the text displayed. A3 and A4 are not used. Value is what
8309 resize_mini_window returns. */
8311 static int
8312 resize_mini_window_1 (a1, exactly, a3, a4)
8313 EMACS_INT a1;
8314 Lisp_Object exactly;
8315 EMACS_INT a3, a4;
8317 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8321 /* Resize mini-window W to fit the size of its contents. EXACT_P
8322 means size the window exactly to the size needed. Otherwise, it's
8323 only enlarged until W's buffer is empty.
8325 Set W->start to the right place to begin display. If the whole
8326 contents fit, start at the beginning. Otherwise, start so as
8327 to make the end of the contents appear. This is particularly
8328 important for y-or-n-p, but seems desirable generally.
8330 Value is non-zero if the window height has been changed. */
8333 resize_mini_window (w, exact_p)
8334 struct window *w;
8335 int exact_p;
8337 struct frame *f = XFRAME (w->frame);
8338 int window_height_changed_p = 0;
8340 xassert (MINI_WINDOW_P (w));
8342 /* By default, start display at the beginning. */
8343 set_marker_both (w->start, w->buffer,
8344 BUF_BEGV (XBUFFER (w->buffer)),
8345 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8347 /* Don't resize windows while redisplaying a window; it would
8348 confuse redisplay functions when the size of the window they are
8349 displaying changes from under them. Such a resizing can happen,
8350 for instance, when which-func prints a long message while
8351 we are running fontification-functions. We're running these
8352 functions with safe_call which binds inhibit-redisplay to t. */
8353 if (!NILP (Vinhibit_redisplay))
8354 return 0;
8356 /* Nil means don't try to resize. */
8357 if (NILP (Vresize_mini_windows)
8358 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8359 return 0;
8361 if (!FRAME_MINIBUF_ONLY_P (f))
8363 struct it it;
8364 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8365 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8366 int height, max_height;
8367 int unit = FRAME_LINE_HEIGHT (f);
8368 struct text_pos start;
8369 struct buffer *old_current_buffer = NULL;
8371 if (current_buffer != XBUFFER (w->buffer))
8373 old_current_buffer = current_buffer;
8374 set_buffer_internal (XBUFFER (w->buffer));
8377 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8379 /* Compute the max. number of lines specified by the user. */
8380 if (FLOATP (Vmax_mini_window_height))
8381 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8382 else if (INTEGERP (Vmax_mini_window_height))
8383 max_height = XINT (Vmax_mini_window_height);
8384 else
8385 max_height = total_height / 4;
8387 /* Correct that max. height if it's bogus. */
8388 max_height = max (1, max_height);
8389 max_height = min (total_height, max_height);
8391 /* Find out the height of the text in the window. */
8392 if (it.truncate_lines_p)
8393 height = 1;
8394 else
8396 last_height = 0;
8397 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8398 if (it.max_ascent == 0 && it.max_descent == 0)
8399 height = it.current_y + last_height;
8400 else
8401 height = it.current_y + it.max_ascent + it.max_descent;
8402 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8403 height = (height + unit - 1) / unit;
8406 /* Compute a suitable window start. */
8407 if (height > max_height)
8409 height = max_height;
8410 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8411 move_it_vertically_backward (&it, (height - 1) * unit);
8412 start = it.current.pos;
8414 else
8415 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8416 SET_MARKER_FROM_TEXT_POS (w->start, start);
8418 if (EQ (Vresize_mini_windows, Qgrow_only))
8420 /* Let it grow only, until we display an empty message, in which
8421 case the window shrinks again. */
8422 if (height > WINDOW_TOTAL_LINES (w))
8424 int old_height = WINDOW_TOTAL_LINES (w);
8425 freeze_window_starts (f, 1);
8426 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8427 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8429 else if (height < WINDOW_TOTAL_LINES (w)
8430 && (exact_p || BEGV == ZV))
8432 int old_height = WINDOW_TOTAL_LINES (w);
8433 freeze_window_starts (f, 0);
8434 shrink_mini_window (w);
8435 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8438 else
8440 /* Always resize to exact size needed. */
8441 if (height > WINDOW_TOTAL_LINES (w))
8443 int old_height = WINDOW_TOTAL_LINES (w);
8444 freeze_window_starts (f, 1);
8445 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8446 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8448 else if (height < WINDOW_TOTAL_LINES (w))
8450 int old_height = WINDOW_TOTAL_LINES (w);
8451 freeze_window_starts (f, 0);
8452 shrink_mini_window (w);
8454 if (height)
8456 freeze_window_starts (f, 1);
8457 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8460 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8464 if (old_current_buffer)
8465 set_buffer_internal (old_current_buffer);
8468 return window_height_changed_p;
8472 /* Value is the current message, a string, or nil if there is no
8473 current message. */
8475 Lisp_Object
8476 current_message ()
8478 Lisp_Object msg;
8480 if (NILP (echo_area_buffer[0]))
8481 msg = Qnil;
8482 else
8484 with_echo_area_buffer (0, 0, current_message_1,
8485 (EMACS_INT) &msg, Qnil, 0, 0);
8486 if (NILP (msg))
8487 echo_area_buffer[0] = Qnil;
8490 return msg;
8494 static int
8495 current_message_1 (a1, a2, a3, a4)
8496 EMACS_INT a1;
8497 Lisp_Object a2;
8498 EMACS_INT a3, a4;
8500 Lisp_Object *msg = (Lisp_Object *) a1;
8502 if (Z > BEG)
8503 *msg = make_buffer_string (BEG, Z, 1);
8504 else
8505 *msg = Qnil;
8506 return 0;
8510 /* Push the current message on Vmessage_stack for later restauration
8511 by restore_message. Value is non-zero if the current message isn't
8512 empty. This is a relatively infrequent operation, so it's not
8513 worth optimizing. */
8516 push_message ()
8518 Lisp_Object msg;
8519 msg = current_message ();
8520 Vmessage_stack = Fcons (msg, Vmessage_stack);
8521 return STRINGP (msg);
8525 /* Restore message display from the top of Vmessage_stack. */
8527 void
8528 restore_message ()
8530 Lisp_Object msg;
8532 xassert (CONSP (Vmessage_stack));
8533 msg = XCAR (Vmessage_stack);
8534 if (STRINGP (msg))
8535 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8536 else
8537 message3_nolog (msg, 0, 0);
8541 /* Handler for record_unwind_protect calling pop_message. */
8543 Lisp_Object
8544 pop_message_unwind (dummy)
8545 Lisp_Object dummy;
8547 pop_message ();
8548 return Qnil;
8551 /* Pop the top-most entry off Vmessage_stack. */
8553 void
8554 pop_message ()
8556 xassert (CONSP (Vmessage_stack));
8557 Vmessage_stack = XCDR (Vmessage_stack);
8561 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8562 exits. If the stack is not empty, we have a missing pop_message
8563 somewhere. */
8565 void
8566 check_message_stack ()
8568 if (!NILP (Vmessage_stack))
8569 abort ();
8573 /* Truncate to NCHARS what will be displayed in the echo area the next
8574 time we display it---but don't redisplay it now. */
8576 void
8577 truncate_echo_area (nchars)
8578 int nchars;
8580 if (nchars == 0)
8581 echo_area_buffer[0] = Qnil;
8582 /* A null message buffer means that the frame hasn't really been
8583 initialized yet. Error messages get reported properly by
8584 cmd_error, so this must be just an informative message; toss it. */
8585 else if (!noninteractive
8586 && INTERACTIVE
8587 && !NILP (echo_area_buffer[0]))
8589 struct frame *sf = SELECTED_FRAME ();
8590 if (FRAME_MESSAGE_BUF (sf))
8591 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8596 /* Helper function for truncate_echo_area. Truncate the current
8597 message to at most NCHARS characters. */
8599 static int
8600 truncate_message_1 (nchars, a2, a3, a4)
8601 EMACS_INT nchars;
8602 Lisp_Object a2;
8603 EMACS_INT a3, a4;
8605 if (BEG + nchars < Z)
8606 del_range (BEG + nchars, Z);
8607 if (Z == BEG)
8608 echo_area_buffer[0] = Qnil;
8609 return 0;
8613 /* Set the current message to a substring of S or STRING.
8615 If STRING is a Lisp string, set the message to the first NBYTES
8616 bytes from STRING. NBYTES zero means use the whole string. If
8617 STRING is multibyte, the message will be displayed multibyte.
8619 If S is not null, set the message to the first LEN bytes of S. LEN
8620 zero means use the whole string. MULTIBYTE_P non-zero means S is
8621 multibyte. Display the message multibyte in that case.
8623 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8624 to t before calling set_message_1 (which calls insert).
8627 void
8628 set_message (s, string, nbytes, multibyte_p)
8629 const char *s;
8630 Lisp_Object string;
8631 int nbytes, multibyte_p;
8633 message_enable_multibyte
8634 = ((s && multibyte_p)
8635 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8637 with_echo_area_buffer (0, 0, set_message_1,
8638 (EMACS_INT) s, string, nbytes, multibyte_p);
8639 message_buf_print = 0;
8640 help_echo_showing_p = 0;
8644 /* Helper function for set_message. Arguments have the same meaning
8645 as there, with A1 corresponding to S and A2 corresponding to STRING
8646 This function is called with the echo area buffer being
8647 current. */
8649 static int
8650 set_message_1 (a1, a2, nbytes, multibyte_p)
8651 EMACS_INT a1;
8652 Lisp_Object a2;
8653 EMACS_INT nbytes, multibyte_p;
8655 const char *s = (const char *) a1;
8656 Lisp_Object string = a2;
8658 /* Change multibyteness of the echo buffer appropriately. */
8659 if (message_enable_multibyte
8660 != !NILP (current_buffer->enable_multibyte_characters))
8661 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8663 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8665 /* Insert new message at BEG. */
8666 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8667 Ferase_buffer ();
8669 if (STRINGP (string))
8671 int nchars;
8673 if (nbytes == 0)
8674 nbytes = SBYTES (string);
8675 nchars = string_byte_to_char (string, nbytes);
8677 /* This function takes care of single/multibyte conversion. We
8678 just have to ensure that the echo area buffer has the right
8679 setting of enable_multibyte_characters. */
8680 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8682 else if (s)
8684 if (nbytes == 0)
8685 nbytes = strlen (s);
8687 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8689 /* Convert from multi-byte to single-byte. */
8690 int i, c, n;
8691 unsigned char work[1];
8693 /* Convert a multibyte string to single-byte. */
8694 for (i = 0; i < nbytes; i += n)
8696 c = string_char_and_length (s + i, nbytes - i, &n);
8697 work[0] = (SINGLE_BYTE_CHAR_P (c)
8699 : multibyte_char_to_unibyte (c, Qnil));
8700 insert_1_both (work, 1, 1, 1, 0, 0);
8703 else if (!multibyte_p
8704 && !NILP (current_buffer->enable_multibyte_characters))
8706 /* Convert from single-byte to multi-byte. */
8707 int i, c, n;
8708 const unsigned char *msg = (const unsigned char *) s;
8709 unsigned char str[MAX_MULTIBYTE_LENGTH];
8711 /* Convert a single-byte string to multibyte. */
8712 for (i = 0; i < nbytes; i++)
8714 c = unibyte_char_to_multibyte (msg[i]);
8715 n = CHAR_STRING (c, str);
8716 insert_1_both (str, 1, n, 1, 0, 0);
8719 else
8720 insert_1 (s, nbytes, 1, 0, 0);
8723 return 0;
8727 /* Clear messages. CURRENT_P non-zero means clear the current
8728 message. LAST_DISPLAYED_P non-zero means clear the message
8729 last displayed. */
8731 void
8732 clear_message (current_p, last_displayed_p)
8733 int current_p, last_displayed_p;
8735 if (current_p)
8737 echo_area_buffer[0] = Qnil;
8738 message_cleared_p = 1;
8741 if (last_displayed_p)
8742 echo_area_buffer[1] = Qnil;
8744 message_buf_print = 0;
8747 /* Clear garbaged frames.
8749 This function is used where the old redisplay called
8750 redraw_garbaged_frames which in turn called redraw_frame which in
8751 turn called clear_frame. The call to clear_frame was a source of
8752 flickering. I believe a clear_frame is not necessary. It should
8753 suffice in the new redisplay to invalidate all current matrices,
8754 and ensure a complete redisplay of all windows. */
8756 static void
8757 clear_garbaged_frames ()
8759 if (frame_garbaged)
8761 Lisp_Object tail, frame;
8762 int changed_count = 0;
8764 FOR_EACH_FRAME (tail, frame)
8766 struct frame *f = XFRAME (frame);
8768 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8770 if (f->resized_p)
8772 Fredraw_frame (frame);
8773 f->force_flush_display_p = 1;
8775 clear_current_matrices (f);
8776 changed_count++;
8777 f->garbaged = 0;
8778 f->resized_p = 0;
8782 frame_garbaged = 0;
8783 if (changed_count)
8784 ++windows_or_buffers_changed;
8789 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8790 is non-zero update selected_frame. Value is non-zero if the
8791 mini-windows height has been changed. */
8793 static int
8794 echo_area_display (update_frame_p)
8795 int update_frame_p;
8797 Lisp_Object mini_window;
8798 struct window *w;
8799 struct frame *f;
8800 int window_height_changed_p = 0;
8801 struct frame *sf = SELECTED_FRAME ();
8803 mini_window = FRAME_MINIBUF_WINDOW (sf);
8804 w = XWINDOW (mini_window);
8805 f = XFRAME (WINDOW_FRAME (w));
8807 /* Don't display if frame is invisible or not yet initialized. */
8808 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8809 return 0;
8811 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8812 #ifndef MAC_OS8
8813 #ifdef HAVE_WINDOW_SYSTEM
8814 /* When Emacs starts, selected_frame may be a visible terminal
8815 frame, even if we run under a window system. If we let this
8816 through, a message would be displayed on the terminal. */
8817 if (EQ (selected_frame, Vterminal_frame)
8818 && !NILP (Vwindow_system))
8819 return 0;
8820 #endif /* HAVE_WINDOW_SYSTEM */
8821 #endif
8823 /* Redraw garbaged frames. */
8824 if (frame_garbaged)
8825 clear_garbaged_frames ();
8827 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8829 echo_area_window = mini_window;
8830 window_height_changed_p = display_echo_area (w);
8831 w->must_be_updated_p = 1;
8833 /* Update the display, unless called from redisplay_internal.
8834 Also don't update the screen during redisplay itself. The
8835 update will happen at the end of redisplay, and an update
8836 here could cause confusion. */
8837 if (update_frame_p && !redisplaying_p)
8839 int n = 0;
8841 /* If the display update has been interrupted by pending
8842 input, update mode lines in the frame. Due to the
8843 pending input, it might have been that redisplay hasn't
8844 been called, so that mode lines above the echo area are
8845 garbaged. This looks odd, so we prevent it here. */
8846 if (!display_completed)
8847 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8849 if (window_height_changed_p
8850 /* Don't do this if Emacs is shutting down. Redisplay
8851 needs to run hooks. */
8852 && !NILP (Vrun_hooks))
8854 /* Must update other windows. Likewise as in other
8855 cases, don't let this update be interrupted by
8856 pending input. */
8857 int count = SPECPDL_INDEX ();
8858 specbind (Qredisplay_dont_pause, Qt);
8859 windows_or_buffers_changed = 1;
8860 redisplay_internal (0);
8861 unbind_to (count, Qnil);
8863 else if (FRAME_WINDOW_P (f) && n == 0)
8865 /* Window configuration is the same as before.
8866 Can do with a display update of the echo area,
8867 unless we displayed some mode lines. */
8868 update_single_window (w, 1);
8869 rif->flush_display (f);
8871 else
8872 update_frame (f, 1, 1);
8874 /* If cursor is in the echo area, make sure that the next
8875 redisplay displays the minibuffer, so that the cursor will
8876 be replaced with what the minibuffer wants. */
8877 if (cursor_in_echo_area)
8878 ++windows_or_buffers_changed;
8881 else if (!EQ (mini_window, selected_window))
8882 windows_or_buffers_changed++;
8884 /* The current message is now also the last one displayed. */
8885 echo_area_buffer[1] = echo_area_buffer[0];
8887 /* Prevent redisplay optimization in redisplay_internal by resetting
8888 this_line_start_pos. This is done because the mini-buffer now
8889 displays the message instead of its buffer text. */
8890 if (EQ (mini_window, selected_window))
8891 CHARPOS (this_line_start_pos) = 0;
8893 return window_height_changed_p;
8898 /***********************************************************************
8899 Mode Lines and Frame Titles
8900 ***********************************************************************/
8902 /* A buffer for constructing non-propertized mode-line strings and
8903 frame titles in it; allocated from the heap in init_xdisp and
8904 resized as needed in store_mode_line_noprop_char. */
8906 static char *mode_line_noprop_buf;
8908 /* The buffer's end, and a current output position in it. */
8910 static char *mode_line_noprop_buf_end;
8911 static char *mode_line_noprop_ptr;
8913 #define MODE_LINE_NOPROP_LEN(start) \
8914 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8916 static enum {
8917 MODE_LINE_DISPLAY = 0,
8918 MODE_LINE_TITLE,
8919 MODE_LINE_NOPROP,
8920 MODE_LINE_STRING
8921 } mode_line_target;
8923 /* Alist that caches the results of :propertize.
8924 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8925 static Lisp_Object mode_line_proptrans_alist;
8927 /* List of strings making up the mode-line. */
8928 static Lisp_Object mode_line_string_list;
8930 /* Base face property when building propertized mode line string. */
8931 static Lisp_Object mode_line_string_face;
8932 static Lisp_Object mode_line_string_face_prop;
8935 /* Unwind data for mode line strings */
8937 static Lisp_Object Vmode_line_unwind_vector;
8939 static Lisp_Object
8940 format_mode_line_unwind_data (obuf, save_proptrans)
8941 struct buffer *obuf;
8943 Lisp_Object vector;
8945 /* Reduce consing by keeping one vector in
8946 Vwith_echo_area_save_vector. */
8947 vector = Vmode_line_unwind_vector;
8948 Vmode_line_unwind_vector = Qnil;
8950 if (NILP (vector))
8951 vector = Fmake_vector (make_number (7), Qnil);
8953 AREF (vector, 0) = make_number (mode_line_target);
8954 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8955 AREF (vector, 2) = mode_line_string_list;
8956 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8957 AREF (vector, 4) = mode_line_string_face;
8958 AREF (vector, 5) = mode_line_string_face_prop;
8960 if (obuf)
8961 XSETBUFFER (AREF (vector, 6), obuf);
8962 else
8963 AREF (vector, 6) = Qnil;
8965 return vector;
8968 static Lisp_Object
8969 unwind_format_mode_line (vector)
8970 Lisp_Object vector;
8972 mode_line_target = XINT (AREF (vector, 0));
8973 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8974 mode_line_string_list = AREF (vector, 2);
8975 if (! EQ (AREF (vector, 3), Qt))
8976 mode_line_proptrans_alist = AREF (vector, 3);
8977 mode_line_string_face = AREF (vector, 4);
8978 mode_line_string_face_prop = AREF (vector, 5);
8980 if (!NILP (AREF (vector, 6)))
8982 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8983 AREF (vector, 6) = Qnil;
8986 Vmode_line_unwind_vector = vector;
8987 return Qnil;
8991 /* Store a single character C for the frame title in mode_line_noprop_buf.
8992 Re-allocate mode_line_noprop_buf if necessary. */
8994 static void
8995 #ifdef PROTOTYPES
8996 store_mode_line_noprop_char (char c)
8997 #else
8998 store_mode_line_noprop_char (c)
8999 char c;
9000 #endif
9002 /* If output position has reached the end of the allocated buffer,
9003 double the buffer's size. */
9004 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9006 int len = MODE_LINE_NOPROP_LEN (0);
9007 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9008 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9009 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9010 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9013 *mode_line_noprop_ptr++ = c;
9017 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9018 mode_line_noprop_ptr. STR is the string to store. Do not copy
9019 characters that yield more columns than PRECISION; PRECISION <= 0
9020 means copy the whole string. Pad with spaces until FIELD_WIDTH
9021 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9022 pad. Called from display_mode_element when it is used to build a
9023 frame title. */
9025 static int
9026 store_mode_line_noprop (str, field_width, precision)
9027 const unsigned char *str;
9028 int field_width, precision;
9030 int n = 0;
9031 int dummy, nbytes;
9033 /* Copy at most PRECISION chars from STR. */
9034 nbytes = strlen (str);
9035 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9036 while (nbytes--)
9037 store_mode_line_noprop_char (*str++);
9039 /* Fill up with spaces until FIELD_WIDTH reached. */
9040 while (field_width > 0
9041 && n < field_width)
9043 store_mode_line_noprop_char (' ');
9044 ++n;
9047 return n;
9050 /***********************************************************************
9051 Frame Titles
9052 ***********************************************************************/
9054 #ifdef HAVE_WINDOW_SYSTEM
9056 /* Set the title of FRAME, if it has changed. The title format is
9057 Vicon_title_format if FRAME is iconified, otherwise it is
9058 frame_title_format. */
9060 static void
9061 x_consider_frame_title (frame)
9062 Lisp_Object frame;
9064 struct frame *f = XFRAME (frame);
9066 if (FRAME_WINDOW_P (f)
9067 || FRAME_MINIBUF_ONLY_P (f)
9068 || f->explicit_name)
9070 /* Do we have more than one visible frame on this X display? */
9071 Lisp_Object tail;
9072 Lisp_Object fmt;
9073 int title_start;
9074 char *title;
9075 int len;
9076 struct it it;
9077 int count = SPECPDL_INDEX ();
9079 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9081 Lisp_Object other_frame = XCAR (tail);
9082 struct frame *tf = XFRAME (other_frame);
9084 if (tf != f
9085 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9086 && !FRAME_MINIBUF_ONLY_P (tf)
9087 && !EQ (other_frame, tip_frame)
9088 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9089 break;
9092 /* Set global variable indicating that multiple frames exist. */
9093 multiple_frames = CONSP (tail);
9095 /* Switch to the buffer of selected window of the frame. Set up
9096 mode_line_target so that display_mode_element will output into
9097 mode_line_noprop_buf; then display the title. */
9098 record_unwind_protect (unwind_format_mode_line,
9099 format_mode_line_unwind_data (current_buffer, 0));
9101 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9102 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9104 mode_line_target = MODE_LINE_TITLE;
9105 title_start = MODE_LINE_NOPROP_LEN (0);
9106 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9107 NULL, DEFAULT_FACE_ID);
9108 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9109 len = MODE_LINE_NOPROP_LEN (title_start);
9110 title = mode_line_noprop_buf + title_start;
9111 unbind_to (count, Qnil);
9113 /* Set the title only if it's changed. This avoids consing in
9114 the common case where it hasn't. (If it turns out that we've
9115 already wasted too much time by walking through the list with
9116 display_mode_element, then we might need to optimize at a
9117 higher level than this.) */
9118 if (! STRINGP (f->name)
9119 || SBYTES (f->name) != len
9120 || bcmp (title, SDATA (f->name), len) != 0)
9121 x_implicitly_set_name (f, make_string (title, len), Qnil);
9125 #endif /* not HAVE_WINDOW_SYSTEM */
9130 /***********************************************************************
9131 Menu Bars
9132 ***********************************************************************/
9135 /* Prepare for redisplay by updating menu-bar item lists when
9136 appropriate. This can call eval. */
9138 void
9139 prepare_menu_bars ()
9141 int all_windows;
9142 struct gcpro gcpro1, gcpro2;
9143 struct frame *f;
9144 Lisp_Object tooltip_frame;
9146 #ifdef HAVE_WINDOW_SYSTEM
9147 tooltip_frame = tip_frame;
9148 #else
9149 tooltip_frame = Qnil;
9150 #endif
9152 /* Update all frame titles based on their buffer names, etc. We do
9153 this before the menu bars so that the buffer-menu will show the
9154 up-to-date frame titles. */
9155 #ifdef HAVE_WINDOW_SYSTEM
9156 if (windows_or_buffers_changed || update_mode_lines)
9158 Lisp_Object tail, frame;
9160 FOR_EACH_FRAME (tail, frame)
9162 f = XFRAME (frame);
9163 if (!EQ (frame, tooltip_frame)
9164 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9165 x_consider_frame_title (frame);
9168 #endif /* HAVE_WINDOW_SYSTEM */
9170 /* Update the menu bar item lists, if appropriate. This has to be
9171 done before any actual redisplay or generation of display lines. */
9172 all_windows = (update_mode_lines
9173 || buffer_shared > 1
9174 || windows_or_buffers_changed);
9175 if (all_windows)
9177 Lisp_Object tail, frame;
9178 int count = SPECPDL_INDEX ();
9179 /* 1 means that update_menu_bar has run its hooks
9180 so any further calls to update_menu_bar shouldn't do so again. */
9181 int menu_bar_hooks_run = 0;
9183 record_unwind_save_match_data ();
9185 FOR_EACH_FRAME (tail, frame)
9187 f = XFRAME (frame);
9189 /* Ignore tooltip frame. */
9190 if (EQ (frame, tooltip_frame))
9191 continue;
9193 /* If a window on this frame changed size, report that to
9194 the user and clear the size-change flag. */
9195 if (FRAME_WINDOW_SIZES_CHANGED (f))
9197 Lisp_Object functions;
9199 /* Clear flag first in case we get an error below. */
9200 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9201 functions = Vwindow_size_change_functions;
9202 GCPRO2 (tail, functions);
9204 while (CONSP (functions))
9206 call1 (XCAR (functions), frame);
9207 functions = XCDR (functions);
9209 UNGCPRO;
9212 GCPRO1 (tail);
9213 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9214 #ifdef HAVE_WINDOW_SYSTEM
9215 update_tool_bar (f, 0);
9216 #ifdef MAC_OS
9217 mac_update_title_bar (f, 0);
9218 #endif
9219 #endif
9220 UNGCPRO;
9223 unbind_to (count, Qnil);
9225 else
9227 struct frame *sf = SELECTED_FRAME ();
9228 update_menu_bar (sf, 1, 0);
9229 #ifdef HAVE_WINDOW_SYSTEM
9230 update_tool_bar (sf, 1);
9231 #ifdef MAC_OS
9232 mac_update_title_bar (sf, 1);
9233 #endif
9234 #endif
9237 /* Motif needs this. See comment in xmenu.c. Turn it off when
9238 pending_menu_activation is not defined. */
9239 #ifdef USE_X_TOOLKIT
9240 pending_menu_activation = 0;
9241 #endif
9245 /* Update the menu bar item list for frame F. This has to be done
9246 before we start to fill in any display lines, because it can call
9247 eval.
9249 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9251 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9252 already ran the menu bar hooks for this redisplay, so there
9253 is no need to run them again. The return value is the
9254 updated value of this flag, to pass to the next call. */
9256 static int
9257 update_menu_bar (f, save_match_data, hooks_run)
9258 struct frame *f;
9259 int save_match_data;
9260 int hooks_run;
9262 Lisp_Object window;
9263 register struct window *w;
9265 /* If called recursively during a menu update, do nothing. This can
9266 happen when, for instance, an activate-menubar-hook causes a
9267 redisplay. */
9268 if (inhibit_menubar_update)
9269 return hooks_run;
9271 window = FRAME_SELECTED_WINDOW (f);
9272 w = XWINDOW (window);
9274 #if 0 /* The if statement below this if statement used to include the
9275 condition !NILP (w->update_mode_line), rather than using
9276 update_mode_lines directly, and this if statement may have
9277 been added to make that condition work. Now the if
9278 statement below matches its comment, this isn't needed. */
9279 if (update_mode_lines)
9280 w->update_mode_line = Qt;
9281 #endif
9283 if (FRAME_WINDOW_P (f)
9285 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9286 || defined (USE_GTK)
9287 FRAME_EXTERNAL_MENU_BAR (f)
9288 #else
9289 FRAME_MENU_BAR_LINES (f) > 0
9290 #endif
9291 : FRAME_MENU_BAR_LINES (f) > 0)
9293 /* If the user has switched buffers or windows, we need to
9294 recompute to reflect the new bindings. But we'll
9295 recompute when update_mode_lines is set too; that means
9296 that people can use force-mode-line-update to request
9297 that the menu bar be recomputed. The adverse effect on
9298 the rest of the redisplay algorithm is about the same as
9299 windows_or_buffers_changed anyway. */
9300 if (windows_or_buffers_changed
9301 /* This used to test w->update_mode_line, but we believe
9302 there is no need to recompute the menu in that case. */
9303 || update_mode_lines
9304 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9305 < BUF_MODIFF (XBUFFER (w->buffer)))
9306 != !NILP (w->last_had_star))
9307 || ((!NILP (Vtransient_mark_mode)
9308 && !NILP (XBUFFER (w->buffer)->mark_active))
9309 != !NILP (w->region_showing)))
9311 struct buffer *prev = current_buffer;
9312 int count = SPECPDL_INDEX ();
9314 specbind (Qinhibit_menubar_update, Qt);
9316 set_buffer_internal_1 (XBUFFER (w->buffer));
9317 if (save_match_data)
9318 record_unwind_save_match_data ();
9319 if (NILP (Voverriding_local_map_menu_flag))
9321 specbind (Qoverriding_terminal_local_map, Qnil);
9322 specbind (Qoverriding_local_map, Qnil);
9325 if (!hooks_run)
9327 /* Run the Lucid hook. */
9328 safe_run_hooks (Qactivate_menubar_hook);
9330 /* If it has changed current-menubar from previous value,
9331 really recompute the menu-bar from the value. */
9332 if (! NILP (Vlucid_menu_bar_dirty_flag))
9333 call0 (Qrecompute_lucid_menubar);
9335 safe_run_hooks (Qmenu_bar_update_hook);
9337 hooks_run = 1;
9340 XSETFRAME (Vmenu_updating_frame, f);
9341 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9343 /* Redisplay the menu bar in case we changed it. */
9344 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9345 || defined (USE_GTK)
9346 if (FRAME_WINDOW_P (f))
9348 #ifdef MAC_OS
9349 /* All frames on Mac OS share the same menubar. So only
9350 the selected frame should be allowed to set it. */
9351 if (f == SELECTED_FRAME ())
9352 #endif
9353 set_frame_menubar (f, 0, 0);
9355 else
9356 /* On a terminal screen, the menu bar is an ordinary screen
9357 line, and this makes it get updated. */
9358 w->update_mode_line = Qt;
9359 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9360 /* In the non-toolkit version, the menu bar is an ordinary screen
9361 line, and this makes it get updated. */
9362 w->update_mode_line = Qt;
9363 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9365 unbind_to (count, Qnil);
9366 set_buffer_internal_1 (prev);
9370 return hooks_run;
9375 /***********************************************************************
9376 Output Cursor
9377 ***********************************************************************/
9379 #ifdef HAVE_WINDOW_SYSTEM
9381 /* EXPORT:
9382 Nominal cursor position -- where to draw output.
9383 HPOS and VPOS are window relative glyph matrix coordinates.
9384 X and Y are window relative pixel coordinates. */
9386 struct cursor_pos output_cursor;
9389 /* EXPORT:
9390 Set the global variable output_cursor to CURSOR. All cursor
9391 positions are relative to updated_window. */
9393 void
9394 set_output_cursor (cursor)
9395 struct cursor_pos *cursor;
9397 output_cursor.hpos = cursor->hpos;
9398 output_cursor.vpos = cursor->vpos;
9399 output_cursor.x = cursor->x;
9400 output_cursor.y = cursor->y;
9404 /* EXPORT for RIF:
9405 Set a nominal cursor position.
9407 HPOS and VPOS are column/row positions in a window glyph matrix. X
9408 and Y are window text area relative pixel positions.
9410 If this is done during an update, updated_window will contain the
9411 window that is being updated and the position is the future output
9412 cursor position for that window. If updated_window is null, use
9413 selected_window and display the cursor at the given position. */
9415 void
9416 x_cursor_to (vpos, hpos, y, x)
9417 int vpos, hpos, y, x;
9419 struct window *w;
9421 /* If updated_window is not set, work on selected_window. */
9422 if (updated_window)
9423 w = updated_window;
9424 else
9425 w = XWINDOW (selected_window);
9427 /* Set the output cursor. */
9428 output_cursor.hpos = hpos;
9429 output_cursor.vpos = vpos;
9430 output_cursor.x = x;
9431 output_cursor.y = y;
9433 /* If not called as part of an update, really display the cursor.
9434 This will also set the cursor position of W. */
9435 if (updated_window == NULL)
9437 BLOCK_INPUT;
9438 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9439 if (rif->flush_display_optional)
9440 rif->flush_display_optional (SELECTED_FRAME ());
9441 UNBLOCK_INPUT;
9445 #endif /* HAVE_WINDOW_SYSTEM */
9448 /***********************************************************************
9449 Tool-bars
9450 ***********************************************************************/
9452 #ifdef HAVE_WINDOW_SYSTEM
9454 /* Where the mouse was last time we reported a mouse event. */
9456 FRAME_PTR last_mouse_frame;
9458 /* Tool-bar item index of the item on which a mouse button was pressed
9459 or -1. */
9461 int last_tool_bar_item;
9464 /* Update the tool-bar item list for frame F. This has to be done
9465 before we start to fill in any display lines. Called from
9466 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9467 and restore it here. */
9469 static void
9470 update_tool_bar (f, save_match_data)
9471 struct frame *f;
9472 int save_match_data;
9474 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9475 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9476 #else
9477 int do_update = WINDOWP (f->tool_bar_window)
9478 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9479 #endif
9481 if (do_update)
9483 Lisp_Object window;
9484 struct window *w;
9486 window = FRAME_SELECTED_WINDOW (f);
9487 w = XWINDOW (window);
9489 /* If the user has switched buffers or windows, we need to
9490 recompute to reflect the new bindings. But we'll
9491 recompute when update_mode_lines is set too; that means
9492 that people can use force-mode-line-update to request
9493 that the menu bar be recomputed. The adverse effect on
9494 the rest of the redisplay algorithm is about the same as
9495 windows_or_buffers_changed anyway. */
9496 if (windows_or_buffers_changed
9497 || !NILP (w->update_mode_line)
9498 || update_mode_lines
9499 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9500 < BUF_MODIFF (XBUFFER (w->buffer)))
9501 != !NILP (w->last_had_star))
9502 || ((!NILP (Vtransient_mark_mode)
9503 && !NILP (XBUFFER (w->buffer)->mark_active))
9504 != !NILP (w->region_showing)))
9506 struct buffer *prev = current_buffer;
9507 int count = SPECPDL_INDEX ();
9508 Lisp_Object new_tool_bar;
9509 int new_n_tool_bar;
9510 struct gcpro gcpro1;
9512 /* Set current_buffer to the buffer of the selected
9513 window of the frame, so that we get the right local
9514 keymaps. */
9515 set_buffer_internal_1 (XBUFFER (w->buffer));
9517 /* Save match data, if we must. */
9518 if (save_match_data)
9519 record_unwind_save_match_data ();
9521 /* Make sure that we don't accidentally use bogus keymaps. */
9522 if (NILP (Voverriding_local_map_menu_flag))
9524 specbind (Qoverriding_terminal_local_map, Qnil);
9525 specbind (Qoverriding_local_map, Qnil);
9528 GCPRO1 (new_tool_bar);
9530 /* Build desired tool-bar items from keymaps. */
9531 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9532 &new_n_tool_bar);
9534 /* Redisplay the tool-bar if we changed it. */
9535 if (new_n_tool_bar != f->n_tool_bar_items
9536 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9538 /* Redisplay that happens asynchronously due to an expose event
9539 may access f->tool_bar_items. Make sure we update both
9540 variables within BLOCK_INPUT so no such event interrupts. */
9541 BLOCK_INPUT;
9542 f->tool_bar_items = new_tool_bar;
9543 f->n_tool_bar_items = new_n_tool_bar;
9544 w->update_mode_line = Qt;
9545 UNBLOCK_INPUT;
9548 UNGCPRO;
9550 unbind_to (count, Qnil);
9551 set_buffer_internal_1 (prev);
9557 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9558 F's desired tool-bar contents. F->tool_bar_items must have
9559 been set up previously by calling prepare_menu_bars. */
9561 static void
9562 build_desired_tool_bar_string (f)
9563 struct frame *f;
9565 int i, size, size_needed;
9566 struct gcpro gcpro1, gcpro2, gcpro3;
9567 Lisp_Object image, plist, props;
9569 image = plist = props = Qnil;
9570 GCPRO3 (image, plist, props);
9572 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9573 Otherwise, make a new string. */
9575 /* The size of the string we might be able to reuse. */
9576 size = (STRINGP (f->desired_tool_bar_string)
9577 ? SCHARS (f->desired_tool_bar_string)
9578 : 0);
9580 /* We need one space in the string for each image. */
9581 size_needed = f->n_tool_bar_items;
9583 /* Reuse f->desired_tool_bar_string, if possible. */
9584 if (size < size_needed || NILP (f->desired_tool_bar_string))
9585 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9586 make_number (' '));
9587 else
9589 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9590 Fremove_text_properties (make_number (0), make_number (size),
9591 props, f->desired_tool_bar_string);
9594 /* Put a `display' property on the string for the images to display,
9595 put a `menu_item' property on tool-bar items with a value that
9596 is the index of the item in F's tool-bar item vector. */
9597 for (i = 0; i < f->n_tool_bar_items; ++i)
9599 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9601 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9602 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9603 int hmargin, vmargin, relief, idx, end;
9604 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9606 /* If image is a vector, choose the image according to the
9607 button state. */
9608 image = PROP (TOOL_BAR_ITEM_IMAGES);
9609 if (VECTORP (image))
9611 if (enabled_p)
9612 idx = (selected_p
9613 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9614 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9615 else
9616 idx = (selected_p
9617 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9618 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9620 xassert (ASIZE (image) >= idx);
9621 image = AREF (image, idx);
9623 else
9624 idx = -1;
9626 /* Ignore invalid image specifications. */
9627 if (!valid_image_p (image))
9628 continue;
9630 /* Display the tool-bar button pressed, or depressed. */
9631 plist = Fcopy_sequence (XCDR (image));
9633 /* Compute margin and relief to draw. */
9634 relief = (tool_bar_button_relief >= 0
9635 ? tool_bar_button_relief
9636 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9637 hmargin = vmargin = relief;
9639 if (INTEGERP (Vtool_bar_button_margin)
9640 && XINT (Vtool_bar_button_margin) > 0)
9642 hmargin += XFASTINT (Vtool_bar_button_margin);
9643 vmargin += XFASTINT (Vtool_bar_button_margin);
9645 else if (CONSP (Vtool_bar_button_margin))
9647 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9648 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9649 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9651 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9652 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9653 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9656 if (auto_raise_tool_bar_buttons_p)
9658 /* Add a `:relief' property to the image spec if the item is
9659 selected. */
9660 if (selected_p)
9662 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9663 hmargin -= relief;
9664 vmargin -= relief;
9667 else
9669 /* If image is selected, display it pressed, i.e. with a
9670 negative relief. If it's not selected, display it with a
9671 raised relief. */
9672 plist = Fplist_put (plist, QCrelief,
9673 (selected_p
9674 ? make_number (-relief)
9675 : make_number (relief)));
9676 hmargin -= relief;
9677 vmargin -= relief;
9680 /* Put a margin around the image. */
9681 if (hmargin || vmargin)
9683 if (hmargin == vmargin)
9684 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9685 else
9686 plist = Fplist_put (plist, QCmargin,
9687 Fcons (make_number (hmargin),
9688 make_number (vmargin)));
9691 /* If button is not enabled, and we don't have special images
9692 for the disabled state, make the image appear disabled by
9693 applying an appropriate algorithm to it. */
9694 if (!enabled_p && idx < 0)
9695 plist = Fplist_put (plist, QCconversion, Qdisabled);
9697 /* Put a `display' text property on the string for the image to
9698 display. Put a `menu-item' property on the string that gives
9699 the start of this item's properties in the tool-bar items
9700 vector. */
9701 image = Fcons (Qimage, plist);
9702 props = list4 (Qdisplay, image,
9703 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9705 /* Let the last image hide all remaining spaces in the tool bar
9706 string. The string can be longer than needed when we reuse a
9707 previous string. */
9708 if (i + 1 == f->n_tool_bar_items)
9709 end = SCHARS (f->desired_tool_bar_string);
9710 else
9711 end = i + 1;
9712 Fadd_text_properties (make_number (i), make_number (end),
9713 props, f->desired_tool_bar_string);
9714 #undef PROP
9717 UNGCPRO;
9721 /* Display one line of the tool-bar of frame IT->f.
9723 HEIGHT specifies the desired height of the tool-bar line.
9724 If the actual height of the glyph row is less than HEIGHT, the
9725 row's height is increased to HEIGHT, and the icons are centered
9726 vertically in the new height.
9728 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9729 count a final empty row in case the tool-bar width exactly matches
9730 the window width.
9733 static void
9734 display_tool_bar_line (it, height)
9735 struct it *it;
9736 int height;
9738 struct glyph_row *row = it->glyph_row;
9739 int max_x = it->last_visible_x;
9740 struct glyph *last;
9742 prepare_desired_row (row);
9743 row->y = it->current_y;
9745 /* Note that this isn't made use of if the face hasn't a box,
9746 so there's no need to check the face here. */
9747 it->start_of_box_run_p = 1;
9749 while (it->current_x < max_x)
9751 int x, n_glyphs_before, i, nglyphs;
9752 struct it it_before;
9754 /* Get the next display element. */
9755 if (!get_next_display_element (it))
9757 /* Don't count empty row if we are counting needed tool-bar lines. */
9758 if (height < 0 && !it->hpos)
9759 return;
9760 break;
9763 /* Produce glyphs. */
9764 n_glyphs_before = row->used[TEXT_AREA];
9765 it_before = *it;
9767 PRODUCE_GLYPHS (it);
9769 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9770 i = 0;
9771 x = it_before.current_x;
9772 while (i < nglyphs)
9774 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9776 if (x + glyph->pixel_width > max_x)
9778 /* Glyph doesn't fit on line. Backtrack. */
9779 row->used[TEXT_AREA] = n_glyphs_before;
9780 *it = it_before;
9781 /* If this is the only glyph on this line, it will never fit on the
9782 toolbar, so skip it. But ensure there is at least one glyph,
9783 so we don't accidentally disable the tool-bar. */
9784 if (n_glyphs_before == 0
9785 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9786 break;
9787 goto out;
9790 ++it->hpos;
9791 x += glyph->pixel_width;
9792 ++i;
9795 /* Stop at line ends. */
9796 if (ITERATOR_AT_END_OF_LINE_P (it))
9797 break;
9799 set_iterator_to_next (it, 1);
9802 out:;
9804 row->displays_text_p = row->used[TEXT_AREA] != 0;
9806 /* Use default face for the border below the tool bar.
9808 FIXME: When auto-resize-tool-bars is grow-only, there is
9809 no additional border below the possibly empty tool-bar lines.
9810 So to make the extra empty lines look "normal", we have to
9811 use the tool-bar face for the border too. */
9812 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9813 it->face_id = DEFAULT_FACE_ID;
9815 extend_face_to_end_of_line (it);
9816 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9817 last->right_box_line_p = 1;
9818 if (last == row->glyphs[TEXT_AREA])
9819 last->left_box_line_p = 1;
9821 /* Make line the desired height and center it vertically. */
9822 if ((height -= it->max_ascent + it->max_descent) > 0)
9824 /* Don't add more than one line height. */
9825 height %= FRAME_LINE_HEIGHT (it->f);
9826 it->max_ascent += height / 2;
9827 it->max_descent += (height + 1) / 2;
9830 compute_line_metrics (it);
9832 /* If line is empty, make it occupy the rest of the tool-bar. */
9833 if (!row->displays_text_p)
9835 row->height = row->phys_height = it->last_visible_y - row->y;
9836 row->visible_height = row->height;
9837 row->ascent = row->phys_ascent = 0;
9838 row->extra_line_spacing = 0;
9841 row->full_width_p = 1;
9842 row->continued_p = 0;
9843 row->truncated_on_left_p = 0;
9844 row->truncated_on_right_p = 0;
9846 it->current_x = it->hpos = 0;
9847 it->current_y += row->height;
9848 ++it->vpos;
9849 ++it->glyph_row;
9853 /* Max tool-bar height. */
9855 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9856 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9858 /* Value is the number of screen lines needed to make all tool-bar
9859 items of frame F visible. The number of actual rows needed is
9860 returned in *N_ROWS if non-NULL. */
9862 static int
9863 tool_bar_lines_needed (f, n_rows)
9864 struct frame *f;
9865 int *n_rows;
9867 struct window *w = XWINDOW (f->tool_bar_window);
9868 struct it it;
9869 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9870 the desired matrix, so use (unused) mode-line row as temporary row to
9871 avoid destroying the first tool-bar row. */
9872 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9874 /* Initialize an iterator for iteration over
9875 F->desired_tool_bar_string in the tool-bar window of frame F. */
9876 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9877 it.first_visible_x = 0;
9878 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9879 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9881 while (!ITERATOR_AT_END_P (&it))
9883 clear_glyph_row (temp_row);
9884 it.glyph_row = temp_row;
9885 display_tool_bar_line (&it, -1);
9887 clear_glyph_row (temp_row);
9889 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9890 if (n_rows)
9891 *n_rows = it.vpos > 0 ? it.vpos : -1;
9893 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9897 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9898 0, 1, 0,
9899 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9900 (frame)
9901 Lisp_Object frame;
9903 struct frame *f;
9904 struct window *w;
9905 int nlines = 0;
9907 if (NILP (frame))
9908 frame = selected_frame;
9909 else
9910 CHECK_FRAME (frame);
9911 f = XFRAME (frame);
9913 if (WINDOWP (f->tool_bar_window)
9914 || (w = XWINDOW (f->tool_bar_window),
9915 WINDOW_TOTAL_LINES (w) > 0))
9917 update_tool_bar (f, 1);
9918 if (f->n_tool_bar_items)
9920 build_desired_tool_bar_string (f);
9921 nlines = tool_bar_lines_needed (f, NULL);
9925 return make_number (nlines);
9929 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9930 height should be changed. */
9932 static int
9933 redisplay_tool_bar (f)
9934 struct frame *f;
9936 struct window *w;
9937 struct it it;
9938 struct glyph_row *row;
9940 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9941 if (FRAME_EXTERNAL_TOOL_BAR (f))
9942 update_frame_tool_bar (f);
9943 return 0;
9944 #endif
9946 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9947 do anything. This means you must start with tool-bar-lines
9948 non-zero to get the auto-sizing effect. Or in other words, you
9949 can turn off tool-bars by specifying tool-bar-lines zero. */
9950 if (!WINDOWP (f->tool_bar_window)
9951 || (w = XWINDOW (f->tool_bar_window),
9952 WINDOW_TOTAL_LINES (w) == 0))
9953 return 0;
9955 /* Set up an iterator for the tool-bar window. */
9956 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9957 it.first_visible_x = 0;
9958 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9959 row = it.glyph_row;
9961 /* Build a string that represents the contents of the tool-bar. */
9962 build_desired_tool_bar_string (f);
9963 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9965 if (f->n_tool_bar_rows == 0)
9967 int nlines;
9969 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9970 nlines != WINDOW_TOTAL_LINES (w)))
9972 extern Lisp_Object Qtool_bar_lines;
9973 Lisp_Object frame;
9974 int old_height = WINDOW_TOTAL_LINES (w);
9976 XSETFRAME (frame, f);
9977 Fmodify_frame_parameters (frame,
9978 Fcons (Fcons (Qtool_bar_lines,
9979 make_number (nlines)),
9980 Qnil));
9981 if (WINDOW_TOTAL_LINES (w) != old_height)
9983 clear_glyph_matrix (w->desired_matrix);
9984 fonts_changed_p = 1;
9985 return 1;
9990 /* Display as many lines as needed to display all tool-bar items. */
9992 if (f->n_tool_bar_rows > 0)
9994 int border, rows, height, extra;
9996 if (INTEGERP (Vtool_bar_border))
9997 border = XINT (Vtool_bar_border);
9998 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9999 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10000 else if (EQ (Vtool_bar_border, Qborder_width))
10001 border = f->border_width;
10002 else
10003 border = 0;
10004 if (border < 0)
10005 border = 0;
10007 rows = f->n_tool_bar_rows;
10008 height = max (1, (it.last_visible_y - border) / rows);
10009 extra = it.last_visible_y - border - height * rows;
10011 while (it.current_y < it.last_visible_y)
10013 int h = 0;
10014 if (extra > 0 && rows-- > 0)
10016 h = (extra + rows - 1) / rows;
10017 extra -= h;
10019 display_tool_bar_line (&it, height + h);
10022 else
10024 while (it.current_y < it.last_visible_y)
10025 display_tool_bar_line (&it, 0);
10028 /* It doesn't make much sense to try scrolling in the tool-bar
10029 window, so don't do it. */
10030 w->desired_matrix->no_scrolling_p = 1;
10031 w->must_be_updated_p = 1;
10033 if (!NILP (Vauto_resize_tool_bars))
10035 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10036 int change_height_p = 0;
10038 /* If we couldn't display everything, change the tool-bar's
10039 height if there is room for more. */
10040 if (IT_STRING_CHARPOS (it) < it.end_charpos
10041 && it.current_y < max_tool_bar_height)
10042 change_height_p = 1;
10044 row = it.glyph_row - 1;
10046 /* If there are blank lines at the end, except for a partially
10047 visible blank line at the end that is smaller than
10048 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10049 if (!row->displays_text_p
10050 && row->height >= FRAME_LINE_HEIGHT (f))
10051 change_height_p = 1;
10053 /* If row displays tool-bar items, but is partially visible,
10054 change the tool-bar's height. */
10055 if (row->displays_text_p
10056 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10057 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10058 change_height_p = 1;
10060 /* Resize windows as needed by changing the `tool-bar-lines'
10061 frame parameter. */
10062 if (change_height_p)
10064 extern Lisp_Object Qtool_bar_lines;
10065 Lisp_Object frame;
10066 int old_height = WINDOW_TOTAL_LINES (w);
10067 int nrows;
10068 int nlines = tool_bar_lines_needed (f, &nrows);
10070 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10071 && !f->minimize_tool_bar_window_p)
10072 ? (nlines > old_height)
10073 : (nlines != old_height));
10074 f->minimize_tool_bar_window_p = 0;
10076 if (change_height_p)
10078 XSETFRAME (frame, f);
10079 Fmodify_frame_parameters (frame,
10080 Fcons (Fcons (Qtool_bar_lines,
10081 make_number (nlines)),
10082 Qnil));
10083 if (WINDOW_TOTAL_LINES (w) != old_height)
10085 clear_glyph_matrix (w->desired_matrix);
10086 f->n_tool_bar_rows = nrows;
10087 fonts_changed_p = 1;
10088 return 1;
10094 f->minimize_tool_bar_window_p = 0;
10095 return 0;
10099 /* Get information about the tool-bar item which is displayed in GLYPH
10100 on frame F. Return in *PROP_IDX the index where tool-bar item
10101 properties start in F->tool_bar_items. Value is zero if
10102 GLYPH doesn't display a tool-bar item. */
10104 static int
10105 tool_bar_item_info (f, glyph, prop_idx)
10106 struct frame *f;
10107 struct glyph *glyph;
10108 int *prop_idx;
10110 Lisp_Object prop;
10111 int success_p;
10112 int charpos;
10114 /* This function can be called asynchronously, which means we must
10115 exclude any possibility that Fget_text_property signals an
10116 error. */
10117 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10118 charpos = max (0, charpos);
10120 /* Get the text property `menu-item' at pos. The value of that
10121 property is the start index of this item's properties in
10122 F->tool_bar_items. */
10123 prop = Fget_text_property (make_number (charpos),
10124 Qmenu_item, f->current_tool_bar_string);
10125 if (INTEGERP (prop))
10127 *prop_idx = XINT (prop);
10128 success_p = 1;
10130 else
10131 success_p = 0;
10133 return success_p;
10137 /* Get information about the tool-bar item at position X/Y on frame F.
10138 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10139 the current matrix of the tool-bar window of F, or NULL if not
10140 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10141 item in F->tool_bar_items. Value is
10143 -1 if X/Y is not on a tool-bar item
10144 0 if X/Y is on the same item that was highlighted before.
10145 1 otherwise. */
10147 static int
10148 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10149 struct frame *f;
10150 int x, y;
10151 struct glyph **glyph;
10152 int *hpos, *vpos, *prop_idx;
10154 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10155 struct window *w = XWINDOW (f->tool_bar_window);
10156 int area;
10158 /* Find the glyph under X/Y. */
10159 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10160 if (*glyph == NULL)
10161 return -1;
10163 /* Get the start of this tool-bar item's properties in
10164 f->tool_bar_items. */
10165 if (!tool_bar_item_info (f, *glyph, prop_idx))
10166 return -1;
10168 /* Is mouse on the highlighted item? */
10169 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10170 && *vpos >= dpyinfo->mouse_face_beg_row
10171 && *vpos <= dpyinfo->mouse_face_end_row
10172 && (*vpos > dpyinfo->mouse_face_beg_row
10173 || *hpos >= dpyinfo->mouse_face_beg_col)
10174 && (*vpos < dpyinfo->mouse_face_end_row
10175 || *hpos < dpyinfo->mouse_face_end_col
10176 || dpyinfo->mouse_face_past_end))
10177 return 0;
10179 return 1;
10183 /* EXPORT:
10184 Handle mouse button event on the tool-bar of frame F, at
10185 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10186 0 for button release. MODIFIERS is event modifiers for button
10187 release. */
10189 void
10190 handle_tool_bar_click (f, x, y, down_p, modifiers)
10191 struct frame *f;
10192 int x, y, down_p;
10193 unsigned int modifiers;
10195 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10196 struct window *w = XWINDOW (f->tool_bar_window);
10197 int hpos, vpos, prop_idx;
10198 struct glyph *glyph;
10199 Lisp_Object enabled_p;
10201 /* If not on the highlighted tool-bar item, return. */
10202 frame_to_window_pixel_xy (w, &x, &y);
10203 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10204 return;
10206 /* If item is disabled, do nothing. */
10207 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10208 if (NILP (enabled_p))
10209 return;
10211 if (down_p)
10213 /* Show item in pressed state. */
10214 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10215 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10216 last_tool_bar_item = prop_idx;
10218 else
10220 Lisp_Object key, frame;
10221 struct input_event event;
10222 EVENT_INIT (event);
10224 /* Show item in released state. */
10225 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10226 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10228 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10230 XSETFRAME (frame, f);
10231 event.kind = TOOL_BAR_EVENT;
10232 event.frame_or_window = frame;
10233 event.arg = frame;
10234 kbd_buffer_store_event (&event);
10236 event.kind = TOOL_BAR_EVENT;
10237 event.frame_or_window = frame;
10238 event.arg = key;
10239 event.modifiers = modifiers;
10240 kbd_buffer_store_event (&event);
10241 last_tool_bar_item = -1;
10246 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10247 tool-bar window-relative coordinates X/Y. Called from
10248 note_mouse_highlight. */
10250 static void
10251 note_tool_bar_highlight (f, x, y)
10252 struct frame *f;
10253 int x, y;
10255 Lisp_Object window = f->tool_bar_window;
10256 struct window *w = XWINDOW (window);
10257 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10258 int hpos, vpos;
10259 struct glyph *glyph;
10260 struct glyph_row *row;
10261 int i;
10262 Lisp_Object enabled_p;
10263 int prop_idx;
10264 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10265 int mouse_down_p, rc;
10267 /* Function note_mouse_highlight is called with negative x(y
10268 values when mouse moves outside of the frame. */
10269 if (x <= 0 || y <= 0)
10271 clear_mouse_face (dpyinfo);
10272 return;
10275 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10276 if (rc < 0)
10278 /* Not on tool-bar item. */
10279 clear_mouse_face (dpyinfo);
10280 return;
10282 else if (rc == 0)
10283 /* On same tool-bar item as before. */
10284 goto set_help_echo;
10286 clear_mouse_face (dpyinfo);
10288 /* Mouse is down, but on different tool-bar item? */
10289 mouse_down_p = (dpyinfo->grabbed
10290 && f == last_mouse_frame
10291 && FRAME_LIVE_P (f));
10292 if (mouse_down_p
10293 && last_tool_bar_item != prop_idx)
10294 return;
10296 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10297 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10299 /* If tool-bar item is not enabled, don't highlight it. */
10300 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10301 if (!NILP (enabled_p))
10303 /* Compute the x-position of the glyph. In front and past the
10304 image is a space. We include this in the highlighted area. */
10305 row = MATRIX_ROW (w->current_matrix, vpos);
10306 for (i = x = 0; i < hpos; ++i)
10307 x += row->glyphs[TEXT_AREA][i].pixel_width;
10309 /* Record this as the current active region. */
10310 dpyinfo->mouse_face_beg_col = hpos;
10311 dpyinfo->mouse_face_beg_row = vpos;
10312 dpyinfo->mouse_face_beg_x = x;
10313 dpyinfo->mouse_face_beg_y = row->y;
10314 dpyinfo->mouse_face_past_end = 0;
10316 dpyinfo->mouse_face_end_col = hpos + 1;
10317 dpyinfo->mouse_face_end_row = vpos;
10318 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10319 dpyinfo->mouse_face_end_y = row->y;
10320 dpyinfo->mouse_face_window = window;
10321 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10323 /* Display it as active. */
10324 show_mouse_face (dpyinfo, draw);
10325 dpyinfo->mouse_face_image_state = draw;
10328 set_help_echo:
10330 /* Set help_echo_string to a help string to display for this tool-bar item.
10331 XTread_socket does the rest. */
10332 help_echo_object = help_echo_window = Qnil;
10333 help_echo_pos = -1;
10334 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10335 if (NILP (help_echo_string))
10336 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10339 #endif /* HAVE_WINDOW_SYSTEM */
10343 /************************************************************************
10344 Horizontal scrolling
10345 ************************************************************************/
10347 static int hscroll_window_tree P_ ((Lisp_Object));
10348 static int hscroll_windows P_ ((Lisp_Object));
10350 /* For all leaf windows in the window tree rooted at WINDOW, set their
10351 hscroll value so that PT is (i) visible in the window, and (ii) so
10352 that it is not within a certain margin at the window's left and
10353 right border. Value is non-zero if any window's hscroll has been
10354 changed. */
10356 static int
10357 hscroll_window_tree (window)
10358 Lisp_Object window;
10360 int hscrolled_p = 0;
10361 int hscroll_relative_p = FLOATP (Vhscroll_step);
10362 int hscroll_step_abs = 0;
10363 double hscroll_step_rel = 0;
10365 if (hscroll_relative_p)
10367 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10368 if (hscroll_step_rel < 0)
10370 hscroll_relative_p = 0;
10371 hscroll_step_abs = 0;
10374 else if (INTEGERP (Vhscroll_step))
10376 hscroll_step_abs = XINT (Vhscroll_step);
10377 if (hscroll_step_abs < 0)
10378 hscroll_step_abs = 0;
10380 else
10381 hscroll_step_abs = 0;
10383 while (WINDOWP (window))
10385 struct window *w = XWINDOW (window);
10387 if (WINDOWP (w->hchild))
10388 hscrolled_p |= hscroll_window_tree (w->hchild);
10389 else if (WINDOWP (w->vchild))
10390 hscrolled_p |= hscroll_window_tree (w->vchild);
10391 else if (w->cursor.vpos >= 0)
10393 int h_margin;
10394 int text_area_width;
10395 struct glyph_row *current_cursor_row
10396 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10397 struct glyph_row *desired_cursor_row
10398 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10399 struct glyph_row *cursor_row
10400 = (desired_cursor_row->enabled_p
10401 ? desired_cursor_row
10402 : current_cursor_row);
10404 text_area_width = window_box_width (w, TEXT_AREA);
10406 /* Scroll when cursor is inside this scroll margin. */
10407 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10409 if ((XFASTINT (w->hscroll)
10410 && w->cursor.x <= h_margin)
10411 || (cursor_row->enabled_p
10412 && cursor_row->truncated_on_right_p
10413 && (w->cursor.x >= text_area_width - h_margin)))
10415 struct it it;
10416 int hscroll;
10417 struct buffer *saved_current_buffer;
10418 int pt;
10419 int wanted_x;
10421 /* Find point in a display of infinite width. */
10422 saved_current_buffer = current_buffer;
10423 current_buffer = XBUFFER (w->buffer);
10425 if (w == XWINDOW (selected_window))
10426 pt = BUF_PT (current_buffer);
10427 else
10429 pt = marker_position (w->pointm);
10430 pt = max (BEGV, pt);
10431 pt = min (ZV, pt);
10434 /* Move iterator to pt starting at cursor_row->start in
10435 a line with infinite width. */
10436 init_to_row_start (&it, w, cursor_row);
10437 it.last_visible_x = INFINITY;
10438 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10439 current_buffer = saved_current_buffer;
10441 /* Position cursor in window. */
10442 if (!hscroll_relative_p && hscroll_step_abs == 0)
10443 hscroll = max (0, (it.current_x
10444 - (ITERATOR_AT_END_OF_LINE_P (&it)
10445 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10446 : (text_area_width / 2))))
10447 / FRAME_COLUMN_WIDTH (it.f);
10448 else if (w->cursor.x >= text_area_width - h_margin)
10450 if (hscroll_relative_p)
10451 wanted_x = text_area_width * (1 - hscroll_step_rel)
10452 - h_margin;
10453 else
10454 wanted_x = text_area_width
10455 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10456 - h_margin;
10457 hscroll
10458 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10460 else
10462 if (hscroll_relative_p)
10463 wanted_x = text_area_width * hscroll_step_rel
10464 + h_margin;
10465 else
10466 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10467 + h_margin;
10468 hscroll
10469 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10471 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10473 /* Don't call Fset_window_hscroll if value hasn't
10474 changed because it will prevent redisplay
10475 optimizations. */
10476 if (XFASTINT (w->hscroll) != hscroll)
10478 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10479 w->hscroll = make_number (hscroll);
10480 hscrolled_p = 1;
10485 window = w->next;
10488 /* Value is non-zero if hscroll of any leaf window has been changed. */
10489 return hscrolled_p;
10493 /* Set hscroll so that cursor is visible and not inside horizontal
10494 scroll margins for all windows in the tree rooted at WINDOW. See
10495 also hscroll_window_tree above. Value is non-zero if any window's
10496 hscroll has been changed. If it has, desired matrices on the frame
10497 of WINDOW are cleared. */
10499 static int
10500 hscroll_windows (window)
10501 Lisp_Object window;
10503 int hscrolled_p;
10505 if (automatic_hscrolling_p)
10507 hscrolled_p = hscroll_window_tree (window);
10508 if (hscrolled_p)
10509 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10511 else
10512 hscrolled_p = 0;
10513 return hscrolled_p;
10518 /************************************************************************
10519 Redisplay
10520 ************************************************************************/
10522 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10523 to a non-zero value. This is sometimes handy to have in a debugger
10524 session. */
10526 #if GLYPH_DEBUG
10528 /* First and last unchanged row for try_window_id. */
10530 int debug_first_unchanged_at_end_vpos;
10531 int debug_last_unchanged_at_beg_vpos;
10533 /* Delta vpos and y. */
10535 int debug_dvpos, debug_dy;
10537 /* Delta in characters and bytes for try_window_id. */
10539 int debug_delta, debug_delta_bytes;
10541 /* Values of window_end_pos and window_end_vpos at the end of
10542 try_window_id. */
10544 EMACS_INT debug_end_pos, debug_end_vpos;
10546 /* Append a string to W->desired_matrix->method. FMT is a printf
10547 format string. A1...A9 are a supplement for a variable-length
10548 argument list. If trace_redisplay_p is non-zero also printf the
10549 resulting string to stderr. */
10551 static void
10552 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10553 struct window *w;
10554 char *fmt;
10555 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10557 char buffer[512];
10558 char *method = w->desired_matrix->method;
10559 int len = strlen (method);
10560 int size = sizeof w->desired_matrix->method;
10561 int remaining = size - len - 1;
10563 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10564 if (len && remaining)
10566 method[len] = '|';
10567 --remaining, ++len;
10570 strncpy (method + len, buffer, remaining);
10572 if (trace_redisplay_p)
10573 fprintf (stderr, "%p (%s): %s\n",
10575 ((BUFFERP (w->buffer)
10576 && STRINGP (XBUFFER (w->buffer)->name))
10577 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10578 : "no buffer"),
10579 buffer);
10582 #endif /* GLYPH_DEBUG */
10585 /* Value is non-zero if all changes in window W, which displays
10586 current_buffer, are in the text between START and END. START is a
10587 buffer position, END is given as a distance from Z. Used in
10588 redisplay_internal for display optimization. */
10590 static INLINE int
10591 text_outside_line_unchanged_p (w, start, end)
10592 struct window *w;
10593 int start, end;
10595 int unchanged_p = 1;
10597 /* If text or overlays have changed, see where. */
10598 if (XFASTINT (w->last_modified) < MODIFF
10599 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10601 /* Gap in the line? */
10602 if (GPT < start || Z - GPT < end)
10603 unchanged_p = 0;
10605 /* Changes start in front of the line, or end after it? */
10606 if (unchanged_p
10607 && (BEG_UNCHANGED < start - 1
10608 || END_UNCHANGED < end))
10609 unchanged_p = 0;
10611 /* If selective display, can't optimize if changes start at the
10612 beginning of the line. */
10613 if (unchanged_p
10614 && INTEGERP (current_buffer->selective_display)
10615 && XINT (current_buffer->selective_display) > 0
10616 && (BEG_UNCHANGED < start || GPT <= start))
10617 unchanged_p = 0;
10619 /* If there are overlays at the start or end of the line, these
10620 may have overlay strings with newlines in them. A change at
10621 START, for instance, may actually concern the display of such
10622 overlay strings as well, and they are displayed on different
10623 lines. So, quickly rule out this case. (For the future, it
10624 might be desirable to implement something more telling than
10625 just BEG/END_UNCHANGED.) */
10626 if (unchanged_p)
10628 if (BEG + BEG_UNCHANGED == start
10629 && overlay_touches_p (start))
10630 unchanged_p = 0;
10631 if (END_UNCHANGED == end
10632 && overlay_touches_p (Z - end))
10633 unchanged_p = 0;
10637 return unchanged_p;
10641 /* Do a frame update, taking possible shortcuts into account. This is
10642 the main external entry point for redisplay.
10644 If the last redisplay displayed an echo area message and that message
10645 is no longer requested, we clear the echo area or bring back the
10646 mini-buffer if that is in use. */
10648 void
10649 redisplay ()
10651 redisplay_internal (0);
10655 static Lisp_Object
10656 overlay_arrow_string_or_property (var)
10657 Lisp_Object var;
10659 Lisp_Object val;
10661 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10662 return val;
10664 return Voverlay_arrow_string;
10667 /* Return 1 if there are any overlay-arrows in current_buffer. */
10668 static int
10669 overlay_arrow_in_current_buffer_p ()
10671 Lisp_Object vlist;
10673 for (vlist = Voverlay_arrow_variable_list;
10674 CONSP (vlist);
10675 vlist = XCDR (vlist))
10677 Lisp_Object var = XCAR (vlist);
10678 Lisp_Object val;
10680 if (!SYMBOLP (var))
10681 continue;
10682 val = find_symbol_value (var);
10683 if (MARKERP (val)
10684 && current_buffer == XMARKER (val)->buffer)
10685 return 1;
10687 return 0;
10691 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10692 has changed. */
10694 static int
10695 overlay_arrows_changed_p ()
10697 Lisp_Object vlist;
10699 for (vlist = Voverlay_arrow_variable_list;
10700 CONSP (vlist);
10701 vlist = XCDR (vlist))
10703 Lisp_Object var = XCAR (vlist);
10704 Lisp_Object val, pstr;
10706 if (!SYMBOLP (var))
10707 continue;
10708 val = find_symbol_value (var);
10709 if (!MARKERP (val))
10710 continue;
10711 if (! EQ (COERCE_MARKER (val),
10712 Fget (var, Qlast_arrow_position))
10713 || ! (pstr = overlay_arrow_string_or_property (var),
10714 EQ (pstr, Fget (var, Qlast_arrow_string))))
10715 return 1;
10717 return 0;
10720 /* Mark overlay arrows to be updated on next redisplay. */
10722 static void
10723 update_overlay_arrows (up_to_date)
10724 int up_to_date;
10726 Lisp_Object vlist;
10728 for (vlist = Voverlay_arrow_variable_list;
10729 CONSP (vlist);
10730 vlist = XCDR (vlist))
10732 Lisp_Object var = XCAR (vlist);
10734 if (!SYMBOLP (var))
10735 continue;
10737 if (up_to_date > 0)
10739 Lisp_Object val = find_symbol_value (var);
10740 Fput (var, Qlast_arrow_position,
10741 COERCE_MARKER (val));
10742 Fput (var, Qlast_arrow_string,
10743 overlay_arrow_string_or_property (var));
10745 else if (up_to_date < 0
10746 || !NILP (Fget (var, Qlast_arrow_position)))
10748 Fput (var, Qlast_arrow_position, Qt);
10749 Fput (var, Qlast_arrow_string, Qt);
10755 /* Return overlay arrow string to display at row.
10756 Return integer (bitmap number) for arrow bitmap in left fringe.
10757 Return nil if no overlay arrow. */
10759 static Lisp_Object
10760 overlay_arrow_at_row (it, row)
10761 struct it *it;
10762 struct glyph_row *row;
10764 Lisp_Object vlist;
10766 for (vlist = Voverlay_arrow_variable_list;
10767 CONSP (vlist);
10768 vlist = XCDR (vlist))
10770 Lisp_Object var = XCAR (vlist);
10771 Lisp_Object val;
10773 if (!SYMBOLP (var))
10774 continue;
10776 val = find_symbol_value (var);
10778 if (MARKERP (val)
10779 && current_buffer == XMARKER (val)->buffer
10780 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10782 if (FRAME_WINDOW_P (it->f)
10783 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10785 #ifdef HAVE_WINDOW_SYSTEM
10786 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10788 int fringe_bitmap;
10789 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10790 return make_number (fringe_bitmap);
10792 #endif
10793 return make_number (-1); /* Use default arrow bitmap */
10795 return overlay_arrow_string_or_property (var);
10799 return Qnil;
10802 /* Return 1 if point moved out of or into a composition. Otherwise
10803 return 0. PREV_BUF and PREV_PT are the last point buffer and
10804 position. BUF and PT are the current point buffer and position. */
10807 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10808 struct buffer *prev_buf, *buf;
10809 int prev_pt, pt;
10811 int start, end;
10812 Lisp_Object prop;
10813 Lisp_Object buffer;
10815 XSETBUFFER (buffer, buf);
10816 /* Check a composition at the last point if point moved within the
10817 same buffer. */
10818 if (prev_buf == buf)
10820 if (prev_pt == pt)
10821 /* Point didn't move. */
10822 return 0;
10824 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10825 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10826 && COMPOSITION_VALID_P (start, end, prop)
10827 && start < prev_pt && end > prev_pt)
10828 /* The last point was within the composition. Return 1 iff
10829 point moved out of the composition. */
10830 return (pt <= start || pt >= end);
10833 /* Check a composition at the current point. */
10834 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10835 && find_composition (pt, -1, &start, &end, &prop, buffer)
10836 && COMPOSITION_VALID_P (start, end, prop)
10837 && start < pt && end > pt);
10841 /* Reconsider the setting of B->clip_changed which is displayed
10842 in window W. */
10844 static INLINE void
10845 reconsider_clip_changes (w, b)
10846 struct window *w;
10847 struct buffer *b;
10849 if (b->clip_changed
10850 && !NILP (w->window_end_valid)
10851 && w->current_matrix->buffer == b
10852 && w->current_matrix->zv == BUF_ZV (b)
10853 && w->current_matrix->begv == BUF_BEGV (b))
10854 b->clip_changed = 0;
10856 /* If display wasn't paused, and W is not a tool bar window, see if
10857 point has been moved into or out of a composition. In that case,
10858 we set b->clip_changed to 1 to force updating the screen. If
10859 b->clip_changed has already been set to 1, we can skip this
10860 check. */
10861 if (!b->clip_changed
10862 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10864 int pt;
10866 if (w == XWINDOW (selected_window))
10867 pt = BUF_PT (current_buffer);
10868 else
10869 pt = marker_position (w->pointm);
10871 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10872 || pt != XINT (w->last_point))
10873 && check_point_in_composition (w->current_matrix->buffer,
10874 XINT (w->last_point),
10875 XBUFFER (w->buffer), pt))
10876 b->clip_changed = 1;
10881 /* Select FRAME to forward the values of frame-local variables into C
10882 variables so that the redisplay routines can access those values
10883 directly. */
10885 static void
10886 select_frame_for_redisplay (frame)
10887 Lisp_Object frame;
10889 Lisp_Object tail, sym, val;
10890 Lisp_Object old = selected_frame;
10892 selected_frame = frame;
10894 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10895 if (CONSP (XCAR (tail))
10896 && (sym = XCAR (XCAR (tail)),
10897 SYMBOLP (sym))
10898 && (sym = indirect_variable (sym),
10899 val = SYMBOL_VALUE (sym),
10900 (BUFFER_LOCAL_VALUEP (val)
10901 || SOME_BUFFER_LOCAL_VALUEP (val)))
10902 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10903 /* Use find_symbol_value rather than Fsymbol_value
10904 to avoid an error if it is void. */
10905 find_symbol_value (sym);
10907 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10908 if (CONSP (XCAR (tail))
10909 && (sym = XCAR (XCAR (tail)),
10910 SYMBOLP (sym))
10911 && (sym = indirect_variable (sym),
10912 val = SYMBOL_VALUE (sym),
10913 (BUFFER_LOCAL_VALUEP (val)
10914 || SOME_BUFFER_LOCAL_VALUEP (val)))
10915 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10916 find_symbol_value (sym);
10920 #define STOP_POLLING \
10921 do { if (! polling_stopped_here) stop_polling (); \
10922 polling_stopped_here = 1; } while (0)
10924 #define RESUME_POLLING \
10925 do { if (polling_stopped_here) start_polling (); \
10926 polling_stopped_here = 0; } while (0)
10929 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10930 response to any user action; therefore, we should preserve the echo
10931 area. (Actually, our caller does that job.) Perhaps in the future
10932 avoid recentering windows if it is not necessary; currently that
10933 causes some problems. */
10935 static void
10936 redisplay_internal (preserve_echo_area)
10937 int preserve_echo_area;
10939 struct window *w = XWINDOW (selected_window);
10940 struct frame *f;
10941 int pause;
10942 int must_finish = 0;
10943 struct text_pos tlbufpos, tlendpos;
10944 int number_of_visible_frames;
10945 int count, count1;
10946 struct frame *sf;
10947 int polling_stopped_here = 0;
10949 /* Non-zero means redisplay has to consider all windows on all
10950 frames. Zero means, only selected_window is considered. */
10951 int consider_all_windows_p;
10953 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10955 /* No redisplay if running in batch mode or frame is not yet fully
10956 initialized, or redisplay is explicitly turned off by setting
10957 Vinhibit_redisplay. */
10958 if (noninteractive
10959 || !NILP (Vinhibit_redisplay))
10960 return;
10962 /* Don't examine these until after testing Vinhibit_redisplay.
10963 When Emacs is shutting down, perhaps because its connection to
10964 X has dropped, we should not look at them at all. */
10965 f = XFRAME (w->frame);
10966 sf = SELECTED_FRAME ();
10968 if (!f->glyphs_initialized_p)
10969 return;
10971 /* The flag redisplay_performed_directly_p is set by
10972 direct_output_for_insert when it already did the whole screen
10973 update necessary. */
10974 if (redisplay_performed_directly_p)
10976 redisplay_performed_directly_p = 0;
10977 if (!hscroll_windows (selected_window))
10978 return;
10981 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10982 if (popup_activated ())
10983 return;
10984 #endif
10986 /* I don't think this happens but let's be paranoid. */
10987 if (redisplaying_p)
10988 return;
10990 /* Record a function that resets redisplaying_p to its old value
10991 when we leave this function. */
10992 count = SPECPDL_INDEX ();
10993 record_unwind_protect (unwind_redisplay,
10994 Fcons (make_number (redisplaying_p), selected_frame));
10995 ++redisplaying_p;
10996 specbind (Qinhibit_free_realized_faces, Qnil);
10999 Lisp_Object tail, frame;
11001 FOR_EACH_FRAME (tail, frame)
11003 struct frame *f = XFRAME (frame);
11004 f->already_hscrolled_p = 0;
11008 retry:
11009 pause = 0;
11010 reconsider_clip_changes (w, current_buffer);
11011 last_escape_glyph_frame = NULL;
11012 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11014 /* If new fonts have been loaded that make a glyph matrix adjustment
11015 necessary, do it. */
11016 if (fonts_changed_p)
11018 adjust_glyphs (NULL);
11019 ++windows_or_buffers_changed;
11020 fonts_changed_p = 0;
11023 /* If face_change_count is non-zero, init_iterator will free all
11024 realized faces, which includes the faces referenced from current
11025 matrices. So, we can't reuse current matrices in this case. */
11026 if (face_change_count)
11027 ++windows_or_buffers_changed;
11029 if (! FRAME_WINDOW_P (sf)
11030 && previous_terminal_frame != sf)
11032 /* Since frames on an ASCII terminal share the same display
11033 area, displaying a different frame means redisplay the whole
11034 thing. */
11035 windows_or_buffers_changed++;
11036 SET_FRAME_GARBAGED (sf);
11037 XSETFRAME (Vterminal_frame, sf);
11039 previous_terminal_frame = sf;
11041 /* Set the visible flags for all frames. Do this before checking
11042 for resized or garbaged frames; they want to know if their frames
11043 are visible. See the comment in frame.h for
11044 FRAME_SAMPLE_VISIBILITY. */
11046 Lisp_Object tail, frame;
11048 number_of_visible_frames = 0;
11050 FOR_EACH_FRAME (tail, frame)
11052 struct frame *f = XFRAME (frame);
11054 FRAME_SAMPLE_VISIBILITY (f);
11055 if (FRAME_VISIBLE_P (f))
11056 ++number_of_visible_frames;
11057 clear_desired_matrices (f);
11061 /* Notice any pending interrupt request to change frame size. */
11062 do_pending_window_change (1);
11064 /* Clear frames marked as garbaged. */
11065 if (frame_garbaged)
11066 clear_garbaged_frames ();
11068 /* Build menubar and tool-bar items. */
11069 if (NILP (Vmemory_full))
11070 prepare_menu_bars ();
11072 if (windows_or_buffers_changed)
11073 update_mode_lines++;
11075 /* Detect case that we need to write or remove a star in the mode line. */
11076 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11078 w->update_mode_line = Qt;
11079 if (buffer_shared > 1)
11080 update_mode_lines++;
11083 /* Avoid invocation of point motion hooks by `current_column' below. */
11084 count1 = SPECPDL_INDEX ();
11085 specbind (Qinhibit_point_motion_hooks, Qt);
11087 /* If %c is in the mode line, update it if needed. */
11088 if (!NILP (w->column_number_displayed)
11089 /* This alternative quickly identifies a common case
11090 where no change is needed. */
11091 && !(PT == XFASTINT (w->last_point)
11092 && XFASTINT (w->last_modified) >= MODIFF
11093 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11094 && (XFASTINT (w->column_number_displayed)
11095 != (int) current_column ())) /* iftc */
11096 w->update_mode_line = Qt;
11098 unbind_to (count1, Qnil);
11100 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11102 /* The variable buffer_shared is set in redisplay_window and
11103 indicates that we redisplay a buffer in different windows. See
11104 there. */
11105 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11106 || cursor_type_changed);
11108 /* If specs for an arrow have changed, do thorough redisplay
11109 to ensure we remove any arrow that should no longer exist. */
11110 if (overlay_arrows_changed_p ())
11111 consider_all_windows_p = windows_or_buffers_changed = 1;
11113 /* Normally the message* functions will have already displayed and
11114 updated the echo area, but the frame may have been trashed, or
11115 the update may have been preempted, so display the echo area
11116 again here. Checking message_cleared_p captures the case that
11117 the echo area should be cleared. */
11118 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11119 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11120 || (message_cleared_p
11121 && minibuf_level == 0
11122 /* If the mini-window is currently selected, this means the
11123 echo-area doesn't show through. */
11124 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11126 int window_height_changed_p = echo_area_display (0);
11127 must_finish = 1;
11129 /* If we don't display the current message, don't clear the
11130 message_cleared_p flag, because, if we did, we wouldn't clear
11131 the echo area in the next redisplay which doesn't preserve
11132 the echo area. */
11133 if (!display_last_displayed_message_p)
11134 message_cleared_p = 0;
11136 if (fonts_changed_p)
11137 goto retry;
11138 else if (window_height_changed_p)
11140 consider_all_windows_p = 1;
11141 ++update_mode_lines;
11142 ++windows_or_buffers_changed;
11144 /* If window configuration was changed, frames may have been
11145 marked garbaged. Clear them or we will experience
11146 surprises wrt scrolling. */
11147 if (frame_garbaged)
11148 clear_garbaged_frames ();
11151 else if (EQ (selected_window, minibuf_window)
11152 && (current_buffer->clip_changed
11153 || XFASTINT (w->last_modified) < MODIFF
11154 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11155 && resize_mini_window (w, 0))
11157 /* Resized active mini-window to fit the size of what it is
11158 showing if its contents might have changed. */
11159 must_finish = 1;
11160 consider_all_windows_p = 1;
11161 ++windows_or_buffers_changed;
11162 ++update_mode_lines;
11164 /* If window configuration was changed, frames may have been
11165 marked garbaged. Clear them or we will experience
11166 surprises wrt scrolling. */
11167 if (frame_garbaged)
11168 clear_garbaged_frames ();
11172 /* If showing the region, and mark has changed, we must redisplay
11173 the whole window. The assignment to this_line_start_pos prevents
11174 the optimization directly below this if-statement. */
11175 if (((!NILP (Vtransient_mark_mode)
11176 && !NILP (XBUFFER (w->buffer)->mark_active))
11177 != !NILP (w->region_showing))
11178 || (!NILP (w->region_showing)
11179 && !EQ (w->region_showing,
11180 Fmarker_position (XBUFFER (w->buffer)->mark))))
11181 CHARPOS (this_line_start_pos) = 0;
11183 /* Optimize the case that only the line containing the cursor in the
11184 selected window has changed. Variables starting with this_ are
11185 set in display_line and record information about the line
11186 containing the cursor. */
11187 tlbufpos = this_line_start_pos;
11188 tlendpos = this_line_end_pos;
11189 if (!consider_all_windows_p
11190 && CHARPOS (tlbufpos) > 0
11191 && NILP (w->update_mode_line)
11192 && !current_buffer->clip_changed
11193 && !current_buffer->prevent_redisplay_optimizations_p
11194 && FRAME_VISIBLE_P (XFRAME (w->frame))
11195 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11196 /* Make sure recorded data applies to current buffer, etc. */
11197 && this_line_buffer == current_buffer
11198 && current_buffer == XBUFFER (w->buffer)
11199 && NILP (w->force_start)
11200 && NILP (w->optional_new_start)
11201 /* Point must be on the line that we have info recorded about. */
11202 && PT >= CHARPOS (tlbufpos)
11203 && PT <= Z - CHARPOS (tlendpos)
11204 /* All text outside that line, including its final newline,
11205 must be unchanged */
11206 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11207 CHARPOS (tlendpos)))
11209 if (CHARPOS (tlbufpos) > BEGV
11210 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11211 && (CHARPOS (tlbufpos) == ZV
11212 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11213 /* Former continuation line has disappeared by becoming empty */
11214 goto cancel;
11215 else if (XFASTINT (w->last_modified) < MODIFF
11216 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11217 || MINI_WINDOW_P (w))
11219 /* We have to handle the case of continuation around a
11220 wide-column character (See the comment in indent.c around
11221 line 885).
11223 For instance, in the following case:
11225 -------- Insert --------
11226 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11227 J_I_ ==> J_I_ `^^' are cursors.
11228 ^^ ^^
11229 -------- --------
11231 As we have to redraw the line above, we should goto cancel. */
11233 struct it it;
11234 int line_height_before = this_line_pixel_height;
11236 /* Note that start_display will handle the case that the
11237 line starting at tlbufpos is a continuation lines. */
11238 start_display (&it, w, tlbufpos);
11240 /* Implementation note: It this still necessary? */
11241 if (it.current_x != this_line_start_x)
11242 goto cancel;
11244 TRACE ((stderr, "trying display optimization 1\n"));
11245 w->cursor.vpos = -1;
11246 overlay_arrow_seen = 0;
11247 it.vpos = this_line_vpos;
11248 it.current_y = this_line_y;
11249 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11250 display_line (&it);
11252 /* If line contains point, is not continued,
11253 and ends at same distance from eob as before, we win */
11254 if (w->cursor.vpos >= 0
11255 /* Line is not continued, otherwise this_line_start_pos
11256 would have been set to 0 in display_line. */
11257 && CHARPOS (this_line_start_pos)
11258 /* Line ends as before. */
11259 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11260 /* Line has same height as before. Otherwise other lines
11261 would have to be shifted up or down. */
11262 && this_line_pixel_height == line_height_before)
11264 /* If this is not the window's last line, we must adjust
11265 the charstarts of the lines below. */
11266 if (it.current_y < it.last_visible_y)
11268 struct glyph_row *row
11269 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11270 int delta, delta_bytes;
11272 if (Z - CHARPOS (tlendpos) == ZV)
11274 /* This line ends at end of (accessible part of)
11275 buffer. There is no newline to count. */
11276 delta = (Z
11277 - CHARPOS (tlendpos)
11278 - MATRIX_ROW_START_CHARPOS (row));
11279 delta_bytes = (Z_BYTE
11280 - BYTEPOS (tlendpos)
11281 - MATRIX_ROW_START_BYTEPOS (row));
11283 else
11285 /* This line ends in a newline. Must take
11286 account of the newline and the rest of the
11287 text that follows. */
11288 delta = (Z
11289 - CHARPOS (tlendpos)
11290 - MATRIX_ROW_START_CHARPOS (row));
11291 delta_bytes = (Z_BYTE
11292 - BYTEPOS (tlendpos)
11293 - MATRIX_ROW_START_BYTEPOS (row));
11296 increment_matrix_positions (w->current_matrix,
11297 this_line_vpos + 1,
11298 w->current_matrix->nrows,
11299 delta, delta_bytes);
11302 /* If this row displays text now but previously didn't,
11303 or vice versa, w->window_end_vpos may have to be
11304 adjusted. */
11305 if ((it.glyph_row - 1)->displays_text_p)
11307 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11308 XSETINT (w->window_end_vpos, this_line_vpos);
11310 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11311 && this_line_vpos > 0)
11312 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11313 w->window_end_valid = Qnil;
11315 /* Update hint: No need to try to scroll in update_window. */
11316 w->desired_matrix->no_scrolling_p = 1;
11318 #if GLYPH_DEBUG
11319 *w->desired_matrix->method = 0;
11320 debug_method_add (w, "optimization 1");
11321 #endif
11322 #ifdef HAVE_WINDOW_SYSTEM
11323 update_window_fringes (w, 0);
11324 #endif
11325 goto update;
11327 else
11328 goto cancel;
11330 else if (/* Cursor position hasn't changed. */
11331 PT == XFASTINT (w->last_point)
11332 /* Make sure the cursor was last displayed
11333 in this window. Otherwise we have to reposition it. */
11334 && 0 <= w->cursor.vpos
11335 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11337 if (!must_finish)
11339 do_pending_window_change (1);
11341 /* We used to always goto end_of_redisplay here, but this
11342 isn't enough if we have a blinking cursor. */
11343 if (w->cursor_off_p == w->last_cursor_off_p)
11344 goto end_of_redisplay;
11346 goto update;
11348 /* If highlighting the region, or if the cursor is in the echo area,
11349 then we can't just move the cursor. */
11350 else if (! (!NILP (Vtransient_mark_mode)
11351 && !NILP (current_buffer->mark_active))
11352 && (EQ (selected_window, current_buffer->last_selected_window)
11353 || highlight_nonselected_windows)
11354 && NILP (w->region_showing)
11355 && NILP (Vshow_trailing_whitespace)
11356 && !cursor_in_echo_area)
11358 struct it it;
11359 struct glyph_row *row;
11361 /* Skip from tlbufpos to PT and see where it is. Note that
11362 PT may be in invisible text. If so, we will end at the
11363 next visible position. */
11364 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11365 NULL, DEFAULT_FACE_ID);
11366 it.current_x = this_line_start_x;
11367 it.current_y = this_line_y;
11368 it.vpos = this_line_vpos;
11370 /* The call to move_it_to stops in front of PT, but
11371 moves over before-strings. */
11372 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11374 if (it.vpos == this_line_vpos
11375 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11376 row->enabled_p))
11378 xassert (this_line_vpos == it.vpos);
11379 xassert (this_line_y == it.current_y);
11380 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11381 #if GLYPH_DEBUG
11382 *w->desired_matrix->method = 0;
11383 debug_method_add (w, "optimization 3");
11384 #endif
11385 goto update;
11387 else
11388 goto cancel;
11391 cancel:
11392 /* Text changed drastically or point moved off of line. */
11393 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11396 CHARPOS (this_line_start_pos) = 0;
11397 consider_all_windows_p |= buffer_shared > 1;
11398 ++clear_face_cache_count;
11399 #ifdef HAVE_WINDOW_SYSTEM
11400 ++clear_image_cache_count;
11401 #endif
11403 /* Build desired matrices, and update the display. If
11404 consider_all_windows_p is non-zero, do it for all windows on all
11405 frames. Otherwise do it for selected_window, only. */
11407 if (consider_all_windows_p)
11409 Lisp_Object tail, frame;
11411 FOR_EACH_FRAME (tail, frame)
11412 XFRAME (frame)->updated_p = 0;
11414 /* Recompute # windows showing selected buffer. This will be
11415 incremented each time such a window is displayed. */
11416 buffer_shared = 0;
11418 FOR_EACH_FRAME (tail, frame)
11420 struct frame *f = XFRAME (frame);
11422 if (FRAME_WINDOW_P (f) || f == sf)
11424 if (! EQ (frame, selected_frame))
11425 /* Select the frame, for the sake of frame-local
11426 variables. */
11427 select_frame_for_redisplay (frame);
11429 /* Mark all the scroll bars to be removed; we'll redeem
11430 the ones we want when we redisplay their windows. */
11431 if (condemn_scroll_bars_hook)
11432 condemn_scroll_bars_hook (f);
11434 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11435 redisplay_windows (FRAME_ROOT_WINDOW (f));
11437 /* Any scroll bars which redisplay_windows should have
11438 nuked should now go away. */
11439 if (judge_scroll_bars_hook)
11440 judge_scroll_bars_hook (f);
11442 /* If fonts changed, display again. */
11443 /* ??? rms: I suspect it is a mistake to jump all the way
11444 back to retry here. It should just retry this frame. */
11445 if (fonts_changed_p)
11446 goto retry;
11448 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11450 /* See if we have to hscroll. */
11451 if (!f->already_hscrolled_p)
11453 f->already_hscrolled_p = 1;
11454 if (hscroll_windows (f->root_window))
11455 goto retry;
11458 /* Prevent various kinds of signals during display
11459 update. stdio is not robust about handling
11460 signals, which can cause an apparent I/O
11461 error. */
11462 if (interrupt_input)
11463 unrequest_sigio ();
11464 STOP_POLLING;
11466 /* Update the display. */
11467 set_window_update_flags (XWINDOW (f->root_window), 1);
11468 pause |= update_frame (f, 0, 0);
11469 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11470 if (pause)
11471 break;
11472 #endif
11474 f->updated_p = 1;
11479 if (!pause)
11481 /* Do the mark_window_display_accurate after all windows have
11482 been redisplayed because this call resets flags in buffers
11483 which are needed for proper redisplay. */
11484 FOR_EACH_FRAME (tail, frame)
11486 struct frame *f = XFRAME (frame);
11487 if (f->updated_p)
11489 mark_window_display_accurate (f->root_window, 1);
11490 if (frame_up_to_date_hook)
11491 frame_up_to_date_hook (f);
11496 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11498 Lisp_Object mini_window;
11499 struct frame *mini_frame;
11501 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11502 /* Use list_of_error, not Qerror, so that
11503 we catch only errors and don't run the debugger. */
11504 internal_condition_case_1 (redisplay_window_1, selected_window,
11505 list_of_error,
11506 redisplay_window_error);
11508 /* Compare desired and current matrices, perform output. */
11510 update:
11511 /* If fonts changed, display again. */
11512 if (fonts_changed_p)
11513 goto retry;
11515 /* Prevent various kinds of signals during display update.
11516 stdio is not robust about handling signals,
11517 which can cause an apparent I/O error. */
11518 if (interrupt_input)
11519 unrequest_sigio ();
11520 STOP_POLLING;
11522 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11524 if (hscroll_windows (selected_window))
11525 goto retry;
11527 XWINDOW (selected_window)->must_be_updated_p = 1;
11528 pause = update_frame (sf, 0, 0);
11531 /* We may have called echo_area_display at the top of this
11532 function. If the echo area is on another frame, that may
11533 have put text on a frame other than the selected one, so the
11534 above call to update_frame would not have caught it. Catch
11535 it here. */
11536 mini_window = FRAME_MINIBUF_WINDOW (sf);
11537 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11539 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11541 XWINDOW (mini_window)->must_be_updated_p = 1;
11542 pause |= update_frame (mini_frame, 0, 0);
11543 if (!pause && hscroll_windows (mini_window))
11544 goto retry;
11548 /* If display was paused because of pending input, make sure we do a
11549 thorough update the next time. */
11550 if (pause)
11552 /* Prevent the optimization at the beginning of
11553 redisplay_internal that tries a single-line update of the
11554 line containing the cursor in the selected window. */
11555 CHARPOS (this_line_start_pos) = 0;
11557 /* Let the overlay arrow be updated the next time. */
11558 update_overlay_arrows (0);
11560 /* If we pause after scrolling, some rows in the current
11561 matrices of some windows are not valid. */
11562 if (!WINDOW_FULL_WIDTH_P (w)
11563 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11564 update_mode_lines = 1;
11566 else
11568 if (!consider_all_windows_p)
11570 /* This has already been done above if
11571 consider_all_windows_p is set. */
11572 mark_window_display_accurate_1 (w, 1);
11574 /* Say overlay arrows are up to date. */
11575 update_overlay_arrows (1);
11577 if (frame_up_to_date_hook != 0)
11578 frame_up_to_date_hook (sf);
11581 update_mode_lines = 0;
11582 windows_or_buffers_changed = 0;
11583 cursor_type_changed = 0;
11586 /* Start SIGIO interrupts coming again. Having them off during the
11587 code above makes it less likely one will discard output, but not
11588 impossible, since there might be stuff in the system buffer here.
11589 But it is much hairier to try to do anything about that. */
11590 if (interrupt_input)
11591 request_sigio ();
11592 RESUME_POLLING;
11594 /* If a frame has become visible which was not before, redisplay
11595 again, so that we display it. Expose events for such a frame
11596 (which it gets when becoming visible) don't call the parts of
11597 redisplay constructing glyphs, so simply exposing a frame won't
11598 display anything in this case. So, we have to display these
11599 frames here explicitly. */
11600 if (!pause)
11602 Lisp_Object tail, frame;
11603 int new_count = 0;
11605 FOR_EACH_FRAME (tail, frame)
11607 int this_is_visible = 0;
11609 if (XFRAME (frame)->visible)
11610 this_is_visible = 1;
11611 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11612 if (XFRAME (frame)->visible)
11613 this_is_visible = 1;
11615 if (this_is_visible)
11616 new_count++;
11619 if (new_count != number_of_visible_frames)
11620 windows_or_buffers_changed++;
11623 /* Change frame size now if a change is pending. */
11624 do_pending_window_change (1);
11626 /* If we just did a pending size change, or have additional
11627 visible frames, redisplay again. */
11628 if (windows_or_buffers_changed && !pause)
11629 goto retry;
11631 /* Clear the face cache eventually. */
11632 if (consider_all_windows_p)
11634 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11636 clear_face_cache (0);
11637 clear_face_cache_count = 0;
11639 #ifdef HAVE_WINDOW_SYSTEM
11640 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11642 Lisp_Object tail, frame;
11643 FOR_EACH_FRAME (tail, frame)
11645 struct frame *f = XFRAME (frame);
11646 if (FRAME_WINDOW_P (f))
11647 clear_image_cache (f, 0);
11649 clear_image_cache_count = 0;
11651 #endif /* HAVE_WINDOW_SYSTEM */
11654 end_of_redisplay:
11655 unbind_to (count, Qnil);
11656 RESUME_POLLING;
11660 /* Redisplay, but leave alone any recent echo area message unless
11661 another message has been requested in its place.
11663 This is useful in situations where you need to redisplay but no
11664 user action has occurred, making it inappropriate for the message
11665 area to be cleared. See tracking_off and
11666 wait_reading_process_output for examples of these situations.
11668 FROM_WHERE is an integer saying from where this function was
11669 called. This is useful for debugging. */
11671 void
11672 redisplay_preserve_echo_area (from_where)
11673 int from_where;
11675 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11677 if (!NILP (echo_area_buffer[1]))
11679 /* We have a previously displayed message, but no current
11680 message. Redisplay the previous message. */
11681 display_last_displayed_message_p = 1;
11682 redisplay_internal (1);
11683 display_last_displayed_message_p = 0;
11685 else
11686 redisplay_internal (1);
11688 if (rif != NULL && rif->flush_display_optional)
11689 rif->flush_display_optional (NULL);
11693 /* Function registered with record_unwind_protect in
11694 redisplay_internal. Reset redisplaying_p to the value it had
11695 before redisplay_internal was called, and clear
11696 prevent_freeing_realized_faces_p. It also selects the previously
11697 selected frame. */
11699 static Lisp_Object
11700 unwind_redisplay (val)
11701 Lisp_Object val;
11703 Lisp_Object old_redisplaying_p, old_frame;
11705 old_redisplaying_p = XCAR (val);
11706 redisplaying_p = XFASTINT (old_redisplaying_p);
11707 old_frame = XCDR (val);
11708 if (! EQ (old_frame, selected_frame))
11709 select_frame_for_redisplay (old_frame);
11710 return Qnil;
11714 /* Mark the display of window W as accurate or inaccurate. If
11715 ACCURATE_P is non-zero mark display of W as accurate. If
11716 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11717 redisplay_internal is called. */
11719 static void
11720 mark_window_display_accurate_1 (w, accurate_p)
11721 struct window *w;
11722 int accurate_p;
11724 if (BUFFERP (w->buffer))
11726 struct buffer *b = XBUFFER (w->buffer);
11728 w->last_modified
11729 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11730 w->last_overlay_modified
11731 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11732 w->last_had_star
11733 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11735 if (accurate_p)
11737 b->clip_changed = 0;
11738 b->prevent_redisplay_optimizations_p = 0;
11740 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11741 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11742 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11743 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11745 w->current_matrix->buffer = b;
11746 w->current_matrix->begv = BUF_BEGV (b);
11747 w->current_matrix->zv = BUF_ZV (b);
11749 w->last_cursor = w->cursor;
11750 w->last_cursor_off_p = w->cursor_off_p;
11752 if (w == XWINDOW (selected_window))
11753 w->last_point = make_number (BUF_PT (b));
11754 else
11755 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11759 if (accurate_p)
11761 w->window_end_valid = w->buffer;
11762 #if 0 /* This is incorrect with variable-height lines. */
11763 xassert (XINT (w->window_end_vpos)
11764 < (WINDOW_TOTAL_LINES (w)
11765 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11766 #endif
11767 w->update_mode_line = Qnil;
11772 /* Mark the display of windows in the window tree rooted at WINDOW as
11773 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11774 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11775 be redisplayed the next time redisplay_internal is called. */
11777 void
11778 mark_window_display_accurate (window, accurate_p)
11779 Lisp_Object window;
11780 int accurate_p;
11782 struct window *w;
11784 for (; !NILP (window); window = w->next)
11786 w = XWINDOW (window);
11787 mark_window_display_accurate_1 (w, accurate_p);
11789 if (!NILP (w->vchild))
11790 mark_window_display_accurate (w->vchild, accurate_p);
11791 if (!NILP (w->hchild))
11792 mark_window_display_accurate (w->hchild, accurate_p);
11795 if (accurate_p)
11797 update_overlay_arrows (1);
11799 else
11801 /* Force a thorough redisplay the next time by setting
11802 last_arrow_position and last_arrow_string to t, which is
11803 unequal to any useful value of Voverlay_arrow_... */
11804 update_overlay_arrows (-1);
11809 /* Return value in display table DP (Lisp_Char_Table *) for character
11810 C. Since a display table doesn't have any parent, we don't have to
11811 follow parent. Do not call this function directly but use the
11812 macro DISP_CHAR_VECTOR. */
11814 Lisp_Object
11815 disp_char_vector (dp, c)
11816 struct Lisp_Char_Table *dp;
11817 int c;
11819 int code[4], i;
11820 Lisp_Object val;
11822 if (SINGLE_BYTE_CHAR_P (c))
11823 return (dp->contents[c]);
11825 SPLIT_CHAR (c, code[0], code[1], code[2]);
11826 if (code[1] < 32)
11827 code[1] = -1;
11828 else if (code[2] < 32)
11829 code[2] = -1;
11831 /* Here, the possible range of code[0] (== charset ID) is
11832 128..max_charset. Since the top level char table contains data
11833 for multibyte characters after 256th element, we must increment
11834 code[0] by 128 to get a correct index. */
11835 code[0] += 128;
11836 code[3] = -1; /* anchor */
11838 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11840 val = dp->contents[code[i]];
11841 if (!SUB_CHAR_TABLE_P (val))
11842 return (NILP (val) ? dp->defalt : val);
11845 /* Here, val is a sub char table. We return the default value of
11846 it. */
11847 return (dp->defalt);
11852 /***********************************************************************
11853 Window Redisplay
11854 ***********************************************************************/
11856 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11858 static void
11859 redisplay_windows (window)
11860 Lisp_Object window;
11862 while (!NILP (window))
11864 struct window *w = XWINDOW (window);
11866 if (!NILP (w->hchild))
11867 redisplay_windows (w->hchild);
11868 else if (!NILP (w->vchild))
11869 redisplay_windows (w->vchild);
11870 else
11872 displayed_buffer = XBUFFER (w->buffer);
11873 /* Use list_of_error, not Qerror, so that
11874 we catch only errors and don't run the debugger. */
11875 internal_condition_case_1 (redisplay_window_0, window,
11876 list_of_error,
11877 redisplay_window_error);
11880 window = w->next;
11884 static Lisp_Object
11885 redisplay_window_error ()
11887 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11888 return Qnil;
11891 static Lisp_Object
11892 redisplay_window_0 (window)
11893 Lisp_Object window;
11895 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11896 redisplay_window (window, 0);
11897 return Qnil;
11900 static Lisp_Object
11901 redisplay_window_1 (window)
11902 Lisp_Object window;
11904 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11905 redisplay_window (window, 1);
11906 return Qnil;
11910 /* Increment GLYPH until it reaches END or CONDITION fails while
11911 adding (GLYPH)->pixel_width to X. */
11913 #define SKIP_GLYPHS(glyph, end, x, condition) \
11914 do \
11916 (x) += (glyph)->pixel_width; \
11917 ++(glyph); \
11919 while ((glyph) < (end) && (condition))
11922 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11923 DELTA is the number of bytes by which positions recorded in ROW
11924 differ from current buffer positions.
11926 Return 0 if cursor is not on this row. 1 otherwise. */
11929 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11930 struct window *w;
11931 struct glyph_row *row;
11932 struct glyph_matrix *matrix;
11933 int delta, delta_bytes, dy, dvpos;
11935 struct glyph *glyph = row->glyphs[TEXT_AREA];
11936 struct glyph *end = glyph + row->used[TEXT_AREA];
11937 struct glyph *cursor = NULL;
11938 /* The first glyph that starts a sequence of glyphs from string. */
11939 struct glyph *string_start;
11940 /* The X coordinate of string_start. */
11941 int string_start_x;
11942 /* The last known character position. */
11943 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11944 /* The last known character position before string_start. */
11945 int string_before_pos;
11946 int x = row->x;
11947 int cursor_x = x;
11948 int cursor_from_overlay_pos = 0;
11949 int pt_old = PT - delta;
11951 /* Skip over glyphs not having an object at the start of the row.
11952 These are special glyphs like truncation marks on terminal
11953 frames. */
11954 if (row->displays_text_p)
11955 while (glyph < end
11956 && INTEGERP (glyph->object)
11957 && glyph->charpos < 0)
11959 x += glyph->pixel_width;
11960 ++glyph;
11963 string_start = NULL;
11964 while (glyph < end
11965 && !INTEGERP (glyph->object)
11966 && (!BUFFERP (glyph->object)
11967 || (last_pos = glyph->charpos) < pt_old))
11969 if (! STRINGP (glyph->object))
11971 string_start = NULL;
11972 x += glyph->pixel_width;
11973 ++glyph;
11974 if (cursor_from_overlay_pos
11975 && last_pos >= cursor_from_overlay_pos)
11977 cursor_from_overlay_pos = 0;
11978 cursor = 0;
11981 else
11983 if (string_start == NULL)
11985 string_before_pos = last_pos;
11986 string_start = glyph;
11987 string_start_x = x;
11989 /* Skip all glyphs from string. */
11992 Lisp_Object cprop;
11993 int pos;
11994 if ((cursor == NULL || glyph > cursor)
11995 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11996 Qcursor, (glyph)->object),
11997 !NILP (cprop))
11998 && (pos = string_buffer_position (w, glyph->object,
11999 string_before_pos),
12000 (pos == 0 /* From overlay */
12001 || pos == pt_old)))
12003 /* Estimate overlay buffer position from the buffer
12004 positions of the glyphs before and after the overlay.
12005 Add 1 to last_pos so that if point corresponds to the
12006 glyph right after the overlay, we still use a 'cursor'
12007 property found in that overlay. */
12008 cursor_from_overlay_pos = (pos ? 0 : last_pos
12009 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12010 cursor = glyph;
12011 cursor_x = x;
12013 x += glyph->pixel_width;
12014 ++glyph;
12016 while (glyph < end && EQ (glyph->object, string_start->object));
12020 if (cursor != NULL)
12022 glyph = cursor;
12023 x = cursor_x;
12025 else if (row->ends_in_ellipsis_p && glyph == end)
12027 /* Scan back over the ellipsis glyphs, decrementing positions. */
12028 while (glyph > row->glyphs[TEXT_AREA]
12029 && (glyph - 1)->charpos == last_pos)
12030 glyph--, x -= glyph->pixel_width;
12031 /* That loop always goes one position too far,
12032 including the glyph before the ellipsis.
12033 So scan forward over that one. */
12034 x += glyph->pixel_width;
12035 glyph++;
12037 else if (string_start
12038 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12040 /* We may have skipped over point because the previous glyphs
12041 are from string. As there's no easy way to know the
12042 character position of the current glyph, find the correct
12043 glyph on point by scanning from string_start again. */
12044 Lisp_Object limit;
12045 Lisp_Object string;
12046 struct glyph *stop = glyph;
12047 int pos;
12049 limit = make_number (pt_old + 1);
12050 glyph = string_start;
12051 x = string_start_x;
12052 string = glyph->object;
12053 pos = string_buffer_position (w, string, string_before_pos);
12054 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12055 because we always put cursor after overlay strings. */
12056 while (pos == 0 && glyph < stop)
12058 string = glyph->object;
12059 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12060 if (glyph < stop)
12061 pos = string_buffer_position (w, glyph->object, string_before_pos);
12064 while (glyph < stop)
12066 pos = XINT (Fnext_single_char_property_change
12067 (make_number (pos), Qdisplay, Qnil, limit));
12068 if (pos > pt_old)
12069 break;
12070 /* Skip glyphs from the same string. */
12071 string = glyph->object;
12072 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12073 /* Skip glyphs from an overlay. */
12074 while (glyph < stop
12075 && ! string_buffer_position (w, glyph->object, pos))
12077 string = glyph->object;
12078 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12082 /* If we reached the end of the line, and end was from a string,
12083 cursor is not on this line. */
12084 if (glyph == end && row->continued_p)
12085 return 0;
12088 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12089 w->cursor.x = x;
12090 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12091 w->cursor.y = row->y + dy;
12093 if (w == XWINDOW (selected_window))
12095 if (!row->continued_p
12096 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12097 && row->x == 0)
12099 this_line_buffer = XBUFFER (w->buffer);
12101 CHARPOS (this_line_start_pos)
12102 = MATRIX_ROW_START_CHARPOS (row) + delta;
12103 BYTEPOS (this_line_start_pos)
12104 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12106 CHARPOS (this_line_end_pos)
12107 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12108 BYTEPOS (this_line_end_pos)
12109 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12111 this_line_y = w->cursor.y;
12112 this_line_pixel_height = row->height;
12113 this_line_vpos = w->cursor.vpos;
12114 this_line_start_x = row->x;
12116 else
12117 CHARPOS (this_line_start_pos) = 0;
12120 return 1;
12124 /* Run window scroll functions, if any, for WINDOW with new window
12125 start STARTP. Sets the window start of WINDOW to that position.
12127 We assume that the window's buffer is really current. */
12129 static INLINE struct text_pos
12130 run_window_scroll_functions (window, startp)
12131 Lisp_Object window;
12132 struct text_pos startp;
12134 struct window *w = XWINDOW (window);
12135 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12137 if (current_buffer != XBUFFER (w->buffer))
12138 abort ();
12140 if (!NILP (Vwindow_scroll_functions))
12142 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12143 make_number (CHARPOS (startp)));
12144 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12145 /* In case the hook functions switch buffers. */
12146 if (current_buffer != XBUFFER (w->buffer))
12147 set_buffer_internal_1 (XBUFFER (w->buffer));
12150 return startp;
12154 /* Make sure the line containing the cursor is fully visible.
12155 A value of 1 means there is nothing to be done.
12156 (Either the line is fully visible, or it cannot be made so,
12157 or we cannot tell.)
12159 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12160 is higher than window.
12162 A value of 0 means the caller should do scrolling
12163 as if point had gone off the screen. */
12165 static int
12166 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12167 struct window *w;
12168 int force_p;
12169 int current_matrix_p;
12171 struct glyph_matrix *matrix;
12172 struct glyph_row *row;
12173 int window_height;
12175 if (!make_cursor_line_fully_visible_p)
12176 return 1;
12178 /* It's not always possible to find the cursor, e.g, when a window
12179 is full of overlay strings. Don't do anything in that case. */
12180 if (w->cursor.vpos < 0)
12181 return 1;
12183 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12184 row = MATRIX_ROW (matrix, w->cursor.vpos);
12186 /* If the cursor row is not partially visible, there's nothing to do. */
12187 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12188 return 1;
12190 /* If the row the cursor is in is taller than the window's height,
12191 it's not clear what to do, so do nothing. */
12192 window_height = window_box_height (w);
12193 if (row->height >= window_height)
12195 if (!force_p || MINI_WINDOW_P (w)
12196 || w->vscroll || w->cursor.vpos == 0)
12197 return 1;
12199 return 0;
12201 #if 0
12202 /* This code used to try to scroll the window just enough to make
12203 the line visible. It returned 0 to say that the caller should
12204 allocate larger glyph matrices. */
12206 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12208 int dy = row->height - row->visible_height;
12209 w->vscroll = 0;
12210 w->cursor.y += dy;
12211 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12213 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12215 int dy = - (row->height - row->visible_height);
12216 w->vscroll = dy;
12217 w->cursor.y += dy;
12218 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12221 /* When we change the cursor y-position of the selected window,
12222 change this_line_y as well so that the display optimization for
12223 the cursor line of the selected window in redisplay_internal uses
12224 the correct y-position. */
12225 if (w == XWINDOW (selected_window))
12226 this_line_y = w->cursor.y;
12228 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12229 redisplay with larger matrices. */
12230 if (matrix->nrows < required_matrix_height (w))
12232 fonts_changed_p = 1;
12233 return 0;
12236 return 1;
12237 #endif /* 0 */
12241 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12242 non-zero means only WINDOW is redisplayed in redisplay_internal.
12243 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12244 in redisplay_window to bring a partially visible line into view in
12245 the case that only the cursor has moved.
12247 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12248 last screen line's vertical height extends past the end of the screen.
12250 Value is
12252 1 if scrolling succeeded
12254 0 if scrolling didn't find point.
12256 -1 if new fonts have been loaded so that we must interrupt
12257 redisplay, adjust glyph matrices, and try again. */
12259 enum
12261 SCROLLING_SUCCESS,
12262 SCROLLING_FAILED,
12263 SCROLLING_NEED_LARGER_MATRICES
12266 static int
12267 try_scrolling (window, just_this_one_p, scroll_conservatively,
12268 scroll_step, temp_scroll_step, last_line_misfit)
12269 Lisp_Object window;
12270 int just_this_one_p;
12271 EMACS_INT scroll_conservatively, scroll_step;
12272 int temp_scroll_step;
12273 int last_line_misfit;
12275 struct window *w = XWINDOW (window);
12276 struct frame *f = XFRAME (w->frame);
12277 struct text_pos scroll_margin_pos;
12278 struct text_pos pos;
12279 struct text_pos startp;
12280 struct it it;
12281 Lisp_Object window_end;
12282 int this_scroll_margin;
12283 int dy = 0;
12284 int scroll_max;
12285 int rc;
12286 int amount_to_scroll = 0;
12287 Lisp_Object aggressive;
12288 int height;
12289 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12291 #if GLYPH_DEBUG
12292 debug_method_add (w, "try_scrolling");
12293 #endif
12295 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12297 /* Compute scroll margin height in pixels. We scroll when point is
12298 within this distance from the top or bottom of the window. */
12299 if (scroll_margin > 0)
12301 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12302 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12304 else
12305 this_scroll_margin = 0;
12307 /* Force scroll_conservatively to have a reasonable value so it doesn't
12308 cause an overflow while computing how much to scroll. */
12309 if (scroll_conservatively)
12310 scroll_conservatively = min (scroll_conservatively,
12311 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12313 /* Compute how much we should try to scroll maximally to bring point
12314 into view. */
12315 if (scroll_step || scroll_conservatively || temp_scroll_step)
12316 scroll_max = max (scroll_step,
12317 max (scroll_conservatively, temp_scroll_step));
12318 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12319 || NUMBERP (current_buffer->scroll_up_aggressively))
12320 /* We're trying to scroll because of aggressive scrolling
12321 but no scroll_step is set. Choose an arbitrary one. Maybe
12322 there should be a variable for this. */
12323 scroll_max = 10;
12324 else
12325 scroll_max = 0;
12326 scroll_max *= FRAME_LINE_HEIGHT (f);
12328 /* Decide whether we have to scroll down. Start at the window end
12329 and move this_scroll_margin up to find the position of the scroll
12330 margin. */
12331 window_end = Fwindow_end (window, Qt);
12333 too_near_end:
12335 CHARPOS (scroll_margin_pos) = XINT (window_end);
12336 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12338 if (this_scroll_margin || extra_scroll_margin_lines)
12340 start_display (&it, w, scroll_margin_pos);
12341 if (this_scroll_margin)
12342 move_it_vertically_backward (&it, this_scroll_margin);
12343 if (extra_scroll_margin_lines)
12344 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12345 scroll_margin_pos = it.current.pos;
12348 if (PT >= CHARPOS (scroll_margin_pos))
12350 int y0;
12352 /* Point is in the scroll margin at the bottom of the window, or
12353 below. Compute a new window start that makes point visible. */
12355 /* Compute the distance from the scroll margin to PT.
12356 Give up if the distance is greater than scroll_max. */
12357 start_display (&it, w, scroll_margin_pos);
12358 y0 = it.current_y;
12359 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12360 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12362 /* To make point visible, we have to move the window start
12363 down so that the line the cursor is in is visible, which
12364 means we have to add in the height of the cursor line. */
12365 dy = line_bottom_y (&it) - y0;
12367 if (dy > scroll_max)
12368 return SCROLLING_FAILED;
12370 /* Move the window start down. If scrolling conservatively,
12371 move it just enough down to make point visible. If
12372 scroll_step is set, move it down by scroll_step. */
12373 start_display (&it, w, startp);
12375 if (scroll_conservatively)
12376 /* Set AMOUNT_TO_SCROLL to at least one line,
12377 and at most scroll_conservatively lines. */
12378 amount_to_scroll
12379 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12380 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12381 else if (scroll_step || temp_scroll_step)
12382 amount_to_scroll = scroll_max;
12383 else
12385 aggressive = current_buffer->scroll_up_aggressively;
12386 height = WINDOW_BOX_TEXT_HEIGHT (w);
12387 if (NUMBERP (aggressive))
12389 double float_amount = XFLOATINT (aggressive) * height;
12390 amount_to_scroll = float_amount;
12391 if (amount_to_scroll == 0 && float_amount > 0)
12392 amount_to_scroll = 1;
12396 if (amount_to_scroll <= 0)
12397 return SCROLLING_FAILED;
12399 /* If moving by amount_to_scroll leaves STARTP unchanged,
12400 move it down one screen line. */
12402 move_it_vertically (&it, amount_to_scroll);
12403 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12404 move_it_by_lines (&it, 1, 1);
12405 startp = it.current.pos;
12407 else
12409 /* See if point is inside the scroll margin at the top of the
12410 window. */
12411 scroll_margin_pos = startp;
12412 if (this_scroll_margin)
12414 start_display (&it, w, startp);
12415 move_it_vertically (&it, this_scroll_margin);
12416 scroll_margin_pos = it.current.pos;
12419 if (PT < CHARPOS (scroll_margin_pos))
12421 /* Point is in the scroll margin at the top of the window or
12422 above what is displayed in the window. */
12423 int y0;
12425 /* Compute the vertical distance from PT to the scroll
12426 margin position. Give up if distance is greater than
12427 scroll_max. */
12428 SET_TEXT_POS (pos, PT, PT_BYTE);
12429 start_display (&it, w, pos);
12430 y0 = it.current_y;
12431 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12432 it.last_visible_y, -1,
12433 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12434 dy = it.current_y - y0;
12435 if (dy > scroll_max)
12436 return SCROLLING_FAILED;
12438 /* Compute new window start. */
12439 start_display (&it, w, startp);
12441 if (scroll_conservatively)
12442 amount_to_scroll
12443 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12444 else if (scroll_step || temp_scroll_step)
12445 amount_to_scroll = scroll_max;
12446 else
12448 aggressive = current_buffer->scroll_down_aggressively;
12449 height = WINDOW_BOX_TEXT_HEIGHT (w);
12450 if (NUMBERP (aggressive))
12452 double float_amount = XFLOATINT (aggressive) * height;
12453 amount_to_scroll = float_amount;
12454 if (amount_to_scroll == 0 && float_amount > 0)
12455 amount_to_scroll = 1;
12459 if (amount_to_scroll <= 0)
12460 return SCROLLING_FAILED;
12462 move_it_vertically_backward (&it, amount_to_scroll);
12463 startp = it.current.pos;
12467 /* Run window scroll functions. */
12468 startp = run_window_scroll_functions (window, startp);
12470 /* Display the window. Give up if new fonts are loaded, or if point
12471 doesn't appear. */
12472 if (!try_window (window, startp, 0))
12473 rc = SCROLLING_NEED_LARGER_MATRICES;
12474 else if (w->cursor.vpos < 0)
12476 clear_glyph_matrix (w->desired_matrix);
12477 rc = SCROLLING_FAILED;
12479 else
12481 /* Maybe forget recorded base line for line number display. */
12482 if (!just_this_one_p
12483 || current_buffer->clip_changed
12484 || BEG_UNCHANGED < CHARPOS (startp))
12485 w->base_line_number = Qnil;
12487 /* If cursor ends up on a partially visible line,
12488 treat that as being off the bottom of the screen. */
12489 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12491 clear_glyph_matrix (w->desired_matrix);
12492 ++extra_scroll_margin_lines;
12493 goto too_near_end;
12495 rc = SCROLLING_SUCCESS;
12498 return rc;
12502 /* Compute a suitable window start for window W if display of W starts
12503 on a continuation line. Value is non-zero if a new window start
12504 was computed.
12506 The new window start will be computed, based on W's width, starting
12507 from the start of the continued line. It is the start of the
12508 screen line with the minimum distance from the old start W->start. */
12510 static int
12511 compute_window_start_on_continuation_line (w)
12512 struct window *w;
12514 struct text_pos pos, start_pos;
12515 int window_start_changed_p = 0;
12517 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12519 /* If window start is on a continuation line... Window start may be
12520 < BEGV in case there's invisible text at the start of the
12521 buffer (M-x rmail, for example). */
12522 if (CHARPOS (start_pos) > BEGV
12523 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12525 struct it it;
12526 struct glyph_row *row;
12528 /* Handle the case that the window start is out of range. */
12529 if (CHARPOS (start_pos) < BEGV)
12530 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12531 else if (CHARPOS (start_pos) > ZV)
12532 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12534 /* Find the start of the continued line. This should be fast
12535 because scan_buffer is fast (newline cache). */
12536 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12537 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12538 row, DEFAULT_FACE_ID);
12539 reseat_at_previous_visible_line_start (&it);
12541 /* If the line start is "too far" away from the window start,
12542 say it takes too much time to compute a new window start. */
12543 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12544 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12546 int min_distance, distance;
12548 /* Move forward by display lines to find the new window
12549 start. If window width was enlarged, the new start can
12550 be expected to be > the old start. If window width was
12551 decreased, the new window start will be < the old start.
12552 So, we're looking for the display line start with the
12553 minimum distance from the old window start. */
12554 pos = it.current.pos;
12555 min_distance = INFINITY;
12556 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12557 distance < min_distance)
12559 min_distance = distance;
12560 pos = it.current.pos;
12561 move_it_by_lines (&it, 1, 0);
12564 /* Set the window start there. */
12565 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12566 window_start_changed_p = 1;
12570 return window_start_changed_p;
12574 /* Try cursor movement in case text has not changed in window WINDOW,
12575 with window start STARTP. Value is
12577 CURSOR_MOVEMENT_SUCCESS if successful
12579 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12581 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12582 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12583 we want to scroll as if scroll-step were set to 1. See the code.
12585 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12586 which case we have to abort this redisplay, and adjust matrices
12587 first. */
12589 enum
12591 CURSOR_MOVEMENT_SUCCESS,
12592 CURSOR_MOVEMENT_CANNOT_BE_USED,
12593 CURSOR_MOVEMENT_MUST_SCROLL,
12594 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12597 static int
12598 try_cursor_movement (window, startp, scroll_step)
12599 Lisp_Object window;
12600 struct text_pos startp;
12601 int *scroll_step;
12603 struct window *w = XWINDOW (window);
12604 struct frame *f = XFRAME (w->frame);
12605 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12607 #if GLYPH_DEBUG
12608 if (inhibit_try_cursor_movement)
12609 return rc;
12610 #endif
12612 /* Handle case where text has not changed, only point, and it has
12613 not moved off the frame. */
12614 if (/* Point may be in this window. */
12615 PT >= CHARPOS (startp)
12616 /* Selective display hasn't changed. */
12617 && !current_buffer->clip_changed
12618 /* Function force-mode-line-update is used to force a thorough
12619 redisplay. It sets either windows_or_buffers_changed or
12620 update_mode_lines. So don't take a shortcut here for these
12621 cases. */
12622 && !update_mode_lines
12623 && !windows_or_buffers_changed
12624 && !cursor_type_changed
12625 /* Can't use this case if highlighting a region. When a
12626 region exists, cursor movement has to do more than just
12627 set the cursor. */
12628 && !(!NILP (Vtransient_mark_mode)
12629 && !NILP (current_buffer->mark_active))
12630 && NILP (w->region_showing)
12631 && NILP (Vshow_trailing_whitespace)
12632 /* Right after splitting windows, last_point may be nil. */
12633 && INTEGERP (w->last_point)
12634 /* This code is not used for mini-buffer for the sake of the case
12635 of redisplaying to replace an echo area message; since in
12636 that case the mini-buffer contents per se are usually
12637 unchanged. This code is of no real use in the mini-buffer
12638 since the handling of this_line_start_pos, etc., in redisplay
12639 handles the same cases. */
12640 && !EQ (window, minibuf_window)
12641 /* When splitting windows or for new windows, it happens that
12642 redisplay is called with a nil window_end_vpos or one being
12643 larger than the window. This should really be fixed in
12644 window.c. I don't have this on my list, now, so we do
12645 approximately the same as the old redisplay code. --gerd. */
12646 && INTEGERP (w->window_end_vpos)
12647 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12648 && (FRAME_WINDOW_P (f)
12649 || !overlay_arrow_in_current_buffer_p ()))
12651 int this_scroll_margin, top_scroll_margin;
12652 struct glyph_row *row = NULL;
12654 #if GLYPH_DEBUG
12655 debug_method_add (w, "cursor movement");
12656 #endif
12658 /* Scroll if point within this distance from the top or bottom
12659 of the window. This is a pixel value. */
12660 this_scroll_margin = max (0, scroll_margin);
12661 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12662 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12664 top_scroll_margin = this_scroll_margin;
12665 if (WINDOW_WANTS_HEADER_LINE_P (w))
12666 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12668 /* Start with the row the cursor was displayed during the last
12669 not paused redisplay. Give up if that row is not valid. */
12670 if (w->last_cursor.vpos < 0
12671 || w->last_cursor.vpos >= w->current_matrix->nrows)
12672 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12673 else
12675 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12676 if (row->mode_line_p)
12677 ++row;
12678 if (!row->enabled_p)
12679 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12682 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12684 int scroll_p = 0;
12685 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12687 if (PT > XFASTINT (w->last_point))
12689 /* Point has moved forward. */
12690 while (MATRIX_ROW_END_CHARPOS (row) < PT
12691 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12693 xassert (row->enabled_p);
12694 ++row;
12697 /* The end position of a row equals the start position
12698 of the next row. If PT is there, we would rather
12699 display it in the next line. */
12700 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12701 && MATRIX_ROW_END_CHARPOS (row) == PT
12702 && !cursor_row_p (w, row))
12703 ++row;
12705 /* If within the scroll margin, scroll. Note that
12706 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12707 the next line would be drawn, and that
12708 this_scroll_margin can be zero. */
12709 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12710 || PT > MATRIX_ROW_END_CHARPOS (row)
12711 /* Line is completely visible last line in window
12712 and PT is to be set in the next line. */
12713 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12714 && PT == MATRIX_ROW_END_CHARPOS (row)
12715 && !row->ends_at_zv_p
12716 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12717 scroll_p = 1;
12719 else if (PT < XFASTINT (w->last_point))
12721 /* Cursor has to be moved backward. Note that PT >=
12722 CHARPOS (startp) because of the outer if-statement. */
12723 while (!row->mode_line_p
12724 && (MATRIX_ROW_START_CHARPOS (row) > PT
12725 || (MATRIX_ROW_START_CHARPOS (row) == PT
12726 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12727 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12728 row > w->current_matrix->rows
12729 && (row-1)->ends_in_newline_from_string_p))))
12730 && (row->y > top_scroll_margin
12731 || CHARPOS (startp) == BEGV))
12733 xassert (row->enabled_p);
12734 --row;
12737 /* Consider the following case: Window starts at BEGV,
12738 there is invisible, intangible text at BEGV, so that
12739 display starts at some point START > BEGV. It can
12740 happen that we are called with PT somewhere between
12741 BEGV and START. Try to handle that case. */
12742 if (row < w->current_matrix->rows
12743 || row->mode_line_p)
12745 row = w->current_matrix->rows;
12746 if (row->mode_line_p)
12747 ++row;
12750 /* Due to newlines in overlay strings, we may have to
12751 skip forward over overlay strings. */
12752 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12753 && MATRIX_ROW_END_CHARPOS (row) == PT
12754 && !cursor_row_p (w, row))
12755 ++row;
12757 /* If within the scroll margin, scroll. */
12758 if (row->y < top_scroll_margin
12759 && CHARPOS (startp) != BEGV)
12760 scroll_p = 1;
12762 else
12764 /* Cursor did not move. So don't scroll even if cursor line
12765 is partially visible, as it was so before. */
12766 rc = CURSOR_MOVEMENT_SUCCESS;
12769 if (PT < MATRIX_ROW_START_CHARPOS (row)
12770 || PT > MATRIX_ROW_END_CHARPOS (row))
12772 /* if PT is not in the glyph row, give up. */
12773 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12775 else if (rc != CURSOR_MOVEMENT_SUCCESS
12776 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12777 && make_cursor_line_fully_visible_p)
12779 if (PT == MATRIX_ROW_END_CHARPOS (row)
12780 && !row->ends_at_zv_p
12781 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12782 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12783 else if (row->height > window_box_height (w))
12785 /* If we end up in a partially visible line, let's
12786 make it fully visible, except when it's taller
12787 than the window, in which case we can't do much
12788 about it. */
12789 *scroll_step = 1;
12790 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12792 else
12794 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12795 if (!cursor_row_fully_visible_p (w, 0, 1))
12796 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12797 else
12798 rc = CURSOR_MOVEMENT_SUCCESS;
12801 else if (scroll_p)
12802 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12803 else
12807 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12809 rc = CURSOR_MOVEMENT_SUCCESS;
12810 break;
12812 ++row;
12814 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12815 && MATRIX_ROW_START_CHARPOS (row) == PT
12816 && cursor_row_p (w, row));
12821 return rc;
12824 void
12825 set_vertical_scroll_bar (w)
12826 struct window *w;
12828 int start, end, whole;
12830 /* Calculate the start and end positions for the current window.
12831 At some point, it would be nice to choose between scrollbars
12832 which reflect the whole buffer size, with special markers
12833 indicating narrowing, and scrollbars which reflect only the
12834 visible region.
12836 Note that mini-buffers sometimes aren't displaying any text. */
12837 if (!MINI_WINDOW_P (w)
12838 || (w == XWINDOW (minibuf_window)
12839 && NILP (echo_area_buffer[0])))
12841 struct buffer *buf = XBUFFER (w->buffer);
12842 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12843 start = marker_position (w->start) - BUF_BEGV (buf);
12844 /* I don't think this is guaranteed to be right. For the
12845 moment, we'll pretend it is. */
12846 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12848 if (end < start)
12849 end = start;
12850 if (whole < (end - start))
12851 whole = end - start;
12853 else
12854 start = end = whole = 0;
12856 /* Indicate what this scroll bar ought to be displaying now. */
12857 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12861 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12862 selected_window is redisplayed.
12864 We can return without actually redisplaying the window if
12865 fonts_changed_p is nonzero. In that case, redisplay_internal will
12866 retry. */
12868 static void
12869 redisplay_window (window, just_this_one_p)
12870 Lisp_Object window;
12871 int just_this_one_p;
12873 struct window *w = XWINDOW (window);
12874 struct frame *f = XFRAME (w->frame);
12875 struct buffer *buffer = XBUFFER (w->buffer);
12876 struct buffer *old = current_buffer;
12877 struct text_pos lpoint, opoint, startp;
12878 int update_mode_line;
12879 int tem;
12880 struct it it;
12881 /* Record it now because it's overwritten. */
12882 int current_matrix_up_to_date_p = 0;
12883 int used_current_matrix_p = 0;
12884 /* This is less strict than current_matrix_up_to_date_p.
12885 It indictes that the buffer contents and narrowing are unchanged. */
12886 int buffer_unchanged_p = 0;
12887 int temp_scroll_step = 0;
12888 int count = SPECPDL_INDEX ();
12889 int rc;
12890 int centering_position = -1;
12891 int last_line_misfit = 0;
12892 int save_beg_unchanged, save_end_unchanged;
12894 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12895 opoint = lpoint;
12897 /* W must be a leaf window here. */
12898 xassert (!NILP (w->buffer));
12899 #if GLYPH_DEBUG
12900 *w->desired_matrix->method = 0;
12901 #endif
12903 specbind (Qinhibit_point_motion_hooks, Qt);
12905 reconsider_clip_changes (w, buffer);
12907 /* Has the mode line to be updated? */
12908 update_mode_line = (!NILP (w->update_mode_line)
12909 || update_mode_lines
12910 || buffer->clip_changed
12911 || buffer->prevent_redisplay_optimizations_p);
12913 if (MINI_WINDOW_P (w))
12915 if (w == XWINDOW (echo_area_window)
12916 && !NILP (echo_area_buffer[0]))
12918 if (update_mode_line)
12919 /* We may have to update a tty frame's menu bar or a
12920 tool-bar. Example `M-x C-h C-h C-g'. */
12921 goto finish_menu_bars;
12922 else
12923 /* We've already displayed the echo area glyphs in this window. */
12924 goto finish_scroll_bars;
12926 else if ((w != XWINDOW (minibuf_window)
12927 || minibuf_level == 0)
12928 /* When buffer is nonempty, redisplay window normally. */
12929 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12930 /* Quail displays non-mini buffers in minibuffer window.
12931 In that case, redisplay the window normally. */
12932 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12934 /* W is a mini-buffer window, but it's not active, so clear
12935 it. */
12936 int yb = window_text_bottom_y (w);
12937 struct glyph_row *row;
12938 int y;
12940 for (y = 0, row = w->desired_matrix->rows;
12941 y < yb;
12942 y += row->height, ++row)
12943 blank_row (w, row, y);
12944 goto finish_scroll_bars;
12947 clear_glyph_matrix (w->desired_matrix);
12950 /* Otherwise set up data on this window; select its buffer and point
12951 value. */
12952 /* Really select the buffer, for the sake of buffer-local
12953 variables. */
12954 set_buffer_internal_1 (XBUFFER (w->buffer));
12955 SET_TEXT_POS (opoint, PT, PT_BYTE);
12957 save_beg_unchanged = BEG_UNCHANGED;
12958 save_end_unchanged = END_UNCHANGED;
12960 current_matrix_up_to_date_p
12961 = (!NILP (w->window_end_valid)
12962 && !current_buffer->clip_changed
12963 && !current_buffer->prevent_redisplay_optimizations_p
12964 && XFASTINT (w->last_modified) >= MODIFF
12965 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12967 buffer_unchanged_p
12968 = (!NILP (w->window_end_valid)
12969 && !current_buffer->clip_changed
12970 && XFASTINT (w->last_modified) >= MODIFF
12971 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12973 /* When windows_or_buffers_changed is non-zero, we can't rely on
12974 the window end being valid, so set it to nil there. */
12975 if (windows_or_buffers_changed)
12977 /* If window starts on a continuation line, maybe adjust the
12978 window start in case the window's width changed. */
12979 if (XMARKER (w->start)->buffer == current_buffer)
12980 compute_window_start_on_continuation_line (w);
12982 w->window_end_valid = Qnil;
12985 /* Some sanity checks. */
12986 CHECK_WINDOW_END (w);
12987 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12988 abort ();
12989 if (BYTEPOS (opoint) < CHARPOS (opoint))
12990 abort ();
12992 /* If %c is in mode line, update it if needed. */
12993 if (!NILP (w->column_number_displayed)
12994 /* This alternative quickly identifies a common case
12995 where no change is needed. */
12996 && !(PT == XFASTINT (w->last_point)
12997 && XFASTINT (w->last_modified) >= MODIFF
12998 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12999 && (XFASTINT (w->column_number_displayed)
13000 != (int) current_column ())) /* iftc */
13001 update_mode_line = 1;
13003 /* Count number of windows showing the selected buffer. An indirect
13004 buffer counts as its base buffer. */
13005 if (!just_this_one_p)
13007 struct buffer *current_base, *window_base;
13008 current_base = current_buffer;
13009 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13010 if (current_base->base_buffer)
13011 current_base = current_base->base_buffer;
13012 if (window_base->base_buffer)
13013 window_base = window_base->base_buffer;
13014 if (current_base == window_base)
13015 buffer_shared++;
13018 /* Point refers normally to the selected window. For any other
13019 window, set up appropriate value. */
13020 if (!EQ (window, selected_window))
13022 int new_pt = XMARKER (w->pointm)->charpos;
13023 int new_pt_byte = marker_byte_position (w->pointm);
13024 if (new_pt < BEGV)
13026 new_pt = BEGV;
13027 new_pt_byte = BEGV_BYTE;
13028 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13030 else if (new_pt > (ZV - 1))
13032 new_pt = ZV;
13033 new_pt_byte = ZV_BYTE;
13034 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13037 /* We don't use SET_PT so that the point-motion hooks don't run. */
13038 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13041 /* If any of the character widths specified in the display table
13042 have changed, invalidate the width run cache. It's true that
13043 this may be a bit late to catch such changes, but the rest of
13044 redisplay goes (non-fatally) haywire when the display table is
13045 changed, so why should we worry about doing any better? */
13046 if (current_buffer->width_run_cache)
13048 struct Lisp_Char_Table *disptab = buffer_display_table ();
13050 if (! disptab_matches_widthtab (disptab,
13051 XVECTOR (current_buffer->width_table)))
13053 invalidate_region_cache (current_buffer,
13054 current_buffer->width_run_cache,
13055 BEG, Z);
13056 recompute_width_table (current_buffer, disptab);
13060 /* If window-start is screwed up, choose a new one. */
13061 if (XMARKER (w->start)->buffer != current_buffer)
13062 goto recenter;
13064 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13066 /* If someone specified a new starting point but did not insist,
13067 check whether it can be used. */
13068 if (!NILP (w->optional_new_start)
13069 && CHARPOS (startp) >= BEGV
13070 && CHARPOS (startp) <= ZV)
13072 w->optional_new_start = Qnil;
13073 start_display (&it, w, startp);
13074 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13075 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13076 if (IT_CHARPOS (it) == PT)
13077 w->force_start = Qt;
13078 /* IT may overshoot PT if text at PT is invisible. */
13079 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13080 w->force_start = Qt;
13083 force_start:
13085 /* Handle case where place to start displaying has been specified,
13086 unless the specified location is outside the accessible range. */
13087 if (!NILP (w->force_start)
13088 || w->frozen_window_start_p)
13090 /* We set this later on if we have to adjust point. */
13091 int new_vpos = -1;
13093 w->force_start = Qnil;
13094 w->vscroll = 0;
13095 w->window_end_valid = Qnil;
13097 /* Forget any recorded base line for line number display. */
13098 if (!buffer_unchanged_p)
13099 w->base_line_number = Qnil;
13101 /* Redisplay the mode line. Select the buffer properly for that.
13102 Also, run the hook window-scroll-functions
13103 because we have scrolled. */
13104 /* Note, we do this after clearing force_start because
13105 if there's an error, it is better to forget about force_start
13106 than to get into an infinite loop calling the hook functions
13107 and having them get more errors. */
13108 if (!update_mode_line
13109 || ! NILP (Vwindow_scroll_functions))
13111 update_mode_line = 1;
13112 w->update_mode_line = Qt;
13113 startp = run_window_scroll_functions (window, startp);
13116 w->last_modified = make_number (0);
13117 w->last_overlay_modified = make_number (0);
13118 if (CHARPOS (startp) < BEGV)
13119 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13120 else if (CHARPOS (startp) > ZV)
13121 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13123 /* Redisplay, then check if cursor has been set during the
13124 redisplay. Give up if new fonts were loaded. */
13125 /* We used to issue a CHECK_MARGINS argument to try_window here,
13126 but this causes scrolling to fail when point begins inside
13127 the scroll margin (bug#148) -- cyd */
13128 if (!try_window (window, startp, 0))
13130 w->force_start = Qt;
13131 clear_glyph_matrix (w->desired_matrix);
13132 goto need_larger_matrices;
13135 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13137 /* If point does not appear, try to move point so it does
13138 appear. The desired matrix has been built above, so we
13139 can use it here. */
13140 new_vpos = window_box_height (w) / 2;
13143 if (!cursor_row_fully_visible_p (w, 0, 0))
13145 /* Point does appear, but on a line partly visible at end of window.
13146 Move it back to a fully-visible line. */
13147 new_vpos = window_box_height (w);
13150 /* If we need to move point for either of the above reasons,
13151 now actually do it. */
13152 if (new_vpos >= 0)
13154 struct glyph_row *row;
13156 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13157 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13158 ++row;
13160 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13161 MATRIX_ROW_START_BYTEPOS (row));
13163 if (w != XWINDOW (selected_window))
13164 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13165 else if (current_buffer == old)
13166 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13168 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13170 /* If we are highlighting the region, then we just changed
13171 the region, so redisplay to show it. */
13172 if (!NILP (Vtransient_mark_mode)
13173 && !NILP (current_buffer->mark_active))
13175 clear_glyph_matrix (w->desired_matrix);
13176 if (!try_window (window, startp, 0))
13177 goto need_larger_matrices;
13181 #if GLYPH_DEBUG
13182 debug_method_add (w, "forced window start");
13183 #endif
13184 goto done;
13187 /* Handle case where text has not changed, only point, and it has
13188 not moved off the frame, and we are not retrying after hscroll.
13189 (current_matrix_up_to_date_p is nonzero when retrying.) */
13190 if (current_matrix_up_to_date_p
13191 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13192 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13194 switch (rc)
13196 case CURSOR_MOVEMENT_SUCCESS:
13197 used_current_matrix_p = 1;
13198 goto done;
13200 #if 0 /* try_cursor_movement never returns this value. */
13201 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13202 goto need_larger_matrices;
13203 #endif
13205 case CURSOR_MOVEMENT_MUST_SCROLL:
13206 goto try_to_scroll;
13208 default:
13209 abort ();
13212 /* If current starting point was originally the beginning of a line
13213 but no longer is, find a new starting point. */
13214 else if (!NILP (w->start_at_line_beg)
13215 && !(CHARPOS (startp) <= BEGV
13216 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13218 #if GLYPH_DEBUG
13219 debug_method_add (w, "recenter 1");
13220 #endif
13221 goto recenter;
13224 /* Try scrolling with try_window_id. Value is > 0 if update has
13225 been done, it is -1 if we know that the same window start will
13226 not work. It is 0 if unsuccessful for some other reason. */
13227 else if ((tem = try_window_id (w)) != 0)
13229 #if GLYPH_DEBUG
13230 debug_method_add (w, "try_window_id %d", tem);
13231 #endif
13233 if (fonts_changed_p)
13234 goto need_larger_matrices;
13235 if (tem > 0)
13236 goto done;
13238 /* Otherwise try_window_id has returned -1 which means that we
13239 don't want the alternative below this comment to execute. */
13241 else if (CHARPOS (startp) >= BEGV
13242 && CHARPOS (startp) <= ZV
13243 && PT >= CHARPOS (startp)
13244 && (CHARPOS (startp) < ZV
13245 /* Avoid starting at end of buffer. */
13246 || CHARPOS (startp) == BEGV
13247 || (XFASTINT (w->last_modified) >= MODIFF
13248 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13251 /* If first window line is a continuation line, and window start
13252 is inside the modified region, but the first change is before
13253 current window start, we must select a new window start.
13255 However, if this is the result of a down-mouse event (e.g. by
13256 extending the mouse-drag-overlay), we don't want to select a
13257 new window start, since that would change the position under
13258 the mouse, resulting in an unwanted mouse-movement rather
13259 than a simple mouse-click. */
13260 if (NILP (w->start_at_line_beg)
13261 && NILP (do_mouse_tracking)
13262 && CHARPOS (startp) > BEGV
13263 && CHARPOS (startp) > BEG + save_beg_unchanged
13264 && CHARPOS (startp) <= Z - save_end_unchanged)
13266 w->force_start = Qt;
13267 if (XMARKER (w->start)->buffer == current_buffer)
13268 compute_window_start_on_continuation_line (w);
13269 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13270 goto force_start;
13273 #if GLYPH_DEBUG
13274 debug_method_add (w, "same window start");
13275 #endif
13277 /* Try to redisplay starting at same place as before.
13278 If point has not moved off frame, accept the results. */
13279 if (!current_matrix_up_to_date_p
13280 /* Don't use try_window_reusing_current_matrix in this case
13281 because a window scroll function can have changed the
13282 buffer. */
13283 || !NILP (Vwindow_scroll_functions)
13284 || MINI_WINDOW_P (w)
13285 || !(used_current_matrix_p
13286 = try_window_reusing_current_matrix (w)))
13288 IF_DEBUG (debug_method_add (w, "1"));
13289 if (try_window (window, startp, 1) < 0)
13290 /* -1 means we need to scroll.
13291 0 means we need new matrices, but fonts_changed_p
13292 is set in that case, so we will detect it below. */
13293 goto try_to_scroll;
13296 if (fonts_changed_p)
13297 goto need_larger_matrices;
13299 if (w->cursor.vpos >= 0)
13301 if (!just_this_one_p
13302 || current_buffer->clip_changed
13303 || BEG_UNCHANGED < CHARPOS (startp))
13304 /* Forget any recorded base line for line number display. */
13305 w->base_line_number = Qnil;
13307 if (!cursor_row_fully_visible_p (w, 1, 0))
13309 clear_glyph_matrix (w->desired_matrix);
13310 last_line_misfit = 1;
13312 /* Drop through and scroll. */
13313 else
13314 goto done;
13316 else
13317 clear_glyph_matrix (w->desired_matrix);
13320 try_to_scroll:
13322 w->last_modified = make_number (0);
13323 w->last_overlay_modified = make_number (0);
13325 /* Redisplay the mode line. Select the buffer properly for that. */
13326 if (!update_mode_line)
13328 update_mode_line = 1;
13329 w->update_mode_line = Qt;
13332 /* Try to scroll by specified few lines. */
13333 if ((scroll_conservatively
13334 || scroll_step
13335 || temp_scroll_step
13336 || NUMBERP (current_buffer->scroll_up_aggressively)
13337 || NUMBERP (current_buffer->scroll_down_aggressively))
13338 && !current_buffer->clip_changed
13339 && CHARPOS (startp) >= BEGV
13340 && CHARPOS (startp) <= ZV)
13342 /* The function returns -1 if new fonts were loaded, 1 if
13343 successful, 0 if not successful. */
13344 int rc = try_scrolling (window, just_this_one_p,
13345 scroll_conservatively,
13346 scroll_step,
13347 temp_scroll_step, last_line_misfit);
13348 switch (rc)
13350 case SCROLLING_SUCCESS:
13351 goto done;
13353 case SCROLLING_NEED_LARGER_MATRICES:
13354 goto need_larger_matrices;
13356 case SCROLLING_FAILED:
13357 break;
13359 default:
13360 abort ();
13364 /* Finally, just choose place to start which centers point */
13366 recenter:
13367 if (centering_position < 0)
13368 centering_position = window_box_height (w) / 2;
13370 #if GLYPH_DEBUG
13371 debug_method_add (w, "recenter");
13372 #endif
13374 /* w->vscroll = 0; */
13376 /* Forget any previously recorded base line for line number display. */
13377 if (!buffer_unchanged_p)
13378 w->base_line_number = Qnil;
13380 /* Move backward half the height of the window. */
13381 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13382 it.current_y = it.last_visible_y;
13383 move_it_vertically_backward (&it, centering_position);
13384 xassert (IT_CHARPOS (it) >= BEGV);
13386 /* The function move_it_vertically_backward may move over more
13387 than the specified y-distance. If it->w is small, e.g. a
13388 mini-buffer window, we may end up in front of the window's
13389 display area. Start displaying at the start of the line
13390 containing PT in this case. */
13391 if (it.current_y <= 0)
13393 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13394 move_it_vertically_backward (&it, 0);
13395 #if 0
13396 /* I think this assert is bogus if buffer contains
13397 invisible text or images. KFS. */
13398 xassert (IT_CHARPOS (it) <= PT);
13399 #endif
13400 it.current_y = 0;
13403 it.current_x = it.hpos = 0;
13405 /* Set startp here explicitly in case that helps avoid an infinite loop
13406 in case the window-scroll-functions functions get errors. */
13407 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13409 /* Run scroll hooks. */
13410 startp = run_window_scroll_functions (window, it.current.pos);
13412 /* Redisplay the window. */
13413 if (!current_matrix_up_to_date_p
13414 || windows_or_buffers_changed
13415 || cursor_type_changed
13416 /* Don't use try_window_reusing_current_matrix in this case
13417 because it can have changed the buffer. */
13418 || !NILP (Vwindow_scroll_functions)
13419 || !just_this_one_p
13420 || MINI_WINDOW_P (w)
13421 || !(used_current_matrix_p
13422 = try_window_reusing_current_matrix (w)))
13423 try_window (window, startp, 0);
13425 /* If new fonts have been loaded (due to fontsets), give up. We
13426 have to start a new redisplay since we need to re-adjust glyph
13427 matrices. */
13428 if (fonts_changed_p)
13429 goto need_larger_matrices;
13431 /* If cursor did not appear assume that the middle of the window is
13432 in the first line of the window. Do it again with the next line.
13433 (Imagine a window of height 100, displaying two lines of height
13434 60. Moving back 50 from it->last_visible_y will end in the first
13435 line.) */
13436 if (w->cursor.vpos < 0)
13438 if (!NILP (w->window_end_valid)
13439 && PT >= Z - XFASTINT (w->window_end_pos))
13441 clear_glyph_matrix (w->desired_matrix);
13442 move_it_by_lines (&it, 1, 0);
13443 try_window (window, it.current.pos, 0);
13445 else if (PT < IT_CHARPOS (it))
13447 clear_glyph_matrix (w->desired_matrix);
13448 move_it_by_lines (&it, -1, 0);
13449 try_window (window, it.current.pos, 0);
13451 else
13453 /* Not much we can do about it. */
13457 /* Consider the following case: Window starts at BEGV, there is
13458 invisible, intangible text at BEGV, so that display starts at
13459 some point START > BEGV. It can happen that we are called with
13460 PT somewhere between BEGV and START. Try to handle that case. */
13461 if (w->cursor.vpos < 0)
13463 struct glyph_row *row = w->current_matrix->rows;
13464 if (row->mode_line_p)
13465 ++row;
13466 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13469 if (!cursor_row_fully_visible_p (w, 0, 0))
13471 /* If vscroll is enabled, disable it and try again. */
13472 if (w->vscroll)
13474 w->vscroll = 0;
13475 clear_glyph_matrix (w->desired_matrix);
13476 goto recenter;
13479 /* If centering point failed to make the whole line visible,
13480 put point at the top instead. That has to make the whole line
13481 visible, if it can be done. */
13482 if (centering_position == 0)
13483 goto done;
13485 clear_glyph_matrix (w->desired_matrix);
13486 centering_position = 0;
13487 goto recenter;
13490 done:
13492 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13493 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13494 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13495 ? Qt : Qnil);
13497 /* Display the mode line, if we must. */
13498 if ((update_mode_line
13499 /* If window not full width, must redo its mode line
13500 if (a) the window to its side is being redone and
13501 (b) we do a frame-based redisplay. This is a consequence
13502 of how inverted lines are drawn in frame-based redisplay. */
13503 || (!just_this_one_p
13504 && !FRAME_WINDOW_P (f)
13505 && !WINDOW_FULL_WIDTH_P (w))
13506 /* Line number to display. */
13507 || INTEGERP (w->base_line_pos)
13508 /* Column number is displayed and different from the one displayed. */
13509 || (!NILP (w->column_number_displayed)
13510 && (XFASTINT (w->column_number_displayed)
13511 != (int) current_column ()))) /* iftc */
13512 /* This means that the window has a mode line. */
13513 && (WINDOW_WANTS_MODELINE_P (w)
13514 || WINDOW_WANTS_HEADER_LINE_P (w)))
13516 display_mode_lines (w);
13518 /* If mode line height has changed, arrange for a thorough
13519 immediate redisplay using the correct mode line height. */
13520 if (WINDOW_WANTS_MODELINE_P (w)
13521 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13523 fonts_changed_p = 1;
13524 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13525 = DESIRED_MODE_LINE_HEIGHT (w);
13528 /* If top line height has changed, arrange for a thorough
13529 immediate redisplay using the correct mode line height. */
13530 if (WINDOW_WANTS_HEADER_LINE_P (w)
13531 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13533 fonts_changed_p = 1;
13534 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13535 = DESIRED_HEADER_LINE_HEIGHT (w);
13538 if (fonts_changed_p)
13539 goto need_larger_matrices;
13542 if (!line_number_displayed
13543 && !BUFFERP (w->base_line_pos))
13545 w->base_line_pos = Qnil;
13546 w->base_line_number = Qnil;
13549 finish_menu_bars:
13551 /* When we reach a frame's selected window, redo the frame's menu bar. */
13552 if (update_mode_line
13553 && EQ (FRAME_SELECTED_WINDOW (f), window))
13555 int redisplay_menu_p = 0;
13556 int redisplay_tool_bar_p = 0;
13558 if (FRAME_WINDOW_P (f))
13560 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13561 || defined (USE_GTK)
13562 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13563 #else
13564 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13565 #endif
13567 else
13568 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13570 if (redisplay_menu_p)
13571 display_menu_bar (w);
13573 #ifdef HAVE_WINDOW_SYSTEM
13574 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13575 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13576 #else
13577 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13578 && (FRAME_TOOL_BAR_LINES (f) > 0
13579 || !NILP (Vauto_resize_tool_bars));
13581 #endif
13583 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13585 extern int ignore_mouse_drag_p;
13586 ignore_mouse_drag_p = 1;
13588 #endif
13591 #ifdef HAVE_WINDOW_SYSTEM
13592 if (FRAME_WINDOW_P (f)
13593 && update_window_fringes (w, (just_this_one_p
13594 || (!used_current_matrix_p && !overlay_arrow_seen)
13595 || w->pseudo_window_p)))
13597 update_begin (f);
13598 BLOCK_INPUT;
13599 if (draw_window_fringes (w, 1))
13600 x_draw_vertical_border (w);
13601 UNBLOCK_INPUT;
13602 update_end (f);
13604 #endif /* HAVE_WINDOW_SYSTEM */
13606 /* We go to this label, with fonts_changed_p nonzero,
13607 if it is necessary to try again using larger glyph matrices.
13608 We have to redeem the scroll bar even in this case,
13609 because the loop in redisplay_internal expects that. */
13610 need_larger_matrices:
13612 finish_scroll_bars:
13614 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13616 /* Set the thumb's position and size. */
13617 set_vertical_scroll_bar (w);
13619 /* Note that we actually used the scroll bar attached to this
13620 window, so it shouldn't be deleted at the end of redisplay. */
13621 redeem_scroll_bar_hook (w);
13624 /* Restore current_buffer and value of point in it. */
13625 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13626 set_buffer_internal_1 (old);
13627 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13628 shorter. This can be caused by log truncation in *Messages*. */
13629 if (CHARPOS (lpoint) <= ZV)
13630 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13632 unbind_to (count, Qnil);
13636 /* Build the complete desired matrix of WINDOW with a window start
13637 buffer position POS.
13639 Value is 1 if successful. It is zero if fonts were loaded during
13640 redisplay which makes re-adjusting glyph matrices necessary, and -1
13641 if point would appear in the scroll margins.
13642 (We check that only if CHECK_MARGINS is nonzero. */
13645 try_window (window, pos, check_margins)
13646 Lisp_Object window;
13647 struct text_pos pos;
13648 int check_margins;
13650 struct window *w = XWINDOW (window);
13651 struct it it;
13652 struct glyph_row *last_text_row = NULL;
13653 struct frame *f = XFRAME (w->frame);
13655 /* Make POS the new window start. */
13656 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13658 /* Mark cursor position as unknown. No overlay arrow seen. */
13659 w->cursor.vpos = -1;
13660 overlay_arrow_seen = 0;
13662 /* Initialize iterator and info to start at POS. */
13663 start_display (&it, w, pos);
13665 /* Display all lines of W. */
13666 while (it.current_y < it.last_visible_y)
13668 if (display_line (&it))
13669 last_text_row = it.glyph_row - 1;
13670 if (fonts_changed_p)
13671 return 0;
13674 /* Don't let the cursor end in the scroll margins. */
13675 if (check_margins
13676 && !MINI_WINDOW_P (w))
13678 int this_scroll_margin;
13680 this_scroll_margin = max (0, scroll_margin);
13681 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13682 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13684 if ((w->cursor.y >= 0 /* not vscrolled */
13685 && w->cursor.y < this_scroll_margin
13686 && CHARPOS (pos) > BEGV
13687 && IT_CHARPOS (it) < ZV)
13688 /* rms: considering make_cursor_line_fully_visible_p here
13689 seems to give wrong results. We don't want to recenter
13690 when the last line is partly visible, we want to allow
13691 that case to be handled in the usual way. */
13692 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
13694 w->cursor.vpos = -1;
13695 clear_glyph_matrix (w->desired_matrix);
13696 return -1;
13700 /* If bottom moved off end of frame, change mode line percentage. */
13701 if (XFASTINT (w->window_end_pos) <= 0
13702 && Z != IT_CHARPOS (it))
13703 w->update_mode_line = Qt;
13705 /* Set window_end_pos to the offset of the last character displayed
13706 on the window from the end of current_buffer. Set
13707 window_end_vpos to its row number. */
13708 if (last_text_row)
13710 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13711 w->window_end_bytepos
13712 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13713 w->window_end_pos
13714 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13715 w->window_end_vpos
13716 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13717 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13718 ->displays_text_p);
13720 else
13722 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13723 w->window_end_pos = make_number (Z - ZV);
13724 w->window_end_vpos = make_number (0);
13727 /* But that is not valid info until redisplay finishes. */
13728 w->window_end_valid = Qnil;
13729 return 1;
13734 /************************************************************************
13735 Window redisplay reusing current matrix when buffer has not changed
13736 ************************************************************************/
13738 /* Try redisplay of window W showing an unchanged buffer with a
13739 different window start than the last time it was displayed by
13740 reusing its current matrix. Value is non-zero if successful.
13741 W->start is the new window start. */
13743 static int
13744 try_window_reusing_current_matrix (w)
13745 struct window *w;
13747 struct frame *f = XFRAME (w->frame);
13748 struct glyph_row *row, *bottom_row;
13749 struct it it;
13750 struct run run;
13751 struct text_pos start, new_start;
13752 int nrows_scrolled, i;
13753 struct glyph_row *last_text_row;
13754 struct glyph_row *last_reused_text_row;
13755 struct glyph_row *start_row;
13756 int start_vpos, min_y, max_y;
13758 #if GLYPH_DEBUG
13759 if (inhibit_try_window_reusing)
13760 return 0;
13761 #endif
13763 if (/* This function doesn't handle terminal frames. */
13764 !FRAME_WINDOW_P (f)
13765 /* Don't try to reuse the display if windows have been split
13766 or such. */
13767 || windows_or_buffers_changed
13768 || cursor_type_changed)
13769 return 0;
13771 /* Can't do this if region may have changed. */
13772 if ((!NILP (Vtransient_mark_mode)
13773 && !NILP (current_buffer->mark_active))
13774 || !NILP (w->region_showing)
13775 || !NILP (Vshow_trailing_whitespace))
13776 return 0;
13778 /* If top-line visibility has changed, give up. */
13779 if (WINDOW_WANTS_HEADER_LINE_P (w)
13780 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13781 return 0;
13783 /* Give up if old or new display is scrolled vertically. We could
13784 make this function handle this, but right now it doesn't. */
13785 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13786 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13787 return 0;
13789 /* The variable new_start now holds the new window start. The old
13790 start `start' can be determined from the current matrix. */
13791 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13792 start = start_row->start.pos;
13793 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13795 /* Clear the desired matrix for the display below. */
13796 clear_glyph_matrix (w->desired_matrix);
13798 if (CHARPOS (new_start) <= CHARPOS (start))
13800 int first_row_y;
13802 /* Don't use this method if the display starts with an ellipsis
13803 displayed for invisible text. It's not easy to handle that case
13804 below, and it's certainly not worth the effort since this is
13805 not a frequent case. */
13806 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13807 return 0;
13809 IF_DEBUG (debug_method_add (w, "twu1"));
13811 /* Display up to a row that can be reused. The variable
13812 last_text_row is set to the last row displayed that displays
13813 text. Note that it.vpos == 0 if or if not there is a
13814 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13815 start_display (&it, w, new_start);
13816 first_row_y = it.current_y;
13817 w->cursor.vpos = -1;
13818 last_text_row = last_reused_text_row = NULL;
13820 while (it.current_y < it.last_visible_y
13821 && !fonts_changed_p)
13823 /* If we have reached into the characters in the START row,
13824 that means the line boundaries have changed. So we
13825 can't start copying with the row START. Maybe it will
13826 work to start copying with the following row. */
13827 while (IT_CHARPOS (it) > CHARPOS (start))
13829 /* Advance to the next row as the "start". */
13830 start_row++;
13831 start = start_row->start.pos;
13832 /* If there are no more rows to try, or just one, give up. */
13833 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13834 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13835 || CHARPOS (start) == ZV)
13837 clear_glyph_matrix (w->desired_matrix);
13838 return 0;
13841 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13843 /* If we have reached alignment,
13844 we can copy the rest of the rows. */
13845 if (IT_CHARPOS (it) == CHARPOS (start))
13846 break;
13848 if (display_line (&it))
13849 last_text_row = it.glyph_row - 1;
13852 /* A value of current_y < last_visible_y means that we stopped
13853 at the previous window start, which in turn means that we
13854 have at least one reusable row. */
13855 if (it.current_y < it.last_visible_y)
13857 /* IT.vpos always starts from 0; it counts text lines. */
13858 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13860 /* Find PT if not already found in the lines displayed. */
13861 if (w->cursor.vpos < 0)
13863 int dy = it.current_y - start_row->y;
13865 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13866 row = row_containing_pos (w, PT, row, NULL, dy);
13867 if (row)
13868 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13869 dy, nrows_scrolled);
13870 else
13872 clear_glyph_matrix (w->desired_matrix);
13873 return 0;
13877 /* Scroll the display. Do it before the current matrix is
13878 changed. The problem here is that update has not yet
13879 run, i.e. part of the current matrix is not up to date.
13880 scroll_run_hook will clear the cursor, and use the
13881 current matrix to get the height of the row the cursor is
13882 in. */
13883 run.current_y = start_row->y;
13884 run.desired_y = it.current_y;
13885 run.height = it.last_visible_y - it.current_y;
13887 if (run.height > 0 && run.current_y != run.desired_y)
13889 update_begin (f);
13890 rif->update_window_begin_hook (w);
13891 rif->clear_window_mouse_face (w);
13892 rif->scroll_run_hook (w, &run);
13893 rif->update_window_end_hook (w, 0, 0);
13894 update_end (f);
13897 /* Shift current matrix down by nrows_scrolled lines. */
13898 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13899 rotate_matrix (w->current_matrix,
13900 start_vpos,
13901 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13902 nrows_scrolled);
13904 /* Disable lines that must be updated. */
13905 for (i = 0; i < nrows_scrolled; ++i)
13906 (start_row + i)->enabled_p = 0;
13908 /* Re-compute Y positions. */
13909 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13910 max_y = it.last_visible_y;
13911 for (row = start_row + nrows_scrolled;
13912 row < bottom_row;
13913 ++row)
13915 row->y = it.current_y;
13916 row->visible_height = row->height;
13918 if (row->y < min_y)
13919 row->visible_height -= min_y - row->y;
13920 if (row->y + row->height > max_y)
13921 row->visible_height -= row->y + row->height - max_y;
13922 row->redraw_fringe_bitmaps_p = 1;
13924 it.current_y += row->height;
13926 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13927 last_reused_text_row = row;
13928 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13929 break;
13932 /* Disable lines in the current matrix which are now
13933 below the window. */
13934 for (++row; row < bottom_row; ++row)
13935 row->enabled_p = row->mode_line_p = 0;
13938 /* Update window_end_pos etc.; last_reused_text_row is the last
13939 reused row from the current matrix containing text, if any.
13940 The value of last_text_row is the last displayed line
13941 containing text. */
13942 if (last_reused_text_row)
13944 w->window_end_bytepos
13945 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13946 w->window_end_pos
13947 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13948 w->window_end_vpos
13949 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13950 w->current_matrix));
13952 else if (last_text_row)
13954 w->window_end_bytepos
13955 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13956 w->window_end_pos
13957 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13958 w->window_end_vpos
13959 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13961 else
13963 /* This window must be completely empty. */
13964 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13965 w->window_end_pos = make_number (Z - ZV);
13966 w->window_end_vpos = make_number (0);
13968 w->window_end_valid = Qnil;
13970 /* Update hint: don't try scrolling again in update_window. */
13971 w->desired_matrix->no_scrolling_p = 1;
13973 #if GLYPH_DEBUG
13974 debug_method_add (w, "try_window_reusing_current_matrix 1");
13975 #endif
13976 return 1;
13978 else if (CHARPOS (new_start) > CHARPOS (start))
13980 struct glyph_row *pt_row, *row;
13981 struct glyph_row *first_reusable_row;
13982 struct glyph_row *first_row_to_display;
13983 int dy;
13984 int yb = window_text_bottom_y (w);
13986 /* Find the row starting at new_start, if there is one. Don't
13987 reuse a partially visible line at the end. */
13988 first_reusable_row = start_row;
13989 while (first_reusable_row->enabled_p
13990 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13991 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13992 < CHARPOS (new_start)))
13993 ++first_reusable_row;
13995 /* Give up if there is no row to reuse. */
13996 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13997 || !first_reusable_row->enabled_p
13998 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13999 != CHARPOS (new_start)))
14000 return 0;
14002 /* We can reuse fully visible rows beginning with
14003 first_reusable_row to the end of the window. Set
14004 first_row_to_display to the first row that cannot be reused.
14005 Set pt_row to the row containing point, if there is any. */
14006 pt_row = NULL;
14007 for (first_row_to_display = first_reusable_row;
14008 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14009 ++first_row_to_display)
14011 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14012 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14013 pt_row = first_row_to_display;
14016 /* Start displaying at the start of first_row_to_display. */
14017 xassert (first_row_to_display->y < yb);
14018 init_to_row_start (&it, w, first_row_to_display);
14020 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14021 - start_vpos);
14022 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14023 - nrows_scrolled);
14024 it.current_y = (first_row_to_display->y - first_reusable_row->y
14025 + WINDOW_HEADER_LINE_HEIGHT (w));
14027 /* Display lines beginning with first_row_to_display in the
14028 desired matrix. Set last_text_row to the last row displayed
14029 that displays text. */
14030 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14031 if (pt_row == NULL)
14032 w->cursor.vpos = -1;
14033 last_text_row = NULL;
14034 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14035 if (display_line (&it))
14036 last_text_row = it.glyph_row - 1;
14038 /* Give up If point isn't in a row displayed or reused. */
14039 if (w->cursor.vpos < 0)
14041 clear_glyph_matrix (w->desired_matrix);
14042 return 0;
14045 /* If point is in a reused row, adjust y and vpos of the cursor
14046 position. */
14047 if (pt_row)
14049 w->cursor.vpos -= nrows_scrolled;
14050 w->cursor.y -= first_reusable_row->y - start_row->y;
14053 /* Scroll the display. */
14054 run.current_y = first_reusable_row->y;
14055 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14056 run.height = it.last_visible_y - run.current_y;
14057 dy = run.current_y - run.desired_y;
14059 if (run.height)
14061 update_begin (f);
14062 rif->update_window_begin_hook (w);
14063 rif->clear_window_mouse_face (w);
14064 rif->scroll_run_hook (w, &run);
14065 rif->update_window_end_hook (w, 0, 0);
14066 update_end (f);
14069 /* Adjust Y positions of reused rows. */
14070 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14071 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14072 max_y = it.last_visible_y;
14073 for (row = first_reusable_row; row < first_row_to_display; ++row)
14075 row->y -= dy;
14076 row->visible_height = row->height;
14077 if (row->y < min_y)
14078 row->visible_height -= min_y - row->y;
14079 if (row->y + row->height > max_y)
14080 row->visible_height -= row->y + row->height - max_y;
14081 row->redraw_fringe_bitmaps_p = 1;
14084 /* Scroll the current matrix. */
14085 xassert (nrows_scrolled > 0);
14086 rotate_matrix (w->current_matrix,
14087 start_vpos,
14088 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14089 -nrows_scrolled);
14091 /* Disable rows not reused. */
14092 for (row -= nrows_scrolled; row < bottom_row; ++row)
14093 row->enabled_p = 0;
14095 /* Point may have moved to a different line, so we cannot assume that
14096 the previous cursor position is valid; locate the correct row. */
14097 if (pt_row)
14099 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14100 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14101 row++)
14103 w->cursor.vpos++;
14104 w->cursor.y = row->y;
14106 if (row < bottom_row)
14108 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14109 while (glyph->charpos < PT)
14111 w->cursor.hpos++;
14112 w->cursor.x += glyph->pixel_width;
14113 glyph++;
14118 /* Adjust window end. A null value of last_text_row means that
14119 the window end is in reused rows which in turn means that
14120 only its vpos can have changed. */
14121 if (last_text_row)
14123 w->window_end_bytepos
14124 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14125 w->window_end_pos
14126 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14127 w->window_end_vpos
14128 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14130 else
14132 w->window_end_vpos
14133 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14136 w->window_end_valid = Qnil;
14137 w->desired_matrix->no_scrolling_p = 1;
14139 #if GLYPH_DEBUG
14140 debug_method_add (w, "try_window_reusing_current_matrix 2");
14141 #endif
14142 return 1;
14145 return 0;
14150 /************************************************************************
14151 Window redisplay reusing current matrix when buffer has changed
14152 ************************************************************************/
14154 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14155 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14156 int *, int *));
14157 static struct glyph_row *
14158 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14159 struct glyph_row *));
14162 /* Return the last row in MATRIX displaying text. If row START is
14163 non-null, start searching with that row. IT gives the dimensions
14164 of the display. Value is null if matrix is empty; otherwise it is
14165 a pointer to the row found. */
14167 static struct glyph_row *
14168 find_last_row_displaying_text (matrix, it, start)
14169 struct glyph_matrix *matrix;
14170 struct it *it;
14171 struct glyph_row *start;
14173 struct glyph_row *row, *row_found;
14175 /* Set row_found to the last row in IT->w's current matrix
14176 displaying text. The loop looks funny but think of partially
14177 visible lines. */
14178 row_found = NULL;
14179 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14180 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14182 xassert (row->enabled_p);
14183 row_found = row;
14184 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14185 break;
14186 ++row;
14189 return row_found;
14193 /* Return the last row in the current matrix of W that is not affected
14194 by changes at the start of current_buffer that occurred since W's
14195 current matrix was built. Value is null if no such row exists.
14197 BEG_UNCHANGED us the number of characters unchanged at the start of
14198 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14199 first changed character in current_buffer. Characters at positions <
14200 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14201 when the current matrix was built. */
14203 static struct glyph_row *
14204 find_last_unchanged_at_beg_row (w)
14205 struct window *w;
14207 int first_changed_pos = BEG + BEG_UNCHANGED;
14208 struct glyph_row *row;
14209 struct glyph_row *row_found = NULL;
14210 int yb = window_text_bottom_y (w);
14212 /* Find the last row displaying unchanged text. */
14213 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14214 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14215 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14217 if (/* If row ends before first_changed_pos, it is unchanged,
14218 except in some case. */
14219 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14220 /* When row ends in ZV and we write at ZV it is not
14221 unchanged. */
14222 && !row->ends_at_zv_p
14223 /* When first_changed_pos is the end of a continued line,
14224 row is not unchanged because it may be no longer
14225 continued. */
14226 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14227 && (row->continued_p
14228 || row->exact_window_width_line_p)))
14229 row_found = row;
14231 /* Stop if last visible row. */
14232 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14233 break;
14235 ++row;
14238 return row_found;
14242 /* Find the first glyph row in the current matrix of W that is not
14243 affected by changes at the end of current_buffer since the
14244 time W's current matrix was built.
14246 Return in *DELTA the number of chars by which buffer positions in
14247 unchanged text at the end of current_buffer must be adjusted.
14249 Return in *DELTA_BYTES the corresponding number of bytes.
14251 Value is null if no such row exists, i.e. all rows are affected by
14252 changes. */
14254 static struct glyph_row *
14255 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14256 struct window *w;
14257 int *delta, *delta_bytes;
14259 struct glyph_row *row;
14260 struct glyph_row *row_found = NULL;
14262 *delta = *delta_bytes = 0;
14264 /* Display must not have been paused, otherwise the current matrix
14265 is not up to date. */
14266 if (NILP (w->window_end_valid))
14267 abort ();
14269 /* A value of window_end_pos >= END_UNCHANGED means that the window
14270 end is in the range of changed text. If so, there is no
14271 unchanged row at the end of W's current matrix. */
14272 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14273 return NULL;
14275 /* Set row to the last row in W's current matrix displaying text. */
14276 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14278 /* If matrix is entirely empty, no unchanged row exists. */
14279 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14281 /* The value of row is the last glyph row in the matrix having a
14282 meaningful buffer position in it. The end position of row
14283 corresponds to window_end_pos. This allows us to translate
14284 buffer positions in the current matrix to current buffer
14285 positions for characters not in changed text. */
14286 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14287 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14288 int last_unchanged_pos, last_unchanged_pos_old;
14289 struct glyph_row *first_text_row
14290 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14292 *delta = Z - Z_old;
14293 *delta_bytes = Z_BYTE - Z_BYTE_old;
14295 /* Set last_unchanged_pos to the buffer position of the last
14296 character in the buffer that has not been changed. Z is the
14297 index + 1 of the last character in current_buffer, i.e. by
14298 subtracting END_UNCHANGED we get the index of the last
14299 unchanged character, and we have to add BEG to get its buffer
14300 position. */
14301 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14302 last_unchanged_pos_old = last_unchanged_pos - *delta;
14304 /* Search backward from ROW for a row displaying a line that
14305 starts at a minimum position >= last_unchanged_pos_old. */
14306 for (; row > first_text_row; --row)
14308 /* This used to abort, but it can happen.
14309 It is ok to just stop the search instead here. KFS. */
14310 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14311 break;
14313 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14314 row_found = row;
14318 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14319 abort ();
14321 return row_found;
14325 /* Make sure that glyph rows in the current matrix of window W
14326 reference the same glyph memory as corresponding rows in the
14327 frame's frame matrix. This function is called after scrolling W's
14328 current matrix on a terminal frame in try_window_id and
14329 try_window_reusing_current_matrix. */
14331 static void
14332 sync_frame_with_window_matrix_rows (w)
14333 struct window *w;
14335 struct frame *f = XFRAME (w->frame);
14336 struct glyph_row *window_row, *window_row_end, *frame_row;
14338 /* Preconditions: W must be a leaf window and full-width. Its frame
14339 must have a frame matrix. */
14340 xassert (NILP (w->hchild) && NILP (w->vchild));
14341 xassert (WINDOW_FULL_WIDTH_P (w));
14342 xassert (!FRAME_WINDOW_P (f));
14344 /* If W is a full-width window, glyph pointers in W's current matrix
14345 have, by definition, to be the same as glyph pointers in the
14346 corresponding frame matrix. Note that frame matrices have no
14347 marginal areas (see build_frame_matrix). */
14348 window_row = w->current_matrix->rows;
14349 window_row_end = window_row + w->current_matrix->nrows;
14350 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14351 while (window_row < window_row_end)
14353 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14354 struct glyph *end = window_row->glyphs[LAST_AREA];
14356 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14357 frame_row->glyphs[TEXT_AREA] = start;
14358 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14359 frame_row->glyphs[LAST_AREA] = end;
14361 /* Disable frame rows whose corresponding window rows have
14362 been disabled in try_window_id. */
14363 if (!window_row->enabled_p)
14364 frame_row->enabled_p = 0;
14366 ++window_row, ++frame_row;
14371 /* Find the glyph row in window W containing CHARPOS. Consider all
14372 rows between START and END (not inclusive). END null means search
14373 all rows to the end of the display area of W. Value is the row
14374 containing CHARPOS or null. */
14376 struct glyph_row *
14377 row_containing_pos (w, charpos, start, end, dy)
14378 struct window *w;
14379 int charpos;
14380 struct glyph_row *start, *end;
14381 int dy;
14383 struct glyph_row *row = start;
14384 int last_y;
14386 /* If we happen to start on a header-line, skip that. */
14387 if (row->mode_line_p)
14388 ++row;
14390 if ((end && row >= end) || !row->enabled_p)
14391 return NULL;
14393 last_y = window_text_bottom_y (w) - dy;
14395 while (1)
14397 /* Give up if we have gone too far. */
14398 if (end && row >= end)
14399 return NULL;
14400 /* This formerly returned if they were equal.
14401 I think that both quantities are of a "last plus one" type;
14402 if so, when they are equal, the row is within the screen. -- rms. */
14403 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14404 return NULL;
14406 /* If it is in this row, return this row. */
14407 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14408 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14409 /* The end position of a row equals the start
14410 position of the next row. If CHARPOS is there, we
14411 would rather display it in the next line, except
14412 when this line ends in ZV. */
14413 && !row->ends_at_zv_p
14414 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14415 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14416 return row;
14417 ++row;
14422 /* Try to redisplay window W by reusing its existing display. W's
14423 current matrix must be up to date when this function is called,
14424 i.e. window_end_valid must not be nil.
14426 Value is
14428 1 if display has been updated
14429 0 if otherwise unsuccessful
14430 -1 if redisplay with same window start is known not to succeed
14432 The following steps are performed:
14434 1. Find the last row in the current matrix of W that is not
14435 affected by changes at the start of current_buffer. If no such row
14436 is found, give up.
14438 2. Find the first row in W's current matrix that is not affected by
14439 changes at the end of current_buffer. Maybe there is no such row.
14441 3. Display lines beginning with the row + 1 found in step 1 to the
14442 row found in step 2 or, if step 2 didn't find a row, to the end of
14443 the window.
14445 4. If cursor is not known to appear on the window, give up.
14447 5. If display stopped at the row found in step 2, scroll the
14448 display and current matrix as needed.
14450 6. Maybe display some lines at the end of W, if we must. This can
14451 happen under various circumstances, like a partially visible line
14452 becoming fully visible, or because newly displayed lines are displayed
14453 in smaller font sizes.
14455 7. Update W's window end information. */
14457 static int
14458 try_window_id (w)
14459 struct window *w;
14461 struct frame *f = XFRAME (w->frame);
14462 struct glyph_matrix *current_matrix = w->current_matrix;
14463 struct glyph_matrix *desired_matrix = w->desired_matrix;
14464 struct glyph_row *last_unchanged_at_beg_row;
14465 struct glyph_row *first_unchanged_at_end_row;
14466 struct glyph_row *row;
14467 struct glyph_row *bottom_row;
14468 int bottom_vpos;
14469 struct it it;
14470 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14471 struct text_pos start_pos;
14472 struct run run;
14473 int first_unchanged_at_end_vpos = 0;
14474 struct glyph_row *last_text_row, *last_text_row_at_end;
14475 struct text_pos start;
14476 int first_changed_charpos, last_changed_charpos;
14478 #if GLYPH_DEBUG
14479 if (inhibit_try_window_id)
14480 return 0;
14481 #endif
14483 /* This is handy for debugging. */
14484 #if 0
14485 #define GIVE_UP(X) \
14486 do { \
14487 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14488 return 0; \
14489 } while (0)
14490 #else
14491 #define GIVE_UP(X) return 0
14492 #endif
14494 SET_TEXT_POS_FROM_MARKER (start, w->start);
14496 /* Don't use this for mini-windows because these can show
14497 messages and mini-buffers, and we don't handle that here. */
14498 if (MINI_WINDOW_P (w))
14499 GIVE_UP (1);
14501 /* This flag is used to prevent redisplay optimizations. */
14502 if (windows_or_buffers_changed || cursor_type_changed)
14503 GIVE_UP (2);
14505 /* Verify that narrowing has not changed.
14506 Also verify that we were not told to prevent redisplay optimizations.
14507 It would be nice to further
14508 reduce the number of cases where this prevents try_window_id. */
14509 if (current_buffer->clip_changed
14510 || current_buffer->prevent_redisplay_optimizations_p)
14511 GIVE_UP (3);
14513 /* Window must either use window-based redisplay or be full width. */
14514 if (!FRAME_WINDOW_P (f)
14515 && (!line_ins_del_ok
14516 || !WINDOW_FULL_WIDTH_P (w)))
14517 GIVE_UP (4);
14519 /* Give up if point is not known NOT to appear in W. */
14520 if (PT < CHARPOS (start))
14521 GIVE_UP (5);
14523 /* Another way to prevent redisplay optimizations. */
14524 if (XFASTINT (w->last_modified) == 0)
14525 GIVE_UP (6);
14527 /* Verify that window is not hscrolled. */
14528 if (XFASTINT (w->hscroll) != 0)
14529 GIVE_UP (7);
14531 /* Verify that display wasn't paused. */
14532 if (NILP (w->window_end_valid))
14533 GIVE_UP (8);
14535 /* Can't use this if highlighting a region because a cursor movement
14536 will do more than just set the cursor. */
14537 if (!NILP (Vtransient_mark_mode)
14538 && !NILP (current_buffer->mark_active))
14539 GIVE_UP (9);
14541 /* Likewise if highlighting trailing whitespace. */
14542 if (!NILP (Vshow_trailing_whitespace))
14543 GIVE_UP (11);
14545 /* Likewise if showing a region. */
14546 if (!NILP (w->region_showing))
14547 GIVE_UP (10);
14549 /* Can use this if overlay arrow position and or string have changed. */
14550 if (overlay_arrows_changed_p ())
14551 GIVE_UP (12);
14554 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14555 only if buffer has really changed. The reason is that the gap is
14556 initially at Z for freshly visited files. The code below would
14557 set end_unchanged to 0 in that case. */
14558 if (MODIFF > SAVE_MODIFF
14559 /* This seems to happen sometimes after saving a buffer. */
14560 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14562 if (GPT - BEG < BEG_UNCHANGED)
14563 BEG_UNCHANGED = GPT - BEG;
14564 if (Z - GPT < END_UNCHANGED)
14565 END_UNCHANGED = Z - GPT;
14568 /* The position of the first and last character that has been changed. */
14569 first_changed_charpos = BEG + BEG_UNCHANGED;
14570 last_changed_charpos = Z - END_UNCHANGED;
14572 /* If window starts after a line end, and the last change is in
14573 front of that newline, then changes don't affect the display.
14574 This case happens with stealth-fontification. Note that although
14575 the display is unchanged, glyph positions in the matrix have to
14576 be adjusted, of course. */
14577 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14578 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14579 && ((last_changed_charpos < CHARPOS (start)
14580 && CHARPOS (start) == BEGV)
14581 || (last_changed_charpos < CHARPOS (start) - 1
14582 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14584 int Z_old, delta, Z_BYTE_old, delta_bytes;
14585 struct glyph_row *r0;
14587 /* Compute how many chars/bytes have been added to or removed
14588 from the buffer. */
14589 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14590 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14591 delta = Z - Z_old;
14592 delta_bytes = Z_BYTE - Z_BYTE_old;
14594 /* Give up if PT is not in the window. Note that it already has
14595 been checked at the start of try_window_id that PT is not in
14596 front of the window start. */
14597 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14598 GIVE_UP (13);
14600 /* If window start is unchanged, we can reuse the whole matrix
14601 as is, after adjusting glyph positions. No need to compute
14602 the window end again, since its offset from Z hasn't changed. */
14603 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14604 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14605 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14606 /* PT must not be in a partially visible line. */
14607 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14608 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14610 /* Adjust positions in the glyph matrix. */
14611 if (delta || delta_bytes)
14613 struct glyph_row *r1
14614 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14615 increment_matrix_positions (w->current_matrix,
14616 MATRIX_ROW_VPOS (r0, current_matrix),
14617 MATRIX_ROW_VPOS (r1, current_matrix),
14618 delta, delta_bytes);
14621 /* Set the cursor. */
14622 row = row_containing_pos (w, PT, r0, NULL, 0);
14623 if (row)
14624 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14625 else
14626 abort ();
14627 return 1;
14631 /* Handle the case that changes are all below what is displayed in
14632 the window, and that PT is in the window. This shortcut cannot
14633 be taken if ZV is visible in the window, and text has been added
14634 there that is visible in the window. */
14635 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14636 /* ZV is not visible in the window, or there are no
14637 changes at ZV, actually. */
14638 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14639 || first_changed_charpos == last_changed_charpos))
14641 struct glyph_row *r0;
14643 /* Give up if PT is not in the window. Note that it already has
14644 been checked at the start of try_window_id that PT is not in
14645 front of the window start. */
14646 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14647 GIVE_UP (14);
14649 /* If window start is unchanged, we can reuse the whole matrix
14650 as is, without changing glyph positions since no text has
14651 been added/removed in front of the window end. */
14652 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14653 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14654 /* PT must not be in a partially visible line. */
14655 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14656 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14658 /* We have to compute the window end anew since text
14659 can have been added/removed after it. */
14660 w->window_end_pos
14661 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14662 w->window_end_bytepos
14663 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14665 /* Set the cursor. */
14666 row = row_containing_pos (w, PT, r0, NULL, 0);
14667 if (row)
14668 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14669 else
14670 abort ();
14671 return 2;
14675 /* Give up if window start is in the changed area.
14677 The condition used to read
14679 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14681 but why that was tested escapes me at the moment. */
14682 if (CHARPOS (start) >= first_changed_charpos
14683 && CHARPOS (start) <= last_changed_charpos)
14684 GIVE_UP (15);
14686 /* Check that window start agrees with the start of the first glyph
14687 row in its current matrix. Check this after we know the window
14688 start is not in changed text, otherwise positions would not be
14689 comparable. */
14690 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14691 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14692 GIVE_UP (16);
14694 /* Give up if the window ends in strings. Overlay strings
14695 at the end are difficult to handle, so don't try. */
14696 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14697 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14698 GIVE_UP (20);
14700 /* Compute the position at which we have to start displaying new
14701 lines. Some of the lines at the top of the window might be
14702 reusable because they are not displaying changed text. Find the
14703 last row in W's current matrix not affected by changes at the
14704 start of current_buffer. Value is null if changes start in the
14705 first line of window. */
14706 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14707 if (last_unchanged_at_beg_row)
14709 /* Avoid starting to display in the moddle of a character, a TAB
14710 for instance. This is easier than to set up the iterator
14711 exactly, and it's not a frequent case, so the additional
14712 effort wouldn't really pay off. */
14713 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14714 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14715 && last_unchanged_at_beg_row > w->current_matrix->rows)
14716 --last_unchanged_at_beg_row;
14718 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14719 GIVE_UP (17);
14721 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14722 GIVE_UP (18);
14723 start_pos = it.current.pos;
14725 /* Start displaying new lines in the desired matrix at the same
14726 vpos we would use in the current matrix, i.e. below
14727 last_unchanged_at_beg_row. */
14728 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14729 current_matrix);
14730 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14731 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14733 xassert (it.hpos == 0 && it.current_x == 0);
14735 else
14737 /* There are no reusable lines at the start of the window.
14738 Start displaying in the first text line. */
14739 start_display (&it, w, start);
14740 it.vpos = it.first_vpos;
14741 start_pos = it.current.pos;
14744 /* Find the first row that is not affected by changes at the end of
14745 the buffer. Value will be null if there is no unchanged row, in
14746 which case we must redisplay to the end of the window. delta
14747 will be set to the value by which buffer positions beginning with
14748 first_unchanged_at_end_row have to be adjusted due to text
14749 changes. */
14750 first_unchanged_at_end_row
14751 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14752 IF_DEBUG (debug_delta = delta);
14753 IF_DEBUG (debug_delta_bytes = delta_bytes);
14755 /* Set stop_pos to the buffer position up to which we will have to
14756 display new lines. If first_unchanged_at_end_row != NULL, this
14757 is the buffer position of the start of the line displayed in that
14758 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14759 that we don't stop at a buffer position. */
14760 stop_pos = 0;
14761 if (first_unchanged_at_end_row)
14763 xassert (last_unchanged_at_beg_row == NULL
14764 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14766 /* If this is a continuation line, move forward to the next one
14767 that isn't. Changes in lines above affect this line.
14768 Caution: this may move first_unchanged_at_end_row to a row
14769 not displaying text. */
14770 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14771 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14772 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14773 < it.last_visible_y))
14774 ++first_unchanged_at_end_row;
14776 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14777 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14778 >= it.last_visible_y))
14779 first_unchanged_at_end_row = NULL;
14780 else
14782 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14783 + delta);
14784 first_unchanged_at_end_vpos
14785 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14786 xassert (stop_pos >= Z - END_UNCHANGED);
14789 else if (last_unchanged_at_beg_row == NULL)
14790 GIVE_UP (19);
14793 #if GLYPH_DEBUG
14795 /* Either there is no unchanged row at the end, or the one we have
14796 now displays text. This is a necessary condition for the window
14797 end pos calculation at the end of this function. */
14798 xassert (first_unchanged_at_end_row == NULL
14799 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14801 debug_last_unchanged_at_beg_vpos
14802 = (last_unchanged_at_beg_row
14803 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14804 : -1);
14805 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14807 #endif /* GLYPH_DEBUG != 0 */
14810 /* Display new lines. Set last_text_row to the last new line
14811 displayed which has text on it, i.e. might end up as being the
14812 line where the window_end_vpos is. */
14813 w->cursor.vpos = -1;
14814 last_text_row = NULL;
14815 overlay_arrow_seen = 0;
14816 while (it.current_y < it.last_visible_y
14817 && !fonts_changed_p
14818 && (first_unchanged_at_end_row == NULL
14819 || IT_CHARPOS (it) < stop_pos))
14821 if (display_line (&it))
14822 last_text_row = it.glyph_row - 1;
14825 if (fonts_changed_p)
14826 return -1;
14829 /* Compute differences in buffer positions, y-positions etc. for
14830 lines reused at the bottom of the window. Compute what we can
14831 scroll. */
14832 if (first_unchanged_at_end_row
14833 /* No lines reused because we displayed everything up to the
14834 bottom of the window. */
14835 && it.current_y < it.last_visible_y)
14837 dvpos = (it.vpos
14838 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14839 current_matrix));
14840 dy = it.current_y - first_unchanged_at_end_row->y;
14841 run.current_y = first_unchanged_at_end_row->y;
14842 run.desired_y = run.current_y + dy;
14843 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14845 else
14847 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14848 first_unchanged_at_end_row = NULL;
14850 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14853 /* Find the cursor if not already found. We have to decide whether
14854 PT will appear on this window (it sometimes doesn't, but this is
14855 not a very frequent case.) This decision has to be made before
14856 the current matrix is altered. A value of cursor.vpos < 0 means
14857 that PT is either in one of the lines beginning at
14858 first_unchanged_at_end_row or below the window. Don't care for
14859 lines that might be displayed later at the window end; as
14860 mentioned, this is not a frequent case. */
14861 if (w->cursor.vpos < 0)
14863 /* Cursor in unchanged rows at the top? */
14864 if (PT < CHARPOS (start_pos)
14865 && last_unchanged_at_beg_row)
14867 row = row_containing_pos (w, PT,
14868 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14869 last_unchanged_at_beg_row + 1, 0);
14870 if (row)
14871 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14874 /* Start from first_unchanged_at_end_row looking for PT. */
14875 else if (first_unchanged_at_end_row)
14877 row = row_containing_pos (w, PT - delta,
14878 first_unchanged_at_end_row, NULL, 0);
14879 if (row)
14880 set_cursor_from_row (w, row, w->current_matrix, delta,
14881 delta_bytes, dy, dvpos);
14884 /* Give up if cursor was not found. */
14885 if (w->cursor.vpos < 0)
14887 clear_glyph_matrix (w->desired_matrix);
14888 return -1;
14892 /* Don't let the cursor end in the scroll margins. */
14894 int this_scroll_margin, cursor_height;
14896 this_scroll_margin = max (0, scroll_margin);
14897 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14898 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14899 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14901 if ((w->cursor.y < this_scroll_margin
14902 && CHARPOS (start) > BEGV)
14903 /* Old redisplay didn't take scroll margin into account at the bottom,
14904 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14905 || (w->cursor.y + (make_cursor_line_fully_visible_p
14906 ? cursor_height + this_scroll_margin
14907 : 1)) > it.last_visible_y)
14909 w->cursor.vpos = -1;
14910 clear_glyph_matrix (w->desired_matrix);
14911 return -1;
14915 /* Scroll the display. Do it before changing the current matrix so
14916 that xterm.c doesn't get confused about where the cursor glyph is
14917 found. */
14918 if (dy && run.height)
14920 update_begin (f);
14922 if (FRAME_WINDOW_P (f))
14924 rif->update_window_begin_hook (w);
14925 rif->clear_window_mouse_face (w);
14926 rif->scroll_run_hook (w, &run);
14927 rif->update_window_end_hook (w, 0, 0);
14929 else
14931 /* Terminal frame. In this case, dvpos gives the number of
14932 lines to scroll by; dvpos < 0 means scroll up. */
14933 int first_unchanged_at_end_vpos
14934 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14935 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14936 int end = (WINDOW_TOP_EDGE_LINE (w)
14937 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14938 + window_internal_height (w));
14940 /* Perform the operation on the screen. */
14941 if (dvpos > 0)
14943 /* Scroll last_unchanged_at_beg_row to the end of the
14944 window down dvpos lines. */
14945 set_terminal_window (end);
14947 /* On dumb terminals delete dvpos lines at the end
14948 before inserting dvpos empty lines. */
14949 if (!scroll_region_ok)
14950 ins_del_lines (end - dvpos, -dvpos);
14952 /* Insert dvpos empty lines in front of
14953 last_unchanged_at_beg_row. */
14954 ins_del_lines (from, dvpos);
14956 else if (dvpos < 0)
14958 /* Scroll up last_unchanged_at_beg_vpos to the end of
14959 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14960 set_terminal_window (end);
14962 /* Delete dvpos lines in front of
14963 last_unchanged_at_beg_vpos. ins_del_lines will set
14964 the cursor to the given vpos and emit |dvpos| delete
14965 line sequences. */
14966 ins_del_lines (from + dvpos, dvpos);
14968 /* On a dumb terminal insert dvpos empty lines at the
14969 end. */
14970 if (!scroll_region_ok)
14971 ins_del_lines (end + dvpos, -dvpos);
14974 set_terminal_window (0);
14977 update_end (f);
14980 /* Shift reused rows of the current matrix to the right position.
14981 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14982 text. */
14983 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14984 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14985 if (dvpos < 0)
14987 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14988 bottom_vpos, dvpos);
14989 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14990 bottom_vpos, 0);
14992 else if (dvpos > 0)
14994 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14995 bottom_vpos, dvpos);
14996 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14997 first_unchanged_at_end_vpos + dvpos, 0);
15000 /* For frame-based redisplay, make sure that current frame and window
15001 matrix are in sync with respect to glyph memory. */
15002 if (!FRAME_WINDOW_P (f))
15003 sync_frame_with_window_matrix_rows (w);
15005 /* Adjust buffer positions in reused rows. */
15006 if (delta || delta_bytes)
15007 increment_matrix_positions (current_matrix,
15008 first_unchanged_at_end_vpos + dvpos,
15009 bottom_vpos, delta, delta_bytes);
15011 /* Adjust Y positions. */
15012 if (dy)
15013 shift_glyph_matrix (w, current_matrix,
15014 first_unchanged_at_end_vpos + dvpos,
15015 bottom_vpos, dy);
15017 if (first_unchanged_at_end_row)
15019 first_unchanged_at_end_row += dvpos;
15020 if (first_unchanged_at_end_row->y >= it.last_visible_y
15021 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15022 first_unchanged_at_end_row = NULL;
15025 /* If scrolling up, there may be some lines to display at the end of
15026 the window. */
15027 last_text_row_at_end = NULL;
15028 if (dy < 0)
15030 /* Scrolling up can leave for example a partially visible line
15031 at the end of the window to be redisplayed. */
15032 /* Set last_row to the glyph row in the current matrix where the
15033 window end line is found. It has been moved up or down in
15034 the matrix by dvpos. */
15035 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15036 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15038 /* If last_row is the window end line, it should display text. */
15039 xassert (last_row->displays_text_p);
15041 /* If window end line was partially visible before, begin
15042 displaying at that line. Otherwise begin displaying with the
15043 line following it. */
15044 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15046 init_to_row_start (&it, w, last_row);
15047 it.vpos = last_vpos;
15048 it.current_y = last_row->y;
15050 else
15052 init_to_row_end (&it, w, last_row);
15053 it.vpos = 1 + last_vpos;
15054 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15055 ++last_row;
15058 /* We may start in a continuation line. If so, we have to
15059 get the right continuation_lines_width and current_x. */
15060 it.continuation_lines_width = last_row->continuation_lines_width;
15061 it.hpos = it.current_x = 0;
15063 /* Display the rest of the lines at the window end. */
15064 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15065 while (it.current_y < it.last_visible_y
15066 && !fonts_changed_p)
15068 /* Is it always sure that the display agrees with lines in
15069 the current matrix? I don't think so, so we mark rows
15070 displayed invalid in the current matrix by setting their
15071 enabled_p flag to zero. */
15072 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15073 if (display_line (&it))
15074 last_text_row_at_end = it.glyph_row - 1;
15078 /* Update window_end_pos and window_end_vpos. */
15079 if (first_unchanged_at_end_row
15080 && !last_text_row_at_end)
15082 /* Window end line if one of the preserved rows from the current
15083 matrix. Set row to the last row displaying text in current
15084 matrix starting at first_unchanged_at_end_row, after
15085 scrolling. */
15086 xassert (first_unchanged_at_end_row->displays_text_p);
15087 row = find_last_row_displaying_text (w->current_matrix, &it,
15088 first_unchanged_at_end_row);
15089 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15091 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15092 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15093 w->window_end_vpos
15094 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15095 xassert (w->window_end_bytepos >= 0);
15096 IF_DEBUG (debug_method_add (w, "A"));
15098 else if (last_text_row_at_end)
15100 w->window_end_pos
15101 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15102 w->window_end_bytepos
15103 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15104 w->window_end_vpos
15105 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15106 xassert (w->window_end_bytepos >= 0);
15107 IF_DEBUG (debug_method_add (w, "B"));
15109 else if (last_text_row)
15111 /* We have displayed either to the end of the window or at the
15112 end of the window, i.e. the last row with text is to be found
15113 in the desired matrix. */
15114 w->window_end_pos
15115 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15116 w->window_end_bytepos
15117 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15118 w->window_end_vpos
15119 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15120 xassert (w->window_end_bytepos >= 0);
15122 else if (first_unchanged_at_end_row == NULL
15123 && last_text_row == NULL
15124 && last_text_row_at_end == NULL)
15126 /* Displayed to end of window, but no line containing text was
15127 displayed. Lines were deleted at the end of the window. */
15128 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15129 int vpos = XFASTINT (w->window_end_vpos);
15130 struct glyph_row *current_row = current_matrix->rows + vpos;
15131 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15133 for (row = NULL;
15134 row == NULL && vpos >= first_vpos;
15135 --vpos, --current_row, --desired_row)
15137 if (desired_row->enabled_p)
15139 if (desired_row->displays_text_p)
15140 row = desired_row;
15142 else if (current_row->displays_text_p)
15143 row = current_row;
15146 xassert (row != NULL);
15147 w->window_end_vpos = make_number (vpos + 1);
15148 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15149 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15150 xassert (w->window_end_bytepos >= 0);
15151 IF_DEBUG (debug_method_add (w, "C"));
15153 else
15154 abort ();
15156 #if 0 /* This leads to problems, for instance when the cursor is
15157 at ZV, and the cursor line displays no text. */
15158 /* Disable rows below what's displayed in the window. This makes
15159 debugging easier. */
15160 enable_glyph_matrix_rows (current_matrix,
15161 XFASTINT (w->window_end_vpos) + 1,
15162 bottom_vpos, 0);
15163 #endif
15165 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15166 debug_end_vpos = XFASTINT (w->window_end_vpos));
15168 /* Record that display has not been completed. */
15169 w->window_end_valid = Qnil;
15170 w->desired_matrix->no_scrolling_p = 1;
15171 return 3;
15173 #undef GIVE_UP
15178 /***********************************************************************
15179 More debugging support
15180 ***********************************************************************/
15182 #if GLYPH_DEBUG
15184 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15185 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15186 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15189 /* Dump the contents of glyph matrix MATRIX on stderr.
15191 GLYPHS 0 means don't show glyph contents.
15192 GLYPHS 1 means show glyphs in short form
15193 GLYPHS > 1 means show glyphs in long form. */
15195 void
15196 dump_glyph_matrix (matrix, glyphs)
15197 struct glyph_matrix *matrix;
15198 int glyphs;
15200 int i;
15201 for (i = 0; i < matrix->nrows; ++i)
15202 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15206 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15207 the glyph row and area where the glyph comes from. */
15209 void
15210 dump_glyph (row, glyph, area)
15211 struct glyph_row *row;
15212 struct glyph *glyph;
15213 int area;
15215 if (glyph->type == CHAR_GLYPH)
15217 fprintf (stderr,
15218 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15219 glyph - row->glyphs[TEXT_AREA],
15220 'C',
15221 glyph->charpos,
15222 (BUFFERP (glyph->object)
15223 ? 'B'
15224 : (STRINGP (glyph->object)
15225 ? 'S'
15226 : '-')),
15227 glyph->pixel_width,
15228 glyph->u.ch,
15229 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15230 ? glyph->u.ch
15231 : '.'),
15232 glyph->face_id,
15233 glyph->left_box_line_p,
15234 glyph->right_box_line_p);
15236 else if (glyph->type == STRETCH_GLYPH)
15238 fprintf (stderr,
15239 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15240 glyph - row->glyphs[TEXT_AREA],
15241 'S',
15242 glyph->charpos,
15243 (BUFFERP (glyph->object)
15244 ? 'B'
15245 : (STRINGP (glyph->object)
15246 ? 'S'
15247 : '-')),
15248 glyph->pixel_width,
15250 '.',
15251 glyph->face_id,
15252 glyph->left_box_line_p,
15253 glyph->right_box_line_p);
15255 else if (glyph->type == IMAGE_GLYPH)
15257 fprintf (stderr,
15258 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15259 glyph - row->glyphs[TEXT_AREA],
15260 'I',
15261 glyph->charpos,
15262 (BUFFERP (glyph->object)
15263 ? 'B'
15264 : (STRINGP (glyph->object)
15265 ? 'S'
15266 : '-')),
15267 glyph->pixel_width,
15268 glyph->u.img_id,
15269 '.',
15270 glyph->face_id,
15271 glyph->left_box_line_p,
15272 glyph->right_box_line_p);
15274 else if (glyph->type == COMPOSITE_GLYPH)
15276 fprintf (stderr,
15277 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15278 glyph - row->glyphs[TEXT_AREA],
15279 '+',
15280 glyph->charpos,
15281 (BUFFERP (glyph->object)
15282 ? 'B'
15283 : (STRINGP (glyph->object)
15284 ? 'S'
15285 : '-')),
15286 glyph->pixel_width,
15287 glyph->u.cmp_id,
15288 '.',
15289 glyph->face_id,
15290 glyph->left_box_line_p,
15291 glyph->right_box_line_p);
15296 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15297 GLYPHS 0 means don't show glyph contents.
15298 GLYPHS 1 means show glyphs in short form
15299 GLYPHS > 1 means show glyphs in long form. */
15301 void
15302 dump_glyph_row (row, vpos, glyphs)
15303 struct glyph_row *row;
15304 int vpos, glyphs;
15306 if (glyphs != 1)
15308 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15309 fprintf (stderr, "======================================================================\n");
15311 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15312 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15313 vpos,
15314 MATRIX_ROW_START_CHARPOS (row),
15315 MATRIX_ROW_END_CHARPOS (row),
15316 row->used[TEXT_AREA],
15317 row->contains_overlapping_glyphs_p,
15318 row->enabled_p,
15319 row->truncated_on_left_p,
15320 row->truncated_on_right_p,
15321 row->continued_p,
15322 MATRIX_ROW_CONTINUATION_LINE_P (row),
15323 row->displays_text_p,
15324 row->ends_at_zv_p,
15325 row->fill_line_p,
15326 row->ends_in_middle_of_char_p,
15327 row->starts_in_middle_of_char_p,
15328 row->mouse_face_p,
15329 row->x,
15330 row->y,
15331 row->pixel_width,
15332 row->height,
15333 row->visible_height,
15334 row->ascent,
15335 row->phys_ascent);
15336 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15337 row->end.overlay_string_index,
15338 row->continuation_lines_width);
15339 fprintf (stderr, "%9d %5d\n",
15340 CHARPOS (row->start.string_pos),
15341 CHARPOS (row->end.string_pos));
15342 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15343 row->end.dpvec_index);
15346 if (glyphs > 1)
15348 int area;
15350 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15352 struct glyph *glyph = row->glyphs[area];
15353 struct glyph *glyph_end = glyph + row->used[area];
15355 /* Glyph for a line end in text. */
15356 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15357 ++glyph_end;
15359 if (glyph < glyph_end)
15360 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15362 for (; glyph < glyph_end; ++glyph)
15363 dump_glyph (row, glyph, area);
15366 else if (glyphs == 1)
15368 int area;
15370 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15372 char *s = (char *) alloca (row->used[area] + 1);
15373 int i;
15375 for (i = 0; i < row->used[area]; ++i)
15377 struct glyph *glyph = row->glyphs[area] + i;
15378 if (glyph->type == CHAR_GLYPH
15379 && glyph->u.ch < 0x80
15380 && glyph->u.ch >= ' ')
15381 s[i] = glyph->u.ch;
15382 else
15383 s[i] = '.';
15386 s[i] = '\0';
15387 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15393 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15394 Sdump_glyph_matrix, 0, 1, "p",
15395 doc: /* Dump the current matrix of the selected window to stderr.
15396 Shows contents of glyph row structures. With non-nil
15397 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15398 glyphs in short form, otherwise show glyphs in long form. */)
15399 (glyphs)
15400 Lisp_Object glyphs;
15402 struct window *w = XWINDOW (selected_window);
15403 struct buffer *buffer = XBUFFER (w->buffer);
15405 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15406 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15407 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15408 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15409 fprintf (stderr, "=============================================\n");
15410 dump_glyph_matrix (w->current_matrix,
15411 NILP (glyphs) ? 0 : XINT (glyphs));
15412 return Qnil;
15416 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15417 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15420 struct frame *f = XFRAME (selected_frame);
15421 dump_glyph_matrix (f->current_matrix, 1);
15422 return Qnil;
15426 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15427 doc: /* Dump glyph row ROW to stderr.
15428 GLYPH 0 means don't dump glyphs.
15429 GLYPH 1 means dump glyphs in short form.
15430 GLYPH > 1 or omitted means dump glyphs in long form. */)
15431 (row, glyphs)
15432 Lisp_Object row, glyphs;
15434 struct glyph_matrix *matrix;
15435 int vpos;
15437 CHECK_NUMBER (row);
15438 matrix = XWINDOW (selected_window)->current_matrix;
15439 vpos = XINT (row);
15440 if (vpos >= 0 && vpos < matrix->nrows)
15441 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15442 vpos,
15443 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15444 return Qnil;
15448 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15449 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15450 GLYPH 0 means don't dump glyphs.
15451 GLYPH 1 means dump glyphs in short form.
15452 GLYPH > 1 or omitted means dump glyphs in long form. */)
15453 (row, glyphs)
15454 Lisp_Object row, glyphs;
15456 struct frame *sf = SELECTED_FRAME ();
15457 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15458 int vpos;
15460 CHECK_NUMBER (row);
15461 vpos = XINT (row);
15462 if (vpos >= 0 && vpos < m->nrows)
15463 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15464 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15465 return Qnil;
15469 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15470 doc: /* Toggle tracing of redisplay.
15471 With ARG, turn tracing on if and only if ARG is positive. */)
15472 (arg)
15473 Lisp_Object arg;
15475 if (NILP (arg))
15476 trace_redisplay_p = !trace_redisplay_p;
15477 else
15479 arg = Fprefix_numeric_value (arg);
15480 trace_redisplay_p = XINT (arg) > 0;
15483 return Qnil;
15487 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15488 doc: /* Like `format', but print result to stderr.
15489 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15490 (nargs, args)
15491 int nargs;
15492 Lisp_Object *args;
15494 Lisp_Object s = Fformat (nargs, args);
15495 fprintf (stderr, "%s", SDATA (s));
15496 return Qnil;
15499 #endif /* GLYPH_DEBUG */
15503 /***********************************************************************
15504 Building Desired Matrix Rows
15505 ***********************************************************************/
15507 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15508 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15510 static struct glyph_row *
15511 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15512 struct window *w;
15513 Lisp_Object overlay_arrow_string;
15515 struct frame *f = XFRAME (WINDOW_FRAME (w));
15516 struct buffer *buffer = XBUFFER (w->buffer);
15517 struct buffer *old = current_buffer;
15518 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15519 int arrow_len = SCHARS (overlay_arrow_string);
15520 const unsigned char *arrow_end = arrow_string + arrow_len;
15521 const unsigned char *p;
15522 struct it it;
15523 int multibyte_p;
15524 int n_glyphs_before;
15526 set_buffer_temp (buffer);
15527 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15528 it.glyph_row->used[TEXT_AREA] = 0;
15529 SET_TEXT_POS (it.position, 0, 0);
15531 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15532 p = arrow_string;
15533 while (p < arrow_end)
15535 Lisp_Object face, ilisp;
15537 /* Get the next character. */
15538 if (multibyte_p)
15539 it.c = string_char_and_length (p, arrow_len, &it.len);
15540 else
15541 it.c = *p, it.len = 1;
15542 p += it.len;
15544 /* Get its face. */
15545 ilisp = make_number (p - arrow_string);
15546 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15547 it.face_id = compute_char_face (f, it.c, face);
15549 /* Compute its width, get its glyphs. */
15550 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15551 SET_TEXT_POS (it.position, -1, -1);
15552 PRODUCE_GLYPHS (&it);
15554 /* If this character doesn't fit any more in the line, we have
15555 to remove some glyphs. */
15556 if (it.current_x > it.last_visible_x)
15558 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15559 break;
15563 set_buffer_temp (old);
15564 return it.glyph_row;
15568 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15569 glyphs are only inserted for terminal frames since we can't really
15570 win with truncation glyphs when partially visible glyphs are
15571 involved. Which glyphs to insert is determined by
15572 produce_special_glyphs. */
15574 static void
15575 insert_left_trunc_glyphs (it)
15576 struct it *it;
15578 struct it truncate_it;
15579 struct glyph *from, *end, *to, *toend;
15581 xassert (!FRAME_WINDOW_P (it->f));
15583 /* Get the truncation glyphs. */
15584 truncate_it = *it;
15585 truncate_it.current_x = 0;
15586 truncate_it.face_id = DEFAULT_FACE_ID;
15587 truncate_it.glyph_row = &scratch_glyph_row;
15588 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15589 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15590 truncate_it.object = make_number (0);
15591 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15593 /* Overwrite glyphs from IT with truncation glyphs. */
15594 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15595 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15596 to = it->glyph_row->glyphs[TEXT_AREA];
15597 toend = to + it->glyph_row->used[TEXT_AREA];
15599 while (from < end)
15600 *to++ = *from++;
15602 /* There may be padding glyphs left over. Overwrite them too. */
15603 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15605 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15606 while (from < end)
15607 *to++ = *from++;
15610 if (to > toend)
15611 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15615 /* Compute the pixel height and width of IT->glyph_row.
15617 Most of the time, ascent and height of a display line will be equal
15618 to the max_ascent and max_height values of the display iterator
15619 structure. This is not the case if
15621 1. We hit ZV without displaying anything. In this case, max_ascent
15622 and max_height will be zero.
15624 2. We have some glyphs that don't contribute to the line height.
15625 (The glyph row flag contributes_to_line_height_p is for future
15626 pixmap extensions).
15628 The first case is easily covered by using default values because in
15629 these cases, the line height does not really matter, except that it
15630 must not be zero. */
15632 static void
15633 compute_line_metrics (it)
15634 struct it *it;
15636 struct glyph_row *row = it->glyph_row;
15637 int area, i;
15639 if (FRAME_WINDOW_P (it->f))
15641 int i, min_y, max_y;
15643 /* The line may consist of one space only, that was added to
15644 place the cursor on it. If so, the row's height hasn't been
15645 computed yet. */
15646 if (row->height == 0)
15648 if (it->max_ascent + it->max_descent == 0)
15649 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15650 row->ascent = it->max_ascent;
15651 row->height = it->max_ascent + it->max_descent;
15652 row->phys_ascent = it->max_phys_ascent;
15653 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15654 row->extra_line_spacing = it->max_extra_line_spacing;
15657 /* Compute the width of this line. */
15658 row->pixel_width = row->x;
15659 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15660 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15662 xassert (row->pixel_width >= 0);
15663 xassert (row->ascent >= 0 && row->height > 0);
15665 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15666 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15668 /* If first line's physical ascent is larger than its logical
15669 ascent, use the physical ascent, and make the row taller.
15670 This makes accented characters fully visible. */
15671 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15672 && row->phys_ascent > row->ascent)
15674 row->height += row->phys_ascent - row->ascent;
15675 row->ascent = row->phys_ascent;
15678 /* Compute how much of the line is visible. */
15679 row->visible_height = row->height;
15681 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15682 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15684 if (row->y < min_y)
15685 row->visible_height -= min_y - row->y;
15686 if (row->y + row->height > max_y)
15687 row->visible_height -= row->y + row->height - max_y;
15689 else
15691 row->pixel_width = row->used[TEXT_AREA];
15692 if (row->continued_p)
15693 row->pixel_width -= it->continuation_pixel_width;
15694 else if (row->truncated_on_right_p)
15695 row->pixel_width -= it->truncation_pixel_width;
15696 row->ascent = row->phys_ascent = 0;
15697 row->height = row->phys_height = row->visible_height = 1;
15698 row->extra_line_spacing = 0;
15701 /* Compute a hash code for this row. */
15702 row->hash = 0;
15703 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15704 for (i = 0; i < row->used[area]; ++i)
15705 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15706 + row->glyphs[area][i].u.val
15707 + row->glyphs[area][i].face_id
15708 + row->glyphs[area][i].padding_p
15709 + (row->glyphs[area][i].type << 2));
15711 it->max_ascent = it->max_descent = 0;
15712 it->max_phys_ascent = it->max_phys_descent = 0;
15716 /* Append one space to the glyph row of iterator IT if doing a
15717 window-based redisplay. The space has the same face as
15718 IT->face_id. Value is non-zero if a space was added.
15720 This function is called to make sure that there is always one glyph
15721 at the end of a glyph row that the cursor can be set on under
15722 window-systems. (If there weren't such a glyph we would not know
15723 how wide and tall a box cursor should be displayed).
15725 At the same time this space let's a nicely handle clearing to the
15726 end of the line if the row ends in italic text. */
15728 static int
15729 append_space_for_newline (it, default_face_p)
15730 struct it *it;
15731 int default_face_p;
15733 if (FRAME_WINDOW_P (it->f))
15735 int n = it->glyph_row->used[TEXT_AREA];
15737 if (it->glyph_row->glyphs[TEXT_AREA] + n
15738 < it->glyph_row->glyphs[1 + TEXT_AREA])
15740 /* Save some values that must not be changed.
15741 Must save IT->c and IT->len because otherwise
15742 ITERATOR_AT_END_P wouldn't work anymore after
15743 append_space_for_newline has been called. */
15744 enum display_element_type saved_what = it->what;
15745 int saved_c = it->c, saved_len = it->len;
15746 int saved_x = it->current_x;
15747 int saved_face_id = it->face_id;
15748 struct text_pos saved_pos;
15749 Lisp_Object saved_object;
15750 struct face *face;
15752 saved_object = it->object;
15753 saved_pos = it->position;
15755 it->what = IT_CHARACTER;
15756 bzero (&it->position, sizeof it->position);
15757 it->object = make_number (0);
15758 it->c = ' ';
15759 it->len = 1;
15761 if (default_face_p)
15762 it->face_id = DEFAULT_FACE_ID;
15763 else if (it->face_before_selective_p)
15764 it->face_id = it->saved_face_id;
15765 face = FACE_FROM_ID (it->f, it->face_id);
15766 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15768 PRODUCE_GLYPHS (it);
15770 it->override_ascent = -1;
15771 it->constrain_row_ascent_descent_p = 0;
15772 it->current_x = saved_x;
15773 it->object = saved_object;
15774 it->position = saved_pos;
15775 it->what = saved_what;
15776 it->face_id = saved_face_id;
15777 it->len = saved_len;
15778 it->c = saved_c;
15779 return 1;
15783 return 0;
15787 /* Extend the face of the last glyph in the text area of IT->glyph_row
15788 to the end of the display line. Called from display_line.
15789 If the glyph row is empty, add a space glyph to it so that we
15790 know the face to draw. Set the glyph row flag fill_line_p. */
15792 static void
15793 extend_face_to_end_of_line (it)
15794 struct it *it;
15796 struct face *face;
15797 struct frame *f = it->f;
15799 /* If line is already filled, do nothing. */
15800 if (it->current_x >= it->last_visible_x)
15801 return;
15803 /* Face extension extends the background and box of IT->face_id
15804 to the end of the line. If the background equals the background
15805 of the frame, we don't have to do anything. */
15806 if (it->face_before_selective_p)
15807 face = FACE_FROM_ID (it->f, it->saved_face_id);
15808 else
15809 face = FACE_FROM_ID (f, it->face_id);
15811 if (FRAME_WINDOW_P (f)
15812 && it->glyph_row->displays_text_p
15813 && face->box == FACE_NO_BOX
15814 && face->background == FRAME_BACKGROUND_PIXEL (f)
15815 && !face->stipple)
15816 return;
15818 /* Set the glyph row flag indicating that the face of the last glyph
15819 in the text area has to be drawn to the end of the text area. */
15820 it->glyph_row->fill_line_p = 1;
15822 /* If current character of IT is not ASCII, make sure we have the
15823 ASCII face. This will be automatically undone the next time
15824 get_next_display_element returns a multibyte character. Note
15825 that the character will always be single byte in unibyte text. */
15826 if (!SINGLE_BYTE_CHAR_P (it->c))
15828 it->face_id = FACE_FOR_CHAR (f, face, 0);
15831 if (FRAME_WINDOW_P (f))
15833 /* If the row is empty, add a space with the current face of IT,
15834 so that we know which face to draw. */
15835 if (it->glyph_row->used[TEXT_AREA] == 0)
15837 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15838 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15839 it->glyph_row->used[TEXT_AREA] = 1;
15842 else
15844 /* Save some values that must not be changed. */
15845 int saved_x = it->current_x;
15846 struct text_pos saved_pos;
15847 Lisp_Object saved_object;
15848 enum display_element_type saved_what = it->what;
15849 int saved_face_id = it->face_id;
15851 saved_object = it->object;
15852 saved_pos = it->position;
15854 it->what = IT_CHARACTER;
15855 bzero (&it->position, sizeof it->position);
15856 it->object = make_number (0);
15857 it->c = ' ';
15858 it->len = 1;
15859 it->face_id = face->id;
15861 PRODUCE_GLYPHS (it);
15863 while (it->current_x <= it->last_visible_x)
15864 PRODUCE_GLYPHS (it);
15866 /* Don't count these blanks really. It would let us insert a left
15867 truncation glyph below and make us set the cursor on them, maybe. */
15868 it->current_x = saved_x;
15869 it->object = saved_object;
15870 it->position = saved_pos;
15871 it->what = saved_what;
15872 it->face_id = saved_face_id;
15877 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15878 trailing whitespace. */
15880 static int
15881 trailing_whitespace_p (charpos)
15882 int charpos;
15884 int bytepos = CHAR_TO_BYTE (charpos);
15885 int c = 0;
15887 while (bytepos < ZV_BYTE
15888 && (c = FETCH_CHAR (bytepos),
15889 c == ' ' || c == '\t'))
15890 ++bytepos;
15892 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15894 if (bytepos != PT_BYTE)
15895 return 1;
15897 return 0;
15901 /* Highlight trailing whitespace, if any, in ROW. */
15903 void
15904 highlight_trailing_whitespace (f, row)
15905 struct frame *f;
15906 struct glyph_row *row;
15908 int used = row->used[TEXT_AREA];
15910 if (used)
15912 struct glyph *start = row->glyphs[TEXT_AREA];
15913 struct glyph *glyph = start + used - 1;
15915 /* Skip over glyphs inserted to display the cursor at the
15916 end of a line, for extending the face of the last glyph
15917 to the end of the line on terminals, and for truncation
15918 and continuation glyphs. */
15919 while (glyph >= start
15920 && glyph->type == CHAR_GLYPH
15921 && INTEGERP (glyph->object))
15922 --glyph;
15924 /* If last glyph is a space or stretch, and it's trailing
15925 whitespace, set the face of all trailing whitespace glyphs in
15926 IT->glyph_row to `trailing-whitespace'. */
15927 if (glyph >= start
15928 && BUFFERP (glyph->object)
15929 && (glyph->type == STRETCH_GLYPH
15930 || (glyph->type == CHAR_GLYPH
15931 && glyph->u.ch == ' '))
15932 && trailing_whitespace_p (glyph->charpos))
15934 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15935 if (face_id < 0)
15936 return;
15938 while (glyph >= start
15939 && BUFFERP (glyph->object)
15940 && (glyph->type == STRETCH_GLYPH
15941 || (glyph->type == CHAR_GLYPH
15942 && glyph->u.ch == ' ')))
15943 (glyph--)->face_id = face_id;
15949 /* Value is non-zero if glyph row ROW in window W should be
15950 used to hold the cursor. */
15952 static int
15953 cursor_row_p (w, row)
15954 struct window *w;
15955 struct glyph_row *row;
15957 int cursor_row_p = 1;
15959 if (PT == MATRIX_ROW_END_CHARPOS (row))
15961 /* Suppose the row ends on a string.
15962 Unless the row is continued, that means it ends on a newline
15963 in the string. If it's anything other than a display string
15964 (e.g. a before-string from an overlay), we don't want the
15965 cursor there. (This heuristic seems to give the optimal
15966 behavior for the various types of multi-line strings.) */
15967 if (CHARPOS (row->end.string_pos) >= 0)
15969 if (row->continued_p)
15970 cursor_row_p = 1;
15971 else
15973 /* Check for `display' property. */
15974 struct glyph *beg = row->glyphs[TEXT_AREA];
15975 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15976 struct glyph *glyph;
15978 cursor_row_p = 0;
15979 for (glyph = end; glyph >= beg; --glyph)
15980 if (STRINGP (glyph->object))
15982 Lisp_Object prop
15983 = Fget_char_property (make_number (PT),
15984 Qdisplay, Qnil);
15985 cursor_row_p =
15986 (!NILP (prop)
15987 && display_prop_string_p (prop, glyph->object));
15988 break;
15992 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15994 /* If the row ends in middle of a real character,
15995 and the line is continued, we want the cursor here.
15996 That's because MATRIX_ROW_END_CHARPOS would equal
15997 PT if PT is before the character. */
15998 if (!row->ends_in_ellipsis_p)
15999 cursor_row_p = row->continued_p;
16000 else
16001 /* If the row ends in an ellipsis, then
16002 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16003 We want that position to be displayed after the ellipsis. */
16004 cursor_row_p = 0;
16006 /* If the row ends at ZV, display the cursor at the end of that
16007 row instead of at the start of the row below. */
16008 else if (row->ends_at_zv_p)
16009 cursor_row_p = 1;
16010 else
16011 cursor_row_p = 0;
16014 return cursor_row_p;
16018 /* Construct the glyph row IT->glyph_row in the desired matrix of
16019 IT->w from text at the current position of IT. See dispextern.h
16020 for an overview of struct it. Value is non-zero if
16021 IT->glyph_row displays text, as opposed to a line displaying ZV
16022 only. */
16024 static int
16025 display_line (it)
16026 struct it *it;
16028 struct glyph_row *row = it->glyph_row;
16029 Lisp_Object overlay_arrow_string;
16031 /* We always start displaying at hpos zero even if hscrolled. */
16032 xassert (it->hpos == 0 && it->current_x == 0);
16034 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16035 >= it->w->desired_matrix->nrows)
16037 it->w->nrows_scale_factor++;
16038 fonts_changed_p = 1;
16039 return 0;
16042 /* Is IT->w showing the region? */
16043 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16045 /* Clear the result glyph row and enable it. */
16046 prepare_desired_row (row);
16048 row->y = it->current_y;
16049 row->start = it->start;
16050 row->continuation_lines_width = it->continuation_lines_width;
16051 row->displays_text_p = 1;
16052 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16053 it->starts_in_middle_of_char_p = 0;
16055 /* Arrange the overlays nicely for our purposes. Usually, we call
16056 display_line on only one line at a time, in which case this
16057 can't really hurt too much, or we call it on lines which appear
16058 one after another in the buffer, in which case all calls to
16059 recenter_overlay_lists but the first will be pretty cheap. */
16060 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16062 /* Move over display elements that are not visible because we are
16063 hscrolled. This may stop at an x-position < IT->first_visible_x
16064 if the first glyph is partially visible or if we hit a line end. */
16065 if (it->current_x < it->first_visible_x)
16067 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16068 MOVE_TO_POS | MOVE_TO_X);
16071 /* Get the initial row height. This is either the height of the
16072 text hscrolled, if there is any, or zero. */
16073 row->ascent = it->max_ascent;
16074 row->height = it->max_ascent + it->max_descent;
16075 row->phys_ascent = it->max_phys_ascent;
16076 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16077 row->extra_line_spacing = it->max_extra_line_spacing;
16079 /* Loop generating characters. The loop is left with IT on the next
16080 character to display. */
16081 while (1)
16083 int n_glyphs_before, hpos_before, x_before;
16084 int x, i, nglyphs;
16085 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16087 /* Retrieve the next thing to display. Value is zero if end of
16088 buffer reached. */
16089 if (!get_next_display_element (it))
16091 /* Maybe add a space at the end of this line that is used to
16092 display the cursor there under X. Set the charpos of the
16093 first glyph of blank lines not corresponding to any text
16094 to -1. */
16095 #ifdef HAVE_WINDOW_SYSTEM
16096 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16097 row->exact_window_width_line_p = 1;
16098 else
16099 #endif /* HAVE_WINDOW_SYSTEM */
16100 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16101 || row->used[TEXT_AREA] == 0)
16103 row->glyphs[TEXT_AREA]->charpos = -1;
16104 row->displays_text_p = 0;
16106 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16107 && (!MINI_WINDOW_P (it->w)
16108 || (minibuf_level && EQ (it->window, minibuf_window))))
16109 row->indicate_empty_line_p = 1;
16112 it->continuation_lines_width = 0;
16113 row->ends_at_zv_p = 1;
16114 break;
16117 /* Now, get the metrics of what we want to display. This also
16118 generates glyphs in `row' (which is IT->glyph_row). */
16119 n_glyphs_before = row->used[TEXT_AREA];
16120 x = it->current_x;
16122 /* Remember the line height so far in case the next element doesn't
16123 fit on the line. */
16124 if (!it->truncate_lines_p)
16126 ascent = it->max_ascent;
16127 descent = it->max_descent;
16128 phys_ascent = it->max_phys_ascent;
16129 phys_descent = it->max_phys_descent;
16132 PRODUCE_GLYPHS (it);
16134 /* If this display element was in marginal areas, continue with
16135 the next one. */
16136 if (it->area != TEXT_AREA)
16138 row->ascent = max (row->ascent, it->max_ascent);
16139 row->height = max (row->height, it->max_ascent + it->max_descent);
16140 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16141 row->phys_height = max (row->phys_height,
16142 it->max_phys_ascent + it->max_phys_descent);
16143 row->extra_line_spacing = max (row->extra_line_spacing,
16144 it->max_extra_line_spacing);
16145 set_iterator_to_next (it, 1);
16146 continue;
16149 /* Does the display element fit on the line? If we truncate
16150 lines, we should draw past the right edge of the window. If
16151 we don't truncate, we want to stop so that we can display the
16152 continuation glyph before the right margin. If lines are
16153 continued, there are two possible strategies for characters
16154 resulting in more than 1 glyph (e.g. tabs): Display as many
16155 glyphs as possible in this line and leave the rest for the
16156 continuation line, or display the whole element in the next
16157 line. Original redisplay did the former, so we do it also. */
16158 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16159 hpos_before = it->hpos;
16160 x_before = x;
16162 if (/* Not a newline. */
16163 nglyphs > 0
16164 /* Glyphs produced fit entirely in the line. */
16165 && it->current_x < it->last_visible_x)
16167 it->hpos += nglyphs;
16168 row->ascent = max (row->ascent, it->max_ascent);
16169 row->height = max (row->height, it->max_ascent + it->max_descent);
16170 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16171 row->phys_height = max (row->phys_height,
16172 it->max_phys_ascent + it->max_phys_descent);
16173 row->extra_line_spacing = max (row->extra_line_spacing,
16174 it->max_extra_line_spacing);
16175 if (it->current_x - it->pixel_width < it->first_visible_x)
16176 row->x = x - it->first_visible_x;
16178 else
16180 int new_x;
16181 struct glyph *glyph;
16183 for (i = 0; i < nglyphs; ++i, x = new_x)
16185 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16186 new_x = x + glyph->pixel_width;
16188 if (/* Lines are continued. */
16189 !it->truncate_lines_p
16190 && (/* Glyph doesn't fit on the line. */
16191 new_x > it->last_visible_x
16192 /* Or it fits exactly on a window system frame. */
16193 || (new_x == it->last_visible_x
16194 && FRAME_WINDOW_P (it->f))))
16196 /* End of a continued line. */
16198 if (it->hpos == 0
16199 || (new_x == it->last_visible_x
16200 && FRAME_WINDOW_P (it->f)))
16202 /* Current glyph is the only one on the line or
16203 fits exactly on the line. We must continue
16204 the line because we can't draw the cursor
16205 after the glyph. */
16206 row->continued_p = 1;
16207 it->current_x = new_x;
16208 it->continuation_lines_width += new_x;
16209 ++it->hpos;
16210 if (i == nglyphs - 1)
16212 set_iterator_to_next (it, 1);
16213 #ifdef HAVE_WINDOW_SYSTEM
16214 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16216 if (!get_next_display_element (it))
16218 row->exact_window_width_line_p = 1;
16219 it->continuation_lines_width = 0;
16220 row->continued_p = 0;
16221 row->ends_at_zv_p = 1;
16223 else if (ITERATOR_AT_END_OF_LINE_P (it))
16225 row->continued_p = 0;
16226 row->exact_window_width_line_p = 1;
16229 #endif /* HAVE_WINDOW_SYSTEM */
16232 else if (CHAR_GLYPH_PADDING_P (*glyph)
16233 && !FRAME_WINDOW_P (it->f))
16235 /* A padding glyph that doesn't fit on this line.
16236 This means the whole character doesn't fit
16237 on the line. */
16238 row->used[TEXT_AREA] = n_glyphs_before;
16240 /* Fill the rest of the row with continuation
16241 glyphs like in 20.x. */
16242 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16243 < row->glyphs[1 + TEXT_AREA])
16244 produce_special_glyphs (it, IT_CONTINUATION);
16246 row->continued_p = 1;
16247 it->current_x = x_before;
16248 it->continuation_lines_width += x_before;
16250 /* Restore the height to what it was before the
16251 element not fitting on the line. */
16252 it->max_ascent = ascent;
16253 it->max_descent = descent;
16254 it->max_phys_ascent = phys_ascent;
16255 it->max_phys_descent = phys_descent;
16257 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16259 /* A TAB that extends past the right edge of the
16260 window. This produces a single glyph on
16261 window system frames. We leave the glyph in
16262 this row and let it fill the row, but don't
16263 consume the TAB. */
16264 it->continuation_lines_width += it->last_visible_x;
16265 row->ends_in_middle_of_char_p = 1;
16266 row->continued_p = 1;
16267 glyph->pixel_width = it->last_visible_x - x;
16268 it->starts_in_middle_of_char_p = 1;
16270 else
16272 /* Something other than a TAB that draws past
16273 the right edge of the window. Restore
16274 positions to values before the element. */
16275 row->used[TEXT_AREA] = n_glyphs_before + i;
16277 /* Display continuation glyphs. */
16278 if (!FRAME_WINDOW_P (it->f))
16279 produce_special_glyphs (it, IT_CONTINUATION);
16280 row->continued_p = 1;
16282 it->current_x = x_before;
16283 it->continuation_lines_width += x;
16284 extend_face_to_end_of_line (it);
16286 if (nglyphs > 1 && i > 0)
16288 row->ends_in_middle_of_char_p = 1;
16289 it->starts_in_middle_of_char_p = 1;
16292 /* Restore the height to what it was before the
16293 element not fitting on the line. */
16294 it->max_ascent = ascent;
16295 it->max_descent = descent;
16296 it->max_phys_ascent = phys_ascent;
16297 it->max_phys_descent = phys_descent;
16300 break;
16302 else if (new_x > it->first_visible_x)
16304 /* Increment number of glyphs actually displayed. */
16305 ++it->hpos;
16307 if (x < it->first_visible_x)
16308 /* Glyph is partially visible, i.e. row starts at
16309 negative X position. */
16310 row->x = x - it->first_visible_x;
16312 else
16314 /* Glyph is completely off the left margin of the
16315 window. This should not happen because of the
16316 move_it_in_display_line at the start of this
16317 function, unless the text display area of the
16318 window is empty. */
16319 xassert (it->first_visible_x <= it->last_visible_x);
16323 row->ascent = max (row->ascent, it->max_ascent);
16324 row->height = max (row->height, it->max_ascent + it->max_descent);
16325 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16326 row->phys_height = max (row->phys_height,
16327 it->max_phys_ascent + it->max_phys_descent);
16328 row->extra_line_spacing = max (row->extra_line_spacing,
16329 it->max_extra_line_spacing);
16331 /* End of this display line if row is continued. */
16332 if (row->continued_p || row->ends_at_zv_p)
16333 break;
16336 at_end_of_line:
16337 /* Is this a line end? If yes, we're also done, after making
16338 sure that a non-default face is extended up to the right
16339 margin of the window. */
16340 if (ITERATOR_AT_END_OF_LINE_P (it))
16342 int used_before = row->used[TEXT_AREA];
16344 row->ends_in_newline_from_string_p = STRINGP (it->object);
16346 #ifdef HAVE_WINDOW_SYSTEM
16347 /* Add a space at the end of the line that is used to
16348 display the cursor there. */
16349 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16350 append_space_for_newline (it, 0);
16351 #endif /* HAVE_WINDOW_SYSTEM */
16353 /* Extend the face to the end of the line. */
16354 extend_face_to_end_of_line (it);
16356 /* Make sure we have the position. */
16357 if (used_before == 0)
16358 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16360 /* Consume the line end. This skips over invisible lines. */
16361 set_iterator_to_next (it, 1);
16362 it->continuation_lines_width = 0;
16363 break;
16366 /* Proceed with next display element. Note that this skips
16367 over lines invisible because of selective display. */
16368 set_iterator_to_next (it, 1);
16370 /* If we truncate lines, we are done when the last displayed
16371 glyphs reach past the right margin of the window. */
16372 if (it->truncate_lines_p
16373 && (FRAME_WINDOW_P (it->f)
16374 ? (it->current_x >= it->last_visible_x)
16375 : (it->current_x > it->last_visible_x)))
16377 /* Maybe add truncation glyphs. */
16378 if (!FRAME_WINDOW_P (it->f))
16380 int i, n;
16382 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16383 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16384 break;
16386 for (n = row->used[TEXT_AREA]; i < n; ++i)
16388 row->used[TEXT_AREA] = i;
16389 produce_special_glyphs (it, IT_TRUNCATION);
16392 #ifdef HAVE_WINDOW_SYSTEM
16393 else
16395 /* Don't truncate if we can overflow newline into fringe. */
16396 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16398 if (!get_next_display_element (it))
16400 it->continuation_lines_width = 0;
16401 row->ends_at_zv_p = 1;
16402 row->exact_window_width_line_p = 1;
16403 break;
16405 if (ITERATOR_AT_END_OF_LINE_P (it))
16407 row->exact_window_width_line_p = 1;
16408 goto at_end_of_line;
16412 #endif /* HAVE_WINDOW_SYSTEM */
16414 row->truncated_on_right_p = 1;
16415 it->continuation_lines_width = 0;
16416 reseat_at_next_visible_line_start (it, 0);
16417 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16418 it->hpos = hpos_before;
16419 it->current_x = x_before;
16420 break;
16424 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16425 at the left window margin. */
16426 if (it->first_visible_x
16427 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16429 if (!FRAME_WINDOW_P (it->f))
16430 insert_left_trunc_glyphs (it);
16431 row->truncated_on_left_p = 1;
16434 /* If the start of this line is the overlay arrow-position, then
16435 mark this glyph row as the one containing the overlay arrow.
16436 This is clearly a mess with variable size fonts. It would be
16437 better to let it be displayed like cursors under X. */
16438 if ((row->displays_text_p || !overlay_arrow_seen)
16439 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16440 !NILP (overlay_arrow_string)))
16442 /* Overlay arrow in window redisplay is a fringe bitmap. */
16443 if (STRINGP (overlay_arrow_string))
16445 struct glyph_row *arrow_row
16446 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16447 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16448 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16449 struct glyph *p = row->glyphs[TEXT_AREA];
16450 struct glyph *p2, *end;
16452 /* Copy the arrow glyphs. */
16453 while (glyph < arrow_end)
16454 *p++ = *glyph++;
16456 /* Throw away padding glyphs. */
16457 p2 = p;
16458 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16459 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16460 ++p2;
16461 if (p2 > p)
16463 while (p2 < end)
16464 *p++ = *p2++;
16465 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16468 else
16470 xassert (INTEGERP (overlay_arrow_string));
16471 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16473 overlay_arrow_seen = 1;
16476 /* Compute pixel dimensions of this line. */
16477 compute_line_metrics (it);
16479 /* Remember the position at which this line ends. */
16480 row->end = it->current;
16482 /* Record whether this row ends inside an ellipsis. */
16483 row->ends_in_ellipsis_p
16484 = (it->method == GET_FROM_DISPLAY_VECTOR
16485 && it->ellipsis_p);
16487 /* Save fringe bitmaps in this row. */
16488 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16489 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16490 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16491 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16493 it->left_user_fringe_bitmap = 0;
16494 it->left_user_fringe_face_id = 0;
16495 it->right_user_fringe_bitmap = 0;
16496 it->right_user_fringe_face_id = 0;
16498 /* Maybe set the cursor. */
16499 if (it->w->cursor.vpos < 0
16500 && PT >= MATRIX_ROW_START_CHARPOS (row)
16501 && PT <= MATRIX_ROW_END_CHARPOS (row)
16502 && cursor_row_p (it->w, row))
16503 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16505 /* Highlight trailing whitespace. */
16506 if (!NILP (Vshow_trailing_whitespace))
16507 highlight_trailing_whitespace (it->f, it->glyph_row);
16509 /* Prepare for the next line. This line starts horizontally at (X
16510 HPOS) = (0 0). Vertical positions are incremented. As a
16511 convenience for the caller, IT->glyph_row is set to the next
16512 row to be used. */
16513 it->current_x = it->hpos = 0;
16514 it->current_y += row->height;
16515 ++it->vpos;
16516 ++it->glyph_row;
16517 it->start = it->current;
16518 return row->displays_text_p;
16523 /***********************************************************************
16524 Menu Bar
16525 ***********************************************************************/
16527 /* Redisplay the menu bar in the frame for window W.
16529 The menu bar of X frames that don't have X toolkit support is
16530 displayed in a special window W->frame->menu_bar_window.
16532 The menu bar of terminal frames is treated specially as far as
16533 glyph matrices are concerned. Menu bar lines are not part of
16534 windows, so the update is done directly on the frame matrix rows
16535 for the menu bar. */
16537 static void
16538 display_menu_bar (w)
16539 struct window *w;
16541 struct frame *f = XFRAME (WINDOW_FRAME (w));
16542 struct it it;
16543 Lisp_Object items;
16544 int i;
16546 /* Don't do all this for graphical frames. */
16547 #ifdef HAVE_NTGUI
16548 if (!NILP (Vwindow_system))
16549 return;
16550 #endif
16551 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16552 if (FRAME_X_P (f))
16553 return;
16554 #endif
16555 #ifdef MAC_OS
16556 if (FRAME_MAC_P (f))
16557 return;
16558 #endif
16560 #ifdef USE_X_TOOLKIT
16561 xassert (!FRAME_WINDOW_P (f));
16562 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16563 it.first_visible_x = 0;
16564 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16565 #else /* not USE_X_TOOLKIT */
16566 if (FRAME_WINDOW_P (f))
16568 /* Menu bar lines are displayed in the desired matrix of the
16569 dummy window menu_bar_window. */
16570 struct window *menu_w;
16571 xassert (WINDOWP (f->menu_bar_window));
16572 menu_w = XWINDOW (f->menu_bar_window);
16573 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16574 MENU_FACE_ID);
16575 it.first_visible_x = 0;
16576 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16578 else
16580 /* This is a TTY frame, i.e. character hpos/vpos are used as
16581 pixel x/y. */
16582 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16583 MENU_FACE_ID);
16584 it.first_visible_x = 0;
16585 it.last_visible_x = FRAME_COLS (f);
16587 #endif /* not USE_X_TOOLKIT */
16589 if (! mode_line_inverse_video)
16590 /* Force the menu-bar to be displayed in the default face. */
16591 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16593 /* Clear all rows of the menu bar. */
16594 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16596 struct glyph_row *row = it.glyph_row + i;
16597 clear_glyph_row (row);
16598 row->enabled_p = 1;
16599 row->full_width_p = 1;
16602 /* Display all items of the menu bar. */
16603 items = FRAME_MENU_BAR_ITEMS (it.f);
16604 for (i = 0; i < XVECTOR (items)->size; i += 4)
16606 Lisp_Object string;
16608 /* Stop at nil string. */
16609 string = AREF (items, i + 1);
16610 if (NILP (string))
16611 break;
16613 /* Remember where item was displayed. */
16614 AREF (items, i + 3) = make_number (it.hpos);
16616 /* Display the item, pad with one space. */
16617 if (it.current_x < it.last_visible_x)
16618 display_string (NULL, string, Qnil, 0, 0, &it,
16619 SCHARS (string) + 1, 0, 0, -1);
16622 /* Fill out the line with spaces. */
16623 if (it.current_x < it.last_visible_x)
16624 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16626 /* Compute the total height of the lines. */
16627 compute_line_metrics (&it);
16632 /***********************************************************************
16633 Mode Line
16634 ***********************************************************************/
16636 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16637 FORCE is non-zero, redisplay mode lines unconditionally.
16638 Otherwise, redisplay only mode lines that are garbaged. Value is
16639 the number of windows whose mode lines were redisplayed. */
16641 static int
16642 redisplay_mode_lines (window, force)
16643 Lisp_Object window;
16644 int force;
16646 int nwindows = 0;
16648 while (!NILP (window))
16650 struct window *w = XWINDOW (window);
16652 if (WINDOWP (w->hchild))
16653 nwindows += redisplay_mode_lines (w->hchild, force);
16654 else if (WINDOWP (w->vchild))
16655 nwindows += redisplay_mode_lines (w->vchild, force);
16656 else if (force
16657 || FRAME_GARBAGED_P (XFRAME (w->frame))
16658 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16660 struct text_pos lpoint;
16661 struct buffer *old = current_buffer;
16663 /* Set the window's buffer for the mode line display. */
16664 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16665 set_buffer_internal_1 (XBUFFER (w->buffer));
16667 /* Point refers normally to the selected window. For any
16668 other window, set up appropriate value. */
16669 if (!EQ (window, selected_window))
16671 struct text_pos pt;
16673 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16674 if (CHARPOS (pt) < BEGV)
16675 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16676 else if (CHARPOS (pt) > (ZV - 1))
16677 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16678 else
16679 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16682 /* Display mode lines. */
16683 clear_glyph_matrix (w->desired_matrix);
16684 if (display_mode_lines (w))
16686 ++nwindows;
16687 w->must_be_updated_p = 1;
16690 /* Restore old settings. */
16691 set_buffer_internal_1 (old);
16692 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16695 window = w->next;
16698 return nwindows;
16702 /* Display the mode and/or top line of window W. Value is the number
16703 of mode lines displayed. */
16705 static int
16706 display_mode_lines (w)
16707 struct window *w;
16709 Lisp_Object old_selected_window, old_selected_frame;
16710 int n = 0;
16712 old_selected_frame = selected_frame;
16713 selected_frame = w->frame;
16714 old_selected_window = selected_window;
16715 XSETWINDOW (selected_window, w);
16717 /* These will be set while the mode line specs are processed. */
16718 line_number_displayed = 0;
16719 w->column_number_displayed = Qnil;
16721 if (WINDOW_WANTS_MODELINE_P (w))
16723 struct window *sel_w = XWINDOW (old_selected_window);
16725 /* Select mode line face based on the real selected window. */
16726 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16727 current_buffer->mode_line_format);
16728 ++n;
16731 if (WINDOW_WANTS_HEADER_LINE_P (w))
16733 display_mode_line (w, HEADER_LINE_FACE_ID,
16734 current_buffer->header_line_format);
16735 ++n;
16738 selected_frame = old_selected_frame;
16739 selected_window = old_selected_window;
16740 return n;
16744 /* Display mode or top line of window W. FACE_ID specifies which line
16745 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16746 FORMAT is the mode line format to display. Value is the pixel
16747 height of the mode line displayed. */
16749 static int
16750 display_mode_line (w, face_id, format)
16751 struct window *w;
16752 enum face_id face_id;
16753 Lisp_Object format;
16755 struct it it;
16756 struct face *face;
16757 int count = SPECPDL_INDEX ();
16759 init_iterator (&it, w, -1, -1, NULL, face_id);
16760 /* Don't extend on a previously drawn mode-line.
16761 This may happen if called from pos_visible_p. */
16762 it.glyph_row->enabled_p = 0;
16763 prepare_desired_row (it.glyph_row);
16765 it.glyph_row->mode_line_p = 1;
16767 if (! mode_line_inverse_video)
16768 /* Force the mode-line to be displayed in the default face. */
16769 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16771 record_unwind_protect (unwind_format_mode_line,
16772 format_mode_line_unwind_data (NULL, 0));
16774 mode_line_target = MODE_LINE_DISPLAY;
16776 /* Temporarily make frame's keyboard the current kboard so that
16777 kboard-local variables in the mode_line_format will get the right
16778 values. */
16779 push_frame_kboard (it.f);
16780 record_unwind_save_match_data ();
16781 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16782 pop_frame_kboard ();
16784 unbind_to (count, Qnil);
16786 /* Fill up with spaces. */
16787 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16789 compute_line_metrics (&it);
16790 it.glyph_row->full_width_p = 1;
16791 it.glyph_row->continued_p = 0;
16792 it.glyph_row->truncated_on_left_p = 0;
16793 it.glyph_row->truncated_on_right_p = 0;
16795 /* Make a 3D mode-line have a shadow at its right end. */
16796 face = FACE_FROM_ID (it.f, face_id);
16797 extend_face_to_end_of_line (&it);
16798 if (face->box != FACE_NO_BOX)
16800 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16801 + it.glyph_row->used[TEXT_AREA] - 1);
16802 last->right_box_line_p = 1;
16805 return it.glyph_row->height;
16808 /* Move element ELT in LIST to the front of LIST.
16809 Return the updated list. */
16811 static Lisp_Object
16812 move_elt_to_front (elt, list)
16813 Lisp_Object elt, list;
16815 register Lisp_Object tail, prev;
16816 register Lisp_Object tem;
16818 tail = list;
16819 prev = Qnil;
16820 while (CONSP (tail))
16822 tem = XCAR (tail);
16824 if (EQ (elt, tem))
16826 /* Splice out the link TAIL. */
16827 if (NILP (prev))
16828 list = XCDR (tail);
16829 else
16830 Fsetcdr (prev, XCDR (tail));
16832 /* Now make it the first. */
16833 Fsetcdr (tail, list);
16834 return tail;
16836 else
16837 prev = tail;
16838 tail = XCDR (tail);
16839 QUIT;
16842 /* Not found--return unchanged LIST. */
16843 return list;
16846 /* Contribute ELT to the mode line for window IT->w. How it
16847 translates into text depends on its data type.
16849 IT describes the display environment in which we display, as usual.
16851 DEPTH is the depth in recursion. It is used to prevent
16852 infinite recursion here.
16854 FIELD_WIDTH is the number of characters the display of ELT should
16855 occupy in the mode line, and PRECISION is the maximum number of
16856 characters to display from ELT's representation. See
16857 display_string for details.
16859 Returns the hpos of the end of the text generated by ELT.
16861 PROPS is a property list to add to any string we encounter.
16863 If RISKY is nonzero, remove (disregard) any properties in any string
16864 we encounter, and ignore :eval and :propertize.
16866 The global variable `mode_line_target' determines whether the
16867 output is passed to `store_mode_line_noprop',
16868 `store_mode_line_string', or `display_string'. */
16870 static int
16871 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16872 struct it *it;
16873 int depth;
16874 int field_width, precision;
16875 Lisp_Object elt, props;
16876 int risky;
16878 int n = 0, field, prec;
16879 int literal = 0;
16881 tail_recurse:
16882 if (depth > 100)
16883 elt = build_string ("*too-deep*");
16885 depth++;
16887 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16889 case Lisp_String:
16891 /* A string: output it and check for %-constructs within it. */
16892 unsigned char c;
16893 int offset = 0;
16895 if (SCHARS (elt) > 0
16896 && (!NILP (props) || risky))
16898 Lisp_Object oprops, aelt;
16899 oprops = Ftext_properties_at (make_number (0), elt);
16901 /* If the starting string's properties are not what
16902 we want, translate the string. Also, if the string
16903 is risky, do that anyway. */
16905 if (NILP (Fequal (props, oprops)) || risky)
16907 /* If the starting string has properties,
16908 merge the specified ones onto the existing ones. */
16909 if (! NILP (oprops) && !risky)
16911 Lisp_Object tem;
16913 oprops = Fcopy_sequence (oprops);
16914 tem = props;
16915 while (CONSP (tem))
16917 oprops = Fplist_put (oprops, XCAR (tem),
16918 XCAR (XCDR (tem)));
16919 tem = XCDR (XCDR (tem));
16921 props = oprops;
16924 aelt = Fassoc (elt, mode_line_proptrans_alist);
16925 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16927 /* AELT is what we want. Move it to the front
16928 without consing. */
16929 elt = XCAR (aelt);
16930 mode_line_proptrans_alist
16931 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16933 else
16935 Lisp_Object tem;
16937 /* If AELT has the wrong props, it is useless.
16938 so get rid of it. */
16939 if (! NILP (aelt))
16940 mode_line_proptrans_alist
16941 = Fdelq (aelt, mode_line_proptrans_alist);
16943 elt = Fcopy_sequence (elt);
16944 Fset_text_properties (make_number (0), Flength (elt),
16945 props, elt);
16946 /* Add this item to mode_line_proptrans_alist. */
16947 mode_line_proptrans_alist
16948 = Fcons (Fcons (elt, props),
16949 mode_line_proptrans_alist);
16950 /* Truncate mode_line_proptrans_alist
16951 to at most 50 elements. */
16952 tem = Fnthcdr (make_number (50),
16953 mode_line_proptrans_alist);
16954 if (! NILP (tem))
16955 XSETCDR (tem, Qnil);
16960 offset = 0;
16962 if (literal)
16964 prec = precision - n;
16965 switch (mode_line_target)
16967 case MODE_LINE_NOPROP:
16968 case MODE_LINE_TITLE:
16969 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16970 break;
16971 case MODE_LINE_STRING:
16972 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16973 break;
16974 case MODE_LINE_DISPLAY:
16975 n += display_string (NULL, elt, Qnil, 0, 0, it,
16976 0, prec, 0, STRING_MULTIBYTE (elt));
16977 break;
16980 break;
16983 /* Handle the non-literal case. */
16985 while ((precision <= 0 || n < precision)
16986 && SREF (elt, offset) != 0
16987 && (mode_line_target != MODE_LINE_DISPLAY
16988 || it->current_x < it->last_visible_x))
16990 int last_offset = offset;
16992 /* Advance to end of string or next format specifier. */
16993 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16996 if (offset - 1 != last_offset)
16998 int nchars, nbytes;
17000 /* Output to end of string or up to '%'. Field width
17001 is length of string. Don't output more than
17002 PRECISION allows us. */
17003 offset--;
17005 prec = c_string_width (SDATA (elt) + last_offset,
17006 offset - last_offset, precision - n,
17007 &nchars, &nbytes);
17009 switch (mode_line_target)
17011 case MODE_LINE_NOPROP:
17012 case MODE_LINE_TITLE:
17013 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17014 break;
17015 case MODE_LINE_STRING:
17017 int bytepos = last_offset;
17018 int charpos = string_byte_to_char (elt, bytepos);
17019 int endpos = (precision <= 0
17020 ? string_byte_to_char (elt, offset)
17021 : charpos + nchars);
17023 n += store_mode_line_string (NULL,
17024 Fsubstring (elt, make_number (charpos),
17025 make_number (endpos)),
17026 0, 0, 0, Qnil);
17028 break;
17029 case MODE_LINE_DISPLAY:
17031 int bytepos = last_offset;
17032 int charpos = string_byte_to_char (elt, bytepos);
17034 if (precision <= 0)
17035 nchars = string_byte_to_char (elt, offset) - charpos;
17036 n += display_string (NULL, elt, Qnil, 0, charpos,
17037 it, 0, nchars, 0,
17038 STRING_MULTIBYTE (elt));
17040 break;
17043 else /* c == '%' */
17045 int percent_position = offset;
17047 /* Get the specified minimum width. Zero means
17048 don't pad. */
17049 field = 0;
17050 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17051 field = field * 10 + c - '0';
17053 /* Don't pad beyond the total padding allowed. */
17054 if (field_width - n > 0 && field > field_width - n)
17055 field = field_width - n;
17057 /* Note that either PRECISION <= 0 or N < PRECISION. */
17058 prec = precision - n;
17060 if (c == 'M')
17061 n += display_mode_element (it, depth, field, prec,
17062 Vglobal_mode_string, props,
17063 risky);
17064 else if (c != 0)
17066 int multibyte;
17067 int bytepos, charpos;
17068 unsigned char *spec;
17070 bytepos = percent_position;
17071 charpos = (STRING_MULTIBYTE (elt)
17072 ? string_byte_to_char (elt, bytepos)
17073 : bytepos);
17075 spec
17076 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17078 switch (mode_line_target)
17080 case MODE_LINE_NOPROP:
17081 case MODE_LINE_TITLE:
17082 n += store_mode_line_noprop (spec, field, prec);
17083 break;
17084 case MODE_LINE_STRING:
17086 int len = strlen (spec);
17087 Lisp_Object tem = make_string (spec, len);
17088 props = Ftext_properties_at (make_number (charpos), elt);
17089 /* Should only keep face property in props */
17090 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17092 break;
17093 case MODE_LINE_DISPLAY:
17095 int nglyphs_before, nwritten;
17097 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17098 nwritten = display_string (spec, Qnil, elt,
17099 charpos, 0, it,
17100 field, prec, 0,
17101 multibyte);
17103 /* Assign to the glyphs written above the
17104 string where the `%x' came from, position
17105 of the `%'. */
17106 if (nwritten > 0)
17108 struct glyph *glyph
17109 = (it->glyph_row->glyphs[TEXT_AREA]
17110 + nglyphs_before);
17111 int i;
17113 for (i = 0; i < nwritten; ++i)
17115 glyph[i].object = elt;
17116 glyph[i].charpos = charpos;
17119 n += nwritten;
17122 break;
17125 else /* c == 0 */
17126 break;
17130 break;
17132 case Lisp_Symbol:
17133 /* A symbol: process the value of the symbol recursively
17134 as if it appeared here directly. Avoid error if symbol void.
17135 Special case: if value of symbol is a string, output the string
17136 literally. */
17138 register Lisp_Object tem;
17140 /* If the variable is not marked as risky to set
17141 then its contents are risky to use. */
17142 if (NILP (Fget (elt, Qrisky_local_variable)))
17143 risky = 1;
17145 tem = Fboundp (elt);
17146 if (!NILP (tem))
17148 tem = Fsymbol_value (elt);
17149 /* If value is a string, output that string literally:
17150 don't check for % within it. */
17151 if (STRINGP (tem))
17152 literal = 1;
17154 if (!EQ (tem, elt))
17156 /* Give up right away for nil or t. */
17157 elt = tem;
17158 goto tail_recurse;
17162 break;
17164 case Lisp_Cons:
17166 register Lisp_Object car, tem;
17168 /* A cons cell: five distinct cases.
17169 If first element is :eval or :propertize, do something special.
17170 If first element is a string or a cons, process all the elements
17171 and effectively concatenate them.
17172 If first element is a negative number, truncate displaying cdr to
17173 at most that many characters. If positive, pad (with spaces)
17174 to at least that many characters.
17175 If first element is a symbol, process the cadr or caddr recursively
17176 according to whether the symbol's value is non-nil or nil. */
17177 car = XCAR (elt);
17178 if (EQ (car, QCeval))
17180 /* An element of the form (:eval FORM) means evaluate FORM
17181 and use the result as mode line elements. */
17183 if (risky)
17184 break;
17186 if (CONSP (XCDR (elt)))
17188 Lisp_Object spec;
17189 spec = safe_eval (XCAR (XCDR (elt)));
17190 n += display_mode_element (it, depth, field_width - n,
17191 precision - n, spec, props,
17192 risky);
17195 else if (EQ (car, QCpropertize))
17197 /* An element of the form (:propertize ELT PROPS...)
17198 means display ELT but applying properties PROPS. */
17200 if (risky)
17201 break;
17203 if (CONSP (XCDR (elt)))
17204 n += display_mode_element (it, depth, field_width - n,
17205 precision - n, XCAR (XCDR (elt)),
17206 XCDR (XCDR (elt)), risky);
17208 else if (SYMBOLP (car))
17210 tem = Fboundp (car);
17211 elt = XCDR (elt);
17212 if (!CONSP (elt))
17213 goto invalid;
17214 /* elt is now the cdr, and we know it is a cons cell.
17215 Use its car if CAR has a non-nil value. */
17216 if (!NILP (tem))
17218 tem = Fsymbol_value (car);
17219 if (!NILP (tem))
17221 elt = XCAR (elt);
17222 goto tail_recurse;
17225 /* Symbol's value is nil (or symbol is unbound)
17226 Get the cddr of the original list
17227 and if possible find the caddr and use that. */
17228 elt = XCDR (elt);
17229 if (NILP (elt))
17230 break;
17231 else if (!CONSP (elt))
17232 goto invalid;
17233 elt = XCAR (elt);
17234 goto tail_recurse;
17236 else if (INTEGERP (car))
17238 register int lim = XINT (car);
17239 elt = XCDR (elt);
17240 if (lim < 0)
17242 /* Negative int means reduce maximum width. */
17243 if (precision <= 0)
17244 precision = -lim;
17245 else
17246 precision = min (precision, -lim);
17248 else if (lim > 0)
17250 /* Padding specified. Don't let it be more than
17251 current maximum. */
17252 if (precision > 0)
17253 lim = min (precision, lim);
17255 /* If that's more padding than already wanted, queue it.
17256 But don't reduce padding already specified even if
17257 that is beyond the current truncation point. */
17258 field_width = max (lim, field_width);
17260 goto tail_recurse;
17262 else if (STRINGP (car) || CONSP (car))
17264 register int limit = 50;
17265 /* Limit is to protect against circular lists. */
17266 while (CONSP (elt)
17267 && --limit > 0
17268 && (precision <= 0 || n < precision))
17270 n += display_mode_element (it, depth,
17271 /* Do padding only after the last
17272 element in the list. */
17273 (! CONSP (XCDR (elt))
17274 ? field_width - n
17275 : 0),
17276 precision - n, XCAR (elt),
17277 props, risky);
17278 elt = XCDR (elt);
17282 break;
17284 default:
17285 invalid:
17286 elt = build_string ("*invalid*");
17287 goto tail_recurse;
17290 /* Pad to FIELD_WIDTH. */
17291 if (field_width > 0 && n < field_width)
17293 switch (mode_line_target)
17295 case MODE_LINE_NOPROP:
17296 case MODE_LINE_TITLE:
17297 n += store_mode_line_noprop ("", field_width - n, 0);
17298 break;
17299 case MODE_LINE_STRING:
17300 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17301 break;
17302 case MODE_LINE_DISPLAY:
17303 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17304 0, 0, 0);
17305 break;
17309 return n;
17312 /* Store a mode-line string element in mode_line_string_list.
17314 If STRING is non-null, display that C string. Otherwise, the Lisp
17315 string LISP_STRING is displayed.
17317 FIELD_WIDTH is the minimum number of output glyphs to produce.
17318 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17319 with spaces. FIELD_WIDTH <= 0 means don't pad.
17321 PRECISION is the maximum number of characters to output from
17322 STRING. PRECISION <= 0 means don't truncate the string.
17324 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17325 properties to the string.
17327 PROPS are the properties to add to the string.
17328 The mode_line_string_face face property is always added to the string.
17331 static int
17332 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17333 char *string;
17334 Lisp_Object lisp_string;
17335 int copy_string;
17336 int field_width;
17337 int precision;
17338 Lisp_Object props;
17340 int len;
17341 int n = 0;
17343 if (string != NULL)
17345 len = strlen (string);
17346 if (precision > 0 && len > precision)
17347 len = precision;
17348 lisp_string = make_string (string, len);
17349 if (NILP (props))
17350 props = mode_line_string_face_prop;
17351 else if (!NILP (mode_line_string_face))
17353 Lisp_Object face = Fplist_get (props, Qface);
17354 props = Fcopy_sequence (props);
17355 if (NILP (face))
17356 face = mode_line_string_face;
17357 else
17358 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17359 props = Fplist_put (props, Qface, face);
17361 Fadd_text_properties (make_number (0), make_number (len),
17362 props, lisp_string);
17364 else
17366 len = XFASTINT (Flength (lisp_string));
17367 if (precision > 0 && len > precision)
17369 len = precision;
17370 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17371 precision = -1;
17373 if (!NILP (mode_line_string_face))
17375 Lisp_Object face;
17376 if (NILP (props))
17377 props = Ftext_properties_at (make_number (0), lisp_string);
17378 face = Fplist_get (props, Qface);
17379 if (NILP (face))
17380 face = mode_line_string_face;
17381 else
17382 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17383 props = Fcons (Qface, Fcons (face, Qnil));
17384 if (copy_string)
17385 lisp_string = Fcopy_sequence (lisp_string);
17387 if (!NILP (props))
17388 Fadd_text_properties (make_number (0), make_number (len),
17389 props, lisp_string);
17392 if (len > 0)
17394 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17395 n += len;
17398 if (field_width > len)
17400 field_width -= len;
17401 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17402 if (!NILP (props))
17403 Fadd_text_properties (make_number (0), make_number (field_width),
17404 props, lisp_string);
17405 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17406 n += field_width;
17409 return n;
17413 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17414 1, 4, 0,
17415 doc: /* Format a string out of a mode line format specification.
17416 First arg FORMAT specifies the mode line format (see `mode-line-format'
17417 for details) to use.
17419 Optional second arg FACE specifies the face property to put
17420 on all characters for which no face is specified.
17421 The value t means whatever face the window's mode line currently uses
17422 \(either `mode-line' or `mode-line-inactive', depending).
17423 A value of nil means the default is no face property.
17424 If FACE is an integer, the value string has no text properties.
17426 Optional third and fourth args WINDOW and BUFFER specify the window
17427 and buffer to use as the context for the formatting (defaults
17428 are the selected window and the window's buffer). */)
17429 (format, face, window, buffer)
17430 Lisp_Object format, face, window, buffer;
17432 struct it it;
17433 int len;
17434 struct window *w;
17435 struct buffer *old_buffer = NULL;
17436 int face_id = -1;
17437 int no_props = INTEGERP (face);
17438 int count = SPECPDL_INDEX ();
17439 Lisp_Object str;
17440 int string_start = 0;
17442 if (NILP (window))
17443 window = selected_window;
17444 CHECK_WINDOW (window);
17445 w = XWINDOW (window);
17447 if (NILP (buffer))
17448 buffer = w->buffer;
17449 CHECK_BUFFER (buffer);
17451 /* Make formatting the modeline a non-op when noninteractive, otherwise
17452 there will be problems later caused by a partially initialized frame. */
17453 if (NILP (format) || noninteractive)
17454 return build_string ("");
17456 if (no_props)
17457 face = Qnil;
17459 if (!NILP (face))
17461 if (EQ (face, Qt))
17462 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17463 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17466 if (face_id < 0)
17467 face_id = DEFAULT_FACE_ID;
17469 if (XBUFFER (buffer) != current_buffer)
17470 old_buffer = current_buffer;
17472 /* Save things including mode_line_proptrans_alist,
17473 and set that to nil so that we don't alter the outer value. */
17474 record_unwind_protect (unwind_format_mode_line,
17475 format_mode_line_unwind_data (old_buffer, 1));
17476 mode_line_proptrans_alist = Qnil;
17478 if (old_buffer)
17479 set_buffer_internal_1 (XBUFFER (buffer));
17481 init_iterator (&it, w, -1, -1, NULL, face_id);
17483 if (no_props)
17485 mode_line_target = MODE_LINE_NOPROP;
17486 mode_line_string_face_prop = Qnil;
17487 mode_line_string_list = Qnil;
17488 string_start = MODE_LINE_NOPROP_LEN (0);
17490 else
17492 mode_line_target = MODE_LINE_STRING;
17493 mode_line_string_list = Qnil;
17494 mode_line_string_face = face;
17495 mode_line_string_face_prop
17496 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17499 push_frame_kboard (it.f);
17500 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17501 pop_frame_kboard ();
17503 if (no_props)
17505 len = MODE_LINE_NOPROP_LEN (string_start);
17506 str = make_string (mode_line_noprop_buf + string_start, len);
17508 else
17510 mode_line_string_list = Fnreverse (mode_line_string_list);
17511 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17512 make_string ("", 0));
17515 unbind_to (count, Qnil);
17516 return str;
17519 /* Write a null-terminated, right justified decimal representation of
17520 the positive integer D to BUF using a minimal field width WIDTH. */
17522 static void
17523 pint2str (buf, width, d)
17524 register char *buf;
17525 register int width;
17526 register int d;
17528 register char *p = buf;
17530 if (d <= 0)
17531 *p++ = '0';
17532 else
17534 while (d > 0)
17536 *p++ = d % 10 + '0';
17537 d /= 10;
17541 for (width -= (int) (p - buf); width > 0; --width)
17542 *p++ = ' ';
17543 *p-- = '\0';
17544 while (p > buf)
17546 d = *buf;
17547 *buf++ = *p;
17548 *p-- = d;
17552 /* Write a null-terminated, right justified decimal and "human
17553 readable" representation of the nonnegative integer D to BUF using
17554 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17556 static const char power_letter[] =
17558 0, /* not used */
17559 'k', /* kilo */
17560 'M', /* mega */
17561 'G', /* giga */
17562 'T', /* tera */
17563 'P', /* peta */
17564 'E', /* exa */
17565 'Z', /* zetta */
17566 'Y' /* yotta */
17569 static void
17570 pint2hrstr (buf, width, d)
17571 char *buf;
17572 int width;
17573 int d;
17575 /* We aim to represent the nonnegative integer D as
17576 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17577 int quotient = d;
17578 int remainder = 0;
17579 /* -1 means: do not use TENTHS. */
17580 int tenths = -1;
17581 int exponent = 0;
17583 /* Length of QUOTIENT.TENTHS as a string. */
17584 int length;
17586 char * psuffix;
17587 char * p;
17589 if (1000 <= quotient)
17591 /* Scale to the appropriate EXPONENT. */
17594 remainder = quotient % 1000;
17595 quotient /= 1000;
17596 exponent++;
17598 while (1000 <= quotient);
17600 /* Round to nearest and decide whether to use TENTHS or not. */
17601 if (quotient <= 9)
17603 tenths = remainder / 100;
17604 if (50 <= remainder % 100)
17606 if (tenths < 9)
17607 tenths++;
17608 else
17610 quotient++;
17611 if (quotient == 10)
17612 tenths = -1;
17613 else
17614 tenths = 0;
17618 else
17619 if (500 <= remainder)
17621 if (quotient < 999)
17622 quotient++;
17623 else
17625 quotient = 1;
17626 exponent++;
17627 tenths = 0;
17632 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17633 if (tenths == -1 && quotient <= 99)
17634 if (quotient <= 9)
17635 length = 1;
17636 else
17637 length = 2;
17638 else
17639 length = 3;
17640 p = psuffix = buf + max (width, length);
17642 /* Print EXPONENT. */
17643 if (exponent)
17644 *psuffix++ = power_letter[exponent];
17645 *psuffix = '\0';
17647 /* Print TENTHS. */
17648 if (tenths >= 0)
17650 *--p = '0' + tenths;
17651 *--p = '.';
17654 /* Print QUOTIENT. */
17657 int digit = quotient % 10;
17658 *--p = '0' + digit;
17660 while ((quotient /= 10) != 0);
17662 /* Print leading spaces. */
17663 while (buf < p)
17664 *--p = ' ';
17667 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17668 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17669 type of CODING_SYSTEM. Return updated pointer into BUF. */
17671 static unsigned char invalid_eol_type[] = "(*invalid*)";
17673 static char *
17674 decode_mode_spec_coding (coding_system, buf, eol_flag)
17675 Lisp_Object coding_system;
17676 register char *buf;
17677 int eol_flag;
17679 Lisp_Object val;
17680 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17681 const unsigned char *eol_str;
17682 int eol_str_len;
17683 /* The EOL conversion we are using. */
17684 Lisp_Object eoltype;
17686 val = Fget (coding_system, Qcoding_system);
17687 eoltype = Qnil;
17689 if (!VECTORP (val)) /* Not yet decided. */
17691 if (multibyte)
17692 *buf++ = '-';
17693 if (eol_flag)
17694 eoltype = eol_mnemonic_undecided;
17695 /* Don't mention EOL conversion if it isn't decided. */
17697 else
17699 Lisp_Object eolvalue;
17701 eolvalue = Fget (coding_system, Qeol_type);
17703 if (multibyte)
17704 *buf++ = XFASTINT (AREF (val, 1));
17706 if (eol_flag)
17708 /* The EOL conversion that is normal on this system. */
17710 if (NILP (eolvalue)) /* Not yet decided. */
17711 eoltype = eol_mnemonic_undecided;
17712 else if (VECTORP (eolvalue)) /* Not yet decided. */
17713 eoltype = eol_mnemonic_undecided;
17714 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17715 eoltype = (XFASTINT (eolvalue) == 0
17716 ? eol_mnemonic_unix
17717 : (XFASTINT (eolvalue) == 1
17718 ? eol_mnemonic_dos : eol_mnemonic_mac));
17722 if (eol_flag)
17724 /* Mention the EOL conversion if it is not the usual one. */
17725 if (STRINGP (eoltype))
17727 eol_str = SDATA (eoltype);
17728 eol_str_len = SBYTES (eoltype);
17730 else if (INTEGERP (eoltype)
17731 && CHAR_VALID_P (XINT (eoltype), 0))
17733 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17734 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17735 eol_str = tmp;
17737 else
17739 eol_str = invalid_eol_type;
17740 eol_str_len = sizeof (invalid_eol_type) - 1;
17742 bcopy (eol_str, buf, eol_str_len);
17743 buf += eol_str_len;
17746 return buf;
17749 /* Return a string for the output of a mode line %-spec for window W,
17750 generated by character C. PRECISION >= 0 means don't return a
17751 string longer than that value. FIELD_WIDTH > 0 means pad the
17752 string returned with spaces to that value. Return 1 in *MULTIBYTE
17753 if the result is multibyte text.
17755 Note we operate on the current buffer for most purposes,
17756 the exception being w->base_line_pos. */
17758 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17760 static char *
17761 decode_mode_spec (w, c, field_width, precision, multibyte)
17762 struct window *w;
17763 register int c;
17764 int field_width, precision;
17765 int *multibyte;
17767 Lisp_Object obj;
17768 struct frame *f = XFRAME (WINDOW_FRAME (w));
17769 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17770 struct buffer *b = current_buffer;
17772 obj = Qnil;
17773 *multibyte = 0;
17775 switch (c)
17777 case '*':
17778 if (!NILP (b->read_only))
17779 return "%";
17780 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17781 return "*";
17782 return "-";
17784 case '+':
17785 /* This differs from %* only for a modified read-only buffer. */
17786 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17787 return "*";
17788 if (!NILP (b->read_only))
17789 return "%";
17790 return "-";
17792 case '&':
17793 /* This differs from %* in ignoring read-only-ness. */
17794 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17795 return "*";
17796 return "-";
17798 case '%':
17799 return "%";
17801 case '[':
17803 int i;
17804 char *p;
17806 if (command_loop_level > 5)
17807 return "[[[... ";
17808 p = decode_mode_spec_buf;
17809 for (i = 0; i < command_loop_level; i++)
17810 *p++ = '[';
17811 *p = 0;
17812 return decode_mode_spec_buf;
17815 case ']':
17817 int i;
17818 char *p;
17820 if (command_loop_level > 5)
17821 return " ...]]]";
17822 p = decode_mode_spec_buf;
17823 for (i = 0; i < command_loop_level; i++)
17824 *p++ = ']';
17825 *p = 0;
17826 return decode_mode_spec_buf;
17829 case '-':
17831 register int i;
17833 /* Let lots_of_dashes be a string of infinite length. */
17834 if (mode_line_target == MODE_LINE_NOPROP ||
17835 mode_line_target == MODE_LINE_STRING)
17836 return "--";
17837 if (field_width <= 0
17838 || field_width > sizeof (lots_of_dashes))
17840 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17841 decode_mode_spec_buf[i] = '-';
17842 decode_mode_spec_buf[i] = '\0';
17843 return decode_mode_spec_buf;
17845 else
17846 return lots_of_dashes;
17849 case 'b':
17850 obj = b->name;
17851 break;
17853 case 'c':
17854 /* %c and %l are ignored in `frame-title-format'.
17855 (In redisplay_internal, the frame title is drawn _before_ the
17856 windows are updated, so the stuff which depends on actual
17857 window contents (such as %l) may fail to render properly, or
17858 even crash emacs.) */
17859 if (mode_line_target == MODE_LINE_TITLE)
17860 return "";
17861 else
17863 int col = (int) current_column (); /* iftc */
17864 w->column_number_displayed = make_number (col);
17865 pint2str (decode_mode_spec_buf, field_width, col);
17866 return decode_mode_spec_buf;
17869 case 'e':
17870 #ifndef SYSTEM_MALLOC
17872 if (NILP (Vmemory_full))
17873 return "";
17874 else
17875 return "!MEM FULL! ";
17877 #else
17878 return "";
17879 #endif
17881 case 'F':
17882 /* %F displays the frame name. */
17883 if (!NILP (f->title))
17884 return (char *) SDATA (f->title);
17885 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17886 return (char *) SDATA (f->name);
17887 return "Emacs";
17889 case 'f':
17890 obj = b->filename;
17891 break;
17893 case 'i':
17895 int size = ZV - BEGV;
17896 pint2str (decode_mode_spec_buf, field_width, size);
17897 return decode_mode_spec_buf;
17900 case 'I':
17902 int size = ZV - BEGV;
17903 pint2hrstr (decode_mode_spec_buf, field_width, size);
17904 return decode_mode_spec_buf;
17907 case 'l':
17909 int startpos, startpos_byte, line, linepos, linepos_byte;
17910 int topline, nlines, junk, height;
17912 /* %c and %l are ignored in `frame-title-format'. */
17913 if (mode_line_target == MODE_LINE_TITLE)
17914 return "";
17916 startpos = XMARKER (w->start)->charpos;
17917 startpos_byte = marker_byte_position (w->start);
17918 height = WINDOW_TOTAL_LINES (w);
17920 /* If we decided that this buffer isn't suitable for line numbers,
17921 don't forget that too fast. */
17922 if (EQ (w->base_line_pos, w->buffer))
17923 goto no_value;
17924 /* But do forget it, if the window shows a different buffer now. */
17925 else if (BUFFERP (w->base_line_pos))
17926 w->base_line_pos = Qnil;
17928 /* If the buffer is very big, don't waste time. */
17929 if (INTEGERP (Vline_number_display_limit)
17930 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17932 w->base_line_pos = Qnil;
17933 w->base_line_number = Qnil;
17934 goto no_value;
17937 if (!NILP (w->base_line_number)
17938 && !NILP (w->base_line_pos)
17939 && XFASTINT (w->base_line_pos) <= startpos)
17941 line = XFASTINT (w->base_line_number);
17942 linepos = XFASTINT (w->base_line_pos);
17943 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17945 else
17947 line = 1;
17948 linepos = BUF_BEGV (b);
17949 linepos_byte = BUF_BEGV_BYTE (b);
17952 /* Count lines from base line to window start position. */
17953 nlines = display_count_lines (linepos, linepos_byte,
17954 startpos_byte,
17955 startpos, &junk);
17957 topline = nlines + line;
17959 /* Determine a new base line, if the old one is too close
17960 or too far away, or if we did not have one.
17961 "Too close" means it's plausible a scroll-down would
17962 go back past it. */
17963 if (startpos == BUF_BEGV (b))
17965 w->base_line_number = make_number (topline);
17966 w->base_line_pos = make_number (BUF_BEGV (b));
17968 else if (nlines < height + 25 || nlines > height * 3 + 50
17969 || linepos == BUF_BEGV (b))
17971 int limit = BUF_BEGV (b);
17972 int limit_byte = BUF_BEGV_BYTE (b);
17973 int position;
17974 int distance = (height * 2 + 30) * line_number_display_limit_width;
17976 if (startpos - distance > limit)
17978 limit = startpos - distance;
17979 limit_byte = CHAR_TO_BYTE (limit);
17982 nlines = display_count_lines (startpos, startpos_byte,
17983 limit_byte,
17984 - (height * 2 + 30),
17985 &position);
17986 /* If we couldn't find the lines we wanted within
17987 line_number_display_limit_width chars per line,
17988 give up on line numbers for this window. */
17989 if (position == limit_byte && limit == startpos - distance)
17991 w->base_line_pos = w->buffer;
17992 w->base_line_number = Qnil;
17993 goto no_value;
17996 w->base_line_number = make_number (topline - nlines);
17997 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18000 /* Now count lines from the start pos to point. */
18001 nlines = display_count_lines (startpos, startpos_byte,
18002 PT_BYTE, PT, &junk);
18004 /* Record that we did display the line number. */
18005 line_number_displayed = 1;
18007 /* Make the string to show. */
18008 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18009 return decode_mode_spec_buf;
18010 no_value:
18012 char* p = decode_mode_spec_buf;
18013 int pad = field_width - 2;
18014 while (pad-- > 0)
18015 *p++ = ' ';
18016 *p++ = '?';
18017 *p++ = '?';
18018 *p = '\0';
18019 return decode_mode_spec_buf;
18022 break;
18024 case 'm':
18025 obj = b->mode_name;
18026 break;
18028 case 'n':
18029 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18030 return " Narrow";
18031 break;
18033 case 'p':
18035 int pos = marker_position (w->start);
18036 int total = BUF_ZV (b) - BUF_BEGV (b);
18038 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18040 if (pos <= BUF_BEGV (b))
18041 return "All";
18042 else
18043 return "Bottom";
18045 else if (pos <= BUF_BEGV (b))
18046 return "Top";
18047 else
18049 if (total > 1000000)
18050 /* Do it differently for a large value, to avoid overflow. */
18051 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18052 else
18053 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18054 /* We can't normally display a 3-digit number,
18055 so get us a 2-digit number that is close. */
18056 if (total == 100)
18057 total = 99;
18058 sprintf (decode_mode_spec_buf, "%2d%%", total);
18059 return decode_mode_spec_buf;
18063 /* Display percentage of size above the bottom of the screen. */
18064 case 'P':
18066 int toppos = marker_position (w->start);
18067 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18068 int total = BUF_ZV (b) - BUF_BEGV (b);
18070 if (botpos >= BUF_ZV (b))
18072 if (toppos <= BUF_BEGV (b))
18073 return "All";
18074 else
18075 return "Bottom";
18077 else
18079 if (total > 1000000)
18080 /* Do it differently for a large value, to avoid overflow. */
18081 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18082 else
18083 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18084 /* We can't normally display a 3-digit number,
18085 so get us a 2-digit number that is close. */
18086 if (total == 100)
18087 total = 99;
18088 if (toppos <= BUF_BEGV (b))
18089 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18090 else
18091 sprintf (decode_mode_spec_buf, "%2d%%", total);
18092 return decode_mode_spec_buf;
18096 case 's':
18097 /* status of process */
18098 obj = Fget_buffer_process (Fcurrent_buffer ());
18099 if (NILP (obj))
18100 return "no process";
18101 #ifdef subprocesses
18102 obj = Fsymbol_name (Fprocess_status (obj));
18103 #endif
18104 break;
18106 case 't': /* indicate TEXT or BINARY */
18107 #ifdef MODE_LINE_BINARY_TEXT
18108 return MODE_LINE_BINARY_TEXT (b);
18109 #else
18110 return "T";
18111 #endif
18113 case 'z':
18114 /* coding-system (not including end-of-line format) */
18115 case 'Z':
18116 /* coding-system (including end-of-line type) */
18118 int eol_flag = (c == 'Z');
18119 char *p = decode_mode_spec_buf;
18121 if (! FRAME_WINDOW_P (f))
18123 /* No need to mention EOL here--the terminal never needs
18124 to do EOL conversion. */
18125 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
18126 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
18128 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18129 p, eol_flag);
18131 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18132 #ifdef subprocesses
18133 obj = Fget_buffer_process (Fcurrent_buffer ());
18134 if (PROCESSP (obj))
18136 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18137 p, eol_flag);
18138 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18139 p, eol_flag);
18141 #endif /* subprocesses */
18142 #endif /* 0 */
18143 *p = 0;
18144 return decode_mode_spec_buf;
18148 if (STRINGP (obj))
18150 *multibyte = STRING_MULTIBYTE (obj);
18151 return (char *) SDATA (obj);
18153 else
18154 return "";
18158 /* Count up to COUNT lines starting from START / START_BYTE.
18159 But don't go beyond LIMIT_BYTE.
18160 Return the number of lines thus found (always nonnegative).
18162 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18164 static int
18165 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18166 int start, start_byte, limit_byte, count;
18167 int *byte_pos_ptr;
18169 register unsigned char *cursor;
18170 unsigned char *base;
18172 register int ceiling;
18173 register unsigned char *ceiling_addr;
18174 int orig_count = count;
18176 /* If we are not in selective display mode,
18177 check only for newlines. */
18178 int selective_display = (!NILP (current_buffer->selective_display)
18179 && !INTEGERP (current_buffer->selective_display));
18181 if (count > 0)
18183 while (start_byte < limit_byte)
18185 ceiling = BUFFER_CEILING_OF (start_byte);
18186 ceiling = min (limit_byte - 1, ceiling);
18187 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18188 base = (cursor = BYTE_POS_ADDR (start_byte));
18189 while (1)
18191 if (selective_display)
18192 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18194 else
18195 while (*cursor != '\n' && ++cursor != ceiling_addr)
18198 if (cursor != ceiling_addr)
18200 if (--count == 0)
18202 start_byte += cursor - base + 1;
18203 *byte_pos_ptr = start_byte;
18204 return orig_count;
18206 else
18207 if (++cursor == ceiling_addr)
18208 break;
18210 else
18211 break;
18213 start_byte += cursor - base;
18216 else
18218 while (start_byte > limit_byte)
18220 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18221 ceiling = max (limit_byte, ceiling);
18222 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18223 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18224 while (1)
18226 if (selective_display)
18227 while (--cursor != ceiling_addr
18228 && *cursor != '\n' && *cursor != 015)
18230 else
18231 while (--cursor != ceiling_addr && *cursor != '\n')
18234 if (cursor != ceiling_addr)
18236 if (++count == 0)
18238 start_byte += cursor - base + 1;
18239 *byte_pos_ptr = start_byte;
18240 /* When scanning backwards, we should
18241 not count the newline posterior to which we stop. */
18242 return - orig_count - 1;
18245 else
18246 break;
18248 /* Here we add 1 to compensate for the last decrement
18249 of CURSOR, which took it past the valid range. */
18250 start_byte += cursor - base + 1;
18254 *byte_pos_ptr = limit_byte;
18256 if (count < 0)
18257 return - orig_count + count;
18258 return orig_count - count;
18264 /***********************************************************************
18265 Displaying strings
18266 ***********************************************************************/
18268 /* Display a NUL-terminated string, starting with index START.
18270 If STRING is non-null, display that C string. Otherwise, the Lisp
18271 string LISP_STRING is displayed.
18273 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18274 FACE_STRING. Display STRING or LISP_STRING with the face at
18275 FACE_STRING_POS in FACE_STRING:
18277 Display the string in the environment given by IT, but use the
18278 standard display table, temporarily.
18280 FIELD_WIDTH is the minimum number of output glyphs to produce.
18281 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18282 with spaces. If STRING has more characters, more than FIELD_WIDTH
18283 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18285 PRECISION is the maximum number of characters to output from
18286 STRING. PRECISION < 0 means don't truncate the string.
18288 This is roughly equivalent to printf format specifiers:
18290 FIELD_WIDTH PRECISION PRINTF
18291 ----------------------------------------
18292 -1 -1 %s
18293 -1 10 %.10s
18294 10 -1 %10s
18295 20 10 %20.10s
18297 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18298 display them, and < 0 means obey the current buffer's value of
18299 enable_multibyte_characters.
18301 Value is the number of columns displayed. */
18303 static int
18304 display_string (string, lisp_string, face_string, face_string_pos,
18305 start, it, field_width, precision, max_x, multibyte)
18306 unsigned char *string;
18307 Lisp_Object lisp_string;
18308 Lisp_Object face_string;
18309 int face_string_pos;
18310 int start;
18311 struct it *it;
18312 int field_width, precision, max_x;
18313 int multibyte;
18315 int hpos_at_start = it->hpos;
18316 int saved_face_id = it->face_id;
18317 struct glyph_row *row = it->glyph_row;
18319 /* Initialize the iterator IT for iteration over STRING beginning
18320 with index START. */
18321 reseat_to_string (it, string, lisp_string, start,
18322 precision, field_width, multibyte);
18324 /* If displaying STRING, set up the face of the iterator
18325 from LISP_STRING, if that's given. */
18326 if (STRINGP (face_string))
18328 int endptr;
18329 struct face *face;
18331 it->face_id
18332 = face_at_string_position (it->w, face_string, face_string_pos,
18333 0, it->region_beg_charpos,
18334 it->region_end_charpos,
18335 &endptr, it->base_face_id, 0);
18336 face = FACE_FROM_ID (it->f, it->face_id);
18337 it->face_box_p = face->box != FACE_NO_BOX;
18340 /* Set max_x to the maximum allowed X position. Don't let it go
18341 beyond the right edge of the window. */
18342 if (max_x <= 0)
18343 max_x = it->last_visible_x;
18344 else
18345 max_x = min (max_x, it->last_visible_x);
18347 /* Skip over display elements that are not visible. because IT->w is
18348 hscrolled. */
18349 if (it->current_x < it->first_visible_x)
18350 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18351 MOVE_TO_POS | MOVE_TO_X);
18353 row->ascent = it->max_ascent;
18354 row->height = it->max_ascent + it->max_descent;
18355 row->phys_ascent = it->max_phys_ascent;
18356 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18357 row->extra_line_spacing = it->max_extra_line_spacing;
18359 /* This condition is for the case that we are called with current_x
18360 past last_visible_x. */
18361 while (it->current_x < max_x)
18363 int x_before, x, n_glyphs_before, i, nglyphs;
18365 /* Get the next display element. */
18366 if (!get_next_display_element (it))
18367 break;
18369 /* Produce glyphs. */
18370 x_before = it->current_x;
18371 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18372 PRODUCE_GLYPHS (it);
18374 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18375 i = 0;
18376 x = x_before;
18377 while (i < nglyphs)
18379 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18381 if (!it->truncate_lines_p
18382 && x + glyph->pixel_width > max_x)
18384 /* End of continued line or max_x reached. */
18385 if (CHAR_GLYPH_PADDING_P (*glyph))
18387 /* A wide character is unbreakable. */
18388 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18389 it->current_x = x_before;
18391 else
18393 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18394 it->current_x = x;
18396 break;
18398 else if (x + glyph->pixel_width > it->first_visible_x)
18400 /* Glyph is at least partially visible. */
18401 ++it->hpos;
18402 if (x < it->first_visible_x)
18403 it->glyph_row->x = x - it->first_visible_x;
18405 else
18407 /* Glyph is off the left margin of the display area.
18408 Should not happen. */
18409 abort ();
18412 row->ascent = max (row->ascent, it->max_ascent);
18413 row->height = max (row->height, it->max_ascent + it->max_descent);
18414 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18415 row->phys_height = max (row->phys_height,
18416 it->max_phys_ascent + it->max_phys_descent);
18417 row->extra_line_spacing = max (row->extra_line_spacing,
18418 it->max_extra_line_spacing);
18419 x += glyph->pixel_width;
18420 ++i;
18423 /* Stop if max_x reached. */
18424 if (i < nglyphs)
18425 break;
18427 /* Stop at line ends. */
18428 if (ITERATOR_AT_END_OF_LINE_P (it))
18430 it->continuation_lines_width = 0;
18431 break;
18434 set_iterator_to_next (it, 1);
18436 /* Stop if truncating at the right edge. */
18437 if (it->truncate_lines_p
18438 && it->current_x >= it->last_visible_x)
18440 /* Add truncation mark, but don't do it if the line is
18441 truncated at a padding space. */
18442 if (IT_CHARPOS (*it) < it->string_nchars)
18444 if (!FRAME_WINDOW_P (it->f))
18446 int i, n;
18448 if (it->current_x > it->last_visible_x)
18450 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18451 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18452 break;
18453 for (n = row->used[TEXT_AREA]; i < n; ++i)
18455 row->used[TEXT_AREA] = i;
18456 produce_special_glyphs (it, IT_TRUNCATION);
18459 produce_special_glyphs (it, IT_TRUNCATION);
18461 it->glyph_row->truncated_on_right_p = 1;
18463 break;
18467 /* Maybe insert a truncation at the left. */
18468 if (it->first_visible_x
18469 && IT_CHARPOS (*it) > 0)
18471 if (!FRAME_WINDOW_P (it->f))
18472 insert_left_trunc_glyphs (it);
18473 it->glyph_row->truncated_on_left_p = 1;
18476 it->face_id = saved_face_id;
18478 /* Value is number of columns displayed. */
18479 return it->hpos - hpos_at_start;
18484 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18485 appears as an element of LIST or as the car of an element of LIST.
18486 If PROPVAL is a list, compare each element against LIST in that
18487 way, and return 1/2 if any element of PROPVAL is found in LIST.
18488 Otherwise return 0. This function cannot quit.
18489 The return value is 2 if the text is invisible but with an ellipsis
18490 and 1 if it's invisible and without an ellipsis. */
18493 invisible_p (propval, list)
18494 register Lisp_Object propval;
18495 Lisp_Object list;
18497 register Lisp_Object tail, proptail;
18499 for (tail = list; CONSP (tail); tail = XCDR (tail))
18501 register Lisp_Object tem;
18502 tem = XCAR (tail);
18503 if (EQ (propval, tem))
18504 return 1;
18505 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18506 return NILP (XCDR (tem)) ? 1 : 2;
18509 if (CONSP (propval))
18511 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18513 Lisp_Object propelt;
18514 propelt = XCAR (proptail);
18515 for (tail = list; CONSP (tail); tail = XCDR (tail))
18517 register Lisp_Object tem;
18518 tem = XCAR (tail);
18519 if (EQ (propelt, tem))
18520 return 1;
18521 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18522 return NILP (XCDR (tem)) ? 1 : 2;
18527 return 0;
18530 /* Calculate a width or height in pixels from a specification using
18531 the following elements:
18533 SPEC ::=
18534 NUM - a (fractional) multiple of the default font width/height
18535 (NUM) - specifies exactly NUM pixels
18536 UNIT - a fixed number of pixels, see below.
18537 ELEMENT - size of a display element in pixels, see below.
18538 (NUM . SPEC) - equals NUM * SPEC
18539 (+ SPEC SPEC ...) - add pixel values
18540 (- SPEC SPEC ...) - subtract pixel values
18541 (- SPEC) - negate pixel value
18543 NUM ::=
18544 INT or FLOAT - a number constant
18545 SYMBOL - use symbol's (buffer local) variable binding.
18547 UNIT ::=
18548 in - pixels per inch *)
18549 mm - pixels per 1/1000 meter *)
18550 cm - pixels per 1/100 meter *)
18551 width - width of current font in pixels.
18552 height - height of current font in pixels.
18554 *) using the ratio(s) defined in display-pixels-per-inch.
18556 ELEMENT ::=
18558 left-fringe - left fringe width in pixels
18559 right-fringe - right fringe width in pixels
18561 left-margin - left margin width in pixels
18562 right-margin - right margin width in pixels
18564 scroll-bar - scroll-bar area width in pixels
18566 Examples:
18568 Pixels corresponding to 5 inches:
18569 (5 . in)
18571 Total width of non-text areas on left side of window (if scroll-bar is on left):
18572 '(space :width (+ left-fringe left-margin scroll-bar))
18574 Align to first text column (in header line):
18575 '(space :align-to 0)
18577 Align to middle of text area minus half the width of variable `my-image'
18578 containing a loaded image:
18579 '(space :align-to (0.5 . (- text my-image)))
18581 Width of left margin minus width of 1 character in the default font:
18582 '(space :width (- left-margin 1))
18584 Width of left margin minus width of 2 characters in the current font:
18585 '(space :width (- left-margin (2 . width)))
18587 Center 1 character over left-margin (in header line):
18588 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18590 Different ways to express width of left fringe plus left margin minus one pixel:
18591 '(space :width (- (+ left-fringe left-margin) (1)))
18592 '(space :width (+ left-fringe left-margin (- (1))))
18593 '(space :width (+ left-fringe left-margin (-1)))
18597 #define NUMVAL(X) \
18598 ((INTEGERP (X) || FLOATP (X)) \
18599 ? XFLOATINT (X) \
18600 : - 1)
18603 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18604 double *res;
18605 struct it *it;
18606 Lisp_Object prop;
18607 void *font;
18608 int width_p, *align_to;
18610 double pixels;
18612 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18613 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18615 if (NILP (prop))
18616 return OK_PIXELS (0);
18618 if (SYMBOLP (prop))
18620 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18622 char *unit = SDATA (SYMBOL_NAME (prop));
18624 if (unit[0] == 'i' && unit[1] == 'n')
18625 pixels = 1.0;
18626 else if (unit[0] == 'm' && unit[1] == 'm')
18627 pixels = 25.4;
18628 else if (unit[0] == 'c' && unit[1] == 'm')
18629 pixels = 2.54;
18630 else
18631 pixels = 0;
18632 if (pixels > 0)
18634 double ppi;
18635 #ifdef HAVE_WINDOW_SYSTEM
18636 if (FRAME_WINDOW_P (it->f)
18637 && (ppi = (width_p
18638 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18639 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18640 ppi > 0))
18641 return OK_PIXELS (ppi / pixels);
18642 #endif
18644 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18645 || (CONSP (Vdisplay_pixels_per_inch)
18646 && (ppi = (width_p
18647 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18648 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18649 ppi > 0)))
18650 return OK_PIXELS (ppi / pixels);
18652 return 0;
18656 #ifdef HAVE_WINDOW_SYSTEM
18657 if (EQ (prop, Qheight))
18658 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18659 if (EQ (prop, Qwidth))
18660 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18661 #else
18662 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18663 return OK_PIXELS (1);
18664 #endif
18666 if (EQ (prop, Qtext))
18667 return OK_PIXELS (width_p
18668 ? window_box_width (it->w, TEXT_AREA)
18669 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18671 if (align_to && *align_to < 0)
18673 *res = 0;
18674 if (EQ (prop, Qleft))
18675 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18676 if (EQ (prop, Qright))
18677 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18678 if (EQ (prop, Qcenter))
18679 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18680 + window_box_width (it->w, TEXT_AREA) / 2);
18681 if (EQ (prop, Qleft_fringe))
18682 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18683 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18684 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18685 if (EQ (prop, Qright_fringe))
18686 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18687 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18688 : window_box_right_offset (it->w, TEXT_AREA));
18689 if (EQ (prop, Qleft_margin))
18690 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18691 if (EQ (prop, Qright_margin))
18692 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18693 if (EQ (prop, Qscroll_bar))
18694 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18696 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18697 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18698 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18699 : 0)));
18701 else
18703 if (EQ (prop, Qleft_fringe))
18704 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18705 if (EQ (prop, Qright_fringe))
18706 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18707 if (EQ (prop, Qleft_margin))
18708 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18709 if (EQ (prop, Qright_margin))
18710 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18711 if (EQ (prop, Qscroll_bar))
18712 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18715 prop = Fbuffer_local_value (prop, it->w->buffer);
18718 if (INTEGERP (prop) || FLOATP (prop))
18720 int base_unit = (width_p
18721 ? FRAME_COLUMN_WIDTH (it->f)
18722 : FRAME_LINE_HEIGHT (it->f));
18723 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18726 if (CONSP (prop))
18728 Lisp_Object car = XCAR (prop);
18729 Lisp_Object cdr = XCDR (prop);
18731 if (SYMBOLP (car))
18733 #ifdef HAVE_WINDOW_SYSTEM
18734 if (valid_image_p (prop))
18736 int id = lookup_image (it->f, prop);
18737 struct image *img = IMAGE_FROM_ID (it->f, id);
18739 return OK_PIXELS (width_p ? img->width : img->height);
18741 #endif
18742 if (EQ (car, Qplus) || EQ (car, Qminus))
18744 int first = 1;
18745 double px;
18747 pixels = 0;
18748 while (CONSP (cdr))
18750 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18751 font, width_p, align_to))
18752 return 0;
18753 if (first)
18754 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18755 else
18756 pixels += px;
18757 cdr = XCDR (cdr);
18759 if (EQ (car, Qminus))
18760 pixels = -pixels;
18761 return OK_PIXELS (pixels);
18764 car = Fbuffer_local_value (car, it->w->buffer);
18767 if (INTEGERP (car) || FLOATP (car))
18769 double fact;
18770 pixels = XFLOATINT (car);
18771 if (NILP (cdr))
18772 return OK_PIXELS (pixels);
18773 if (calc_pixel_width_or_height (&fact, it, cdr,
18774 font, width_p, align_to))
18775 return OK_PIXELS (pixels * fact);
18776 return 0;
18779 return 0;
18782 return 0;
18786 /***********************************************************************
18787 Glyph Display
18788 ***********************************************************************/
18790 #ifdef HAVE_WINDOW_SYSTEM
18792 #if GLYPH_DEBUG
18794 void
18795 dump_glyph_string (s)
18796 struct glyph_string *s;
18798 fprintf (stderr, "glyph string\n");
18799 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18800 s->x, s->y, s->width, s->height);
18801 fprintf (stderr, " ybase = %d\n", s->ybase);
18802 fprintf (stderr, " hl = %d\n", s->hl);
18803 fprintf (stderr, " left overhang = %d, right = %d\n",
18804 s->left_overhang, s->right_overhang);
18805 fprintf (stderr, " nchars = %d\n", s->nchars);
18806 fprintf (stderr, " extends to end of line = %d\n",
18807 s->extends_to_end_of_line_p);
18808 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18809 fprintf (stderr, " bg width = %d\n", s->background_width);
18812 #endif /* GLYPH_DEBUG */
18814 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18815 of XChar2b structures for S; it can't be allocated in
18816 init_glyph_string because it must be allocated via `alloca'. W
18817 is the window on which S is drawn. ROW and AREA are the glyph row
18818 and area within the row from which S is constructed. START is the
18819 index of the first glyph structure covered by S. HL is a
18820 face-override for drawing S. */
18822 #ifdef HAVE_NTGUI
18823 #define OPTIONAL_HDC(hdc) hdc,
18824 #define DECLARE_HDC(hdc) HDC hdc;
18825 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18826 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18827 #endif
18829 #ifndef OPTIONAL_HDC
18830 #define OPTIONAL_HDC(hdc)
18831 #define DECLARE_HDC(hdc)
18832 #define ALLOCATE_HDC(hdc, f)
18833 #define RELEASE_HDC(hdc, f)
18834 #endif
18836 static void
18837 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18838 struct glyph_string *s;
18839 DECLARE_HDC (hdc)
18840 XChar2b *char2b;
18841 struct window *w;
18842 struct glyph_row *row;
18843 enum glyph_row_area area;
18844 int start;
18845 enum draw_glyphs_face hl;
18847 bzero (s, sizeof *s);
18848 s->w = w;
18849 s->f = XFRAME (w->frame);
18850 #ifdef HAVE_NTGUI
18851 s->hdc = hdc;
18852 #endif
18853 s->display = FRAME_X_DISPLAY (s->f);
18854 s->window = FRAME_X_WINDOW (s->f);
18855 s->char2b = char2b;
18856 s->hl = hl;
18857 s->row = row;
18858 s->area = area;
18859 s->first_glyph = row->glyphs[area] + start;
18860 s->height = row->height;
18861 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18863 /* Display the internal border below the tool-bar window. */
18864 if (WINDOWP (s->f->tool_bar_window)
18865 && s->w == XWINDOW (s->f->tool_bar_window))
18866 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18868 s->ybase = s->y + row->ascent;
18872 /* Append the list of glyph strings with head H and tail T to the list
18873 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18875 static INLINE void
18876 append_glyph_string_lists (head, tail, h, t)
18877 struct glyph_string **head, **tail;
18878 struct glyph_string *h, *t;
18880 if (h)
18882 if (*head)
18883 (*tail)->next = h;
18884 else
18885 *head = h;
18886 h->prev = *tail;
18887 *tail = t;
18892 /* Prepend the list of glyph strings with head H and tail T to the
18893 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18894 result. */
18896 static INLINE void
18897 prepend_glyph_string_lists (head, tail, h, t)
18898 struct glyph_string **head, **tail;
18899 struct glyph_string *h, *t;
18901 if (h)
18903 if (*head)
18904 (*head)->prev = t;
18905 else
18906 *tail = t;
18907 t->next = *head;
18908 *head = h;
18913 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18914 Set *HEAD and *TAIL to the resulting list. */
18916 static INLINE void
18917 append_glyph_string (head, tail, s)
18918 struct glyph_string **head, **tail;
18919 struct glyph_string *s;
18921 s->next = s->prev = NULL;
18922 append_glyph_string_lists (head, tail, s, s);
18926 /* Get face and two-byte form of character glyph GLYPH on frame F.
18927 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18928 a pointer to a realized face that is ready for display. */
18930 static INLINE struct face *
18931 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18932 struct frame *f;
18933 struct glyph *glyph;
18934 XChar2b *char2b;
18935 int *two_byte_p;
18937 struct face *face;
18939 xassert (glyph->type == CHAR_GLYPH);
18940 face = FACE_FROM_ID (f, glyph->face_id);
18942 if (two_byte_p)
18943 *two_byte_p = 0;
18945 if (!glyph->multibyte_p)
18947 /* Unibyte case. We don't have to encode, but we have to make
18948 sure to use a face suitable for unibyte. */
18949 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18951 else if (glyph->u.ch < 128)
18953 /* Case of ASCII in a face known to fit ASCII. */
18954 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18956 else
18958 int c1, c2, charset;
18960 /* Split characters into bytes. If c2 is -1 afterwards, C is
18961 really a one-byte character so that byte1 is zero. */
18962 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18963 if (c2 > 0)
18964 STORE_XCHAR2B (char2b, c1, c2);
18965 else
18966 STORE_XCHAR2B (char2b, 0, c1);
18968 /* Maybe encode the character in *CHAR2B. */
18969 if (charset != CHARSET_ASCII)
18971 struct font_info *font_info
18972 = FONT_INFO_FROM_ID (f, face->font_info_id);
18973 if (font_info)
18974 glyph->font_type
18975 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18979 /* Make sure X resources of the face are allocated. */
18980 xassert (face != NULL);
18981 PREPARE_FACE_FOR_DISPLAY (f, face);
18982 return face;
18986 /* Fill glyph string S with composition components specified by S->cmp.
18988 FACES is an array of faces for all components of this composition.
18989 S->gidx is the index of the first component for S.
18991 OVERLAPS non-zero means S should draw the foreground only, and use
18992 its physical height for clipping. See also draw_glyphs.
18994 Value is the index of a component not in S. */
18996 static int
18997 fill_composite_glyph_string (s, faces, overlaps)
18998 struct glyph_string *s;
18999 struct face **faces;
19000 int overlaps;
19002 int i;
19004 xassert (s);
19006 s->for_overlaps = overlaps;
19008 s->face = faces[s->gidx];
19009 s->font = s->face->font;
19010 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19012 /* For all glyphs of this composition, starting at the offset
19013 S->gidx, until we reach the end of the definition or encounter a
19014 glyph that requires the different face, add it to S. */
19015 ++s->nchars;
19016 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
19017 ++s->nchars;
19019 /* All glyph strings for the same composition has the same width,
19020 i.e. the width set for the first component of the composition. */
19022 s->width = s->first_glyph->pixel_width;
19024 /* If the specified font could not be loaded, use the frame's
19025 default font, but record the fact that we couldn't load it in
19026 the glyph string so that we can draw rectangles for the
19027 characters of the glyph string. */
19028 if (s->font == NULL)
19030 s->font_not_found_p = 1;
19031 s->font = FRAME_FONT (s->f);
19034 /* Adjust base line for subscript/superscript text. */
19035 s->ybase += s->first_glyph->voffset;
19037 xassert (s->face && s->face->gc);
19039 /* This glyph string must always be drawn with 16-bit functions. */
19040 s->two_byte_p = 1;
19042 return s->gidx + s->nchars;
19046 /* Fill glyph string S from a sequence of character glyphs.
19048 FACE_ID is the face id of the string. START is the index of the
19049 first glyph to consider, END is the index of the last + 1.
19050 OVERLAPS non-zero means S should draw the foreground only, and use
19051 its physical height for clipping. See also draw_glyphs.
19053 Value is the index of the first glyph not in S. */
19055 static int
19056 fill_glyph_string (s, face_id, start, end, overlaps)
19057 struct glyph_string *s;
19058 int face_id;
19059 int start, end, overlaps;
19061 struct glyph *glyph, *last;
19062 int voffset;
19063 int glyph_not_available_p;
19065 xassert (s->f == XFRAME (s->w->frame));
19066 xassert (s->nchars == 0);
19067 xassert (start >= 0 && end > start);
19069 s->for_overlaps = overlaps,
19070 glyph = s->row->glyphs[s->area] + start;
19071 last = s->row->glyphs[s->area] + end;
19072 voffset = glyph->voffset;
19074 glyph_not_available_p = glyph->glyph_not_available_p;
19076 while (glyph < last
19077 && glyph->type == CHAR_GLYPH
19078 && glyph->voffset == voffset
19079 /* Same face id implies same font, nowadays. */
19080 && glyph->face_id == face_id
19081 && glyph->glyph_not_available_p == glyph_not_available_p)
19083 int two_byte_p;
19085 s->face = get_glyph_face_and_encoding (s->f, glyph,
19086 s->char2b + s->nchars,
19087 &two_byte_p);
19088 s->two_byte_p = two_byte_p;
19089 ++s->nchars;
19090 xassert (s->nchars <= end - start);
19091 s->width += glyph->pixel_width;
19092 ++glyph;
19095 s->font = s->face->font;
19096 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19098 /* If the specified font could not be loaded, use the frame's font,
19099 but record the fact that we couldn't load it in
19100 S->font_not_found_p so that we can draw rectangles for the
19101 characters of the glyph string. */
19102 if (s->font == NULL || glyph_not_available_p)
19104 s->font_not_found_p = 1;
19105 s->font = FRAME_FONT (s->f);
19108 /* Adjust base line for subscript/superscript text. */
19109 s->ybase += voffset;
19111 xassert (s->face && s->face->gc);
19112 return glyph - s->row->glyphs[s->area];
19116 /* Fill glyph string S from image glyph S->first_glyph. */
19118 static void
19119 fill_image_glyph_string (s)
19120 struct glyph_string *s;
19122 xassert (s->first_glyph->type == IMAGE_GLYPH);
19123 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19124 xassert (s->img);
19125 s->slice = s->first_glyph->slice;
19126 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19127 s->font = s->face->font;
19128 s->width = s->first_glyph->pixel_width;
19130 /* Adjust base line for subscript/superscript text. */
19131 s->ybase += s->first_glyph->voffset;
19135 /* Fill glyph string S from a sequence of stretch glyphs.
19137 ROW is the glyph row in which the glyphs are found, AREA is the
19138 area within the row. START is the index of the first glyph to
19139 consider, END is the index of the last + 1.
19141 Value is the index of the first glyph not in S. */
19143 static int
19144 fill_stretch_glyph_string (s, row, area, start, end)
19145 struct glyph_string *s;
19146 struct glyph_row *row;
19147 enum glyph_row_area area;
19148 int start, end;
19150 struct glyph *glyph, *last;
19151 int voffset, face_id;
19153 xassert (s->first_glyph->type == STRETCH_GLYPH);
19155 glyph = s->row->glyphs[s->area] + start;
19156 last = s->row->glyphs[s->area] + end;
19157 face_id = glyph->face_id;
19158 s->face = FACE_FROM_ID (s->f, face_id);
19159 s->font = s->face->font;
19160 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19161 s->width = glyph->pixel_width;
19162 s->nchars = 1;
19163 voffset = glyph->voffset;
19165 for (++glyph;
19166 (glyph < last
19167 && glyph->type == STRETCH_GLYPH
19168 && glyph->voffset == voffset
19169 && glyph->face_id == face_id);
19170 ++glyph)
19171 s->width += glyph->pixel_width;
19173 /* Adjust base line for subscript/superscript text. */
19174 s->ybase += voffset;
19176 /* The case that face->gc == 0 is handled when drawing the glyph
19177 string by calling PREPARE_FACE_FOR_DISPLAY. */
19178 xassert (s->face);
19179 return glyph - s->row->glyphs[s->area];
19183 /* EXPORT for RIF:
19184 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19185 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19186 assumed to be zero. */
19188 void
19189 x_get_glyph_overhangs (glyph, f, left, right)
19190 struct glyph *glyph;
19191 struct frame *f;
19192 int *left, *right;
19194 *left = *right = 0;
19196 if (glyph->type == CHAR_GLYPH)
19198 XFontStruct *font;
19199 struct face *face;
19200 struct font_info *font_info;
19201 XChar2b char2b;
19202 XCharStruct *pcm;
19204 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19205 font = face->font;
19206 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19207 if (font /* ++KFS: Should this be font_info ? */
19208 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19210 if (pcm->rbearing > pcm->width)
19211 *right = pcm->rbearing - pcm->width;
19212 if (pcm->lbearing < 0)
19213 *left = -pcm->lbearing;
19219 /* Return the index of the first glyph preceding glyph string S that
19220 is overwritten by S because of S's left overhang. Value is -1
19221 if no glyphs are overwritten. */
19223 static int
19224 left_overwritten (s)
19225 struct glyph_string *s;
19227 int k;
19229 if (s->left_overhang)
19231 int x = 0, i;
19232 struct glyph *glyphs = s->row->glyphs[s->area];
19233 int first = s->first_glyph - glyphs;
19235 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19236 x -= glyphs[i].pixel_width;
19238 k = i + 1;
19240 else
19241 k = -1;
19243 return k;
19247 /* Return the index of the first glyph preceding glyph string S that
19248 is overwriting S because of its right overhang. Value is -1 if no
19249 glyph in front of S overwrites S. */
19251 static int
19252 left_overwriting (s)
19253 struct glyph_string *s;
19255 int i, k, x;
19256 struct glyph *glyphs = s->row->glyphs[s->area];
19257 int first = s->first_glyph - glyphs;
19259 k = -1;
19260 x = 0;
19261 for (i = first - 1; i >= 0; --i)
19263 int left, right;
19264 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19265 if (x + right > 0)
19266 k = i;
19267 x -= glyphs[i].pixel_width;
19270 return k;
19274 /* Return the index of the last glyph following glyph string S that is
19275 not overwritten by S because of S's right overhang. Value is -1 if
19276 no such glyph is found. */
19278 static int
19279 right_overwritten (s)
19280 struct glyph_string *s;
19282 int k = -1;
19284 if (s->right_overhang)
19286 int x = 0, i;
19287 struct glyph *glyphs = s->row->glyphs[s->area];
19288 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19289 int end = s->row->used[s->area];
19291 for (i = first; i < end && s->right_overhang > x; ++i)
19292 x += glyphs[i].pixel_width;
19294 k = i;
19297 return k;
19301 /* Return the index of the last glyph following glyph string S that
19302 overwrites S because of its left overhang. Value is negative
19303 if no such glyph is found. */
19305 static int
19306 right_overwriting (s)
19307 struct glyph_string *s;
19309 int i, k, x;
19310 int end = s->row->used[s->area];
19311 struct glyph *glyphs = s->row->glyphs[s->area];
19312 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19314 k = -1;
19315 x = 0;
19316 for (i = first; i < end; ++i)
19318 int left, right;
19319 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19320 if (x - left < 0)
19321 k = i;
19322 x += glyphs[i].pixel_width;
19325 return k;
19329 /* Get face and two-byte form of character C in face FACE_ID on frame
19330 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19331 means we want to display multibyte text. DISPLAY_P non-zero means
19332 make sure that X resources for the face returned are allocated.
19333 Value is a pointer to a realized face that is ready for display if
19334 DISPLAY_P is non-zero. */
19336 static INLINE struct face *
19337 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19338 struct frame *f;
19339 int c, face_id;
19340 XChar2b *char2b;
19341 int multibyte_p, display_p;
19343 struct face *face = FACE_FROM_ID (f, face_id);
19345 if (!multibyte_p)
19347 /* Unibyte case. We don't have to encode, but we have to make
19348 sure to use a face suitable for unibyte. */
19349 STORE_XCHAR2B (char2b, 0, c);
19350 face_id = FACE_FOR_CHAR (f, face, c);
19351 face = FACE_FROM_ID (f, face_id);
19353 else if (c < 128)
19355 /* Case of ASCII in a face known to fit ASCII. */
19356 STORE_XCHAR2B (char2b, 0, c);
19358 else
19360 int c1, c2, charset;
19362 /* Split characters into bytes. If c2 is -1 afterwards, C is
19363 really a one-byte character so that byte1 is zero. */
19364 SPLIT_CHAR (c, charset, c1, c2);
19365 if (c2 > 0)
19366 STORE_XCHAR2B (char2b, c1, c2);
19367 else
19368 STORE_XCHAR2B (char2b, 0, c1);
19370 /* Maybe encode the character in *CHAR2B. */
19371 if (face->font != NULL)
19373 struct font_info *font_info
19374 = FONT_INFO_FROM_ID (f, face->font_info_id);
19375 if (font_info)
19376 rif->encode_char (c, char2b, font_info, 0);
19380 /* Make sure X resources of the face are allocated. */
19381 #ifdef HAVE_X_WINDOWS
19382 if (display_p)
19383 #endif
19385 xassert (face != NULL);
19386 PREPARE_FACE_FOR_DISPLAY (f, face);
19389 return face;
19393 /* Set background width of glyph string S. START is the index of the
19394 first glyph following S. LAST_X is the right-most x-position + 1
19395 in the drawing area. */
19397 static INLINE void
19398 set_glyph_string_background_width (s, start, last_x)
19399 struct glyph_string *s;
19400 int start;
19401 int last_x;
19403 /* If the face of this glyph string has to be drawn to the end of
19404 the drawing area, set S->extends_to_end_of_line_p. */
19406 if (start == s->row->used[s->area]
19407 && s->area == TEXT_AREA
19408 && ((s->row->fill_line_p
19409 && (s->hl == DRAW_NORMAL_TEXT
19410 || s->hl == DRAW_IMAGE_RAISED
19411 || s->hl == DRAW_IMAGE_SUNKEN))
19412 || s->hl == DRAW_MOUSE_FACE))
19413 s->extends_to_end_of_line_p = 1;
19415 /* If S extends its face to the end of the line, set its
19416 background_width to the distance to the right edge of the drawing
19417 area. */
19418 if (s->extends_to_end_of_line_p)
19419 s->background_width = last_x - s->x + 1;
19420 else
19421 s->background_width = s->width;
19425 /* Compute overhangs and x-positions for glyph string S and its
19426 predecessors, or successors. X is the starting x-position for S.
19427 BACKWARD_P non-zero means process predecessors. */
19429 static void
19430 compute_overhangs_and_x (s, x, backward_p)
19431 struct glyph_string *s;
19432 int x;
19433 int backward_p;
19435 if (backward_p)
19437 while (s)
19439 if (rif->compute_glyph_string_overhangs)
19440 rif->compute_glyph_string_overhangs (s);
19441 x -= s->width;
19442 s->x = x;
19443 s = s->prev;
19446 else
19448 while (s)
19450 if (rif->compute_glyph_string_overhangs)
19451 rif->compute_glyph_string_overhangs (s);
19452 s->x = x;
19453 x += s->width;
19454 s = s->next;
19461 /* The following macros are only called from draw_glyphs below.
19462 They reference the following parameters of that function directly:
19463 `w', `row', `area', and `overlap_p'
19464 as well as the following local variables:
19465 `s', `f', and `hdc' (in W32) */
19467 #ifdef HAVE_NTGUI
19468 /* On W32, silently add local `hdc' variable to argument list of
19469 init_glyph_string. */
19470 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19471 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19472 #else
19473 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19474 init_glyph_string (s, char2b, w, row, area, start, hl)
19475 #endif
19477 /* Add a glyph string for a stretch glyph to the list of strings
19478 between HEAD and TAIL. START is the index of the stretch glyph in
19479 row area AREA of glyph row ROW. END is the index of the last glyph
19480 in that glyph row area. X is the current output position assigned
19481 to the new glyph string constructed. HL overrides that face of the
19482 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19483 is the right-most x-position of the drawing area. */
19485 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19486 and below -- keep them on one line. */
19487 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19488 do \
19490 s = (struct glyph_string *) alloca (sizeof *s); \
19491 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19492 START = fill_stretch_glyph_string (s, row, area, START, END); \
19493 append_glyph_string (&HEAD, &TAIL, s); \
19494 s->x = (X); \
19496 while (0)
19499 /* Add a glyph string for an image glyph to the list of strings
19500 between HEAD and TAIL. START is the index of the image glyph in
19501 row area AREA of glyph row ROW. END is the index of the last glyph
19502 in that glyph row area. X is the current output position assigned
19503 to the new glyph string constructed. HL overrides that face of the
19504 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19505 is the right-most x-position of the drawing area. */
19507 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19508 do \
19510 s = (struct glyph_string *) alloca (sizeof *s); \
19511 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19512 fill_image_glyph_string (s); \
19513 append_glyph_string (&HEAD, &TAIL, s); \
19514 ++START; \
19515 s->x = (X); \
19517 while (0)
19520 /* Add a glyph string for a sequence of character glyphs to the list
19521 of strings between HEAD and TAIL. START is the index of the first
19522 glyph in row area AREA of glyph row ROW that is part of the new
19523 glyph string. END is the index of the last glyph in that glyph row
19524 area. X is the current output position assigned to the new glyph
19525 string constructed. HL overrides that face of the glyph; e.g. it
19526 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19527 right-most x-position of the drawing area. */
19529 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19530 do \
19532 int c, face_id; \
19533 XChar2b *char2b; \
19535 c = (row)->glyphs[area][START].u.ch; \
19536 face_id = (row)->glyphs[area][START].face_id; \
19538 s = (struct glyph_string *) alloca (sizeof *s); \
19539 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19540 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19541 append_glyph_string (&HEAD, &TAIL, s); \
19542 s->x = (X); \
19543 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19545 while (0)
19548 /* Add a glyph string for a composite sequence to the list of strings
19549 between HEAD and TAIL. START is the index of the first glyph in
19550 row area AREA of glyph row ROW that is part of the new glyph
19551 string. END is the index of the last glyph in that glyph row area.
19552 X is the current output position assigned to the new glyph string
19553 constructed. HL overrides that face of the glyph; e.g. it is
19554 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19555 x-position of the drawing area. */
19557 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19558 do { \
19559 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19560 int face_id = (row)->glyphs[area][START].face_id; \
19561 struct face *base_face = FACE_FROM_ID (f, face_id); \
19562 struct composition *cmp = composition_table[cmp_id]; \
19563 int glyph_len = cmp->glyph_len; \
19564 XChar2b *char2b; \
19565 struct face **faces; \
19566 struct glyph_string *first_s = NULL; \
19567 int n; \
19569 base_face = base_face->ascii_face; \
19570 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19571 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19572 /* At first, fill in `char2b' and `faces'. */ \
19573 for (n = 0; n < glyph_len; n++) \
19575 int c = COMPOSITION_GLYPH (cmp, n); \
19576 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19577 faces[n] = FACE_FROM_ID (f, this_face_id); \
19578 get_char_face_and_encoding (f, c, this_face_id, \
19579 char2b + n, 1, 1); \
19582 /* Make glyph_strings for each glyph sequence that is drawable by \
19583 the same face, and append them to HEAD/TAIL. */ \
19584 for (n = 0; n < cmp->glyph_len;) \
19586 s = (struct glyph_string *) alloca (sizeof *s); \
19587 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19588 append_glyph_string (&(HEAD), &(TAIL), s); \
19589 s->cmp = cmp; \
19590 s->gidx = n; \
19591 s->x = (X); \
19593 if (n == 0) \
19594 first_s = s; \
19596 n = fill_composite_glyph_string (s, faces, overlaps); \
19599 ++START; \
19600 s = first_s; \
19601 } while (0)
19604 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19605 of AREA of glyph row ROW on window W between indices START and END.
19606 HL overrides the face for drawing glyph strings, e.g. it is
19607 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19608 x-positions of the drawing area.
19610 This is an ugly monster macro construct because we must use alloca
19611 to allocate glyph strings (because draw_glyphs can be called
19612 asynchronously). */
19614 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19615 do \
19617 HEAD = TAIL = NULL; \
19618 while (START < END) \
19620 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19621 switch (first_glyph->type) \
19623 case CHAR_GLYPH: \
19624 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19625 HL, X, LAST_X); \
19626 break; \
19628 case COMPOSITE_GLYPH: \
19629 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19630 HL, X, LAST_X); \
19631 break; \
19633 case STRETCH_GLYPH: \
19634 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19635 HL, X, LAST_X); \
19636 break; \
19638 case IMAGE_GLYPH: \
19639 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19640 HL, X, LAST_X); \
19641 break; \
19643 default: \
19644 abort (); \
19647 set_glyph_string_background_width (s, START, LAST_X); \
19648 (X) += s->width; \
19651 while (0)
19654 /* Draw glyphs between START and END in AREA of ROW on window W,
19655 starting at x-position X. X is relative to AREA in W. HL is a
19656 face-override with the following meaning:
19658 DRAW_NORMAL_TEXT draw normally
19659 DRAW_CURSOR draw in cursor face
19660 DRAW_MOUSE_FACE draw in mouse face.
19661 DRAW_INVERSE_VIDEO draw in mode line face
19662 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19663 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19665 If OVERLAPS is non-zero, draw only the foreground of characters and
19666 clip to the physical height of ROW. Non-zero value also defines
19667 the overlapping part to be drawn:
19669 OVERLAPS_PRED overlap with preceding rows
19670 OVERLAPS_SUCC overlap with succeeding rows
19671 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19672 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19674 Value is the x-position reached, relative to AREA of W. */
19676 static int
19677 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19678 struct window *w;
19679 int x;
19680 struct glyph_row *row;
19681 enum glyph_row_area area;
19682 int start, end;
19683 enum draw_glyphs_face hl;
19684 int overlaps;
19686 struct glyph_string *head, *tail;
19687 struct glyph_string *s;
19688 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19689 int last_x, area_width;
19690 int x_reached;
19691 int i, j;
19692 struct frame *f = XFRAME (WINDOW_FRAME (w));
19693 DECLARE_HDC (hdc);
19695 ALLOCATE_HDC (hdc, f);
19697 /* Let's rather be paranoid than getting a SEGV. */
19698 end = min (end, row->used[area]);
19699 start = max (0, start);
19700 start = min (end, start);
19702 /* Translate X to frame coordinates. Set last_x to the right
19703 end of the drawing area. */
19704 if (row->full_width_p)
19706 /* X is relative to the left edge of W, without scroll bars
19707 or fringes. */
19708 x += WINDOW_LEFT_EDGE_X (w);
19709 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19711 else
19713 int area_left = window_box_left (w, area);
19714 x += area_left;
19715 area_width = window_box_width (w, area);
19716 last_x = area_left + area_width;
19719 /* Build a doubly-linked list of glyph_string structures between
19720 head and tail from what we have to draw. Note that the macro
19721 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19722 the reason we use a separate variable `i'. */
19723 i = start;
19724 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19725 if (tail)
19726 x_reached = tail->x + tail->background_width;
19727 else
19728 x_reached = x;
19730 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19731 the row, redraw some glyphs in front or following the glyph
19732 strings built above. */
19733 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19735 int dummy_x = 0;
19736 struct glyph_string *h, *t;
19738 /* Compute overhangs for all glyph strings. */
19739 if (rif->compute_glyph_string_overhangs)
19740 for (s = head; s; s = s->next)
19741 rif->compute_glyph_string_overhangs (s);
19743 /* Prepend glyph strings for glyphs in front of the first glyph
19744 string that are overwritten because of the first glyph
19745 string's left overhang. The background of all strings
19746 prepended must be drawn because the first glyph string
19747 draws over it. */
19748 i = left_overwritten (head);
19749 if (i >= 0)
19751 j = i;
19752 BUILD_GLYPH_STRINGS (j, start, h, t,
19753 DRAW_NORMAL_TEXT, dummy_x, last_x);
19754 start = i;
19755 compute_overhangs_and_x (t, head->x, 1);
19756 prepend_glyph_string_lists (&head, &tail, h, t);
19757 clip_head = head;
19760 /* Prepend glyph strings for glyphs in front of the first glyph
19761 string that overwrite that glyph string because of their
19762 right overhang. For these strings, only the foreground must
19763 be drawn, because it draws over the glyph string at `head'.
19764 The background must not be drawn because this would overwrite
19765 right overhangs of preceding glyphs for which no glyph
19766 strings exist. */
19767 i = left_overwriting (head);
19768 if (i >= 0)
19770 clip_head = head;
19771 BUILD_GLYPH_STRINGS (i, start, h, t,
19772 DRAW_NORMAL_TEXT, dummy_x, last_x);
19773 for (s = h; s; s = s->next)
19774 s->background_filled_p = 1;
19775 compute_overhangs_and_x (t, head->x, 1);
19776 prepend_glyph_string_lists (&head, &tail, h, t);
19779 /* Append glyphs strings for glyphs following the last glyph
19780 string tail that are overwritten by tail. The background of
19781 these strings has to be drawn because tail's foreground draws
19782 over it. */
19783 i = right_overwritten (tail);
19784 if (i >= 0)
19786 BUILD_GLYPH_STRINGS (end, i, h, t,
19787 DRAW_NORMAL_TEXT, x, last_x);
19788 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19789 append_glyph_string_lists (&head, &tail, h, t);
19790 clip_tail = tail;
19793 /* Append glyph strings for glyphs following the last glyph
19794 string tail that overwrite tail. The foreground of such
19795 glyphs has to be drawn because it writes into the background
19796 of tail. The background must not be drawn because it could
19797 paint over the foreground of following glyphs. */
19798 i = right_overwriting (tail);
19799 if (i >= 0)
19801 clip_tail = tail;
19802 BUILD_GLYPH_STRINGS (end, i, h, t,
19803 DRAW_NORMAL_TEXT, x, last_x);
19804 for (s = h; s; s = s->next)
19805 s->background_filled_p = 1;
19806 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19807 append_glyph_string_lists (&head, &tail, h, t);
19809 if (clip_head || clip_tail)
19810 for (s = head; s; s = s->next)
19812 s->clip_head = clip_head;
19813 s->clip_tail = clip_tail;
19817 /* Draw all strings. */
19818 for (s = head; s; s = s->next)
19819 rif->draw_glyph_string (s);
19821 if (area == TEXT_AREA
19822 && !row->full_width_p
19823 /* When drawing overlapping rows, only the glyph strings'
19824 foreground is drawn, which doesn't erase a cursor
19825 completely. */
19826 && !overlaps)
19828 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19829 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19830 : (tail ? tail->x + tail->background_width : x));
19832 int text_left = window_box_left (w, TEXT_AREA);
19833 x0 -= text_left;
19834 x1 -= text_left;
19836 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19837 row->y, MATRIX_ROW_BOTTOM_Y (row));
19840 /* Value is the x-position up to which drawn, relative to AREA of W.
19841 This doesn't include parts drawn because of overhangs. */
19842 if (row->full_width_p)
19843 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19844 else
19845 x_reached -= window_box_left (w, area);
19847 RELEASE_HDC (hdc, f);
19849 return x_reached;
19852 /* Expand row matrix if too narrow. Don't expand if area
19853 is not present. */
19855 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19857 if (!fonts_changed_p \
19858 && (it->glyph_row->glyphs[area] \
19859 < it->glyph_row->glyphs[area + 1])) \
19861 it->w->ncols_scale_factor++; \
19862 fonts_changed_p = 1; \
19866 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19867 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19869 static INLINE void
19870 append_glyph (it)
19871 struct it *it;
19873 struct glyph *glyph;
19874 enum glyph_row_area area = it->area;
19876 xassert (it->glyph_row);
19877 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19879 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19880 if (glyph < it->glyph_row->glyphs[area + 1])
19882 glyph->charpos = CHARPOS (it->position);
19883 glyph->object = it->object;
19884 glyph->pixel_width = it->pixel_width;
19885 glyph->ascent = it->ascent;
19886 glyph->descent = it->descent;
19887 glyph->voffset = it->voffset;
19888 glyph->type = CHAR_GLYPH;
19889 glyph->multibyte_p = it->multibyte_p;
19890 glyph->left_box_line_p = it->start_of_box_run_p;
19891 glyph->right_box_line_p = it->end_of_box_run_p;
19892 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19893 || it->phys_descent > it->descent);
19894 glyph->padding_p = 0;
19895 glyph->glyph_not_available_p = it->glyph_not_available_p;
19896 glyph->face_id = it->face_id;
19897 glyph->u.ch = it->char_to_display;
19898 glyph->slice = null_glyph_slice;
19899 glyph->font_type = FONT_TYPE_UNKNOWN;
19900 ++it->glyph_row->used[area];
19902 else
19903 IT_EXPAND_MATRIX_WIDTH (it, area);
19906 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19907 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19909 static INLINE void
19910 append_composite_glyph (it)
19911 struct it *it;
19913 struct glyph *glyph;
19914 enum glyph_row_area area = it->area;
19916 xassert (it->glyph_row);
19918 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19919 if (glyph < it->glyph_row->glyphs[area + 1])
19921 glyph->charpos = CHARPOS (it->position);
19922 glyph->object = it->object;
19923 glyph->pixel_width = it->pixel_width;
19924 glyph->ascent = it->ascent;
19925 glyph->descent = it->descent;
19926 glyph->voffset = it->voffset;
19927 glyph->type = COMPOSITE_GLYPH;
19928 glyph->multibyte_p = it->multibyte_p;
19929 glyph->left_box_line_p = it->start_of_box_run_p;
19930 glyph->right_box_line_p = it->end_of_box_run_p;
19931 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19932 || it->phys_descent > it->descent);
19933 glyph->padding_p = 0;
19934 glyph->glyph_not_available_p = 0;
19935 glyph->face_id = it->face_id;
19936 glyph->u.cmp_id = it->cmp_id;
19937 glyph->slice = null_glyph_slice;
19938 glyph->font_type = FONT_TYPE_UNKNOWN;
19939 ++it->glyph_row->used[area];
19941 else
19942 IT_EXPAND_MATRIX_WIDTH (it, area);
19946 /* Change IT->ascent and IT->height according to the setting of
19947 IT->voffset. */
19949 static INLINE void
19950 take_vertical_position_into_account (it)
19951 struct it *it;
19953 if (it->voffset)
19955 if (it->voffset < 0)
19956 /* Increase the ascent so that we can display the text higher
19957 in the line. */
19958 it->ascent -= it->voffset;
19959 else
19960 /* Increase the descent so that we can display the text lower
19961 in the line. */
19962 it->descent += it->voffset;
19967 /* Produce glyphs/get display metrics for the image IT is loaded with.
19968 See the description of struct display_iterator in dispextern.h for
19969 an overview of struct display_iterator. */
19971 static void
19972 produce_image_glyph (it)
19973 struct it *it;
19975 struct image *img;
19976 struct face *face;
19977 int glyph_ascent, crop;
19978 struct glyph_slice slice;
19980 xassert (it->what == IT_IMAGE);
19982 face = FACE_FROM_ID (it->f, it->face_id);
19983 xassert (face);
19984 /* Make sure X resources of the face is loaded. */
19985 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19987 if (it->image_id < 0)
19989 /* Fringe bitmap. */
19990 it->ascent = it->phys_ascent = 0;
19991 it->descent = it->phys_descent = 0;
19992 it->pixel_width = 0;
19993 it->nglyphs = 0;
19994 return;
19997 img = IMAGE_FROM_ID (it->f, it->image_id);
19998 xassert (img);
19999 /* Make sure X resources of the image is loaded. */
20000 prepare_image_for_display (it->f, img);
20002 slice.x = slice.y = 0;
20003 slice.width = img->width;
20004 slice.height = img->height;
20006 if (INTEGERP (it->slice.x))
20007 slice.x = XINT (it->slice.x);
20008 else if (FLOATP (it->slice.x))
20009 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20011 if (INTEGERP (it->slice.y))
20012 slice.y = XINT (it->slice.y);
20013 else if (FLOATP (it->slice.y))
20014 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20016 if (INTEGERP (it->slice.width))
20017 slice.width = XINT (it->slice.width);
20018 else if (FLOATP (it->slice.width))
20019 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20021 if (INTEGERP (it->slice.height))
20022 slice.height = XINT (it->slice.height);
20023 else if (FLOATP (it->slice.height))
20024 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20026 if (slice.x >= img->width)
20027 slice.x = img->width;
20028 if (slice.y >= img->height)
20029 slice.y = img->height;
20030 if (slice.x + slice.width >= img->width)
20031 slice.width = img->width - slice.x;
20032 if (slice.y + slice.height > img->height)
20033 slice.height = img->height - slice.y;
20035 if (slice.width == 0 || slice.height == 0)
20036 return;
20038 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20040 it->descent = slice.height - glyph_ascent;
20041 if (slice.y == 0)
20042 it->descent += img->vmargin;
20043 if (slice.y + slice.height == img->height)
20044 it->descent += img->vmargin;
20045 it->phys_descent = it->descent;
20047 it->pixel_width = slice.width;
20048 if (slice.x == 0)
20049 it->pixel_width += img->hmargin;
20050 if (slice.x + slice.width == img->width)
20051 it->pixel_width += img->hmargin;
20053 /* It's quite possible for images to have an ascent greater than
20054 their height, so don't get confused in that case. */
20055 if (it->descent < 0)
20056 it->descent = 0;
20058 #if 0 /* this breaks image tiling */
20059 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20060 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20061 if (face_ascent > it->ascent)
20062 it->ascent = it->phys_ascent = face_ascent;
20063 #endif
20065 it->nglyphs = 1;
20067 if (face->box != FACE_NO_BOX)
20069 if (face->box_line_width > 0)
20071 if (slice.y == 0)
20072 it->ascent += face->box_line_width;
20073 if (slice.y + slice.height == img->height)
20074 it->descent += face->box_line_width;
20077 if (it->start_of_box_run_p && slice.x == 0)
20078 it->pixel_width += abs (face->box_line_width);
20079 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20080 it->pixel_width += abs (face->box_line_width);
20083 take_vertical_position_into_account (it);
20085 /* Automatically crop wide image glyphs at right edge so we can
20086 draw the cursor on same display row. */
20087 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20088 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20090 it->pixel_width -= crop;
20091 slice.width -= crop;
20094 if (it->glyph_row)
20096 struct glyph *glyph;
20097 enum glyph_row_area area = it->area;
20099 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20100 if (glyph < it->glyph_row->glyphs[area + 1])
20102 glyph->charpos = CHARPOS (it->position);
20103 glyph->object = it->object;
20104 glyph->pixel_width = it->pixel_width;
20105 glyph->ascent = glyph_ascent;
20106 glyph->descent = it->descent;
20107 glyph->voffset = it->voffset;
20108 glyph->type = IMAGE_GLYPH;
20109 glyph->multibyte_p = it->multibyte_p;
20110 glyph->left_box_line_p = it->start_of_box_run_p;
20111 glyph->right_box_line_p = it->end_of_box_run_p;
20112 glyph->overlaps_vertically_p = 0;
20113 glyph->padding_p = 0;
20114 glyph->glyph_not_available_p = 0;
20115 glyph->face_id = it->face_id;
20116 glyph->u.img_id = img->id;
20117 glyph->slice = slice;
20118 glyph->font_type = FONT_TYPE_UNKNOWN;
20119 ++it->glyph_row->used[area];
20121 else
20122 IT_EXPAND_MATRIX_WIDTH (it, area);
20127 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20128 of the glyph, WIDTH and HEIGHT are the width and height of the
20129 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20131 static void
20132 append_stretch_glyph (it, object, width, height, ascent)
20133 struct it *it;
20134 Lisp_Object object;
20135 int width, height;
20136 int ascent;
20138 struct glyph *glyph;
20139 enum glyph_row_area area = it->area;
20141 xassert (ascent >= 0 && ascent <= height);
20143 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20144 if (glyph < it->glyph_row->glyphs[area + 1])
20146 glyph->charpos = CHARPOS (it->position);
20147 glyph->object = object;
20148 glyph->pixel_width = width;
20149 glyph->ascent = ascent;
20150 glyph->descent = height - ascent;
20151 glyph->voffset = it->voffset;
20152 glyph->type = STRETCH_GLYPH;
20153 glyph->multibyte_p = it->multibyte_p;
20154 glyph->left_box_line_p = it->start_of_box_run_p;
20155 glyph->right_box_line_p = it->end_of_box_run_p;
20156 glyph->overlaps_vertically_p = 0;
20157 glyph->padding_p = 0;
20158 glyph->glyph_not_available_p = 0;
20159 glyph->face_id = it->face_id;
20160 glyph->u.stretch.ascent = ascent;
20161 glyph->u.stretch.height = height;
20162 glyph->slice = null_glyph_slice;
20163 glyph->font_type = FONT_TYPE_UNKNOWN;
20164 ++it->glyph_row->used[area];
20166 else
20167 IT_EXPAND_MATRIX_WIDTH (it, area);
20171 /* Produce a stretch glyph for iterator IT. IT->object is the value
20172 of the glyph property displayed. The value must be a list
20173 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20174 being recognized:
20176 1. `:width WIDTH' specifies that the space should be WIDTH *
20177 canonical char width wide. WIDTH may be an integer or floating
20178 point number.
20180 2. `:relative-width FACTOR' specifies that the width of the stretch
20181 should be computed from the width of the first character having the
20182 `glyph' property, and should be FACTOR times that width.
20184 3. `:align-to HPOS' specifies that the space should be wide enough
20185 to reach HPOS, a value in canonical character units.
20187 Exactly one of the above pairs must be present.
20189 4. `:height HEIGHT' specifies that the height of the stretch produced
20190 should be HEIGHT, measured in canonical character units.
20192 5. `:relative-height FACTOR' specifies that the height of the
20193 stretch should be FACTOR times the height of the characters having
20194 the glyph property.
20196 Either none or exactly one of 4 or 5 must be present.
20198 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20199 of the stretch should be used for the ascent of the stretch.
20200 ASCENT must be in the range 0 <= ASCENT <= 100. */
20202 static void
20203 produce_stretch_glyph (it)
20204 struct it *it;
20206 /* (space :width WIDTH :height HEIGHT ...) */
20207 Lisp_Object prop, plist;
20208 int width = 0, height = 0, align_to = -1;
20209 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20210 int ascent = 0;
20211 double tem;
20212 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20213 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20215 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20217 /* List should start with `space'. */
20218 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20219 plist = XCDR (it->object);
20221 /* Compute the width of the stretch. */
20222 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20223 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20225 /* Absolute width `:width WIDTH' specified and valid. */
20226 zero_width_ok_p = 1;
20227 width = (int)tem;
20229 else if (prop = Fplist_get (plist, QCrelative_width),
20230 NUMVAL (prop) > 0)
20232 /* Relative width `:relative-width FACTOR' specified and valid.
20233 Compute the width of the characters having the `glyph'
20234 property. */
20235 struct it it2;
20236 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20238 it2 = *it;
20239 if (it->multibyte_p)
20241 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20242 - IT_BYTEPOS (*it));
20243 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20245 else
20246 it2.c = *p, it2.len = 1;
20248 it2.glyph_row = NULL;
20249 it2.what = IT_CHARACTER;
20250 x_produce_glyphs (&it2);
20251 width = NUMVAL (prop) * it2.pixel_width;
20253 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20254 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20256 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20257 align_to = (align_to < 0
20259 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20260 else if (align_to < 0)
20261 align_to = window_box_left_offset (it->w, TEXT_AREA);
20262 width = max (0, (int)tem + align_to - it->current_x);
20263 zero_width_ok_p = 1;
20265 else
20266 /* Nothing specified -> width defaults to canonical char width. */
20267 width = FRAME_COLUMN_WIDTH (it->f);
20269 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20270 width = 1;
20272 /* Compute height. */
20273 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20274 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20276 height = (int)tem;
20277 zero_height_ok_p = 1;
20279 else if (prop = Fplist_get (plist, QCrelative_height),
20280 NUMVAL (prop) > 0)
20281 height = FONT_HEIGHT (font) * NUMVAL (prop);
20282 else
20283 height = FONT_HEIGHT (font);
20285 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20286 height = 1;
20288 /* Compute percentage of height used for ascent. If
20289 `:ascent ASCENT' is present and valid, use that. Otherwise,
20290 derive the ascent from the font in use. */
20291 if (prop = Fplist_get (plist, QCascent),
20292 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20293 ascent = height * NUMVAL (prop) / 100.0;
20294 else if (!NILP (prop)
20295 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20296 ascent = min (max (0, (int)tem), height);
20297 else
20298 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20300 if (width > 0 && !it->truncate_lines_p
20301 && it->current_x + width > it->last_visible_x)
20302 width = it->last_visible_x - it->current_x - 1;
20304 if (width > 0 && height > 0 && it->glyph_row)
20306 Lisp_Object object = it->stack[it->sp - 1].string;
20307 if (!STRINGP (object))
20308 object = it->w->buffer;
20309 append_stretch_glyph (it, object, width, height, ascent);
20312 it->pixel_width = width;
20313 it->ascent = it->phys_ascent = ascent;
20314 it->descent = it->phys_descent = height - it->ascent;
20315 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20317 take_vertical_position_into_account (it);
20320 /* Get line-height and line-spacing property at point.
20321 If line-height has format (HEIGHT TOTAL), return TOTAL
20322 in TOTAL_HEIGHT. */
20324 static Lisp_Object
20325 get_line_height_property (it, prop)
20326 struct it *it;
20327 Lisp_Object prop;
20329 Lisp_Object position;
20331 if (STRINGP (it->object))
20332 position = make_number (IT_STRING_CHARPOS (*it));
20333 else if (BUFFERP (it->object))
20334 position = make_number (IT_CHARPOS (*it));
20335 else
20336 return Qnil;
20338 return Fget_char_property (position, prop, it->object);
20341 /* Calculate line-height and line-spacing properties.
20342 An integer value specifies explicit pixel value.
20343 A float value specifies relative value to current face height.
20344 A cons (float . face-name) specifies relative value to
20345 height of specified face font.
20347 Returns height in pixels, or nil. */
20350 static Lisp_Object
20351 calc_line_height_property (it, val, font, boff, override)
20352 struct it *it;
20353 Lisp_Object val;
20354 XFontStruct *font;
20355 int boff, override;
20357 Lisp_Object face_name = Qnil;
20358 int ascent, descent, height;
20360 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20361 return val;
20363 if (CONSP (val))
20365 face_name = XCAR (val);
20366 val = XCDR (val);
20367 if (!NUMBERP (val))
20368 val = make_number (1);
20369 if (NILP (face_name))
20371 height = it->ascent + it->descent;
20372 goto scale;
20376 if (NILP (face_name))
20378 font = FRAME_FONT (it->f);
20379 boff = FRAME_BASELINE_OFFSET (it->f);
20381 else if (EQ (face_name, Qt))
20383 override = 0;
20385 else
20387 int face_id;
20388 struct face *face;
20389 struct font_info *font_info;
20391 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20392 if (face_id < 0)
20393 return make_number (-1);
20395 face = FACE_FROM_ID (it->f, face_id);
20396 font = face->font;
20397 if (font == NULL)
20398 return make_number (-1);
20400 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20401 boff = font_info->baseline_offset;
20402 if (font_info->vertical_centering)
20403 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20406 ascent = FONT_BASE (font) + boff;
20407 descent = FONT_DESCENT (font) - boff;
20409 if (override)
20411 it->override_ascent = ascent;
20412 it->override_descent = descent;
20413 it->override_boff = boff;
20416 height = ascent + descent;
20418 scale:
20419 if (FLOATP (val))
20420 height = (int)(XFLOAT_DATA (val) * height);
20421 else if (INTEGERP (val))
20422 height *= XINT (val);
20424 return make_number (height);
20428 /* RIF:
20429 Produce glyphs/get display metrics for the display element IT is
20430 loaded with. See the description of struct it in dispextern.h
20431 for an overview of struct it. */
20433 void
20434 x_produce_glyphs (it)
20435 struct it *it;
20437 int extra_line_spacing = it->extra_line_spacing;
20439 it->glyph_not_available_p = 0;
20441 if (it->what == IT_CHARACTER)
20443 XChar2b char2b;
20444 XFontStruct *font;
20445 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20446 XCharStruct *pcm;
20447 int font_not_found_p;
20448 struct font_info *font_info;
20449 int boff; /* baseline offset */
20450 /* We may change it->multibyte_p upon unibyte<->multibyte
20451 conversion. So, save the current value now and restore it
20452 later.
20454 Note: It seems that we don't have to record multibyte_p in
20455 struct glyph because the character code itself tells if or
20456 not the character is multibyte. Thus, in the future, we must
20457 consider eliminating the field `multibyte_p' in the struct
20458 glyph. */
20459 int saved_multibyte_p = it->multibyte_p;
20461 /* Maybe translate single-byte characters to multibyte, or the
20462 other way. */
20463 it->char_to_display = it->c;
20464 if (!ASCII_BYTE_P (it->c))
20466 if (unibyte_display_via_language_environment
20467 && SINGLE_BYTE_CHAR_P (it->c)
20468 && (it->c >= 0240
20469 || !NILP (Vnonascii_translation_table)))
20471 it->char_to_display = unibyte_char_to_multibyte (it->c);
20472 it->multibyte_p = 1;
20473 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20474 face = FACE_FROM_ID (it->f, it->face_id);
20476 else if (!SINGLE_BYTE_CHAR_P (it->c)
20477 && !it->multibyte_p)
20479 it->multibyte_p = 1;
20480 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20481 face = FACE_FROM_ID (it->f, it->face_id);
20485 /* Get font to use. Encode IT->char_to_display. */
20486 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20487 &char2b, it->multibyte_p, 0);
20488 font = face->font;
20490 /* When no suitable font found, use the default font. */
20491 font_not_found_p = font == NULL;
20492 if (font_not_found_p)
20494 font = FRAME_FONT (it->f);
20495 boff = FRAME_BASELINE_OFFSET (it->f);
20496 font_info = NULL;
20498 else
20500 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20501 boff = font_info->baseline_offset;
20502 if (font_info->vertical_centering)
20503 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20506 if (it->char_to_display >= ' '
20507 && (!it->multibyte_p || it->char_to_display < 128))
20509 /* Either unibyte or ASCII. */
20510 int stretched_p;
20512 it->nglyphs = 1;
20514 pcm = rif->per_char_metric (font, &char2b,
20515 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20517 if (it->override_ascent >= 0)
20519 it->ascent = it->override_ascent;
20520 it->descent = it->override_descent;
20521 boff = it->override_boff;
20523 else
20525 it->ascent = FONT_BASE (font) + boff;
20526 it->descent = FONT_DESCENT (font) - boff;
20529 if (pcm)
20531 it->phys_ascent = pcm->ascent + boff;
20532 it->phys_descent = pcm->descent - boff;
20533 it->pixel_width = pcm->width;
20535 else
20537 it->glyph_not_available_p = 1;
20538 it->phys_ascent = it->ascent;
20539 it->phys_descent = it->descent;
20540 it->pixel_width = FONT_WIDTH (font);
20543 if (it->constrain_row_ascent_descent_p)
20545 if (it->descent > it->max_descent)
20547 it->ascent += it->descent - it->max_descent;
20548 it->descent = it->max_descent;
20550 if (it->ascent > it->max_ascent)
20552 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20553 it->ascent = it->max_ascent;
20555 it->phys_ascent = min (it->phys_ascent, it->ascent);
20556 it->phys_descent = min (it->phys_descent, it->descent);
20557 extra_line_spacing = 0;
20560 /* If this is a space inside a region of text with
20561 `space-width' property, change its width. */
20562 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20563 if (stretched_p)
20564 it->pixel_width *= XFLOATINT (it->space_width);
20566 /* If face has a box, add the box thickness to the character
20567 height. If character has a box line to the left and/or
20568 right, add the box line width to the character's width. */
20569 if (face->box != FACE_NO_BOX)
20571 int thick = face->box_line_width;
20573 if (thick > 0)
20575 it->ascent += thick;
20576 it->descent += thick;
20578 else
20579 thick = -thick;
20581 if (it->start_of_box_run_p)
20582 it->pixel_width += thick;
20583 if (it->end_of_box_run_p)
20584 it->pixel_width += thick;
20587 /* If face has an overline, add the height of the overline
20588 (1 pixel) and a 1 pixel margin to the character height. */
20589 if (face->overline_p)
20590 it->ascent += overline_margin;
20592 if (it->constrain_row_ascent_descent_p)
20594 if (it->ascent > it->max_ascent)
20595 it->ascent = it->max_ascent;
20596 if (it->descent > it->max_descent)
20597 it->descent = it->max_descent;
20600 take_vertical_position_into_account (it);
20602 /* If we have to actually produce glyphs, do it. */
20603 if (it->glyph_row)
20605 if (stretched_p)
20607 /* Translate a space with a `space-width' property
20608 into a stretch glyph. */
20609 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20610 / FONT_HEIGHT (font));
20611 append_stretch_glyph (it, it->object, it->pixel_width,
20612 it->ascent + it->descent, ascent);
20614 else
20615 append_glyph (it);
20617 /* If characters with lbearing or rbearing are displayed
20618 in this line, record that fact in a flag of the
20619 glyph row. This is used to optimize X output code. */
20620 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20621 it->glyph_row->contains_overlapping_glyphs_p = 1;
20624 else if (it->char_to_display == '\n')
20626 /* A newline has no width but we need the height of the line.
20627 But if previous part of the line set a height, don't
20628 increase that height */
20630 Lisp_Object height;
20631 Lisp_Object total_height = Qnil;
20633 it->override_ascent = -1;
20634 it->pixel_width = 0;
20635 it->nglyphs = 0;
20637 height = get_line_height_property(it, Qline_height);
20638 /* Split (line-height total-height) list */
20639 if (CONSP (height)
20640 && CONSP (XCDR (height))
20641 && NILP (XCDR (XCDR (height))))
20643 total_height = XCAR (XCDR (height));
20644 height = XCAR (height);
20646 height = calc_line_height_property(it, height, font, boff, 1);
20648 if (it->override_ascent >= 0)
20650 it->ascent = it->override_ascent;
20651 it->descent = it->override_descent;
20652 boff = it->override_boff;
20654 else
20656 it->ascent = FONT_BASE (font) + boff;
20657 it->descent = FONT_DESCENT (font) - boff;
20660 if (EQ (height, Qt))
20662 if (it->descent > it->max_descent)
20664 it->ascent += it->descent - it->max_descent;
20665 it->descent = it->max_descent;
20667 if (it->ascent > it->max_ascent)
20669 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20670 it->ascent = it->max_ascent;
20672 it->phys_ascent = min (it->phys_ascent, it->ascent);
20673 it->phys_descent = min (it->phys_descent, it->descent);
20674 it->constrain_row_ascent_descent_p = 1;
20675 extra_line_spacing = 0;
20677 else
20679 Lisp_Object spacing;
20681 it->phys_ascent = it->ascent;
20682 it->phys_descent = it->descent;
20684 if ((it->max_ascent > 0 || it->max_descent > 0)
20685 && face->box != FACE_NO_BOX
20686 && face->box_line_width > 0)
20688 it->ascent += face->box_line_width;
20689 it->descent += face->box_line_width;
20691 if (!NILP (height)
20692 && XINT (height) > it->ascent + it->descent)
20693 it->ascent = XINT (height) - it->descent;
20695 if (!NILP (total_height))
20696 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20697 else
20699 spacing = get_line_height_property(it, Qline_spacing);
20700 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20702 if (INTEGERP (spacing))
20704 extra_line_spacing = XINT (spacing);
20705 if (!NILP (total_height))
20706 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20710 else if (it->char_to_display == '\t')
20712 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20713 int x = it->current_x + it->continuation_lines_width;
20714 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20716 /* If the distance from the current position to the next tab
20717 stop is less than a space character width, use the
20718 tab stop after that. */
20719 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20720 next_tab_x += tab_width;
20722 it->pixel_width = next_tab_x - x;
20723 it->nglyphs = 1;
20724 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20725 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20727 if (it->glyph_row)
20729 append_stretch_glyph (it, it->object, it->pixel_width,
20730 it->ascent + it->descent, it->ascent);
20733 else
20735 /* A multi-byte character. Assume that the display width of the
20736 character is the width of the character multiplied by the
20737 width of the font. */
20739 /* If we found a font, this font should give us the right
20740 metrics. If we didn't find a font, use the frame's
20741 default font and calculate the width of the character
20742 from the charset width; this is what old redisplay code
20743 did. */
20745 pcm = rif->per_char_metric (font, &char2b,
20746 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20748 if (font_not_found_p || !pcm)
20750 int charset = CHAR_CHARSET (it->char_to_display);
20752 it->glyph_not_available_p = 1;
20753 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20754 * CHARSET_WIDTH (charset));
20755 it->phys_ascent = FONT_BASE (font) + boff;
20756 it->phys_descent = FONT_DESCENT (font) - boff;
20758 else
20760 it->pixel_width = pcm->width;
20761 it->phys_ascent = pcm->ascent + boff;
20762 it->phys_descent = pcm->descent - boff;
20763 if (it->glyph_row
20764 && (pcm->lbearing < 0
20765 || pcm->rbearing > pcm->width))
20766 it->glyph_row->contains_overlapping_glyphs_p = 1;
20768 it->nglyphs = 1;
20769 it->ascent = FONT_BASE (font) + boff;
20770 it->descent = FONT_DESCENT (font) - boff;
20771 if (face->box != FACE_NO_BOX)
20773 int thick = face->box_line_width;
20775 if (thick > 0)
20777 it->ascent += thick;
20778 it->descent += thick;
20780 else
20781 thick = - thick;
20783 if (it->start_of_box_run_p)
20784 it->pixel_width += thick;
20785 if (it->end_of_box_run_p)
20786 it->pixel_width += thick;
20789 /* If face has an overline, add the height of the overline
20790 (1 pixel) and a 1 pixel margin to the character height. */
20791 if (face->overline_p)
20792 it->ascent += overline_margin;
20794 take_vertical_position_into_account (it);
20796 if (it->glyph_row)
20797 append_glyph (it);
20799 it->multibyte_p = saved_multibyte_p;
20801 else if (it->what == IT_COMPOSITION)
20803 /* Note: A composition is represented as one glyph in the
20804 glyph matrix. There are no padding glyphs. */
20805 XChar2b char2b;
20806 XFontStruct *font;
20807 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20808 XCharStruct *pcm;
20809 int font_not_found_p;
20810 struct font_info *font_info;
20811 int boff; /* baseline offset */
20812 struct composition *cmp = composition_table[it->cmp_id];
20814 /* Maybe translate single-byte characters to multibyte. */
20815 it->char_to_display = it->c;
20816 if (unibyte_display_via_language_environment
20817 && SINGLE_BYTE_CHAR_P (it->c)
20818 && (it->c >= 0240
20819 || (it->c >= 0200
20820 && !NILP (Vnonascii_translation_table))))
20822 it->char_to_display = unibyte_char_to_multibyte (it->c);
20825 /* Get face and font to use. Encode IT->char_to_display. */
20826 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20827 face = FACE_FROM_ID (it->f, it->face_id);
20828 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20829 &char2b, it->multibyte_p, 0);
20830 font = face->font;
20832 /* When no suitable font found, use the default font. */
20833 font_not_found_p = font == NULL;
20834 if (font_not_found_p)
20836 font = FRAME_FONT (it->f);
20837 boff = FRAME_BASELINE_OFFSET (it->f);
20838 font_info = NULL;
20840 else
20842 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20843 boff = font_info->baseline_offset;
20844 if (font_info->vertical_centering)
20845 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20848 /* There are no padding glyphs, so there is only one glyph to
20849 produce for the composition. Important is that pixel_width,
20850 ascent and descent are the values of what is drawn by
20851 draw_glyphs (i.e. the values of the overall glyphs composed). */
20852 it->nglyphs = 1;
20854 /* If we have not yet calculated pixel size data of glyphs of
20855 the composition for the current face font, calculate them
20856 now. Theoretically, we have to check all fonts for the
20857 glyphs, but that requires much time and memory space. So,
20858 here we check only the font of the first glyph. This leads
20859 to incorrect display very rarely, and C-l (recenter) can
20860 correct the display anyway. */
20861 if (cmp->font != (void *) font)
20863 /* Ascent and descent of the font of the first character of
20864 this composition (adjusted by baseline offset). Ascent
20865 and descent of overall glyphs should not be less than
20866 them respectively. */
20867 int font_ascent = FONT_BASE (font) + boff;
20868 int font_descent = FONT_DESCENT (font) - boff;
20869 /* Bounding box of the overall glyphs. */
20870 int leftmost, rightmost, lowest, highest;
20871 int i, width, ascent, descent;
20873 cmp->font = (void *) font;
20875 /* Initialize the bounding box. */
20876 if (font_info
20877 && (pcm = rif->per_char_metric (font, &char2b,
20878 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20880 width = pcm->width;
20881 ascent = pcm->ascent;
20882 descent = pcm->descent;
20884 else
20886 width = FONT_WIDTH (font);
20887 ascent = FONT_BASE (font);
20888 descent = FONT_DESCENT (font);
20891 rightmost = width;
20892 lowest = - descent + boff;
20893 highest = ascent + boff;
20894 leftmost = 0;
20896 if (font_info
20897 && font_info->default_ascent
20898 && CHAR_TABLE_P (Vuse_default_ascent)
20899 && !NILP (Faref (Vuse_default_ascent,
20900 make_number (it->char_to_display))))
20901 highest = font_info->default_ascent + boff;
20903 /* Draw the first glyph at the normal position. It may be
20904 shifted to right later if some other glyphs are drawn at
20905 the left. */
20906 cmp->offsets[0] = 0;
20907 cmp->offsets[1] = boff;
20909 /* Set cmp->offsets for the remaining glyphs. */
20910 for (i = 1; i < cmp->glyph_len; i++)
20912 int left, right, btm, top;
20913 int ch = COMPOSITION_GLYPH (cmp, i);
20914 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20916 face = FACE_FROM_ID (it->f, face_id);
20917 get_char_face_and_encoding (it->f, ch, face->id,
20918 &char2b, it->multibyte_p, 0);
20919 font = face->font;
20920 if (font == NULL)
20922 font = FRAME_FONT (it->f);
20923 boff = FRAME_BASELINE_OFFSET (it->f);
20924 font_info = NULL;
20926 else
20928 font_info
20929 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20930 boff = font_info->baseline_offset;
20931 if (font_info->vertical_centering)
20932 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20935 if (font_info
20936 && (pcm = rif->per_char_metric (font, &char2b,
20937 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20939 width = pcm->width;
20940 ascent = pcm->ascent;
20941 descent = pcm->descent;
20943 else
20945 width = FONT_WIDTH (font);
20946 ascent = 1;
20947 descent = 0;
20950 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20952 /* Relative composition with or without
20953 alternate chars. */
20954 left = (leftmost + rightmost - width) / 2;
20955 btm = - descent + boff;
20956 if (font_info && font_info->relative_compose
20957 && (! CHAR_TABLE_P (Vignore_relative_composition)
20958 || NILP (Faref (Vignore_relative_composition,
20959 make_number (ch)))))
20962 if (- descent >= font_info->relative_compose)
20963 /* One extra pixel between two glyphs. */
20964 btm = highest + 1;
20965 else if (ascent <= 0)
20966 /* One extra pixel between two glyphs. */
20967 btm = lowest - 1 - ascent - descent;
20970 else
20972 /* A composition rule is specified by an integer
20973 value that encodes global and new reference
20974 points (GREF and NREF). GREF and NREF are
20975 specified by numbers as below:
20977 0---1---2 -- ascent
20981 9--10--11 -- center
20983 ---3---4---5--- baseline
20985 6---7---8 -- descent
20987 int rule = COMPOSITION_RULE (cmp, i);
20988 int gref, nref, grefx, grefy, nrefx, nrefy;
20990 COMPOSITION_DECODE_RULE (rule, gref, nref);
20991 grefx = gref % 3, nrefx = nref % 3;
20992 grefy = gref / 3, nrefy = nref / 3;
20994 left = (leftmost
20995 + grefx * (rightmost - leftmost) / 2
20996 - nrefx * width / 2);
20997 btm = ((grefy == 0 ? highest
20998 : grefy == 1 ? 0
20999 : grefy == 2 ? lowest
21000 : (highest + lowest) / 2)
21001 - (nrefy == 0 ? ascent + descent
21002 : nrefy == 1 ? descent - boff
21003 : nrefy == 2 ? 0
21004 : (ascent + descent) / 2));
21007 cmp->offsets[i * 2] = left;
21008 cmp->offsets[i * 2 + 1] = btm + descent;
21010 /* Update the bounding box of the overall glyphs. */
21011 right = left + width;
21012 top = btm + descent + ascent;
21013 if (left < leftmost)
21014 leftmost = left;
21015 if (right > rightmost)
21016 rightmost = right;
21017 if (top > highest)
21018 highest = top;
21019 if (btm < lowest)
21020 lowest = btm;
21023 /* If there are glyphs whose x-offsets are negative,
21024 shift all glyphs to the right and make all x-offsets
21025 non-negative. */
21026 if (leftmost < 0)
21028 for (i = 0; i < cmp->glyph_len; i++)
21029 cmp->offsets[i * 2] -= leftmost;
21030 rightmost -= leftmost;
21033 cmp->pixel_width = rightmost;
21034 cmp->ascent = highest;
21035 cmp->descent = - lowest;
21036 if (cmp->ascent < font_ascent)
21037 cmp->ascent = font_ascent;
21038 if (cmp->descent < font_descent)
21039 cmp->descent = font_descent;
21042 it->pixel_width = cmp->pixel_width;
21043 it->ascent = it->phys_ascent = cmp->ascent;
21044 it->descent = it->phys_descent = cmp->descent;
21046 if (face->box != FACE_NO_BOX)
21048 int thick = face->box_line_width;
21050 if (thick > 0)
21052 it->ascent += thick;
21053 it->descent += thick;
21055 else
21056 thick = - thick;
21058 if (it->start_of_box_run_p)
21059 it->pixel_width += thick;
21060 if (it->end_of_box_run_p)
21061 it->pixel_width += thick;
21064 /* If face has an overline, add the height of the overline
21065 (1 pixel) and a 1 pixel margin to the character height. */
21066 if (face->overline_p)
21067 it->ascent += overline_margin;
21069 take_vertical_position_into_account (it);
21071 if (it->glyph_row)
21072 append_composite_glyph (it);
21074 else if (it->what == IT_IMAGE)
21075 produce_image_glyph (it);
21076 else if (it->what == IT_STRETCH)
21077 produce_stretch_glyph (it);
21079 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21080 because this isn't true for images with `:ascent 100'. */
21081 xassert (it->ascent >= 0 && it->descent >= 0);
21082 if (it->area == TEXT_AREA)
21083 it->current_x += it->pixel_width;
21085 if (extra_line_spacing > 0)
21087 it->descent += extra_line_spacing;
21088 if (extra_line_spacing > it->max_extra_line_spacing)
21089 it->max_extra_line_spacing = extra_line_spacing;
21092 it->max_ascent = max (it->max_ascent, it->ascent);
21093 it->max_descent = max (it->max_descent, it->descent);
21094 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21095 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21098 /* EXPORT for RIF:
21099 Output LEN glyphs starting at START at the nominal cursor position.
21100 Advance the nominal cursor over the text. The global variable
21101 updated_window contains the window being updated, updated_row is
21102 the glyph row being updated, and updated_area is the area of that
21103 row being updated. */
21105 void
21106 x_write_glyphs (start, len)
21107 struct glyph *start;
21108 int len;
21110 int x, hpos;
21112 xassert (updated_window && updated_row);
21113 BLOCK_INPUT;
21115 /* Write glyphs. */
21117 hpos = start - updated_row->glyphs[updated_area];
21118 x = draw_glyphs (updated_window, output_cursor.x,
21119 updated_row, updated_area,
21120 hpos, hpos + len,
21121 DRAW_NORMAL_TEXT, 0);
21123 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21124 if (updated_area == TEXT_AREA
21125 && updated_window->phys_cursor_on_p
21126 && updated_window->phys_cursor.vpos == output_cursor.vpos
21127 && updated_window->phys_cursor.hpos >= hpos
21128 && updated_window->phys_cursor.hpos < hpos + len)
21129 updated_window->phys_cursor_on_p = 0;
21131 UNBLOCK_INPUT;
21133 /* Advance the output cursor. */
21134 output_cursor.hpos += len;
21135 output_cursor.x = x;
21139 /* EXPORT for RIF:
21140 Insert LEN glyphs from START at the nominal cursor position. */
21142 void
21143 x_insert_glyphs (start, len)
21144 struct glyph *start;
21145 int len;
21147 struct frame *f;
21148 struct window *w;
21149 int line_height, shift_by_width, shifted_region_width;
21150 struct glyph_row *row;
21151 struct glyph *glyph;
21152 int frame_x, frame_y, hpos;
21154 xassert (updated_window && updated_row);
21155 BLOCK_INPUT;
21156 w = updated_window;
21157 f = XFRAME (WINDOW_FRAME (w));
21159 /* Get the height of the line we are in. */
21160 row = updated_row;
21161 line_height = row->height;
21163 /* Get the width of the glyphs to insert. */
21164 shift_by_width = 0;
21165 for (glyph = start; glyph < start + len; ++glyph)
21166 shift_by_width += glyph->pixel_width;
21168 /* Get the width of the region to shift right. */
21169 shifted_region_width = (window_box_width (w, updated_area)
21170 - output_cursor.x
21171 - shift_by_width);
21173 /* Shift right. */
21174 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21175 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21177 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21178 line_height, shift_by_width);
21180 /* Write the glyphs. */
21181 hpos = start - row->glyphs[updated_area];
21182 draw_glyphs (w, output_cursor.x, row, updated_area,
21183 hpos, hpos + len,
21184 DRAW_NORMAL_TEXT, 0);
21186 /* Advance the output cursor. */
21187 output_cursor.hpos += len;
21188 output_cursor.x += shift_by_width;
21189 UNBLOCK_INPUT;
21193 /* EXPORT for RIF:
21194 Erase the current text line from the nominal cursor position
21195 (inclusive) to pixel column TO_X (exclusive). The idea is that
21196 everything from TO_X onward is already erased.
21198 TO_X is a pixel position relative to updated_area of
21199 updated_window. TO_X == -1 means clear to the end of this area. */
21201 void
21202 x_clear_end_of_line (to_x)
21203 int to_x;
21205 struct frame *f;
21206 struct window *w = updated_window;
21207 int max_x, min_y, max_y;
21208 int from_x, from_y, to_y;
21210 xassert (updated_window && updated_row);
21211 f = XFRAME (w->frame);
21213 if (updated_row->full_width_p)
21214 max_x = WINDOW_TOTAL_WIDTH (w);
21215 else
21216 max_x = window_box_width (w, updated_area);
21217 max_y = window_text_bottom_y (w);
21219 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21220 of window. For TO_X > 0, truncate to end of drawing area. */
21221 if (to_x == 0)
21222 return;
21223 else if (to_x < 0)
21224 to_x = max_x;
21225 else
21226 to_x = min (to_x, max_x);
21228 to_y = min (max_y, output_cursor.y + updated_row->height);
21230 /* Notice if the cursor will be cleared by this operation. */
21231 if (!updated_row->full_width_p)
21232 notice_overwritten_cursor (w, updated_area,
21233 output_cursor.x, -1,
21234 updated_row->y,
21235 MATRIX_ROW_BOTTOM_Y (updated_row));
21237 from_x = output_cursor.x;
21239 /* Translate to frame coordinates. */
21240 if (updated_row->full_width_p)
21242 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21243 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21245 else
21247 int area_left = window_box_left (w, updated_area);
21248 from_x += area_left;
21249 to_x += area_left;
21252 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21253 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21254 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21256 /* Prevent inadvertently clearing to end of the X window. */
21257 if (to_x > from_x && to_y > from_y)
21259 BLOCK_INPUT;
21260 rif->clear_frame_area (f, from_x, from_y,
21261 to_x - from_x, to_y - from_y);
21262 UNBLOCK_INPUT;
21266 #endif /* HAVE_WINDOW_SYSTEM */
21270 /***********************************************************************
21271 Cursor types
21272 ***********************************************************************/
21274 /* Value is the internal representation of the specified cursor type
21275 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21276 of the bar cursor. */
21278 static enum text_cursor_kinds
21279 get_specified_cursor_type (arg, width)
21280 Lisp_Object arg;
21281 int *width;
21283 enum text_cursor_kinds type;
21285 if (NILP (arg))
21286 return NO_CURSOR;
21288 if (EQ (arg, Qbox))
21289 return FILLED_BOX_CURSOR;
21291 if (EQ (arg, Qhollow))
21292 return HOLLOW_BOX_CURSOR;
21294 if (EQ (arg, Qbar))
21296 *width = 2;
21297 return BAR_CURSOR;
21300 if (CONSP (arg)
21301 && EQ (XCAR (arg), Qbar)
21302 && INTEGERP (XCDR (arg))
21303 && XINT (XCDR (arg)) >= 0)
21305 *width = XINT (XCDR (arg));
21306 return BAR_CURSOR;
21309 if (EQ (arg, Qhbar))
21311 *width = 2;
21312 return HBAR_CURSOR;
21315 if (CONSP (arg)
21316 && EQ (XCAR (arg), Qhbar)
21317 && INTEGERP (XCDR (arg))
21318 && XINT (XCDR (arg)) >= 0)
21320 *width = XINT (XCDR (arg));
21321 return HBAR_CURSOR;
21324 /* Treat anything unknown as "hollow box cursor".
21325 It was bad to signal an error; people have trouble fixing
21326 .Xdefaults with Emacs, when it has something bad in it. */
21327 type = HOLLOW_BOX_CURSOR;
21329 return type;
21332 /* Set the default cursor types for specified frame. */
21333 void
21334 set_frame_cursor_types (f, arg)
21335 struct frame *f;
21336 Lisp_Object arg;
21338 int width;
21339 Lisp_Object tem;
21341 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21342 FRAME_CURSOR_WIDTH (f) = width;
21344 /* By default, set up the blink-off state depending on the on-state. */
21346 tem = Fassoc (arg, Vblink_cursor_alist);
21347 if (!NILP (tem))
21349 FRAME_BLINK_OFF_CURSOR (f)
21350 = get_specified_cursor_type (XCDR (tem), &width);
21351 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21353 else
21354 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21358 /* Return the cursor we want to be displayed in window W. Return
21359 width of bar/hbar cursor through WIDTH arg. Return with
21360 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21361 (i.e. if the `system caret' should track this cursor).
21363 In a mini-buffer window, we want the cursor only to appear if we
21364 are reading input from this window. For the selected window, we
21365 want the cursor type given by the frame parameter or buffer local
21366 setting of cursor-type. If explicitly marked off, draw no cursor.
21367 In all other cases, we want a hollow box cursor. */
21369 static enum text_cursor_kinds
21370 get_window_cursor_type (w, glyph, width, active_cursor)
21371 struct window *w;
21372 struct glyph *glyph;
21373 int *width;
21374 int *active_cursor;
21376 struct frame *f = XFRAME (w->frame);
21377 struct buffer *b = XBUFFER (w->buffer);
21378 int cursor_type = DEFAULT_CURSOR;
21379 Lisp_Object alt_cursor;
21380 int non_selected = 0;
21382 *active_cursor = 1;
21384 /* Echo area */
21385 if (cursor_in_echo_area
21386 && FRAME_HAS_MINIBUF_P (f)
21387 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21389 if (w == XWINDOW (echo_area_window))
21391 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21393 *width = FRAME_CURSOR_WIDTH (f);
21394 return FRAME_DESIRED_CURSOR (f);
21396 else
21397 return get_specified_cursor_type (b->cursor_type, width);
21400 *active_cursor = 0;
21401 non_selected = 1;
21404 /* Detect a nonselected window or nonselected frame. */
21405 else if (w != XWINDOW (f->selected_window)
21406 #ifdef HAVE_WINDOW_SYSTEM
21407 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21408 #endif
21411 *active_cursor = 0;
21413 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21414 return NO_CURSOR;
21416 non_selected = 1;
21419 /* Never display a cursor in a window in which cursor-type is nil. */
21420 if (NILP (b->cursor_type))
21421 return NO_CURSOR;
21423 /* Get the normal cursor type for this window. */
21424 if (EQ (b->cursor_type, Qt))
21426 cursor_type = FRAME_DESIRED_CURSOR (f);
21427 *width = FRAME_CURSOR_WIDTH (f);
21429 else
21430 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21432 /* Use cursor-in-non-selected-windows instead
21433 for non-selected window or frame. */
21434 if (non_selected)
21436 alt_cursor = b->cursor_in_non_selected_windows;
21437 if (!EQ (Qt, alt_cursor))
21438 return get_specified_cursor_type (alt_cursor, width);
21439 /* t means modify the normal cursor type. */
21440 if (cursor_type == FILLED_BOX_CURSOR)
21441 cursor_type = HOLLOW_BOX_CURSOR;
21442 else if (cursor_type == BAR_CURSOR && *width > 1)
21443 --*width;
21444 return cursor_type;
21447 /* Use normal cursor if not blinked off. */
21448 if (!w->cursor_off_p)
21450 #ifdef HAVE_WINDOW_SYSTEM
21451 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21453 if (cursor_type == FILLED_BOX_CURSOR)
21455 /* Using a block cursor on large images can be very annoying.
21456 So use a hollow cursor for "large" images.
21457 If image is not transparent (no mask), also use hollow cursor. */
21458 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21459 if (img != NULL && IMAGEP (img->spec))
21461 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21462 where N = size of default frame font size.
21463 This should cover most of the "tiny" icons people may use. */
21464 if (!img->mask
21465 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21466 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21467 cursor_type = HOLLOW_BOX_CURSOR;
21470 else if (cursor_type != NO_CURSOR)
21472 /* Display current only supports BOX and HOLLOW cursors for images.
21473 So for now, unconditionally use a HOLLOW cursor when cursor is
21474 not a solid box cursor. */
21475 cursor_type = HOLLOW_BOX_CURSOR;
21478 #endif
21479 return cursor_type;
21482 /* Cursor is blinked off, so determine how to "toggle" it. */
21484 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21485 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21486 return get_specified_cursor_type (XCDR (alt_cursor), width);
21488 /* Then see if frame has specified a specific blink off cursor type. */
21489 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21491 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21492 return FRAME_BLINK_OFF_CURSOR (f);
21495 #if 0
21496 /* Some people liked having a permanently visible blinking cursor,
21497 while others had very strong opinions against it. So it was
21498 decided to remove it. KFS 2003-09-03 */
21500 /* Finally perform built-in cursor blinking:
21501 filled box <-> hollow box
21502 wide [h]bar <-> narrow [h]bar
21503 narrow [h]bar <-> no cursor
21504 other type <-> no cursor */
21506 if (cursor_type == FILLED_BOX_CURSOR)
21507 return HOLLOW_BOX_CURSOR;
21509 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21511 *width = 1;
21512 return cursor_type;
21514 #endif
21516 return NO_CURSOR;
21520 #ifdef HAVE_WINDOW_SYSTEM
21522 /* Notice when the text cursor of window W has been completely
21523 overwritten by a drawing operation that outputs glyphs in AREA
21524 starting at X0 and ending at X1 in the line starting at Y0 and
21525 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21526 the rest of the line after X0 has been written. Y coordinates
21527 are window-relative. */
21529 static void
21530 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21531 struct window *w;
21532 enum glyph_row_area area;
21533 int x0, y0, x1, y1;
21535 int cx0, cx1, cy0, cy1;
21536 struct glyph_row *row;
21538 if (!w->phys_cursor_on_p)
21539 return;
21540 if (area != TEXT_AREA)
21541 return;
21543 if (w->phys_cursor.vpos < 0
21544 || w->phys_cursor.vpos >= w->current_matrix->nrows
21545 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21546 !(row->enabled_p && row->displays_text_p)))
21547 return;
21549 if (row->cursor_in_fringe_p)
21551 row->cursor_in_fringe_p = 0;
21552 draw_fringe_bitmap (w, row, 0);
21553 w->phys_cursor_on_p = 0;
21554 return;
21557 cx0 = w->phys_cursor.x;
21558 cx1 = cx0 + w->phys_cursor_width;
21559 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21560 return;
21562 /* The cursor image will be completely removed from the
21563 screen if the output area intersects the cursor area in
21564 y-direction. When we draw in [y0 y1[, and some part of
21565 the cursor is at y < y0, that part must have been drawn
21566 before. When scrolling, the cursor is erased before
21567 actually scrolling, so we don't come here. When not
21568 scrolling, the rows above the old cursor row must have
21569 changed, and in this case these rows must have written
21570 over the cursor image.
21572 Likewise if part of the cursor is below y1, with the
21573 exception of the cursor being in the first blank row at
21574 the buffer and window end because update_text_area
21575 doesn't draw that row. (Except when it does, but
21576 that's handled in update_text_area.) */
21578 cy0 = w->phys_cursor.y;
21579 cy1 = cy0 + w->phys_cursor_height;
21580 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21581 return;
21583 w->phys_cursor_on_p = 0;
21586 #endif /* HAVE_WINDOW_SYSTEM */
21589 /************************************************************************
21590 Mouse Face
21591 ************************************************************************/
21593 #ifdef HAVE_WINDOW_SYSTEM
21595 /* EXPORT for RIF:
21596 Fix the display of area AREA of overlapping row ROW in window W
21597 with respect to the overlapping part OVERLAPS. */
21599 void
21600 x_fix_overlapping_area (w, row, area, overlaps)
21601 struct window *w;
21602 struct glyph_row *row;
21603 enum glyph_row_area area;
21604 int overlaps;
21606 int i, x;
21608 BLOCK_INPUT;
21610 x = 0;
21611 for (i = 0; i < row->used[area];)
21613 if (row->glyphs[area][i].overlaps_vertically_p)
21615 int start = i, start_x = x;
21619 x += row->glyphs[area][i].pixel_width;
21620 ++i;
21622 while (i < row->used[area]
21623 && row->glyphs[area][i].overlaps_vertically_p);
21625 draw_glyphs (w, start_x, row, area,
21626 start, i,
21627 DRAW_NORMAL_TEXT, overlaps);
21629 else
21631 x += row->glyphs[area][i].pixel_width;
21632 ++i;
21636 UNBLOCK_INPUT;
21640 /* EXPORT:
21641 Draw the cursor glyph of window W in glyph row ROW. See the
21642 comment of draw_glyphs for the meaning of HL. */
21644 void
21645 draw_phys_cursor_glyph (w, row, hl)
21646 struct window *w;
21647 struct glyph_row *row;
21648 enum draw_glyphs_face hl;
21650 /* If cursor hpos is out of bounds, don't draw garbage. This can
21651 happen in mini-buffer windows when switching between echo area
21652 glyphs and mini-buffer. */
21653 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21655 int on_p = w->phys_cursor_on_p;
21656 int x1;
21657 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21658 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21659 hl, 0);
21660 w->phys_cursor_on_p = on_p;
21662 if (hl == DRAW_CURSOR)
21663 w->phys_cursor_width = x1 - w->phys_cursor.x;
21664 /* When we erase the cursor, and ROW is overlapped by other
21665 rows, make sure that these overlapping parts of other rows
21666 are redrawn. */
21667 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21669 w->phys_cursor_width = x1 - w->phys_cursor.x;
21671 if (row > w->current_matrix->rows
21672 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21673 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21674 OVERLAPS_ERASED_CURSOR);
21676 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21677 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21678 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21679 OVERLAPS_ERASED_CURSOR);
21685 /* EXPORT:
21686 Erase the image of a cursor of window W from the screen. */
21688 void
21689 erase_phys_cursor (w)
21690 struct window *w;
21692 struct frame *f = XFRAME (w->frame);
21693 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21694 int hpos = w->phys_cursor.hpos;
21695 int vpos = w->phys_cursor.vpos;
21696 int mouse_face_here_p = 0;
21697 struct glyph_matrix *active_glyphs = w->current_matrix;
21698 struct glyph_row *cursor_row;
21699 struct glyph *cursor_glyph;
21700 enum draw_glyphs_face hl;
21702 /* No cursor displayed or row invalidated => nothing to do on the
21703 screen. */
21704 if (w->phys_cursor_type == NO_CURSOR)
21705 goto mark_cursor_off;
21707 /* VPOS >= active_glyphs->nrows means that window has been resized.
21708 Don't bother to erase the cursor. */
21709 if (vpos >= active_glyphs->nrows)
21710 goto mark_cursor_off;
21712 /* If row containing cursor is marked invalid, there is nothing we
21713 can do. */
21714 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21715 if (!cursor_row->enabled_p)
21716 goto mark_cursor_off;
21718 /* If line spacing is > 0, old cursor may only be partially visible in
21719 window after split-window. So adjust visible height. */
21720 cursor_row->visible_height = min (cursor_row->visible_height,
21721 window_text_bottom_y (w) - cursor_row->y);
21723 /* If row is completely invisible, don't attempt to delete a cursor which
21724 isn't there. This can happen if cursor is at top of a window, and
21725 we switch to a buffer with a header line in that window. */
21726 if (cursor_row->visible_height <= 0)
21727 goto mark_cursor_off;
21729 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21730 if (cursor_row->cursor_in_fringe_p)
21732 cursor_row->cursor_in_fringe_p = 0;
21733 draw_fringe_bitmap (w, cursor_row, 0);
21734 goto mark_cursor_off;
21737 /* This can happen when the new row is shorter than the old one.
21738 In this case, either draw_glyphs or clear_end_of_line
21739 should have cleared the cursor. Note that we wouldn't be
21740 able to erase the cursor in this case because we don't have a
21741 cursor glyph at hand. */
21742 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21743 goto mark_cursor_off;
21745 /* If the cursor is in the mouse face area, redisplay that when
21746 we clear the cursor. */
21747 if (! NILP (dpyinfo->mouse_face_window)
21748 && w == XWINDOW (dpyinfo->mouse_face_window)
21749 && (vpos > dpyinfo->mouse_face_beg_row
21750 || (vpos == dpyinfo->mouse_face_beg_row
21751 && hpos >= dpyinfo->mouse_face_beg_col))
21752 && (vpos < dpyinfo->mouse_face_end_row
21753 || (vpos == dpyinfo->mouse_face_end_row
21754 && hpos < dpyinfo->mouse_face_end_col))
21755 /* Don't redraw the cursor's spot in mouse face if it is at the
21756 end of a line (on a newline). The cursor appears there, but
21757 mouse highlighting does not. */
21758 && cursor_row->used[TEXT_AREA] > hpos)
21759 mouse_face_here_p = 1;
21761 /* Maybe clear the display under the cursor. */
21762 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21764 int x, y, left_x;
21765 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21766 int width;
21768 cursor_glyph = get_phys_cursor_glyph (w);
21769 if (cursor_glyph == NULL)
21770 goto mark_cursor_off;
21772 width = cursor_glyph->pixel_width;
21773 left_x = window_box_left_offset (w, TEXT_AREA);
21774 x = w->phys_cursor.x;
21775 if (x < left_x)
21776 width -= left_x - x;
21777 width = min (width, window_box_width (w, TEXT_AREA) - x);
21778 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21779 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21781 if (width > 0)
21782 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21785 /* Erase the cursor by redrawing the character underneath it. */
21786 if (mouse_face_here_p)
21787 hl = DRAW_MOUSE_FACE;
21788 else
21789 hl = DRAW_NORMAL_TEXT;
21790 draw_phys_cursor_glyph (w, cursor_row, hl);
21792 mark_cursor_off:
21793 w->phys_cursor_on_p = 0;
21794 w->phys_cursor_type = NO_CURSOR;
21798 /* EXPORT:
21799 Display or clear cursor of window W. If ON is zero, clear the
21800 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21801 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21803 void
21804 display_and_set_cursor (w, on, hpos, vpos, x, y)
21805 struct window *w;
21806 int on, hpos, vpos, x, y;
21808 struct frame *f = XFRAME (w->frame);
21809 int new_cursor_type;
21810 int new_cursor_width;
21811 int active_cursor;
21812 struct glyph_row *glyph_row;
21813 struct glyph *glyph;
21815 /* This is pointless on invisible frames, and dangerous on garbaged
21816 windows and frames; in the latter case, the frame or window may
21817 be in the midst of changing its size, and x and y may be off the
21818 window. */
21819 if (! FRAME_VISIBLE_P (f)
21820 || FRAME_GARBAGED_P (f)
21821 || vpos >= w->current_matrix->nrows
21822 || hpos >= w->current_matrix->matrix_w)
21823 return;
21825 /* If cursor is off and we want it off, return quickly. */
21826 if (!on && !w->phys_cursor_on_p)
21827 return;
21829 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21830 /* If cursor row is not enabled, we don't really know where to
21831 display the cursor. */
21832 if (!glyph_row->enabled_p)
21834 w->phys_cursor_on_p = 0;
21835 return;
21838 glyph = NULL;
21839 if (!glyph_row->exact_window_width_line_p
21840 || hpos < glyph_row->used[TEXT_AREA])
21841 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21843 xassert (interrupt_input_blocked);
21845 /* Set new_cursor_type to the cursor we want to be displayed. */
21846 new_cursor_type = get_window_cursor_type (w, glyph,
21847 &new_cursor_width, &active_cursor);
21849 /* If cursor is currently being shown and we don't want it to be or
21850 it is in the wrong place, or the cursor type is not what we want,
21851 erase it. */
21852 if (w->phys_cursor_on_p
21853 && (!on
21854 || w->phys_cursor.x != x
21855 || w->phys_cursor.y != y
21856 || new_cursor_type != w->phys_cursor_type
21857 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21858 && new_cursor_width != w->phys_cursor_width)))
21859 erase_phys_cursor (w);
21861 /* Don't check phys_cursor_on_p here because that flag is only set
21862 to zero in some cases where we know that the cursor has been
21863 completely erased, to avoid the extra work of erasing the cursor
21864 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21865 still not be visible, or it has only been partly erased. */
21866 if (on)
21868 w->phys_cursor_ascent = glyph_row->ascent;
21869 w->phys_cursor_height = glyph_row->height;
21871 /* Set phys_cursor_.* before x_draw_.* is called because some
21872 of them may need the information. */
21873 w->phys_cursor.x = x;
21874 w->phys_cursor.y = glyph_row->y;
21875 w->phys_cursor.hpos = hpos;
21876 w->phys_cursor.vpos = vpos;
21879 rif->draw_window_cursor (w, glyph_row, x, y,
21880 new_cursor_type, new_cursor_width,
21881 on, active_cursor);
21885 /* Switch the display of W's cursor on or off, according to the value
21886 of ON. */
21888 static void
21889 update_window_cursor (w, on)
21890 struct window *w;
21891 int on;
21893 /* Don't update cursor in windows whose frame is in the process
21894 of being deleted. */
21895 if (w->current_matrix)
21897 BLOCK_INPUT;
21898 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21899 w->phys_cursor.x, w->phys_cursor.y);
21900 UNBLOCK_INPUT;
21905 /* Call update_window_cursor with parameter ON_P on all leaf windows
21906 in the window tree rooted at W. */
21908 static void
21909 update_cursor_in_window_tree (w, on_p)
21910 struct window *w;
21911 int on_p;
21913 while (w)
21915 if (!NILP (w->hchild))
21916 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21917 else if (!NILP (w->vchild))
21918 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21919 else
21920 update_window_cursor (w, on_p);
21922 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21927 /* EXPORT:
21928 Display the cursor on window W, or clear it, according to ON_P.
21929 Don't change the cursor's position. */
21931 void
21932 x_update_cursor (f, on_p)
21933 struct frame *f;
21934 int on_p;
21936 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21940 /* EXPORT:
21941 Clear the cursor of window W to background color, and mark the
21942 cursor as not shown. This is used when the text where the cursor
21943 is is about to be rewritten. */
21945 void
21946 x_clear_cursor (w)
21947 struct window *w;
21949 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21950 update_window_cursor (w, 0);
21954 /* EXPORT:
21955 Display the active region described by mouse_face_* according to DRAW. */
21957 void
21958 show_mouse_face (dpyinfo, draw)
21959 Display_Info *dpyinfo;
21960 enum draw_glyphs_face draw;
21962 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21963 struct frame *f = XFRAME (WINDOW_FRAME (w));
21965 if (/* If window is in the process of being destroyed, don't bother
21966 to do anything. */
21967 w->current_matrix != NULL
21968 /* Don't update mouse highlight if hidden */
21969 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21970 /* Recognize when we are called to operate on rows that don't exist
21971 anymore. This can happen when a window is split. */
21972 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21974 int phys_cursor_on_p = w->phys_cursor_on_p;
21975 struct glyph_row *row, *first, *last;
21977 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21978 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21980 for (row = first; row <= last && row->enabled_p; ++row)
21982 int start_hpos, end_hpos, start_x;
21984 /* For all but the first row, the highlight starts at column 0. */
21985 if (row == first)
21987 start_hpos = dpyinfo->mouse_face_beg_col;
21988 start_x = dpyinfo->mouse_face_beg_x;
21990 else
21992 start_hpos = 0;
21993 start_x = 0;
21996 if (row == last)
21997 end_hpos = dpyinfo->mouse_face_end_col;
21998 else
22000 end_hpos = row->used[TEXT_AREA];
22001 if (draw == DRAW_NORMAL_TEXT)
22002 row->fill_line_p = 1; /* Clear to end of line */
22005 if (end_hpos > start_hpos)
22007 draw_glyphs (w, start_x, row, TEXT_AREA,
22008 start_hpos, end_hpos,
22009 draw, 0);
22011 row->mouse_face_p
22012 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22016 /* When we've written over the cursor, arrange for it to
22017 be displayed again. */
22018 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22020 BLOCK_INPUT;
22021 display_and_set_cursor (w, 1,
22022 w->phys_cursor.hpos, w->phys_cursor.vpos,
22023 w->phys_cursor.x, w->phys_cursor.y);
22024 UNBLOCK_INPUT;
22028 /* Change the mouse cursor. */
22029 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22030 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22031 else if (draw == DRAW_MOUSE_FACE)
22032 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22033 else
22034 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22037 /* EXPORT:
22038 Clear out the mouse-highlighted active region.
22039 Redraw it un-highlighted first. Value is non-zero if mouse
22040 face was actually drawn unhighlighted. */
22043 clear_mouse_face (dpyinfo)
22044 Display_Info *dpyinfo;
22046 int cleared = 0;
22048 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22050 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22051 cleared = 1;
22054 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22055 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22056 dpyinfo->mouse_face_window = Qnil;
22057 dpyinfo->mouse_face_overlay = Qnil;
22058 return cleared;
22062 /* EXPORT:
22063 Non-zero if physical cursor of window W is within mouse face. */
22066 cursor_in_mouse_face_p (w)
22067 struct window *w;
22069 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22070 int in_mouse_face = 0;
22072 if (WINDOWP (dpyinfo->mouse_face_window)
22073 && XWINDOW (dpyinfo->mouse_face_window) == w)
22075 int hpos = w->phys_cursor.hpos;
22076 int vpos = w->phys_cursor.vpos;
22078 if (vpos >= dpyinfo->mouse_face_beg_row
22079 && vpos <= dpyinfo->mouse_face_end_row
22080 && (vpos > dpyinfo->mouse_face_beg_row
22081 || hpos >= dpyinfo->mouse_face_beg_col)
22082 && (vpos < dpyinfo->mouse_face_end_row
22083 || hpos < dpyinfo->mouse_face_end_col
22084 || dpyinfo->mouse_face_past_end))
22085 in_mouse_face = 1;
22088 return in_mouse_face;
22094 /* Find the glyph matrix position of buffer position CHARPOS in window
22095 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22096 current glyphs must be up to date. If CHARPOS is above window
22097 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22098 of last line in W. In the row containing CHARPOS, stop before glyphs
22099 having STOP as object. */
22101 #if 1 /* This is a version of fast_find_position that's more correct
22102 in the presence of hscrolling, for example. I didn't install
22103 it right away because the problem fixed is minor, it failed
22104 in 20.x as well, and I think it's too risky to install
22105 so near the release of 21.1. 2001-09-25 gerd. */
22107 #ifndef HAVE_CARBON
22108 static
22109 #endif
22111 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22112 struct window *w;
22113 int charpos;
22114 int *hpos, *vpos, *x, *y;
22115 Lisp_Object stop;
22117 struct glyph_row *row, *first;
22118 struct glyph *glyph, *end;
22119 int past_end = 0;
22121 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22122 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22124 *x = first->x;
22125 *y = first->y;
22126 *hpos = 0;
22127 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22128 return 1;
22131 row = row_containing_pos (w, charpos, first, NULL, 0);
22132 if (row == NULL)
22134 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22135 past_end = 1;
22138 /* If whole rows or last part of a row came from a display overlay,
22139 row_containing_pos will skip over such rows because their end pos
22140 equals the start pos of the overlay or interval.
22142 Move back if we have a STOP object and previous row's
22143 end glyph came from STOP. */
22144 if (!NILP (stop))
22146 struct glyph_row *prev;
22147 while ((prev = row - 1, prev >= first)
22148 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22149 && prev->used[TEXT_AREA] > 0)
22151 struct glyph *beg = prev->glyphs[TEXT_AREA];
22152 glyph = beg + prev->used[TEXT_AREA];
22153 while (--glyph >= beg
22154 && INTEGERP (glyph->object));
22155 if (glyph < beg
22156 || !EQ (stop, glyph->object))
22157 break;
22158 row = prev;
22162 *x = row->x;
22163 *y = row->y;
22164 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22166 glyph = row->glyphs[TEXT_AREA];
22167 end = glyph + row->used[TEXT_AREA];
22169 /* Skip over glyphs not having an object at the start of the row.
22170 These are special glyphs like truncation marks on terminal
22171 frames. */
22172 if (row->displays_text_p)
22173 while (glyph < end
22174 && INTEGERP (glyph->object)
22175 && !EQ (stop, glyph->object)
22176 && glyph->charpos < 0)
22178 *x += glyph->pixel_width;
22179 ++glyph;
22182 while (glyph < end
22183 && !INTEGERP (glyph->object)
22184 && !EQ (stop, glyph->object)
22185 && (!BUFFERP (glyph->object)
22186 || glyph->charpos < charpos))
22188 *x += glyph->pixel_width;
22189 ++glyph;
22192 *hpos = glyph - row->glyphs[TEXT_AREA];
22193 return !past_end;
22196 #else /* not 1 */
22198 static int
22199 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22200 struct window *w;
22201 int pos;
22202 int *hpos, *vpos, *x, *y;
22203 Lisp_Object stop;
22205 int i;
22206 int lastcol;
22207 int maybe_next_line_p = 0;
22208 int line_start_position;
22209 int yb = window_text_bottom_y (w);
22210 struct glyph_row *row, *best_row;
22211 int row_vpos, best_row_vpos;
22212 int current_x;
22214 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22215 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22217 while (row->y < yb)
22219 if (row->used[TEXT_AREA])
22220 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22221 else
22222 line_start_position = 0;
22224 if (line_start_position > pos)
22225 break;
22226 /* If the position sought is the end of the buffer,
22227 don't include the blank lines at the bottom of the window. */
22228 else if (line_start_position == pos
22229 && pos == BUF_ZV (XBUFFER (w->buffer)))
22231 maybe_next_line_p = 1;
22232 break;
22234 else if (line_start_position > 0)
22236 best_row = row;
22237 best_row_vpos = row_vpos;
22240 if (row->y + row->height >= yb)
22241 break;
22243 ++row;
22244 ++row_vpos;
22247 /* Find the right column within BEST_ROW. */
22248 lastcol = 0;
22249 current_x = best_row->x;
22250 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22252 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22253 int charpos = glyph->charpos;
22255 if (BUFFERP (glyph->object))
22257 if (charpos == pos)
22259 *hpos = i;
22260 *vpos = best_row_vpos;
22261 *x = current_x;
22262 *y = best_row->y;
22263 return 1;
22265 else if (charpos > pos)
22266 break;
22268 else if (EQ (glyph->object, stop))
22269 break;
22271 if (charpos > 0)
22272 lastcol = i;
22273 current_x += glyph->pixel_width;
22276 /* If we're looking for the end of the buffer,
22277 and we didn't find it in the line we scanned,
22278 use the start of the following line. */
22279 if (maybe_next_line_p)
22281 ++best_row;
22282 ++best_row_vpos;
22283 lastcol = 0;
22284 current_x = best_row->x;
22287 *vpos = best_row_vpos;
22288 *hpos = lastcol + 1;
22289 *x = current_x;
22290 *y = best_row->y;
22291 return 0;
22294 #endif /* not 1 */
22297 /* Find the position of the glyph for position POS in OBJECT in
22298 window W's current matrix, and return in *X, *Y the pixel
22299 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22301 RIGHT_P non-zero means return the position of the right edge of the
22302 glyph, RIGHT_P zero means return the left edge position.
22304 If no glyph for POS exists in the matrix, return the position of
22305 the glyph with the next smaller position that is in the matrix, if
22306 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22307 exists in the matrix, return the position of the glyph with the
22308 next larger position in OBJECT.
22310 Value is non-zero if a glyph was found. */
22312 static int
22313 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22314 struct window *w;
22315 int pos;
22316 Lisp_Object object;
22317 int *hpos, *vpos, *x, *y;
22318 int right_p;
22320 int yb = window_text_bottom_y (w);
22321 struct glyph_row *r;
22322 struct glyph *best_glyph = NULL;
22323 struct glyph_row *best_row = NULL;
22324 int best_x = 0;
22326 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22327 r->enabled_p && r->y < yb;
22328 ++r)
22330 struct glyph *g = r->glyphs[TEXT_AREA];
22331 struct glyph *e = g + r->used[TEXT_AREA];
22332 int gx;
22334 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22335 if (EQ (g->object, object))
22337 if (g->charpos == pos)
22339 best_glyph = g;
22340 best_x = gx;
22341 best_row = r;
22342 goto found;
22344 else if (best_glyph == NULL
22345 || ((abs (g->charpos - pos)
22346 < abs (best_glyph->charpos - pos))
22347 && (right_p
22348 ? g->charpos < pos
22349 : g->charpos > pos)))
22351 best_glyph = g;
22352 best_x = gx;
22353 best_row = r;
22358 found:
22360 if (best_glyph)
22362 *x = best_x;
22363 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22365 if (right_p)
22367 *x += best_glyph->pixel_width;
22368 ++*hpos;
22371 *y = best_row->y;
22372 *vpos = best_row - w->current_matrix->rows;
22375 return best_glyph != NULL;
22379 /* See if position X, Y is within a hot-spot of an image. */
22381 static int
22382 on_hot_spot_p (hot_spot, x, y)
22383 Lisp_Object hot_spot;
22384 int x, y;
22386 if (!CONSP (hot_spot))
22387 return 0;
22389 if (EQ (XCAR (hot_spot), Qrect))
22391 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22392 Lisp_Object rect = XCDR (hot_spot);
22393 Lisp_Object tem;
22394 if (!CONSP (rect))
22395 return 0;
22396 if (!CONSP (XCAR (rect)))
22397 return 0;
22398 if (!CONSP (XCDR (rect)))
22399 return 0;
22400 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22401 return 0;
22402 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22403 return 0;
22404 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22405 return 0;
22406 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22407 return 0;
22408 return 1;
22410 else if (EQ (XCAR (hot_spot), Qcircle))
22412 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22413 Lisp_Object circ = XCDR (hot_spot);
22414 Lisp_Object lr, lx0, ly0;
22415 if (CONSP (circ)
22416 && CONSP (XCAR (circ))
22417 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22418 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22419 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22421 double r = XFLOATINT (lr);
22422 double dx = XINT (lx0) - x;
22423 double dy = XINT (ly0) - y;
22424 return (dx * dx + dy * dy <= r * r);
22427 else if (EQ (XCAR (hot_spot), Qpoly))
22429 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22430 if (VECTORP (XCDR (hot_spot)))
22432 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22433 Lisp_Object *poly = v->contents;
22434 int n = v->size;
22435 int i;
22436 int inside = 0;
22437 Lisp_Object lx, ly;
22438 int x0, y0;
22440 /* Need an even number of coordinates, and at least 3 edges. */
22441 if (n < 6 || n & 1)
22442 return 0;
22444 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22445 If count is odd, we are inside polygon. Pixels on edges
22446 may or may not be included depending on actual geometry of the
22447 polygon. */
22448 if ((lx = poly[n-2], !INTEGERP (lx))
22449 || (ly = poly[n-1], !INTEGERP (lx)))
22450 return 0;
22451 x0 = XINT (lx), y0 = XINT (ly);
22452 for (i = 0; i < n; i += 2)
22454 int x1 = x0, y1 = y0;
22455 if ((lx = poly[i], !INTEGERP (lx))
22456 || (ly = poly[i+1], !INTEGERP (ly)))
22457 return 0;
22458 x0 = XINT (lx), y0 = XINT (ly);
22460 /* Does this segment cross the X line? */
22461 if (x0 >= x)
22463 if (x1 >= x)
22464 continue;
22466 else if (x1 < x)
22467 continue;
22468 if (y > y0 && y > y1)
22469 continue;
22470 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22471 inside = !inside;
22473 return inside;
22476 /* If we don't understand the format, pretend we're not in the hot-spot. */
22477 return 0;
22480 Lisp_Object
22481 find_hot_spot (map, x, y)
22482 Lisp_Object map;
22483 int x, y;
22485 while (CONSP (map))
22487 if (CONSP (XCAR (map))
22488 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22489 return XCAR (map);
22490 map = XCDR (map);
22493 return Qnil;
22496 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22497 3, 3, 0,
22498 doc: /* Lookup in image map MAP coordinates X and Y.
22499 An image map is an alist where each element has the format (AREA ID PLIST).
22500 An AREA is specified as either a rectangle, a circle, or a polygon:
22501 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22502 pixel coordinates of the upper left and bottom right corners.
22503 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22504 and the radius of the circle; r may be a float or integer.
22505 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22506 vector describes one corner in the polygon.
22507 Returns the alist element for the first matching AREA in MAP. */)
22508 (map, x, y)
22509 Lisp_Object map;
22510 Lisp_Object x, y;
22512 if (NILP (map))
22513 return Qnil;
22515 CHECK_NUMBER (x);
22516 CHECK_NUMBER (y);
22518 return find_hot_spot (map, XINT (x), XINT (y));
22522 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22523 static void
22524 define_frame_cursor1 (f, cursor, pointer)
22525 struct frame *f;
22526 Cursor cursor;
22527 Lisp_Object pointer;
22529 /* Do not change cursor shape while dragging mouse. */
22530 if (!NILP (do_mouse_tracking))
22531 return;
22533 if (!NILP (pointer))
22535 if (EQ (pointer, Qarrow))
22536 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22537 else if (EQ (pointer, Qhand))
22538 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22539 else if (EQ (pointer, Qtext))
22540 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22541 else if (EQ (pointer, intern ("hdrag")))
22542 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22543 #ifdef HAVE_X_WINDOWS
22544 else if (EQ (pointer, intern ("vdrag")))
22545 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22546 #endif
22547 else if (EQ (pointer, intern ("hourglass")))
22548 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22549 else if (EQ (pointer, Qmodeline))
22550 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22551 else
22552 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22555 if (cursor != No_Cursor)
22556 rif->define_frame_cursor (f, cursor);
22559 /* Take proper action when mouse has moved to the mode or header line
22560 or marginal area AREA of window W, x-position X and y-position Y.
22561 X is relative to the start of the text display area of W, so the
22562 width of bitmap areas and scroll bars must be subtracted to get a
22563 position relative to the start of the mode line. */
22565 static void
22566 note_mode_line_or_margin_highlight (window, x, y, area)
22567 Lisp_Object window;
22568 int x, y;
22569 enum window_part area;
22571 struct window *w = XWINDOW (window);
22572 struct frame *f = XFRAME (w->frame);
22573 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22574 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22575 Lisp_Object pointer = Qnil;
22576 int charpos, dx, dy, width, height;
22577 Lisp_Object string, object = Qnil;
22578 Lisp_Object pos, help;
22580 Lisp_Object mouse_face;
22581 int original_x_pixel = x;
22582 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22583 struct glyph_row *row;
22585 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22587 int x0;
22588 struct glyph *end;
22590 string = mode_line_string (w, area, &x, &y, &charpos,
22591 &object, &dx, &dy, &width, &height);
22593 row = (area == ON_MODE_LINE
22594 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22595 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22597 /* Find glyph */
22598 if (row->mode_line_p && row->enabled_p)
22600 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22601 end = glyph + row->used[TEXT_AREA];
22603 for (x0 = original_x_pixel;
22604 glyph < end && x0 >= glyph->pixel_width;
22605 ++glyph)
22606 x0 -= glyph->pixel_width;
22608 if (glyph >= end)
22609 glyph = NULL;
22612 else
22614 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22615 string = marginal_area_string (w, area, &x, &y, &charpos,
22616 &object, &dx, &dy, &width, &height);
22619 help = Qnil;
22621 if (IMAGEP (object))
22623 Lisp_Object image_map, hotspot;
22624 if ((image_map = Fplist_get (XCDR (object), QCmap),
22625 !NILP (image_map))
22626 && (hotspot = find_hot_spot (image_map, dx, dy),
22627 CONSP (hotspot))
22628 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22630 Lisp_Object area_id, plist;
22632 area_id = XCAR (hotspot);
22633 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22634 If so, we could look for mouse-enter, mouse-leave
22635 properties in PLIST (and do something...). */
22636 hotspot = XCDR (hotspot);
22637 if (CONSP (hotspot)
22638 && (plist = XCAR (hotspot), CONSP (plist)))
22640 pointer = Fplist_get (plist, Qpointer);
22641 if (NILP (pointer))
22642 pointer = Qhand;
22643 help = Fplist_get (plist, Qhelp_echo);
22644 if (!NILP (help))
22646 help_echo_string = help;
22647 /* Is this correct? ++kfs */
22648 XSETWINDOW (help_echo_window, w);
22649 help_echo_object = w->buffer;
22650 help_echo_pos = charpos;
22654 if (NILP (pointer))
22655 pointer = Fplist_get (XCDR (object), QCpointer);
22658 if (STRINGP (string))
22660 pos = make_number (charpos);
22661 /* If we're on a string with `help-echo' text property, arrange
22662 for the help to be displayed. This is done by setting the
22663 global variable help_echo_string to the help string. */
22664 if (NILP (help))
22666 help = Fget_text_property (pos, Qhelp_echo, string);
22667 if (!NILP (help))
22669 help_echo_string = help;
22670 XSETWINDOW (help_echo_window, w);
22671 help_echo_object = string;
22672 help_echo_pos = charpos;
22676 if (NILP (pointer))
22677 pointer = Fget_text_property (pos, Qpointer, string);
22679 /* Change the mouse pointer according to what is under X/Y. */
22680 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22682 Lisp_Object map;
22683 map = Fget_text_property (pos, Qlocal_map, string);
22684 if (!KEYMAPP (map))
22685 map = Fget_text_property (pos, Qkeymap, string);
22686 if (!KEYMAPP (map))
22687 cursor = dpyinfo->vertical_scroll_bar_cursor;
22690 /* Change the mouse face according to what is under X/Y. */
22691 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22692 if (!NILP (mouse_face)
22693 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22694 && glyph)
22696 Lisp_Object b, e;
22698 struct glyph * tmp_glyph;
22700 int gpos;
22701 int gseq_length;
22702 int total_pixel_width;
22703 int ignore;
22705 int vpos, hpos;
22707 b = Fprevious_single_property_change (make_number (charpos + 1),
22708 Qmouse_face, string, Qnil);
22709 if (NILP (b))
22710 b = make_number (0);
22712 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22713 if (NILP (e))
22714 e = make_number (SCHARS (string));
22716 /* Calculate the position(glyph position: GPOS) of GLYPH in
22717 displayed string. GPOS is different from CHARPOS.
22719 CHARPOS is the position of glyph in internal string
22720 object. A mode line string format has structures which
22721 is converted to a flatten by emacs lisp interpreter.
22722 The internal string is an element of the structures.
22723 The displayed string is the flatten string. */
22724 gpos = 0;
22725 if (glyph > row_start_glyph)
22727 tmp_glyph = glyph - 1;
22728 while (tmp_glyph >= row_start_glyph
22729 && tmp_glyph->charpos >= XINT (b)
22730 && EQ (tmp_glyph->object, glyph->object))
22732 tmp_glyph--;
22733 gpos++;
22737 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22738 displayed string holding GLYPH.
22740 GSEQ_LENGTH is different from SCHARS (STRING).
22741 SCHARS (STRING) returns the length of the internal string. */
22742 for (tmp_glyph = glyph, gseq_length = gpos;
22743 tmp_glyph->charpos < XINT (e);
22744 tmp_glyph++, gseq_length++)
22746 if (!EQ (tmp_glyph->object, glyph->object))
22747 break;
22750 total_pixel_width = 0;
22751 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22752 total_pixel_width += tmp_glyph->pixel_width;
22754 /* Pre calculation of re-rendering position */
22755 vpos = (x - gpos);
22756 hpos = (area == ON_MODE_LINE
22757 ? (w->current_matrix)->nrows - 1
22758 : 0);
22760 /* If the re-rendering position is included in the last
22761 re-rendering area, we should do nothing. */
22762 if ( EQ (window, dpyinfo->mouse_face_window)
22763 && dpyinfo->mouse_face_beg_col <= vpos
22764 && vpos < dpyinfo->mouse_face_end_col
22765 && dpyinfo->mouse_face_beg_row == hpos )
22766 return;
22768 if (clear_mouse_face (dpyinfo))
22769 cursor = No_Cursor;
22771 dpyinfo->mouse_face_beg_col = vpos;
22772 dpyinfo->mouse_face_beg_row = hpos;
22774 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22775 dpyinfo->mouse_face_beg_y = 0;
22777 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22778 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22780 dpyinfo->mouse_face_end_x = 0;
22781 dpyinfo->mouse_face_end_y = 0;
22783 dpyinfo->mouse_face_past_end = 0;
22784 dpyinfo->mouse_face_window = window;
22786 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22787 charpos,
22788 0, 0, 0, &ignore,
22789 glyph->face_id, 1);
22790 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22792 if (NILP (pointer))
22793 pointer = Qhand;
22795 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22796 clear_mouse_face (dpyinfo);
22798 define_frame_cursor1 (f, cursor, pointer);
22802 /* EXPORT:
22803 Take proper action when the mouse has moved to position X, Y on
22804 frame F as regards highlighting characters that have mouse-face
22805 properties. Also de-highlighting chars where the mouse was before.
22806 X and Y can be negative or out of range. */
22808 void
22809 note_mouse_highlight (f, x, y)
22810 struct frame *f;
22811 int x, y;
22813 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22814 enum window_part part;
22815 Lisp_Object window;
22816 struct window *w;
22817 Cursor cursor = No_Cursor;
22818 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22819 struct buffer *b;
22821 /* When a menu is active, don't highlight because this looks odd. */
22822 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22823 if (popup_activated ())
22824 return;
22825 #endif
22827 if (NILP (Vmouse_highlight)
22828 || !f->glyphs_initialized_p)
22829 return;
22831 dpyinfo->mouse_face_mouse_x = x;
22832 dpyinfo->mouse_face_mouse_y = y;
22833 dpyinfo->mouse_face_mouse_frame = f;
22835 if (dpyinfo->mouse_face_defer)
22836 return;
22838 if (gc_in_progress)
22840 dpyinfo->mouse_face_deferred_gc = 1;
22841 return;
22844 /* Which window is that in? */
22845 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22847 /* If we were displaying active text in another window, clear that.
22848 Also clear if we move out of text area in same window. */
22849 if (! EQ (window, dpyinfo->mouse_face_window)
22850 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22851 && !NILP (dpyinfo->mouse_face_window)))
22852 clear_mouse_face (dpyinfo);
22854 /* Not on a window -> return. */
22855 if (!WINDOWP (window))
22856 return;
22858 /* Reset help_echo_string. It will get recomputed below. */
22859 help_echo_string = Qnil;
22861 /* Convert to window-relative pixel coordinates. */
22862 w = XWINDOW (window);
22863 frame_to_window_pixel_xy (w, &x, &y);
22865 /* Handle tool-bar window differently since it doesn't display a
22866 buffer. */
22867 if (EQ (window, f->tool_bar_window))
22869 note_tool_bar_highlight (f, x, y);
22870 return;
22873 /* Mouse is on the mode, header line or margin? */
22874 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22875 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22877 note_mode_line_or_margin_highlight (window, x, y, part);
22878 return;
22881 if (part == ON_VERTICAL_BORDER)
22883 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22884 help_echo_string = build_string ("drag-mouse-1: resize");
22886 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22887 || part == ON_SCROLL_BAR)
22888 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22889 else
22890 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22892 /* Are we in a window whose display is up to date?
22893 And verify the buffer's text has not changed. */
22894 b = XBUFFER (w->buffer);
22895 if (part == ON_TEXT
22896 && EQ (w->window_end_valid, w->buffer)
22897 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22898 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22900 int hpos, vpos, pos, i, dx, dy, area;
22901 struct glyph *glyph;
22902 Lisp_Object object;
22903 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22904 Lisp_Object *overlay_vec = NULL;
22905 int noverlays;
22906 struct buffer *obuf;
22907 int obegv, ozv, same_region;
22909 /* Find the glyph under X/Y. */
22910 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22912 /* Look for :pointer property on image. */
22913 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22915 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22916 if (img != NULL && IMAGEP (img->spec))
22918 Lisp_Object image_map, hotspot;
22919 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22920 !NILP (image_map))
22921 && (hotspot = find_hot_spot (image_map,
22922 glyph->slice.x + dx,
22923 glyph->slice.y + dy),
22924 CONSP (hotspot))
22925 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22927 Lisp_Object area_id, plist;
22929 area_id = XCAR (hotspot);
22930 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22931 If so, we could look for mouse-enter, mouse-leave
22932 properties in PLIST (and do something...). */
22933 hotspot = XCDR (hotspot);
22934 if (CONSP (hotspot)
22935 && (plist = XCAR (hotspot), CONSP (plist)))
22937 pointer = Fplist_get (plist, Qpointer);
22938 if (NILP (pointer))
22939 pointer = Qhand;
22940 help_echo_string = Fplist_get (plist, Qhelp_echo);
22941 if (!NILP (help_echo_string))
22943 help_echo_window = window;
22944 help_echo_object = glyph->object;
22945 help_echo_pos = glyph->charpos;
22949 if (NILP (pointer))
22950 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22954 /* Clear mouse face if X/Y not over text. */
22955 if (glyph == NULL
22956 || area != TEXT_AREA
22957 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22959 if (clear_mouse_face (dpyinfo))
22960 cursor = No_Cursor;
22961 if (NILP (pointer))
22963 if (area != TEXT_AREA)
22964 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22965 else
22966 pointer = Vvoid_text_area_pointer;
22968 goto set_cursor;
22971 pos = glyph->charpos;
22972 object = glyph->object;
22973 if (!STRINGP (object) && !BUFFERP (object))
22974 goto set_cursor;
22976 /* If we get an out-of-range value, return now; avoid an error. */
22977 if (BUFFERP (object) && pos > BUF_Z (b))
22978 goto set_cursor;
22980 /* Make the window's buffer temporarily current for
22981 overlays_at and compute_char_face. */
22982 obuf = current_buffer;
22983 current_buffer = b;
22984 obegv = BEGV;
22985 ozv = ZV;
22986 BEGV = BEG;
22987 ZV = Z;
22989 /* Is this char mouse-active or does it have help-echo? */
22990 position = make_number (pos);
22992 if (BUFFERP (object))
22994 /* Put all the overlays we want in a vector in overlay_vec. */
22995 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22996 /* Sort overlays into increasing priority order. */
22997 noverlays = sort_overlays (overlay_vec, noverlays, w);
22999 else
23000 noverlays = 0;
23002 same_region = (EQ (window, dpyinfo->mouse_face_window)
23003 && vpos >= dpyinfo->mouse_face_beg_row
23004 && vpos <= dpyinfo->mouse_face_end_row
23005 && (vpos > dpyinfo->mouse_face_beg_row
23006 || hpos >= dpyinfo->mouse_face_beg_col)
23007 && (vpos < dpyinfo->mouse_face_end_row
23008 || hpos < dpyinfo->mouse_face_end_col
23009 || dpyinfo->mouse_face_past_end));
23011 if (same_region)
23012 cursor = No_Cursor;
23014 /* Check mouse-face highlighting. */
23015 if (! same_region
23016 /* If there exists an overlay with mouse-face overlapping
23017 the one we are currently highlighting, we have to
23018 check if we enter the overlapping overlay, and then
23019 highlight only that. */
23020 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23021 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23023 /* Find the highest priority overlay that has a mouse-face
23024 property. */
23025 overlay = Qnil;
23026 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23028 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23029 if (!NILP (mouse_face))
23030 overlay = overlay_vec[i];
23033 /* If we're actually highlighting the same overlay as
23034 before, there's no need to do that again. */
23035 if (!NILP (overlay)
23036 && EQ (overlay, dpyinfo->mouse_face_overlay))
23037 goto check_help_echo;
23039 dpyinfo->mouse_face_overlay = overlay;
23041 /* Clear the display of the old active region, if any. */
23042 if (clear_mouse_face (dpyinfo))
23043 cursor = No_Cursor;
23045 /* If no overlay applies, get a text property. */
23046 if (NILP (overlay))
23047 mouse_face = Fget_text_property (position, Qmouse_face, object);
23049 /* Handle the overlay case. */
23050 if (!NILP (overlay))
23052 /* Find the range of text around this char that
23053 should be active. */
23054 Lisp_Object before, after;
23055 int ignore;
23057 before = Foverlay_start (overlay);
23058 after = Foverlay_end (overlay);
23059 /* Record this as the current active region. */
23060 fast_find_position (w, XFASTINT (before),
23061 &dpyinfo->mouse_face_beg_col,
23062 &dpyinfo->mouse_face_beg_row,
23063 &dpyinfo->mouse_face_beg_x,
23064 &dpyinfo->mouse_face_beg_y, Qnil);
23066 dpyinfo->mouse_face_past_end
23067 = !fast_find_position (w, XFASTINT (after),
23068 &dpyinfo->mouse_face_end_col,
23069 &dpyinfo->mouse_face_end_row,
23070 &dpyinfo->mouse_face_end_x,
23071 &dpyinfo->mouse_face_end_y, Qnil);
23072 dpyinfo->mouse_face_window = window;
23074 dpyinfo->mouse_face_face_id
23075 = face_at_buffer_position (w, pos, 0, 0,
23076 &ignore, pos + 1,
23077 !dpyinfo->mouse_face_hidden);
23079 /* Display it as active. */
23080 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23081 cursor = No_Cursor;
23083 /* Handle the text property case. */
23084 else if (!NILP (mouse_face) && BUFFERP (object))
23086 /* Find the range of text around this char that
23087 should be active. */
23088 Lisp_Object before, after, beginning, end;
23089 int ignore;
23091 beginning = Fmarker_position (w->start);
23092 end = make_number (BUF_Z (XBUFFER (object))
23093 - XFASTINT (w->window_end_pos));
23094 before
23095 = Fprevious_single_property_change (make_number (pos + 1),
23096 Qmouse_face,
23097 object, beginning);
23098 after
23099 = Fnext_single_property_change (position, Qmouse_face,
23100 object, end);
23102 /* Record this as the current active region. */
23103 fast_find_position (w, XFASTINT (before),
23104 &dpyinfo->mouse_face_beg_col,
23105 &dpyinfo->mouse_face_beg_row,
23106 &dpyinfo->mouse_face_beg_x,
23107 &dpyinfo->mouse_face_beg_y, Qnil);
23108 dpyinfo->mouse_face_past_end
23109 = !fast_find_position (w, XFASTINT (after),
23110 &dpyinfo->mouse_face_end_col,
23111 &dpyinfo->mouse_face_end_row,
23112 &dpyinfo->mouse_face_end_x,
23113 &dpyinfo->mouse_face_end_y, Qnil);
23114 dpyinfo->mouse_face_window = window;
23116 if (BUFFERP (object))
23117 dpyinfo->mouse_face_face_id
23118 = face_at_buffer_position (w, pos, 0, 0,
23119 &ignore, pos + 1,
23120 !dpyinfo->mouse_face_hidden);
23122 /* Display it as active. */
23123 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23124 cursor = No_Cursor;
23126 else if (!NILP (mouse_face) && STRINGP (object))
23128 Lisp_Object b, e;
23129 int ignore;
23131 b = Fprevious_single_property_change (make_number (pos + 1),
23132 Qmouse_face,
23133 object, Qnil);
23134 e = Fnext_single_property_change (position, Qmouse_face,
23135 object, Qnil);
23136 if (NILP (b))
23137 b = make_number (0);
23138 if (NILP (e))
23139 e = make_number (SCHARS (object) - 1);
23141 fast_find_string_pos (w, XINT (b), object,
23142 &dpyinfo->mouse_face_beg_col,
23143 &dpyinfo->mouse_face_beg_row,
23144 &dpyinfo->mouse_face_beg_x,
23145 &dpyinfo->mouse_face_beg_y, 0);
23146 fast_find_string_pos (w, XINT (e), object,
23147 &dpyinfo->mouse_face_end_col,
23148 &dpyinfo->mouse_face_end_row,
23149 &dpyinfo->mouse_face_end_x,
23150 &dpyinfo->mouse_face_end_y, 1);
23151 dpyinfo->mouse_face_past_end = 0;
23152 dpyinfo->mouse_face_window = window;
23153 dpyinfo->mouse_face_face_id
23154 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23155 glyph->face_id, 1);
23156 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23157 cursor = No_Cursor;
23159 else if (STRINGP (object) && NILP (mouse_face))
23161 /* A string which doesn't have mouse-face, but
23162 the text ``under'' it might have. */
23163 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23164 int start = MATRIX_ROW_START_CHARPOS (r);
23166 pos = string_buffer_position (w, object, start);
23167 if (pos > 0)
23168 mouse_face = get_char_property_and_overlay (make_number (pos),
23169 Qmouse_face,
23170 w->buffer,
23171 &overlay);
23172 if (!NILP (mouse_face) && !NILP (overlay))
23174 Lisp_Object before = Foverlay_start (overlay);
23175 Lisp_Object after = Foverlay_end (overlay);
23176 int ignore;
23178 /* Note that we might not be able to find position
23179 BEFORE in the glyph matrix if the overlay is
23180 entirely covered by a `display' property. In
23181 this case, we overshoot. So let's stop in
23182 the glyph matrix before glyphs for OBJECT. */
23183 fast_find_position (w, XFASTINT (before),
23184 &dpyinfo->mouse_face_beg_col,
23185 &dpyinfo->mouse_face_beg_row,
23186 &dpyinfo->mouse_face_beg_x,
23187 &dpyinfo->mouse_face_beg_y,
23188 object);
23190 dpyinfo->mouse_face_past_end
23191 = !fast_find_position (w, XFASTINT (after),
23192 &dpyinfo->mouse_face_end_col,
23193 &dpyinfo->mouse_face_end_row,
23194 &dpyinfo->mouse_face_end_x,
23195 &dpyinfo->mouse_face_end_y,
23196 Qnil);
23197 dpyinfo->mouse_face_window = window;
23198 dpyinfo->mouse_face_face_id
23199 = face_at_buffer_position (w, pos, 0, 0,
23200 &ignore, pos + 1,
23201 !dpyinfo->mouse_face_hidden);
23203 /* Display it as active. */
23204 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23205 cursor = No_Cursor;
23210 check_help_echo:
23212 /* Look for a `help-echo' property. */
23213 if (NILP (help_echo_string)) {
23214 Lisp_Object help, overlay;
23216 /* Check overlays first. */
23217 help = overlay = Qnil;
23218 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23220 overlay = overlay_vec[i];
23221 help = Foverlay_get (overlay, Qhelp_echo);
23224 if (!NILP (help))
23226 help_echo_string = help;
23227 help_echo_window = window;
23228 help_echo_object = overlay;
23229 help_echo_pos = pos;
23231 else
23233 Lisp_Object object = glyph->object;
23234 int charpos = glyph->charpos;
23236 /* Try text properties. */
23237 if (STRINGP (object)
23238 && charpos >= 0
23239 && charpos < SCHARS (object))
23241 help = Fget_text_property (make_number (charpos),
23242 Qhelp_echo, object);
23243 if (NILP (help))
23245 /* If the string itself doesn't specify a help-echo,
23246 see if the buffer text ``under'' it does. */
23247 struct glyph_row *r
23248 = MATRIX_ROW (w->current_matrix, vpos);
23249 int start = MATRIX_ROW_START_CHARPOS (r);
23250 int pos = string_buffer_position (w, object, start);
23251 if (pos > 0)
23253 help = Fget_char_property (make_number (pos),
23254 Qhelp_echo, w->buffer);
23255 if (!NILP (help))
23257 charpos = pos;
23258 object = w->buffer;
23263 else if (BUFFERP (object)
23264 && charpos >= BEGV
23265 && charpos < ZV)
23266 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23267 object);
23269 if (!NILP (help))
23271 help_echo_string = help;
23272 help_echo_window = window;
23273 help_echo_object = object;
23274 help_echo_pos = charpos;
23279 /* Look for a `pointer' property. */
23280 if (NILP (pointer))
23282 /* Check overlays first. */
23283 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23284 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23286 if (NILP (pointer))
23288 Lisp_Object object = glyph->object;
23289 int charpos = glyph->charpos;
23291 /* Try text properties. */
23292 if (STRINGP (object)
23293 && charpos >= 0
23294 && charpos < SCHARS (object))
23296 pointer = Fget_text_property (make_number (charpos),
23297 Qpointer, object);
23298 if (NILP (pointer))
23300 /* If the string itself doesn't specify a pointer,
23301 see if the buffer text ``under'' it does. */
23302 struct glyph_row *r
23303 = MATRIX_ROW (w->current_matrix, vpos);
23304 int start = MATRIX_ROW_START_CHARPOS (r);
23305 int pos = string_buffer_position (w, object, start);
23306 if (pos > 0)
23307 pointer = Fget_char_property (make_number (pos),
23308 Qpointer, w->buffer);
23311 else if (BUFFERP (object)
23312 && charpos >= BEGV
23313 && charpos < ZV)
23314 pointer = Fget_text_property (make_number (charpos),
23315 Qpointer, object);
23319 BEGV = obegv;
23320 ZV = ozv;
23321 current_buffer = obuf;
23324 set_cursor:
23326 define_frame_cursor1 (f, cursor, pointer);
23330 /* EXPORT for RIF:
23331 Clear any mouse-face on window W. This function is part of the
23332 redisplay interface, and is called from try_window_id and similar
23333 functions to ensure the mouse-highlight is off. */
23335 void
23336 x_clear_window_mouse_face (w)
23337 struct window *w;
23339 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23340 Lisp_Object window;
23342 BLOCK_INPUT;
23343 XSETWINDOW (window, w);
23344 if (EQ (window, dpyinfo->mouse_face_window))
23345 clear_mouse_face (dpyinfo);
23346 UNBLOCK_INPUT;
23350 /* EXPORT:
23351 Just discard the mouse face information for frame F, if any.
23352 This is used when the size of F is changed. */
23354 void
23355 cancel_mouse_face (f)
23356 struct frame *f;
23358 Lisp_Object window;
23359 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23361 window = dpyinfo->mouse_face_window;
23362 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23364 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23365 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23366 dpyinfo->mouse_face_window = Qnil;
23371 #endif /* HAVE_WINDOW_SYSTEM */
23374 /***********************************************************************
23375 Exposure Events
23376 ***********************************************************************/
23378 #ifdef HAVE_WINDOW_SYSTEM
23380 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23381 which intersects rectangle R. R is in window-relative coordinates. */
23383 static void
23384 expose_area (w, row, r, area)
23385 struct window *w;
23386 struct glyph_row *row;
23387 XRectangle *r;
23388 enum glyph_row_area area;
23390 struct glyph *first = row->glyphs[area];
23391 struct glyph *end = row->glyphs[area] + row->used[area];
23392 struct glyph *last;
23393 int first_x, start_x, x;
23395 if (area == TEXT_AREA && row->fill_line_p)
23396 /* If row extends face to end of line write the whole line. */
23397 draw_glyphs (w, 0, row, area,
23398 0, row->used[area],
23399 DRAW_NORMAL_TEXT, 0);
23400 else
23402 /* Set START_X to the window-relative start position for drawing glyphs of
23403 AREA. The first glyph of the text area can be partially visible.
23404 The first glyphs of other areas cannot. */
23405 start_x = window_box_left_offset (w, area);
23406 x = start_x;
23407 if (area == TEXT_AREA)
23408 x += row->x;
23410 /* Find the first glyph that must be redrawn. */
23411 while (first < end
23412 && x + first->pixel_width < r->x)
23414 x += first->pixel_width;
23415 ++first;
23418 /* Find the last one. */
23419 last = first;
23420 first_x = x;
23421 while (last < end
23422 && x < r->x + r->width)
23424 x += last->pixel_width;
23425 ++last;
23428 /* Repaint. */
23429 if (last > first)
23430 draw_glyphs (w, first_x - start_x, row, area,
23431 first - row->glyphs[area], last - row->glyphs[area],
23432 DRAW_NORMAL_TEXT, 0);
23437 /* Redraw the parts of the glyph row ROW on window W intersecting
23438 rectangle R. R is in window-relative coordinates. Value is
23439 non-zero if mouse-face was overwritten. */
23441 static int
23442 expose_line (w, row, r)
23443 struct window *w;
23444 struct glyph_row *row;
23445 XRectangle *r;
23447 xassert (row->enabled_p);
23449 if (row->mode_line_p || w->pseudo_window_p)
23450 draw_glyphs (w, 0, row, TEXT_AREA,
23451 0, row->used[TEXT_AREA],
23452 DRAW_NORMAL_TEXT, 0);
23453 else
23455 if (row->used[LEFT_MARGIN_AREA])
23456 expose_area (w, row, r, LEFT_MARGIN_AREA);
23457 if (row->used[TEXT_AREA])
23458 expose_area (w, row, r, TEXT_AREA);
23459 if (row->used[RIGHT_MARGIN_AREA])
23460 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23461 draw_row_fringe_bitmaps (w, row);
23464 return row->mouse_face_p;
23468 /* Redraw those parts of glyphs rows during expose event handling that
23469 overlap other rows. Redrawing of an exposed line writes over parts
23470 of lines overlapping that exposed line; this function fixes that.
23472 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23473 row in W's current matrix that is exposed and overlaps other rows.
23474 LAST_OVERLAPPING_ROW is the last such row. */
23476 static void
23477 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23478 struct window *w;
23479 struct glyph_row *first_overlapping_row;
23480 struct glyph_row *last_overlapping_row;
23482 struct glyph_row *row;
23484 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23485 if (row->overlapping_p)
23487 xassert (row->enabled_p && !row->mode_line_p);
23489 if (row->used[LEFT_MARGIN_AREA])
23490 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23492 if (row->used[TEXT_AREA])
23493 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23495 if (row->used[RIGHT_MARGIN_AREA])
23496 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23501 /* Return non-zero if W's cursor intersects rectangle R. */
23503 static int
23504 phys_cursor_in_rect_p (w, r)
23505 struct window *w;
23506 XRectangle *r;
23508 XRectangle cr, result;
23509 struct glyph *cursor_glyph;
23510 struct glyph_row *row;
23512 if (w->phys_cursor.vpos >= 0
23513 && w->phys_cursor.vpos < w->current_matrix->nrows
23514 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23515 row->enabled_p)
23516 && row->cursor_in_fringe_p)
23518 /* Cursor is in the fringe. */
23519 cr.x = window_box_right_offset (w,
23520 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23521 ? RIGHT_MARGIN_AREA
23522 : TEXT_AREA));
23523 cr.y = row->y;
23524 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23525 cr.height = row->height;
23526 return x_intersect_rectangles (&cr, r, &result);
23529 cursor_glyph = get_phys_cursor_glyph (w);
23530 if (cursor_glyph)
23532 /* r is relative to W's box, but w->phys_cursor.x is relative
23533 to left edge of W's TEXT area. Adjust it. */
23534 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23535 cr.y = w->phys_cursor.y;
23536 cr.width = cursor_glyph->pixel_width;
23537 cr.height = w->phys_cursor_height;
23538 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23539 I assume the effect is the same -- and this is portable. */
23540 return x_intersect_rectangles (&cr, r, &result);
23542 else
23543 return 0;
23547 /* EXPORT:
23548 Draw a vertical window border to the right of window W if W doesn't
23549 have vertical scroll bars. */
23551 void
23552 x_draw_vertical_border (w)
23553 struct window *w;
23555 /* We could do better, if we knew what type of scroll-bar the adjacent
23556 windows (on either side) have... But we don't :-(
23557 However, I think this works ok. ++KFS 2003-04-25 */
23559 /* Redraw borders between horizontally adjacent windows. Don't
23560 do it for frames with vertical scroll bars because either the
23561 right scroll bar of a window, or the left scroll bar of its
23562 neighbor will suffice as a border. */
23563 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23564 return;
23566 if (!WINDOW_RIGHTMOST_P (w)
23567 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23569 int x0, x1, y0, y1;
23571 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23572 y1 -= 1;
23574 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23575 x1 -= 1;
23577 rif->draw_vertical_window_border (w, x1, y0, y1);
23579 else if (!WINDOW_LEFTMOST_P (w)
23580 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23582 int x0, x1, y0, y1;
23584 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23585 y1 -= 1;
23587 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23588 x0 -= 1;
23590 rif->draw_vertical_window_border (w, x0, y0, y1);
23595 /* Redraw the part of window W intersection rectangle FR. Pixel
23596 coordinates in FR are frame-relative. Call this function with
23597 input blocked. Value is non-zero if the exposure overwrites
23598 mouse-face. */
23600 static int
23601 expose_window (w, fr)
23602 struct window *w;
23603 XRectangle *fr;
23605 struct frame *f = XFRAME (w->frame);
23606 XRectangle wr, r;
23607 int mouse_face_overwritten_p = 0;
23609 /* If window is not yet fully initialized, do nothing. This can
23610 happen when toolkit scroll bars are used and a window is split.
23611 Reconfiguring the scroll bar will generate an expose for a newly
23612 created window. */
23613 if (w->current_matrix == NULL)
23614 return 0;
23616 /* When we're currently updating the window, display and current
23617 matrix usually don't agree. Arrange for a thorough display
23618 later. */
23619 if (w == updated_window)
23621 SET_FRAME_GARBAGED (f);
23622 return 0;
23625 /* Frame-relative pixel rectangle of W. */
23626 wr.x = WINDOW_LEFT_EDGE_X (w);
23627 wr.y = WINDOW_TOP_EDGE_Y (w);
23628 wr.width = WINDOW_TOTAL_WIDTH (w);
23629 wr.height = WINDOW_TOTAL_HEIGHT (w);
23631 if (x_intersect_rectangles (fr, &wr, &r))
23633 int yb = window_text_bottom_y (w);
23634 struct glyph_row *row;
23635 int cursor_cleared_p;
23636 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23638 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23639 r.x, r.y, r.width, r.height));
23641 /* Convert to window coordinates. */
23642 r.x -= WINDOW_LEFT_EDGE_X (w);
23643 r.y -= WINDOW_TOP_EDGE_Y (w);
23645 /* Turn off the cursor. */
23646 if (!w->pseudo_window_p
23647 && phys_cursor_in_rect_p (w, &r))
23649 x_clear_cursor (w);
23650 cursor_cleared_p = 1;
23652 else
23653 cursor_cleared_p = 0;
23655 /* Update lines intersecting rectangle R. */
23656 first_overlapping_row = last_overlapping_row = NULL;
23657 for (row = w->current_matrix->rows;
23658 row->enabled_p;
23659 ++row)
23661 int y0 = row->y;
23662 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23664 if ((y0 >= r.y && y0 < r.y + r.height)
23665 || (y1 > r.y && y1 < r.y + r.height)
23666 || (r.y >= y0 && r.y < y1)
23667 || (r.y + r.height > y0 && r.y + r.height < y1))
23669 /* A header line may be overlapping, but there is no need
23670 to fix overlapping areas for them. KFS 2005-02-12 */
23671 if (row->overlapping_p && !row->mode_line_p)
23673 if (first_overlapping_row == NULL)
23674 first_overlapping_row = row;
23675 last_overlapping_row = row;
23678 if (expose_line (w, row, &r))
23679 mouse_face_overwritten_p = 1;
23682 if (y1 >= yb)
23683 break;
23686 /* Display the mode line if there is one. */
23687 if (WINDOW_WANTS_MODELINE_P (w)
23688 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23689 row->enabled_p)
23690 && row->y < r.y + r.height)
23692 if (expose_line (w, row, &r))
23693 mouse_face_overwritten_p = 1;
23696 if (!w->pseudo_window_p)
23698 /* Fix the display of overlapping rows. */
23699 if (first_overlapping_row)
23700 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23702 /* Draw border between windows. */
23703 x_draw_vertical_border (w);
23705 /* Turn the cursor on again. */
23706 if (cursor_cleared_p)
23707 update_window_cursor (w, 1);
23711 return mouse_face_overwritten_p;
23716 /* Redraw (parts) of all windows in the window tree rooted at W that
23717 intersect R. R contains frame pixel coordinates. Value is
23718 non-zero if the exposure overwrites mouse-face. */
23720 static int
23721 expose_window_tree (w, r)
23722 struct window *w;
23723 XRectangle *r;
23725 struct frame *f = XFRAME (w->frame);
23726 int mouse_face_overwritten_p = 0;
23728 while (w && !FRAME_GARBAGED_P (f))
23730 if (!NILP (w->hchild))
23731 mouse_face_overwritten_p
23732 |= expose_window_tree (XWINDOW (w->hchild), r);
23733 else if (!NILP (w->vchild))
23734 mouse_face_overwritten_p
23735 |= expose_window_tree (XWINDOW (w->vchild), r);
23736 else
23737 mouse_face_overwritten_p |= expose_window (w, r);
23739 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23742 return mouse_face_overwritten_p;
23746 /* EXPORT:
23747 Redisplay an exposed area of frame F. X and Y are the upper-left
23748 corner of the exposed rectangle. W and H are width and height of
23749 the exposed area. All are pixel values. W or H zero means redraw
23750 the entire frame. */
23752 void
23753 expose_frame (f, x, y, w, h)
23754 struct frame *f;
23755 int x, y, w, h;
23757 XRectangle r;
23758 int mouse_face_overwritten_p = 0;
23760 TRACE ((stderr, "expose_frame "));
23762 /* No need to redraw if frame will be redrawn soon. */
23763 if (FRAME_GARBAGED_P (f))
23765 TRACE ((stderr, " garbaged\n"));
23766 return;
23769 /* If basic faces haven't been realized yet, there is no point in
23770 trying to redraw anything. This can happen when we get an expose
23771 event while Emacs is starting, e.g. by moving another window. */
23772 if (FRAME_FACE_CACHE (f) == NULL
23773 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23775 TRACE ((stderr, " no faces\n"));
23776 return;
23779 if (w == 0 || h == 0)
23781 r.x = r.y = 0;
23782 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23783 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23785 else
23787 r.x = x;
23788 r.y = y;
23789 r.width = w;
23790 r.height = h;
23793 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23794 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23796 if (WINDOWP (f->tool_bar_window))
23797 mouse_face_overwritten_p
23798 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23800 #ifdef HAVE_X_WINDOWS
23801 #ifndef MSDOS
23802 #ifndef USE_X_TOOLKIT
23803 if (WINDOWP (f->menu_bar_window))
23804 mouse_face_overwritten_p
23805 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23806 #endif /* not USE_X_TOOLKIT */
23807 #endif
23808 #endif
23810 /* Some window managers support a focus-follows-mouse style with
23811 delayed raising of frames. Imagine a partially obscured frame,
23812 and moving the mouse into partially obscured mouse-face on that
23813 frame. The visible part of the mouse-face will be highlighted,
23814 then the WM raises the obscured frame. With at least one WM, KDE
23815 2.1, Emacs is not getting any event for the raising of the frame
23816 (even tried with SubstructureRedirectMask), only Expose events.
23817 These expose events will draw text normally, i.e. not
23818 highlighted. Which means we must redo the highlight here.
23819 Subsume it under ``we love X''. --gerd 2001-08-15 */
23820 /* Included in Windows version because Windows most likely does not
23821 do the right thing if any third party tool offers
23822 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23823 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23825 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23826 if (f == dpyinfo->mouse_face_mouse_frame)
23828 int x = dpyinfo->mouse_face_mouse_x;
23829 int y = dpyinfo->mouse_face_mouse_y;
23830 clear_mouse_face (dpyinfo);
23831 note_mouse_highlight (f, x, y);
23837 /* EXPORT:
23838 Determine the intersection of two rectangles R1 and R2. Return
23839 the intersection in *RESULT. Value is non-zero if RESULT is not
23840 empty. */
23843 x_intersect_rectangles (r1, r2, result)
23844 XRectangle *r1, *r2, *result;
23846 XRectangle *left, *right;
23847 XRectangle *upper, *lower;
23848 int intersection_p = 0;
23850 /* Rearrange so that R1 is the left-most rectangle. */
23851 if (r1->x < r2->x)
23852 left = r1, right = r2;
23853 else
23854 left = r2, right = r1;
23856 /* X0 of the intersection is right.x0, if this is inside R1,
23857 otherwise there is no intersection. */
23858 if (right->x <= left->x + left->width)
23860 result->x = right->x;
23862 /* The right end of the intersection is the minimum of the
23863 the right ends of left and right. */
23864 result->width = (min (left->x + left->width, right->x + right->width)
23865 - result->x);
23867 /* Same game for Y. */
23868 if (r1->y < r2->y)
23869 upper = r1, lower = r2;
23870 else
23871 upper = r2, lower = r1;
23873 /* The upper end of the intersection is lower.y0, if this is inside
23874 of upper. Otherwise, there is no intersection. */
23875 if (lower->y <= upper->y + upper->height)
23877 result->y = lower->y;
23879 /* The lower end of the intersection is the minimum of the lower
23880 ends of upper and lower. */
23881 result->height = (min (lower->y + lower->height,
23882 upper->y + upper->height)
23883 - result->y);
23884 intersection_p = 1;
23888 return intersection_p;
23891 #endif /* HAVE_WINDOW_SYSTEM */
23894 /***********************************************************************
23895 Initialization
23896 ***********************************************************************/
23898 void
23899 syms_of_xdisp ()
23901 Vwith_echo_area_save_vector = Qnil;
23902 staticpro (&Vwith_echo_area_save_vector);
23904 Vmessage_stack = Qnil;
23905 staticpro (&Vmessage_stack);
23907 Qinhibit_redisplay = intern ("inhibit-redisplay");
23908 staticpro (&Qinhibit_redisplay);
23910 message_dolog_marker1 = Fmake_marker ();
23911 staticpro (&message_dolog_marker1);
23912 message_dolog_marker2 = Fmake_marker ();
23913 staticpro (&message_dolog_marker2);
23914 message_dolog_marker3 = Fmake_marker ();
23915 staticpro (&message_dolog_marker3);
23917 #if GLYPH_DEBUG
23918 defsubr (&Sdump_frame_glyph_matrix);
23919 defsubr (&Sdump_glyph_matrix);
23920 defsubr (&Sdump_glyph_row);
23921 defsubr (&Sdump_tool_bar_row);
23922 defsubr (&Strace_redisplay);
23923 defsubr (&Strace_to_stderr);
23924 #endif
23925 #ifdef HAVE_WINDOW_SYSTEM
23926 defsubr (&Stool_bar_lines_needed);
23927 defsubr (&Slookup_image_map);
23928 #endif
23929 defsubr (&Sformat_mode_line);
23931 staticpro (&Qmenu_bar_update_hook);
23932 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23934 staticpro (&Qoverriding_terminal_local_map);
23935 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23937 staticpro (&Qoverriding_local_map);
23938 Qoverriding_local_map = intern ("overriding-local-map");
23940 staticpro (&Qwindow_scroll_functions);
23941 Qwindow_scroll_functions = intern ("window-scroll-functions");
23943 staticpro (&Qredisplay_end_trigger_functions);
23944 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23946 staticpro (&Qinhibit_point_motion_hooks);
23947 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23949 QCdata = intern (":data");
23950 staticpro (&QCdata);
23951 Qdisplay = intern ("display");
23952 staticpro (&Qdisplay);
23953 Qspace_width = intern ("space-width");
23954 staticpro (&Qspace_width);
23955 Qraise = intern ("raise");
23956 staticpro (&Qraise);
23957 Qslice = intern ("slice");
23958 staticpro (&Qslice);
23959 Qspace = intern ("space");
23960 staticpro (&Qspace);
23961 Qmargin = intern ("margin");
23962 staticpro (&Qmargin);
23963 Qpointer = intern ("pointer");
23964 staticpro (&Qpointer);
23965 Qleft_margin = intern ("left-margin");
23966 staticpro (&Qleft_margin);
23967 Qright_margin = intern ("right-margin");
23968 staticpro (&Qright_margin);
23969 Qcenter = intern ("center");
23970 staticpro (&Qcenter);
23971 Qline_height = intern ("line-height");
23972 staticpro (&Qline_height);
23973 QCalign_to = intern (":align-to");
23974 staticpro (&QCalign_to);
23975 QCrelative_width = intern (":relative-width");
23976 staticpro (&QCrelative_width);
23977 QCrelative_height = intern (":relative-height");
23978 staticpro (&QCrelative_height);
23979 QCeval = intern (":eval");
23980 staticpro (&QCeval);
23981 QCpropertize = intern (":propertize");
23982 staticpro (&QCpropertize);
23983 QCfile = intern (":file");
23984 staticpro (&QCfile);
23985 Qfontified = intern ("fontified");
23986 staticpro (&Qfontified);
23987 Qfontification_functions = intern ("fontification-functions");
23988 staticpro (&Qfontification_functions);
23989 Qtrailing_whitespace = intern ("trailing-whitespace");
23990 staticpro (&Qtrailing_whitespace);
23991 Qescape_glyph = intern ("escape-glyph");
23992 staticpro (&Qescape_glyph);
23993 Qnobreak_space = intern ("nobreak-space");
23994 staticpro (&Qnobreak_space);
23995 Qimage = intern ("image");
23996 staticpro (&Qimage);
23997 QCmap = intern (":map");
23998 staticpro (&QCmap);
23999 QCpointer = intern (":pointer");
24000 staticpro (&QCpointer);
24001 Qrect = intern ("rect");
24002 staticpro (&Qrect);
24003 Qcircle = intern ("circle");
24004 staticpro (&Qcircle);
24005 Qpoly = intern ("poly");
24006 staticpro (&Qpoly);
24007 Qmessage_truncate_lines = intern ("message-truncate-lines");
24008 staticpro (&Qmessage_truncate_lines);
24009 Qgrow_only = intern ("grow-only");
24010 staticpro (&Qgrow_only);
24011 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24012 staticpro (&Qinhibit_menubar_update);
24013 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24014 staticpro (&Qinhibit_eval_during_redisplay);
24015 Qposition = intern ("position");
24016 staticpro (&Qposition);
24017 Qbuffer_position = intern ("buffer-position");
24018 staticpro (&Qbuffer_position);
24019 Qobject = intern ("object");
24020 staticpro (&Qobject);
24021 Qbar = intern ("bar");
24022 staticpro (&Qbar);
24023 Qhbar = intern ("hbar");
24024 staticpro (&Qhbar);
24025 Qbox = intern ("box");
24026 staticpro (&Qbox);
24027 Qhollow = intern ("hollow");
24028 staticpro (&Qhollow);
24029 Qhand = intern ("hand");
24030 staticpro (&Qhand);
24031 Qarrow = intern ("arrow");
24032 staticpro (&Qarrow);
24033 Qtext = intern ("text");
24034 staticpro (&Qtext);
24035 Qrisky_local_variable = intern ("risky-local-variable");
24036 staticpro (&Qrisky_local_variable);
24037 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24038 staticpro (&Qinhibit_free_realized_faces);
24040 list_of_error = Fcons (Fcons (intern ("error"),
24041 Fcons (intern ("void-variable"), Qnil)),
24042 Qnil);
24043 staticpro (&list_of_error);
24045 Qlast_arrow_position = intern ("last-arrow-position");
24046 staticpro (&Qlast_arrow_position);
24047 Qlast_arrow_string = intern ("last-arrow-string");
24048 staticpro (&Qlast_arrow_string);
24050 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24051 staticpro (&Qoverlay_arrow_string);
24052 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24053 staticpro (&Qoverlay_arrow_bitmap);
24055 echo_buffer[0] = echo_buffer[1] = Qnil;
24056 staticpro (&echo_buffer[0]);
24057 staticpro (&echo_buffer[1]);
24059 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24060 staticpro (&echo_area_buffer[0]);
24061 staticpro (&echo_area_buffer[1]);
24063 Vmessages_buffer_name = build_string ("*Messages*");
24064 staticpro (&Vmessages_buffer_name);
24066 mode_line_proptrans_alist = Qnil;
24067 staticpro (&mode_line_proptrans_alist);
24068 mode_line_string_list = Qnil;
24069 staticpro (&mode_line_string_list);
24070 mode_line_string_face = Qnil;
24071 staticpro (&mode_line_string_face);
24072 mode_line_string_face_prop = Qnil;
24073 staticpro (&mode_line_string_face_prop);
24074 Vmode_line_unwind_vector = Qnil;
24075 staticpro (&Vmode_line_unwind_vector);
24077 help_echo_string = Qnil;
24078 staticpro (&help_echo_string);
24079 help_echo_object = Qnil;
24080 staticpro (&help_echo_object);
24081 help_echo_window = Qnil;
24082 staticpro (&help_echo_window);
24083 previous_help_echo_string = Qnil;
24084 staticpro (&previous_help_echo_string);
24085 help_echo_pos = -1;
24087 #ifdef HAVE_WINDOW_SYSTEM
24088 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24089 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24090 For example, if a block cursor is over a tab, it will be drawn as
24091 wide as that tab on the display. */);
24092 x_stretch_cursor_p = 0;
24093 #endif
24095 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24096 doc: /* *Non-nil means highlight trailing whitespace.
24097 The face used for trailing whitespace is `trailing-whitespace'. */);
24098 Vshow_trailing_whitespace = Qnil;
24100 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24101 doc: /* *Control highlighting of nobreak space and soft hyphen.
24102 A value of t means highlight the character itself (for nobreak space,
24103 use face `nobreak-space').
24104 A value of nil means no highlighting.
24105 Other values mean display the escape glyph followed by an ordinary
24106 space or ordinary hyphen. */);
24107 Vnobreak_char_display = Qt;
24109 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24110 doc: /* *The pointer shape to show in void text areas.
24111 A value of nil means to show the text pointer. Other options are `arrow',
24112 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24113 Vvoid_text_area_pointer = Qarrow;
24115 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24116 doc: /* Non-nil means don't actually do any redisplay.
24117 This is used for internal purposes. */);
24118 Vinhibit_redisplay = Qnil;
24120 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24121 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24122 Vglobal_mode_string = Qnil;
24124 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24125 doc: /* Marker for where to display an arrow on top of the buffer text.
24126 This must be the beginning of a line in order to work.
24127 See also `overlay-arrow-string'. */);
24128 Voverlay_arrow_position = Qnil;
24130 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24131 doc: /* String to display as an arrow in non-window frames.
24132 See also `overlay-arrow-position'. */);
24133 Voverlay_arrow_string = build_string ("=>");
24135 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24136 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24137 The symbols on this list are examined during redisplay to determine
24138 where to display overlay arrows. */);
24139 Voverlay_arrow_variable_list
24140 = Fcons (intern ("overlay-arrow-position"), Qnil);
24142 DEFVAR_INT ("scroll-step", &scroll_step,
24143 doc: /* *The number of lines to try scrolling a window by when point moves out.
24144 If that fails to bring point back on frame, point is centered instead.
24145 If this is zero, point is always centered after it moves off frame.
24146 If you want scrolling to always be a line at a time, you should set
24147 `scroll-conservatively' to a large value rather than set this to 1. */);
24149 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24150 doc: /* *Scroll up to this many lines, to bring point back on screen.
24151 If point moves off-screen, redisplay will scroll by up to
24152 `scroll-conservatively' lines in order to bring point just barely
24153 onto the screen again. If that cannot be done, then redisplay
24154 recenters point as usual.
24156 A value of zero means always recenter point if it moves off screen. */);
24157 scroll_conservatively = 0;
24159 DEFVAR_INT ("scroll-margin", &scroll_margin,
24160 doc: /* *Number of lines of margin at the top and bottom of a window.
24161 Recenter the window whenever point gets within this many lines
24162 of the top or bottom of the window. */);
24163 scroll_margin = 0;
24165 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24166 doc: /* Pixels per inch value for non-window system displays.
24167 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24168 Vdisplay_pixels_per_inch = make_float (72.0);
24170 #if GLYPH_DEBUG
24171 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24172 #endif
24174 DEFVAR_BOOL ("truncate-partial-width-windows",
24175 &truncate_partial_width_windows,
24176 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24177 truncate_partial_width_windows = 1;
24179 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24180 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24181 Any other value means to use the appropriate face, `mode-line',
24182 `header-line', or `menu' respectively. */);
24183 mode_line_inverse_video = 1;
24185 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24186 doc: /* *Maximum buffer size for which line number should be displayed.
24187 If the buffer is bigger than this, the line number does not appear
24188 in the mode line. A value of nil means no limit. */);
24189 Vline_number_display_limit = Qnil;
24191 DEFVAR_INT ("line-number-display-limit-width",
24192 &line_number_display_limit_width,
24193 doc: /* *Maximum line width (in characters) for line number display.
24194 If the average length of the lines near point is bigger than this, then the
24195 line number may be omitted from the mode line. */);
24196 line_number_display_limit_width = 200;
24198 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24199 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24200 highlight_nonselected_windows = 0;
24202 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24203 doc: /* Non-nil if more than one frame is visible on this display.
24204 Minibuffer-only frames don't count, but iconified frames do.
24205 This variable is not guaranteed to be accurate except while processing
24206 `frame-title-format' and `icon-title-format'. */);
24208 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24209 doc: /* Template for displaying the title bar of visible frames.
24210 \(Assuming the window manager supports this feature.)
24212 This variable has the same structure as `mode-line-format', except that
24213 the %c and %l constructs are ignored. It is used only on frames for
24214 which no explicit name has been set \(see `modify-frame-parameters'). */);
24216 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24217 doc: /* Template for displaying the title bar of an iconified frame.
24218 \(Assuming the window manager supports this feature.)
24219 This variable has the same structure as `mode-line-format' (which see),
24220 and is used only on frames for which no explicit name has been set
24221 \(see `modify-frame-parameters'). */);
24222 Vicon_title_format
24223 = Vframe_title_format
24224 = Fcons (intern ("multiple-frames"),
24225 Fcons (build_string ("%b"),
24226 Fcons (Fcons (empty_string,
24227 Fcons (intern ("invocation-name"),
24228 Fcons (build_string ("@"),
24229 Fcons (intern ("system-name"),
24230 Qnil)))),
24231 Qnil)));
24233 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24234 doc: /* Maximum number of lines to keep in the message log buffer.
24235 If nil, disable message logging. If t, log messages but don't truncate
24236 the buffer when it becomes large. */);
24237 Vmessage_log_max = make_number (100);
24239 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24240 doc: /* Functions called before redisplay, if window sizes have changed.
24241 The value should be a list of functions that take one argument.
24242 Just before redisplay, for each frame, if any of its windows have changed
24243 size since the last redisplay, or have been split or deleted,
24244 all the functions in the list are called, with the frame as argument. */);
24245 Vwindow_size_change_functions = Qnil;
24247 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24248 doc: /* List of functions to call before redisplaying a window with scrolling.
24249 Each function is called with two arguments, the window
24250 and its new display-start position. Note that the value of `window-end'
24251 is not valid when these functions are called. */);
24252 Vwindow_scroll_functions = Qnil;
24254 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24255 doc: /* Functions called when redisplay of a window reaches the end trigger.
24256 Each function is called with two arguments, the window and the end trigger value.
24257 See `set-window-redisplay-end-trigger'. */);
24258 Vredisplay_end_trigger_functions = Qnil;
24260 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24261 doc: /* *Non-nil means autoselect window with mouse pointer.
24262 If nil, do not autoselect windows.
24263 A positive number means delay autoselection by that many seconds: a
24264 window is autoselected only after the mouse has remained in that
24265 window for the duration of the delay.
24266 A negative number has a similar effect, but causes windows to be
24267 autoselected only after the mouse has stopped moving. \(Because of
24268 the way Emacs compares mouse events, you will occasionally wait twice
24269 that time before the window gets selected.\)
24270 Any other value means to autoselect window instantaneously when the
24271 mouse pointer enters it.
24273 Autoselection selects the minibuffer only if it is active, and never
24274 unselects the minibuffer if it is active.
24276 When customizing this variable make sure that the actual value of
24277 `focus-follows-mouse' matches the behavior of your window manager. */);
24278 Vmouse_autoselect_window = Qnil;
24280 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24281 doc: /* *Non-nil means automatically resize tool-bars.
24282 This dynamically changes the tool-bar's height to the minimum height
24283 that is needed to make all tool-bar items visible.
24284 If value is `grow-only', the tool-bar's height is only increased
24285 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24286 Vauto_resize_tool_bars = Qt;
24288 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24289 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24290 auto_raise_tool_bar_buttons_p = 1;
24292 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24293 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24294 make_cursor_line_fully_visible_p = 1;
24296 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24297 doc: /* *Border below tool-bar in pixels.
24298 If an integer, use it as the height of the border.
24299 If it is one of `internal-border-width' or `border-width', use the
24300 value of the corresponding frame parameter.
24301 Otherwise, no border is added below the tool-bar. */);
24302 Vtool_bar_border = Qinternal_border_width;
24304 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24305 doc: /* *Margin around tool-bar buttons in pixels.
24306 If an integer, use that for both horizontal and vertical margins.
24307 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24308 HORZ specifying the horizontal margin, and VERT specifying the
24309 vertical margin. */);
24310 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24312 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24313 doc: /* *Relief thickness of tool-bar buttons. */);
24314 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24316 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24317 doc: /* List of functions to call to fontify regions of text.
24318 Each function is called with one argument POS. Functions must
24319 fontify a region starting at POS in the current buffer, and give
24320 fontified regions the property `fontified'. */);
24321 Vfontification_functions = Qnil;
24322 Fmake_variable_buffer_local (Qfontification_functions);
24324 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24325 &unibyte_display_via_language_environment,
24326 doc: /* *Non-nil means display unibyte text according to language environment.
24327 Specifically this means that unibyte non-ASCII characters
24328 are displayed by converting them to the equivalent multibyte characters
24329 according to the current language environment. As a result, they are
24330 displayed according to the current fontset. */);
24331 unibyte_display_via_language_environment = 0;
24333 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24334 doc: /* *Maximum height for resizing mini-windows.
24335 If a float, it specifies a fraction of the mini-window frame's height.
24336 If an integer, it specifies a number of lines. */);
24337 Vmax_mini_window_height = make_float (0.25);
24339 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24340 doc: /* *How to resize mini-windows.
24341 A value of nil means don't automatically resize mini-windows.
24342 A value of t means resize them to fit the text displayed in them.
24343 A value of `grow-only', the default, means let mini-windows grow
24344 only, until their display becomes empty, at which point the windows
24345 go back to their normal size. */);
24346 Vresize_mini_windows = Qgrow_only;
24348 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24349 doc: /* Alist specifying how to blink the cursor off.
24350 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24351 `cursor-type' frame-parameter or variable equals ON-STATE,
24352 comparing using `equal', Emacs uses OFF-STATE to specify
24353 how to blink it off. ON-STATE and OFF-STATE are values for
24354 the `cursor-type' frame parameter.
24356 If a frame's ON-STATE has no entry in this list,
24357 the frame's other specifications determine how to blink the cursor off. */);
24358 Vblink_cursor_alist = Qnil;
24360 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24361 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24362 automatic_hscrolling_p = 1;
24364 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24365 doc: /* *How many columns away from the window edge point is allowed to get
24366 before automatic hscrolling will horizontally scroll the window. */);
24367 hscroll_margin = 5;
24369 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24370 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24371 When point is less than `hscroll-margin' columns from the window
24372 edge, automatic hscrolling will scroll the window by the amount of columns
24373 determined by this variable. If its value is a positive integer, scroll that
24374 many columns. If it's a positive floating-point number, it specifies the
24375 fraction of the window's width to scroll. If it's nil or zero, point will be
24376 centered horizontally after the scroll. Any other value, including negative
24377 numbers, are treated as if the value were zero.
24379 Automatic hscrolling always moves point outside the scroll margin, so if
24380 point was more than scroll step columns inside the margin, the window will
24381 scroll more than the value given by the scroll step.
24383 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24384 and `scroll-right' overrides this variable's effect. */);
24385 Vhscroll_step = make_number (0);
24387 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24388 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24389 Bind this around calls to `message' to let it take effect. */);
24390 message_truncate_lines = 0;
24392 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24393 doc: /* Normal hook run to update the menu bar definitions.
24394 Redisplay runs this hook before it redisplays the menu bar.
24395 This is used to update submenus such as Buffers,
24396 whose contents depend on various data. */);
24397 Vmenu_bar_update_hook = Qnil;
24399 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24400 doc: /* Frame for which we are updating a menu.
24401 The enable predicate for a menu binding should check this variable. */);
24402 Vmenu_updating_frame = Qnil;
24404 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24405 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24406 inhibit_menubar_update = 0;
24408 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24409 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24410 inhibit_eval_during_redisplay = 0;
24412 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24413 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24414 inhibit_free_realized_faces = 0;
24416 #if GLYPH_DEBUG
24417 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24418 doc: /* Inhibit try_window_id display optimization. */);
24419 inhibit_try_window_id = 0;
24421 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24422 doc: /* Inhibit try_window_reusing display optimization. */);
24423 inhibit_try_window_reusing = 0;
24425 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24426 doc: /* Inhibit try_cursor_movement display optimization. */);
24427 inhibit_try_cursor_movement = 0;
24428 #endif /* GLYPH_DEBUG */
24430 DEFVAR_INT ("overline-margin", &overline_margin,
24431 doc: /* *Space between overline and text, in pixels.
24432 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24433 margin to the caracter height. */);
24434 overline_margin = 2;
24438 /* Initialize this module when Emacs starts. */
24440 void
24441 init_xdisp ()
24443 Lisp_Object root_window;
24444 struct window *mini_w;
24446 current_header_line_height = current_mode_line_height = -1;
24448 CHARPOS (this_line_start_pos) = 0;
24450 mini_w = XWINDOW (minibuf_window);
24451 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24453 if (!noninteractive)
24455 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24456 int i;
24458 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24459 set_window_height (root_window,
24460 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24462 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24463 set_window_height (minibuf_window, 1, 0);
24465 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24466 mini_w->total_cols = make_number (FRAME_COLS (f));
24468 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24469 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24470 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24472 /* The default ellipsis glyphs `...'. */
24473 for (i = 0; i < 3; ++i)
24474 default_invis_vector[i] = make_number ('.');
24478 /* Allocate the buffer for frame titles.
24479 Also used for `format-mode-line'. */
24480 int size = 100;
24481 mode_line_noprop_buf = (char *) xmalloc (size);
24482 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24483 mode_line_noprop_ptr = mode_line_noprop_buf;
24484 mode_line_target = MODE_LINE_DISPLAY;
24487 help_echo_showing_p = 0;
24491 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24492 (do not change this comment) */